@bridgebrew/shell 0.0.0-stage → 0.0.2

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.
@@ -0,0 +1,379 @@
1
+ // The shell's theme controls (E8), compact icon buttons at the bottom of the sidebar column (research-018 F1): the
2
+ // theme picker (with the kit's rendering and the icon set; its name says the theme shown) and, before it, what plugins
3
+ // put in the `controls` zone (the light/dark toggle, the help's "?": plugins of their own, research-112).
4
+ // The menu is a shortcut to the user's own appearance (research-028, appearance slice 3): its Style, Icons, Theme and
5
+ // Window manager write Mine for the workspace shown (Settings › Look › Mine shows the same), "Workspace's look" drops
6
+ // Mine there; a theme is set as the pair (it and its counterpart), its mode with it. The mode (Light, Dark, System) is the
7
+ // user's, client-wide: the toggle and the menu's Mode set it, the theme of the pair follows. The window managers are every
8
+ // installed one and "None (one window)"; the shell opens the menu on request (a window manager set aside: its toast's
9
+ // "Choose"). The menu's button is Legacy, shown when the shell's `themeMenu` setting is on, or while a request opens it
10
+ // (the toast's "Choose"). Navigation: the navigators shown (research-035), several at once, never none, Mine as the rest. Last, the
11
+ // button hiding the sidebar column (⌥B). With the column hidden, the controls stand in the rail, one above the other,
12
+ // the help above the light/dark toggle; the rail puts its status and Admin under them, then that button
13
+ // (`SidebarToggle`), showing the column again.
14
+ import * as React from 'react';
15
+ import {
16
+ Button,
17
+ DropdownMenu,
18
+ DropdownMenuContent,
19
+ DropdownMenuItem,
20
+ DropdownMenuLabel,
21
+ DropdownMenuSeparator,
22
+ DropdownMenuTrigger,
23
+ Tooltip,
24
+ TooltipContent,
25
+ TooltipTrigger,
26
+ DEFAULT_ICON_SET,
27
+ DEFAULT_KIT,
28
+ useThemes,
29
+ getModeChoice,
30
+ setMine,
31
+ setMineFields,
32
+ setMode,
33
+ themeOfMode,
34
+ useClientAppearance,
35
+ useColourMode,
36
+ useThemeOfMode,
37
+ useIconSetId,
38
+ useIconSets,
39
+ useKit,
40
+ useRenderings,
41
+ } from '@bridge/ui';
42
+
43
+ const Icon = ({ d }: { d: string }) => (
44
+ <svg
45
+ viewBox="0 0 24 24"
46
+ fill="none"
47
+ stroke="currentColor"
48
+ strokeWidth="2"
49
+ strokeLinecap="round"
50
+ strokeLinejoin="round"
51
+ aria-hidden="true"
52
+ >
53
+ <path d={d} />
54
+ </svg>
55
+ );
56
+ const check = 'M5 12l5 5 9-10';
57
+ const panel = 'M5 4h14a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2ZM9 4v16';
58
+ const palette =
59
+ 'M12 21a9 9 0 1 1 9-9c0 2.2-1.8 3.5-4 3.5h-1.7a1.8 1.8 0 0 0-1.3 3.1A1.5 1.5 0 0 1 12 21ZM7.5 11.5h.01M10 7.5h.01M14.5 7.5h.01M17 11h.01';
60
+
61
+ /** An installed window manager: its plugin key, its title. */
62
+ export type ManagerOption = { key: string; title: string };
63
+
64
+ export function ThemeControls({
65
+ managers,
66
+ active,
67
+ workspace,
68
+ hasWallpaper,
69
+ onChoose,
70
+ onShortcuts,
71
+ openRequests,
72
+ sidebarHidden,
73
+ onSidebarHidden,
74
+ navigators,
75
+ navigatorShown,
76
+ extra,
77
+ look,
78
+ themeMenu = true,
79
+ }: {
80
+ managers: ManagerOption[];
81
+ /** The window manager active on this page, `null` for none. */
82
+ active: string | null;
83
+ /** The workspace shown, whose Mine the menu writes (none: only the mode can be chosen). */
84
+ workspace: string | undefined;
85
+ /** Whether the workspace keeps an image: without one, the themes from the wallpaper cannot be chosen. */
86
+ hasWallpaper: boolean;
87
+ /** A window manager for the workspace shown (`null`: none; `undefined`: the workspace's own again). */
88
+ onChoose: (key: string | null | undefined) => void;
89
+ onShortcuts: () => void;
90
+ /** The requests to open the menu (none: this copy is out of sight and opens none). */
91
+ openRequests: { subscribe(listener: () => void): () => void } | undefined;
92
+ /** The sidebar column hidden: the controls stand one above the other (in the rail), their menus open to the right. */
93
+ sidebarHidden: boolean;
94
+ /** None: no sidebar to show or hide (no plugin fills it), no button. */
95
+ onSidebarHidden: ((hidden: boolean) => void) | undefined;
96
+ /** The navigators this Bridge has (research-035), and which the look shows. */
97
+ navigators: readonly { id: string; label: string }[];
98
+ navigatorShown: (id: string) => boolean;
99
+ /** Before them, what other plugins put with them (the shell's `controls` slot). */
100
+ extra?: React.ReactNode;
101
+ /** The workspace's look as it applies (Mine over the proposal): what it names that this Bridge lacks is said. */
102
+ look?:
103
+ | {
104
+ style?: string | undefined;
105
+ icons?: string | undefined;
106
+ windowManager?: string | undefined;
107
+ navigation?: string[] | undefined;
108
+ }
109
+ | undefined;
110
+ /** The menu's button shown (the shell's `themeMenu` setting, Legacy); off, it shows only while a request opens it. */
111
+ themeMenu?: boolean;
112
+ }) {
113
+ // Every theme, again when the packs' change, names too (research-038).
114
+ const themes = useThemes();
115
+ const [open, setOpen] = React.useState(false);
116
+ const [requested, setRequested] = React.useState(false);
117
+ React.useEffect(
118
+ () =>
119
+ openRequests?.subscribe(() => {
120
+ setRequested(true);
121
+ setOpen(true);
122
+ }),
123
+ [openRequests],
124
+ );
125
+ const onOpenChange = (next: boolean) => {
126
+ setOpen(next);
127
+ if (!next) setRequested(false);
128
+ };
129
+ const client = useClientAppearance();
130
+ const mine = workspace === undefined ? {} : (client.workspaces[workspace] ?? {});
131
+ // Read on each change of the client's appearance (`useClientAppearance` re-renders).
132
+ const modeChoice = getModeChoice();
133
+ const mode = useColourMode();
134
+ const shown = useThemeOfMode(mode);
135
+ const kit = useKit();
136
+ const renderings = useRenderings();
137
+ const iconSet = useIconSetId();
138
+ const iconSets = useIconSets();
139
+ const write = (field: string, value: unknown) => {
140
+ if (workspace !== undefined) void setMine(workspace, field, value);
141
+ };
142
+ /** A theme as the pair (it and its counterpart), its mode with it. */
143
+ const pick = (id: string) => {
144
+ if (workspace === undefined) return;
145
+ void setMineFields(workspace, {
146
+ ...mine,
147
+ lightTheme: themeOfMode(id, 'light'),
148
+ darkTheme: themeOfMode(id, 'dark'),
149
+ });
150
+ void setMode(themes.find((t) => t.id === id)?.type ?? 'light');
151
+ };
152
+ const ownTheme = typeof mine.lightTheme === 'string' || typeof mine.darkTheme === 'string';
153
+ // The theme shown, named and checked when it is Mine's.
154
+ const label = ownTheme ? (themes.find((t) => t.id === shown)?.label ?? shown) : "Workspace's";
155
+ const item = (
156
+ id: string,
157
+ text: string,
158
+ checked: boolean,
159
+ onClick: () => void,
160
+ disabled = false,
161
+ ) => (
162
+ <DropdownMenuItem key={id} onClick={onClick} disabled={disabled}>
163
+ <span className="flex-1">{text}</span>
164
+ {checked && (
165
+ <span className="size-4 text-muted">
166
+ <Icon d={check} />
167
+ </span>
168
+ )}
169
+ </DropdownMenuItem>
170
+ );
171
+ const none = workspace === undefined;
172
+ // What the look names that this Bridge lacks (research-035): it falls back, and says so here.
173
+ const missing = [
174
+ look?.style !== undefined &&
175
+ look.style !== DEFAULT_KIT &&
176
+ !renderings.some((r) => r.id === look.style)
177
+ ? look.style
178
+ : undefined,
179
+ look?.icons !== undefined &&
180
+ look.icons !== DEFAULT_ICON_SET &&
181
+ !iconSets.some((set) => set.id === look.icons)
182
+ ? look.icons
183
+ : undefined,
184
+ look?.windowManager !== undefined &&
185
+ look.windowManager !== 'none' &&
186
+ !managers.some((m) => m.key === look.windowManager)
187
+ ? look.windowManager
188
+ : undefined,
189
+ ...(look?.navigation ?? []).filter((id) => !navigators.some((each) => each.id === id)),
190
+ ].filter((each): each is string => each !== undefined);
191
+ const tip = (text: string, trigger: React.ReactElement) => (
192
+ <Tooltip>
193
+ <TooltipTrigger render={trigger} />
194
+ <TooltipContent side={sidebarHidden ? 'right' : 'top'}>{text}</TooltipContent>
195
+ </Tooltip>
196
+ );
197
+ return (
198
+ <div
199
+ data-slot="theme-controls"
200
+ className={
201
+ sidebarHidden ? 'flex flex-col items-center gap-0.5' : 'flex flex-1 items-center gap-0.5'
202
+ }
203
+ >
204
+ {extra}
205
+ {(themeMenu || requested) && (
206
+ <DropdownMenu open={open} onOpenChange={onOpenChange}>
207
+ {/* The name carries the theme shown when it is the user's ("Theme: Linear Dark"), else "Workspace's"; the icon alone shows. */}
208
+ {tip(
209
+ `Theme: ${label}`,
210
+ <DropdownMenuTrigger render={<Button size="icon" aria-label={`Theme: ${label}`} />}>
211
+ <Icon d={palette} />
212
+ </DropdownMenuTrigger>,
213
+ )}
214
+ <DropdownMenuContent
215
+ side={sidebarHidden ? 'right' : 'top'}
216
+ align={sidebarHidden ? 'end' : 'start'}
217
+ className="max-h-(--available-height) w-[200px] overflow-y-auto"
218
+ >
219
+ {item(
220
+ 'workspace-look',
221
+ "Workspace's look",
222
+ Object.keys(mine).length === 0,
223
+ () => {
224
+ if (workspace !== undefined) void setMineFields(workspace, null);
225
+ },
226
+ none,
227
+ )}
228
+ {missing.length > 0 && (
229
+ <p
230
+ data-slot="look-missing"
231
+ role="note"
232
+ className="m-0 px-2 pb-1 text-xs text-warning-text"
233
+ >
234
+ {`Not installed on this Bridge: ${missing.join(', ')}.`}
235
+ </p>
236
+ )}
237
+ <DropdownMenuSeparator />
238
+ <DropdownMenuLabel>Style</DropdownMenuLabel>
239
+ {item(
240
+ `kit-${DEFAULT_KIT}`,
241
+ 'Bridge',
242
+ kit === DEFAULT_KIT,
243
+ () => write('style', DEFAULT_KIT),
244
+ none,
245
+ )}
246
+ {renderings.map((r) =>
247
+ item(`kit-${r.id}`, r.label, r.id === kit, () => write('style', r.id), none),
248
+ )}
249
+ <DropdownMenuSeparator />
250
+ <DropdownMenuLabel>Icons</DropdownMenuLabel>
251
+ {item(
252
+ `icons-${DEFAULT_ICON_SET}`,
253
+ 'Lucide',
254
+ iconSet === DEFAULT_ICON_SET,
255
+ () => write('icons', DEFAULT_ICON_SET),
256
+ none,
257
+ )}
258
+ {iconSets.map((s) =>
259
+ item(`icons-${s.id}`, s.label, s.id === iconSet, () => write('icons', s.id), none),
260
+ )}
261
+ <DropdownMenuSeparator />
262
+ <DropdownMenuLabel>Mode</DropdownMenuLabel>
263
+ {item('mode-light', 'Light', modeChoice === 'light', () => void setMode('light'))}
264
+ {item('mode-dark', 'Dark', modeChoice === 'dark', () => void setMode('dark'))}
265
+ {item('mode-system', 'System', modeChoice === 'system', () => void setMode('system'))}
266
+ <DropdownMenuSeparator />
267
+ <DropdownMenuLabel>Theme</DropdownMenuLabel>
268
+ {item(
269
+ 'theme-workspace',
270
+ "Workspace's",
271
+ !ownTheme,
272
+ () => {
273
+ if (workspace !== undefined)
274
+ void setMineFields(workspace, {
275
+ ...mine,
276
+ lightTheme: undefined,
277
+ darkTheme: undefined,
278
+ });
279
+ },
280
+ none,
281
+ )}
282
+ {themes.map((t) =>
283
+ item(
284
+ t.id,
285
+ t.label,
286
+ ownTheme && t.id === shown,
287
+ () => pick(t.id),
288
+ none || (t.wallpaper === true && !hasWallpaper),
289
+ ),
290
+ )}
291
+ <DropdownMenuSeparator />
292
+ <DropdownMenuLabel>Window manager</DropdownMenuLabel>
293
+ {item(
294
+ 'manager-workspace',
295
+ "Workspace's choice",
296
+ mine.windowManager === undefined,
297
+ () => onChoose(undefined),
298
+ none,
299
+ )}
300
+ {managers.map((m) =>
301
+ item(`manager-${m.key}`, m.title, m.key === active, () => onChoose(m.key), none),
302
+ )}
303
+ {item('manager-none', 'None (one window)', active === null, () => onChoose(null), none)}
304
+ {/* The navigators shown (research-035), several at once: each item adds or removes one; the last shown stays. */}
305
+ {navigators.length > 1 && (
306
+ <>
307
+ <DropdownMenuSeparator />
308
+ <DropdownMenuLabel>Navigation</DropdownMenuLabel>
309
+ {item(
310
+ 'navigation-workspace',
311
+ "Workspace's choice",
312
+ mine.navigation === undefined,
313
+ () => write('navigation', undefined),
314
+ none,
315
+ )}
316
+ {navigators.map((navigator) => {
317
+ const on = navigatorShown(navigator.id);
318
+ const shownIds = navigators
319
+ .filter((each) => navigatorShown(each.id))
320
+ .map((each) => each.id);
321
+ return item(
322
+ `navigation-${navigator.id}`,
323
+ navigator.label,
324
+ on,
325
+ () =>
326
+ write(
327
+ 'navigation',
328
+ on
329
+ ? shownIds.filter((id) => id !== navigator.id)
330
+ : [...shownIds, navigator.id],
331
+ ),
332
+ none || (on && shownIds.length === 1),
333
+ );
334
+ })}
335
+ </>
336
+ )}
337
+ <DropdownMenuSeparator />
338
+ {item('shortcuts', 'Keyboard shortcuts', false, onShortcuts)}
339
+ </DropdownMenuContent>
340
+ </DropdownMenu>
341
+ )}
342
+ {/* The light/dark toggle and the help's "?" are plugins in `controls`
343
+ (`extra`, above), shown here: by the sidebar's footer while it shows, else by the rail. */}
344
+ {onSidebarHidden && (
345
+ <SidebarToggle sidebarHidden={sidebarHidden} onSidebarHidden={onSidebarHidden} />
346
+ )}
347
+ </div>
348
+ );
349
+ }
350
+
351
+ /** The button hiding the sidebar column (⌥B), or showing it again while hidden (at the rail's bottom). */
352
+ export function SidebarToggle({
353
+ sidebarHidden,
354
+ onSidebarHidden,
355
+ }: {
356
+ sidebarHidden: boolean;
357
+ onSidebarHidden: (hidden: boolean) => void;
358
+ }) {
359
+ return (
360
+ <Tooltip>
361
+ <TooltipTrigger
362
+ render={
363
+ <Button
364
+ size="icon"
365
+ aria-label={sidebarHidden ? 'Show the sidebar' : 'Hide the sidebar'}
366
+ aria-pressed={!sidebarHidden}
367
+ className={sidebarHidden ? undefined : 'ml-auto'}
368
+ onClick={() => onSidebarHidden(!sidebarHidden)}
369
+ >
370
+ <Icon d={panel} />
371
+ </Button>
372
+ }
373
+ />
374
+ <TooltipContent side={sidebarHidden ? 'right' : 'top'}>
375
+ {sidebarHidden ? 'Show the sidebar (⌥B)' : 'Hide the sidebar (⌥B)'}
376
+ </TooltipContent>
377
+ </Tooltip>
378
+ );
379
+ }
@@ -0,0 +1,140 @@
1
+ // The change of workspace (the rail's tiles, `shell.setCurrent`), animated: the sidebar column and the stage fade in,
2
+ // sliding a little the way the rail goes (down to a tile below, up to one above), over the old paint fading out; the
3
+ // rail stays put. Three settings of the shell's (./package.json `bridge.settings`, Bridge-wide, `_host`'s), for the
4
+ // machines that draw it badly: whether it is animated, `slide` or `fade`, and its duration; the user's own choice (Settings
5
+ // › Look › Mine, kept by their client) wins over the first two. A browser asking for reduced
6
+ // motion changes at once whatever they say.
7
+ export type Transition = 'slide' | 'fade' | 'none';
8
+ const styles: readonly Transition[] = ['slide', 'fade'];
9
+ export const defaultTransition: Transition = 'slide';
10
+ /** ms, the setting's bounds and default (./package.json `workspaceTransitionDuration`). */
11
+ export const durations = { min: 100, max: 2000, default: 220 };
12
+
13
+ /** The transition the settings in `values` ask for (`ResolvedSettings.values`, or none before the first answer):
14
+ * `none` when `workspaceTransitionEnabled` is off, else the style, a value this version cannot use being the default. */
15
+ export function readTransition(values: Record<string, { value: unknown }> | undefined): Transition {
16
+ const value = (field: string) =>
17
+ values && Object.hasOwn(values, field) ? values[field]!.value : undefined;
18
+ if (value('workspaceTransitionEnabled') === false) return 'none';
19
+ const style = value('workspaceTransition');
20
+ return styles.includes(style as Transition) ? (style as Transition) : defaultTransition;
21
+ }
22
+
23
+ /** The user's own switch animation (a client-wide preference, appearance slice 3: `workspaceTransition` in the client's
24
+ * appearance), when it is one; else `undefined`, the Bridge's applies. */
25
+ export const ownTransition = (value: unknown): Transition | undefined =>
26
+ value === 'slide' || value === 'fade' || value === 'none' ? value : undefined;
27
+
28
+ /** The duration setting from `values`, in ms: a whole number within `durations`, else the default. */
29
+ export function readDuration(values: Record<string, { value: unknown }> | undefined): number {
30
+ const value =
31
+ values && Object.hasOwn(values, 'workspaceTransitionDuration')
32
+ ? values.workspaceTransitionDuration!.value
33
+ : undefined;
34
+ return typeof value === 'number' &&
35
+ Number.isInteger(value) &&
36
+ value >= durations.min &&
37
+ value <= durations.max
38
+ ? value
39
+ : durations.default;
40
+ }
41
+
42
+ /** The way the frame slides from workspace `from` to `to`, by their places in the rail (`slugs`): `down` to one below,
43
+ * `up` to one above; none when the two are not both there (a fade only). */
44
+ export function directionOf(
45
+ slugs: readonly string[],
46
+ from: string | undefined,
47
+ to: string,
48
+ ): 'up' | 'down' | undefined {
49
+ const a = from === undefined ? -1 : slugs.indexOf(from);
50
+ const b = slugs.indexOf(to);
51
+ if (a === -1 || b === -1 || a === b) return undefined;
52
+ return b > a ? 'down' : 'up';
53
+ }
54
+
55
+ const reducedMotion = () =>
56
+ typeof matchMedia === 'function' && matchMedia('(prefers-reduced-motion: reduce)').matches;
57
+ /** Even through the whole duration (a curve that does most of it at once makes every duration look the same). */
58
+ const ease = 'ease-in-out';
59
+ /** The change under way: the old paints' layers still fading and its animations, taken over by the next one. */
60
+ let running: { paints: HTMLElement[]; animations: Animation[] } | undefined;
61
+
62
+ /** An element's opacity as drawn now (an animation's included). */
63
+ const opacityOf = (element: Element) => Number(getComputedStyle(element).opacity);
64
+
65
+ /** Runs `change` (the workspace becoming another), then draws it per `transition`, lasting `duration` ms. The live page
66
+ * is animated, not pictures of it (View Transitions send every click to `<html>` while they run): the sidebar column
67
+ * and the stage fade in (sliding `direction`'s way for `slide`), and the canvas's paint before fades out over the one
68
+ * after, under them, clicks going through. All of it stays usable while it runs. A change while one runs goes on from
69
+ * what shows: the old paints still fading keep fading from where they are, the parts fade in from their opacity. */
70
+ export function transitionTo(
71
+ transition: Transition,
72
+ duration: number,
73
+ direction: 'up' | 'down' | undefined,
74
+ change: () => void,
75
+ ) {
76
+ const frame = document.querySelector<HTMLElement>('[data-slot="shell"]');
77
+ const parts = frame
78
+ ? [
79
+ ...frame.querySelectorAll<HTMLElement>(
80
+ '[data-slot="shell-sidebar"], [data-slot="shell-stage"]',
81
+ ),
82
+ ]
83
+ : [];
84
+ // What shows now, read before anything is cancelled.
85
+ const from = new Map(parts.map((part) => [part, opacityOf(part)]));
86
+ const previous = running;
87
+ const fading = (previous?.paints ?? []).map((layer) => [layer, opacityOf(layer)] as const);
88
+ running = undefined;
89
+ if (previous) for (const each of previous.animations) each.cancel();
90
+ if (transition === 'none' || reducedMotion() || !frame || typeof frame.animate !== 'function') {
91
+ for (const [layer] of fading) layer.remove();
92
+ change();
93
+ return;
94
+ }
95
+ const options: KeyframeAnimationOptions = { duration, easing: ease };
96
+ // The paint before: a layer under the frame's content (the frame is a stacking context, `fixed`: z-index -1 is over its
97
+ // background, under all it holds). The one of a change cut short keeps fading, over it, from where it was.
98
+ const before = getComputedStyle(frame);
99
+ const paint = document.createElement('div');
100
+ paint.setAttribute('aria-hidden', 'true');
101
+ Object.assign(paint.style, {
102
+ position: 'absolute',
103
+ inset: '0',
104
+ zIndex: '-1',
105
+ pointerEvents: 'none',
106
+ backgroundColor: before.backgroundColor,
107
+ backgroundImage: before.backgroundImage,
108
+ });
109
+ frame.prepend(paint);
110
+ // Kept in their order, each over the next one's paint.
111
+ for (const [layer] of fading.toReversed()) frame.prepend(layer);
112
+ const shift = transition === 'slide' && direction ? (direction === 'down' ? 14 : -14) : 0;
113
+ const animations = [
114
+ paint.animate([{ opacity: 1 }, { opacity: 0 }], { ...options, fill: 'forwards' }),
115
+ ...fading.map(([layer, opacity]) =>
116
+ layer.animate([{ opacity }, { opacity: 0 }], { ...options, fill: 'forwards' }),
117
+ ),
118
+ ...parts.map((part) => {
119
+ const start = previous ? (from.get(part) ?? 0) : 0;
120
+ return part.animate(
121
+ [
122
+ { opacity: start, transform: `translateY(${shift * (1 - start)}px)` },
123
+ { opacity: 1, transform: 'none' },
124
+ ],
125
+ options,
126
+ );
127
+ }),
128
+ ];
129
+ const mine = { paints: [...fading.map(([layer]) => layer), paint], animations };
130
+ running = mine;
131
+ animations[0]!.finished.then(
132
+ () => {
133
+ if (running !== mine) return;
134
+ for (const layer of mine.paints) layer.remove();
135
+ running = undefined;
136
+ },
137
+ () => {},
138
+ );
139
+ change();
140
+ }
@@ -0,0 +1,15 @@
1
+ {
2
+ "//": "The page (its tests are typed with the tests project): its services through the Bridge checkout this repository links (../../bridge), the contracts of the plugins it uses from their folders here.",
3
+ "extends": "../.bridge/tsconfig.browser.json",
4
+ "include": [
5
+ ".",
6
+ "../../bridge/packages/browser-boot/src/workspace.d.ts",
7
+ "../../bridge/vendor/types/client-build-environment.d.ts",
8
+ "../../commands/browser/commands.d.ts",
9
+ "../../project/browser/project.d.ts"
10
+ ],
11
+ "exclude": [
12
+ "./**/*.test.ts",
13
+ "./**/*.test.tsx"
14
+ ]
15
+ }
@@ -0,0 +1,75 @@
1
+ // Research-069: each window's view scope (the shell's window body, one per view generation): its editors'
2
+ // registrations (`useEditing` of `@bridge/ui`) and the state the shell shows. A view is `editing` while any editor is
3
+ // dirty. A scope is the window's while its view is mounted (`attach`); another generation is a new scope, the old one's
4
+ // registrations left behind with it.
5
+ import type { ShellViewScope, ViewState } from './shell.d.ts';
6
+
7
+ /** `shown`: what the shell shows in the view's place; `fallback`: the slot's fallback shown instead of the screen's entry
8
+ * (failed or missing: `failed`; the entries not loaded yet: `reloading`), its own say, over `shown`. */
9
+ type Scope = {
10
+ shown: ViewState;
11
+ fallback: 'reloading' | 'failed' | undefined;
12
+ editors: Map<object, boolean>;
13
+ };
14
+ const stateOf = (scope: Scope): ViewState =>
15
+ [...scope.editors.values()].some(Boolean) ? 'editing' : (scope.fallback ?? scope.shown);
16
+
17
+ /** The shell's `viewState` and `viewScope` (./shell.d.ts); `editing`, told each time a mounted window's view becomes
18
+ * editing (an agent's turn is spent by it, research-069). */
19
+ export function createViewScopes(editing: (id: string) => void = () => {}) {
20
+ const scopes = new Map<string, Scope>();
21
+ return {
22
+ viewState(id: string): ViewState | undefined {
23
+ const scope = scopes.get(id);
24
+ return scope && stateOf(scope);
25
+ },
26
+ viewScope(id: string): ShellViewScope {
27
+ const scope: Scope = { shown: 'idle', fallback: undefined, editors: new Map() };
28
+ const watchers = new Set<() => void>();
29
+ let was = stateOf(scope);
30
+ const changed = () => {
31
+ const now = stateOf(scope);
32
+ if (now === 'editing' && was !== 'editing' && scopes.get(id) === scope) editing(id);
33
+ was = now;
34
+ for (const listener of watchers) listener();
35
+ };
36
+ return {
37
+ register() {
38
+ const token = {};
39
+ scope.editors.set(token, false);
40
+ return {
41
+ set(dirty) {
42
+ if (!scope.editors.has(token) || scope.editors.get(token) === dirty) return;
43
+ scope.editors.set(token, dirty);
44
+ changed();
45
+ },
46
+ remove() {
47
+ if (scope.editors.delete(token)) changed();
48
+ },
49
+ };
50
+ },
51
+ show(shown) {
52
+ if (scope.shown === shown) return;
53
+ scope.shown = shown;
54
+ changed();
55
+ },
56
+ fallback(state) {
57
+ if (scope.fallback === state) return;
58
+ scope.fallback = state;
59
+ changed();
60
+ },
61
+ state: () => stateOf(scope),
62
+ subscribe(listener) {
63
+ watchers.add(listener);
64
+ return () => void watchers.delete(listener);
65
+ },
66
+ attach() {
67
+ scopes.set(id, scope);
68
+ return () => {
69
+ if (scopes.get(id) === scope) scopes.delete(id);
70
+ };
71
+ },
72
+ };
73
+ },
74
+ };
75
+ }