@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
@@ -0,0 +1,210 @@
1
+ import { useCallback, useEffect, useRef } from 'react'
2
+ import type { ReactNode } from 'react'
3
+ import { openAppSettings, useDeskApp } from './app.js'
4
+ import type { AboutSection, DeskApp, SettingsSection } from './app.js'
5
+ import { useOptionalDesk, useWindowRoute } from './context.js'
6
+ import { Button } from './controls.js'
7
+ import { useCopy } from './copy.js'
8
+ import { Pane, Section } from './pane.js'
9
+ import { Loading } from './progress.js'
10
+ import { Sidebar } from './sidebar.js'
11
+ import type { SidebarSection } from './sidebar.js'
12
+ import { Split } from './split.js'
13
+
14
+ /*
15
+ * The windows behind About and Settings, drawn from what the app declared. Both read the declaration from
16
+ * the desk, so a section added at runtime, or a fact that changes, shows without the window being rebuilt.
17
+ */
18
+
19
+ /**
20
+ * The app's name and version, then every fact that may be copied, by section, each as its `copyValue` if it
21
+ * has one: what About's Copy copies. A section still pending is left out.
22
+ */
23
+ export function aboutText(app: Pick<DeskApp, 'name' | 'version'>, sections: readonly AboutSection[]): string {
24
+ const blocks = [app.version ? `${app.name} ${app.version}` : app.name]
25
+ for (const section of sections) {
26
+ if (section.pending) continue
27
+ const lines = section.facts.filter(fact => fact.copy !== false).map(({ name, value, copyValue }) => `${name}: ${copyValue ?? value}`)
28
+ if (lines.length) blocks.push([...(section.title ? [section.title] : []), ...lines].join('\n'))
29
+ }
30
+ return blocks.join('\n\n')
31
+ }
32
+
33
+ /**
34
+ * Opens Settings as the gear, the menu item and ⌘, do, on the section `route` names: Desk's panel, or the
35
+ * app's own window when it has one. Nothing, when the app has no Settings to show. Read inside the desk,
36
+ * because whether there are settings depends on what the desk knows: Appearance hides when a host themes it.
37
+ */
38
+ export function useOpenSettings(): (route?: string) => void {
39
+ const app = useDeskApp()
40
+ const desk = useOptionalDesk()
41
+ return useCallback((route?: string) => {
42
+ if (desk) openAppSettings(desk, app, route)
43
+ }, [desk, app])
44
+ }
45
+
46
+ const NO_FACTS = () => undefined
47
+
48
+ /** About the app: its icon, name, version and links, what is running if it says, and Copy for a bug report. */
49
+ export function AboutContent() {
50
+ const app = useDeskApp()
51
+ // A declaration that gains or loses its facts calls one hook more or fewer: drawn afresh, not misordered.
52
+ return app ? <AboutPanel key={app.facts ? 'facts' : 'plain'} app={app} /> : null
53
+ }
54
+
55
+ function AboutPanel({ app }: { readonly app: DeskApp }) {
56
+ // The app's hook, called on every render of this panel and only here, so its own hooks keep their order.
57
+ const sections = (app.facts ?? NO_FACTS)()
58
+ // Where there is no clipboard, the text is selected: this text, held out of sight, not the panel, which
59
+ // shows facts marked not to be copied.
60
+ const text = sections ? aboutText(app, sections) : ''
61
+ const textRef = useRef<HTMLPreElement>(null)
62
+ const copying = useCopy(text, textRef)
63
+ const hasFacts = Boolean(app.facts)
64
+ // Copy is for something to copy: not while facts are read, and not when there are none to report.
65
+ const copyable = Boolean(sections?.some(section => !section.pending && section.facts.some(fact => fact.copy !== false)))
66
+ return (
67
+ <Pane
68
+ padded
69
+ label={`About ${app.name}`}
70
+ className="desk-about"
71
+ footer={
72
+ copyable ? (
73
+ <div className="desk-about-foot">
74
+ <pre ref={textRef} className="desk-about-copied" aria-hidden="true">{text}</pre>
75
+ <Button onClick={() => void copying.copy()}>{copying.button}</Button>
76
+ <span className="desk-text-small desk-text-muted" role="status">{copying.copying === 'copied' ? 'Copied, for a bug report' : copying.status}</span>
77
+ </div>
78
+ ) : null
79
+ }
80
+ >
81
+ <div className="desk-about-body">
82
+ <div className="desk-about-brand">
83
+ {app.icon != null && <div className="desk-about-icon" aria-hidden="true">{app.icon}</div>}
84
+ {app.wordmark != null && <div className="desk-about-wordmark" aria-hidden="true">{app.wordmark}</div>}
85
+ <h2 className={app.wordmark != null ? 'desk-about-name desk-about-name-hidden' : 'desk-about-name'}>{app.name}</h2>
86
+ {app.version && <p className="desk-about-version desk-text-muted">Version {app.version}</p>}
87
+ {app.description != null && <p className="desk-about-description">{app.description}</p>}
88
+ </div>
89
+ {byGroup(app.links ?? [], link => link.group).map(({ group, items }) => (
90
+ <nav key={group} className="desk-about-links" aria-label={group || 'Links'}>
91
+ {group && <h3 className="desk-about-links-heading desk-text-small desk-text-muted">{group}</h3>}
92
+ {items.map(link => (
93
+ <a key={link.url} href={link.url} target="_blank" rel="noopener noreferrer">{link.label}</a>
94
+ ))}
95
+ </nav>
96
+ ))}
97
+ {app.note != null && <p className="desk-about-note desk-text-small desk-text-muted">{app.note}</p>}
98
+ {hasFacts && !sections && <Loading label="Reading what is running" />}
99
+ {sections?.length ? (
100
+ // One grid for every section, so the names make one column down the panel, as wide as the longest.
101
+ <div className="desk-about-sections">
102
+ {sections.map((section, n) => (
103
+ <AboutFacts key={section.title ?? `facts-${n}`} section={section} />
104
+ ))}
105
+ </div>
106
+ ) : null}
107
+ </div>
108
+ {app.legal != null && <p className="desk-about-legal desk-text-small desk-text-muted">{app.legal}</p>}
109
+ </Pane>
110
+ )
111
+ }
112
+
113
+ /** Items by their group, in the order each group first appears; those with none (or '') first, as group ''. */
114
+ function byGroup<T>(items: readonly T[], groupOf: (item: T) => string | undefined): { group: string; items: T[] }[] {
115
+ const groups: { group: string; items: T[] }[] = []
116
+ for (const item of items) {
117
+ const group = groupOf(item) ?? ''
118
+ let found = groups.find(g => g.group === group)
119
+ if (!found) {
120
+ found = { group, items: [] }
121
+ if (group) groups.push(found)
122
+ else groups.unshift(found)
123
+ }
124
+ found.items.push(item)
125
+ }
126
+ return groups
127
+ }
128
+
129
+ function AboutFacts({ section }: { readonly section: AboutSection }) {
130
+ const list = section.pending ? (
131
+ <Loading label="Checking" size="small" />
132
+ ) : (
133
+ <dl className="desk-about-facts">
134
+ {section.facts.map(({ name, value, note, link }) => (
135
+ <div key={name}>
136
+ <dt>{name}</dt>
137
+ <dd>
138
+ {value}
139
+ {(note != null || link) && (
140
+ <span className="desk-about-fact-note desk-text-small desk-text-muted">
141
+ {note}
142
+ {note != null && link && ' '}
143
+ {link && <a href={link.url} target="_blank" rel="noopener noreferrer">{link.label}</a>}
144
+ </span>
145
+ )}
146
+ </dd>
147
+ </div>
148
+ ))}
149
+ </dl>
150
+ )
151
+ return section.title ? <Section title={section.title}>{list}</Section> : <section aria-label="Facts">{list}</section>
152
+ }
153
+
154
+ /** The sidebar's groups, in the order each first appears; sections with no group come first, unheaded. */
155
+ export function settingsGroups(sections: readonly SettingsSection[]): SidebarSection[] {
156
+ return byGroup(sections, section => section.group).map(({ group, items }) => ({
157
+ id: group || 'settings',
158
+ ...(group ? { label: group } : {}),
159
+ items: items.map(s => ({ id: s.id, label: s.title, ...(s.icon != null ? { icon: s.icon } : {}), ...(s.badge != null ? { badge: s.badge } : {}) })),
160
+ }))
161
+ }
162
+
163
+ /**
164
+ * The app's settings sections, Appearance first when it has one, laid out as it declared: on one page, or a sidebar beside the chosen one.
165
+ * Desk's Settings window is this; an app whose Settings window is its own can draw it there. The window's
166
+ * route is the section: a link to `settings` with a route opens on it, and choosing one in the sidebar moves
167
+ * the route, so the address says where Settings is.
168
+ */
169
+ export function SettingsContent() {
170
+ const app = useDeskApp()
171
+ const sections = app?.settings?.sections ?? []
172
+ const [route, setRoute] = useWindowRoute()
173
+ const page = useRef<HTMLDivElement>(null)
174
+ const sidebar = app?.settings?.layout === 'sidebar'
175
+
176
+ // On one page, a link to a section scrolls it into view.
177
+ useEffect(() => {
178
+ if (sidebar || !route) return
179
+ page.current?.querySelector(`[data-settings-section="${CSS.escape(route)}"]`)?.scrollIntoView?.({ block: 'start' })
180
+ }, [route, sidebar])
181
+
182
+ if (!sections.length) return null
183
+ if (!sidebar) {
184
+ return (
185
+ <Pane padded label="Settings" className="desk-settings">
186
+ <div ref={page} className="desk-settings-page">
187
+ {sections.map(section => (
188
+ <div key={section.id} data-settings-section={section.id}>
189
+ <Section title={section.title}>{section.content}</Section>
190
+ </div>
191
+ ))}
192
+ </div>
193
+ </Pane>
194
+ )
195
+ }
196
+ // With no route, or one to a section that has gone, the first section as listed, not as declared.
197
+ const groups = settingsGroups(sections)
198
+ const chosen = sections.find(s => s.id === route) ?? sections.find(s => s.id === groups[0]?.items[0]?.id) ?? sections[0]!
199
+ return (
200
+ <Split side={<Sidebar label="Settings" sections={groups} value={chosen.id} onChange={setRoute} />} size={220} min={180} label="Resize the list of settings">
201
+ <Pane padded label={chosen.title} className="desk-settings">
202
+ <SettingsSectionView section={chosen} />
203
+ </Pane>
204
+ </Split>
205
+ )
206
+ }
207
+
208
+ function SettingsSectionView({ section }: { readonly section: SettingsSection }): ReactNode {
209
+ return <Section title={section.title}>{section.content}</Section>
210
+ }
@@ -0,0 +1,195 @@
1
+ import { useLayoutEffect, useSyncExternalStore } from 'react'
2
+ import type { ReactNode } from 'react'
3
+ import type { LookChoice } from '../core/look.js'
4
+ import { getPreference, preferenceChanged, setPreference, subscribePreferences } from '../core/preferences.js'
5
+ import { COLOR_SCHEME_KEY, LOOK_KEY } from '../core/scheme.js'
6
+ import type { ColorScheme } from '../core/scheme.js'
7
+ import type { Fx } from '../fx/fx.js'
8
+ import { RadioGroup, SegmentedControl } from './controls.js'
9
+ import { SoundsToggle } from './soundsToggle.js'
10
+
11
+ /*
12
+ * APPEARANCE: the rows of Settings that change how the whole app looks and sounds, made once here so every
13
+ * app has them the same way. The app chooses which rows: the colour scheme (light, dark or the system's),
14
+ * the look (the window chrome: the device's, Mac, GNOME or Windows), its sounds, and a slot for a theme
15
+ * picker of its own. An app whose named themes decide light or dark leaves the scheme out.
16
+ *
17
+ * The scheme and the look are remembered in this browser, under `desk.scheme` and `desk.look`, as a widget
18
+ * kept in front is. The scheme is `data-theme` on the root, which the stylesheet already reads; the look is
19
+ * the desk's `look`, chosen here in place of the one the app passes.
20
+ *
21
+ * AN APP THAT RECEIVES ITS THEME FROM A HOST does not choose it: the host does, and sends it (an app in an
22
+ * `AppFrame` with a `theme`, through the bridge). Then the scheme, the look and the app's theme slot are not
23
+ * offered and not applied, since the app follows the host's; sounds stay, being the app's own. The app that
24
+ * is the theme's source keeps them all.
25
+ */
26
+
27
+ /** Which rows Appearance has. `true` is the colour scheme alone. */
28
+ export interface AppearanceRows {
29
+ /** Light, dark or the system's. Leave out for an app whose named themes decide it. */
30
+ readonly scheme?: boolean
31
+ /** The scheme while the person has chosen none: `system` unless the app says. Give the head script the same. */
32
+ readonly defaultScheme?: ColorScheme
33
+ /** The window chrome: the device's, Mac, GNOME or Windows. */
34
+ readonly look?: boolean
35
+ /** The app's sounds: one switch, `SoundsToggle`. */
36
+ readonly sounds?: Fx<string>
37
+ /** What `sounds` silences, said under the switch. */
38
+ readonly soundsDescription?: ReactNode
39
+ /** The app's own theme picker, drawn after the scheme. */
40
+ readonly theme?: ReactNode
41
+ }
42
+
43
+ /** The rows an `appearance` declaration asks for: `true` is the scheme alone, left out is none. */
44
+ export const rowsOf = (appearance: boolean | AppearanceRows | undefined): AppearanceRows =>
45
+ appearance === true ? { scheme: true } : appearance || {}
46
+
47
+ /** The rows left to show: none of the theme's when it comes from a host. */
48
+ export function shownRows(rows: AppearanceRows, fromHost: boolean): AppearanceRows {
49
+ if (!fromHost) return rows
50
+ const { sounds, soundsDescription } = rows
51
+ return { ...(sounds ? { sounds } : {}), ...(soundsDescription != null ? { soundsDescription } : {}) }
52
+ }
53
+
54
+ /** Whether any row is left to show: with none, Appearance is no section at all. */
55
+ export const hasRows = (rows: AppearanceRows): boolean => Boolean(rows.scheme || rows.look || rows.sounds || rows.theme != null)
56
+
57
+ /* A choice kept in the preference store, read by every part that shows it, here and in another tab. */
58
+ function storedChoice<T extends string>(key: string, values: readonly T[]) {
59
+ const now = (): T | null => values.find(v => v === getPreference(key)) ?? null
60
+ const write = (value: T | null) => setPreference(key, value)
61
+ const notify = () => preferenceChanged(key)
62
+ return { now, write, subscribe: subscribePreferences, notify }
63
+ }
64
+
65
+ const scheme = storedChoice<ColorScheme>(COLOR_SCHEME_KEY, ['system', 'light', 'dark'])
66
+ const look = storedChoice<LookChoice>(LOOK_KEY, ['auto', 'mac', 'gnome', 'windows'])
67
+
68
+ /*
69
+ * The app's default, while the person has chosen none: what a desk with the scheme row says, `system` when
70
+ * none does. Choosing the default itself forgets the choice, so a later default still applies.
71
+ */
72
+ let fallback: ColorScheme = 'system'
73
+
74
+ /** The desk with the scheme row says what applies while nothing is chosen, until it unmounts. Returns that default. */
75
+ export function useDefaultColorScheme(active: boolean, given: ColorScheme | undefined): ColorScheme {
76
+ const next = given ?? 'system'
77
+ useLayoutEffect(() => {
78
+ if (!active) return
79
+ fallback = next
80
+ scheme.notify()
81
+ return () => {
82
+ fallback = 'system'
83
+ scheme.notify()
84
+ }
85
+ }, [active, next])
86
+ return next
87
+ }
88
+
89
+ /** The scheme the person chose, or null while the app's default stands. */
90
+ export const useChosenColorScheme = (): ColorScheme | null => useSyncExternalStore(scheme.subscribe, scheme.now, () => null)
91
+
92
+ /** The colour scheme chosen in Appearance, or the app's default (`system` unless it gives one) until one is. */
93
+ export const colorScheme = (): ColorScheme => scheme.now() ?? fallback
94
+ /** Chooses a colour scheme, remembers it, and tells everything that shows it. */
95
+ export const setColorScheme = (next: ColorScheme): void => scheme.write(next === fallback ? null : next)
96
+ /** The colour scheme, kept current: for an app that tells a native shell, or themes something of its own. */
97
+ export const useColorScheme = (): ColorScheme => useSyncExternalStore(scheme.subscribe, colorScheme, () => 'system')
98
+
99
+ /** The look chosen in Appearance, or null when the person has not chosen one and the app's own stands. */
100
+ export const lookChoice = (): LookChoice | null => look.now()
101
+ /** Chooses a look, remembers it, and puts it on the desk in place of the app's own. */
102
+ export const setLookChoice = (next: LookChoice): void => look.write(next)
103
+ /** The look chosen in Appearance, kept current, or null while the app's own stands. */
104
+ export const useLookChoice = (): LookChoice | null => useSyncExternalStore(look.subscribe, lookChoice, () => null)
105
+
106
+ /** Puts the scheme on the page: `data-theme`, or none to follow the system through the stylesheet's media query. */
107
+ export function showColorScheme(next: ColorScheme, root: HTMLElement = document.documentElement): void {
108
+ if (next === 'system') root.removeAttribute('data-theme')
109
+ else root.setAttribute('data-theme', next)
110
+ }
111
+
112
+ /*
113
+ * Whether a host sends this app its theme. The bridge an `AppFrame` puts in the page says so when the first
114
+ * theme arrives; an app that knows some other way says `themeFromHost`.
115
+ */
116
+ interface Bridge {
117
+ readonly theme?: unknown
118
+ onTheme?(handler: (theme: unknown) => void): void
119
+ }
120
+ const bridge = (): Bridge | undefined => (typeof window === 'undefined' ? undefined : (window as unknown as { desk?: Bridge }).desk)
121
+ const hostListeners = new Set<() => void>()
122
+ let watched: Bridge | undefined
123
+ let themed: Bridge | undefined
124
+
125
+ /**
126
+ * Whether a host has sent this page its theme, through the bridge: then `data-theme` on the root is the
127
+ * host's, and Appearance must not take it away as it hides. Read from the bridge itself, not only from its
128
+ * handler, so a theme that came before the desk mounted is known on the first render, and the remembered
129
+ * scheme is never put over it, not even for a frame.
130
+ */
131
+ export const hostThemed = (): boolean => {
132
+ const found = bridge()
133
+ return found !== undefined && (found.theme != null || themed === found)
134
+ }
135
+
136
+ function watchBridge() {
137
+ const found = bridge()
138
+ if (!found?.onTheme || watched === found) return
139
+ watched = found
140
+ found.onTheme(() => {
141
+ themed = found
142
+ hostListeners.forEach(listener => listener())
143
+ })
144
+ }
145
+
146
+ const subscribeHost = (listener: () => void) => {
147
+ watchBridge()
148
+ hostListeners.add(listener)
149
+ return () => void hostListeners.delete(listener)
150
+ }
151
+
152
+ /** Whether this app takes its theme from a host: as declared, or else as the bridge has found. */
153
+ export function useThemeFromHost(declared: boolean | undefined): boolean {
154
+ const found = useSyncExternalStore(subscribeHost, hostThemed, () => false)
155
+ return declared ?? found
156
+ }
157
+
158
+ const SCHEMES = [
159
+ { value: 'system', label: 'Follow the system' },
160
+ { value: 'light', label: 'Light' },
161
+ { value: 'dark', label: 'Dark' },
162
+ ] as const satisfies readonly { value: ColorScheme; label: string }[]
163
+
164
+ const LOOKS = [
165
+ { value: 'auto', label: 'Automatic' },
166
+ { value: 'mac', label: 'Mac' },
167
+ { value: 'gnome', label: 'GNOME' },
168
+ { value: 'windows', label: 'Windows' },
169
+ ] as const satisfies readonly { value: LookChoice; label: string }[]
170
+
171
+ /** What `AppearanceSettings` draws: the rows, and the look to show as chosen when the person has chosen none. */
172
+ export interface AppearanceSettingsProps {
173
+ readonly rows: AppearanceRows
174
+ /** The look the desk wears when the person has chosen none: the app's own. */
175
+ readonly look: LookChoice
176
+ }
177
+
178
+ /** The Appearance rows: what Settings shows, or what an app's own Settings window can draw. */
179
+ export function AppearanceSettings({ rows, look: appLook }: AppearanceSettingsProps) {
180
+ const chosenScheme = useColorScheme()
181
+ const chosenLook = useLookChoice() ?? appLook
182
+ return (
183
+ <div className="desk-fields">
184
+ {rows.scheme && <RadioGroup label="Colour scheme" value={chosenScheme} onChange={setColorScheme} options={SCHEMES} />}
185
+ {rows.theme}
186
+ {rows.look && (
187
+ <div className="desk-field">
188
+ <span className="desk-label" aria-hidden="true">Look</span>
189
+ <SegmentedControl label="Look" options={LOOKS} value={chosenLook} onChange={setLookChoice} />
190
+ </div>
191
+ )}
192
+ {rows.sounds && <SoundsToggle fx={rows.sounds} label="Sounds" description={rows.soundsDescription} />}
193
+ </div>
194
+ )
195
+ }
@@ -1,6 +1,6 @@
1
- import { useEffect, useMemo, useRef, useState } from 'react'
1
+ import { useMemo, useRef, useState } from 'react'
2
2
  import type { ReactNode } from 'react'
