@loidolt/theme-tauri 0.0.0-stage → 0.12.1
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.
- package/LICENSE +21 -0
- package/README.md +521 -2
- package/dist/deep-links.d.ts +23 -0
- package/dist/deep-links.js +62 -0
- package/dist/file-drop.svelte.d.ts +62 -0
- package/dist/file-drop.svelte.js +100 -0
- package/dist/files.d.ts +130 -0
- package/dist/files.js +241 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +10 -0
- package/dist/menu-bar.svelte.d.ts +129 -0
- package/dist/menu-bar.svelte.js +439 -0
- package/dist/notifications.svelte.d.ts +67 -0
- package/dist/notifications.svelte.js +100 -0
- package/dist/platform.d.ts +37 -0
- package/dist/platform.js +102 -0
- package/dist/setup.d.ts +135 -0
- package/dist/setup.js +91 -0
- package/dist/single-instance.d.ts +34 -0
- package/dist/single-instance.js +69 -0
- package/dist/styles.css +35 -0
- package/dist/tray.d.ts +68 -0
- package/dist/tray.js +118 -0
- package/dist/types.d.ts +203 -0
- package/dist/types.js +8 -0
- package/dist/window.svelte.d.ts +102 -0
- package/dist/window.svelte.js +158 -0
- package/package.json +92 -4
package/dist/tray.js
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { isTauri, quietly } from './platform.js';
|
|
2
|
+
const inert = {
|
|
3
|
+
ready: Promise.resolve(),
|
|
4
|
+
update: async ({ icon }) => quietly(icon),
|
|
5
|
+
destroy() { },
|
|
6
|
+
};
|
|
7
|
+
function entries(items) {
|
|
8
|
+
return items.map((item) => item === 'separator'
|
|
9
|
+
? { item: 'Separator' }
|
|
10
|
+
: item === 'quit'
|
|
11
|
+
? { item: 'Quit' }
|
|
12
|
+
: {
|
|
13
|
+
id: item.id,
|
|
14
|
+
text: item.label,
|
|
15
|
+
enabled: item.enabled ?? true,
|
|
16
|
+
action: item.action ? () => item.action() : undefined,
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* An icon in the menu bar (macOS) or system tray (Windows, Linux) with a simple menu, for an app
|
|
21
|
+
* that keeps working with its window closed. A no-op outside a Tauri window.
|
|
22
|
+
*
|
|
23
|
+
* ```ts
|
|
24
|
+
* const tray = createTray({
|
|
25
|
+
* tray, menu, // import * as tray from '@tauri-apps/api/tray', and menu likewise
|
|
26
|
+
* icon: defaultWindowIcon(),
|
|
27
|
+
* tooltip: 'Watching for new files',
|
|
28
|
+
* items: [
|
|
29
|
+
* { id: 'show', label: 'Show window', action: show },
|
|
30
|
+
* 'separator',
|
|
31
|
+
* { id: 'quit', label: 'Quit', action: quit },
|
|
32
|
+
* ],
|
|
33
|
+
* });
|
|
34
|
+
* tray.update({ tooltip: '2 jobs running' });
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export function createTray(options) {
|
|
38
|
+
if (!isTauri()) {
|
|
39
|
+
// `defaultWindowIcon()` rejects in a browser; nothing will await it here.
|
|
40
|
+
quietly(options.icon);
|
|
41
|
+
return inert;
|
|
42
|
+
}
|
|
43
|
+
const { tray, menu, id, iconAsTemplate, onClick } = options;
|
|
44
|
+
let destroyed = false;
|
|
45
|
+
let icon = null;
|
|
46
|
+
let currentMenu = null;
|
|
47
|
+
const buildMenu = (items) => items?.length ? menu.Menu.new({ items: entries(items) }) : Promise.resolve(null);
|
|
48
|
+
const release = (resource) => {
|
|
49
|
+
if (resource)
|
|
50
|
+
quietly(resource.close());
|
|
51
|
+
};
|
|
52
|
+
const ready = (async () => {
|
|
53
|
+
try {
|
|
54
|
+
const [image, built] = await Promise.all([options.icon, buildMenu(options.items)]);
|
|
55
|
+
currentMenu = built;
|
|
56
|
+
if (destroyed)
|
|
57
|
+
return release(built);
|
|
58
|
+
const created = await tray.TrayIcon.new({
|
|
59
|
+
id,
|
|
60
|
+
icon: image ?? undefined,
|
|
61
|
+
tooltip: options.tooltip,
|
|
62
|
+
title: options.title,
|
|
63
|
+
menu: built ?? undefined,
|
|
64
|
+
iconAsTemplate,
|
|
65
|
+
showMenuOnLeftClick: !onClick,
|
|
66
|
+
action: onClick
|
|
67
|
+
? (event) => {
|
|
68
|
+
if (event.type === 'Click' && event.button === 'Left' && event.buttonState === 'Up')
|
|
69
|
+
onClick();
|
|
70
|
+
}
|
|
71
|
+
: undefined,
|
|
72
|
+
});
|
|
73
|
+
// A destroy() while this was in flight has already freed the menu.
|
|
74
|
+
if (destroyed)
|
|
75
|
+
release(created);
|
|
76
|
+
else
|
|
77
|
+
icon = created;
|
|
78
|
+
}
|
|
79
|
+
catch {
|
|
80
|
+
/* not allowed by the capability, or no tray on this desktop */
|
|
81
|
+
}
|
|
82
|
+
})();
|
|
83
|
+
async function update(changes) {
|
|
84
|
+
await ready;
|
|
85
|
+
if (destroyed || !icon)
|
|
86
|
+
return;
|
|
87
|
+
const target = icon;
|
|
88
|
+
try {
|
|
89
|
+
if ('tooltip' in changes)
|
|
90
|
+
await target.setTooltip(changes.tooltip ?? null);
|
|
91
|
+
if ('title' in changes)
|
|
92
|
+
await target.setTitle(changes.title ?? null);
|
|
93
|
+
if ('icon' in changes)
|
|
94
|
+
await target.setIcon((await changes.icon) ?? null);
|
|
95
|
+
if ('items' in changes) {
|
|
96
|
+
const next = await buildMenu(changes.items);
|
|
97
|
+
if (destroyed)
|
|
98
|
+
return release(next);
|
|
99
|
+
await target.setMenu(next);
|
|
100
|
+
release(currentMenu);
|
|
101
|
+
currentMenu = next;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
/* a call the capability does not allow */
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
function destroy() {
|
|
109
|
+
if (destroyed)
|
|
110
|
+
return;
|
|
111
|
+
destroyed = true;
|
|
112
|
+
release(icon);
|
|
113
|
+
release(currentMenu);
|
|
114
|
+
icon = null;
|
|
115
|
+
currentMenu = null;
|
|
116
|
+
}
|
|
117
|
+
return { ready, update, destroy };
|
|
118
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
/** Undoes whatever the adapter installed. Safe to call twice. */
|
|
2
|
+
export type Teardown = () => void;
|
|
3
|
+
/** What every Tauri `listen`, `onOpenUrl` or `on…Event` resolves to. */
|
|
4
|
+
export type UnlistenFn = () => void;
|
|
5
|
+
/** `@tauri-apps/plugin-os`, for telling macOS from the rest without sniffing the user agent. */
|
|
6
|
+
export interface OsPlugin {
|
|
7
|
+
type(): 'linux' | 'windows' | 'macos' | 'ios' | 'android';
|
|
8
|
+
}
|
|
9
|
+
/** A window appearance as Tauri names it; `null` hands it back to the OS. */
|
|
10
|
+
export type WindowTheme = 'light' | 'dark';
|
|
11
|
+
/**
|
|
12
|
+
* The window the page runs in: what `getCurrentWindow()` from `@tauri-apps/api/window` returns.
|
|
13
|
+
* `setupTauri` also takes the function itself, `plugins: { window: getCurrentWindow }`. Each call needs its permission in the
|
|
14
|
+
* capability file: `core:window:allow-set-background-color`, `core:window:allow-set-theme` and
|
|
15
|
+
* `core:window:allow-show` (`isFullscreen` and `onResized` are in `core:window:default`).
|
|
16
|
+
*/
|
|
17
|
+
export interface WindowPlugin {
|
|
18
|
+
/** The colour the window shows before the page paints and behind a transparent one. */
|
|
19
|
+
setBackgroundColor(color: string): Promise<void>;
|
|
20
|
+
/** The window's light or dark appearance; `null` follows the OS. */
|
|
21
|
+
setTheme(theme?: WindowTheme | null): Promise<void>;
|
|
22
|
+
isFullscreen(): Promise<boolean>;
|
|
23
|
+
/** Fires on every resize, entering and leaving full screen included. */
|
|
24
|
+
onResized(handler: () => void): Promise<UnlistenFn>;
|
|
25
|
+
show(): Promise<void>;
|
|
26
|
+
}
|
|
27
|
+
/** `@tauri-apps/plugin-notification`, for OS notifications sent from Rust. */
|
|
28
|
+
export interface NotificationPlugin {
|
|
29
|
+
isPermissionGranted(): Promise<boolean>;
|
|
30
|
+
requestPermission(): Promise<'granted' | 'denied' | 'default'>;
|
|
31
|
+
sendNotification(options: {
|
|
32
|
+
title: string;
|
|
33
|
+
body?: string;
|
|
34
|
+
icon?: string;
|
|
35
|
+
silent?: boolean;
|
|
36
|
+
}): void;
|
|
37
|
+
}
|
|
38
|
+
/** `@tauri-apps/api/event`, for events the Rust side emits. */
|
|
39
|
+
export interface EventModule {
|
|
40
|
+
listen<T>(event: string, handler: (event: {
|
|
41
|
+
payload: T;
|
|
42
|
+
}) => void): Promise<UnlistenFn>;
|
|
43
|
+
}
|
|
44
|
+
/** `@tauri-apps/plugin-deep-link`, for URLs that open the app. */
|
|
45
|
+
export interface DeepLinkPlugin {
|
|
46
|
+
getCurrent(): Promise<string[] | null>;
|
|
47
|
+
onOpenUrl(handler: (urls: string[]) => void): Promise<UnlistenFn>;
|
|
48
|
+
}
|
|
49
|
+
/** A path, PNG or ICO bytes, or an `Image` from `@tauri-apps/api/image`. */
|
|
50
|
+
export type TrayImage = string | Uint8Array | ArrayBuffer | number[] | object;
|
|
51
|
+
/** A menu or tray icon living on the Rust side; `close()` frees it. */
|
|
52
|
+
export interface TauriResource {
|
|
53
|
+
close(): Promise<void>;
|
|
54
|
+
}
|
|
55
|
+
/** What the tray adapter puts in a menu: plain items and separators. */
|
|
56
|
+
export type TrayMenuEntry = {
|
|
57
|
+
id: string;
|
|
58
|
+
text: string;
|
|
59
|
+
enabled?: boolean;
|
|
60
|
+
action?: (id: string) => void;
|
|
61
|
+
} | {
|
|
62
|
+
item: 'Separator' | 'Quit';
|
|
63
|
+
};
|
|
64
|
+
/** `@tauri-apps/api/menu`, for the tray's menu. */
|
|
65
|
+
export interface MenuModule {
|
|
66
|
+
Menu: {
|
|
67
|
+
'new'(options?: {
|
|
68
|
+
items?: TrayMenuEntry[];
|
|
69
|
+
}): Promise<TauriResource>;
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
/** A tray icon from `@tauri-apps/api/tray`. */
|
|
73
|
+
export interface TrayIconHandle extends TauriResource {
|
|
74
|
+
setIcon(icon: TrayImage | null): Promise<void>;
|
|
75
|
+
setTooltip(tooltip: string | null): Promise<void>;
|
|
76
|
+
setTitle(title: string | null): Promise<void>;
|
|
77
|
+
setMenu(menu: TauriResource | null): Promise<void>;
|
|
78
|
+
}
|
|
79
|
+
/** `@tauri-apps/api/tray`. */
|
|
80
|
+
export interface TrayModule {
|
|
81
|
+
TrayIcon: {
|
|
82
|
+
'new'(options?: {
|
|
83
|
+
id?: string;
|
|
84
|
+
icon?: TrayImage;
|
|
85
|
+
tooltip?: string;
|
|
86
|
+
title?: string;
|
|
87
|
+
menu?: TauriResource;
|
|
88
|
+
iconAsTemplate?: boolean;
|
|
89
|
+
showMenuOnLeftClick?: boolean;
|
|
90
|
+
action?(event: {
|
|
91
|
+
type: string;
|
|
92
|
+
button?: string;
|
|
93
|
+
buttonState?: string;
|
|
94
|
+
}): void;
|
|
95
|
+
}): Promise<TrayIconHandle>;
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
/** A file type filter in a native dialog: a name and the extensions it lets through, no dots. */
|
|
99
|
+
export interface DialogFilter {
|
|
100
|
+
name: string;
|
|
101
|
+
extensions: string[];
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* `@tauri-apps/plugin-dialog`, for the native open and save dialogs (`dialog:default`, or
|
|
105
|
+
* `dialog:allow-open` and `dialog:allow-save`). The paths it returns are added to the fs scope.
|
|
106
|
+
*/
|
|
107
|
+
export interface DialogPlugin {
|
|
108
|
+
open(options?: {
|
|
109
|
+
title?: string;
|
|
110
|
+
filters?: DialogFilter[];
|
|
111
|
+
defaultPath?: string;
|
|
112
|
+
multiple?: boolean;
|
|
113
|
+
directory?: boolean;
|
|
114
|
+
}): Promise<string | string[] | null>;
|
|
115
|
+
save(options?: {
|
|
116
|
+
title?: string;
|
|
117
|
+
filters?: DialogFilter[];
|
|
118
|
+
defaultPath?: string;
|
|
119
|
+
}): Promise<string | null>;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* `@tauri-apps/plugin-fs`, for reading picked and dropped files (`fs:allow-read-file`) and writing
|
|
123
|
+
* saved ones (`fs:allow-write-file`).
|
|
124
|
+
*/
|
|
125
|
+
export interface FsPlugin {
|
|
126
|
+
readFile(path: string): Promise<Uint8Array>;
|
|
127
|
+
writeFile(path: string, data: Uint8Array): Promise<void>;
|
|
128
|
+
}
|
|
129
|
+
/** A point in the window, in physical pixels. */
|
|
130
|
+
export interface PhysicalPoint {
|
|
131
|
+
x: number;
|
|
132
|
+
y: number;
|
|
133
|
+
}
|
|
134
|
+
/** What the web view's drag and drop event carries: Tauri's `DragDropEvent`. */
|
|
135
|
+
export type DragDropPayload = {
|
|
136
|
+
type: 'enter';
|
|
137
|
+
paths: string[];
|
|
138
|
+
position: PhysicalPoint;
|
|
139
|
+
} | {
|
|
140
|
+
type: 'over';
|
|
141
|
+
position: PhysicalPoint;
|
|
142
|
+
} | {
|
|
143
|
+
type: 'drop';
|
|
144
|
+
paths: string[];
|
|
145
|
+
position: PhysicalPoint;
|
|
146
|
+
} | {
|
|
147
|
+
type: 'leave';
|
|
148
|
+
};
|
|
149
|
+
/**
|
|
150
|
+
* The web view the page runs in: what `getCurrentWebview()` from `@tauri-apps/api/webview`
|
|
151
|
+
* returns. Its drag and drop event is in `core:default`.
|
|
152
|
+
*/
|
|
153
|
+
export interface WebviewPlugin {
|
|
154
|
+
onDragDropEvent(handler: (event: {
|
|
155
|
+
payload: DragDropPayload;
|
|
156
|
+
}) => void): Promise<UnlistenFn>;
|
|
157
|
+
}
|
|
158
|
+
/** A built-in item the OS provides and handles itself: the clipboard, Quit, Hide and the rest. */
|
|
159
|
+
export type PredefinedMenuItemName = 'Separator' | 'Copy' | 'Cut' | 'Paste' | 'SelectAll' | 'Undo' | 'Redo' | 'Minimize' | 'Maximize' | 'Fullscreen' | 'Hide' | 'HideOthers' | 'ShowAll' | 'CloseWindow' | 'Quit' | 'Services' | 'BringAllToFront' | 'About';
|
|
160
|
+
/** A menu item from `MenuItem.new`, changed in place. */
|
|
161
|
+
export interface MenuItemHandle extends TauriResource {
|
|
162
|
+
setText(text: string): Promise<void>;
|
|
163
|
+
setEnabled(enabled: boolean): Promise<void>;
|
|
164
|
+
setAccelerator(accelerator: string | null): Promise<void>;
|
|
165
|
+
}
|
|
166
|
+
/** A submenu from `Submenu.new`: one heading of the menu bar. */
|
|
167
|
+
export interface SubmenuHandle extends TauriResource {
|
|
168
|
+
setText(text: string): Promise<void>;
|
|
169
|
+
/** macOS: makes it the menu that lists the open windows. */
|
|
170
|
+
setAsWindowsMenuForNSApp?(): Promise<void>;
|
|
171
|
+
/** macOS: makes it the Help menu, with the system's search field. */
|
|
172
|
+
setAsHelpMenuForNSApp?(): Promise<void>;
|
|
173
|
+
}
|
|
174
|
+
/** A menu from `Menu.new` that can become the menu bar. Each call returns the menu it replaced. */
|
|
175
|
+
export interface AppMenuHandle extends TauriResource {
|
|
176
|
+
setAsAppMenu(): Promise<AppMenuHandle | null>;
|
|
177
|
+
setAsWindowMenu(): Promise<AppMenuHandle | null>;
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* `@tauri-apps/api/menu`, for the menu bar. Items go in as Tauri builds them (an item from
|
|
181
|
+
* `MenuItem.new`, or the options of a predefined one), so they are typed loosely here.
|
|
182
|
+
*/
|
|
183
|
+
export interface MenuBarModule extends MenuModule {
|
|
184
|
+
Menu: {
|
|
185
|
+
'new'(options?: {
|
|
186
|
+
items?: unknown[];
|
|
187
|
+
}): Promise<AppMenuHandle>;
|
|
188
|
+
};
|
|
189
|
+
Submenu: {
|
|
190
|
+
'new'(options: {
|
|
191
|
+
text: string;
|
|
192
|
+
items?: unknown[];
|
|
193
|
+
}): Promise<SubmenuHandle>;
|
|
194
|
+
};
|
|
195
|
+
MenuItem: {
|
|
196
|
+
'new'(options: {
|
|
197
|
+
text: string;
|
|
198
|
+
enabled?: boolean;
|
|
199
|
+
accelerator?: string;
|
|
200
|
+
action?: (id: string) => void;
|
|
201
|
+
}): Promise<MenuItemHandle>;
|
|
202
|
+
};
|
|
203
|
+
}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The slices of the Tauri APIs these adapters call. This package never imports `@tauri-apps/api`
|
|
3
|
+
* or a plugin itself: you pass the module you installed (`import * as os from
|
|
4
|
+
* '@tauri-apps/plugin-os'`), so an app pays only for what it uses, the browser and server builds
|
|
5
|
+
* never load the Tauri runtime, and a missing piece is a type error rather than a runtime crash.
|
|
6
|
+
* The real modules satisfy these shapes; so does a test double.
|
|
7
|
+
*/
|
|
8
|
+
export {};
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { type Theme } from '@loidolt/theme-svelte';
|
|
2
|
+
import { type SemanticRole } from '@loidolt/theme-tokens';
|
|
3
|
+
import type { OsPlugin, Teardown, WindowPlugin } from './types.js';
|
|
4
|
+
/**
|
|
5
|
+
* The theme's own page background, for `backgroundColor` in `tauri.conf.json`: the colour the
|
|
6
|
+
* window is filled with before the page's first frame. Match the theme most people launch in —
|
|
7
|
+
* `light` (the default) for a light-first app. With the window starting hidden (see
|
|
8
|
+
* {@link showWindow}) it only shows where the web view paints late.
|
|
9
|
+
*/
|
|
10
|
+
export declare function windowBackground(scheme?: 'light' | 'dark'): string;
|
|
11
|
+
export interface WindowAppearanceOptions {
|
|
12
|
+
/** The window: `getCurrentWindow()` from `@tauri-apps/api/window`. */
|
|
13
|
+
window: WindowPlugin;
|
|
14
|
+
/**
|
|
15
|
+
* The app's `createTheme` store. With it, an explicit light or dark choice reaches the native
|
|
16
|
+
* window (title bar, menus, dialogs, scroll bars) and `system` hands the appearance back to the
|
|
17
|
+
* OS. Without it the window's appearance is left to the OS: pinning it from the page alone
|
|
18
|
+
* would also pin the web view's `prefers-color-scheme`, and a `system` theme would stop
|
|
19
|
+
* following the OS.
|
|
20
|
+
*/
|
|
21
|
+
theme?: Theme;
|
|
22
|
+
/**
|
|
23
|
+
* The colour role the window is filled with: what shows before the page paints, while it
|
|
24
|
+
* resizes and behind a transparent page. Defaults to `background`, which the page body paints.
|
|
25
|
+
*/
|
|
26
|
+
backgroundRole?: SemanticRole;
|
|
27
|
+
/** Where the theme is read from. Defaults to `<html>`. */
|
|
28
|
+
element?: () => Element | null | undefined;
|
|
29
|
+
}
|
|
30
|
+
export interface WindowAppearance {
|
|
31
|
+
/** Settles once the first background and appearance have reached the window. Never rejects. */
|
|
32
|
+
readonly ready: Promise<void>;
|
|
33
|
+
destroy(): void;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Keeps the window in step with the page theme: its background in the live `background` colour,
|
|
37
|
+
* so there is no white flash before the first frame or while resizing a dark window, and, given
|
|
38
|
+
* the `createTheme` store, its native light or dark appearance. It reads the live colour, so a
|
|
39
|
+
* preset or an app's own theme decides, and it follows every theme and preset change.
|
|
40
|
+
*
|
|
41
|
+
* ```ts
|
|
42
|
+
* import { getCurrentWindow } from '@tauri-apps/api/window';
|
|
43
|
+
* // getCurrentWindow() reads the Tauri runtime, so check first in code that also runs in a browser
|
|
44
|
+
* if (isTauri()) syncWindowAppearance({ window: getCurrentWindow(), theme });
|
|
45
|
+
* ```
|
|
46
|
+
*
|
|
47
|
+
* A no-op outside a Tauri desktop window.
|
|
48
|
+
*/
|
|
49
|
+
export declare function syncWindowAppearance(options: WindowAppearanceOptions, os?: OsPlugin): WindowAppearance;
|
|
50
|
+
export interface WindowChromeOptions {
|
|
51
|
+
/** The window: `getCurrentWindow()` from `@tauri-apps/api/window`. */
|
|
52
|
+
window: WindowPlugin;
|
|
53
|
+
/**
|
|
54
|
+
* The title bar `tauri.conf.json` gives the window. `overlay` (`"titleBarStyle": "Overlay"`,
|
|
55
|
+
* with `"hiddenTitle": true`) draws the page under a transparent macOS title bar, and the page
|
|
56
|
+
* is inset to clear the window buttons. `native` (the default) leaves the page alone.
|
|
57
|
+
*/
|
|
58
|
+
titleBar?: 'overlay' | 'native';
|
|
59
|
+
/**
|
|
60
|
+
* Where the macOS window buttons sit under an overlay title bar. `above` (the default): in a
|
|
61
|
+
* strip of their own across the top, which the safe-area top inset (`--ldt-safe-top`) reserves —
|
|
62
|
+
* `Topbar` grows by it, and drawers and toasts clear it. `beside`: in the `Topbar` row itself,
|
|
63
|
+
* which is inset from the left (`--ldt-safe-left`, on the bar only) instead; move the buttons
|
|
64
|
+
* into the bar's middle with `trafficLightPosition` in `tauri.conf.json`.
|
|
65
|
+
*/
|
|
66
|
+
trafficLights?: 'above' | 'beside';
|
|
67
|
+
/** The strip reserved for `above`. Defaults to `28px`, the height of a macOS title bar. */
|
|
68
|
+
top?: string;
|
|
69
|
+
/** The room left for `beside`. Defaults to `78px`, the width of the three buttons and a gap. */
|
|
70
|
+
left?: string;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Makes room for the macOS window buttons under an overlay title bar, through the theme's
|
|
74
|
+
* safe-area knobs, and gives it back in full screen, where the buttons hide. `<html>` carries
|
|
75
|
+
* `data-tauri-traffic-lights="above"` (or `"beside"`) while they show, for CSS of your own.
|
|
76
|
+
*
|
|
77
|
+
* ```ts
|
|
78
|
+
* import { getCurrentWindow } from '@tauri-apps/api/window';
|
|
79
|
+
* if (isTauri()) insetWindowChrome({ window: getCurrentWindow(), titleBar: 'overlay' });
|
|
80
|
+
* ```
|
|
81
|
+
*
|
|
82
|
+
* Returns the teardown. A no-op on Windows, Linux, and outside a Tauri window, and with a
|
|
83
|
+
* `native` title bar.
|
|
84
|
+
*/
|
|
85
|
+
export declare function insetWindowChrome(options: WindowChromeOptions, os?: OsPlugin): Teardown;
|
|
86
|
+
export interface ShowWindowOptions {
|
|
87
|
+
/** The window: `getCurrentWindow()` from `@tauri-apps/api/window`. */
|
|
88
|
+
window: WindowPlugin;
|
|
89
|
+
/** Waited for before the window shows — the theme applied, the first data. */
|
|
90
|
+
until?: Promise<unknown>;
|
|
91
|
+
/** Wait for the bundled faces to load, so no fallback text flashes. Defaults to `true`. */
|
|
92
|
+
waitForFonts?: boolean;
|
|
93
|
+
/** Longest wait in milliseconds before showing anyway. Defaults to 3000. */
|
|
94
|
+
timeout?: number;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Shows a window that starts hidden (`"visible": false` in `tauri.conf.json`) once the page is
|
|
98
|
+
* ready to be seen — the theme applied to the page and the window, the fonts in — so it opens
|
|
99
|
+
* on the app rather than on a white or wrong-theme flash. Never rejects; a no-op outside a
|
|
100
|
+
* Tauri desktop window, and harmless on a window that is already visible.
|
|
101
|
+
*/
|
|
102
|
+
export declare function showWindow(options: ShowWindowOptions, os?: OsPlugin): Promise<void>;
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { colorSchemeOf, observeColorScheme, readRoleColor, } from '@loidolt/theme-svelte';
|
|
2
|
+
import { darkSemantic, resolveRoles, semantic } from '@loidolt/theme-tokens';
|
|
3
|
+
import { combine, listen, noop, quietly, tauriPlatform } from './platform.js';
|
|
4
|
+
/** Window chrome is a desktop affair: Tauri's mobile targets have no title bar or window colour. */
|
|
5
|
+
function desktopPlatform(os) {
|
|
6
|
+
const platform = tauriPlatform(os);
|
|
7
|
+
return platform === 'macos' || platform === 'windows' || platform === 'linux' ? platform : null;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* The theme's own page background, for `backgroundColor` in `tauri.conf.json`: the colour the
|
|
11
|
+
* window is filled with before the page's first frame. Match the theme most people launch in —
|
|
12
|
+
* `light` (the default) for a light-first app. With the window starting hidden (see
|
|
13
|
+
* {@link showWindow}) it only shows where the web view paints late.
|
|
14
|
+
*/
|
|
15
|
+
export function windowBackground(scheme = 'light') {
|
|
16
|
+
return (scheme === 'dark' ? darkSemantic : semantic).background;
|
|
17
|
+
}
|
|
18
|
+
const settled = (calls) => Promise.all(calls.map((call) => call.catch(() => { }))).then(() => { });
|
|
19
|
+
/**
|
|
20
|
+
* Keeps the window in step with the page theme: its background in the live `background` colour,
|
|
21
|
+
* so there is no white flash before the first frame or while resizing a dark window, and, given
|
|
22
|
+
* the `createTheme` store, its native light or dark appearance. It reads the live colour, so a
|
|
23
|
+
* preset or an app's own theme decides, and it follows every theme and preset change.
|
|
24
|
+
*
|
|
25
|
+
* ```ts
|
|
26
|
+
* import { getCurrentWindow } from '@tauri-apps/api/window';
|
|
27
|
+
* // getCurrentWindow() reads the Tauri runtime, so check first in code that also runs in a browser
|
|
28
|
+
* if (isTauri()) syncWindowAppearance({ window: getCurrentWindow(), theme });
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* A no-op outside a Tauri desktop window.
|
|
32
|
+
*/
|
|
33
|
+
export function syncWindowAppearance(options, os) {
|
|
34
|
+
if (!desktopPlatform(os))
|
|
35
|
+
return { ready: Promise.resolve(), destroy: noop };
|
|
36
|
+
const { window: win, theme, backgroundRole = 'background' } = options;
|
|
37
|
+
const target = () => options.element?.() ?? document.documentElement;
|
|
38
|
+
// Only what changed is sent: every theme mutation lands here, most of them irrelevant.
|
|
39
|
+
const last = {};
|
|
40
|
+
const changed = (key, value) => {
|
|
41
|
+
if (key in last && last[key] === value)
|
|
42
|
+
return false;
|
|
43
|
+
last[key] = value;
|
|
44
|
+
return true;
|
|
45
|
+
};
|
|
46
|
+
const apply = () => {
|
|
47
|
+
const calls = [];
|
|
48
|
+
const element = target();
|
|
49
|
+
const color = (readRoleColor(element, backgroundRole) ??
|
|
50
|
+
resolveRoles({ role: backgroundRole }, colorSchemeOf(element)).role).slice(0, 7); // opaque: Windows ignores alpha, and a window is never see-through by accident
|
|
51
|
+
if (changed('background', color))
|
|
52
|
+
calls.push(win.setBackgroundColor(color));
|
|
53
|
+
if (theme) {
|
|
54
|
+
const appearance = theme.preference === 'system' ? null : theme.resolved;
|
|
55
|
+
if (changed('theme', appearance))
|
|
56
|
+
calls.push(win.setTheme(appearance));
|
|
57
|
+
}
|
|
58
|
+
for (const call of calls)
|
|
59
|
+
quietly(call);
|
|
60
|
+
return calls;
|
|
61
|
+
};
|
|
62
|
+
const ready = settled(apply());
|
|
63
|
+
const teardowns = [observeColorScheme(apply)];
|
|
64
|
+
// Going back to `system` while the system matches the old choice changes no attribute, so the
|
|
65
|
+
// preference itself is watched too.
|
|
66
|
+
if (theme) {
|
|
67
|
+
teardowns.push($effect.root(() => {
|
|
68
|
+
$effect(() => {
|
|
69
|
+
void theme.preference;
|
|
70
|
+
apply();
|
|
71
|
+
});
|
|
72
|
+
}));
|
|
73
|
+
}
|
|
74
|
+
return { ready, destroy: combine(...teardowns) };
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Makes room for the macOS window buttons under an overlay title bar, through the theme's
|
|
78
|
+
* safe-area knobs, and gives it back in full screen, where the buttons hide. `<html>` carries
|
|
79
|
+
* `data-tauri-traffic-lights="above"` (or `"beside"`) while they show, for CSS of your own.
|
|
80
|
+
*
|
|
81
|
+
* ```ts
|
|
82
|
+
* import { getCurrentWindow } from '@tauri-apps/api/window';
|
|
83
|
+
* if (isTauri()) insetWindowChrome({ window: getCurrentWindow(), titleBar: 'overlay' });
|
|
84
|
+
* ```
|
|
85
|
+
*
|
|
86
|
+
* Returns the teardown. A no-op on Windows, Linux, and outside a Tauri window, and with a
|
|
87
|
+
* `native` title bar.
|
|
88
|
+
*/
|
|
89
|
+
export function insetWindowChrome(options, os) {
|
|
90
|
+
if (options.titleBar !== 'overlay' || desktopPlatform(os) !== 'macos')
|
|
91
|
+
return noop;
|
|
92
|
+
const { window: win, trafficLights = 'above', top = '28px', left = '78px' } = options;
|
|
93
|
+
const root = document.documentElement;
|
|
94
|
+
const property = trafficLights === 'above' ? '--ldt-safe-top' : '--ldt-tauri-traffic-lights';
|
|
95
|
+
const value = trafficLights === 'above' ? top : left;
|
|
96
|
+
const previous = root.style.getPropertyValue(property);
|
|
97
|
+
const unset = () => {
|
|
98
|
+
delete root.dataset.tauriTrafficLights;
|
|
99
|
+
if (previous)
|
|
100
|
+
root.style.setProperty(property, previous);
|
|
101
|
+
else
|
|
102
|
+
root.style.removeProperty(property);
|
|
103
|
+
};
|
|
104
|
+
const write = (fullscreen) => {
|
|
105
|
+
if (fullscreen)
|
|
106
|
+
return unset();
|
|
107
|
+
root.dataset.tauriTrafficLights = trafficLights;
|
|
108
|
+
root.style.setProperty(property, value);
|
|
109
|
+
};
|
|
110
|
+
let stopped = false;
|
|
111
|
+
const check = () => quietly(win.isFullscreen().then((fullscreen) => {
|
|
112
|
+
if (!stopped)
|
|
113
|
+
write(fullscreen);
|
|
114
|
+
}));
|
|
115
|
+
// Windowed until told otherwise: that is how an app launches, and the inset is then right
|
|
116
|
+
// from the first frame.
|
|
117
|
+
write(false);
|
|
118
|
+
check();
|
|
119
|
+
const unlisten = listen(win.onResized(check));
|
|
120
|
+
return () => {
|
|
121
|
+
if (stopped)
|
|
122
|
+
return;
|
|
123
|
+
stopped = true;
|
|
124
|
+
unlisten();
|
|
125
|
+
unset();
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Shows a window that starts hidden (`"visible": false` in `tauri.conf.json`) once the page is
|
|
130
|
+
* ready to be seen — the theme applied to the page and the window, the fonts in — so it opens
|
|
131
|
+
* on the app rather than on a white or wrong-theme flash. Never rejects; a no-op outside a
|
|
132
|
+
* Tauri desktop window, and harmless on a window that is already visible.
|
|
133
|
+
*/
|
|
134
|
+
export async function showWindow(options, os) {
|
|
135
|
+
if (!desktopPlatform(os))
|
|
136
|
+
return;
|
|
137
|
+
const { window: win, until, waitForFonts = true, timeout = 3000 } = options;
|
|
138
|
+
const waits = [];
|
|
139
|
+
if (until)
|
|
140
|
+
waits.push(until.catch(() => { }));
|
|
141
|
+
if (waitForFonts && document.fonts)
|
|
142
|
+
waits.push(document.fonts.ready.catch(() => { }));
|
|
143
|
+
let timer;
|
|
144
|
+
// No animation frames here: a hidden window may never get one.
|
|
145
|
+
await Promise.race([
|
|
146
|
+
Promise.all(waits),
|
|
147
|
+
new Promise((resolve) => {
|
|
148
|
+
timer = setTimeout(resolve, timeout);
|
|
149
|
+
}),
|
|
150
|
+
]);
|
|
151
|
+
clearTimeout(timer);
|
|
152
|
+
try {
|
|
153
|
+
await win.show();
|
|
154
|
+
}
|
|
155
|
+
catch {
|
|
156
|
+
/* `core:window:allow-show` missing from the capability: nothing more to do from here */
|
|
157
|
+
}
|
|
158
|
+
}
|