@liberation-data/desk 0.43.0 → 0.44.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 +225 -8
- 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/css/apps.css +35 -1
- package/dist/css/shell.css +100 -0
- package/dist/css/tokens.css +10 -0
- package/dist/desk.css +45 -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/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 +50 -9
- package/dist/react/app.d.ts.map +1 -1
- package/dist/react/app.js +59 -13
- 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 +32 -23
- 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 +15 -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 +8 -3
- package/dist/react/index.d.ts.map +1 -1
- package/dist/react/index.js +5 -2
- package/dist/react/index.js.map +1 -1
- 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/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 +48 -14
- package/package.json +10 -1
- 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/css/apps.css +35 -1
- package/src/css/shell.css +99 -0
- package/src/css/tokens.css +10 -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/Dock.tsx +34 -28
- package/src/react/MenuBar.tsx +22 -4
- package/src/react/app.tsx +84 -14
- package/src/react/appFrame.tsx +39 -14
- package/src/react/appPanels.tsx +38 -25
- package/src/react/appearance.tsx +36 -45
- package/src/react/context.tsx +88 -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 +9 -2
- package/src/react/shell.tsx +30 -3
- 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
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { isImageRef } from '../core/theme.js'
|
|
2
|
+
import type { DeskTheme, ThemeFont } from '../core/theme.js'
|
|
3
|
+
import type { Look } from '../core/look.js'
|
|
4
|
+
|
|
5
|
+
/*
|
|
6
|
+
* CHECKING A THEME BEFORE IT RESTYLES SOMEBODY ELSE'S PAGE.
|
|
7
|
+
*
|
|
8
|
+
* A host passes the primary's theme to every other app, and each sets its tokens on its own root. A
|
|
9
|
+
* value that could load something — `url(…)`, `image-set(…)`, a font from elsewhere — would let one
|
|
10
|
+
* app make the others fetch from wherever it liked, or lay an image over part of them. So the host
|
|
11
|
+
* checks the theme first, and passes on only a copy built from what passed.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** A theme fit to pass on, as a fresh copy, or why it is not. */
|
|
15
|
+
export type ThemeCheck = { readonly ok: true; readonly theme: DeskTheme } | { readonly ok: false; readonly reason: string }
|
|
16
|
+
|
|
17
|
+
const LOOKS: readonly string[] = ['mac', 'gnome', 'windows']
|
|
18
|
+
|
|
19
|
+
/** The CSS functions a token may use: colours, maths, gradients and other custom properties. Nothing that loads. */
|
|
20
|
+
const FUNCTIONS = new Set([
|
|
21
|
+
'rgb', 'rgba', 'hsl', 'hsla', 'hwb', 'lab', 'lch', 'oklab', 'oklch', 'color', 'color-mix', 'light-dark',
|
|
22
|
+
'var', 'calc', 'min', 'max', 'clamp',
|
|
23
|
+
'linear-gradient', 'radial-gradient', 'conic-gradient', 'repeating-linear-gradient', 'repeating-radial-gradient',
|
|
24
|
+
])
|
|
25
|
+
|
|
26
|
+
const MAX_VALUE = 300
|
|
27
|
+
const MAX_TOKENS = 400
|
|
28
|
+
const MAX_FONTS = 16
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Whether a custom property's value is plain: a colour, a length, a number, a font list or a gradient.
|
|
32
|
+
* Only letters, digits, spaces and `#.,%/'"+-()*` may appear, every function is one of `FUNCTIONS`, and
|
|
33
|
+
* nothing may escape, end the declaration or open a block. So `url(…)`, `image-set(…)`, `\75rl(` and
|
|
34
|
+
* `red; background: …` are all refused.
|
|
35
|
+
*/
|
|
36
|
+
export function isPlainValue(value: string): boolean {
|
|
37
|
+
if (value.length > MAX_VALUE) return false
|
|
38
|
+
if (!/^[\w\s#.,%/'"+*()-]*$/.test(value)) return false
|
|
39
|
+
for (const match of value.matchAll(/([\w-]*)\s*\(/g)) {
|
|
40
|
+
if (!FUNCTIONS.has((match[1] ?? '').toLowerCase())) return false
|
|
41
|
+
}
|
|
42
|
+
return true
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Whether `url` is on `origin`: the same scheme and host. Compared by hand rather than by `URL.origin`,
|
|
47
|
+
* which is `'null'` for every scheme a browser does not know (an Electron app's `app://` among them,
|
|
48
|
+
* and `javascript:` too), so it would take any of them for any other. A URL with no host is on none.
|
|
49
|
+
*/
|
|
50
|
+
export function onOrigin(url: URL, origin: string): boolean {
|
|
51
|
+
const own = new URL(origin)
|
|
52
|
+
return url.host !== '' && url.protocol === own.protocol && url.host === own.host
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* `src` as a URL safe to put in CSS, when it is a `data:` URI of one of `kinds` or a URL on `origin`;
|
|
57
|
+
* null otherwise. A URL comes back resolved and percent-encoded, so it holds no quote.
|
|
58
|
+
*/
|
|
59
|
+
export function fromOriginOrData(src: string, origin: string, kinds: RegExp): string | null {
|
|
60
|
+
if (src.startsWith('data:')) return kinds.test(src.slice(5)) && !/["\\\s]/.test(src) ? src : null
|
|
61
|
+
try {
|
|
62
|
+
const url = new URL(src, origin)
|
|
63
|
+
return onOrigin(url, origin) ? url.href : null
|
|
64
|
+
} catch {
|
|
65
|
+
return null
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const FONT_DATA = /^(font\/|application\/(font|x-font|octet-stream))/i
|
|
70
|
+
export const IMAGE_DATA = /^image\/(png|jpeg|webp|avif|gif)[;,]/i
|
|
71
|
+
/** A font family as CSS names one: words, spaces and hyphens, nothing to quote or escape. */
|
|
72
|
+
const FAMILY = /^[\w -]{1,80}$/
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Checks a theme published by `origin` before it is passed to other pages, and returns a copy with
|
|
76
|
+
* only the fields a `DeskTheme` has. Tokens must be custom properties with plain values
|
|
77
|
+
* (`isPlainValue`); fonts and an image wallpaper must come from `origin` or be `data:` URIs.
|
|
78
|
+
*/
|
|
79
|
+
export function checkTheme(value: unknown, origin: string): ThemeCheck {
|
|
80
|
+
const fail = (reason: string): ThemeCheck => ({ ok: false, reason })
|
|
81
|
+
if (typeof value !== 'object' || value === null) return fail('not an object')
|
|
82
|
+
const theme = value as Record<string, unknown>
|
|
83
|
+
if (theme.mode !== 'light' && theme.mode !== 'dark') return fail('mode must be light or dark')
|
|
84
|
+
if (typeof theme.look !== 'string' || !LOOKS.includes(theme.look)) return fail('look must be mac, gnome or windows')
|
|
85
|
+
if (typeof theme.tokens !== 'object' || theme.tokens === null) return fail('tokens must be an object')
|
|
86
|
+
const entries = Object.entries(theme.tokens as Record<string, unknown>)
|
|
87
|
+
if (entries.length > MAX_TOKENS) return fail('too many tokens')
|
|
88
|
+
const tokens: Record<string, string> = {}
|
|
89
|
+
for (const [name, token] of entries) {
|
|
90
|
+
if (!/^--[\w-]+$/.test(name)) return fail(`${name} is not a custom property`)
|
|
91
|
+
if (typeof token !== 'string' || !isPlainValue(token)) return fail(`${name} is not a plain value`)
|
|
92
|
+
tokens[name] = token
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
let fonts: ThemeFont[] | undefined
|
|
96
|
+
if (theme.fonts !== undefined) {
|
|
97
|
+
if (!Array.isArray(theme.fonts) || theme.fonts.length > MAX_FONTS) return fail('fonts must be a short list')
|
|
98
|
+
fonts = []
|
|
99
|
+
for (const font of theme.fonts as unknown[]) {
|
|
100
|
+
const f = font as Record<string, unknown> | null
|
|
101
|
+
if (typeof f?.family !== 'string' || !FAMILY.test(f.family)) return fail('a font family is not plain')
|
|
102
|
+
const src = typeof f.src === 'string' ? fromOriginOrData(f.src, origin, FONT_DATA) : null
|
|
103
|
+
if (src === null) return fail(`font ${f.family} is not from the publisher`)
|
|
104
|
+
if (f.weight !== undefined && (typeof f.weight !== 'string' || !/^\d{3}( \d{3})?$|^(normal|bold)$/.test(f.weight))) return fail(`font ${f.family} has a bad weight`)
|
|
105
|
+
if (f.style !== undefined && f.style !== 'normal' && f.style !== 'italic') return fail(`font ${f.family} has a bad style`)
|
|
106
|
+
fonts.push({
|
|
107
|
+
family: f.family,
|
|
108
|
+
src,
|
|
109
|
+
...(f.weight === undefined ? {} : { weight: f.weight as string }),
|
|
110
|
+
...(f.style === undefined ? {} : { style: f.style as 'normal' | 'italic' }),
|
|
111
|
+
})
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
let wallpaper: string | undefined
|
|
116
|
+
if (theme.wallpaper !== undefined) {
|
|
117
|
+
if (typeof theme.wallpaper !== 'string') return fail('wallpaper must be a string')
|
|
118
|
+
const image = isImageRef(theme.wallpaper) ? fromOriginOrData(theme.wallpaper, origin, IMAGE_DATA) : isPlainValue(theme.wallpaper) ? theme.wallpaper : null
|
|
119
|
+
if (image === null) return fail('wallpaper is not from the publisher')
|
|
120
|
+
wallpaper = image
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return {
|
|
124
|
+
ok: true,
|
|
125
|
+
theme: {
|
|
126
|
+
mode: theme.mode,
|
|
127
|
+
look: theme.look as Look,
|
|
128
|
+
tokens,
|
|
129
|
+
...(fonts === undefined ? {} : { fonts }),
|
|
130
|
+
...(wallpaper === undefined ? {} : { wallpaper }),
|
|
131
|
+
},
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The host's side of hosted mode: what a native or web shell uses to run several Desk apps under one
|
|
3
|
+
* menu bar and dock. Apps never load it; they use `connectHost`, through `DeskProvider`, from core.
|
|
4
|
+
*/
|
|
5
|
+
export { checkChrome } from './checkChrome.js'
|
|
6
|
+
export type { ChromeCheck } from './checkChrome.js'
|
|
7
|
+
export { checkTheme, isPlainValue } from './checkTheme.js'
|
|
8
|
+
export type { ThemeCheck } from './checkTheme.js'
|
|
9
|
+
export { createBroadcastRouter, createMemoryHost } from './router.js'
|
|
10
|
+
export type { BroadcastRouter, BroadcastRouterOptions, HostedApp, MemoryHost, Rate, RateLimits, Refusal } from './router.js'
|
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
import { topicMatches } from '../core/events.js'
|
|
2
|
+
import { DESK_PROTOCOL, isAppToHost, THEME_TOPIC } from '../core/hosted.js'
|
|
3
|
+
import type { BroadcastSender, CommandTarget, DeskChrome, DeskHostBridge, HostToApp, Publisher } from '../core/hosted.js'
|
|
4
|
+
import { checkChrome } from './checkChrome.js'
|
|
5
|
+
import { checkTheme } from './checkTheme.js'
|
|
6
|
+
|
|
7
|
+
/*
|
|
8
|
+
* THE HOST'S HALF: WHO MAY SAY WHAT, AND WHO HEARS IT.
|
|
9
|
+
*
|
|
10
|
+
* A host passes every broadcast between its apps, so it is where the rules hold. It stamps each
|
|
11
|
+
* broadcast with the app it really came from; it passes on only topics that app was granted, and
|
|
12
|
+
* the theme only from the primary, checked; and it keeps the last broadcast of each publisher on each
|
|
13
|
+
* topic, so an app that starts late, or one listening while the publisher is down, hears the latest
|
|
14
|
+
* at once. A host builds its transport around this and hands it every message an app sends.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/** An app as its host registers it. */
|
|
18
|
+
export interface HostedApp {
|
|
19
|
+
/** Stable across runs. */
|
|
20
|
+
readonly id: string
|
|
21
|
+
/** Where the app is served: its theme's fonts and images, and its dock's images, must come from here. */
|
|
22
|
+
readonly origin: string
|
|
23
|
+
/** The app whose theme every app wears. At most one. */
|
|
24
|
+
readonly primary?: boolean
|
|
25
|
+
/** Topics it may publish (`ride.*` grants a branch). Granted by the host, never by the app. The primary needs `theme` among them. */
|
|
26
|
+
readonly publishes?: readonly string[]
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** What a router needs from its host: a way to reach each app, and the host's own rules. */
|
|
30
|
+
export interface BroadcastRouterOptions {
|
|
31
|
+
/** Sends a message to an app, through the host's transport. One that throws keeps no other app from hearing: the first error is thrown once all have. */
|
|
32
|
+
readonly deliver: (appId: string, message: HostToApp) => void
|
|
33
|
+
/**
|
|
34
|
+
* An app's menus and dock changed: the host draws them. Checked first (`checkChrome`): a part that
|
|
35
|
+
* fails is dropped and named in `dropped`, for the host to log; chrome of the wrong shape is not
|
|
36
|
+
* passed on, and the app keeps what it last sent. Null when the app is unregistered: the host stops
|
|
37
|
+
* drawing its chrome.
|
|
38
|
+
*/
|
|
39
|
+
readonly chrome?: (appId: string, chrome: DeskChrome | null, dropped: readonly string[]) => void
|
|
40
|
+
/**
|
|
41
|
+
* Checks a broadcast's payload on topics the host knows: return the payload to pass on, or
|
|
42
|
+
* `undefined` to drop it. The theme is always checked as well, whatever this does.
|
|
43
|
+
*/
|
|
44
|
+
readonly check?: (topic: string, payload: unknown, from: HostedApp) => unknown
|
|
45
|
+
/** The most a payload may take as JSON. Default 64 KiB; larger ones are dropped. */
|
|
46
|
+
readonly maxPayload?: number
|
|
47
|
+
/** How fast each publisher may broadcast on each topic: see `RateLimits`. */
|
|
48
|
+
readonly limits?: RateLimits
|
|
49
|
+
/** The time, for tests: `now` in milliseconds, and `later` to run something after a delay, returning a canceller. */
|
|
50
|
+
readonly clock?: { readonly now: () => number; readonly later: (run: () => void, ms: number) => () => void }
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** A token bucket: `burst` broadcasts at once, then `perSecond`. */
|
|
54
|
+
export interface Rate {
|
|
55
|
+
readonly burst: number
|
|
56
|
+
readonly perSecond: number
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Per publisher and topic, a bucket of broadcasts. Over it, nothing is queued and nothing lost: the
|
|
61
|
+
* latest payload is held, replacing any held before, and goes out when the bucket allows, so every
|
|
62
|
+
* subscriber still ends on the last value. Defaults: 20 at once then 10 a second; the theme 2 then 1
|
|
63
|
+
* a second, since it changes on a person's choice; and 64 topics per publisher at once, past which a
|
|
64
|
+
* broadcast on yet another topic is refused (`'rate limited'`) unless one of them is idle and full.
|
|
65
|
+
*/
|
|
66
|
+
export interface RateLimits extends Partial<Rate> {
|
|
67
|
+
/** Rates for particular topics, by exact name. The theme keeps its own unless named here. */
|
|
68
|
+
readonly topics?: Readonly<Record<string, Partial<Rate>>>
|
|
69
|
+
readonly maxTopics?: number
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Why a message was not acted on, or null when it was. */
|
|
73
|
+
export type Refusal = 'unknown app' | 'malformed' | 'not granted' | 'not the primary' | 'refused by check' | 'too large' | 'rate limited' | null
|
|
74
|
+
|
|
75
|
+
/** The rules of a host's broadcasts, kept in one place so every transport keeps them alike. */
|
|
76
|
+
export interface BroadcastRouter {
|
|
77
|
+
/**
|
|
78
|
+
* Adds an app, or updates one. Throws for a second primary: two looks cannot both be worn. An update
|
|
79
|
+
* drops what the app had held over its rate, unsent: it was vetted under the grants and origin it replaces.
|
|
80
|
+
*/
|
|
81
|
+
register(app: HostedApp): void
|
|
82
|
+
/** Forgets an app: its chrome (`chrome` is called with null), its subscriptions, and what it published, except its last theme. */
|
|
83
|
+
unregister(appId: string): void
|
|
84
|
+
/** Acts on a message an app sent. The app is identified by the transport, never by the message. */
|
|
85
|
+
receive(appId: string, message: unknown): Refusal
|
|
86
|
+
/** The primary's id, or null while none is registered. */
|
|
87
|
+
primary(): string | null
|
|
88
|
+
/** The last broadcast `publisher` made on `topic` that went out, if it is kept. */
|
|
89
|
+
kept(publisher: string, topic: string): unknown
|
|
90
|
+
/** How many of an app's broadcasts were held and replaced by a newer one, or refused for its rate. */
|
|
91
|
+
limited(appId: string): number
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const DEFAULT_MAX_PAYLOAD = 64 * 1024
|
|
95
|
+
const RATE: Rate = { burst: 20, perSecond: 10 }
|
|
96
|
+
const THEME_RATE: Rate = { burst: 2, perSecond: 1 }
|
|
97
|
+
|
|
98
|
+
interface Bucket {
|
|
99
|
+
tokens: number
|
|
100
|
+
at: number
|
|
101
|
+
readonly rate: Rate
|
|
102
|
+
/** The latest payload over the limit, and the cancel of the timer that sends it. */
|
|
103
|
+
held?: { from: BroadcastSender; payload: unknown; readonly cancel: () => void }
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** A router for a host to put behind its transport: see `BroadcastRouter`. */
|
|
107
|
+
export function createBroadcastRouter(options: BroadcastRouterOptions): BroadcastRouter {
|
|
108
|
+
const apps = new Map<string, HostedApp>()
|
|
109
|
+
const subscriptions = new Map<string, Map<string, { publisher: Publisher; topic: string }>>()
|
|
110
|
+
/** Per publisher, its last broadcast on each topic. */
|
|
111
|
+
const kept = new Map<string, Map<string, { from: BroadcastSender; payload: unknown }>>()
|
|
112
|
+
const maxPayload = options.maxPayload ?? DEFAULT_MAX_PAYLOAD
|
|
113
|
+
/** Fan-outs waiting their turn; the first is the one being delivered. */
|
|
114
|
+
const pending: (() => void)[] = []
|
|
115
|
+
/** What deliveries threw while they went out. */
|
|
116
|
+
const failures: unknown[] = []
|
|
117
|
+
/** The primary last registered, kept while it is down so its theme still reaches apps that start meanwhile. */
|
|
118
|
+
let lastPrimary: string | null = null
|
|
119
|
+
const limits = options.limits ?? {}
|
|
120
|
+
// setTimeout runs a delay past 2^31 - 1 ms at once, which would let a very slow rate send every held value.
|
|
121
|
+
const clock = options.clock ?? { now: Date.now, later: (run: () => void, ms: number) => clearTimeout.bind(null, setTimeout(run, Math.min(ms, 2 ** 31 - 1))) }
|
|
122
|
+
// A rate that cannot refill (0, negative, NaN) would hold a value for ever or, through setTimeout, not at all.
|
|
123
|
+
for (const rate of [limits, ...Object.values(limits.topics ?? {})]) {
|
|
124
|
+
if (!((rate.burst ?? 0) >= 0 && (rate.perSecond ?? 1) > 0 && (limits.maxTopics ?? 0) >= 0)) throw new RangeError('limits: burst and maxTopics >= 0, perSecond > 0')
|
|
125
|
+
}
|
|
126
|
+
/** Per publisher, a bucket per topic; and how many of its broadcasts the limits held or refused. */
|
|
127
|
+
const buckets = new Map<string, Map<string, Bucket>>()
|
|
128
|
+
const overLimit = new Map<string, number>()
|
|
129
|
+
const count = (appId: string) => overLimit.set(appId, (overLimit.get(appId) ?? 0) + 1)
|
|
130
|
+
/** Refills a bucket at its own topic's rate; a clock that goes back counts as no time passing. */
|
|
131
|
+
const refill = (b: Bucket) => {
|
|
132
|
+
const now = clock.now()
|
|
133
|
+
if (now > b.at) b.tokens = Math.min(b.rate.burst, b.tokens + ((now - b.at) * b.rate.perSecond) / 1000)
|
|
134
|
+
b.at = Math.max(now, b.at)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const primaryId = () => [...apps.values()].find(app => app.primary)?.id ?? null
|
|
138
|
+
const senderOf = (app: HostedApp): BroadcastSender => (app.primary ? { id: app.id, role: 'primary' } : { id: app.id })
|
|
139
|
+
const hears = (publisher: Publisher, from: BroadcastSender) => (publisher === 'primary' ? from.role === 'primary' : publisher === from.id)
|
|
140
|
+
|
|
141
|
+
const deliver = (appId: string, from: BroadcastSender, topic: string, payload: unknown) =>
|
|
142
|
+
options.deliver(appId, { desk: DESK_PROTOCOL, kind: 'broadcast', from, topic, payload })
|
|
143
|
+
|
|
144
|
+
const sizeOf = (payload: unknown) => {
|
|
145
|
+
try {
|
|
146
|
+
return JSON.stringify(payload ?? null).length
|
|
147
|
+
} catch {
|
|
148
|
+
return Infinity
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const publish = (app: HostedApp, topic: string, raw: unknown): Refusal => {
|
|
153
|
+
if (!(app.publishes ?? []).some(pattern => topicMatches(pattern, topic))) return 'not granted'
|
|
154
|
+
if (sizeOf(raw) > maxPayload) return 'too large'
|
|
155
|
+
let payload = raw
|
|
156
|
+
if (topic === THEME_TOPIC) {
|
|
157
|
+
if (!app.primary) return 'not the primary'
|
|
158
|
+
const checked = checkTheme(payload, app.origin)
|
|
159
|
+
if (!checked.ok) return 'refused by check'
|
|
160
|
+
payload = checked.theme
|
|
161
|
+
}
|
|
162
|
+
if (options.check) {
|
|
163
|
+
payload = options.check(topic, payload, app)
|
|
164
|
+
if (payload === undefined) return 'refused by check'
|
|
165
|
+
}
|
|
166
|
+
return limit(app, topic, senderOf(app), payload)
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Sends now if the topic's bucket has a token; else holds the payload, replacing one held before,
|
|
171
|
+
* until it has. A held topic's latest can so go out after a later broadcast on another topic: order
|
|
172
|
+
* holds within a topic, and across topics only while under the limits.
|
|
173
|
+
*/
|
|
174
|
+
const limit = (app: HostedApp, topic: string, from: BroadcastSender, payload: unknown): Refusal => {
|
|
175
|
+
const rate = { ...(topic === THEME_TOPIC ? THEME_RATE : { ...RATE, ...limits }), ...limits.topics?.[topic] }
|
|
176
|
+
const own = buckets.get(app.id) ?? new Map<string, Bucket>()
|
|
177
|
+
buckets.set(app.id, own)
|
|
178
|
+
let bucket = own.get(topic)
|
|
179
|
+
if (!bucket) {
|
|
180
|
+
// A new topic starts with a full burst, so a flood across topics is bounded by how many a
|
|
181
|
+
// publisher may have at once. A full, idle bucket says nothing a new one would not: it can go.
|
|
182
|
+
if (own.size >= (limits.maxTopics ?? 64)) {
|
|
183
|
+
const idle = [...own].find(([t, b]) => !b.held && (refill(b), b.tokens >= b.rate.burst) && t)
|
|
184
|
+
if (!idle) return count(app.id), 'rate limited'
|
|
185
|
+
own.delete(idle[0])
|
|
186
|
+
}
|
|
187
|
+
own.set(topic, (bucket = { tokens: rate.burst, at: clock.now(), rate }))
|
|
188
|
+
}
|
|
189
|
+
refill(bucket)
|
|
190
|
+
if (bucket.held) {
|
|
191
|
+
count(app.id)
|
|
192
|
+
bucket.held.from = from
|
|
193
|
+
bucket.held.payload = payload
|
|
194
|
+
return null
|
|
195
|
+
}
|
|
196
|
+
if (bucket.tokens >= 1) {
|
|
197
|
+
bucket.tokens--
|
|
198
|
+
return send(app.id, topic, from, payload)
|
|
199
|
+
}
|
|
200
|
+
const b = bucket
|
|
201
|
+
const cancel = clock.later(() => {
|
|
202
|
+
const held = b.held
|
|
203
|
+
if (!held) return
|
|
204
|
+
delete b.held
|
|
205
|
+
refill(b)
|
|
206
|
+
b.tokens = Math.max(0, b.tokens - 1)
|
|
207
|
+
try {
|
|
208
|
+
send(app.id, topic, held.from, held.payload)
|
|
209
|
+
} catch (error) {
|
|
210
|
+
// No caller to throw to here: reported as any uncaught error is, after the others have heard.
|
|
211
|
+
queueMicrotask(() => {
|
|
212
|
+
throw error
|
|
213
|
+
})
|
|
214
|
+
}
|
|
215
|
+
}, Math.ceil(((1 - b.tokens) * 1000) / rate.perSecond))
|
|
216
|
+
b.held = { from, payload, cancel }
|
|
217
|
+
return null
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** Keeps `payload` as the publisher's last on `topic` and passes it to every app that subscribed. */
|
|
221
|
+
const send = (appId: string, topic: string, from: BroadcastSender, payload: unknown): Refusal => {
|
|
222
|
+
const own = kept.get(appId) ?? new Map()
|
|
223
|
+
own.set(topic, { from, payload })
|
|
224
|
+
kept.set(appId, own)
|
|
225
|
+
// One broadcast reaches everyone before the next goes out, even one published by a handler while
|
|
226
|
+
// this one is delivered in-process; otherwise an app could hear the newer first and end on the older.
|
|
227
|
+
// A transport that throws for one app (a window already gone) does not keep the rest from hearing:
|
|
228
|
+
// the first failure is thrown once every broadcast has gone out.
|
|
229
|
+
pending.push(() => {
|
|
230
|
+
for (const [appId, subs] of subscriptions) {
|
|
231
|
+
if ([...subs.values()].some(s => hears(s.publisher, from) && topicMatches(s.topic, topic))) {
|
|
232
|
+
try {
|
|
233
|
+
deliver(appId, from, topic, payload)
|
|
234
|
+
} catch (error) {
|
|
235
|
+
failures.push(error)
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
})
|
|
240
|
+
if (pending.length > 1) return null
|
|
241
|
+
failures.length = 0
|
|
242
|
+
for (let next = pending[0]; next; next = pending[0]) {
|
|
243
|
+
next()
|
|
244
|
+
pending.shift()
|
|
245
|
+
}
|
|
246
|
+
if (failures.length > 0) throw failures[0]
|
|
247
|
+
return null
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** Replays to a new subscription what it would have heard: the last broadcast on each topic it covers. */
|
|
251
|
+
const replay = (appId: string, publisher: Publisher, pattern: string) => {
|
|
252
|
+
const source = publisher === 'primary' ? (primaryId() ?? lastPrimary) : publisher
|
|
253
|
+
if (source === null) return
|
|
254
|
+
for (const [topic, { from, payload }] of kept.get(source) ?? []) {
|
|
255
|
+
if (topicMatches(pattern, topic)) deliver(appId, from, topic, payload)
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/** Cancels what an app held over its rate. */
|
|
260
|
+
const dropHeld = (appId: string) => {
|
|
261
|
+
for (const bucket of buckets.get(appId)?.values() ?? []) bucket.held?.cancel(), delete bucket.held
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
return {
|
|
265
|
+
register(app) {
|
|
266
|
+
if (app.primary && primaryId() !== null && primaryId() !== app.id) throw new Error(`${primaryId()} is already the primary`)
|
|
267
|
+
dropHeld(app.id)
|
|
268
|
+
apps.set(app.id, app)
|
|
269
|
+
if (app.primary) lastPrimary = app.id
|
|
270
|
+
else if (lastPrimary === app.id) lastPrimary = null
|
|
271
|
+
// A primary registered again after it went: what it kept still carries its role.
|
|
272
|
+
const own = kept.get(app.id)
|
|
273
|
+
if (own) for (const [topic, entry] of own) own.set(topic, { ...entry, from: senderOf(app) })
|
|
274
|
+
},
|
|
275
|
+
|
|
276
|
+
unregister(appId) {
|
|
277
|
+
if (apps.delete(appId)) options.chrome?.(appId, null, [])
|
|
278
|
+
// What it had held goes with it, unsent.
|
|
279
|
+
dropHeld(appId)
|
|
280
|
+
buckets.delete(appId)
|
|
281
|
+
subscriptions.delete(appId)
|
|
282
|
+
const own = kept.get(appId)
|
|
283
|
+
if (!own) return
|
|
284
|
+
for (const topic of [...own.keys()]) if (topic !== THEME_TOPIC) own.delete(topic)
|
|
285
|
+
if (own.size === 0) kept.delete(appId)
|
|
286
|
+
},
|
|
287
|
+
|
|
288
|
+
receive(appId, message) {
|
|
289
|
+
const app = apps.get(appId)
|
|
290
|
+
if (!app) return 'unknown app'
|
|
291
|
+
if (!isAppToHost(message)) return 'malformed'
|
|
292
|
+
switch (message.kind) {
|
|
293
|
+
case 'chrome': {
|
|
294
|
+
// Drawn in the host's own frame, so checked as a theme is, and only the checked copy drawn.
|
|
295
|
+
const checked = checkChrome(message.chrome, app.origin)
|
|
296
|
+
if (!checked.ok) return 'refused by check'
|
|
297
|
+
options.chrome?.(appId, checked.chrome, checked.dropped)
|
|
298
|
+
return null
|
|
299
|
+
}
|
|
300
|
+
case 'publish':
|
|
301
|
+
return publish(app, message.topic, message.payload)
|
|
302
|
+
case 'subscribe': {
|
|
303
|
+
const subs = subscriptions.get(appId) ?? new Map()
|
|
304
|
+
subs.set(`${message.publisher}\u0000${message.topic}`, { publisher: message.publisher, topic: message.topic })
|
|
305
|
+
subscriptions.set(appId, subs)
|
|
306
|
+
replay(appId, message.publisher, message.topic)
|
|
307
|
+
return null
|
|
308
|
+
}
|
|
309
|
+
case 'unsubscribe':
|
|
310
|
+
subscriptions.get(appId)?.delete(`${message.publisher}\u0000${message.topic}`)
|
|
311
|
+
return null
|
|
312
|
+
}
|
|
313
|
+
},
|
|
314
|
+
|
|
315
|
+
primary: primaryId,
|
|
316
|
+
|
|
317
|
+
kept(publisher, topic) {
|
|
318
|
+
return kept.get(publisher)?.get(topic)?.payload
|
|
319
|
+
},
|
|
320
|
+
|
|
321
|
+
limited: appId => overLimit.get(appId) ?? 0,
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/** A host that lives in one page: for tests, examples, and apps that host others in-process. */
|
|
326
|
+
export interface MemoryHost {
|
|
327
|
+
readonly router: BroadcastRouter
|
|
328
|
+
/** Registers an app and gives it a bridge to this host. */
|
|
329
|
+
connect(app: HostedApp): DeskHostBridge
|
|
330
|
+
/** The chrome each app last sent. */
|
|
331
|
+
chrome(appId: string): DeskChrome | undefined
|
|
332
|
+
/** Sends an app a choice from its menus, its dock or its Settings, as a host's frame would. */
|
|
333
|
+
command(appId: string, target: CommandTarget, id: string): void
|
|
334
|
+
/** Every message an app sent and how it went, newest last. */
|
|
335
|
+
readonly log: readonly { readonly appId: string; readonly message: unknown; readonly refusal: Refusal }[]
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* A host in memory. Messages are copied with `structuredClone` each way, as a real boundary would,
|
|
340
|
+
* so an app that sends something that cannot cross fails here too.
|
|
341
|
+
*/
|
|
342
|
+
export function createMemoryHost(options: Pick<BroadcastRouterOptions, 'check' | 'maxPayload'> = {}): MemoryHost {
|
|
343
|
+
const listeners = new Map<string, Set<(message: HostToApp) => void>>()
|
|
344
|
+
const chromes = new Map<string, DeskChrome>()
|
|
345
|
+
const log: { appId: string; message: unknown; refusal: Refusal }[] = []
|
|
346
|
+
const send = (appId: string, message: HostToApp) => {
|
|
347
|
+
for (const listener of [...(listeners.get(appId) ?? [])]) listener(structuredClone(message))
|
|
348
|
+
}
|
|
349
|
+
const router = createBroadcastRouter({ ...options, deliver: send, chrome: (appId, chrome) => (chrome ? chromes.set(appId, chrome) : chromes.delete(appId)) })
|
|
350
|
+
return {
|
|
351
|
+
router,
|
|
352
|
+
connect(app) {
|
|
353
|
+
router.register(app)
|
|
354
|
+
return {
|
|
355
|
+
version: DESK_PROTOCOL,
|
|
356
|
+
send(message) {
|
|
357
|
+
const copy: unknown = structuredClone(message)
|
|
358
|
+
log.push({ appId: app.id, message: copy, refusal: router.receive(app.id, copy) })
|
|
359
|
+
},
|
|
360
|
+
onMessage(listener) {
|
|
361
|
+
const set = listeners.get(app.id) ?? new Set()
|
|
362
|
+
set.add(listener)
|
|
363
|
+
listeners.set(app.id, set)
|
|
364
|
+
return () => {
|
|
365
|
+
set.delete(listener)
|
|
366
|
+
}
|
|
367
|
+
},
|
|
368
|
+
}
|
|
369
|
+
},
|
|
370
|
+
chrome: appId => chromes.get(appId),
|
|
371
|
+
command: (appId, target, id) => send(appId, { desk: DESK_PROTOCOL, kind: 'command', target, id }),
|
|
372
|
+
log,
|
|
373
|
+
}
|
|
374
|
+
}
|
package/src/react/Dock.tsx
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { useEffect, useId, useLayoutEffect, useRef, useState } from 'react'
|
|
2
2
|
import type { KeyboardEvent, ReactNode } from 'react'
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
5
|
-
import {
|
|
3
|
+
import type { WindowId } from '../core/types.js'
|
|
4
|
+
import { ABOUT_WINDOW, openAppSettings, openPanel, placeAppDockItems, useDeskApp } from './app.js'
|
|
5
|
+
import { statusOf, windowOf } from './dockStatus.js'
|
|
6
|
+
import type { DockStatus } from './dockStatus.js'
|
|
7
|
+
import { useHosted } from './hosted.js'
|
|
8
|
+
import { HostedDock, selectDockItem } from './hostDock.js'
|
|
6
9
|
import { useDesk, useDeskState } from './context.js'
|
|
7
10
|
import { useContextMenu } from './contextMenu.js'
|
|
8
11
|
import { useDragSource, useDropTarget } from './dnd.js'
|
|
@@ -14,6 +17,12 @@ export interface DockItem {
|
|
|
14
17
|
readonly id: string
|
|
15
18
|
readonly label: string
|
|
16
19
|
readonly icon: ReactNode
|
|
20
|
+
/**
|
|
21
|
+
* The icon as an image URL or `data:` URI, for a host that draws the dock in hosted mode: `icon`
|
|
22
|
+
* is a React node and cannot cross to it. Without one a host draws the label. It must be on the
|
|
23
|
+
* app's origin, or a `data:` PNG, JPEG, WebP or GIF: a host drops an SVG or another origin's image and draws the label.
|
|
24
|
+
*/
|
|
25
|
+
readonly image?: string
|
|
17
26
|
/** The window this item opens and reports as running. Defaults to the item's id. */
|
|
18
27
|
readonly window?: WindowId
|
|
19
28
|
/** One line under the label inside a stack. */
|
|
@@ -26,6 +35,11 @@ export interface DockItem {
|
|
|
26
35
|
readonly contextMenu?: () => readonly MenuItem[]
|
|
27
36
|
/** Can be dragged along the dock to a new place, when the dock has `pins.onMove`. */
|
|
28
37
|
readonly movable?: boolean
|
|
38
|
+
/**
|
|
39
|
+
* Whether its windows are open, said by someone else rather than read from this desk: a host's
|
|
40
|
+
* dock, drawing an item for an app it runs, shows what the app reported.
|
|
41
|
+
*/
|
|
42
|
+
readonly status?: DockStatus
|
|
29
43
|
}
|
|
30
44
|
|
|
31
45
|
/** The drag type of an item carried along the dock; its payload is the item's id. */
|
|
@@ -50,6 +64,8 @@ export interface DockStack {
|
|
|
50
64
|
readonly items: readonly DockItem[]
|
|
51
65
|
/** Defaults to the first four item icons, the way a folder previews its contents. */
|
|
52
66
|
readonly icon?: ReactNode
|
|
67
|
+
/** The icon as an image, for a host that draws the dock. See `DockItem.image`. */
|
|
68
|
+
readonly image?: string
|
|
53
69
|
/** What a right-click (or the Menu key) offers for the stack itself: Remove from Dock. */
|
|
54
70
|
readonly contextMenu?: () => readonly MenuItem[]
|
|
55
71
|
/** A kept stack: it takes drops and can be carried along the dock, as a `movable` item can. */
|
|
@@ -60,8 +76,8 @@ export type DockEntry =
|
|
|
60
76
|
| ({ readonly type: 'item' } & DockItem)
|
|
61
77
|
| ({ readonly type: 'stack' } & DockStack)
|
|
62
78
|
| { readonly type: 'separator'; readonly id: string }
|
|
63
|
-
/** Where the app wants its Settings item: see `dockSettings()`. */
|
|
64
|
-
| { readonly type: 'settings'; readonly id: string }
|
|
79
|
+
/** Where the app wants its Settings or About item: see `dockSettings()` and `dockAbout()`. */
|
|
80
|
+
| { readonly type: 'settings' | 'about'; readonly id: string }
|
|
65
81
|
|
|
66
82
|
export const dockItem = (item: DockItem): DockEntry => ({ type: 'item', ...item })
|
|
67
83
|
export const dockStack = (stack: DockStack): DockEntry => ({ type: 'stack', ...stack })
|
|
@@ -87,22 +103,9 @@ export interface DockProps {
|
|
|
87
103
|
readonly pins?: DockPins
|
|
88
104
|
}
|
|
89
105
|
|
|
90
|
-
const windowOf = (item: DockItem) => item.window ?? item.id
|
|
91
|
-
|
|
92
|
-
const statusOf = (state: DeskState, items: readonly DockItem[]) => {
|
|
93
|
-
const key = focusedId(state)
|
|
94
|
-
const windows = items.flatMap(i => instancesOf(state, windowOf(i)))
|
|
95
|
-
return {
|
|
96
|
-
// Any window of this kind counts: a second query window is still Query, running.
|
|
97
|
-
running: windows.length > 0,
|
|
98
|
-
focused: items.some(i => key !== null && windowType(key) === windowOf(i)),
|
|
99
|
-
// Open, and none of it on the desk. One window of a kind still showing means the item is not.
|
|
100
|
-
minimized: windows.length > 0 && windows.every(w => isMinimized(state, w.id)),
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
106
|
|
|
104
107
|
/** Moves focus among the buttons in a container with arrow keys, Home and End. */
|
|
105
|
-
function roveFocus(event: KeyboardEvent<HTMLElement>, keys: { next: string[]; previous: string[] }) {
|
|
108
|
+
export function roveFocus(event: KeyboardEvent<HTMLElement>, keys: { next: string[]; previous: string[] }) {
|
|
106
109
|
// Only this container's own buttons: the dock must not rove into an open stack, nor a stack back out.
|
|
107
110
|
const container = event.currentTarget
|
|
108
111
|
const buttons = [...container.querySelectorAll<HTMLButtonElement>('[data-rove]:not(:disabled)')].filter(
|
|
@@ -122,16 +125,24 @@ function roveFocus(event: KeyboardEvent<HTMLElement>, keys: { next: string[]; pr
|
|
|
122
125
|
target.focus()
|
|
123
126
|
}
|
|
124
127
|
|
|
125
|
-
export function Dock(
|
|
128
|
+
export function Dock(props: DockProps) {
|
|
129
|
+
// In hosted mode the host draws one dock for every app, with its own Settings and About: this one
|
|
130
|
+
// sends it the app's entries instead.
|
|
131
|
+
return useHosted() ? <HostedDock entries={props.entries} /> : <DrawnDock {...props} />
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function DrawnDock({ entries: given, label = 'Dock', placement = 'overlay', side = 'left', className, pins }: DockProps) {
|
|
126
135
|
const vertical = side !== 'bottom'
|
|
127
136
|
const desk = useDesk()
|
|
128
137
|
const app = useDeskApp()
|
|
129
138
|
// Settings and About, where every Desk app has them, when the app has declared itself.
|
|
130
139
|
const entries = placeAppDockItems(given, app, {
|
|
131
|
-
settings: () =>
|
|
140
|
+
settings: () => openAppSettings(desk, app),
|
|
132
141
|
about: () => openPanel(desk, ABOUT_WINDOW),
|
|
133
142
|
})
|
|
134
143
|
const [openStack, setOpenStack] = useState<string | null>(null)
|
|
144
|
+
// A stack that went while open, as one Settings left empty, comes back closed.
|
|
145
|
+
if (openStack && !entries.some(e => e.id === openStack)) setOpenStack(null)
|
|
135
146
|
const firstFocusable = entries.find(e => e.type !== 'separator')?.id
|
|
136
147
|
const { dropProps } = useDropTarget({
|
|
137
148
|
accepts: pinAccepts(pins),
|
|
@@ -219,11 +230,7 @@ function DockButton({ item, tabIndex, pins }: DockButtonProps) {
|
|
|
219
230
|
aria-disabled={(item.disabled && item.contextMenu && true) || undefined}
|
|
220
231
|
{...dropProps}
|
|
221
232
|
{...drag}
|
|
222
|
-
onClick={() =>
|
|
223
|
-
if (item.disabled) return
|
|
224
|
-
if (item.onSelect) item.onSelect()
|
|
225
|
-
else desk.open(windowOf(item))
|
|
226
|
-
}}
|
|
233
|
+
onClick={() => selectDockItem(desk, item)}
|
|
227
234
|
onContextMenu={context.target.onContextMenu}
|
|
228
235
|
onKeyDown={context.target.onKeyDown}
|
|
229
236
|
>
|
|
@@ -314,8 +321,7 @@ function StackButton({ stack, tabIndex, open, setOpenStack, pins }: StackButtonP
|
|
|
314
321
|
}
|
|
315
322
|
|
|
316
323
|
const select = (item: DockItem) => {
|
|
317
|
-
|
|
318
|
-
else desk.open(windowOf(item))
|
|
324
|
+
selectDockItem(desk, item)
|
|
319
325
|
onOpenChange(false)
|
|
320
326
|
}
|
|
321
327
|
|