@skathi-native/cli 0.1.0-beta.1 → 0.1.0-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/aot/host-globals.json +105 -0
  2. package/aot/native.ts +142 -0
  3. package/aot/pin.ts +67 -0
  4. package/aot/toolchain.lock.json +17 -0
  5. package/aot/transform.ts +69 -0
  6. package/cli/aot-build.ts +189 -0
  7. package/cli/build-app.ts +67 -25
  8. package/cli/config.ts +44 -101
  9. package/cli/create.ts +107 -234
  10. package/cli/help.ts +159 -0
  11. package/cli/host-fetch.ts +87 -48
  12. package/cli/host.ts +18 -8
  13. package/cli/sk.ts +35 -110
  14. package/cli/skpk.ts +4 -0
  15. package/package.json +4 -2
  16. package/templates/icon.png +0 -0
  17. package/templates/minimal/.vscode/settings.json +3 -0
  18. package/templates/minimal/assets/hello.txt +1 -0
  19. package/templates/minimal/src/App.vue +142 -0
  20. package/templates/minimal/src/main.ts +4 -0
  21. package/templates/showcase/.vscode/settings.json +3 -0
  22. package/templates/showcase/assets/hello.txt +1 -0
  23. package/templates/showcase/assets/media/aurora.png +0 -0
  24. package/templates/showcase/assets/media/clip.mp4 +0 -0
  25. package/templates/showcase/assets/media/sticker.tgs +0 -0
  26. package/templates/showcase/src/App.vue +128 -0
  27. package/templates/showcase/src/components/Card.vue +41 -0
  28. package/templates/showcase/src/components/Icon.vue +29 -0
  29. package/templates/showcase/src/components/Page.vue +41 -0
  30. package/templates/showcase/src/components/Titlebar.vue +132 -0
  31. package/templates/showcase/src/engine.ts +10 -0
  32. package/templates/showcase/src/icons.ts +22 -0
  33. package/templates/showcase/src/main.ts +7 -0
  34. package/templates/showcase/src/pages/AudioPage.vue +121 -0
  35. package/templates/showcase/src/pages/FilesPage.vue +87 -0
  36. package/templates/showcase/src/pages/FormsPage.vue +138 -0
  37. package/templates/showcase/src/pages/GraphicsPage.vue +154 -0
  38. package/templates/showcase/src/pages/HomePage.vue +140 -0
  39. package/templates/showcase/src/pages/LocalePage.vue +71 -0
  40. package/templates/showcase/src/pages/MediaPage.vue +85 -0
  41. package/templates/showcase/src/pages/MotionPage.vue +199 -0
  42. package/templates/showcase/src/pages/NetworkPage.vue +104 -0
  43. package/templates/showcase/src/pages/PermissionsPage.vue +80 -0
  44. package/templates/showcase/src/pages/StoragePage.vue +104 -0
  45. package/templates/showcase/src/pages/StylesPage.vue +199 -0
  46. package/templates/showcase/src/pages/SystemPage.vue +89 -0
  47. package/templates/showcase/src/pages/TrayPage.vue +66 -0
  48. package/templates/showcase/src/pages/WindowPage.vue +128 -0
  49. package/templates/showcase/src/product.ts +2 -0
  50. package/templates/showcase/src/router.ts +149 -0
  51. package/templates/showcase/src/stores/counter.ts +9 -0
  52. package/templates/showcase/src/stores/desktop.ts +115 -0
  53. package/templates/showcase/src/styles.css +140 -0
