@liberation-data/desk 0.42.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.
Files changed (200) hide show
  1. package/README.md +312 -5
  2. package/dist/core/hosted.d.ts +222 -0
  3. package/dist/core/hosted.d.ts.map +1 -0
  4. package/dist/core/hosted.js +166 -0
  5. package/dist/core/hosted.js.map +1 -0
  6. package/dist/core/index.d.ts +10 -0
  7. package/dist/core/index.d.ts.map +1 -1
  8. package/dist/core/index.js +5 -0
  9. package/dist/core/index.js.map +1 -1
  10. package/dist/core/location.d.ts +8 -1
  11. package/dist/core/location.d.ts.map +1 -1
  12. package/dist/core/location.js +18 -2
  13. package/dist/core/location.js.map +1 -1
  14. package/dist/core/preferences.d.ts +27 -0
  15. package/dist/core/preferences.d.ts.map +1 -0
  16. package/dist/core/preferences.js +121 -0
  17. package/dist/core/preferences.js.map +1 -0
  18. package/dist/core/scheme.d.ts +18 -0
  19. package/dist/core/scheme.d.ts.map +1 -0
  20. package/dist/core/scheme.js +24 -0
  21. package/dist/core/scheme.js.map +1 -0
  22. package/dist/core/theme.d.ts +41 -0
  23. package/dist/core/theme.d.ts.map +1 -0
  24. package/dist/core/theme.js +56 -0
  25. package/dist/core/theme.js.map +1 -0
  26. package/dist/core/themeChange.d.ts +25 -0
  27. package/dist/core/themeChange.d.ts.map +1 -0
  28. package/dist/core/themeChange.js +49 -0
  29. package/dist/core/themeChange.js.map +1 -0
  30. package/dist/core/types.d.ts +6 -0
  31. package/dist/core/types.d.ts.map +1 -1
  32. package/dist/css/apps.css +183 -0
  33. package/dist/css/overlays.css +6 -0
  34. package/dist/css/shell.css +100 -0
  35. package/dist/css/tokens.css +10 -0
  36. package/dist/css/windows.css +21 -0
  37. package/dist/desk.css +220 -0
  38. package/dist/fx/fx.d.ts.map +1 -1
  39. package/dist/fx/fx.js +7 -31
  40. package/dist/fx/fx.js.map +1 -1
  41. package/dist/host/checkChrome.d.ts +20 -0
  42. package/dist/host/checkChrome.d.ts.map +1 -0
  43. package/dist/host/checkChrome.js +219 -0
  44. package/dist/host/checkChrome.js.map +1 -0
  45. package/dist/host/checkTheme.d.ts +35 -0
  46. package/dist/host/checkTheme.d.ts.map +1 -0
  47. package/dist/host/checkTheme.js +128 -0
  48. package/dist/host/checkTheme.js.map +1 -0
  49. package/dist/host/index.d.ts +7 -0
  50. package/dist/host/index.d.ts.map +1 -0
  51. package/dist/host/index.js +8 -0
  52. package/dist/host/index.js.map +1 -0
  53. package/dist/host/router.d.ts +99 -0
  54. package/dist/host/router.d.ts.map +1 -0
  55. package/dist/host/router.js +279 -0
  56. package/dist/host/router.js.map +1 -0
  57. package/dist/react/Desktop.d.ts.map +1 -1
  58. package/dist/react/Desktop.js +13 -2
  59. package/dist/react/Desktop.js.map +1 -1
  60. package/dist/react/Dock.d.ts +26 -2
  61. package/dist/react/Dock.d.ts.map +1 -1
  62. package/dist/react/Dock.js +23 -27
  63. package/dist/react/Dock.js.map +1 -1
  64. package/dist/react/MenuBar.d.ts +1 -1
  65. package/dist/react/MenuBar.d.ts.map +1 -1
  66. package/dist/react/MenuBar.js +36 -6
  67. package/dist/react/MenuBar.js.map +1 -1
  68. package/dist/react/app.d.ts +215 -0
  69. package/dist/react/app.d.ts.map +1 -0
  70. package/dist/react/app.js +192 -0
  71. package/dist/react/app.js.map +1 -0
  72. package/dist/react/appFrame.d.ts +11 -7
  73. package/dist/react/appFrame.d.ts.map +1 -1
  74. package/dist/react/appFrame.js +24 -6
  75. package/dist/react/appFrame.js.map +1 -1
  76. package/dist/react/appPanels.d.ts +25 -0
  77. package/dist/react/appPanels.d.ts.map +1 -0
  78. package/dist/react/appPanels.js +123 -0
  79. package/dist/react/appPanels.js.map +1 -0
  80. package/dist/react/appearance.d.ts +61 -0
  81. package/dist/react/appearance.d.ts.map +1 -0
  82. package/dist/react/appearance.js +119 -0
  83. package/dist/react/appearance.js.map +1 -0
  84. package/dist/react/codeBlock.d.ts.map +1 -1
  85. package/dist/react/codeBlock.js +6 -60
  86. package/dist/react/codeBlock.js.map +1 -1
  87. package/dist/react/context.d.ts +23 -1
  88. package/dist/react/context.d.ts.map +1 -1
  89. package/dist/react/context.js +108 -5
  90. package/dist/react/context.js.map +1 -1
  91. package/dist/react/copy.d.ts +22 -0
  92. package/dist/react/copy.d.ts.map +1 -0
  93. package/dist/react/copy.js +68 -0
  94. package/dist/react/copy.js.map +1 -0
  95. package/dist/react/dockStatus.d.ts +19 -0
  96. package/dist/react/dockStatus.d.ts.map +1 -0
  97. package/dist/react/dockStatus.js +25 -0
  98. package/dist/react/dockStatus.js.map +1 -0
  99. package/dist/react/documentTitle.d.ts.map +1 -1
  100. package/dist/react/documentTitle.js +4 -1
  101. package/dist/react/documentTitle.js.map +1 -1
  102. package/dist/react/frameMenus.d.ts +28 -0
  103. package/dist/react/frameMenus.d.ts.map +1 -0
  104. package/dist/react/frameMenus.js +85 -0
  105. package/dist/react/frameMenus.js.map +1 -0
  106. package/dist/react/hostApp.d.ts +15 -0
  107. package/dist/react/hostApp.d.ts.map +1 -0
  108. package/dist/react/hostApp.js +77 -0
  109. package/dist/react/hostApp.js.map +1 -0
  110. package/dist/react/hostDock.d.ts +25 -0
  111. package/dist/react/hostDock.d.ts.map +1 -0
  112. package/dist/react/hostDock.js +98 -0
  113. package/dist/react/hostDock.js.map +1 -0
  114. package/dist/react/hostMenu.d.ts +2 -36
  115. package/dist/react/hostMenu.d.ts.map +1 -1
  116. package/dist/react/hostMenu.js.map +1 -1
  117. package/dist/react/hosted.d.ts +28 -0
  118. package/dist/react/hosted.d.ts.map +1 -0
  119. package/dist/react/hosted.js +53 -0
  120. package/dist/react/hosted.js.map +1 -0
  121. package/dist/react/index.d.ts +12 -0
  122. package/dist/react/index.d.ts.map +1 -1
  123. package/dist/react/index.js +8 -0
  124. package/dist/react/index.js.map +1 -1
  125. package/dist/react/layers.d.ts.map +1 -1
  126. package/dist/react/layers.js +7 -2
  127. package/dist/react/layers.js.map +1 -1
  128. package/dist/react/overlays.d.ts.map +1 -1
  129. package/dist/react/overlays.js +29 -12
  130. package/dist/react/overlays.js.map +1 -1
  131. package/dist/react/shell.d.ts +16 -1
  132. package/dist/react/shell.d.ts.map +1 -1
  133. package/dist/react/shell.js +19 -3
  134. package/dist/react/shell.js.map +1 -1
  135. package/dist/react/windowMenus.d.ts +19 -0
  136. package/dist/react/windowMenus.d.ts.map +1 -0
  137. package/dist/react/windowMenus.js +88 -0
  138. package/dist/react/windowMenus.js.map +1 -0
  139. package/dist/shell/HostShell.d.ts +53 -0
  140. package/dist/shell/HostShell.d.ts.map +1 -0
  141. package/dist/shell/HostShell.js +116 -0
  142. package/dist/shell/HostShell.js.map +1 -0
  143. package/dist/shell/index.d.ts +7 -0
  144. package/dist/shell/index.d.ts.map +1 -0
  145. package/dist/shell/index.js +9 -0
  146. package/dist/shell/index.js.map +1 -0
  147. package/dist/shell/keys.d.ts +47 -0
  148. package/dist/shell/keys.d.ts.map +1 -0
  149. package/dist/shell/keys.js +68 -0
  150. package/dist/shell/keys.js.map +1 -0
  151. package/dist/shell/motion.d.ts +72 -0
  152. package/dist/shell/motion.d.ts.map +1 -0
  153. package/dist/shell/motion.js +118 -0
  154. package/dist/shell/motion.js.map +1 -0
  155. package/llms.txt +84 -9
  156. package/package.json +10 -1
  157. package/src/core/hosted.ts +351 -0
  158. package/src/core/index.ts +29 -0
  159. package/src/core/location.ts +23 -3
  160. package/src/core/preferences.ts +132 -0
  161. package/src/core/scheme.ts +32 -0
  162. package/src/core/theme.ts +91 -0
  163. package/src/core/themeChange.ts +59 -0
  164. package/src/core/types.ts +6 -0
  165. package/src/css/apps.css +183 -0
  166. package/src/css/overlays.css +6 -0
  167. package/src/css/shell.css +99 -0
  168. package/src/css/tokens.css +10 -0
  169. package/src/css/windows.css +21 -0
  170. package/src/fx/fx.ts +7 -25
  171. package/src/host/checkChrome.ts +257 -0
  172. package/src/host/checkTheme.ts +134 -0
  173. package/src/host/index.ts +10 -0
  174. package/src/host/router.ts +374 -0
  175. package/src/react/Desktop.tsx +21 -3
  176. package/src/react/Dock.tsx +40 -24
  177. package/src/react/MenuBar.tsx +39 -6
  178. package/src/react/app.tsx +354 -0
  179. package/src/react/appFrame.tsx +39 -14
  180. package/src/react/appPanels.tsx +210 -0
  181. package/src/react/appearance.tsx +195 -0
  182. package/src/react/codeBlock.tsx +7 -65
  183. package/src/react/context.tsx +131 -4
  184. package/src/react/copy.ts +81 -0
  185. package/src/react/dockStatus.ts +42 -0
  186. package/src/react/documentTitle.ts +4 -1
  187. package/src/react/frameMenus.ts +100 -0
  188. package/src/react/hostApp.tsx +89 -0
  189. package/src/react/hostDock.tsx +118 -0
  190. package/src/react/hostMenu.ts +3 -38
  191. package/src/react/hosted.tsx +65 -0
  192. package/src/react/index.ts +34 -0
  193. package/src/react/layers.ts +7 -2
  194. package/src/react/overlays.tsx +30 -12
  195. package/src/react/shell.tsx +33 -3
  196. package/src/react/windowMenus.ts +107 -0
  197. package/src/shell/HostShell.tsx +220 -0
  198. package/src/shell/index.ts +11 -0
  199. package/src/shell/keys.ts +101 -0
  200. package/src/shell/motion.ts +163 -0
