@liberation-data/desk 0.45.0 → 0.47.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.
Files changed (84) hide show
  1. package/README.md +73 -7
  2. package/dist/core/hosted.d.ts +39 -1
  3. package/dist/core/hosted.d.ts.map +1 -1
  4. package/dist/core/hosted.js +44 -0
  5. package/dist/core/hosted.js.map +1 -1
  6. package/dist/core/index.d.ts +5 -3
  7. package/dist/core/index.d.ts.map +1 -1
  8. package/dist/core/index.js +3 -2
  9. package/dist/core/index.js.map +1 -1
  10. package/dist/core/swipe.d.ts +15 -0
  11. package/dist/core/swipe.d.ts.map +1 -0
  12. package/dist/core/swipe.js +77 -0
  13. package/dist/core/swipe.js.map +1 -0
  14. package/dist/core/theme.d.ts +15 -0
  15. package/dist/core/theme.d.ts.map +1 -1
  16. package/dist/core/theme.js +73 -1
  17. package/dist/core/theme.js.map +1 -1
  18. package/dist/css/shell.css +89 -0
  19. package/dist/host/checkTheme.d.ts +2 -7
  20. package/dist/host/checkTheme.d.ts.map +1 -1
  21. package/dist/host/checkTheme.js +3 -27
  22. package/dist/host/checkTheme.js.map +1 -1
  23. package/dist/host/router.d.ts +17 -4
  24. package/dist/host/router.d.ts.map +1 -1
  25. package/dist/host/router.js +94 -2
  26. package/dist/host/router.js.map +1 -1
  27. package/dist/react/Desktop.d.ts.map +1 -1
  28. package/dist/react/Desktop.js +5 -0
  29. package/dist/react/Desktop.js.map +1 -1
  30. package/dist/react/context.d.ts +6 -1
  31. package/dist/react/context.d.ts.map +1 -1
  32. package/dist/react/context.js +5 -3
  33. package/dist/react/context.js.map +1 -1
  34. package/dist/react/hostApp.d.ts +3 -1
  35. package/dist/react/hostApp.d.ts.map +1 -1
  36. package/dist/react/hostApp.js +3 -2
  37. package/dist/react/hostApp.js.map +1 -1
  38. package/dist/react/hosted.d.ts +10 -3
  39. package/dist/react/hosted.d.ts.map +1 -1
  40. package/dist/react/hosted.js +68 -8
  41. package/dist/react/hosted.js.map +1 -1
  42. package/dist/react/shell.d.ts +3 -1
  43. package/dist/react/shell.d.ts.map +1 -1
  44. package/dist/react/shell.js +2 -2
  45. package/dist/react/shell.js.map +1 -1
  46. package/dist/shell/HostOverview.d.ts +41 -0
  47. package/dist/shell/HostOverview.d.ts.map +1 -0
  48. package/dist/shell/HostOverview.js +219 -0
  49. package/dist/shell/HostOverview.js.map +1 -0
  50. package/dist/shell/HostShell.d.ts +3 -1
  51. package/dist/shell/HostShell.d.ts.map +1 -1
  52. package/dist/shell/HostShell.js +6 -4
  53. package/dist/shell/HostShell.js.map +1 -1
  54. package/dist/shell/index.d.ts +6 -0
  55. package/dist/shell/index.d.ts.map +1 -1
  56. package/dist/shell/index.js +3 -0
  57. package/dist/shell/index.js.map +1 -1
  58. package/dist/shell/swipe.d.ts +60 -0
  59. package/dist/shell/swipe.d.ts.map +1 -0
  60. package/dist/shell/swipe.js +158 -0
  61. package/dist/shell/swipe.js.map +1 -0
  62. package/dist/shell/zoom.d.ts +62 -0
  63. package/dist/shell/zoom.d.ts.map +1 -0
  64. package/dist/shell/zoom.js +116 -0
  65. package/dist/shell/zoom.js.map +1 -0
  66. package/llms.txt +14 -5
  67. package/package.json +12 -7
  68. package/src/core/hosted.ts +70 -1
  69. package/src/core/index.ts +5 -2
  70. package/src/core/swipe.ts +101 -0
  71. package/src/core/theme.ts +69 -1
  72. package/src/css/shell.css +89 -0
  73. package/src/host/checkTheme.ts +3 -25
  74. package/src/host/router.ts +100 -6
  75. package/src/react/Desktop.tsx +6 -0
  76. package/src/react/context.tsx +9 -2
  77. package/src/react/hostApp.tsx +3 -2
  78. package/src/react/hosted.tsx +68 -8
  79. package/src/react/shell.tsx +4 -2
  80. package/src/shell/HostOverview.tsx +317 -0
  81. package/src/shell/HostShell.tsx +8 -5
  82. package/src/shell/index.ts +20 -0
  83. package/src/shell/swipe.ts +202 -0
  84. package/src/shell/zoom.ts +151 -0