@@ -0,0 +1,89 @@
1
+ <script setup lang="ts">
2
+ import { appDevtools, appHost, appLaunch, appShell, appStorage, type SkLaunchEvent } from "@skathi-native/runtime";
3
+ import { onMounted, onUnmounted, ref } from "vue";
4
+ import Card from "../components/Card.vue";
5
+ import Page from "../components/Page.vue";
6
+ import { describe } from "../engine";
7
+
8
+ const status = ref("");
9
+ const launches = ref<string[]>([]);
10
+ const devtoolsOn = ref(appDevtools.available && appDevtools.isEnabled());
11
+
12
+ async function run(action: Promise<void>) {
13
+ try {
14
+ await action;
15
+ status.value = "";
16
+ } catch (e) {
17
+ status.value = describe(e);
18
+ }
19
+ }
20
+
21
+ // Fires in the running app when the exe is started again under "singleInstance": true, or opened by a URL it owns.
22
+ const onLaunch = (event: Event) => {
23
+ const { source, argv } = event as SkLaunchEvent;
24
+ launches.value = [`${source}: ${argv.join(" ")}`, ...launches.value].slice(0, 4);
25
+ };
26
+ onMounted(() => appLaunch.addEventListener("launch", onLaunch));
27
+ onUnmounted(() => appLaunch.removeEventListener("launch", onLaunch));
28
+
29
+ const off = appDevtools.onChange((on) => (devtoolsOn.value = on));
30
+ onUnmounted(off);
31
+ </script>
32
+
33
+ <template>
34
+ <Page title="System" lead="What a browser cannot do: open things with the system, read how the app was started.">
35
+ <Card title="Shell" api="appShell">
36
+ <div class="row">
37
+ <button class="btn" @click="run(appShell.openExternal('https://github.com/argon-chat/skathi'))">
38
+ Open the Skathi repository
39
+ </button>
40
+ <button v-if="appStorage.folder" class="btn" @click="run(appShell.openPath(appStorage.folder))">
41
+ Open the data folder
42
+ </button>
43
+ <span class="faint">{{ status }}</span>
44
+ </div>
45
+ <p class="faint">openPath refuses programs and scripts unless asked, and openExternal refuses file: URLs.</p>
46
+ </Card>
47
+
48
+ <Card title="Launch" api="appLaunch">
49
+ <div class="kv">
50
+ <span class="k">arguments</span><span class="v mono">{{ appLaunch.argv.slice(1).join(" ") || "none" }}</span>
51
+ </div>
52
+ <div class="kv">
53
+ <span class="k">working directory</span><span class="v mono">{{ appLaunch.cwd }}</span>
54
+ </div>
55
+ <div class="kv">
56
+ <span class="k">single instance</span><span class="v">{{ appLaunch.singleInstance ? "yes" : "no" }}</span>
57
+ </div>
58
+ <div v-for="line in launches" :key="line" class="kv">
59
+ <span class="k">launch event</span><span class="v mono">{{ line }}</span>
60
+ </div>
61
+ <p class="faint">
62
+ Set "singleInstance": true and "protocols": ["myapp"] in sk.config.json to receive later launches and myapp://
63
+ links here.
64
+ </p>
65
+ </Card>
66
+
67
+ <Card title="Devtools" api="appDevtools">
68
+ <div v-if="appDevtools.available" class="row">
69
+ <span class="pill" :class="{ ok: devtoolsOn }">{{ devtoolsOn ? "on" : "off" }}</span>
70
+ <button class="btn" :disabled="!devtoolsOn" @click="appDevtools.open()">Open (F12)</button>
71
+ <button class="btn" @click="devtoolsOn ? appDevtools.disable() : appDevtools.enable()">
72
+ {{ devtoolsOn ? "Turn off" : "Turn on" }}
73
+ </button>
74
+ </div>
75
+ <p v-else class="muted">This build leaves the devtools out (sk publish does unless you keep them).</p>
76
+ <p class="faint">Element tree, styles, console and network, attached to this window.</p>
77
+ </Card>
78
+
79
+ <Card title="Embedding" api="appHost">
80
+ <p class="muted">
81
+ {{
82
+ appHost.available
83
+ ? `Embedded in another program, which offers: ${appHost.functions.join(", ") || "no functions"}.`
84
+ : "Running as its own exe. A program that embeds Skathi as a library gives the app functions to call here."
85
+ }}
86
+ </p>
87
+ </Card>
88
+ </Page>
89
+ </template>
@@ -0,0 +1,66 @@
1
+ <script setup lang="ts">
2
+ import { appHotkeys, appTray } from "@skathi-native/runtime";
3
+ import { ref } from "vue";
4
+ import Card from "../components/Card.vue";
5
+ import Page from "../components/Page.vue";
6
+ import { describe } from "../engine";
7
+ import { useCounter } from "../stores/counter";
8
+ import { chordLabel, DEFAULT_CHORD, useDesktop } from "../stores/desktop";
9
+
10
+ const desktop = useDesktop();
11
+ const counter = useCounter();
12
+ const recording = ref(false);
13
+ const status = ref("");
14
+
15
+ // Records the next key combination anywhere on the system; Esc cancels.
16
+ async function record() {
17
+ recording.value = true;
18
+ status.value = "Press a combination…";
19
+ try {
20
+ const chord = await appHotkeys.captureOnce();
21
+ desktop.bindHotkey(chord);
22
+ status.value = "";
23
+ } catch (e) {
24
+ status.value = describe(e);
25
+ } finally {
26
+ recording.value = false;
27
+ }
28
+ }
29
+ </script>
30
+
31
+ <template>
32
+ <Page title="Tray & hotkeys" lead="Desktop integration that keeps working while the window is in the background.">
33
+ <Card title="Tray icon" api="appTray.set()">
34
+ <template v-if="appTray.available">
35
+ <div class="row">
36
+ <button class="btn" :class="{ on: desktop.trayShown }" @click="desktop.showTray(!desktop.trayShown)">
37
+ {{ desktop.trayShown ? "Remove the tray icon" : "Show a tray icon" }}
38
+ </button>
39
+ </div>
40
+ <p class="faint">
41
+ Its menu counts up, toggles always-on-top and quits; the label follows the count ({{ counter.count }}). The
42
+ icon is drawn from RGBA pixels, so it works in dev as well as in a build.
43
+ </p>
44
+ </template>
45
+ <p v-else class="muted">This platform has no tray in Skathi yet (Windows and macOS do).</p>
46
+ </Card>
47
+
48
+ <Card title="Global hotkey" api="appHotkeys">
49
+ <template v-if="appHotkeys.available">
50
+ <div class="row">
51
+ <span class="pill" :class="{ ok: desktop.chord }">
52
+ {{ desktop.chord ? chordLabel(desktop.chord) : "not bound" }}
53
+ </span>
54
+ <button class="btn" @click="desktop.bindHotkey(desktop.chord ? null : DEFAULT_CHORD)">
55
+ {{ desktop.chord ? "Unbind" : `Bind ${chordLabel(DEFAULT_CHORD)}` }}
56
+ </button>
57
+ <button v-if="!recording" class="btn" @click="record">Record another…</button>
58
+ <button v-else class="btn" @click="appHotkeys.cancelCapture()">Cancel</button>
59
+ </div>
60
+ <p class="muted">Pressed {{ desktop.presses }} times; each press counts up, from any app.</p>
61
+ <p v-if="status" class="faint">{{ status }}</p>
62
+ </template>
63
+ <p v-else class="muted">Global hotkeys need Windows or macOS.</p>
64
+ </Card>
65
+ </Page>
66
+ </template>
@@ -0,0 +1,128 @@
1
+ <script setup lang="ts">
2
+ import { appWindow, SkWindow } from "@skathi-native/runtime";
3
+ import { onMounted, onUnmounted, ref } from "vue";
4
+ import Card from "../components/Card.vue";
5
+ import Page from "../components/Page.vue";
6
+ import { useCounter } from "../stores/counter";
7
+
8
+ const counter = useCounter();
9
+
10
+ const title = ref(appWindow.title);
11
+ const size = ref("");
12
+ const onTop = ref(appWindow.alwaysOnTop);
13
+ const fullscreen = ref(appWindow.isFullscreen());
14
+ const backdrop = ref(appWindow.backdrop);
15
+ const popped = ref(false);
16
+
17
+ const BACKDROPS = ["mica", "mica-alt", "acrylic", "none"];
18
+ const SIZES = [
19
+ [900, 620],
20
+ [1100, 720],
21
+ [1280, 800],
22
+ ] as const;
23
+
24
+ // `resize` is the standard event on `window`; appWindow carries the state the web has no event for.
25
+ const read = () => {
26
+ size.value = `${Math.round(innerWidth)} × ${Math.round(innerHeight)}`;
27
+ fullscreen.value = appWindow.isFullscreen();
28
+ };
29
+ onMounted(() => {
30
+ read();
31
+ addEventListener("resize", read);
32
+ });
33
+ onUnmounted(() => removeEventListener("resize", read));
34
+
35
+ const setSize = (width: number, height: number) => {
36
+ appWindow.setBounds({ width, height });
37
+ appWindow.center();
38
+ };
39
+ const toggleOnTop = () => {
40
+ appWindow.alwaysOnTop = !appWindow.alwaysOnTop;
41
+ onTop.value = appWindow.alwaysOnTop;
42
+ };
43
+ const setBackdrop = (material: string) => {
44
+ appWindow.backdrop = material;
45
+ backdrop.value = appWindow.backdrop;
46
+ };
47
+ const toggleFullscreen = () => {
48
+ appWindow.setFullscreen(!appWindow.isFullscreen());
49
+ read();
50
+ };
51
+ </script>
52
+
53
+ <template>
54
+ <Page title="Window" lead="appWindow drives the OS window directly; every call is synchronous.">
55
+ <Card title="Title" api="appWindow.title">
56
+ <div class="row">
57
+ <input v-model="title" class="input wide" @keyup.enter="appWindow.title = title" />
58
+ <button class="btn" @click="appWindow.title = title">Set</button>
59
+ </div>
60
+ <p class="faint">Shown in the taskbar and Alt+Tab; this window paints its own caption.</p>
61
+ </Card>
62
+
63
+ <Card title="Size and state" :api="size">
64
+ <div class="row">
65
+ <button v-for="[w, h] in SIZES" :key="w" class="btn" @click="setSize(w, h)">{{ w }} × {{ h }}</button>
66
+ </div>
67
+ <div class="row">
68
+ <button class="btn" @click="appWindow.minimize()">Minimize</button>
69
+ <button class="btn" @click="appWindow.toggleMaximize()">Maximize</button>
70
+ <button class="btn" :class="{ on: fullscreen }" @click="toggleFullscreen">Fullscreen</button>
71
+ <button class="btn" :class="{ on: onTop }" @click="toggleOnTop">Always on top</button>
72
+ </div>
73
+ </Card>
74
+
75
+ <Card title="Backdrop" api="appWindow.backdrop">
76
+ <div class="row">
77
+ <button v-for="b in BACKDROPS" :key="b" class="btn" :class="{ on: backdrop === b }" @click="setBackdrop(b)">
78
+ {{ b }}
79
+ </button>
80
+ </div>
81
+ <p class="faint">The page background is translucent, so the material behind the window shows through.</p>
82
+ </Card>
83
+
84
+ <Card title="Pop-out window" api="<SkWindow>">
85
+ <div class="row">
86
+ <button class="btn primary" @click="popped = !popped">{{ popped ? "Close it" : "Pop out a counter" }}</button>
87
+ <span class="muted">count = {{ counter.count }}</span>
88
+ </div>
89
+ <p class="faint">
90
+ A second OS window painting part of this component: same tree, same store, nothing serialized.
91
+ </p>
92
+ <SkWindow
93
+ v-if="popped"
94
+ title="Counter"
95
+ :bounds="{ width: 280, height: 200 }"
96
+ always-on-top
97
+ :resizable="false"
98
+ @close="popped = false"
99
+ >
100
+ <div class="mini">
101
+ <div class="mini-count">{{ counter.count }}</div>
102
+ <button class="btn primary" @click="counter.increment()">Count up</button>
103
+ </div>
104
+ </SkWindow>
105
+ </Card>
106
+ </Page>
107
+ </template>
108
+
109
+ <style scoped>
110
+ .wide {
111
+ width: 320px;
112
+ }
113
+ .mini {
114
+ height: 100%;
115
+ display: flex;
116
+ flex-direction: column;
117
+ align-items: center;
118
+ justify-content: center;
119
+ gap: 14px;
120
+ background: #0b0b10;
121
+ color: var(--text);
122
+ font-family: var(--font);
123
+ }
124
+ .mini-count {
125
+ font-size: 44px;
126
+ font-weight: 700;
127
+ }
128
+ </style>
@@ -0,0 +1,2 @@
1
+ export const productName = "{{productName}}";
2
+ export const appName = "{{name}}";
@@ -0,0 +1,149 @@
1
+ // Route components are static imports: the runtime loads one bundle, so there is no lazy `() => import()`.
2
+ import { createMemoryHistory, createRouter } from "vue-router";
3
+ import type { Component } from "vue";
4
+ import type { IconName } from "./icons";
5
+ import AudioPage from "./pages/AudioPage.vue";
6
+ import FilesPage from "./pages/FilesPage.vue";
7
+ import FormsPage from "./pages/FormsPage.vue";
8
+ import GraphicsPage from "./pages/GraphicsPage.vue";
9
+ import HomePage from "./pages/HomePage.vue";
10
+ import LocalePage from "./pages/LocalePage.vue";
11
+ import MediaPage from "./pages/MediaPage.vue";
12
+ import MotionPage from "./pages/MotionPage.vue";
13
+ import NetworkPage from "./pages/NetworkPage.vue";
14
+ import PermissionsPage from "./pages/PermissionsPage.vue";
15
+ import StoragePage from "./pages/StoragePage.vue";
16
+ import StylesPage from "./pages/StylesPage.vue";
17
+ import SystemPage from "./pages/SystemPage.vue";
18
+ import TrayPage from "./pages/TrayPage.vue";
19
+ import WindowPage from "./pages/WindowPage.vue";
20
+
21
+ export interface PageEntry {
22
+ path: string;
23
+ title: string;
24
+ icon: IconName;
25
+ section: string;
26
+ blurb: string;
27
+ component: Component;
28
+ }
29
+
30
+ export const pages: PageEntry[] = [
31
+ { path: "/", title: "Home", icon: "home", section: "", blurb: "", component: HomePage },
32
+ {
33
+ path: "/window",
34
+ title: "Window",
35
+ icon: "window",
36
+ section: "Desktop",
37
+ blurb: "Title, size, backdrop, always on top and pop-out windows",
38
+ component: WindowPage,
39
+ },
40
+ {
41
+ path: "/tray",
42
+ title: "Tray & hotkeys",
43
+ icon: "tray",
44
+ section: "Desktop",
45
+ blurb: "A tray icon with a menu, and a shortcut that works from any app",
46
+ component: TrayPage,
47
+ },
48
+ {
49
+ path: "/files",
50
+ title: "Files",
51
+ icon: "files",
52
+ section: "Desktop",
53
+ blurb: "Open and save dialogs, and the clipboard",
54
+ component: FilesPage,
55
+ },
56
+ {
57
+ path: "/permissions",
58
+ title: "Permissions",
59
+ icon: "permissions",
60
+ section: "Desktop",
61
+ blurb: "Microphone and camera access, asked for and watched",
62
+ component: PermissionsPage,
63
+ },
64
+ {
65
+ path: "/system",
66
+ title: "System",
67
+ icon: "system",
68
+ section: "Desktop",
69
+ blurb: "Open links and folders, launch arguments, devtools",
70
+ component: SystemPage,
71
+ },
72
+ {
73
+ path: "/storage",
74
+ title: "Storage",
75
+ icon: "storage",
76
+ section: "Data",
77
+ blurb: "localStorage and IndexedDB, kept on disk between runs",
78
+ component: StoragePage,
79
+ },
80
+ {
81
+ path: "/network",
82
+ title: "Network",
83
+ icon: "network",
84
+ section: "Data",
85
+ blurb: "fetch, WebSocket and an embedded web page",
86
+ component: NetworkPage,
87
+ },
88
+ {
89
+ path: "/media",
90
+ title: "Media",
91
+ icon: "media",
92
+ section: "Media",
93
+ blurb: "Images, video and Lottie stickers from an asset pack",
94
+ component: MediaPage,
95
+ },
96
+ {
97
+ path: "/audio",
98
+ title: "Audio",
99
+ icon: "audio",
100
+ section: "Media",
101
+ blurb: "A Web Audio synth with a live spectrum",
102
+ component: AudioPage,
103
+ },
104
+ {
105
+ path: "/graphics",
106
+ title: "Graphics",
107
+ icon: "graphics",
108
+ section: "Media",
109
+ blurb: "Canvas 2D drawing and a WebGPU shader",
110
+ component: GraphicsPage,
111
+ },
112
+ {
113
+ path: "/motion",
114
+ title: "Motion",
115
+ icon: "motion",
116
+ section: "Interface",
117
+ blurb: "Transitions, keyframes and the Web Animations API",
118
+ component: MotionPage,
119
+ },
120
+ {
121
+ path: "/styles",
122
+ title: "Styles",
123
+ icon: "styles",
124
+ section: "Interface",
125
+ blurb: "Gradients, shadows, glass and the engine's own shading",
126
+ component: StylesPage,
127
+ },
128
+ {
129
+ path: "/forms",
130
+ title: "Forms",
131
+ icon: "forms",
132
+ section: "Interface",
133
+ blurb: "Native text fields, controls and text selection",
134
+ component: FormsPage,
135
+ },
136
+ {
137
+ path: "/locale",
138
+ title: "Locale",
139
+ icon: "locale",
140
+ section: "Interface",
141
+ blurb: "Message catalogs and the user's languages",
142
+ component: LocalePage,
143
+ },
144
+ ];
145
+
146
+ export const router = createRouter({
147
+ history: createMemoryHistory(),
148
+ routes: pages.map(({ path, component }) => ({ path, component })),
149
+ });
@@ -0,0 +1,9 @@
1
+ import { defineStore } from "pinia";
2
+ import { ref } from "vue";
3
+
4
+ export const useCounter = defineStore("counter", () => {
5
+ const count = ref(0);
6
+ const increment = () => count.value++;
7
+ const reset = () => (count.value = 0);
8
+ return { count, increment, reset };
9
+ });
@@ -0,0 +1,115 @@
1
+ // The tray icon and the global hotkey outlive the page that turns them on, so their state lives here.
2
+ import { appHotkeys, appTray, appWindow, type HotkeyChord, type TrayPixels } from "@skathi-native/runtime";
3
+ import { defineStore } from "pinia";
4
+ import { ref, watch } from "vue";
5
+ import { productName } from "../product";
6
+ import { useCounter } from "./counter";
7
+
8
+ const CTRL = 0x11;
9
+ const SHIFT = 0x10;
10
+ const KEY_K = 0x4b;
11
+ export const DEFAULT_CHORD: HotkeyChord = {
12
+ buttons: [
13
+ { device: 0, code: CTRL },
14
+ { device: 0, code: SHIFT },
15
+ { device: 0, code: KEY_K },
16
+ ],
17
+ };
18
+
19
+ const KEY_NAMES: Record<number, string> = {
20
+ 0x10: "Shift",
21
+ 0xa0: "Shift",
22
+ 0xa1: "Shift",
23
+ 0x11: "Ctrl",
24
+ 0xa2: "Ctrl",
25
+ 0xa3: "Ctrl",
26
+ 0x12: "Alt",
27
+ 0xa4: "Alt",
28
+ 0xa5: "Alt",
29
+ 0x5b: "Win",
30
+ 0x5c: "Win",
31
+ 0x20: "Space",
32
+ 0x0d: "Enter",
33
+ 0x09: "Tab",
34
+ };
35
+
36
+ /** Virtual-key codes on every platform: letters and digits are their ASCII codes, F1 is 0x70. */
37
+ export function chordLabel(chord: HotkeyChord): string {
38
+ return chord.buttons
39
+ .map(({ device, code }) => {
40
+ if (device === 1) return `Mouse ${code}`;
41
+ if ((code >= 0x30 && code <= 0x39) || (code >= 0x41 && code <= 0x5a)) return String.fromCharCode(code);
42
+ if (code >= 0x70 && code <= 0x87) return `F${code - 0x6f}`;
43
+ return KEY_NAMES[code] ?? `0x${code.toString(16)}`;
44
+ })
45
+ .join(" + ");
46
+ }
47
+
48
+ function trayIcon(): TrayPixels {
49
+ const size = 32;
50
+ const data = new Uint8Array(size * size * 4);
51
+ for (let y = 0; y < size; y++) {
52
+ for (let x = 0; x < size; x++) {
53
+ const dx = Math.max(0, Math.abs(x - 15.5) - 8);
54
+ const dy = Math.max(0, Math.abs(y - 15.5) - 8);
55
+ const t = (x + y) / (2 * size);
56
+ const o = (y * size + x) * 4;
57
+ data[o] = Math.round(0x8b - 0x1e * t);
58
+ data[o + 1] = Math.round(0x9d - 0x21 * t);
59
+ data[o + 2] = 0xff;
60
+ data[o + 3] = dx * dx + dy * dy <= 64 ? 255 : 0;
61
+ }
62
+ }
63
+ return { width: size, height: size, data };
64
+ }
65
+
66
+ export const useDesktop = defineStore("desktop", () => {
67
+ const counter = useCounter();
68
+ const trayShown = ref(false);
69
+ const chord = ref<HotkeyChord | null>(null);
70
+ const presses = ref(0);
71
+
72
+ function showTray(on: boolean) {
73
+ trayShown.value = on;
74
+ if (!on) {
75
+ appTray.set(null);
76
+ return;
77
+ }
78
+ appTray.set({
79
+ icon: trayIcon(),
80
+ tooltip: productName,
81
+ onClick: () => appWindow.show(),
82
+ menu: [
83
+ { label: `Open ${productName}`, default: true, onSelect: () => appWindow.show() },
84
+ { label: `Count up (${counter.count})`, onSelect: counter.increment },
85
+ { label: "Always on top", checked: appWindow.alwaysOnTop, onSelect: toggleOnTop },
86
+ { type: "separator" },
87
+ { role: "quit" },
88
+ ],
89
+ });
90
+ }
91
+
92
+ function toggleOnTop() {
93
+ appWindow.alwaysOnTop = !appWindow.alwaysOnTop;
94
+ showTray(true);
95
+ }
96
+
97
+ // The tray has no reactive model: a new menu is set whenever what it shows changes.
98
+ watch(
99
+ () => counter.count,
100
+ () => trayShown.value && showTray(true),
101
+ );
102
+
103
+ let unlisten: (() => void) | null = null;
104
+ function bindHotkey(next: HotkeyChord | null) {
105
+ appHotkeys.setBindings(next ? [{ id: "count-up", chord: next, triggerCooldownMs: 150 }] : []);
106
+ chord.value = next;
107
+ unlisten ??= appHotkeys.onFired((event) => {
108
+ if (event.id !== "count-up" || event.phase !== "down") return;
109
+ presses.value++;
110
+ counter.increment();
111
+ });
112
+ }
113
+
114
+ return { trayShown, chord, presses, showTray, bindHotkey };
115
+ });