@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.
- package/README.md +312 -5
- package/dist/core/hosted.d.ts +222 -0
- package/dist/core/hosted.d.ts.map +1 -0
- package/dist/core/hosted.js +166 -0
- package/dist/core/hosted.js.map +1 -0
- package/dist/core/index.d.ts +10 -0
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +5 -0
- package/dist/core/index.js.map +1 -1
- package/dist/core/location.d.ts +8 -1
- package/dist/core/location.d.ts.map +1 -1
- package/dist/core/location.js +18 -2
- package/dist/core/location.js.map +1 -1
- package/dist/core/preferences.d.ts +27 -0
- package/dist/core/preferences.d.ts.map +1 -0
- package/dist/core/preferences.js +121 -0
- package/dist/core/preferences.js.map +1 -0
- package/dist/core/scheme.d.ts +18 -0
- package/dist/core/scheme.d.ts.map +1 -0
- package/dist/core/scheme.js +24 -0
- package/dist/core/scheme.js.map +1 -0
- package/dist/core/theme.d.ts +41 -0
- package/dist/core/theme.d.ts.map +1 -0
- package/dist/core/theme.js +56 -0
- package/dist/core/theme.js.map +1 -0
- package/dist/core/themeChange.d.ts +25 -0
- package/dist/core/themeChange.d.ts.map +1 -0
- package/dist/core/themeChange.js +49 -0
- package/dist/core/themeChange.js.map +1 -0
- package/dist/core/types.d.ts +6 -0
- package/dist/core/types.d.ts.map +1 -1
- package/dist/css/apps.css +183 -0
- package/dist/css/overlays.css +6 -0
- package/dist/css/shell.css +100 -0
- package/dist/css/tokens.css +10 -0
- package/dist/css/windows.css +21 -0
- package/dist/desk.css +220 -0
- package/dist/fx/fx.d.ts.map +1 -1
- package/dist/fx/fx.js +7 -31
- package/dist/fx/fx.js.map +1 -1
- package/dist/host/checkChrome.d.ts +20 -0
- package/dist/host/checkChrome.d.ts.map +1 -0
- package/dist/host/checkChrome.js +219 -0
- package/dist/host/checkChrome.js.map +1 -0
- package/dist/host/checkTheme.d.ts +35 -0
- package/dist/host/checkTheme.d.ts.map +1 -0
- package/dist/host/checkTheme.js +128 -0
- package/dist/host/checkTheme.js.map +1 -0
- package/dist/host/index.d.ts +7 -0
- package/dist/host/index.d.ts.map +1 -0
- package/dist/host/index.js +8 -0
- package/dist/host/index.js.map +1 -0
- package/dist/host/router.d.ts +99 -0
- package/dist/host/router.d.ts.map +1 -0
- package/dist/host/router.js +279 -0
- package/dist/host/router.js.map +1 -0
- package/dist/react/Desktop.d.ts.map +1 -1
- package/dist/react/Desktop.js +13 -2
- package/dist/react/Desktop.js.map +1 -1
- package/dist/react/Dock.d.ts +26 -2
- package/dist/react/Dock.d.ts.map +1 -1
- package/dist/react/Dock.js +23 -27
- package/dist/react/Dock.js.map +1 -1
- package/dist/react/MenuBar.d.ts +1 -1
- package/dist/react/MenuBar.d.ts.map +1 -1
- package/dist/react/MenuBar.js +36 -6
- package/dist/react/MenuBar.js.map +1 -1
- package/dist/react/app.d.ts +215 -0
- package/dist/react/app.d.ts.map +1 -0
- package/dist/react/app.js +192 -0
- package/dist/react/app.js.map +1 -0
- package/dist/react/appFrame.d.ts +11 -7
- package/dist/react/appFrame.d.ts.map +1 -1
- package/dist/react/appFrame.js +24 -6
- package/dist/react/appFrame.js.map +1 -1
- package/dist/react/appPanels.d.ts +25 -0
- package/dist/react/appPanels.d.ts.map +1 -0
- package/dist/react/appPanels.js +123 -0
- package/dist/react/appPanels.js.map +1 -0
- package/dist/react/appearance.d.ts +61 -0
- package/dist/react/appearance.d.ts.map +1 -0
- package/dist/react/appearance.js +119 -0
- package/dist/react/appearance.js.map +1 -0
- package/dist/react/codeBlock.d.ts.map +1 -1
- package/dist/react/codeBlock.js +6 -60
- package/dist/react/codeBlock.js.map +1 -1
- package/dist/react/context.d.ts +23 -1
- package/dist/react/context.d.ts.map +1 -1
- package/dist/react/context.js +108 -5
- package/dist/react/context.js.map +1 -1
- package/dist/react/copy.d.ts +22 -0
- package/dist/react/copy.d.ts.map +1 -0
- package/dist/react/copy.js +68 -0
- package/dist/react/copy.js.map +1 -0
- package/dist/react/dockStatus.d.ts +19 -0
- package/dist/react/dockStatus.d.ts.map +1 -0
- package/dist/react/dockStatus.js +25 -0
- package/dist/react/dockStatus.js.map +1 -0
- package/dist/react/documentTitle.d.ts.map +1 -1
- package/dist/react/documentTitle.js +4 -1
- package/dist/react/documentTitle.js.map +1 -1
- package/dist/react/frameMenus.d.ts +28 -0
- package/dist/react/frameMenus.d.ts.map +1 -0
- package/dist/react/frameMenus.js +85 -0
- package/dist/react/frameMenus.js.map +1 -0
- package/dist/react/hostApp.d.ts +15 -0
- package/dist/react/hostApp.d.ts.map +1 -0
- package/dist/react/hostApp.js +77 -0
- package/dist/react/hostApp.js.map +1 -0
- package/dist/react/hostDock.d.ts +25 -0
- package/dist/react/hostDock.d.ts.map +1 -0
- package/dist/react/hostDock.js +98 -0
- package/dist/react/hostDock.js.map +1 -0
- package/dist/react/hostMenu.d.ts +2 -36
- package/dist/react/hostMenu.d.ts.map +1 -1
- package/dist/react/hostMenu.js.map +1 -1
- package/dist/react/hosted.d.ts +28 -0
- package/dist/react/hosted.d.ts.map +1 -0
- package/dist/react/hosted.js +53 -0
- package/dist/react/hosted.js.map +1 -0
- package/dist/react/index.d.ts +12 -0
- package/dist/react/index.d.ts.map +1 -1
- package/dist/react/index.js +8 -0
- package/dist/react/index.js.map +1 -1
- package/dist/react/layers.d.ts.map +1 -1
- package/dist/react/layers.js +7 -2
- package/dist/react/layers.js.map +1 -1
- package/dist/react/overlays.d.ts.map +1 -1
- package/dist/react/overlays.js +29 -12
- package/dist/react/overlays.js.map +1 -1
- package/dist/react/shell.d.ts +16 -1
- package/dist/react/shell.d.ts.map +1 -1
- package/dist/react/shell.js +19 -3
- package/dist/react/shell.js.map +1 -1
- package/dist/react/windowMenus.d.ts +19 -0
- package/dist/react/windowMenus.d.ts.map +1 -0
- package/dist/react/windowMenus.js +88 -0
- package/dist/react/windowMenus.js.map +1 -0
- package/dist/shell/HostShell.d.ts +53 -0
- package/dist/shell/HostShell.d.ts.map +1 -0
- package/dist/shell/HostShell.js +116 -0
- package/dist/shell/HostShell.js.map +1 -0
- package/dist/shell/index.d.ts +7 -0
- package/dist/shell/index.d.ts.map +1 -0
- package/dist/shell/index.js +9 -0
- package/dist/shell/index.js.map +1 -0
- package/dist/shell/keys.d.ts +47 -0
- package/dist/shell/keys.d.ts.map +1 -0
- package/dist/shell/keys.js +68 -0
- package/dist/shell/keys.js.map +1 -0
- package/dist/shell/motion.d.ts +72 -0
- package/dist/shell/motion.d.ts.map +1 -0
- package/dist/shell/motion.js +118 -0
- package/dist/shell/motion.js.map +1 -0
- package/llms.txt +84 -9
- package/package.json +10 -1
- package/src/core/hosted.ts +351 -0
- package/src/core/index.ts +29 -0
- package/src/core/location.ts +23 -3
- package/src/core/preferences.ts +132 -0
- package/src/core/scheme.ts +32 -0
- package/src/core/theme.ts +91 -0
- package/src/core/themeChange.ts +59 -0
- package/src/core/types.ts +6 -0
- package/src/css/apps.css +183 -0
- package/src/css/overlays.css +6 -0
- package/src/css/shell.css +99 -0
- package/src/css/tokens.css +10 -0
- package/src/css/windows.css +21 -0
- package/src/fx/fx.ts +7 -25
- package/src/host/checkChrome.ts +257 -0
- package/src/host/checkTheme.ts +134 -0
- package/src/host/index.ts +10 -0
- package/src/host/router.ts +374 -0
- package/src/react/Desktop.tsx +21 -3
- package/src/react/Dock.tsx +40 -24
- package/src/react/MenuBar.tsx +39 -6
- package/src/react/app.tsx +354 -0
- package/src/react/appFrame.tsx +39 -14
- package/src/react/appPanels.tsx +210 -0
- package/src/react/appearance.tsx +195 -0
- package/src/react/codeBlock.tsx +7 -65
- package/src/react/context.tsx +131 -4
- package/src/react/copy.ts +81 -0
- package/src/react/dockStatus.ts +42 -0
- package/src/react/documentTitle.ts +4 -1
- package/src/react/frameMenus.ts +100 -0
- package/src/react/hostApp.tsx +89 -0
- package/src/react/hostDock.tsx +118 -0
- package/src/react/hostMenu.ts +3 -38
- package/src/react/hosted.tsx +65 -0
- package/src/react/index.ts +34 -0
- package/src/react/layers.ts +7 -2
- package/src/react/overlays.tsx +30 -12
- package/src/react/shell.tsx +33 -3
- package/src/react/windowMenus.ts +107 -0
- package/src/shell/HostShell.tsx +220 -0
- package/src/shell/index.ts +11 -0
- package/src/shell/keys.ts +101 -0
- package/src/shell/motion.ts +163 -0
package/src/react/hostMenu.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import type { CommandId } from '../core/commands.js'
|
|
2
|
+
import type { HostMenu, HostMenuItem } from '../core/hosted.js'
|
|
2
3
|
import { parseShortcut } from '../core/shortcuts.js'
|
|
3
4
|
import type { Menu, MenuItem } from './MenuBar.js'
|
|
4
5
|
|
|
6
|
+
export type { HostMenu, HostMenuItem }
|
|
7
|
+
|
|
5
8
|
/*
|
|
6
9
|
* A MENU BAR SOMEBODY ELSE DRAWS.
|
|
7
10
|
*
|
|
@@ -11,44 +14,6 @@ import type { Menu, MenuItem } from './MenuBar.js'
|
|
|
11
14
|
* under an id. When someone picks a row the host sends the id back, and the page runs the item.
|
|
12
15
|
*/
|
|
13
16
|
|
|
14
|
-
/** One row of a menu a host draws. Plain data, so it can cross a process boundary. */
|
|
15
|
-
export interface HostMenuItem {
|
|
16
|
-
readonly type: 'item' | 'separator' | 'header'
|
|
17
|
-
/**
|
|
18
|
-
* Present on 'item'; unique within one publish. Opaque to a host, which only sends it back: it
|
|
19
|
-
* names the item's menu, place and label, so a choice from a menu that has since changed runs
|
|
20
|
-
* nothing rather than whatever took that place.
|
|
21
|
-
*/
|
|
22
|
-
readonly id?: string
|
|
23
|
-
/** Present on 'item' and 'header'. */
|
|
24
|
-
readonly label?: string
|
|
25
|
-
readonly detail?: string
|
|
26
|
-
/** In desk's format: 'mod+k', 'mod+alt+a', 'alt+]'. */
|
|
27
|
-
readonly shortcut?: string
|
|
28
|
-
/**
|
|
29
|
-
* The page handles this item's shortcut itself, so a host draws it beside the item and gives it
|
|
30
|
-
* no key (Electron: `registerAccelerator: false`). Set on an action's shortcut, which the drawn
|
|
31
|
-
* bar only shows too: binding it as well would fire the action twice.
|
|
32
|
-
*/
|
|
33
|
-
readonly showOnly?: boolean
|
|
34
|
-
/** Defaults to true. */
|
|
35
|
-
readonly enabled?: boolean
|
|
36
|
-
/** Absent means the item is not checkable. */
|
|
37
|
-
readonly checked?: boolean
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/** A whole menu as a host sees it. */
|
|
41
|
-
export interface HostMenu {
|
|
42
|
-
readonly id: string
|
|
43
|
-
/** The menu's `label`: a host draws text, so a status menu's richer `title` stays behind. */
|
|
44
|
-
readonly label: string
|
|
45
|
-
/** The app's own menu, which a `Menu` calls `emphasis`. */
|
|
46
|
-
readonly primary?: boolean
|
|
47
|
-
/** One of the bar's status menus, which a host puts after the others. */
|
|
48
|
-
readonly status?: boolean
|
|
49
|
-
readonly items: readonly HostMenuItem[]
|
|
50
|
-
}
|
|
51
|
-
|
|
52
17
|
/** What a native shell provides so a `MenuBar` can live in its menu bar. */
|
|
53
18
|
export interface MenuHost {
|
|
54
19
|
/** Replaces every menu the page published before. An empty list takes them all away. */
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { createContext, useContext, useEffect, useMemo, useRef } from 'react'
|
|
2
|
+
import { THEME_TOPIC } from '../core/hosted.js'
|
|
3
|
+
import type { Broadcast, HostClient, Publisher } from '../core/hosted.js'
|
|
4
|
+
import type { DeskTheme } from '../core/theme.js'
|
|
5
|
+
import type { MenuHost } from './hostMenu.js'
|
|
6
|
+
|
|
7
|
+
/*
|
|
8
|
+
* An app in hosted mode has its host in context: `MenuBar` and `Dock` find it there and send their
|
|
9
|
+
* contents to the host instead of drawing them, and the app's own code reaches it through `useHost`
|
|
10
|
+
* to publish and listen. Outside a host the context is empty and nothing changes.
|
|
11
|
+
*
|
|
12
|
+
* The client connects in an effect, so for the first render an app knows it is hosted before it has
|
|
13
|
+
* a client: `client` is null then, and the chrome draws nothing rather than flash its own.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
export const HostContext = createContext<{ readonly client: HostClient | null } | null>(null)
|
|
17
|
+
|
|
18
|
+
/** Whether this app runs in a host, connected yet or not. */
|
|
19
|
+
export const useHosted = (): boolean => useContext(HostContext) !== null
|
|
20
|
+
|
|
21
|
+
/** The host this app runs in, or null when it runs on its own (or has not connected yet). */
|
|
22
|
+
export const useHost = (): HostClient | null => useContext(HostContext)?.client ?? null
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The host's menu bar as a `MenuHost`, for a `MenuBar` in hosted mode; null until connected. Shortcuts
|
|
26
|
+
* are shared as with any host's menu bar: the host gives those with `mod` a key and sends the item
|
|
27
|
+
* back, as a native menu's key equivalents do whichever app has focus, and the app binds the rest.
|
|
28
|
+
*/
|
|
29
|
+
export function useHostMenu(): MenuHost | null {
|
|
30
|
+
const host = useHost()
|
|
31
|
+
return useMemo(
|
|
32
|
+
() =>
|
|
33
|
+
host && {
|
|
34
|
+
publish: menus => host.setChrome({ menus }),
|
|
35
|
+
onSelect: listener => host.onCommand('menu', listener),
|
|
36
|
+
},
|
|
37
|
+
[host],
|
|
38
|
+
)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Hears `publisher`'s broadcasts on `topic` while mounted, starting with the last one. `publisher` is
|
|
43
|
+
* an app's id, or `'primary'`. Does nothing when not hosted.
|
|
44
|
+
*/
|
|
45
|
+
export function useHostBroadcast<T = unknown>(publisher: Publisher, topic: string, handler: (broadcast: Broadcast<T>) => void): void {
|
|
46
|
+
const host = useHost()
|
|
47
|
+
const latest = useRef(handler)
|
|
48
|
+
latest.current = handler
|
|
49
|
+
useEffect(() => host?.subscribe<T>(publisher, topic, broadcast => latest.current(broadcast)), [host, publisher, topic])
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Publishes this app's theme to the host, again whenever it changes: what the primary does so that
|
|
54
|
+
* every other app wears its look. The host passes it on only from the primary. Does nothing when not
|
|
55
|
+
* hosted, or while `theme` is null.
|
|
56
|
+
*/
|
|
57
|
+
export function usePublishTheme(theme: DeskTheme | null): void {
|
|
58
|
+
const host = useHost()
|
|
59
|
+
const key = theme ? JSON.stringify(theme) : ''
|
|
60
|
+
const latest = useRef(theme)
|
|
61
|
+
latest.current = theme
|
|
62
|
+
useEffect(() => {
|
|
63
|
+
if (host && latest.current) host.publish(THEME_TOPIC, latest.current)
|
|
64
|
+
}, [host, key])
|
|
65
|
+
}
|
package/src/react/index.ts
CHANGED
|
@@ -12,6 +12,35 @@ export type {
|
|
|
12
12
|
DropTargetResult,
|
|
13
13
|
} from './dnd.js'
|
|
14
14
|
export { DeskShell } from './shell.js'
|
|
15
|
+
export {
|
|
16
|
+
ABOUT_WINDOW,
|
|
17
|
+
appWindowTitle,
|
|
18
|
+
dockAbout,
|
|
19
|
+
dockSettings,
|
|
20
|
+
openAbout,
|
|
21
|
+
openSettings,
|
|
22
|
+
PANEL_LIMITS,
|
|
23
|
+
SETTINGS_WINDOW,
|
|
24
|
+
SettingsCommand,
|
|
25
|
+
SIDEBAR_LIMITS,
|
|
26
|
+
SYSTEM_GROUP,
|
|
27
|
+
useDeskApp,
|
|
28
|
+
} from './app.js'
|
|
29
|
+
export type { AboutFact, AboutSection, AppDockMarker, AppLink, AppSettings, DeskApp, SettingsSection } from './app.js'
|
|
30
|
+
export { AboutContent, aboutText, SettingsContent, useOpenSettings } from './appPanels.js'
|
|
31
|
+
export {
|
|
32
|
+
AppearanceSettings,
|
|
33
|
+
colorScheme,
|
|
34
|
+
lookChoice,
|
|
35
|
+
setColorScheme,
|
|
36
|
+
setLookChoice,
|
|
37
|
+
useColorScheme,
|
|
38
|
+
useLookChoice,
|
|
39
|
+
} from './appearance.js'
|
|
40
|
+
export type { AppearanceRows, AppearanceSettingsProps } from './appearance.js'
|
|
41
|
+
// Also in the core entry, with no React, for a server-rendered <head> and for moving a choice kept elsewhere.
|
|
42
|
+
export { COLOR_SCHEME_KEY, COLOR_SCHEME_SCRIPT, colorSchemeScript, LOOK_KEY } from '../core/scheme.js'
|
|
43
|
+
export type { ColorScheme } from '../core/scheme.js'
|
|
15
44
|
export type { DeskShellProps } from './shell.js'
|
|
16
45
|
export { DeskProvider, useDesk, useDeskState, useLook, useWindowId, useWindowRoute } from './context.js'
|
|
17
46
|
export type { DeskProviderProps } from './context.js'
|
|
@@ -90,6 +119,7 @@ export type { ComposerProps, Message, ThreadProps } from './conversation.js'
|
|
|
90
119
|
export { Alert, Popover, Sheet, ToastProvider, useToast } from './overlays.js'
|
|
91
120
|
export type { AlertProps, PopoverProps, PopoverTriggerProps, SheetProps, Toast, ToastInput } from './overlays.js'
|
|
92
121
|
export { MenuBar, menuAction, menuCommand, menuHeader, menuSeparator, widgetMenuItems, windowMenuItems } from './MenuBar.js'
|
|
122
|
+
export { useWindowMenus } from './windowMenus.js'
|
|
93
123
|
export { useContextMenu } from './contextMenu.js'
|
|
94
124
|
export type { ContextMenuOptions, ContextMenuResult, ContextMenuTargetProps } from './contextMenu.js'
|
|
95
125
|
export type { Menu, MenuBarProps, MenuItem } from './MenuBar.js'
|
|
@@ -98,4 +128,8 @@ export type { HostMenu, HostMenuItem, HostMenuModel, MenuHost } from './hostMenu
|
|
|
98
128
|
export { IconView } from './iconView.js'
|
|
99
129
|
export type { IconViewItem, IconViewProps } from './iconView.js'
|
|
100
130
|
export { Dock, DOCK_ITEM, dockItem, dockSeparator, dockStack } from './Dock.js'
|
|
131
|
+
export { hostDockModel } from './hostDock.js'
|
|
132
|
+
export type { HostDockModel } from './hostDock.js'
|
|
133
|
+
export { useHost, useHostBroadcast, useHosted, usePublishTheme } from './hosted.js'
|
|
101
134
|
export type { DockEntry, DockItem, DockPins, DockProps, DockSide, DockStack } from './Dock.js'
|
|
135
|
+
export type { DockStatus } from './dockStatus.js'
|
package/src/react/layers.ts
CHANGED
|
@@ -45,10 +45,15 @@ export function useFocusTrap(panel: RefObject<HTMLElement | null>, open: boolean
|
|
|
45
45
|
const onKeyDown = (event: KeyboardEvent) => {
|
|
46
46
|
if (event.key !== 'Tab' || !panel.current) return
|
|
47
47
|
const stops = tabbable(panel.current)
|
|
48
|
-
|
|
48
|
+
// Nothing to tab to: focus that is in it (a popover holding only text has it on itself) stays there.
|
|
49
|
+
if (!stops.length) {
|
|
50
|
+
if (panel.current.contains(document.activeElement)) event.preventDefault()
|
|
51
|
+
return
|
|
52
|
+
}
|
|
49
53
|
const [first, last] = [stops[0] as HTMLElement, stops.at(-1) as HTMLElement]
|
|
50
54
|
const active = document.activeElement
|
|
51
|
-
|
|
55
|
+
// The panel itself can hold focus (a popover is focusable so its keys scroll it): back from it is out of it.
|
|
56
|
+
if (event.shiftKey && (active === first || active === panel.current || !panel.current.contains(active))) {
|
|
52
57
|
event.preventDefault()
|
|
53
58
|
last.focus()
|
|
54
59
|
} else if (!event.shiftKey && active === last) {
|
package/src/react/overlays.tsx
CHANGED
|
@@ -59,15 +59,25 @@ export function Popover({ open, onOpenChange, label, trigger, placement = 'below
|
|
|
59
59
|
if (anchor && element) placePopover(anchor, element, placement, align)
|
|
60
60
|
}, [placement, align])
|
|
61
61
|
|
|
62
|
-
// Measured once it exists, then kept beside its control while anything under it scrolls
|
|
62
|
+
// Measured once it exists, then kept beside its control while anything under it scrolls, and placed again
|
|
63
|
+
// when what it shows changes size: content that grows after opening (a live note) still fits, or scrolls.
|
|
63
64
|
useLayoutEffect(() => {
|
|
64
65
|
if (!open) return
|
|
65
66
|
place()
|
|
67
|
+
// Scrolling under it moves its control, so it follows. Its own scrolling doesn't: placing it again then would
|
|
68
|
+
// lose where it was scrolled to (see placePopover), so a popover that scrolls could never be scrolled.
|
|
69
|
+
const onScroll = (event: Event) => {
|
|
70
|
+
if (!panel.current?.contains(event.target as Node)) place()
|
|
71
|
+
}
|
|
66
72
|
addEventListener('resize', place)
|
|
67
|
-
document.addEventListener('scroll',
|
|
73
|
+
document.addEventListener('scroll', onScroll, true)
|
|
74
|
+
const content = panel.current?.firstElementChild
|
|
75
|
+
const observer = typeof ResizeObserver === 'undefined' || !content ? null : new ResizeObserver(() => place())
|
|
76
|
+
if (observer && content) observer.observe(content)
|
|
68
77
|
return () => {
|
|
69
78
|
removeEventListener('resize', place)
|
|
70
|
-
document.removeEventListener('scroll',
|
|
79
|
+
document.removeEventListener('scroll', onScroll, true)
|
|
80
|
+
observer?.disconnect()
|
|
71
81
|
}
|
|
72
82
|
}, [open, place])
|
|
73
83
|
|
|
@@ -75,8 +85,10 @@ export function Popover({ open, onOpenChange, label, trigger, placement = 'below
|
|
|
75
85
|
<span className="desk-popover-anchor">
|
|
76
86
|
{trigger({ ref: button, 'aria-haspopup': 'dialog', 'aria-expanded': open, onClick: () => onOpenChange(!open) })}
|
|
77
87
|
{open && typeof document !== 'undefined' && createPortal(
|
|
78
|
-
|
|
79
|
-
|
|
88
|
+
// Focusable, so the focus trap can land on it when it holds nothing to tab to, and the arrow and page
|
|
89
|
+
// keys then scroll it.
|
|
90
|
+
<div ref={panel} role="dialog" aria-label={label} className="desk-popover" data-placement={placement} data-align={align} tabIndex={-1}>
|
|
91
|
+
<div className="desk-popover-content">{children}</div>
|
|
80
92
|
</div>,
|
|
81
93
|
document.body,
|
|
82
94
|
)}
|
|
@@ -91,18 +103,22 @@ const POPOVER_MARGIN = 8
|
|
|
91
103
|
* A popover floats OVER its window rather than inside it. Drawn in the window's content it made that
|
|
92
104
|
* content taller or wider, so opening an (i) grew a scrollbar or pushed the layout — the window
|
|
93
105
|
* changed size to make room for a note about it. Here it is fixed to the viewport, kept inside the
|
|
94
|
-
* window it belongs to
|
|
95
|
-
*
|
|
106
|
+
* window it belongs to and inside the viewport (a window can reach past the screen's foot), flipped to
|
|
107
|
+
* the other side when its side has no room, and held to the room its side has: what doesn't fit scrolls
|
|
108
|
+
* within it, never off the screen.
|
|
96
109
|
*/
|
|
97
110
|
function placePopover(anchor: HTMLElement, element: HTMLElement, placement: 'below' | 'above', align: 'start' | 'end') {
|
|
98
111
|
const box = anchor.getBoundingClientRect()
|
|
99
112
|
const frame = anchor.closest(`[${WINDOW_ATTRIBUTE}]`)?.getBoundingClientRect()
|
|
100
113
|
const bounds = {
|
|
101
|
-
left: (frame?.left ?? 0) + POPOVER_MARGIN,
|
|
102
|
-
top: (frame?.top ?? 0) + POPOVER_MARGIN,
|
|
103
|
-
right: (frame?.right ?? innerWidth) - POPOVER_MARGIN,
|
|
104
|
-
bottom: (frame?.bottom ?? innerHeight) - POPOVER_MARGIN,
|
|
114
|
+
left: Math.max(frame?.left ?? 0, 0) + POPOVER_MARGIN,
|
|
115
|
+
top: Math.max(frame?.top ?? 0, 0) + POPOVER_MARGIN,
|
|
116
|
+
right: Math.min(frame?.right ?? innerWidth, innerWidth) - POPOVER_MARGIN,
|
|
117
|
+
bottom: Math.min(frame?.bottom ?? innerHeight, innerHeight) - POPOVER_MARGIN,
|
|
105
118
|
}
|
|
119
|
+
// Measured at its full height, which means letting go of its limit for a moment: that would reset where it is
|
|
120
|
+
// scrolled to, so that is kept and given back.
|
|
121
|
+
const scrolled = element.scrollTop
|
|
106
122
|
element.style.maxHeight = ''
|
|
107
123
|
const { width, height } = element.getBoundingClientRect()
|
|
108
124
|
const roomBelow = bounds.bottom - box.bottom - POPOVER_GAP
|
|
@@ -117,7 +133,9 @@ function placePopover(anchor: HTMLElement, element: HTMLElement, placement: 'bel
|
|
|
117
133
|
const left = Math.max(bounds.left, Math.min(preferred, bounds.right - width))
|
|
118
134
|
element.style.left = `${left}px`
|
|
119
135
|
element.style.top = `${below ? box.bottom + POPOVER_GAP : box.top - POPOVER_GAP - shown}px`
|
|
120
|
-
|
|
136
|
+
// Always held to its room, not only when it overflows now: content that grows later scrolls inside it.
|
|
137
|
+
element.style.maxHeight = `${room}px`
|
|
138
|
+
element.scrollTop = scrolled
|
|
121
139
|
element.dataset.side = side
|
|
122
140
|
}
|
|
123
141
|
|
package/src/react/shell.tsx
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
|
-
import { useState } from 'react'
|
|
1
|
+
import { useLayoutEffect, useState } from 'react'
|
|
2
2
|
import type { ReactNode } from 'react'
|
|
3
3
|
import { createDesk } from '../core/desk.js'
|
|
4
4
|
import type { Desk } from '../core/desk.js'
|
|
5
5
|
import type { Bus } from '../core/events.js'
|
|
6
|
+
import type { DeskHostBridge } from '../core/hosted.js'
|
|
6
7
|
import type { LookChoice } from '../core/look.js'
|
|
7
8
|
import type { Holdable } from '../core/services.js'
|
|
9
|
+
import { setPreferenceStore } from '../core/preferences.js'
|
|
10
|
+
import type { PreferenceStore } from '../core/preferences.js'
|
|
11
|
+
import type { DeskApp } from './app.js'
|
|
12
|
+
import type { DeskTheme } from '../core/theme.js'
|
|
8
13
|
import type { DeskOptions } from '../core/types.js'
|
|
9
14
|
import { DeskProvider } from './context.js'
|
|
10
15
|
import { BusProvider } from './events.js'
|
|
@@ -22,12 +27,37 @@ export interface DeskShellProps {
|
|
|
22
27
|
readonly look?: LookChoice
|
|
23
28
|
/** What the desk keeps going while it is on screen. See `DeskProviderProps.services`. */
|
|
24
29
|
readonly services?: readonly Holdable[]
|
|
30
|
+
/** What the app says about itself, for its About and its Settings. See `DeskProviderProps.app`. */
|
|
31
|
+
readonly app?: DeskApp
|
|
32
|
+
/** Hosted mode: see `DeskProviderProps.host`. Found on `globalThis.deskHost` when left out. */
|
|
33
|
+
readonly host?: DeskHostBridge | null
|
|
34
|
+
/** How a hosted app wears the theme it is sent. See `DeskProviderProps.onTheme`. */
|
|
35
|
+
readonly onTheme?: (theme: DeskTheme) => void
|
|
36
|
+
/**
|
|
37
|
+
* Where every choice Desk remembers is kept: scheme, look, sounds. This browser's localStorage when left
|
|
38
|
+
* out. Desk copies the store's values to localStorage too, for the head script's first paint.
|
|
39
|
+
*/
|
|
40
|
+
readonly preferences?: PreferenceStore
|
|
25
41
|
readonly children: ReactNode
|
|
26
42
|
}
|
|
27
43
|
|
|
44
|
+
/* The stores mounted shells were given, the latest last: it is the one in use, until its shell goes. */
|
|
45
|
+
const givenStores: PreferenceStore[] = []
|
|
46
|
+
|
|
28
47
|
/** Everything an app needs around it, in one component: the desk, its bus, and toasts. */
|
|
29
|
-
export function DeskShell({ desk, options, bus, toasts = true, look = 'auto', services = [], children }: DeskShellProps) {
|
|
48
|
+
export function DeskShell({ desk, options, bus, toasts = true, look = 'auto', services = [], app, host, onTheme, preferences, children }: DeskShellProps) {
|
|
30
49
|
const [made] = useState(() => desk ?? createDesk(options))
|
|
50
|
+
useLayoutEffect(() => {
|
|
51
|
+
if (!preferences) return
|
|
52
|
+
givenStores.push(preferences)
|
|
53
|
+
setPreferenceStore(preferences)
|
|
54
|
+
return () => {
|
|
55
|
+
const inUse = givenStores.at(-1)
|
|
56
|
+
givenStores.splice(givenStores.lastIndexOf(preferences), 1)
|
|
57
|
+
if (givenStores.at(-1) !== inUse) setPreferenceStore(givenStores.at(-1))
|
|
58
|
+
}
|
|
59
|
+
}, [preferences])
|
|
31
60
|
const inner = toasts ? <ToastProvider>{children}</ToastProvider> : children
|
|
32
|
-
|
|
61
|
+
const given = { ...(app ? { app } : {}), ...(host === undefined ? {} : { host }), ...(onTheme === undefined ? {} : { onTheme }) }
|
|
62
|
+
return <DeskProvider desk={made} look={look} services={services} {...given}>{bus ? <BusProvider bus={bus}>{inner}</BusProvider> : inner}</DeskProvider>
|
|
33
63
|
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { useEffect, useMemo, useSyncExternalStore } from 'react'
|
|
2
|
+
import { focusedId } from '../core/desk.js'
|
|
3
|
+
import type { Desk } from '../core/desk.js'
|
|
4
|
+
import type { WindowId } from '../core/types.js'
|
|
5
|
+
import { parseShortcut, isApplePlatform } from '../core/shortcuts.js'
|
|
6
|
+
import { warnOnce } from '../core/location.js'
|
|
7
|
+
import { DESK_SHORTCUTS } from './app.js'
|
|
8
|
+
import { useOptionalDesk, useWindowId } from './context.js'
|
|
9
|
+
import type { Menu, MenuItem } from './MenuBar.js'
|
|
10
|
+
|
|
11
|
+
/*
|
|
12
|
+
* MENUS FROM THE WINDOW IN FRONT. As on a Mac, the bar follows what is in front: a window's menus join it
|
|
13
|
+
* while the window has focus, after the app's own and before Window and Help, in the drawn bar or a host's.
|
|
14
|
+
* A window's item never takes a shortcut the app or Desk uses: it keeps its label, loses the key, and the
|
|
15
|
+
* clash is said once on the console.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
interface Registry {
|
|
19
|
+
readonly sources: Set<{ readonly window: WindowId; readonly menus: readonly Menu[] }>
|
|
20
|
+
readonly listeners: Set<() => void>
|
|
21
|
+
version: number
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const registries = new WeakMap<Desk, Registry>()
|
|
25
|
+
const registryOf = (desk: Desk): Registry => {
|
|
26
|
+
let registry = registries.get(desk)
|
|
27
|
+
if (!registry) registries.set(desk, (registry = { sources: new Set(), listeners: new Set(), version: 0 }))
|
|
28
|
+
return registry
|
|
29
|
+
}
|
|
30
|
+
const changed = (registry: Registry) => {
|
|
31
|
+
registry.version++
|
|
32
|
+
registry.listeners.forEach(listener => listener())
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Adds `menus` to the bar while `window` is in front, each id prefixed with the window's. Returns a remover. */
|
|
36
|
+
export function addWindowMenus(desk: Desk, window: WindowId, menus: readonly Menu[]): () => void {
|
|
37
|
+
const registry = registryOf(desk)
|
|
38
|
+
const source = { window, menus: menus.map(menu => ({ ...menu, id: `${window}/${menu.id}` })) }
|
|
39
|
+
registry.sources.add(source)
|
|
40
|
+
changed(registry)
|
|
41
|
+
return () => {
|
|
42
|
+
if (registry.sources.delete(source)) changed(registry)
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Menus this window adds to the bar while it has focus. Call it inside a window's content (outside one it
|
|
48
|
+
* does nothing), with a stable array (`useMemo`).
|
|
49
|
+
*/
|
|
50
|
+
export function useWindowMenus(menus: readonly Menu[]): void {
|
|
51
|
+
const desk = useOptionalDesk()
|
|
52
|
+
const window = useWindowId()
|
|
53
|
+
useEffect(() => (desk && window !== null && menus.length ? addWindowMenus(desk, window, menus) : undefined), [desk, window, menus])
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** The menus the window in front has added; none when nothing has focus. */
|
|
57
|
+
export function useFrontMenus(desk: Desk): readonly Menu[] {
|
|
58
|
+
const registry = registryOf(desk)
|
|
59
|
+
const subscribe = (listener: () => void) => {
|
|
60
|
+
registry.listeners.add(listener)
|
|
61
|
+
const stop = desk.subscribe(listener)
|
|
62
|
+
return () => {
|
|
63
|
+
registry.listeners.delete(listener)
|
|
64
|
+
stop()
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
const front = focusedId(useSyncExternalStore(desk.subscribe, desk.getState))
|
|
68
|
+
const version = useSyncExternalStore(subscribe, () => registry.version)
|
|
69
|
+
return useMemo(() => [...registry.sources].filter(s => s.window === front).flatMap(s => s.menus), [registry, front, version])
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** The keys a shortcut stands for, as `matchesShortcut` reads it here: `MOD+E`, and `ctrl+e` off a Mac, are `mod+e`. */
|
|
73
|
+
const keysOf = (shortcut: string) => {
|
|
74
|
+
const s = parseShortcut(shortcut)
|
|
75
|
+
const apple = isApplePlatform()
|
|
76
|
+
return [s.key, s.shift, s.alt, apple && s.mod, s.ctrl || (!apple && s.mod)].join('|')
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const shortcutsOf = (menus: readonly Menu[]) =>
|
|
80
|
+
new Set([
|
|
81
|
+
...Object.keys(DESK_SHORTCUTS),
|
|
82
|
+
...menus.flatMap(m => (typeof m.items === 'function' ? m.items() : m.items)).flatMap(i => ('shortcut' in i && i.shortcut ? [i.shortcut] : [])),
|
|
83
|
+
].map(keysOf))
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* The app's menus with the front window's joined in, before the ones with ids `window` and `help` (or at
|
|
87
|
+
* the end). A front item asking for a shortcut the app's menus, its `status` menus or Desk use loses it.
|
|
88
|
+
* Function items stay so.
|
|
89
|
+
*/
|
|
90
|
+
export function joinFrontMenus(own: readonly Menu[], front: readonly Menu[], status: readonly Menu[] = []): readonly Menu[] {
|
|
91
|
+
if (front.length === 0) return own
|
|
92
|
+
const yielding = (menu: Menu, items: readonly MenuItem[]) => {
|
|
93
|
+
const taken = shortcutsOf([...own, ...status])
|
|
94
|
+
return items.map(item => {
|
|
95
|
+
if (!('shortcut' in item) || !item.shortcut || !taken.has(keysOf(item.shortcut))) return item
|
|
96
|
+
warnOnce(`desk: "${item.label}" in the ${menu.label} menu asks for ${item.shortcut}, which the app uses. The app's keeps it.`)
|
|
97
|
+
const { shortcut: _taken, ...rest } = item
|
|
98
|
+
return rest as MenuItem
|
|
99
|
+
})
|
|
100
|
+
}
|
|
101
|
+
const joined = front.map(menu => {
|
|
102
|
+
const { items } = menu
|
|
103
|
+
return { ...menu, items: typeof items === 'function' ? () => yielding(menu, items()) : yielding(menu, items) }
|
|
104
|
+
})
|
|
105
|
+
const at = own.findIndex(menu => menu.id === 'window' || menu.id === 'help')
|
|
106
|
+
return at < 0 ? [...own, ...joined] : [...own.slice(0, at), ...joined, ...own.slice(at)]
|
|
107
|
+
}
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import { useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react'
|
|
2
|
+
import type { RefObject } from 'react'
|
|
3
|
+
import type { CommandTarget, DeskChrome, HostDockEntry, HostDockItem } from '../core/hosted.js'
|
|
4
|
+
import { applyTheme } from '../core/theme.js'
|
|
5
|
+
import type { DeskTheme } from '../core/theme.js'
|
|
6
|
+
import { DeskProvider } from '../react/context.js'
|
|
7
|
+
import { Dock, roveFocus } from '../react/Dock.js'
|
|
8
|
+
import type { DockEntry, DockItem, DockSide } from '../react/Dock.js'
|
|
9
|
+
import { neighbour, platformOf, switchKeyAction, switchKeys } from './keys.js'
|
|
10
|
+
import type { SwitchKeys } from './keys.js'
|
|
11
|
+
|
|
12
|
+
/*
|
|
13
|
+
* ONE DOCK AND ONE SWITCHER FOR SEVERAL APPS.
|
|
14
|
+
*
|
|
15
|
+
* A host that runs several Desk apps side by side, each in a view of its own, draws the chrome they
|
|
16
|
+
* share once: the dock, and a switcher between them. This is that chrome, for the host's own page.
|
|
17
|
+
* It draws; it moves nothing. The apps' views are the host's, and so is moving them: the shell says
|
|
18
|
+
* what the person asked for (`onSwitch`, `onCommand`) and the host does it, then says where it landed
|
|
19
|
+
* (`current`).
|
|
20
|
+
*
|
|
21
|
+
* The page sits under the apps' views, which the host bounds to the desktop area that `onInsets`
|
|
22
|
+
* reports, so the chrome around them shows and the apps cover the rest.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/** One app the host runs, as the switcher shows it. */
|
|
26
|
+
export interface ShellWorkspace {
|
|
27
|
+
readonly id: string
|
|
28
|
+
readonly title: string
|
|
29
|
+
/** A CSS colour: the switcher entry's mark. */
|
|
30
|
+
readonly accent?: string
|
|
31
|
+
/** An image URL or `data:` URI. */
|
|
32
|
+
readonly icon?: string
|
|
33
|
+
/** The app whose theme every workspace wears. */
|
|
34
|
+
readonly primary?: boolean
|
|
35
|
+
/** Running and drawn; asleep, with a snapshot; or closed, with a snapshot, until it is visited again. */
|
|
36
|
+
readonly state: 'live' | 'suspended' | 'discarded'
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** How the host should show a switch: the desktops slide, or (from the overview) zoom. */
|
|
40
|
+
export type SwitchHow = 'slide' | 'zoom'
|
|
41
|
+
|
|
42
|
+
/** The room the chrome takes on each side of the window, in CSS pixels: the apps go inside it. */
|
|
43
|
+
export interface ShellInsets {
|
|
44
|
+
readonly top: number
|
|
45
|
+
readonly right: number
|
|
46
|
+
readonly bottom: number
|
|
47
|
+
readonly left: number
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface HostShellProps {
|
|
51
|
+
/** In the switcher's order: the primary first, then as the host registered them. */
|
|
52
|
+
readonly workspaces: readonly ShellWorkspace[]
|
|
53
|
+
/** The workspace on screen. Changes when the host has finished moving to another. */
|
|
54
|
+
readonly current: string
|
|
55
|
+
/** What each app last sent of its chrome, by workspace id; missing or null until it has. */
|
|
56
|
+
readonly chrome: Readonly<Record<string, DeskChrome | null | undefined>>
|
|
57
|
+
/** The primary's theme, applied to this page. */
|
|
58
|
+
readonly theme?: DeskTheme | null
|
|
59
|
+
/** A picture of each workspace that isn't live, for the overview. */
|
|
60
|
+
readonly snapshots?: Readonly<Record<string, string>>
|
|
61
|
+
/** The person asked for another workspace. */
|
|
62
|
+
readonly onSwitch: (id: string, how: SwitchHow) => void
|
|
63
|
+
/** The person chose one of an app's items: `target` says from where, `itemId` is the app's own id for it. */
|
|
64
|
+
readonly onCommand: (appId: string, target: CommandTarget, itemId: string) => void
|
|
65
|
+
/** The room the chrome takes, whenever it changes: bound the apps' views inside it. */
|
|
66
|
+
readonly onInsets?: (insets: ShellInsets) => void
|
|
67
|
+
/** The switching keys; the platform's defaults if left out, none at all with null. */
|
|
68
|
+
readonly keys?: SwitchKeys | null
|
|
69
|
+
/** How a click in the switcher switches. */
|
|
70
|
+
readonly clickSwitch?: SwitchHow
|
|
71
|
+
readonly dockSide?: DockSide
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const imageIcon = (label: string, image: string | undefined) =>
|
|
75
|
+
image ? <img src={image} alt="" draggable={false} /> : <span className="desk-shell-initial">{label.slice(0, 1).toUpperCase()}</span>
|
|
76
|
+
|
|
77
|
+
/** An app's dock entries, as data, turned back into entries the drawn dock draws; choosing one asks the host. */
|
|
78
|
+
export function dockFromHost(entries: readonly HostDockEntry[], choose: (itemId: string) => void): DockEntry[] {
|
|
79
|
+
const item = (entry: HostDockItem): DockItem => ({
|
|
80
|
+
id: entry.id,
|
|
81
|
+
label: entry.label,
|
|
82
|
+
icon: imageIcon(entry.label, entry.image),
|
|
83
|
+
...(entry.description === undefined ? {} : { description: entry.description }),
|
|
84
|
+
...(entry.badge === undefined ? {} : { badge: entry.badge === '' ? <span className="desk-shell-dot" /> : entry.badge }),
|
|
85
|
+
...(entry.disabled ? { disabled: true } : {}),
|
|
86
|
+
status: { running: entry.running, focused: entry.focused, minimized: entry.minimized },
|
|
87
|
+
onSelect: () => choose(entry.id),
|
|
88
|
+
})
|
|
89
|
+
return entries.map((entry): DockEntry => {
|
|
90
|
+
if (entry.type === 'separator') return { type: 'separator', id: entry.id }
|
|
91
|
+
if (entry.type === 'item') return { type: 'item', ...item(entry) }
|
|
92
|
+
return { type: 'stack', id: entry.id, label: entry.label, icon: imageIcon(entry.label, entry.image), items: entry.items.map(item) }
|
|
93
|
+
})
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const defaultKeys = (): SwitchKeys => switchKeys(platformOf(typeof navigator === 'undefined' ? '' : navigator.platform || navigator.userAgent))
|
|
97
|
+
|
|
98
|
+
export function HostShell(props: HostShellProps) {
|
|
99
|
+
// Its own desk, never hosted: this page is the host's, drawing for the apps.
|
|
100
|
+
return (
|
|
101
|
+
<DeskProvider host={null} look={props.theme?.look ?? 'auto'}>
|
|
102
|
+
<Shell {...props} />
|
|
103
|
+
</DeskProvider>
|
|
104
|
+
)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function Shell({ workspaces, current, chrome, theme, onSwitch, onCommand, onInsets, keys, clickSwitch = 'slide', dockSide = 'left' }: HostShellProps) {
|
|
108
|
+
useLayoutEffect(() => {
|
|
109
|
+
if (theme) applyTheme(theme)
|
|
110
|
+
}, [theme])
|
|
111
|
+
|
|
112
|
+
const entries = useMemo(
|
|
113
|
+
() => dockFromHost(chrome[current]?.dock ?? [], itemId => onCommand(current, 'dock', itemId)),
|
|
114
|
+
[chrome, current, onCommand],
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
// The arrived workspace's title, said once, politely: only on a change, never on first showing.
|
|
118
|
+
const [arrived, setArrived] = useState('')
|
|
119
|
+
const shown = useRef(current)
|
|
120
|
+
useEffect(() => {
|
|
121
|
+
if (shown.current === current) return
|
|
122
|
+
shown.current = current
|
|
123
|
+
setArrived(workspaces.find(w => w.id === current)?.title ?? '')
|
|
124
|
+
}, [current, workspaces])
|
|
125
|
+
|
|
126
|
+
// The switching keys, when this page has focus; a host catches them first when an app has it.
|
|
127
|
+
const bound = useMemo(() => (keys === null ? null : keys ?? defaultKeys()), [keys])
|
|
128
|
+
useEffect(() => {
|
|
129
|
+
if (!bound) return
|
|
130
|
+
const onKey = (event: globalThis.KeyboardEvent) => {
|
|
131
|
+
const action = switchKeyAction(event, bound)
|
|
132
|
+
if (action !== 'previous' && action !== 'next') return
|
|
133
|
+
const to = neighbour(workspaces, current, action)
|
|
134
|
+
event.preventDefault()
|
|
135
|
+
if (to) onSwitch(to.id, 'slide')
|
|
136
|
+
}
|
|
137
|
+
window.addEventListener('keydown', onKey)
|
|
138
|
+
return () => window.removeEventListener('keydown', onKey)
|
|
139
|
+
}, [bound, workspaces, current, onSwitch])
|
|
140
|
+
|
|
141
|
+
const root = useRef<HTMLDivElement>(null)
|
|
142
|
+
useInsets(root, dockSide, onInsets)
|
|
143
|
+
|
|
144
|
+
return (
|
|
145
|
+
<div ref={root} className="desk-shell" data-dock-side={dockSide}>
|
|
146
|
+
<div className="desk-shell-bar">
|
|
147
|
+
<Switcher workspaces={workspaces} current={current} onChoose={id => id !== current && onSwitch(id, clickSwitch)} />
|
|
148
|
+
</div>
|
|
149
|
+
<Dock entries={entries} side={dockSide} label={`${workspaces.find(w => w.id === current)?.title ?? 'App'} dock`} />
|
|
150
|
+
<div className="desk-shell-announce" role="status" aria-live="polite">
|
|
151
|
+
{arrived}
|
|
152
|
+
</div>
|
|
153
|
+
</div>
|
|
154
|
+
)
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function Switcher({ workspaces, current, onChoose }: { readonly workspaces: readonly ShellWorkspace[]; readonly current: string; readonly onChoose: (id: string) => void }) {
|
|
158
|
+
// One Tab stop: the current workspace's, or the first while the current one is not listed.
|
|
159
|
+
const stop = workspaces.some(w => w.id === current) ? current : workspaces[0]?.id
|
|
160
|
+
const toolbar = useRef<HTMLDivElement>(null)
|
|
161
|
+
// Arrowing moves the stop by hand; once the host has moved, it is the current workspace's again.
|
|
162
|
+
useLayoutEffect(() => {
|
|
163
|
+
toolbar.current?.querySelectorAll<HTMLButtonElement>('button').forEach(b => (b.tabIndex = b.dataset.workspace === stop ? 0 : -1))
|
|
164
|
+
}, [stop, workspaces])
|
|
165
|
+
return (
|
|
166
|
+
<div ref={toolbar} role="toolbar" aria-label="Workspaces" className="desk-shell-switcher" onKeyDown={event => {
|
|
167
|
+
// A held modifier makes the arrow a switching key, which the page's listener handles: don't rove too.
|
|
168
|
+
if (!event.ctrlKey && !event.altKey && !event.metaKey) roveFocus(event, { next: ['ArrowRight'], previous: ['ArrowLeft'] })
|
|
169
|
+
}}
|
|
170
|
+
>
|
|
171
|
+
{workspaces.map(w => (
|
|
172
|
+
<button
|
|
173
|
+
key={w.id}
|
|
174
|
+
type="button"
|
|
175
|
+
className="desk-shell-workspace"
|
|
176
|
+
data-rove
|
|
177
|
+
data-workspace={w.id}
|
|
178
|
+
tabIndex={w.id === stop ? 0 : -1}
|
|
179
|
+
aria-current={w.id === current ? 'true' : undefined}
|
|
180
|
+
data-state={w.state}
|
|
181
|
+
onClick={() => onChoose(w.id)}
|
|
182
|
+
>
|
|
183
|
+
{w.icon ? <img src={w.icon} alt="" draggable={false} /> : <span className="desk-shell-mark" style={w.accent ? { background: w.accent } : undefined} aria-hidden="true" />}
|
|
184
|
+
<span>{w.title}</span>
|
|
185
|
+
</button>
|
|
186
|
+
))}
|
|
187
|
+
</div>
|
|
188
|
+
)
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** Reports the room the bar and the dock take, whenever either changes size. */
|
|
192
|
+
function useInsets(root: RefObject<HTMLDivElement | null>, side: DockSide, onInsets: ((insets: ShellInsets) => void) | undefined) {
|
|
193
|
+
const last = useRef('')
|
|
194
|
+
useLayoutEffect(() => {
|
|
195
|
+
const el = root.current
|
|
196
|
+
if (!el || !onInsets) return
|
|
197
|
+
const measure = () => {
|
|
198
|
+
const bounds = el.getBoundingClientRect()
|
|
199
|
+
const bar = el.querySelector('.desk-shell-bar')?.getBoundingClientRect()
|
|
200
|
+
const dock = el.querySelector('.desk-dock')?.getBoundingClientRect()
|
|
201
|
+
const gap = dock && dock.width > 0 ? 1 : 0
|
|
202
|
+
const insets: ShellInsets = {
|
|
203
|
+
top: Math.ceil(bar ? bar.bottom - bounds.top : 0),
|
|
204
|
+
left: side === 'left' && gap ? Math.ceil(dock!.right - bounds.left) : 0,
|
|
205
|
+
right: side === 'right' && gap ? Math.ceil(bounds.right - dock!.left) : 0,
|
|
206
|
+
bottom: side === 'bottom' && gap ? Math.ceil(bounds.bottom - dock!.top) : 0,
|
|
207
|
+
}
|
|
208
|
+
const key = JSON.stringify(insets)
|
|
209
|
+
if (key === last.current) return
|
|
210
|
+
last.current = key
|
|
211
|
+
onInsets(insets)
|
|
212
|
+
}
|
|
213
|
+
measure()
|
|
214
|
+
if (typeof ResizeObserver === 'undefined') return
|
|
215
|
+
const observer = new ResizeObserver(measure)
|
|
216
|
+
observer.observe(el)
|
|
217
|
+
el.querySelectorAll('.desk-shell-bar, .desk-dock').forEach(node => observer.observe(node))
|
|
218
|
+
return () => observer.disconnect()
|
|
219
|
+
})
|
|
220
|
+
}
|