Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(window): add setEffects API #392

Merged
merged 5 commits into from
Jun 18, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/window-set-effects.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"window": "patch"
"window-js": "patch"
---

Added the `setEffects` and `clearEffects` API.
21 changes: 7 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ resolver = "2"
[workspace.dependencies]
serde = { version = "1", features = ["derive"] }
log = "0.4"
tauri = "2.0.0-alpha.9"
tauri-build = "2.0.0-alpha.5"
tauri = { git = "https://github.com/tauri-apps/tauri", branch = "next" }
tauri-build = { git = "https://github.com/tauri-apps/tauri", branch = "next" }
serde_json = "1"
thiserror = "1"

Expand Down
7 changes: 6 additions & 1 deletion examples/api/src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub fn run() {
#[cfg(desktop)]
{
window_builder = window_builder
.user_agent("Tauri API")
.user_agent(&format!("Tauri API - {}", std::env::consts::OS))
.title("Tauri API Validation")
.inner_size(1000., 800.)
.min_inner_size(600., 400.)
Expand All @@ -71,6 +71,11 @@ pub fn run() {
.decorations(false);
}

#[cfg(target_os = "macos")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not windows as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Windows is already handled on the block above.

{
window_builder = window_builder.transparent(true);
}

let window = window_builder.build().unwrap();

#[cfg(debug_assertions)]
Expand Down
136 changes: 136 additions & 0 deletions examples/api/src/views/Window.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
UserAttentionType,
PhysicalSize,
PhysicalPosition,
Effect,
EffectState,
} from "@tauri-apps/plugin-window";
import { open as openDialog } from "@tauri-apps/plugin-dialog";
import { open } from "@tauri-apps/plugin-shell";
Expand Down Expand Up @@ -57,7 +59,20 @@
"rowResize",
];

const windowsEffects = ["mica", "blur", "acrylic"];
const isWindows = navigator.appVersion.includes("windows");
const isMacOS = navigator.appVersion.includes("macos");
let effectOptions = isWindows
? windowsEffects
: Object.keys(Effect)
.map((effect) => Effect[effect])
.filter((e) => !windowsEffects.includes(e));
const effectStateOptions = Object.keys(EffectState).map(
(state) => EffectState[state]
);

export let onMessage;
const mainEl = document.querySelector("main");

let newWindowLabel;

Expand Down Expand Up @@ -91,6 +106,12 @@
let cursorIgnoreEvents = false;
let windowTitle = "Awesome Tauri Example!";

let effects = [];
let selectedEffect;
let effectState;
let effectRadius;
let effectR, effectG, effectB, effectA;

function openUrl() {
open(urlValue);
}
Expand Down Expand Up @@ -172,6 +193,38 @@
await windowMap[selectedWindow].requestUserAttention(null);
}

async function addEffect() {
if (!effects.includes(selectedEffect)) {
effects = [...effects, selectedEffect];
}

const payload = {
effects,
state: effectState,
radius: effectRadius,
};
if (
Number.isInteger(effectR) &&
Number.isInteger(effectG) &&
Number.isInteger(effectB) &&
Number.isInteger(effectA)
) {
payload.color = [effectR, effectG, effectB, effectA];
}

mainEl.classList.remove("bg-primary");
mainEl.classList.remove("dark:bg-darkPrimary");
await windowMap[selectedWindow].clearEffects();
await windowMap[selectedWindow].setEffects(payload);
}

async function clearEffects() {
effects = [];
await windowMap[selectedWindow].clearEffects();
mainEl.classList.add("bg-primary");
mainEl.classList.add("dark:bg-darkPrimary");
}

$: {
windowMap[selectedWindow];
loadWindowPosition();
Expand Down Expand Up @@ -455,5 +508,88 @@
<button class="btn" id="open-url"> Open URL </button>
</form>
</div>

<br />

{#if isWindows || isMacOS}
<div class="flex flex-col gap-1">
<div class="flex">
<label>
Effect
<select class="input" bind:value={selectedEffect}>
{#each effectOptions as effect}
<option value={effect}>{effect}</option>
{/each}
</select>
</label>

<label>
State
<select class="input" bind:value={effectState}>
{#each effectStateOptions as state}
<option value={state}>{state}</option>
{/each}
</select>
</label>

<label>
Radius
<input class="input" type="number" bind:value={effectRadius} />
</label>
</div>

<div class="flex">
<label>
Color
<div class="flex">
<input
style="max-width: 120px;"
class="input"
type="number"
placeholder="R"
bind:value={effectR}
/>
<input
style="max-width: 120px;"
class="input"
type="number"
placeholder="G"
bind:value={effectG}
/>
<input
style="max-width: 120px;"
class="input"
type="number"
placeholder="B"
bind:value={effectB}
/>
<input
style="max-width: 120px;"
class="input"
type="number"
placeholder="A"
bind:value={effectA}
/>
</div>
</label>
</div>

<div class="flex">
<button class="btn" style="width: 80px;" on:click={addEffect}
>Add</button
>
</div>

<div class="flex">
<div>
Applied effects: {effects.length ? effects.join(",") : "None"}
</div>

<button class="btn" style="width: 80px;" on:click={clearEffects}
>Clear</button
>
</div>
</div>
{/if}
{/if}
</div>
Loading