@@ -0,0 +1,62 @@
1
+ /** Zooming out, moving across, zooming in, and the capture wait, in ms. */
2
+ export declare const ZOOM_OUT_MS = 200;
3
+ export declare const ZOOM_ACROSS_MS = 250;
4
+ export declare const ZOOM_IN_MS = 200;
5
+ /** How long to wait for a fresh capture of the current workspace before zooming from the picture already held. */
6
+ export declare const CAPTURE_WAIT_MS = 100;
7
+ /** Reduced motion: every move is this cross-fade, in place. */
8
+ export declare const ZOOM_FADE_MS = 120;
9
+ /** A CSS `cubic-bezier`, as a function of time from 0 to 1. */
10
+ export declare function cubicBezier(x1: number, y1: number, x2: number, y2: number): (t: number) => number;
11
+ export declare const easeOut: (t: number) => number;
12
+ export declare const easeIn: (t: number) => number;
13
+ export declare const easeInOut: (t: number) => number;
14
+ export interface OverviewPose {
15
+ /** 0 is a full-size desktop, 1 the overview. */
16
+ readonly zoom: number;
17
+ /** The index of the centred workspace; fractional while the row moves. */
18
+ readonly centre: number;
19
+ readonly fog: number;
20
+ readonly opacity: number;
21
+ }
22
+ export interface ZoomPhase {
23
+ readonly name: 'out' | 'across' | 'in' | 'fade';
24
+ readonly durationMs: number;
25
+ readonly from: OverviewPose;
26
+ readonly to: OverviewPose;
27
+ readonly ease: (t: number) => number;
28
+ }
29
+ export interface ZoomPlan {
30
+ readonly phases: readonly ZoomPhase[];
31
+ readonly durationMs: number;
32
+ /** The pose asked for: what poseAt gives for a plan with nothing to do (no phases). */
33
+ readonly to: OverviewPose;
34
+ }
35
+ /**
36
+ * How to get from one pose to another. Out of a full-size desktop first if the row must move or the
37
+ * overview is the goal (200 ms, ease-out); across if another workspace is to be centred (250 ms,
38
+ * ease-in-out); then in, if the goal is a desktop (200 ms, ease-in). A click on another workspace is
39
+ * all three, about 650 ms. Under reduced motion it is one 120 ms fade in at the goal.
40
+ */
41
+ export declare function planZoom(from: OverviewPose, to: OverviewPose, reducedMotion?: boolean): ZoomPlan;
42
+ /** Where a plan stands `elapsedMs` after it started: its goal from its duration on. */
43
+ export declare function poseAt(plan: ZoomPlan, elapsedMs: number): OverviewPose;
44
+ /** One tile as drawn: offsets in px from the desktop area's centre, a scale, and an opacity. */
45
+ export interface TilePlace {
46
+ readonly x: number;
47
+ /** Into the screen, under the layer's perspective: negative is further back. */
48
+ readonly z: number;
49
+ readonly scale: number;
50
+ readonly opacity: number;
51
+ /** Paint order: the centred tile on top. */
52
+ readonly layer: number;
53
+ }
54
+ /** The centred tile's scale in the overview, and how much space is between tiles. */
55
+ export declare const OVERVIEW_SCALE = 0.62;
56
+ /**
57
+ * Every tile's place at a pose, for a desktop area `width` px wide. At zoom 0 the centred tile fills
58
+ * the area and its neighbours sit just beside it, as in the slide; at 1 the row floats in the fog,
59
+ * neighbours further back, smaller and fainter, and nothing rotates.
60
+ */
61
+ export declare function overviewTiles(count: number, pose: OverviewPose, width: number): TilePlace[];
62
+ //# sourceMappingURL=zoom.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"zoom.d.ts","sourceRoot":"","sources":["../../src/shell/zoom.ts"],"names":[],"mappings":"AAaA,2EAA2E;AAC3E,eAAO,MAAM,WAAW,MAAM,CAAA;AAC9B,eAAO,MAAM,cAAc,MAAM,CAAA;AACjC,eAAO,MAAM,UAAU,MAAM,CAAA;AAC7B,kHAAkH;AAClH,eAAO,MAAM,eAAe,MAAM,CAAA;AAClC,+DAA+D;AAC/D,eAAO,MAAM,YAAY,MAAM,CAAA;AAE/B,+DAA+D;AAC/D,wBAAgB,WAAW,CAAC,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,CAAC,CAAC,EAAE,MAAM,KAAK,MAAM,CAejG;AAED,eAAO,MAAM,OAAO,MAjB6D,MAAM,KAAK,MAiB3C,CAAA;AACjD,eAAO,MAAM,MAAM,MAlB8D,MAAM,KAAK,MAkB5C,CAAA;AAChD,eAAO,MAAM,SAAS,MAnB2D,MAAM,KAAK,MAmBtC,CAAA;AAGtD,MAAM,WAAW,YAAY;IAC3B,gDAAgD;IACh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eAAO,MAAM,cAAc,OAAO,CAAA;AAOlC;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,GAAG,SAAS,EAAE,CAa3F"}
@@ -0,0 +1,116 @@
1
+ /*
2
+ * THE OVERVIEW AND THE ZOOM, AS NUMBERS.
3
+ *
4
+ * A live view can't be scaled (smaller bounds relay it out), so the zoom is drawn with pictures on an
5
+ * overlay: the current workspace's capture sits exactly over its live view, shrinks back into a fog
6
+ * where the workspaces float, the row moves across, and the target grows back to full size, where the
7
+ * host swaps its live view in. This file is where everything stands at each moment, with no DOM: the
8
+ * overlay draws from it, and a host can read the same plan to know how long a move takes.
9
+ *
10
+ * A pose is one moment: how far zoomed out (0 is a full-size desktop, 1 the overview), which workspace
11
+ * is centred (fractional while the row moves), how much fog shows, and how opaque the tiles are.
12
+ */
13
+ /** Zooming out, moving across, zooming in, and the capture wait, in ms. */
14
+ export const ZOOM_OUT_MS = 200;
15
+ export const ZOOM_ACROSS_MS = 250;
16
+ export const ZOOM_IN_MS = 200;
17
+ /** How long to wait for a fresh capture of the current workspace before zooming from the picture already held. */
18
+ export const CAPTURE_WAIT_MS = 100;
19
+ /** Reduced motion: every move is this cross-fade, in place. */
20
+ export const ZOOM_FADE_MS = 120;
21
+ /** A CSS `cubic-bezier`, as a function of time from 0 to 1. */
22
+ export function cubicBezier(x1, y1, x2, y2) {
23
+ const at = (a, b, s) => 3 * a * s * (1 - s) ** 2 + 3 * b * s * s * (1 - s) + s ** 3;
24
+ return t => {
25
+ if (t <= 0)
26
+ return 0;
27
+ if (t >= 1)
28
+ return 1;
29
+ // Find s with x(s) = t by bisection: x is increasing, and 20 halvings are well under a pixel.
30
+ let lo = 0;
31
+ let hi = 1;
32
+ for (let i = 0; i < 20; i++) {
33
+ const mid = (lo + hi) / 2;
34
+ if (at(x1, x2, mid) < t)
35
+ lo = mid;
36
+ else
37
+ hi = mid;
38
+ }
39
+ return at(y1, y2, (lo + hi) / 2);
40
+ };
41
+ }
42
+ export const easeOut = cubicBezier(0, 0, 0.58, 1);
43
+ export const easeIn = cubicBezier(0.42, 0, 1, 1);
44
+ export const easeInOut = cubicBezier(0.42, 0, 0.58, 1);
45
+ const straight = (t) => Math.min(1, Math.max(0, t));
46
+ /**
47
+ * How to get from one pose to another. Out of a full-size desktop first if the row must move or the
48
+ * overview is the goal (200 ms, ease-out); across if another workspace is to be centred (250 ms,
49
+ * ease-in-out); then in, if the goal is a desktop (200 ms, ease-in). A click on another workspace is
50
+ * all three, about 650 ms. Under reduced motion it is one 120 ms fade in at the goal.
51
+ */
52
+ export function planZoom(from, to, reducedMotion = false) {
53
+ if (reducedMotion) {
54
+ const phases = [{ name: 'fade', durationMs: ZOOM_FADE_MS, from: { ...to, opacity: 0 }, to: { ...to, opacity: 1 }, ease: straight }];
55
+ return { phases, durationMs: ZOOM_FADE_MS, to };
56
+ }
57
+ const phases = [];
58
+ let at = from;
59
+ const go = (name, durationMs, ease, next) => {
60
+ phases.push({ name, durationMs, from: at, to: next, ease });
61
+ at = next;
62
+ };
63
+ const moves = from.centre !== to.centre;
64
+ if (at.zoom < 1 && (moves || to.zoom === 1))
65
+ go('out', ZOOM_OUT_MS, easeOut, { zoom: 1, centre: at.centre, fog: Math.max(at.fog, to.fog), opacity: 1 });
66
+ if (moves)
67
+ go('across', ZOOM_ACROSS_MS, easeInOut, { ...at, centre: to.centre });
68
+ if (to.zoom < at.zoom)
69
+ go('in', ZOOM_IN_MS, easeIn, { ...to, centre: to.centre });
70
+ return { phases, durationMs: phases.reduce((total, p) => total + p.durationMs, 0), to };
71
+ }
72
+ const lerp = (a, b, t) => a + (b - a) * t;
73
+ /** Where a plan stands `elapsedMs` after it started: its goal from its duration on. */
74
+ export function poseAt(plan, elapsedMs) {
75
+ let left = elapsedMs;
76
+ for (const phase of plan.phases) {
77
+ if (left < phase.durationMs) {
78
+ const t = phase.ease(left / phase.durationMs);
79
+ return {
80
+ zoom: lerp(phase.from.zoom, phase.to.zoom, t),
81
+ centre: lerp(phase.from.centre, phase.to.centre, t),
82
+ fog: lerp(phase.from.fog, phase.to.fog, t),
83
+ opacity: lerp(phase.from.opacity, phase.to.opacity, t),
84
+ };
85
+ }
86
+ left -= phase.durationMs;
87
+ }
88
+ return plan.phases.at(-1)?.to ?? plan.to;
89
+ }
90
+ /** The centred tile's scale in the overview, and how much space is between tiles. */
91
+ export const OVERVIEW_SCALE = 0.62;
92
+ const GAP = 1.08;
93
+ /** Each step away from the centre: this far back, this much smaller, this much fainter. */
94
+ const STEP_BACK = 80;
95
+ const STEP_SCALE = 0.92;
96
+ const STEP_OPACITY = 0.85;
97
+ /**
98
+ * Every tile's place at a pose, for a desktop area `width` px wide. At zoom 0 the centred tile fills
99
+ * the area and its neighbours sit just beside it, as in the slide; at 1 the row floats in the fog,
100
+ * neighbours further back, smaller and fainter, and nothing rotates.
101
+ */
102
+ export function overviewTiles(count, pose, width) {
103
+ const scale = lerp(1, OVERVIEW_SCALE, pose.zoom);
104
+ const pitch = width * lerp(1, OVERVIEW_SCALE * GAP, pose.zoom);
105
+ return Array.from({ length: count }, (_, i) => {
106
+ const away = Math.min(Math.abs(i - pose.centre), 2);
107
+ return {
108
+ x: Math.round((i - pose.centre) * pitch),
109
+ z: -STEP_BACK * away * pose.zoom,
110
+ scale: scale * lerp(1, STEP_SCALE ** away, pose.zoom),
111
+ opacity: lerp(1, STEP_OPACITY ** away, pose.zoom),
112
+ layer: count - Math.round(Math.abs(i - pose.centre)),
113
+ };
114
+ });
115
+ }
116
+ //# sourceMappingURL=zoom.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"zoom.js","sourceRoot":"","sources":["../../src/shell/zoom.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,2EAA2E;AAC3E,MAAM,CAAC,MAAM,WAAW,GAAG,GAAG,CAAA;AAC9B,MAAM,CAAC,MAAM,cAAc,GAAG,GAAG,CAAA;AACj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AAG,KAAK,GAAG,IAAI,CAAC,CAAC,EAAE,cAAc,GAAG,GAAG,EAAE,IAAI,CAAC,IAAI,CAAC,CAAA;IAC9D,OAAO,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE;QAC5C,MAAM,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,CAAA;QACnD,OAAO;YACL,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,KAAK,CAAC;YACxC,CAAC,EAAE,CAAC,SAAS,GAAG,IAAI,GAAG,IAAI,CAAC,IAAI;YAChC,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC,CAAC,EAAE,UAAU,IAAI,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC;YACrD,OAAO,EAAE,IAAI,CAAC,CAAC,EAAE,YAAY,IAAI,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC;YACjD,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC;SACrD,CAAA;IACH,CAAC,CAAC,CAAA;AACJ,CAAC"}
package/llms.txt CHANGED
@@ -381,18 +381,26 @@ useWindowMenus(menus: Menu[]) // inside a window: its menus join the bar while
381
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
382
  interface DeskHostBridge { version: 1; send(m: AppToHost): void; onMessage(l: (m: HostToApp) => void): () => void }