@@ -1,13 +1,26 @@
1
- import { createContext, useCallback, useContext, useEffect, useLayoutEffect, useState, useSyncExternalStore } from 'react'
1
+ import { createContext, useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState, useSyncExternalStore } from 'react'
2
2
  import type { ReactNode, RefObject } from 'react'
3
+ import { addCommandHandler } from '../core/commands.js'
3
4
  import { createBus } from '../core/events.js'
4
5
  import type { Bus } from '../core/events.js'
5
6
  import { createDesk, routeOf } from '../core/desk.js'
7
+ import { connectHost, findDeskHost, THEME_TOPIC } from '../core/hosted.js'
8
+ import type { DeskHostBridge, HostClient } from '../core/hosted.js'
9
+ import { applyTheme, applyThemeBasics } from '../core/theme.js'
10
+ import type { DeskTheme } from '../core/theme.js'
6
11
  import { deviceLook, resolveLook } from '../core/look.js'
12
+ import { watchThemeChanges } from '../core/themeChange.js'
13
+ import { deskDrawsWindows } from '../core/location.js'
7
14
  import { holdAll } from '../core/services.js'
8
15
  import type { Holdable } from '../core/services.js'
9
16
  import type { Look, LookChoice } from '../core/look.js'
17
+ import { AppProvider, bindDeskShortcuts, declareOn, desksWindow, hasSettings, openAppSettings, SettingsCommand } from './app.js'
18
+ import { AppearanceSettings, hasRows, hostThemed, rowsOf, showColorScheme, shownRows, useChosenColorScheme, useDefaultColorScheme, useLookChoice, useThemeFromHost } from './appearance.js'
19
+ import type { DeskApp, SettingsSection } from './app.js'
20
+ import type { AppearanceRows } from './appearance.js'
10
21
  import { DragProvider } from './dragContext.js'
