File size: 1,866 Bytes
2d8be8f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | <script>
import { writable, get } from "svelte/store";
import {
register as registerShortcut,
unregister as unregisterShortcut,
} from "@tauri-apps/plugin-global-shortcut";
export let onMessage;
const shortcuts = writable([]);
let shortcut = "CmdOrControl+X";
function register() {
const shortcut_ = shortcut;
registerShortcut(shortcut_, (e) => {
onMessage(`Shortcut ${shortcut_} triggered ${e.state}`);
})
.then(() => {
shortcuts.update((shortcuts_) => [...shortcuts_, shortcut_]);
onMessage(`Shortcut ${shortcut_} registered successfully`);
})
.catch(onMessage);
}
function unregister(shortcut) {
const shortcut_ = shortcut;
unregisterShortcut(shortcut_)
.then(() => {
shortcuts.update((shortcuts_) =>
shortcuts_.filter((s) => s !== shortcut_)
);
onMessage(`Shortcut ${shortcut_} unregistered`);
})
.catch(onMessage);
}
function unregisterAll() {
unregisterShortcut(get(shortcuts))
.then(() => {
shortcuts.update(() => []);
onMessage(`Unregistered all shortcuts`);
})
.catch(onMessage);
}
</script>
<div class="flex gap-1">
<input
class="input grow"
placeholder="Type a shortcut with '+' as separator..."
bind:value={shortcut}
/>
<button class="btn" type="button" on:click={register}>Register</button>
</div>
<br />
<div class="flex flex-col gap-1">
{#each $shortcuts as savedShortcut}
<div class="flex justify-between">
{savedShortcut}
<button
class="btn"
type="button"
on:click={() => unregister(savedShortcut)}>Unregister</button
>
</div>
{/each}
{#if $shortcuts.length > 1}
<br />
<button class="btn" type="button" on:click={unregisterAll}
>Unregister all</button
>
{/if}
</div>
|