383
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
384
+ useHost(): HostClient | null // publish(topic, payload); subscribe(publisher: appId | 'primary', topic, handler); onCommand; setChrome; primary(): boolean | null; onRole(l: (primary) => void); gesture(AppGesture) (a hosted Desktop calls it)
385
+ // connectHost sends { kind: 'hello' }; the host answers { kind: 'role', primary: boolean }, again on any change (createBroadcastRouter does; a host without it must). No answer: nothing published by default.
386
+ // The primary publishes appliedTheme(root?) (scheme, look, --desk-* tokens as worn; url() tokens and fonts left out) by default, again on change; <DeskShell|DeskProvider publishTheme={false}> turns it off
387
+ useHosted(): boolean; useHostBroadcast(publisher, topic, handler); usePublishTheme(theme: DeskTheme | null) // publishes this theme instead of appliedTheme while mounted
386
388
  interface DeskTheme { mode: 'light'|'dark'; look: Look; tokens: Record<`--${string}`, string>; fonts?: { family; src; weight?; style? }[]; wallpaper?: string }
387
389
  <DeskShell onTheme={theme => …}> // keep some looks of your own; mode and look always apply. Default: applyTheme(theme)
388
390
  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
+ // '@liberation-data/desk/host' (the host's half): createBroadcastRouter({ deliver, chrome?: (appId, chrome, dropped: string[]) => void, check?, gesture?: (appId, AppGesture) => void, front?: () => appId | null, maxPayload?, limits?: { burst?, perSecond?, topics?, maxTopics? }, clock? }), router.limited(appId), checkTheme(theme, origin), checkChrome(chrome, origin): { ok: true, chrome, dropped } | { ok: false, reason }, createMemoryHost()
392
+ // '@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?={'zoom'|'slide'} dockSide? onSwitch={(id, how) => …} onOverview?={() => …} onCommand={(appId, 'dock'|'menu'|'settings', itemId) => …} onInsets?={({ top, right, bottom, left }) => …} />
391
393
  // 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
