@liberation-data/desk 0.43.0 → 0.45.0
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/README.md +238 -10
- package/dist/core/desk.d.ts +2 -0
- package/dist/core/desk.d.ts.map +1 -1
- package/dist/core/desk.js +11 -2
- package/dist/core/desk.js.map +1 -1
- package/dist/core/hosted.d.ts +222 -0
- package/dist/core/hosted.d.ts.map +1 -0
- package/dist/core/hosted.js +166 -0
- package/dist/core/hosted.js.map +1 -0
- package/dist/core/index.d.ts +9 -1
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +5 -1
- package/dist/core/index.js.map +1 -1
- package/dist/core/location.d.ts +8 -1
- package/dist/core/location.d.ts.map +1 -1
- package/dist/core/location.js +18 -2
- package/dist/core/location.js.map +1 -1
- package/dist/core/preferences.d.ts +27 -0
- package/dist/core/preferences.d.ts.map +1 -0
- package/dist/core/preferences.js +121 -0
- package/dist/core/preferences.js.map +1 -0
- package/dist/core/scheme.d.ts +9 -3
- package/dist/core/scheme.d.ts.map +1 -1
- package/dist/core/scheme.js +13 -4
- package/dist/core/scheme.js.map +1 -1
- package/dist/core/theme.d.ts +41 -0
- package/dist/core/theme.d.ts.map +1 -0
- package/dist/core/theme.js +56 -0
- package/dist/core/theme.js.map +1 -0
- package/dist/core/themeChange.d.ts +25 -0
- package/dist/core/themeChange.d.ts.map +1 -0
- package/dist/core/themeChange.js +49 -0
- package/dist/core/themeChange.js.map +1 -0
- package/dist/core/types.d.ts +5 -0
- package/dist/core/types.d.ts.map +1 -1
- package/dist/css/apps.css +52 -1
- package/dist/css/shell.css +100 -0
- package/dist/css/tokens.css +10 -0
- package/dist/css/windows.css +28 -0
- package/dist/desk.css +90 -1
- package/dist/fx/fx.d.ts.map +1 -1
- package/dist/fx/fx.js +7 -31
- package/dist/fx/fx.js.map +1 -1
- package/dist/host/checkChrome.d.ts +20 -0
- package/dist/host/checkChrome.d.ts.map +1 -0
- package/dist/host/checkChrome.js +219 -0
- package/dist/host/checkChrome.js.map +1 -0
- package/dist/host/checkTheme.d.ts +35 -0
- package/dist/host/checkTheme.d.ts.map +1 -0
- package/dist/host/checkTheme.js +128 -0
- package/dist/host/checkTheme.js.map +1 -0
- package/dist/host/index.d.ts +7 -0
- package/dist/host/index.d.ts.map +1 -0
- package/dist/host/index.js +8 -0
- package/dist/host/index.js.map +1 -0
- package/dist/host/router.d.ts +99 -0
- package/dist/host/router.d.ts.map +1 -0
- package/dist/host/router.js +279 -0
- package/dist/host/router.js.map +1 -0
- package/dist/react/Desktop.d.ts.map +1 -1
- package/dist/react/Desktop.js +11 -14
- package/dist/react/Desktop.js.map +1 -1
- package/dist/react/Dock.d.ts +23 -4
- package/dist/react/Dock.d.ts.map +1 -1
- package/dist/react/Dock.js +17 -29
- package/dist/react/Dock.js.map +1 -1
- package/dist/react/MenuBar.d.ts +1 -1
- package/dist/react/MenuBar.d.ts.map +1 -1
- package/dist/react/MenuBar.js +20 -4
- package/dist/react/MenuBar.js.map +1 -1
- package/dist/react/app.d.ts +65 -12
- package/dist/react/app.d.ts.map +1 -1
- package/dist/react/app.js +79 -21
- package/dist/react/app.js.map +1 -1
- package/dist/react/appFrame.d.ts +11 -7
- package/dist/react/appFrame.d.ts.map +1 -1
- package/dist/react/appFrame.js +24 -6
- package/dist/react/appFrame.js.map +1 -1
- package/dist/react/appPanels.d.ts +4 -1
- package/dist/react/appPanels.d.ts.map +1 -1
- package/dist/react/appPanels.js +76 -28
- package/dist/react/appPanels.js.map +1 -1
- package/dist/react/appearance.d.ts +7 -1
- package/dist/react/appearance.d.ts.map +1 -1
- package/dist/react/appearance.js +32 -53
- package/dist/react/appearance.js.map +1 -1
- package/dist/react/context.d.ts +25 -1
- package/dist/react/context.d.ts.map +1 -1
- package/dist/react/context.js +73 -14
- package/dist/react/context.js.map +1 -1
- package/dist/react/dockStatus.d.ts +19 -0
- package/dist/react/dockStatus.d.ts.map +1 -0
- package/dist/react/dockStatus.js +25 -0
- package/dist/react/dockStatus.js.map +1 -0
- package/dist/react/frameMenus.d.ts +28 -0
- package/dist/react/frameMenus.d.ts.map +1 -0
- package/dist/react/frameMenus.js +85 -0
- package/dist/react/frameMenus.js.map +1 -0
- package/dist/react/hostApp.d.ts +15 -0
- package/dist/react/hostApp.d.ts.map +1 -0
- package/dist/react/hostApp.js +77 -0
- package/dist/react/hostApp.js.map +1 -0
- package/dist/react/hostDock.d.ts +25 -0
- package/dist/react/hostDock.d.ts.map +1 -0
- package/dist/react/hostDock.js +98 -0
- package/dist/react/hostDock.js.map +1 -0
- package/dist/react/hostMenu.d.ts +2 -36
- package/dist/react/hostMenu.d.ts.map +1 -1
- package/dist/react/hostMenu.js.map +1 -1
- package/dist/react/hosted.d.ts +28 -0
- package/dist/react/hosted.d.ts.map +1 -0
- package/dist/react/hosted.js +53 -0
- package/dist/react/hosted.js.map +1 -0
- package/dist/react/index.d.ts +10 -5
- package/dist/react/index.d.ts.map +1 -1
- package/dist/react/index.js +6 -3
- package/dist/react/index.js.map +1 -1
- package/dist/react/mediaQuery.d.ts +3 -0
- package/dist/react/mediaQuery.d.ts.map +1 -0
- package/dist/react/mediaQuery.js +11 -0
- package/dist/react/mediaQuery.js.map +1 -0
- package/dist/react/shell.d.ts +13 -1
- package/dist/react/shell.d.ts.map +1 -1
- package/dist/react/shell.js +19 -3
- package/dist/react/shell.js.map +1 -1
- package/dist/react/sidebar.d.ts +6 -1
- package/dist/react/sidebar.d.ts.map +1 -1
- package/dist/react/sidebar.js +6 -3
- package/dist/react/sidebar.js.map +1 -1
- package/dist/react/windowDescription.d.ts +7 -0
- package/dist/react/windowDescription.d.ts.map +1 -1
- package/dist/react/windowDescription.js +9 -0
- package/dist/react/windowDescription.js.map +1 -1
- package/dist/react/windowMenus.d.ts +19 -0
- package/dist/react/windowMenus.d.ts.map +1 -0
- package/dist/react/windowMenus.js +88 -0
- package/dist/react/windowMenus.js.map +1 -0
- package/dist/shell/HostShell.d.ts +53 -0
- package/dist/shell/HostShell.d.ts.map +1 -0
- package/dist/shell/HostShell.js +116 -0
- package/dist/shell/HostShell.js.map +1 -0
- package/dist/shell/index.d.ts +7 -0
- package/dist/shell/index.d.ts.map +1 -0
- package/dist/shell/index.js +9 -0
- package/dist/shell/index.js.map +1 -0
- package/dist/shell/keys.d.ts +47 -0
- package/dist/shell/keys.d.ts.map +1 -0
- package/dist/shell/keys.js +68 -0
- package/dist/shell/keys.js.map +1 -0
- package/dist/shell/motion.d.ts +72 -0
- package/dist/shell/motion.d.ts.map +1 -0
- package/dist/shell/motion.js +118 -0
- package/dist/shell/motion.js.map +1 -0
- package/llms.txt +57 -20
- package/package.json +10 -1
- package/src/core/desk.ts +11 -2
- package/src/core/hosted.ts +351 -0
- package/src/core/index.ts +28 -1
- package/src/core/location.ts +23 -3
- package/src/core/preferences.ts +132 -0
- package/src/core/scheme.ts +16 -5
- package/src/core/theme.ts +91 -0
- package/src/core/themeChange.ts +59 -0
- package/src/core/types.ts +5 -0
- package/src/css/apps.css +52 -1
- package/src/css/shell.css +99 -0
- package/src/css/tokens.css +10 -0
- package/src/css/windows.css +28 -0
- package/src/fx/fx.ts +7 -25
- package/src/host/checkChrome.ts +257 -0
- package/src/host/checkTheme.ts +134 -0
- package/src/host/index.ts +10 -0
- package/src/host/router.ts +374 -0
- package/src/react/Desktop.tsx +16 -21
- package/src/react/Dock.tsx +34 -28
- package/src/react/MenuBar.tsx +22 -4
- package/src/react/app.tsx +116 -22
- package/src/react/appFrame.tsx +39 -14
- package/src/react/appPanels.tsx +102 -31
- package/src/react/appearance.tsx +36 -45
- package/src/react/context.tsx +99 -13
- package/src/react/dockStatus.ts +42 -0
- package/src/react/frameMenus.ts +100 -0
- package/src/react/hostApp.tsx +89 -0
- package/src/react/hostDock.tsx +118 -0
- package/src/react/hostMenu.ts +3 -38
- package/src/react/hosted.tsx +65 -0
- package/src/react/index.ts +12 -4
- package/src/react/mediaQuery.ts +18 -0
- package/src/react/shell.tsx +30 -3
- package/src/react/sidebar.tsx +10 -3
- package/src/react/windowDescription.ts +12 -1
- package/src/react/windowMenus.ts +107 -0
- package/src/shell/HostShell.tsx +220 -0
- package/src/shell/index.ts +11 -0
- package/src/shell/keys.ts +101 -0
- package/src/shell/motion.ts +163 -0
package/llms.txt
CHANGED
|
@@ -64,7 +64,8 @@ fitFrame(frame, stage, min?): Frame | null; stepFrom(frame, open: Frame[], stage
|
|
|
64
64
|
interface Desk {
|
|
65
65
|
getState(): DeskState
|
|
66
66
|
subscribe(listener: (state: DeskState) => void): () => void
|
|
67
|
-
open(id: string, options?: { mode?: 'filled' | 'floating'; frame?: Frame; route?: string }): void // default fills; a frame floats;
|
|
67
|
+
open(id: string, options?: { mode?: 'filled' | 'floating'; frame?: Frame; size?: Size; route?: string }): void // default fills; a frame floats;
|
|
68
|
+
// size: free, centred at that size, unless left filled or placed last time;
|
|
68
69
|
// a route moves it there, open already or not
|
|
69
70
|
openInstance(type: string, options?): string // 'query', then 'query#2' — returns the id it opened
|
|
70
71
|
close(id: string): void
|
|
@@ -177,7 +178,7 @@ formatShortcut('mod+comma', apple?): string // '⌘,' or 'Ctrl+,'
|
|
|
177
178
|
isApplePlatform(): boolean // the KEYBOARD; the look is separate
|
|
178
179
|
deviceLook(): Look // 'mac' | 'gnome' | 'windows' for this device
|
|
179
180
|
lookFor(platform, userAgent): Look // the same, pure; resolveLook('auto' | Look): Look
|
|
180
|
-
COLOR_SCHEME_KEY; LOOK_KEY; COLOR_SCHEME_SCRIPT
|
|
181
|
+
COLOR_SCHEME_KEY; LOOK_KEY; COLOR_SCHEME_SCRIPT; colorSchemeScript(default?) // Appearance's storage keys and <head> script; see Settings and About
|
|
181
182
|
isInstalledApp(): boolean // gate ⌘W / ⌘` on this; never take ⌘T or ⌘N
|
|
182
183
|
titleCase('ride card'): string // 'Ride Card' — app and window names; a document keeps its own name
|
|
183
184
|
isTitleCase(name): boolean // for a test over the names an app defines
|
|
@@ -264,6 +265,7 @@ useWindowRoute(): [route, setRoute] // where this window is insi
|
|
|
264
265
|
useWindowProgress({ value, max, label } | null): boolean // a real count in this window's title bar; false outside one
|
|
265
266
|
useWindowNote(note: ReactNode): boolean // this view's note in place of the window's; null defers, false hides
|
|
266
267
|
useWindowInfo(info: ReactNode): boolean // this view's (i) in place of the window's; null defers
|
|
268
|
+
useWindowBack({ label, title, onBack } | null): boolean // a view one step in: "‹ label" (named "Back to label") before the title, which becomes `title`
|
|
267
269
|
|
|
268
270
|
<Desktop
|
|
269
271
|
renderWindow={(id) => ReactNode} // memoised: chrome re-renders, content does not
|
|
@@ -348,7 +350,7 @@ explanation only — never a warning, which belongs in front of people.
|
|
|
348
350
|
```tsx
|
|
349
351
|
<Dock entries={DockEntry[]} label?="Dock" placement?={'overlay' | 'inline'} side?={'left' | 'right' | 'bottom'}
|
|
350
352
|
pins?={{ accepts, onPin: (drag, before: id | null) => void, onMove?: (id, before: id | null) => void }} />
|
|
351
|
-
dockItem({ id, label, icon, window?, description?, badge?, onSelect?, disabled?, contextMenu?: () => MenuItem[], movable
|
|
353
|
+
dockItem({ id, label, icon, window?, description?, badge?, onSelect?, disabled?, contextMenu?: () => MenuItem[], movable?, status?: { running, focused, minimized } }) // status: told by a host drawing a hosted app's item, instead of read from this desk
|
|
352
354
|
dockStack({ id, label, items: DockItem[], icon?, contextMenu?: () => MenuItem[], movable? })
|
|
353
355
|
dockSeparator(id)
|
|
354
356
|
```
|
|
@@ -371,6 +373,29 @@ interface HostMenu { id; label; primary?; status?; items: { type: 'item'|'separa
|
|
|
371
373
|
// Republished on render and focus move only; a choice is re-checked before it runs.
|
|
372
374
|
hostMenuModel(menus, status, canPerform): { model: HostMenu[]; run: Map<id, MenuItem> } // the same data, for a host built by hand
|
|
373
375
|
hasModKey(shortcut): boolean // the default ownsShortcut: has `mod` in it
|
|
376
|
+
useWindowMenus(menus: Menu[]) // inside a window: its menus join the bar while it is in front, after the app's own, before menus with ids 'window'/'help'; drawn bar and host alike
|
|
377
|
+
// A window's item never takes the app's shortcut (or Desk's mod+comma): it keeps its label, loses the key, and the clash is warned once.
|
|
378
|
+
// Hosted mode: a host draws one menu bar and dock for several Desk apps. Hosted when globalThis.deskHost is a DeskHostBridge, or <DeskProvider|DeskShell host={bridge}>.
|
|
379
|
+
// MenuBar and Dock then draw nothing and send DeskChrome { menus, dock, settings, about } to the host; shortcuts are shared as with any MenuHost (the host owns `mod` ones, ⌘, included). DockItem.image?: string for the host.
|
|
380
|
+
// The host's frame has the one Settings and About: no gear or About item in the app. settings: { id, title, group?, badge? }[] from app.settings.sections; host command 'settings' with a section id opens the app's own Settings there.
|
|
381
|
+
// about: { name, version?, links, note? (text only), facts: { title?, facts: { name, value, link?, copy?: false }[] }[] | null } from the app declaration; facts null while read, [] for nothing to report. Hosted implies themeFromHost; the primary declares themeFromHost: false to keep its Appearance and its own theme.
|
|
382
|
+
interface DeskHostBridge { version: 1; send(m: AppToHost): void; onMessage(l: (m: HostToApp) => void): () => void }
|
|
383
|
+
connectHost(bridge): HostClient // core, for a page without React; DeskProvider calls it
|
|
384
|
+
useHost(): HostClient | null // publish(topic, payload); subscribe(publisher: appId | 'primary', topic, handler); onCommand; setChrome
|
|
385
|
+
useHosted(): boolean; useHostBroadcast(publisher, topic, handler); usePublishTheme(theme: DeskTheme | null) // the primary publishes its look
|
|
386
|
+
interface DeskTheme { mode: 'light'|'dark'; look: Look; tokens: Record<`--${string}`, string>; fonts?: { family; src; weight?; style? }[]; wallpaper?: string }
|
|
387
|
+
<DeskShell onTheme={theme => …}> // keep some looks of your own; mode and look always apply. Default: applyTheme(theme)
|
|
388
|
+
hostDockModel(entries, state): { model: HostDockEntry[]; run: Map<id, DockItem> } // the dock as data, as HostedDock sends it
|
|
389
|
+
// '@liberation-data/desk/host' (the host's half): createBroadcastRouter({ deliver, chrome?: (appId, chrome, dropped: string[]) => void, check?, maxPayload?, limits?: { burst?, perSecond?, topics?, maxTopics? }, clock? }), router.limited(appId), checkTheme(theme, origin), checkChrome(chrome, origin): { ok: true, chrome, dropped } | { ok: false, reason }, createMemoryHost()
|
|
390
|
+
// '@liberation-data/desk/shell' (the host's page only, with css/shell.css): <HostShell workspaces={{ id, title, accent?, icon?, primary?, state: 'live'|'suspended'|'discarded' }[]} current chrome={Record<appId, DeskChrome|null>} theme? snapshots?={Record<id, imageUrl>} keys?={SwitchKeys|null} clickSwitch?={'slide'} dockSide? onSwitch={(id, how) => …} onCommand={(appId, 'dock'|'menu'|'settings', itemId) => …} onInsets?={({ top, right, bottom, left }) => …} />
|
|
391
|
+
// It draws the current app's dock and a switcher (one Tab stop, arrows/Home/End, title announced once on arrival); the host moves the views and passes the new current.
|
|
392
|
+
// switchKeys(platformOf(process.platform)): mac Ctrl+Opt+←/→/↑, Linux Super+Alt, Windows Ctrl+Alt; switchKeyAction(pageEvent | electronInput, keys): 'previous'|'next'|'overview'|null (key releases ignored); neighbour(workspaces, current, step) | null at the ends
|
|
393
|
+
// slidePlan(reducedMotion): { kind: 'slide', 350 } | { kind: 'fade', 120 }; spring(ms, from?, velocity?) → t => progress, no overshoot; slideOffsets(progress, 1|-1, width): { leaving, arriving } whole px, no gap: clip views to the desktop area or they cover the dock
|
|
394
|
+
// animate({ durationMs, step, curve?, clock?: pageClock()|timerClock(), timeoutMs? = 250 }): { done: 'finished'|'jumped'|'stopped', progress(), stop() }; jumps to 1 when frames stop
|
|
395
|
+
// Rate limits per publisher+topic: 20 then 10/s (theme 2 then 1/s), 64 topics at once; over it the latest is held (never queued), so subscribers end on the last value; order holds within a topic.
|
|
396
|
+
// register({ id, origin, primary?, publishes? }): the primary's publishes must include 'theme'. A deliver that throws for one app still lets the rest hear; the error is thrown after.
|
|
397
|
+
// chrome(appId, null) when an app is unregistered: stop drawing its chrome. The primary's last theme is kept while it is down and replayed to subscribers of 'primary'.
|
|
398
|
+
// The router checks chrome before chrome() sees it: bounded text and lists, links http(s), mailto: or the app's origin, dock images from the app's origin or data: raster (no SVG). A bad part (menu item, dock item, section, link, fact; an image or fact link from its item) is dropped and named in dropped; the rest is drawn. Only the wrong shape (not lists, too large, a stack in a stack) refuses it whole. Open About links in the system browser, rel="noopener noreferrer".
|
|
374
399
|
interface Menu { id: string; label: string; title?: ReactNode; emphasis?: boolean; items: MenuItem[] | (() => MenuItem[]) }
|
|
375
400
|
// emphasis: the app's own menu, its name in bold, conventionally first
|
|
376
401
|
menuCommand(label, command, { shortcut?, checked?, detail?, args? })
|
|
@@ -397,26 +422,33 @@ would. `items` as a function is re-read on every open.
|
|
|
397
422
|
|
|
398
423
|
```tsx
|
|
399
424
|
<DeskShell app={DeskApp}> // or DeskProvider app={…}; read on every render, so it may change
|
|
400
|
-
interface DeskApp { name; icon?; version?; description?; links?: { label, url }[]; legal?;
|
|
425
|
+
interface DeskApp { name; icon?; wordmark?: ReactNode; version?; description?; links?: { label, url, group? }[]; legal?; // wordmark: a wide mark under the icon; group: links under their heading, ungrouped first
|
|
401
426
|
note?: ReactNode; // a line under the links, not copied
|
|
402
427
|
facts?: () => AboutSection[] | null; // a HOOK, called inside the About panel; null = still reading, [] = nothing, no Copy
|
|
403
428
|
settings?: AppSettings } // leave out: no gear, no "Settings…", no ⌘,
|
|
404
|
-
interface AboutSection { title?; facts: { name; value: string; note?: ReactNode; link?: { label, url }; copy?: false }[] } // copy:false = shown, not copied; note and link never copied
|
|
405
|
-
interface AppSettings { sections?: SettingsSection[]; layout?: 'panel' | 'sidebar'; badge?: ReactNode;
|
|
429
|
+
interface AboutSection { title?; pending?: boolean; facts: { name; value: string; copyValue?: string; note?: ReactNode; link?: { label, url }; copy?: false }[] } // copy:false = shown, not copied; note and link never copied; copyValue copied in place of value; pending = "Checking", left out of Copy
|
|
430
|
+
interface AppSettings { sections?: SettingsSection[]; layout?: 'panel' | 'sidebar'; badge?: ReactNode; limits?: WindowLimits; // limits: the sidebar layout's
|
|
406
431
|
open?: (route?: string) => void } // open: the app's own Settings window; Desk draws none
|
|
407
|
-
interface SettingsSection { id; title; group?; icon?; badge?; content: ReactNode } // id = the route of `settings
|
|
408
|
-
// AppSettings.appearance?: true | { scheme?; look?; sounds?: Fx; soundsDescription?; theme?: ReactNode } // Desk's first section
|
|
432
|
+
interface SettingsSection { id; title; group?; icon?; badge?; note?: ReactNode; content: ReactNode } // id = the route of `settings`; note: the window's note while open (one page: under the heading)
|
|
433
|
+
// AppSettings.appearance?: true | { scheme?; defaultScheme?: 'system'|'light'|'dark'; look?; sounds?: Fx; soundsDescription?; theme?: ReactNode } // Desk's first section
|
|
434
|
+
// defaultScheme: what applies while nothing is chosen (default 'system'); give colorSchemeScript the same
|
|
409
435
|
// DeskApp.themeFromHost?: boolean // receives its theme from a host: no scheme/look/theme rows (sounds stay); default: found from the bridge
|
|
410
|
-
useColorScheme(): 'system'|'light'|'dark'; colorScheme(); setColorScheme(s) //
|
|
411
|
-
useLookChoice(): LookChoice | null; lookChoice(); setLookChoice(l) //
|
|
412
|
-
COLOR_SCHEME_SCRIPT; COLOR_SCHEME_KEY = 'desk.scheme'; LOOK_KEY = 'desk.look' // also from the core entry (no React)
|
|
413
|
-
// script: for <head>, the remembered scheme before first paint; keys: for migrations
|
|
436
|
+
useColorScheme(): 'system'|'light'|'dark'; colorScheme(); setColorScheme(s) // the preference store, desk.scheme (absent = the default); data-theme on the root
|
|
437
|
+
useLookChoice(): LookChoice | null; lookChoice(); setLookChoice(l) // the preference store, desk.look; replaces the `look` prop
|
|
438
|
+
COLOR_SCHEME_SCRIPT; colorSchemeScript(defaultScheme?); COLOR_SCHEME_KEY = 'desk.scheme'; LOOK_KEY = 'desk.look' // also from the core entry (no React)
|
|
439
|
+
// script: for <head>, the remembered scheme (or the default) before first paint; keys: for migrations
|
|
440
|
+
interface PreferenceStore { get(key): string | null | undefined; set(key, value: string | null): void; subscribe?(fn: (key | null) => void): () => unsubscribe }
|
|
441
|
+
<DeskShell preferences={store}> // every remembered choice (COLOR_SCHEME_KEY, LOOK_KEY, createFx's storageKey) in the app's store; default localStorage (+ other tabs)
|
|
442
|
+
// get is sync; undefined = not loaded yet → this browser's copy; Desk writes values through to localStorage for the head script
|
|
443
|
+
setPreferenceStore(store?); getPreference(key); setPreference(key, value); subscribePreferences(fn); localPreferences // core entry, no React
|
|
414
444
|
<AppearanceSettings rows look /> // the rows, for an app's own Settings window
|
|
415
|
-
dockSettings()
|
|
445
|
+
dockSettings(); dockAbout() // markers: where the gear / About go, among dock entries OR in a stack's items
|
|
446
|
+
// (a System stack); placed, Desk adds no loose one. Default: end of the system group
|
|
416
447
|
SETTINGS_WINDOW = 'settings'; ABOUT_WINDOW = 'about'; SettingsCommand = 'desk.settings'; SYSTEM_GROUP = 'system'
|
|
417
|
-
PANEL_LIMITS; SIDEBAR_LIMITS
|
|
448
|
+
PANEL_LIMITS; SIDEBAR_LIMITS; SIDEBAR_SIZE // the panel fits its content; the sidebar layout opens free at SIDEBAR_SIZE (720 × 640), resizable and fillable; Desktop `limits` for those ids overrides
|
|
418
449
|
openAbout(desk) // About as the dock opens it: a centred panel, or forward where it is
|
|
419
450
|
useOpenSettings(): (route?) => void // Settings as the gear opens it (the app's own `open` too); nothing without settings
|
|
451
|
+
openSettings(desk, route?) // the same, outside React (search results, link handlers); reads the provider's declaration
|
|
420
452
|
appWindowTitle(app, id): string | null // 'Settings' / 'About' for Desk's windows, for a Window menu's title function
|
|
421
453
|
<AboutContent />; <SettingsContent />; aboutText(app, sections): string
|
|
422
454
|
useDeskApp(): DeskApp | null
|
|
@@ -426,11 +458,13 @@ Declared, Desk adds: in the Dock, the gear (when there are settings) and About a
|
|
|
426
458
|
group (entries after `dockSeparator('system')`; a separator and group of their own when there is none); in the
|
|
427
459
|
MenuBar's app menu (first with `emphasis`, else one is made, named `name`) "About <name>", separator,
|
|
428
460
|
"Settings…" (mod+comma), separator, then the app's items; Desktop draws the `about` and (with `settings` and no `open`) `settings` windows as
|
|
429
|
-
centred panels (sidebar layout: a
|
|
461
|
+
centred panels (sidebar layout: a window like any other, with a Sidebar grouped by `group`; on a narrow screen,
|
|
462
|
+
(max-width: 600px), the list alone, then the section, its title in the title row after "‹ Settings"; arrows move, Enter opens). Gear, menu and ⌘, focus an
|
|
430
463
|
open panel where it is. ⌘, is bound by the provider on the command, so it works with no MenuBar; the menu item only
|
|
431
464
|
shows it. A `host` menu bar gets the same items through the same publish; the host owns ⌘, while it is up.
|
|
432
465
|
With facts, About has Copy: name + version, then each section's copyable facts. Never put credentials in facts.
|
|
433
466
|
Don't also add your own About/Settings dock or menu items: they would be there twice.
|
|
467
|
+
`syncWithLocation` keeps links to `settings` and `about` once a provider has declared the app: `isKnown` lists only the app's own ids.
|
|
434
468
|
|
|
435
469
|
### Commands and input
|
|
436
470
|
|
|
@@ -483,7 +517,7 @@ useServiceValue(service): T // the value, live, without
|
|
|
483
517
|
### Generated apps
|
|
484
518
|
|
|
485
519
|
```tsx
|
|
486
|
-
<AppFrame title src?|srcDoc? listens?={string[]} says?={string[]} accepts?={Accepts} opens?={string[]}
|
|
520
|
+
<AppFrame title src?|srcDoc? listens?={string[]} says?={string[]} accepts?={Accepts} opens?={string[]} menus?={boolean}
|
|
487
521
|
theme?={{ mode: 'light'|'dark', tokens: Record<string,string> }}
|
|
488
522
|
sandbox?="allow-scripts allow-forms" />
|
|
489
523
|
// theme: sent with hello and again on change; the bridge sets the tokens as custom properties on the
|
|
@@ -493,7 +527,7 @@ APP_BRIDGE_SCRIPT // the same script, to include some other w
|
|
|
493
527
|
```
|
|
494
528
|
|
|
495
529
|
Inside the page: `desk.on(topic, message => …)`, `desk.onDrop(message => …)`, `desk.publish(topic, payload)`,
|
|
496
|
-
`desk.open(window)`, `desk.theme`, `desk.onTheme(fn)`, and `desk.window` once connected. Messages carry `desk: 1`. The host ignores anything not
|
|
530
|
+
`desk.open(window)`, `desk.theme`, `desk.onTheme(fn)`, `desk.setMenus(HostMenu[])` and `desk.onCommand(id => …)` (with `menus`: they join the bar while the frame's window is in front; checked with checkChrome, at most 4 menus; ids namespaced per frame; a shortcut needs mod/ctrl/alt or is F1–F24, else the item loses its key (warned once); a choice goes back to that frame only), and `desk.window` once connected. Messages carry `desk: 1`. The host ignores anything not
|
|
497
531
|
from its own frame, drops publishes outside `says` and opens outside `opens`, never echoes an app its own
|
|
498
532
|
events, and sets the frame's `pointer-events: none` while a drag is in the air so drops reach it.
|
|
499
533
|
|
|
@@ -585,7 +619,7 @@ interface Column<Row> { key; header; render?; align?; numeric?; sortable?; width
|
|
|
585
619
|
interface Sort { key: string; direction: 'ascending' | 'descending' }
|
|
586
620
|
|
|
587
621
|
<Sidebar sections={{ id, label?, items: { id, label, icon?, badge?, disabled? }[] }[]}
|
|
588
|
-
value onChange label footer? />
|
|
622
|
+
value onChange label footer? focusOnly? /> // focusOnly: arrows move focus only, Enter/click chooses (a list shown alone)
|
|
589
623
|
|
|
590
624
|
<Outline rows={{ id, label, depth, disabled? }[]} value={string | null} onChange showing? label />
|
|
591
625
|
```
|
|
@@ -667,8 +701,11 @@ Every value reads a `--desk-*` token; redefine tokens, never override selectors.
|
|
|
667
701
|
`--desk-ease`, `--desk-dock-size`, `--desk-dock-glass`, `--desk-dock-offset`, `--desk-dock-room`,
|
|
668
702
|
`--desk-widget-width`, `--desk-inset-bottom`, `--desk-wizard-height`, `--desk-field-gap`. Code colours (`CodeBlock`, on `--desk-page`):
|
|
669
703
|
`--desk-code-comment|string|keyword|number|key`, 4.5:1 in light and dark.
|
|
704
|
+
An overview background for a workspace switcher: `--desk-overview-fog` (centre; default ground 82% + accent 18%,
|
|
705
|
+
nudged toward mid grey), `--desk-overview-fog-far` (edge; the fog 8% darker light, 10% dark) and `--desk-overview-shadow`
|
|
706
|
+
(tile shadow colour; `rgb(0 0 0 / 0.28)` light, `0.55` dark). Derived in CSS; a theme's own values win; checkTheme takes them.
|
|
670
707
|
|
|
671
|
-
Light and dark follow `prefers-color-scheme`; `data-theme="light" | "dark"` on the root wins. The look is
|
|
708
|
+
Light and dark follow `prefers-color-scheme`; `data-theme="light" | "dark"` on the root wins. Outside React (a canvas), listen for `desk-themechange` on `document`: once per change to the drawn scheme or look, from any source (host theme, new colours included through Desk's handler, the person's choice, the system flipping, a look change), `detail: { scheme, look }`; `drawnTheme()` reads them; with no desk mounted and no theme applied, call `watchThemeChanges()` first. In React use the hooks. The look is
|
|
672
709
|
`data-look="mac" | "gnome" | "windows"` on the root, set by the desk; don't set it yourself — pass `look`.
|
|
673
710
|
|
|
674
711
|
## Sounds: `@liberation-data/desk/fx`
|
|
@@ -711,7 +748,7 @@ in the accessibility tree.
|
|
|
711
748
|
- A `DeskProvider` already carries an event bus; `BusProvider` is only for supplying or sharing one.
|
|
712
749
|
- `renderWindow` must be stable (module scope or `useCallback`) for the memoisation to hold.
|
|
713
750
|
- `parse`/`syncWithLocation` warn once when no `isKnown` is given: unknown ids from a URL are then trusted.
|
|
714
|
-
- `npm run size` enforces a gzipped
|
|
751
|
+
- `npm run size` enforces a budget on each entry, measured gzipped without comments (so explanations cost nothing): core 13.5 KiB, react 70.5, css 14, fx 4, host 7.5; each is the measured size plus 1 KiB, rounded up to a half.
|
|
715
752
|
- `fx.play` never resumes audio itself: outside a press a browser's resume may never settle. Call
|
|
716
753
|
`fx.unlock()` at startup, or nothing sounds until a press after the sound was due.
|
|
717
754
|
- The package is ESM only, with `"sideEffects": ["**/*.css"]`.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@liberation-data/desk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.45.0",
|
|
4
4
|
"description": "A desktop environment for React apps: windows, a dock, menus and commands, controls, tours and setup assistants.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react",
|
|
@@ -42,6 +42,14 @@
|
|
|
42
42
|
"types": "./dist/react/index.d.ts",
|
|
43
43
|
"import": "./dist/react/index.js"
|
|
44
44
|
},
|
|
45
|
+
"./host": {
|
|
46
|
+
"types": "./dist/host/index.d.ts",
|
|
47
|
+
"import": "./dist/host/index.js"
|
|
48
|
+
},
|
|
49
|
+
"./shell": {
|
|
50
|
+
"types": "./dist/shell/index.d.ts",
|
|
51
|
+
"import": "./dist/shell/index.js"
|
|
52
|
+
},
|
|
45
53
|
"./fx": {
|
|
46
54
|
"types": "./dist/fx/index.d.ts",
|
|
47
55
|
"import": "./dist/fx/index.js"
|
|
@@ -78,6 +86,7 @@
|
|
|
78
86
|
"puppeteer-core": "^25.11.0",
|
|
79
87
|
"react": "^19.3.0",
|
|
80
88
|
"react-dom": "^19.3.0",
|
|
89
|
+
"rolldown": "~1.2.8",
|
|
81
90
|
"typescript": "^7.0.2",
|
|
82
91
|
"vite": "^8.3.0",
|
|
83
92
|
"vitest": "^5.0.0"
|
package/src/core/desk.ts
CHANGED
|
@@ -139,6 +139,15 @@ export function nextCascadeFrame(windows: readonly DeskWindow[], stage: Size, op
|
|
|
139
139
|
return cascadeFrame(taken.size % options.wrap, stage, options)
|
|
140
140
|
}
|
|
141
141
|
|
|
142
|
+
/** A frame of this size in the middle of the stage, made smaller where the stage is, keeping a margin. */
|
|
143
|
+
export function centredFrame(size: Size, stage: Size, margin: number): Frame {
|
|
144
|
+
// A stage not measured yet has no size to fit to: the size as asked, at the margin.
|
|
145
|
+
if (stage.width <= 0 || stage.height <= 0) return { x: margin, y: margin, ...size }
|
|
146
|
+
const width = Math.max(0, Math.min(size.width, stage.width - margin * 2))
|
|
147
|
+
const height = Math.max(0, Math.min(size.height, stage.height - margin * 2))
|
|
148
|
+
return { x: Math.round((stage.width - width) / 2), y: Math.round((stage.height - height) / 2), width, height }
|
|
149
|
+
}
|
|
150
|
+
|
|
142
151
|
/** Is this frame wholly on a desk of this size? */
|
|
143
152
|
const withinStage = (frame: Frame, stage: Size) =>
|
|
144
153
|
frame.x >= 0 && frame.y >= 0 && frame.x + frame.width <= stage.width && frame.y + frame.height <= stage.height
|
|
@@ -279,10 +288,10 @@ export function createDesk(options: DeskOptions = {}): Desk {
|
|
|
279
288
|
}
|
|
280
289
|
const saved = opts.mode || opts.frame ? undefined : layouts?.load(windowType(id))
|
|
281
290
|
const kept = saved?.mode === 'floating' ? fitFrame(saved.frame, stage()) : null
|
|
282
|
-
const mode = opts.mode ?? (opts.frame || kept ? 'floating' : 'filled')
|
|
291
|
+
const mode = opts.mode ?? (opts.frame || kept || (opts.size && !saved) ? 'floating' : 'filled')
|
|
283
292
|
const frame = () => {
|
|
284
293
|
if (opts.frame) return opts.frame
|
|
285
|
-
if (!kept) return nextFrame(state)
|
|
294
|
+
if (!kept) return opts.size ? centredFrame(opts.size, stage(), cascade.margin) : nextFrame(state)
|
|
286
295
|
const siblings = state.stack.flatMap(other => {
|
|
287
296
|
const w = find(other)
|
|
288
297
|
return w?.mode === 'floating' && windowType(w.id) === windowType(id) ? [w.frame] : []
|
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
import { topicMatches } from './events.js'
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* HOSTED MODE: SEVERAL DESK APPS UNDER ONE MENU BAR AND DOCK.
|
|
5
|
+
*
|
|
6
|
+
* A host — a native shell such as Electron, or a web page framing several apps — can run more than
|
|
7
|
+
* one Desk app side by side and draw one menu bar and one dock above all of them, so moving between
|
|
8
|
+
* apps changes only what fills the desktop. Each app then draws no chrome of its own: it sends its
|
|
9
|
+
* menus and dock to the host as plain data, and the host sends back what the person chose.
|
|
10
|
+
*
|
|
11
|
+
* Apps never talk to each other directly. Everything goes through the host, which knows which app
|
|
12
|
+
* each message came from and stamps it on what it passes on: an app cannot speak as another. An app
|
|
13
|
+
* publishes on a topic, and apps that asked for that publisher's topic hear it. The first topic is
|
|
14
|
+
* the theme: one app, the primary, sets the look every app wears.
|
|
15
|
+
*
|
|
16
|
+
* The transport is the host's: an Electron preload, or `postMessage` between frames. Either way it
|
|
17
|
+
* is a `DeskHostBridge`, found on `globalThis.deskHost`, and every message carries the protocol
|
|
18
|
+
* number, so a mismatch is ignored rather than misread.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/** The protocol version. Every message carries it as `desk`, framed apps' messages included. */
|
|
22
|
+
export const DESK_PROTOCOL = 1
|
|
23
|
+
|
|
24
|
+
/** The topic of the look every app wears: a `DeskTheme`. Only the primary may publish it. */
|
|
25
|
+
export const THEME_TOPIC = 'theme'
|
|
26
|
+
|
|
27
|
+
/** One row of a menu a host draws. Plain data, so it can cross a process boundary. */
|
|
28
|
+
export interface HostMenuItem {
|
|
29
|
+
readonly type: 'item' | 'separator' | 'header'
|
|
30
|
+
/**
|
|
31
|
+
* Present on 'item'; unique within one publish. Opaque to a host, which only sends it back: it
|
|
32
|
+
* names the item's menu, place and label, so a choice from a menu that has since changed runs
|
|
33
|
+
* nothing rather than whatever took that place.
|
|
34
|
+
*/
|
|
35
|
+
readonly id?: string
|
|
36
|
+
/** Present on 'item' and 'header'. */
|
|
37
|
+
readonly label?: string
|
|
38
|
+
readonly detail?: string
|
|
39
|
+
/** In desk's format: 'mod+k', 'mod+alt+a', 'alt+]'. */
|
|
40
|
+
readonly shortcut?: string
|
|
41
|
+
/**
|
|
42
|
+
* The page handles this item's shortcut itself, so a host draws it beside the item and gives it
|
|
43
|
+
* no key (Electron: `registerAccelerator: false`). Set on an action's shortcut, which the drawn
|
|
44
|
+
* bar only shows too: binding it as well would fire the action twice.
|
|
45
|
+
*/
|
|
46
|
+
readonly showOnly?: boolean
|
|
47
|
+
/** Defaults to true. */
|
|
48
|
+
readonly enabled?: boolean
|
|
49
|
+
/** Absent means the item is not checkable. */
|
|
50
|
+
readonly checked?: boolean
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** A whole menu as a host sees it. */
|
|
54
|
+
export interface HostMenu {
|
|
55
|
+
readonly id: string
|
|
56
|
+
/** The menu's `label`: a host draws text, so a status menu's richer `title` stays behind. */
|
|
57
|
+
readonly label: string
|
|
58
|
+
/** The app's own menu, which a `Menu` calls `emphasis`. */
|
|
59
|
+
readonly primary?: boolean
|
|
60
|
+
/** One of the bar's status menus, which a host puts after the others. */
|
|
61
|
+
readonly status?: boolean
|
|
62
|
+
readonly items: readonly HostMenuItem[]
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** One dock item as a host draws it: what the app's dock would show, as data. */
|
|
66
|
+
export interface HostDockItem {
|
|
67
|
+
readonly type: 'item'
|
|
68
|
+
/** Sent back in a `command` when the item is chosen. */
|
|
69
|
+
readonly id: string
|
|
70
|
+
readonly label: string
|
|
71
|
+
readonly description?: string
|
|
72
|
+
/** An image URL or `data:` URI from the item's `image`; a host without one draws the label. */
|
|
73
|
+
readonly image?: string
|
|
74
|
+
/** The badge as text; `''` for a badge that is not text, which a host draws as a dot. */
|
|
75
|
+
readonly badge?: string
|
|
76
|
+
readonly disabled?: boolean
|
|
77
|
+
readonly running: boolean
|
|
78
|
+
readonly focused: boolean
|
|
79
|
+
readonly minimized: boolean
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** A dock stack as a host draws it: a folder of items that opens on the host's dock. */
|
|
83
|
+
export interface HostDockStack {
|
|
84
|
+
readonly type: 'stack'
|
|
85
|
+
readonly id: string
|
|
86
|
+
readonly label: string
|
|
87
|
+
readonly image?: string
|
|
88
|
+
readonly items: readonly HostDockItem[]
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** One place on the dock a host draws. */
|
|
92
|
+
export type HostDockEntry = HostDockItem | HostDockStack | { readonly type: 'separator'; readonly id: string }
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* A section of the app's Settings, for the host's one Settings, which lists each app's sections. Its
|
|
96
|
+
* controls are the app's and stay in the app: choosing one sends a `settings` command with its id, and
|
|
97
|
+
* the app opens its own Settings there.
|
|
98
|
+
*/
|
|
99
|
+
export interface HostSettingsSection {
|
|
100
|
+
readonly id: string
|
|
101
|
+
readonly title: string
|
|
102
|
+
/** The heading it is listed under, as in a sidebar of sections. */
|
|
103
|
+
readonly group?: string
|
|
104
|
+
/** As text; `''` for a badge that is not text. */
|
|
105
|
+
readonly badge?: string
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** One fact of the app's About: what something is, and its value. */
|
|
109
|
+
export interface HostAboutFact {
|
|
110
|
+
readonly name: string
|
|
111
|
+
readonly value: string
|
|
112
|
+
/** Where to read more, shown with the fact as a link. Never copied. */
|
|
113
|
+
readonly link?: { readonly label: string; readonly url: string }
|
|
114
|
+
/** `false` keeps it out of what the host copies for a bug report. It is still shown. */
|
|
115
|
+
readonly copy?: false
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export interface HostAboutSection {
|
|
119
|
+
readonly title?: string
|
|
120
|
+
readonly facts: readonly HostAboutFact[]
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* What the app says about itself, for its section of the host's one About. Its links, and its facts',
|
|
125
|
+
* are http(s), `mailto:` or on the app's origin (`checkChrome`); a host opens them in the system
|
|
126
|
+
* browser or mail app, or in a new window with `rel="noopener noreferrer"`, never in its own frame.
|
|
127
|
+
*/
|
|
128
|
+
export interface HostAbout {
|
|
129
|
+
readonly name: string
|
|
130
|
+
readonly version?: string
|
|
131
|
+
readonly links: readonly { readonly label: string; readonly url: string }[]
|
|
132
|
+
/** A short line under the links, never copied: what this build can and cannot say. Text only. */
|
|
133
|
+
readonly note?: string
|
|
134
|
+
/** Null while the app is still reading them; `[]` when there is nothing to report, and nothing to copy. */
|
|
135
|
+
readonly facts: readonly HostAboutSection[] | null
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Everything an app would draw around its desktop, for the host to draw instead. The host's frame has
|
|
140
|
+
* the one Settings and About: an app sends its sections and facts, and draws no gear and no About item
|
|
141
|
+
* of its own. Widgets are not in it: they stay on the app's desktop.
|
|
142
|
+
*/
|
|
143
|
+
export interface DeskChrome {
|
|
144
|
+
readonly menus: readonly HostMenu[]
|
|
145
|
+
readonly dock: readonly HostDockEntry[]
|
|
146
|
+
/** Empty for an app with no settings; one entry with the id `''` for one whose Settings is a window of its own. */
|
|
147
|
+
readonly settings: readonly HostSettingsSection[]
|
|
148
|
+
/** Null for an app that declared nothing about itself. */
|
|
149
|
+
readonly about: HostAbout | null
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Who sent a broadcast, as the host stamped it. */
|
|
153
|
+
export interface BroadcastSender {
|
|
154
|
+
readonly id: string
|
|
155
|
+
/** Present when the sender is the app whose theme every app wears. */
|
|
156
|
+
readonly role?: 'primary'
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Whose broadcasts a subscription is for: an app's id, or `'primary'` for whichever app is the
|
|
161
|
+
* primary, so a subscriber need not change when the host makes another app primary.
|
|
162
|
+
*/
|
|
163
|
+
export type Publisher = 'primary' | (string & {})
|
|
164
|
+
|
|
165
|
+
/** What a host's command is for: the app's menus, its dock, or its Settings. */
|
|
166
|
+
export type CommandTarget = 'menu' | 'dock' | 'settings'
|
|
167
|
+
|
|
168
|
+
/** What an app sends its host. Untrusted at the host's end: check it with `isAppToHost`. */
|
|
169
|
+
export type AppToHost =
|
|
170
|
+
| { readonly desk: 1; readonly kind: 'chrome'; readonly chrome: DeskChrome }
|
|
171
|
+
| { readonly desk: 1; readonly kind: 'publish'; readonly topic: string; readonly payload: unknown }
|
|
172
|
+
| { readonly desk: 1; readonly kind: 'subscribe'; readonly publisher: Publisher; readonly topic: string }
|
|
173
|
+
| { readonly desk: 1; readonly kind: 'unsubscribe'; readonly publisher: Publisher; readonly topic: string }
|
|
174
|
+
|
|
175
|
+
/** What a host sends an app: a choice from the chrome it draws, or another app's broadcast, stamped. */
|
|
176
|
+
export type HostToApp =
|
|
177
|
+
/** A menu item or dock item chosen by its id, or Settings opened at a section's id (`''` for the first). */
|
|
178
|
+
| { readonly desk: 1; readonly kind: 'command'; readonly target: CommandTarget; readonly id: string }
|
|
179
|
+
| { readonly desk: 1; readonly kind: 'broadcast'; readonly from: BroadcastSender; readonly topic: string; readonly payload: unknown }
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* The app's end of the connection to its host. The host provides it — an Electron preload through
|
|
183
|
+
* `contextBridge`, a framed page through `postMessage` — as `globalThis.deskHost`.
|
|
184
|
+
*/
|
|
185
|
+
export interface DeskHostBridge {
|
|
186
|
+
/** The protocol the host speaks. An app that does not speak it runs unhosted. */
|
|
187
|
+
readonly version: number
|
|
188
|
+
send(message: AppToHost): void
|
|
189
|
+
/** Calls the listener with each message from the host; returns an unsubscribe. */
|
|
190
|
+
onMessage(listener: (message: HostToApp) => void): () => void
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const isRecord = (value: unknown): value is Record<string, unknown> => typeof value === 'object' && value !== null
|
|
194
|
+
|
|
195
|
+
/** Whether `value` has a bridge's shape: `globalThis.deskHost` is set by code this page does not own. */
|
|
196
|
+
export function isDeskHostBridge(value: unknown): value is DeskHostBridge {
|
|
197
|
+
return isRecord(value) && typeof value.version === 'number' && typeof value.send === 'function' && typeof value.onMessage === 'function'
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** The host this page runs in, when one put a bridge that speaks this protocol on `globalThis.deskHost`; null otherwise. */
|
|
201
|
+
export function findDeskHost(): DeskHostBridge | null {
|
|
202
|
+
const candidate = (globalThis as { deskHost?: unknown }).deskHost
|
|
203
|
+
return isDeskHostBridge(candidate) && candidate.version === DESK_PROTOCOL ? candidate : null
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const isString = (value: unknown): value is string => typeof value === 'string'
|
|
207
|
+
const isList = (value: unknown): value is unknown[] => Array.isArray(value)
|
|
208
|
+
|
|
209
|
+
/** Whether a message from an app has the shape of one. A host checks before acting on it: the app is not trusted. */
|
|
210
|
+
export function isAppToHost(value: unknown): value is AppToHost {
|
|
211
|
+
if (!isRecord(value) || value.desk !== DESK_PROTOCOL) return false
|
|
212
|
+
switch (value.kind) {
|
|
213
|
+
case 'chrome': {
|
|
214
|
+
const chrome = value.chrome
|
|
215
|
+
return isRecord(chrome) && isList(chrome.menus) && isList(chrome.dock) && isList(chrome.settings) && (chrome.about === null || isRecord(chrome.about))
|
|
216
|
+
}
|
|
217
|
+
case 'publish':
|
|
218
|
+
return isString(value.topic) && value.topic.length > 0
|
|
219
|
+
case 'subscribe':
|
|
220
|
+
case 'unsubscribe':
|
|
221
|
+
return isString(value.publisher) && isString(value.topic) && value.topic.length > 0
|
|
222
|
+
default:
|
|
223
|
+
return false
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** Whether a message from a host has the shape of one. */
|
|
228
|
+
export function isHostToApp(value: unknown): value is HostToApp {
|
|
229
|
+
if (!isRecord(value) || value.desk !== DESK_PROTOCOL) return false
|
|
230
|
+
switch (value.kind) {
|
|
231
|
+
case 'command':
|
|
232
|
+
return (value.target === 'menu' || value.target === 'dock' || value.target === 'settings') && isString(value.id)
|
|
233
|
+
case 'broadcast':
|
|
234
|
+
return isRecord(value.from) && isString(value.from.id) && isString(value.topic)
|
|
235
|
+
default:
|
|
236
|
+
return false
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** A broadcast as an app hears it. */
|
|
241
|
+
export interface Broadcast<T = unknown> {
|
|
242
|
+
readonly from: BroadcastSender
|
|
243
|
+
readonly topic: string
|
|
244
|
+
readonly payload: T
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/** The app's side of hosted mode, over a bridge: what it sends the host, and what it hears back. */
|
|
248
|
+
export interface HostClient {
|
|
249
|
+
readonly bridge: DeskHostBridge
|
|
250
|
+
/** Replaces part of the chrome. Changes made in one task go to the host as one message. */
|
|
251
|
+
setChrome(part: Partial<DeskChrome>): void
|
|
252
|
+
/** Calls the listener with the id of each menu item, dock item or Settings section the host says was chosen. */
|
|
253
|
+
onCommand(target: CommandTarget, listener: (id: string) => void): () => void
|
|
254
|
+
/** Says something on a topic. The host passes it on only if this app may publish it. */
|
|
255
|
+
publish(topic: string, payload: unknown): void
|
|
256
|
+
/**
|
|
257
|
+
* Hears `publisher`'s broadcasts on `topic` (`ride.*` takes a branch). The host replays the last
|
|
258
|
+
* one straight away, so an app that starts late still hears the current theme.
|
|
259
|
+
*/
|
|
260
|
+
subscribe<T = unknown>(publisher: Publisher, topic: string, handler: (broadcast: Broadcast<T>) => void): () => void
|
|
261
|
+
/** Stops listening, and takes the chrome away. */
|
|
262
|
+
close(): void
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const EMPTY_CHROME: DeskChrome = { menus: [], dock: [], settings: [], about: null }
|
|
266
|
+
|
|
267
|
+
/** Connects an app to its host over `bridge`. `DeskProvider` does this for a React app; a page without React calls it itself. */
|
|
268
|
+
export function connectHost(bridge: DeskHostBridge): HostClient {
|
|
269
|
+
let chrome = EMPTY_CHROME
|
|
270
|
+
let queued = false
|
|
271
|
+
let closed = false
|
|
272
|
+
const commands: Record<CommandTarget, Set<(id: string) => void>> = { menu: new Set(), dock: new Set(), settings: new Set() }
|
|
273
|
+
type Handler = (broadcast: Broadcast<never>) => void
|
|
274
|
+
/** Per subscription, the broadcasts last heard on each topic it covers, for a second handler that joins later. */
|
|
275
|
+
const subscriptions = new Map<string, { publisher: Publisher; topic: string; handlers: Set<Handler>; last: Map<string, Broadcast> }>()
|
|
276
|
+
const keyOf = (publisher: string, topic: string) => `${publisher}\u0000${topic}`
|
|
277
|
+
|
|
278
|
+
const stop = bridge.onMessage(message => {
|
|
279
|
+
if (closed || !isHostToApp(message)) return
|
|
280
|
+
if (message.kind === 'command') {
|
|
281
|
+
const listeners = commands[message.target]
|
|
282
|
+
// Copied, and each still checked: a listener may take another away while this one runs.
|
|
283
|
+
for (const listener of [...listeners]) if (listeners.has(listener)) listener(message.id)
|
|
284
|
+
return
|
|
285
|
+
}
|
|
286
|
+
const { from, topic, payload } = message
|
|
287
|
+
for (const { publisher, topic: pattern, handlers, last } of [...subscriptions.values()]) {
|
|
288
|
+
const fromThem = publisher === 'primary' ? from.role === 'primary' : publisher === from.id
|
|
289
|
+
if (!fromThem || !topicMatches(pattern, topic)) continue
|
|
290
|
+
const broadcast: Broadcast = { from, topic, payload }
|
|
291
|
+
last.set(topic, broadcast)
|
|
292
|
+
for (const handler of [...handlers]) if (handlers.has(handler)) (handler as (b: Broadcast) => void)(broadcast)
|
|
293
|
+
}
|
|
294
|
+
})
|
|
295
|
+
|
|
296
|
+
return {
|
|
297
|
+
bridge,
|
|
298
|
+
setChrome(part) {
|
|
299
|
+
if (closed) return
|
|
300
|
+
chrome = { ...chrome, ...part }
|
|
301
|
+
if (queued) return
|
|
302
|
+
queued = true
|
|
303
|
+
queueMicrotask(() => {
|
|
304
|
+
queued = false
|
|
305
|
+
if (!closed) bridge.send({ desk: DESK_PROTOCOL, kind: 'chrome', chrome })
|
|
306
|
+
})
|
|
307
|
+
},
|
|
308
|
+
onCommand(target, listener) {
|
|
309
|
+
commands[target].add(listener)
|
|
310
|
+
return () => {
|
|
311
|
+
commands[target].delete(listener)
|
|
312
|
+
}
|
|
313
|
+
},
|
|
314
|
+
publish(topic, payload) {
|
|
315
|
+
if (!closed) bridge.send({ desk: DESK_PROTOCOL, kind: 'publish', topic, payload })
|
|
316
|
+
},
|
|
317
|
+
subscribe(publisher, topic, handler) {
|
|
318
|
+
const key = keyOf(publisher, topic)
|
|
319
|
+
const existing = subscriptions.get(key)
|
|
320
|
+
const own = handler as Handler
|
|
321
|
+
if (existing) {
|
|
322
|
+
// The host replayed to the first handler only: this one gets what was heard since, here.
|
|
323
|
+
existing.handlers.add(own)
|
|
324
|
+
for (const broadcast of existing.last.values()) (handler as (b: Broadcast) => void)(broadcast)
|
|
325
|
+
} else if (!closed) {
|
|
326
|
+
// Added before the subscribe goes out, so a host that replays at once reaches it.
|
|
327
|
+
subscriptions.set(key, { publisher, topic, handlers: new Set([own]), last: new Map() })
|
|
328
|
+
bridge.send({ desk: DESK_PROTOCOL, kind: 'subscribe', publisher, topic })
|
|
329
|
+
}
|
|
330
|
+
const handlers = subscriptions.get(key)?.handlers
|
|
331
|
+
return () => {
|
|
332
|
+
if (!handlers) return
|
|
333
|
+
handlers.delete(own)
|
|
334
|
+
if (handlers.size === 0 && subscriptions.get(key)?.handlers === handlers) {
|
|
335
|
+
subscriptions.delete(key)
|
|
336
|
+
if (!closed) bridge.send({ desk: DESK_PROTOCOL, kind: 'unsubscribe', publisher, topic })
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
},
|
|
340
|
+
close() {
|
|
341
|
+
if (closed) return
|
|
342
|
+
bridge.send({ desk: DESK_PROTOCOL, kind: 'chrome', chrome: EMPTY_CHROME })
|
|
343
|
+
// So the host stops sending what nobody here hears.
|
|
344
|
+
for (const { publisher, topic } of subscriptions.values()) bridge.send({ desk: DESK_PROTOCOL, kind: 'unsubscribe', publisher, topic })
|
|
345
|
+
closed = true
|
|
346
|
+
stop()
|
|
347
|
+
subscriptions.clear()
|
|
348
|
+
for (const listeners of Object.values(commands)) listeners.clear()
|
|
349
|
+
},
|
|
350
|
+
}
|
|
351
|
+
}
|
package/src/core/index.ts
CHANGED
|
@@ -35,8 +35,35 @@ export type { Bus, DeskEvent, EventHandler, InputDescription, SubscribeOptions }
|
|
|
35
35
|
export { bindShortcuts, formatShortcut, isApplePlatform, isInstalledApp, matchesShortcut, parseShortcut } from './shortcuts.js'
|
|
36
36
|
export type { Keymap, Shortcut, ShortcutOptions } from './shortcuts.js'
|
|
37
37
|
export { deviceLook, lookFor, resolveLook } from './look.js'
|
|
38
|
-
export { COLOR_SCHEME_KEY, COLOR_SCHEME_SCRIPT, LOOK_KEY } from './scheme.js'
|
|
38
|
+
export { COLOR_SCHEME_KEY, COLOR_SCHEME_SCRIPT, colorSchemeScript, LOOK_KEY } from './scheme.js'
|
|
39
|
+
export { getPreference, localPreferences, setPreference, setPreferenceStore, subscribePreferences } from './preferences.js'
|
|
40
|
+
export type { PreferenceStore } from './preferences.js'
|
|
39
41
|
export type { ColorScheme } from './scheme.js'
|
|
42
|
+
export { applyTheme, applyThemeBasics } from './theme.js'
|
|
43
|
+
export { drawnTheme, THEME_CHANGE, watchThemeChanges } from './themeChange.js'
|
|
44
|
+
export type { ThemeChange } from './themeChange.js'
|
|
45
|
+
export type { AppTheme, DeskTheme, ThemeFont } from './theme.js'
|
|
46
|
+
export { connectHost, DESK_PROTOCOL, findDeskHost, isAppToHost, isDeskHostBridge, isHostToApp, THEME_TOPIC } from './hosted.js'
|
|
47
|
+
export type {
|
|
48
|
+
AppToHost,
|
|
49
|
+
Broadcast,
|
|
50
|
+
BroadcastSender,
|
|
51
|
+
CommandTarget,
|
|
52
|
+
DeskChrome,
|
|
53
|
+
DeskHostBridge,
|
|
54
|
+
HostAbout,
|
|
55
|
+
HostAboutFact,
|
|
56
|
+
HostAboutSection,
|
|
57
|
+
HostClient,
|
|
58
|
+
HostDockEntry,
|
|
59
|
+
HostDockItem,
|
|
60
|
+
HostDockStack,
|
|
61
|
+
HostMenu,
|
|
62
|
+
HostMenuItem,
|
|
63
|
+
HostSettingsSection,
|
|
64
|
+
HostToApp,
|
|
65
|
+
Publisher,
|
|
66
|
+
} from './hosted.js'
|
|
40
67
|
export type { Look, LookChoice } from './look.js'
|
|
41
68
|
export { parse, serialize, syncWithLocation } from './location.js'
|
|
42
69
|
export { isTitleCase, titleCase } from './titles.js'
|