22
+ import { HostedApp } from './hostApp.js'
23
+ import { HostContext } from './hosted.js'
11
24
  import { missingProvider } from './missingProvider.js'
12
25
  import type { Desk } from '../core/desk.js'
13
26
  import type { DeskOptions, DeskState, WindowId } from '../core/types.js'
@@ -28,6 +41,7 @@ export interface DeskProviderProps {
28
41
  /**
29
42
  * The platform chrome: `auto` (the default) matches the device, or the app passes the one its user
30
43
  * chose. The app keeps that choice where it keeps its other preferences; the desk does not store it.
44
+ * With Appearance's `look` row, the person's choice there, remembered by Desk, stands in for this one.
31
45
  */
32
46
  readonly look?: LookChoice
33
47
  /**
@@ -35,33 +49,146 @@ export interface DeskProviderProps {
35
49
  * so the array need not be stable.
36
50
  */
37
51
  readonly services?: readonly Holdable[]
52
+ /**
53
+ * What the app says about itself: its name, icon, version and links, which give it an About, and its
54
+ * settings, if it has any, which give it Settings. The dock, the menu bar and the desktop put both in their
55
+ * standard places. Read on every render, so it may change.
56
+ */
57
+ readonly app?: DeskApp
58
+ /**
59
+ * Hosted mode: a host that draws one menu bar and dock for several apps. The app's `MenuBar` and
60
+ * `Dock` then draw nothing and send their contents to the host, and the app wears the theme the
61
+ * host passes on. Left out, the app is hosted when the page has a `globalThis.deskHost` that speaks
62
+ * this protocol; `null` keeps it unhosted whatever the page has. Read once.
63
+ */
64
+ readonly host?: DeskHostBridge | null
65
+ /**
66
+ * How a hosted app wears the theme it is sent, for one that keeps some looks of its own. Light or
67
+ * dark and the platform chrome apply whatever this does; the default, `applyTheme`, takes all of it.
68
+ */
69
+ readonly onTheme?: (theme: DeskTheme) => void
38
70
  readonly children: ReactNode
39
71
  }
40
72
 