394
  // 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
395
  // 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
396
  // animate({ durationMs, step, curve?, clock?: pageClock()|timerClock(), timeoutMs? = 250 }): { done: 'finished'|'jumped'|'stopped', progress(), stop() }; jumps to 1 when frames stop
397
+ // createSwipeController({ count(), current(), width(), reducedMotion?(), clock? = timerClock(), frame: (SlideFrame { from, to, progress, offsets, fade? }) => …, arrive: index => … }): { gesture(AppGesture), slideTo(index), busy(), stop() } // follows 1:1 (rubber band at the ends), goes on past COMMIT_DISTANCE 0.5 or a COMMIT_SPEED 0.5 widths/s flick, else springs back; catches a settling slide, a key's too; arrive once per landing, at rest (a slideTo from it starts afresh); stop() on closing: no more frames or arrive. swipeProgress, swipeDecision
398
+ // <HostOverview workspaces current snapshots?={Record<id, imageUrl>} request={{ kind: 'open' } | { kind: 'switch', to } | null} theme? insets? visible? reducedMotion? pose? onWantCapture?={id => …} onCover?={() => …} onArrive={id => …} /> // an overlay over the views; capture on onWantCapture into snapshots and return a promise of its URL (an identical picture still counts; waits 100 ms at most), hide views on onCover, swap the view in and clear request on onArrive; each once per move; the host revokes its own snapshot URLs
399
+ // planZoom(from, to, reducedMotion?): { phases: out 200 / across 250 / in 200 | fade 120, durationMs, to }; poseAt(plan, ms): { zoom, centre, fog, opacity }; overviewTiles(count, pose, width): { x, z, scale, opacity, layer }[]
395
400
  // 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.