3
- import { formatShortcut } from '../core/shortcuts.js'
3
+ import { useCopy } from './copy.js'
4
4
  import { Button, SegmentedControl } from './controls.js'
5
5
 
6
6
  /*
@@ -262,11 +262,6 @@ const NAMES: Record<CodeLanguage, string> = {
262
262
  shell: 'Shell', json: 'JSON', text: '',
263
263
  }
264
264
 
265
- /** How long "Copied" stays before the button says Copy again. */
266
- const COPIED_MS = 2000
267
-
268
- type Copying = 'idle' | 'copied' | 'select'
269
-
270
265
  /** A framed block of code to copy, highlighted for its language, with a Copy button in its corner. */
271
266
  export function CodeBlock(props: CodeBlockProps) {
272
267
  const { label, copy = true, highlight, className, variants, value, onValueChange } = props
@@ -279,72 +274,19 @@ export function CodeBlock(props: CodeBlockProps) {
279
274
  const code = (variants ? variant?.code : props.code) ?? ''
280
275
  const language: CodeLanguage = (variants ? variant?.language : props.language) ?? 'text'
281
276
  const body = useRef<HTMLElement>(null)
282
- const [copying, setCopying] = useState<Copying>('idle')
283
- const timer = useRef<ReturnType<typeof setTimeout>>(undefined)
284
- const alive = useRef(true)
285
- const copied = useRef(code)
277
+ // The selection, where there is no clipboard, is the code as drawn: without the trailing newline,
278
+ // and with whatever a host's `highlight` adds to it.
279
+ const copying = useCopy(code, body)
286
280
  const shown = code.endsWith('\n') ? code.slice(0, -1) : code
287
281
  const content = useMemo(() => (highlight ?? highlighted)(shown, language), [shown, language, highlight])
288
282
 
289
- useEffect(() => {
290
- alive.current = true
291
- return () => {
292
- alive.current = false
293
- clearTimeout(timer.current)
294
- }
295
- }, [])
296
- // Copied or selected says something about this code; new code has been neither. Put back while
297
- // rendering, so not one frame says Copied over code nobody copied.
298
- if (copied.current !== code) {
299
- copied.current = code
300
- clearTimeout(timer.current)
301
- if (copying !== 'idle') setCopying('idle')
302
- }
303
-
304
- const settle = (next: Copying) => {
305
- // A copy that finishes after the block has gone has nothing left to say it in.
306
- if (!alive.current) return
307
- clearTimeout(timer.current)
308
- setCopying(next)
309
- if (next === 'copied') timer.current = setTimeout(() => setCopying('idle'), COPIED_MS)
310
- }
311
-
312
- /*
313
- * THE CLIPBOARD IS NOT ALWAYS THERE: a page over plain http has none, and a browser may refuse
314
- * it. Then the code is selected and the older copy command tried; where that fails too, the
315
- * selection stays and the button says which keys copy it, so the person is one keystroke away.
316
- * The selection is the code as drawn: without the trailing newline, and with whatever a host's
317
- * `highlight` adds to it.
318
- */
319
- const onCopy = async () => {
320
- try {
321
- await navigator.clipboard.writeText(code)
322
- settle('copied')
323
- } catch {
324
- const selection = window.getSelection()
325
- if (body.current && selection) selection.selectAllChildren(body.current)
326
- let copied = false
327
- try {
328
- copied = document.execCommand('copy')
329
- } catch {
330
- // Unsupported: the selection is the way.
331
- }
332
- settle(copied ? 'copied' : 'select')
333
- }
334
- }
335
-
336
- const keys = formatShortcut('mod+c')
337
283
  const name = variant
338
284
  ? label ? `${label}, ${variant.label}` : `${variant.label} code`
339
285
  : label ?? (NAMES[language] ? `${NAMES[language]} code` : 'Code')
340
286
  const actions = copy && (
341
287
  <div className="desk-code-actions">
342
- <Button intent="quiet" size="small" onClick={onCopy}>
343
- {copying === 'copied' ? 'Copied' : copying === 'select' ? `Press ${keys}` : 'Copy'}
344
- </Button>
345
- <span className="desk-code-status" role="status">
346
- {copying === 'copied' ? 'Copied' : copying === 'select' ? `Selected. Press ${keys} to copy.` : ''}
347
- </span>
288
+ <Button intent="quiet" size="small" onClick={() => void copying.copy()}>{copying.button}</Button>
289
+ <span className="desk-code-status" role="status">{copying.status}</span>
348
290
  </div>
349
291
  )
350
292
  return (