41
- export function DeskProvider({ desk, options, look = 'auto', services, children }: DeskProviderProps) {
73
+ /**
74
+ * Connects to the host while mounted, and wears the theme it passes on unless `wears` is false: the
75
+ * primary, whose theme it is, keeps its own. Null client until connected.
76
+ */
77
+ function useHostConnection(bridge: DeskHostBridge | null, wears: boolean, onTheme: ((theme: DeskTheme) => void) | undefined) {
78
+ const [client, setClient] = useState<HostClient | null>(null)
79
+ const [theme, setTheme] = useState<DeskTheme | null>(null)
80
+ const wear = useRef(onTheme)
81
+ wear.current = onTheme
82
+ useEffect(() => {
83
+ if (!bridge) return
84
+ const connected = connectHost(bridge)
85
+ const stop = !wears ? () => {} : connected.subscribe<DeskTheme>('primary', THEME_TOPIC, ({ payload }) => {
86
+ ;(wear.current ?? applyTheme)(payload)
87
+ // After the app's own handler, so light or dark and the look hold whatever it did.
88
+ applyThemeBasics(payload)
89
+ setTheme(payload)
90
+ })
91
+ setClient(connected)
92
+ return () => {
93
+ stop()
94
+ connected.close()
95
+ setClient(null)
96
+ setTheme(null)
97
+ }
98
+ }, [bridge, wears])
99
+ return { client, theme }
100
+ }
101
+
102
+ export function DeskProvider({ desk, options, look: appLook = 'auto', services, app: declared, host, onTheme, children }: DeskProviderProps) {
42
103
  const [value] = useState(() => desk ?? createDesk(options))
43
104
  const [bus] = useState(createBus)
44
105
  const [held] = useState(() => services ?? [])
45
106
  useEffect(() => holdAll(held), [held])
46
- const resolved = resolveLook(look)
107
+ const [bridge] = useState(() => (host === undefined ? findDeskHost() : host))
108
+ // The primary declares `themeFromHost: false`: the theme is its own, so it keeps its Appearance.
109
+ const hosted = useHostConnection(bridge, declared?.themeFromHost !== false, onTheme)
110
+ const hosting = useMemo(() => (bridge ? { client: hosted.client } : null), [bridge, hosted.client])
111
+ // Appearance: the rows the app asked for, less the theme's when a host chooses it. A hosted app's
112
+ // host always does, so it offers no scheme, look or theme of its own.
113
+ const fromHost = useThemeFromHost(declared?.themeFromHost ?? (bridge ? true : undefined))
114
+ const rows = shownRows(rowsOf(declared?.settings?.appearance), fromHost)
115
+ const app = useMemo(() => withAppearance(declared, rows, appLook), [declared, rows.scheme, rows.defaultScheme, rows.look, rows.sounds, rows.theme, rows.soundsDescription, appLook])
116
+ const chosenLook = useLookChoice()
117
+ const defaultScheme = useDefaultColorScheme(Boolean(rows.scheme), rows.defaultScheme)
118
+ const scheme = useChosenColorScheme() ?? defaultScheme
119
+ const look = (rows.look && chosenLook) || appLook
120
+ // A hosted app wears the look of the theme it was sent, once there is one.
121
+ const resolved = hosted.theme?.look ?? resolveLook(look)
122
+ useLayoutEffect(() => {
123
+ if (!rows.scheme) return
124
+ showColorScheme(scheme)
125
+ // Hiding because a host's theme arrived: the attribute is the host's now, so it stays.
126
+ return () => {
127
+ if (!hostThemed()) showColorScheme('system')
128
+ }
129
+ }, [rows.scheme, scheme])
47
130
  // On the root, not the desk: menus and popovers are portalled out of it, and are chrome too.
48
131
  useLayoutEffect(() => {
132
+ watchThemeChanges()
49
133
  document.documentElement.dataset.look = resolved
50
134
  return () => {
51
135
  delete document.documentElement.dataset.look
52
136
  }
53
137
  }, [resolved])
138
+ // "Settings…" and ⌘, perform this; it answers at the top of the chain, so a window can still take it.
139
+ const latestApp = useRef(app ?? null)
140
+ latestApp.current = app ?? null
141
+ // Outside React, openSettings(desk) and syncWithLocation read the declaration from the desk. Said as the
142
+ // provider renders, so a link read in the app's first effect already knows Settings and About, and again
143
+ // in an effect, which a remount in development runs after the cleanup that forgot it.
144
+ const [declaration] = useState(() => ({ app: () => latestApp.current, draws: (id: WindowId) => desksWindow(latestApp.current, id) !== null }))
145
+ declareOn(value, declaration.app)
146
+ deskDrawsWindows(value, declaration.draws)
147
+ useLayoutEffect(() => {
148
+ const forgetApp = declareOn(value, declaration.app)
149
+ const forgetWindows = deskDrawsWindows(value, declaration.draws)
150
+ return () => {
151
+ forgetApp()
152
+ forgetWindows()
153
+ }
154
+ }, [value, declaration])
155
+ const hasApp = app !== undefined
156
+ useEffect(() => {
157
+ if (!hasApp) return
158
+ const remove = addCommandHandler(document, SettingsCommand, (route?: unknown) => openAppSettings(value, latestApp.current, typeof route === 'string' ? route : undefined), {
159
+ enabled: () => hasSettings(latestApp.current),
160
+ })
161
+ // ⌘, here, not only in the menu bar's keymap, so it works in an app that has none. Not when hosted:
162
+ // the host's frame owns it, menu bar or not, connected yet or not.
163
+ const unbind = bridge ? () => {} : bindDeskShortcuts(value)
164
+ return () => {
165
+ remove()
166
+ unbind()
167
+ }
168
+ }, [value, hasApp, bridge])
54
169
  return (
55
170
  <DeskContext.Provider value={value}>
56
171
  <BusContext.Provider value={bus}>
57
172
  <LookContext.Provider value={resolved}>
58
- <DragProvider>{children}</DragProvider>
173
+ <AppProvider value={app ?? null}>
174
+ <HostContext.Provider value={hosting}>
175
+ {bridge && <HostedApp />}
176
+ <DragProvider>{children}</DragProvider>
177
+ </HostContext.Provider>
178
+ </AppProvider>
59
179
  </LookContext.Provider>
60
180
  </BusContext.Provider>
61
181
  </DeskContext.Provider>
62
182
  )
63
183
  }
64
184
 
185
+ /** The app's declaration with Desk's Appearance as its first section, when it has rows to show. */
186
+ function withAppearance(app: DeskApp | undefined, rows: AppearanceRows, look: LookChoice): DeskApp | undefined {
187
+ if (!app?.settings || !hasRows(rows)) return app
188
+ const appearance: SettingsSection = { id: 'appearance', title: 'Appearance', content: <AppearanceSettings rows={rows} look={look} /> }
189
+ return { ...app, settings: { ...app.settings, sections: [appearance, ...(app.settings.sections ?? [])] } }
190
+ }
191
+
65
192
  /** The look the desk is wearing, for an app that draws chrome of its own. Outside a desk, the device's. */
66
193
  export const useLook = (): Look => useContext(LookContext) ?? deviceLook()
67
194
 
@@ -0,0 +1,81 @@
1
+ import { useEffect, useRef, useState } from 'react'
2
+ import type { RefObject } from 'react'
3
+ import { formatShortcut } from '../core/shortcuts.js'
4
+
5
+ /** How long "Copied" stays before the button says Copy again. */
6
+ const COPIED_MS = 2000
7
+
8
+ /** Where a copy has got to: nothing yet, done, or the text selected for the person to copy. */
9
+ export type Copying = 'idle' | 'copied' | 'select'
10
+
11
+ /** What `useCopy` gives a view: where the copy has got to, the action, and what to say about it. */
12
+ export interface CopyState {
13
+ readonly copying: Copying
14
+ readonly copy: () => Promise<void>
15
+ /** What the button says: Copy, Copied, or which keys copy the selection. */
16
+ readonly button: string
17
+ /** What a polite status region says, so the result is heard as well as seen. */
18
+ readonly status: string
19
+ }
20
+
21
+ /**
22
+ * Copying a block of text with a button, as `CodeBlock` and the About panel do.
23
+ *
24
+ * THE CLIPBOARD IS NOT ALWAYS THERE: a page over plain http has none, and a browser may refuse it.
25
+ * Then `select`'s element is selected and the older copy command tried; where that fails too, the
26
+ * selection stays and the button says which keys copy it, so the person is one keystroke away.
27
+ * New text has been neither copied nor selected, so the state goes back to idle when `text` changes.
28
+ */
29
+ export function useCopy(text: string, select: RefObject<HTMLElement | null>): CopyState {
30
+ const [copying, setCopying] = useState<Copying>('idle')
31
+ const timer = useRef<ReturnType<typeof setTimeout>>(undefined)
32
+ const alive = useRef(true)
33
+ const copied = useRef(text)
34
+
35
+ useEffect(() => {
36
+ alive.current = true
37
+ return () => {
38
+ alive.current = false
39
+ clearTimeout(timer.current)
40
+ }
41
+ }, [])
42
+ // Put back while rendering, so not one frame says Copied over text nobody copied.
43
+ if (copied.current !== text) {
44
+ copied.current = text
45
+ clearTimeout(timer.current)
46
+ if (copying !== 'idle') setCopying('idle')
47
+ }
48
+
49
+ const settle = (next: Copying) => {
50
+ // A copy that finishes after the view has gone has nothing left to say it in.
51
+ if (!alive.current) return
52
+ clearTimeout(timer.current)
53
+ setCopying(next)
54
+ if (next === 'copied') timer.current = setTimeout(() => setCopying('idle'), COPIED_MS)
55
+ }
56
+
57
+ const copy = async () => {
58
+ try {
59
+ await navigator.clipboard.writeText(text)
60
+ settle('copied')
61
+ } catch {
62
+ const selection = window.getSelection()
63
+ if (select.current && selection) selection.selectAllChildren(select.current)
64
+ let done = false
65
+ try {
66
+ done = document.execCommand('copy')
67
+ } catch {
68
+ // Unsupported: the selection is the way.
69
+ }
70
+ settle(done ? 'copied' : 'select')
71
+ }
72
+ }
73
+
74
+ const keys = formatShortcut('mod+c')
75
+ return {
76
+ copying,
77
+ copy,
78
+ button: copying === 'copied' ? 'Copied' : copying === 'select' ? `Press ${keys}` : 'Copy',
79
+ status: copying === 'copied' ? 'Copied' : copying === 'select' ? `Selected. Press ${keys} to copy.` : '',
80
+ }
81
+ }
@@ -0,0 +1,42 @@
1
+ import { focusedId, instancesOf, isMinimized, windowType } from '../core/desk.js'
2
+ import type { DeskState, WindowId } from '../core/types.js'
3
+
4
+ /** What a dock item needs to say whether its windows are open: the item's window kind, and its id. */
5
+ export interface DockTarget {
6
+ readonly id: string
7
+ readonly window?: WindowId
8
+ /** Whether its windows are open, said by someone else: an app a host runs reports its own. */
9
+ readonly status?: DockStatus
10
+ }
11
+
12
+ /** Whether a dock item's windows are open, have focus, or are all minimized. */
13
+ export interface DockStatus {
14
+ readonly running: boolean
15
+ readonly focused: boolean
16
+ readonly minimized: boolean
17
+ }
18
+
19
+ /** The window a dock item opens and reports as running: its `window`, or its id. */
20
+ export const windowOf = (item: DockTarget): WindowId => item.window ?? item.id
21
+
22
+ /** Whether any of these items' windows are open, have focus, or are all minimized. */
23
+ export const statusOf = (state: DeskState, items: readonly DockTarget[]): DockStatus => {
24
+ const told = items.flatMap(i => (i.status ? [i.status] : []))
25
+ // Items whose status is reported (a hosted app's, drawn by its host) say it themselves.
26
+ if (told.length) {
27
+ return {
28
+ running: told.some(s => s.running),
29
+ focused: told.some(s => s.focused),
30
+ minimized: told.some(s => s.running) && told.every(s => !s.running || s.minimized),
31
+ }
32
+ }
33
+ const key = focusedId(state)
34
+ const windows = items.flatMap(i => instancesOf(state, windowOf(i)))
35
+ return {
36
+ // Any window of this kind counts: a second query window is still Query, running.
37
+ running: windows.length > 0,
38
+ focused: items.some(i => key !== null && windowType(key) === windowOf(i)),
39
+ // Open, and none of it on the desk. One window of a kind still showing means the item is not.
40
+ minimized: windows.length > 0 && windows.every(w => isMinimized(state, w.id)),
41
+ }
42
+ }
@@ -1,6 +1,7 @@
1
1
  import { useEffect, useState } from 'react'
2
2
  import { focusedId } from '../core/desk.js'
3
3
  import type { WindowId } from '../core/types.js'
4
+ import { appWindowTitle, useDeskApp } from './app.js'
4
5
  import { useDeskState } from './context.js'
5
6
 
6
7
  /*
@@ -32,13 +33,15 @@ export function useDocumentTitle(title: (id: WindowId) => string, options: Docum
32
33
  const { app, separator = DEFAULT_SEPARATOR } = options
33
34
  const state = useDeskState()
34
35
  const id = focusedId(state)
36
+ // Desk names its own About and Settings, so the app's title function need not.
37
+ const declared = useDeskApp()
35
38
 
36
39
  // What the page was called before the desk took it over: the app's name by default, and what
37
40
  // is put back on the way out. Read once, so a title this hook set is never mistaken for it.
38
41
  const [original] = useState(() => (typeof document === 'undefined' ? '' : document.title))
39
42
  const base = app ?? original
40
43
 
41
- const front = id === null ? '' : title(id)
44
+ const front = id === null ? '' : (appWindowTitle(declared, id) ?? title(id))
42
45
  const next = front && base ? `${front}${separator}${base}` : front || base
43
46
 
44
47
  useEffect(() => {
@@ -0,0 +1,100 @@
1
+ import { useEffect } from 'react'
2
+ import { addCommandHandler } from '../core/commands.js'
3
+ import type { CommandId } from '../core/commands.js'
4
+ import { focusedId } from '../core/desk.js'
5
+ import { warnOnce } from '../core/location.js'
6
+ import { parseShortcut } from '../core/shortcuts.js'
7
+ import type { Desk } from '../core/desk.js'
8
+ import type { HostMenuItem } from '../core/hosted.js'
9
+ import { checkChrome } from '../host/checkChrome.js'
10
+ import type { WindowId } from '../core/types.js'
11
+ import type { Menu, MenuItem } from './MenuBar.js'
12
+ import { addWindowMenus } from './windowMenus.js'
13
+
14
+ /*
15
+ * MENUS FROM AN EMBEDDED APP.
16
+ *
17
+ * An app in an `AppFrame` sends its menus as hosted mode's apps do, as data in a `chrome` message, and
18
+ * hears what was chosen as a `command`. It is somebody else's page, so its menus get the check a host
19
+ * gives any app's chrome (`checkChrome`): a bad menu or item is dropped and the rest kept. The frame then
20
+ * turns them into the page's own menus, for the window it is in, so they join the bar while that window is
21
+ * in front. Each item becomes a command of its own, named for this frame, so a shortcut works through the
22
+ * bar like any other and a choice goes back only to the frame that sent it.
23
+ */
24
+
25
+ /** The most menus one frame adds to the bar: room for a menu or two, not a bar of its own. */
26
+ const MAX_MENUS = 4
27
+
28
+ /** One item the frame turned into a command, and the app's own id for it. */
29
+ export interface FrameItem {
30
+ readonly command: CommandId
31
+ readonly id: string
32
+ readonly enabled: boolean
33
+ }
34
+
35
+ /**
36
+ * Whether an embedded app may bind this shortcut: only with ⌘/Ctrl or ⌥ held, or a function key. A bare
37
+ * letter, Enter, Escape or an arrow belongs to whatever has focus, and a page in a frame must not take it
38
+ * from the rest of the desk. Read as `matchesShortcut` reads it, so a modifier it ignores (`meta+k`) does not count.
39
+ */
40
+ const framesMayBind = (shortcut: string) => {
41
+ const s = parseShortcut(shortcut)
42
+ return s.mod || s.ctrl || s.alt || /^f([1-9]|1\d|2[0-4])$/.test(s.key)
43
+ }
44
+
45
+ const converted = (row: HostMenuItem, command: CommandId, menu: string): MenuItem[] => {
46
+ if (row.type === 'separator') return [{ type: 'separator' }]
47
+ if (row.label === undefined) return []
48
+ if (row.type === 'header') return [{ type: 'header', label: row.label }]
49
+ let { shortcut } = row
50
+ if (shortcut !== undefined && !framesMayBind(shortcut)) {
51
+ warnOnce(`desk: "${row.label}" in the ${menu} menu asks for ${shortcut}, which has no modifier. An embedded app's shortcut needs one, so it keeps its label without the key.`)
52
+ shortcut = undefined
53
+ }
54
+ return [{ type: 'command', label: row.label, command, ...(row.detail === undefined ? {} : { detail: row.detail }), ...(shortcut === undefined ? {} : { shortcut }), ...(row.checked === undefined ? {} : { checked: row.checked }) }]
55
+ }
56
+
57
+ /**
58
+ * An app's menus as the page's own, after `checkChrome`, its commands named `prefix`:menu index:row index, and what
59
+ * each stands for. Items without an id or label are left out, and menus past the first few. Null when
60
+ * there is nothing.
61
+ */
62
+ export function frameMenus(raw: unknown, prefix: string): { readonly menus: Menu[]; readonly items: FrameItem[] } | null {
63
+ const checked = checkChrome({ menus: raw, dock: [], settings: [], about: null }, location.origin)
64
+ if (!checked.ok) return null
65
+ const items: FrameItem[] = []
66
+ // Named by place, not by the app's ids, which may repeat or run into each other ("a:b" + "c", "a" + "b:c").
67
+ const menus = checked.chrome.menus.slice(0, MAX_MENUS).map((menu, m) => ({
68
+ id: `${prefix}/${m}`,
69
+ label: menu.label,
70
+ items: menu.items.flatMap((row, r) => {
71
+ if (row.type === 'item' && row.id === undefined) return []
72
+ const command = `${prefix}:${m}:${r}`
73
+ const made = converted(row, command, menu.label)
74
+ if (row.type === 'item' && made.length) items.push({ command, id: row.id as string, enabled: row.enabled !== false })
75
+ return made
76
+ }),
77
+ }))
78
+ return menus.length ? { menus, items } : null
79
+ }
80
+
81
+ /**
82
+ * While the frame has menus and its window is in front, they join the bar, and choosing an item, or
83
+ * pressing its shortcut, calls `choose` with the app's own id for it.
84
+ */
85
+ export function useFrameMenus(
86
+ desk: Desk,
87
+ window: WindowId | null,
88
+ menus: { readonly menus: Menu[]; readonly items: FrameItem[] } | null,
89
+ choose: (id: string) => void,
90
+ ): void {
91
+ useEffect(() => {
92
+ if (!menus || window === null) return undefined
93
+ const front = () => focusedId(desk.getState()) === window
94
+ const stops = menus.items.map(item => addCommandHandler(document, item.command, () => choose(item.id), { enabled: () => item.enabled && front() }))
95
+ stops.push(addWindowMenus(desk, window, menus.menus))
96
+ return () => stops.forEach(stop => stop())
97
+ // choose is read fresh by the caller through a ref; the menus are what change.
98
+ // eslint-disable-next-line react-hooks/exhaustive-deps
99
+ }, [desk, window, menus])
100
+ }
@@ -0,0 +1,89 @@
1
+ import { useEffect, useRef } from 'react'
2
+ import type { ReactNode } from 'react'
3
+ import type { HostAbout, HostAboutSection, HostSettingsSection } from '../core/hosted.js'
4
+ import { hasSettings, useDeskApp } from './app.js'
5
+ import type { AboutSection, AppLink, DeskApp } from './app.js'
6
+ import { useOpenSettings } from './appPanels.js'
7
+ import { badgeText } from './hostDock.js'
8
+ import { useHost } from './hosted.js'
9
+
10
+ /*
11
+ * SETTINGS AND ABOUT, WHEN A HOST HAS THEM.
12
+ *
13
+ * A host that runs several apps has one Settings and one About in its own frame, so a hosted app draws
14
+ * no gear and no About item. It sends what it declared instead: its Settings sections by name (their
15
+ * controls are React and stay in the app), and its name, version, links and facts as plain data. When
16
+ * the person picks a section in the host's Settings, the app opens its own Settings there.
17
+ */
18
+
19
+ /** An app without facts has nothing to report. */
20
+ const NO_FACTS = (): readonly AboutSection[] | null => []
21
+
22
+ const linkOf = ({ label, url }: AppLink) => ({ label, url })
23
+
24
+ /** A note as text, when it is text; a note drawn with React cannot cross, and is left out. */
25
+ const noteText = (note: ReactNode): string | undefined => (typeof note === 'string' || typeof note === 'number' ? String(note) : undefined)
26
+
27
+ /**
28
+ * An app's About as data: its note and each fact's link, `copy: false` kept, and facts `[]` for nothing
29
+ * to report as against null while they are read. A fact's own note is React, and stays behind.
30
+ */
31
+ export function hostAbout(app: DeskApp, facts: readonly AboutSection[] | null): HostAbout {
32
+ const sections = facts?.map(
33
+ (section): HostAboutSection => ({
34
+ ...(section.title === undefined ? {} : { title: section.title }),
35
+ facts: section.facts.map(({ name, value, link, copy }) => ({
36
+ name,
37
+ value,
38
+ ...(link === undefined ? {} : { link: linkOf(link) }),
39
+ ...(copy === false ? { copy } : {}),
40
+ })),
41
+ }),
42
+ )
43
+ const note = noteText(app.note)
44
+ return {
45
+ name: app.name,
46
+ ...(app.version === undefined ? {} : { version: app.version }),
47
+ links: (app.links ?? []).map(linkOf),
48
+ ...(note === undefined ? {} : { note }),
49
+ facts: sections ?? null,
50
+ }
51
+ }
52
+
53
+ /**
54
+ * An app's Settings sections as data, in order; none for an app without settings. An app whose Settings
55
+ * is a window of its own, with no sections declared, is listed as one entry with the id `''`.
56
+ */
57
+ export function hostSettings(app: DeskApp | null): HostSettingsSection[] {
58
+ if (!app || !hasSettings(app)) return []
59
+ const sections = app.settings?.sections ?? []
60
+ if (sections.length === 0) return [{ id: '', title: 'Settings' }]
61
+ return sections.map(({ id, title, group, badge }) => {
62
+ const text = badgeText(badge)
63
+ return { id, title, ...(group === undefined ? {} : { group }), ...(text === undefined ? {} : { badge: text }) }
64
+ })
65
+ }
66
+
67
+ /** Sends the host the app's Settings sections and About, and opens Settings where the host says. Draws nothing. */
68
+ export function HostedApp() {
69
+ const host = useHost()
70
+ const app = useDeskApp()
71
+ const openSettings = useOpenSettings()
72
+ // The facts are a hook, read here as the About panel reads them, so they can change.
73
+ const facts = (app?.facts ?? NO_FACTS)()
74
+ const settings = hostSettings(app)
75
+ const about = app ? hostAbout(app, facts) : null
76
+ const key = JSON.stringify([settings, about])
77
+ const open = useRef(openSettings)
78
+ open.current = openSettings
79
+
80
+ useEffect(() => {
81
+ host?.setChrome({ settings, about })
82
+ // key stands for both, which are rebuilt on every render.
83
+ // eslint-disable-next-line react-hooks/exhaustive-deps
84
+ }, [host, key])
85
+
86
+ useEffect(() => host?.onCommand('settings', id => open.current(id === '' ? undefined : id)), [host])
87
+
88
+ return null
89
+ }
@@ -0,0 +1,118 @@
1
+ import { useEffect, useRef } from 'react'
2
+ import type { ReactNode } from 'react'
3
+ import type { HostDockEntry, HostDockItem } from '../core/hosted.js'
4
+ import type { DeskState } from '../core/types.js'
5
+ import type { Desk } from '../core/desk.js'
6
+ import { markerOf } from './app.js'
7
+ import { useDesk, useDeskState } from './context.js'
8
+ import type { DockEntry, DockItem } from './Dock.js'
9
+ import { statusOf, windowOf } from './dockStatus.js'
10
+ import { useHost } from './hosted.js'
11
+
12
+ /*
13
+ * A DOCK SOMEBODY ELSE DRAWS.
14
+ *
15
+ * In hosted mode one dock serves several apps, drawn by the host. Each app's dock sends its entries
16
+ * as data — label, image, badge, and whether the item's windows are open — and the host sends back
17
+ * the id of the item chosen, which runs here as a click on the drawn dock would.
18
+ */
19
+
20
+ /** The dock as plain data, and the item each id opens. */
21
+ export interface HostDockModel {
22
+ readonly model: readonly HostDockEntry[]
23
+ readonly run: ReadonlyMap<string, DockItem>
24
+ }
25
+
26
+ /** A badge as text: numbers and strings as they are, anything else drawn as a dot. */
27
+ export const badgeText = (badge: ReactNode): string | undefined =>
28
+ badge == null || typeof badge === 'boolean' ? undefined : typeof badge === 'string' || typeof badge === 'number' ? String(badge) : ''
29
+
30
+ function itemModel(item: DockItem, state: DeskState): HostDockItem {
31
+ const badge = badgeText(item.badge)
32
+ return {
33
+ type: 'item',
34
+ id: item.id,
35
+ label: item.label,
36
+ ...(item.description === undefined ? {} : { description: item.description }),
37
+ ...(item.image === undefined ? {} : { image: item.image }),
38
+ ...(badge === undefined ? {} : { badge }),
39
+ ...(item.disabled ? { disabled: true } : {}),
40
+ ...statusOf(state, [item]),
41
+ }
42
+ }
43
+
44
+ /** Turns dock entries into what a host can draw, as the desk stands now. */
45
+ export function hostDockModel(entries: readonly DockEntry[], state: DeskState): HostDockModel {
46
+ const run = new Map<string, DockItem>()
47
+ const model = entries.flatMap((entry): HostDockEntry[] => {
48
+ switch (entry.type) {
49
+ case 'separator':
50
+ return [{ type: 'separator', id: entry.id }]
51
+ case 'settings':
52
+ case 'about':
53
+ // The host's frame has the one Settings and About: the app's places for them are not drawn.
54
+ return []
55
+ case 'item':
56
+ run.set(entry.id, entry)
57
+ return [itemModel(entry, state)]
58
+ case 'stack': {
59
+ // Settings and About placed in a stack are the host's too: dropped, and the stack with them if it empties.
60
+ const items = entry.items.filter(item => !markerOf(item))
61
+ for (const item of items) run.set(item.id, item)
62
+ if (!items.length) return []
63
+ return [
64
+ {
65
+ type: 'stack',
66
+ id: entry.id,
67
+ label: entry.label,
68
+ ...(entry.image === undefined ? {} : { image: entry.image }),
69
+ items: items.map(item => itemModel(item, state)),
70
+ },
71
+ ]
72
+ }
73
+ }
74
+ })
75
+ return { model, run }
76
+ }
77
+
78
+ /** Opens a dock item's window, or runs its own `onSelect`, as clicking it on a drawn dock does. */
79
+ export function selectDockItem(desk: Desk, item: DockItem): void {
80
+ if (item.disabled) return
81
+ if (item.onSelect) item.onSelect()
82
+ else desk.open(windowOf(item))
83
+ }
84
+
85
+ /**
86
+ * The dock in hosted mode. Draws nothing. Sends the host the entries whenever they or the desk
87
+ * change, and runs the item the host says was chosen. A right-click menu, pinning and dragging
88
+ * stay with the drawn dock: a host offers its own.
89
+ */
90
+ export function HostedDock({ entries }: { readonly entries: readonly DockEntry[] }) {
91
+ const host = useHost()
92
+ const desk = useDesk()
93
+ const state = useDeskState()
94
+ const { model, run } = hostDockModel(entries, state)
95
+ const latest = useRef(run)
96
+ latest.current = run
97
+ const key = JSON.stringify(model)
98
+
99
+ useEffect(() => {
100
+ host?.setChrome({ dock: model })
101
+ // key stands for the model's contents, which is rebuilt on every render.
102
+ // eslint-disable-next-line react-hooks/exhaustive-deps
103
+ }, [host, key])
104
+
105
+ useEffect(() => {
106
+ if (!host) return undefined
107
+ const stop = host.onCommand('dock', id => {
108
+ const item = latest.current.get(id)
109
+ if (item) selectDockItem(desk, item)
110
+ })
111
+ return () => {
112
+ stop()
113
+ host.setChrome({ dock: [] })
114
+ }
115
+ }, [host, desk])
116
+
117
+ return null
118
+ }