401
+ // Swipes: a hosted <Desktop> passes a two-finger sideways wheel run that starts on its background (not in a window or the widget list) to the host: AppGesture { type: 'swipe', phase: 'begin'|'move'|'end'|'cancel', delta: total px since begin (+ toward the next workspace), velocity?: px/s at end }; isAppGesture(value). watchSwipes(element, send, { isBackground?, endAfterMs? = 120 }) → stop, for a page without React: send = g => client.gesture(g). Lines and pages taken as px; Shift and Ctrl wheels ignored; cancel on blur or a hidden page.
402
+ // The router passes gestures from front() only ('not in front' otherwise; a swipe under way is cancelled, its held move dropped, if its app leaves the front, is unregistered or says hello again, or another begin comes, its own app's too; a move, end or cancel with no swipe of its app under way is dropped); moves 30 then 120/s (limits.topics.gesture), the latest held; begin/end/cancel never held, and sent after any held move.
403
+ // Known limit, macOS: momentum wheel events after lift-off count as part of the swipe, which ends when they stop.
396
404
  // 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
405
  // 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
406
  // 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".
@@ -751,4 +759,5 @@ in the accessibility tree.
751
759
  - `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.
752
760
  - `fx.play` never resumes audio itself: outside a press a browser's resume may never settle. Call
753
761
  `fx.unlock()` at startup, or nothing sounds until a press after the sound was due.
754
- - The package is ESM only, with `"sideEffects": ["**/*.css"]`.
762
+ - The package is ESM, with `"sideEffects": ["**/*.css"]`. CommonJS code (an Electron main process) can `require()` every
763
+ entry on Node 22.12 or later.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@liberation-data/desk",
3
- "version": "0.45.0",
3
+ "version": "0.47.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",
@@ -36,23 +36,28 @@
36
36
  "exports": {
37
37
  ".": {
38
38
  "types": "./dist/core/index.d.ts",
39
- "import": "./dist/core/index.js"
39
+ "import": "./dist/core/index.js",
40
+ "default": "./dist/core/index.js"
40
41
  },
41
42
  "./react": {
42
43
  "types": "./dist/react/index.d.ts",
43
- "import": "./dist/react/index.js"
44
+ "import": "./dist/react/index.js",
45
+ "default": "./dist/react/index.js"
44
46
  },
45
47
  "./host": {
46
48
  "types": "./dist/host/index.d.ts",
47
- "import": "./dist/host/index.js"
49
+ "import": "./dist/host/index.js",
50
+ "default": "./dist/host/index.js"
48
51
  },
49
52
  "./shell": {
50
53
  "types": "./dist/shell/index.d.ts",
51
- "import": "./dist/shell/index.js"
54
+ "import": "./dist/shell/index.js",
55
+ "default": "./dist/shell/index.js"
52
56
  },
53
57
  "./fx": {
54
58
  "types": "./dist/fx/index.d.ts",
55
- "import": "./dist/fx/index.js"
59
+ "import": "./dist/fx/index.js",
60
+ "default": "./dist/fx/index.js"
56
61
  },
57
62
  "./desk.css": "./dist/desk.css",
58
63
  "./css/*": "./dist/css/*"
@@ -64,7 +69,7 @@
64
69
  "example": "vite examples/garage",
65
70
  "prepublishOnly": "npm run typecheck && npm test && npm run build",
66
71
  "size": "node scripts/size.mjs",
67
- "check": "npm run typecheck && npm test && npm run build && npm run size",
72
+ "check": "npm run typecheck && npm test && npm run build && npm run size && node scripts/require.mjs",
68
73
  "check:browser": "node scripts/browser-check.mjs",
69
74
  "screenshots": "node scripts/screenshots.mjs"
70
75
  },
@@ -165,18 +165,39 @@ export type Publisher = 'primary' | (string & {})
165
165
  /** What a host's command is for: the app's menus, its dock, or its Settings. */
166
166
  export type CommandTarget = 'menu' | 'dock' | 'settings'
167
167
 
168
+ /**
169
+ * A two-finger horizontal swipe that started on the app's desktop background, passed to the host,
170
+ * which moves between apps with it. One that starts inside a window is the window's, and never sent.
171
+ */
172
+ export interface AppGesture {
173
+ readonly type: 'swipe'
174
+ readonly phase: 'begin' | 'move' | 'end' | 'cancel'
175
+ /**
176
+ * How far the fingers have gone since `begin`, in CSS pixels, as the page scrolls: positive toward
177
+ * the next workspace. The total, not the step, so a host that drops moves loses nothing.
178
+ */
179
+ readonly delta: number
180
+ /** At `end`: how fast they were going as they lifted, in pixels a second, signed as `delta`. */
181
+ readonly velocity?: number
182
+ }
183
+
168
184
  /** What an app sends its host. Untrusted at the host's end: check it with `isAppToHost`. */
169
185
  export type AppToHost =
170
186
  | { readonly desk: 1; readonly kind: 'chrome'; readonly chrome: DeskChrome }
171
187
  | { readonly desk: 1; readonly kind: 'publish'; readonly topic: string; readonly payload: unknown }
172
188
  | { readonly desk: 1; readonly kind: 'subscribe'; readonly publisher: Publisher; readonly topic: string }
173
189
  | { readonly desk: 1; readonly kind: 'unsubscribe'; readonly publisher: Publisher; readonly topic: string }
190
+ /** Sent on connecting: the host answers with the app's `role`. */
191
+ | { readonly desk: 1; readonly kind: 'hello' }
192
+ | { readonly desk: 1; readonly kind: 'gesture'; readonly gesture: AppGesture }
174
193
 
175
194
  /** What a host sends an app: a choice from the chrome it draws, or another app's broadcast, stamped. */
176
195
  export type HostToApp =
177
196
  /** A menu item or dock item chosen by its id, or Settings opened at a section's id (`''` for the first). */
178
197
  | { readonly desk: 1; readonly kind: 'command'; readonly target: CommandTarget; readonly id: string }
179
198
  | { readonly desk: 1; readonly kind: 'broadcast'; readonly from: BroadcastSender; readonly topic: string; readonly payload: unknown }
199
+ /** Whether the app is the primary, whose theme the others wear: the answer to `hello`, and again when it changes. */
200
+ | { readonly desk: 1; readonly kind: 'role'; readonly primary: boolean }
180
201
 
181
202
  /**
182
203
  * The app's end of the connection to its host. The host provides it — an Electron preload through
@@ -219,11 +240,22 @@ export function isAppToHost(value: unknown): value is AppToHost {
219
240
  case 'subscribe':
220
241
  case 'unsubscribe':
221
242
  return isString(value.publisher) && isString(value.topic) && value.topic.length > 0
243
+ case 'hello':
244
+ return true
245
+ case 'gesture':
246
+ return isAppGesture(value.gesture)
222
247
  default:
223
248
  return false
224
249
  }
225
250
  }
226
251
 
252
+ const PHASES: readonly unknown[] = ['begin', 'move', 'end', 'cancel']
253
+ const isFiniteNumber = (value: unknown): value is number => typeof value === 'number' && Number.isFinite(value)
254
+
255
+ /** Whether `value` is a gesture as an app sends one: a known phase, and finite numbers. */
256
+ export const isAppGesture = (value: unknown): value is AppGesture =>
257
+ isRecord(value) && value.type === 'swipe' && PHASES.includes(value.phase) && isFiniteNumber(value.delta) && (value.velocity === undefined || isFiniteNumber(value.velocity))
258
+
227
259
  /** Whether a message from a host has the shape of one. */
228
260
  export function isHostToApp(value: unknown): value is HostToApp {
229
261
  if (!isRecord(value) || value.desk !== DESK_PROTOCOL) return false
@@ -232,6 +264,8 @@ export function isHostToApp(value: unknown): value is HostToApp {
232
264
  return (value.target === 'menu' || value.target === 'dock' || value.target === 'settings') && isString(value.id)
233
265
  case 'broadcast':
234
266
  return isRecord(value.from) && isString(value.from.id) && isString(value.topic)
267
+ case 'role':
268
+ return typeof value.primary === 'boolean'
235
269
  default:
236
270
  return false
237
271
  }
@@ -258,7 +292,13 @@ export interface HostClient {
258
292
  * one straight away, so an app that starts late still hears the current theme.
259
293
  */
260
294
  subscribe<T = unknown>(publisher: Publisher, topic: string, handler: (broadcast: Broadcast<T>) => void): () => void
261
- /** Stops listening, and takes the chrome away. */
295
+ /** Whether the host says this app is the primary; null until it has said. */
296
+ primary(): boolean | null
297
+ /** Calls the listener when the host says whether this app is the primary, now and on any change. */
298
+ onRole(listener: (primary: boolean) => void): () => void
299
+ /** Passes a swipe on the desktop background to the host. `DeskProvider`'s `Desktop` does this itself. */
300
+ gesture(gesture: AppGesture): void
301
+ /** Stops listening, takes the chrome away, and puts back a swipe under way. */
262
302
  close(): void
263
303
  }
264
304
 
@@ -274,9 +314,19 @@ export function connectHost(bridge: DeskHostBridge): HostClient {
274
314
  /** Per subscription, the broadcasts last heard on each topic it covers, for a second handler that joins later. */
275
315
  const subscriptions = new Map<string, { publisher: Publisher; topic: string; handlers: Set<Handler>; last: Map<string, Broadcast> }>()
276
316
  const keyOf = (publisher: string, topic: string) => `${publisher}\u0000${topic}`
317
+ let primary: boolean | null = null
318
+ const roles = new Set<(primary: boolean) => void>()
319
+ /** How far a swipe under way had got, so closing puts it back: a watcher stopped after the close is not heard. */
320
+ let swipe: number | null = null
277
321
 
278
322
  const stop = bridge.onMessage(message => {
279
323
  if (closed || !isHostToApp(message)) return
324
+ if (message.kind === 'role') {
325
+ if (primary === message.primary) return
326
+ primary = message.primary
327
+ for (const listener of [...roles]) if (roles.has(listener)) listener(message.primary)
328
+ return
329
+ }
280
330
  if (message.kind === 'command') {
281
331
  const listeners = commands[message.target]
282
332
  // Copied, and each still checked: a listener may take another away while this one runs.
@@ -293,6 +343,9 @@ export function connectHost(bridge: DeskHostBridge): HostClient {
293
343
  }
294
344
  })
295
345
 
346
+ // Listening first, so a host that answers at once is heard.
347
+ bridge.send({ desk: DESK_PROTOCOL, kind: 'hello' })
348
+
296
349
  return {
297
350
  bridge,
298
351
  setChrome(part) {
@@ -314,6 +367,11 @@ export function connectHost(bridge: DeskHostBridge): HostClient {
314
367
  publish(topic, payload) {
315
368
  if (!closed) bridge.send({ desk: DESK_PROTOCOL, kind: 'publish', topic, payload })
316
369
  },
370
+ gesture(gesture) {
371
+ if (closed) return
372
+ swipe = gesture.phase === 'begin' || gesture.phase === 'move' ? gesture.delta : null
373
+ bridge.send({ desk: DESK_PROTOCOL, kind: 'gesture', gesture })
374
+ },
317
375
  subscribe(publisher, topic, handler) {
318
376
  const key = keyOf(publisher, topic)
319
377
  const existing = subscriptions.get(key)
@@ -337,9 +395,20 @@ export function connectHost(bridge: DeskHostBridge): HostClient {
337
395
  }
338
396
  }
339
397
  },
398
+ primary: () => primary,
399
+ onRole(listener) {
400
+ roles.add(listener)
401
+ if (primary !== null) listener(primary)
402
+ return () => {
403
+ roles.delete(listener)
404
+ }
405
+ },
340
406
  close() {
341
407
  if (closed) return
408
+ roles.clear()
409
+ primary = null
342
410
  bridge.send({ desk: DESK_PROTOCOL, kind: 'chrome', chrome: EMPTY_CHROME })
411
+ if (swipe !== null) bridge.send({ desk: DESK_PROTOCOL, kind: 'gesture', gesture: { type: 'swipe', phase: 'cancel', delta: swipe } })
343
412
  // So the host stops sending what nobody here hears.
344
413
  for (const { publisher, topic } of subscriptions.values()) bridge.send({ desk: DESK_PROTOCOL, kind: 'unsubscribe', publisher, topic })
345
414
  closed = true
package/src/core/index.ts CHANGED
@@ -27,6 +27,8 @@ export {
27
27
  } from './commands.js'
28
28
  export type { ChainOptions, CommandHandler, CommandId, CommandOptions } from './commands.js'
29
29
  export { createService, holdAll } from './services.js'
30
+ export { watchSwipes } from './swipe.js'
31
+ export type { SwipeOptions } from './swipe.js'
30
32
  export type { Holdable, Service, ServiceOptions, ServiceRun } from './services.js'
31
33
  export { attended, attentionOn, becomesNews, watchAttention } from './attention.js'
32
34
  export type { Attention } from './attention.js'
@@ -39,12 +41,13 @@ export { COLOR_SCHEME_KEY, COLOR_SCHEME_SCRIPT, colorSchemeScript, LOOK_KEY } fr
39
41
  export { getPreference, localPreferences, setPreference, setPreferenceStore, subscribePreferences } from './preferences.js'
40
42
  export type { PreferenceStore } from './preferences.js'
41
43
  export type { ColorScheme } from './scheme.js'
42
- export { applyTheme, applyThemeBasics } from './theme.js'
44
+ export { applyTheme, applyThemeBasics, appliedTheme } from './theme.js'
43
45
  export { drawnTheme, THEME_CHANGE, watchThemeChanges } from './themeChange.js'
44
46
  export type { ThemeChange } from './themeChange.js'
45
47
  export type { AppTheme, DeskTheme, ThemeFont } from './theme.js'
46
- export { connectHost, DESK_PROTOCOL, findDeskHost, isAppToHost, isDeskHostBridge, isHostToApp, THEME_TOPIC } from './hosted.js'
48
+ export { connectHost, DESK_PROTOCOL, findDeskHost, isAppGesture, isAppToHost, isDeskHostBridge, isHostToApp, THEME_TOPIC } from './hosted.js'
47
49
  export type {
50
+ AppGesture,
48
51
  AppToHost,
49
52
  Broadcast,
50
53
  BroadcastSender,
@@ -0,0 +1,101 @@
1
+ import { WINDOW_ATTRIBUTE } from './commands.js'
2
+ import type { AppGesture } from './hosted.js'
3
+
4
+ /*
5
+ * A TWO-FINGER SWIPE ON THE DESKTOP BACKGROUND.
6
+ *
7
+ * Chromium hands a page a trackpad's two-finger movement as wheel events, with no begin or end: a
8
+ * swipe is a run of them, mostly sideways, ended by a pause. So a run that starts on the background
9
+ * and goes sideways first is a swipe, and is passed on as begin, moves and end; one that starts in a
10
+ * window, or goes up and down first, is scrolling, and is left alone until it pauses. Three and four
11
+ * fingers never reach a page: the OS keeps them.
12
+ *
13
+ * Wheel steps come in pixels from a trackpad on every platform Chromium runs on, but in lines or
14
+ * pages from some mice and other browsers: all are turned into pixels. Shift with a wheel is a
15
+ * mouse's sideways scroll, not a swipe. On a Mac the trackpad's momentum arrives as more wheel
16
+ * events after the fingers lift; they are taken as part of the swipe, which ends when they stop.
17
+ */
18
+
19
+ export interface SwipeOptions {
20
+ /** Whether a run that starts on this element is the background's; by default, anything outside a window or the widget list. */
21
+ readonly isBackground?: (target: EventTarget | null) => boolean
22
+ /** The pause that ends a swipe, in ms. */
23
+ readonly endAfterMs?: number
24
+ /** For tests: the time, and a delay that returns its cancel. */
25
+ readonly clock?: { readonly now: () => number; readonly later: (run: () => void, ms: number) => () => void }
26
+ }
27
+
28
+ /** How far a run must go before it is a swipe or a scroll, in px, and how much more sideways than up and down. */
29
+ const DECIDE_PX = 6
30
+ const SIDEWAYS = 1.5
31
+ /** A line of a wheel in px, as Chromium scrolls one. */
32
+ const LINE_PX = 40
33
+ /** How far back the lift-off speed is measured, in ms. */
34
+ const VELOCITY_MS = 80
35
+
36
+ const pixels = (delta: number, mode: number, page: number) => (mode === 1 ? delta * LINE_PX : mode === 2 ? delta * page : delta)
37
+
38
+ const outsideWindows = (target: EventTarget | null) =>
39
+ typeof Element !== 'undefined' && target instanceof Element && !target.closest(`[${WINDOW_ATTRIBUTE}], .desk-widget-list`)
40
+
41
+ /** Watches `element` for swipes that start on its background and passes each on. Returns a stop. */
42
+ export function watchSwipes(element: HTMLElement, send: (gesture: AppGesture) => void, options: SwipeOptions = {}): () => void {
43
+ const isBackground = options.isBackground ?? outsideWindows
44
+ const endAfter = options.endAfterMs ?? 120
45
+ const clock = options.clock ?? { now: () => performance.now(), later: (run, ms) => clearTimeout.bind(null, setTimeout(run, ms)) }
46
+ let state: 'idle' | 'deciding' | 'swiping' | 'scrolling' = 'idle'
47
+ let x = 0
48
+ let y = 0
49
+ let samples: { at: number; x: number }[] = []
50
+ let cancelEnd: (() => void) | null = null
51
+
52
+ const velocity = () => {
53
+ const last = samples.at(-1)
54
+ const first = last && samples.find(s => last.at - s.at <= VELOCITY_MS)
55
+ return last && first && last.at > first.at ? ((last.x - first.x) * 1000) / (last.at - first.at) : 0
56
+ }
57
+ const finish = (phase: 'end' | 'cancel') => {
58
+ cancelEnd?.()
59
+ cancelEnd = null
60
+ if (state === 'swiping') send(phase === 'end' ? { type: 'swipe', phase, delta: x, velocity: velocity() } : { type: 'swipe', phase, delta: x })
61
+ state = 'idle'
62
+ x = y = 0
63
+ samples = []
64
+ }
65
+
66
+ const onWheel = (event: WheelEvent) => {
67
+ if (event.ctrlKey || event.shiftKey) return
68
+ cancelEnd?.()
69
+ cancelEnd = clock.later(() => finish('end'), endAfter)
70
+ if (state === 'idle') state = isBackground(event.target) ? 'deciding' : 'scrolling'
71
+ if (state === 'scrolling') return
72
+ x += pixels(event.deltaX, event.deltaMode, element.clientWidth || innerWidth)
73
+ y += pixels(event.deltaY, event.deltaMode, element.clientHeight || innerHeight)
74
+ if (state === 'deciding') {
75
+ if (Math.abs(x) + Math.abs(y) < DECIDE_PX) return
76
+ if (Math.abs(x) <= SIDEWAYS * Math.abs(y)) return void (state = 'scrolling')
77
+ state = 'swiping'
78
+ send({ type: 'swipe', phase: 'begin', delta: 0 })
79
+ // Stopped as the begin was heard: it was cancelled, so no move comes after.
80
+ if (state !== 'swiping') return
81
+ }
82
+ // The background's to move now: not the browser's back and forward.
83
+ event.preventDefault()
84
+ samples.push({ at: clock.now(), x })
85
+ if (samples.length > 32) samples.shift()
86
+ send({ type: 'swipe', phase: 'move', delta: x })
87
+ }
88
+ // Leaving the window mid-swipe leaves it unfinished: the host puts things back.
89
+ const onLeave = () => finish('cancel')
90
+ const onVisibility = () => document.visibilityState === 'hidden' && finish('cancel')
91
+
92
+ element.addEventListener('wheel', onWheel, { passive: false })
93
+ addEventListener('blur', onLeave)
94
+ document.addEventListener('visibilitychange', onVisibility)
95
+ return () => {
96
+ finish('cancel')
97
+ element.removeEventListener('wheel', onWheel)
98
+ removeEventListener('blur', onLeave)
99
+ document.removeEventListener('visibilitychange', onVisibility)
100
+ }
101
+ }