@tearleads/windowing 0.2.22 → 0.2.24
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 +94 -0
- package/desktop/DesktopIcon.d.ts +28 -0
- package/desktop/DesktopIcon.js +38 -0
- package/desktop/DesktopIcon.js.map +1 -0
- package/desktop/DesktopIconMenu.d.ts +18 -0
- package/desktop/DesktopIconMenu.js +14 -0
- package/desktop/DesktopIconMenu.js.map +1 -0
- package/desktop/DesktopIcons.css +69 -0
- package/desktop/DesktopIcons.d.ts +26 -0
- package/desktop/DesktopIcons.js +43 -0
- package/desktop/DesktopIcons.js.map +1 -0
- package/desktop/desktopState.d.ts +43 -0
- package/desktop/desktopState.js +142 -0
- package/desktop/desktopState.js.map +1 -0
- package/desktop/desktopStateStorage.d.ts +21 -0
- package/desktop/desktopStateStorage.js +66 -0
- package/desktop/desktopStateStorage.js.map +1 -0
- package/desktop/useDesktopIconDrag.d.ts +24 -0
- package/desktop/useDesktopIconDrag.js +70 -0
- package/desktop/useDesktopIconDrag.js.map +1 -0
- package/desktop/useDesktopIconSelection.d.ts +12 -0
- package/desktop/useDesktopIconSelection.js +46 -0
- package/desktop/useDesktopIconSelection.js.map +1 -0
- package/desktop/useDesktopIconState.d.ts +17 -0
- package/desktop/useDesktopIconState.js +77 -0
- package/desktop/useDesktopIconState.js.map +1 -0
- package/desktop/useDesktopSurface.d.ts +10 -0
- package/desktop/useDesktopSurface.js +27 -0
- package/desktop/useDesktopSurface.js.map +1 -0
- package/display/DisplayProperties.css +106 -0
- package/display/DisplayProperties.d.ts +8 -0
- package/display/DisplayProperties.js +36 -0
- package/display/DisplayProperties.js.map +1 -0
- package/index.d.ts +9 -0
- package/index.js +10 -2
- package/index.js.map +1 -1
- package/launcher/LauncherNavigationProvider.js +1 -0
- package/launcher/LauncherNavigationProvider.js.map +1 -1
- package/launcher/launcherDefinition.d.ts +6 -0
- package/launcher/launcherDefinition.js.map +1 -1
- package/package.json +1 -1
- package/wallpaper/DesktopWallpaper.d.ts +15 -0
- package/wallpaper/DesktopWallpaper.js +22 -0
- package/wallpaper/DesktopWallpaper.js.map +1 -0
- package/wallpaper/Wallpaper.css +34 -0
- package/wallpaper/WallpaperProvider.d.ts +33 -0
- package/wallpaper/WallpaperProvider.js +37 -0
- package/wallpaper/WallpaperProvider.js.map +1 -0
- package/wallpaper/tessellations.d.ts +13 -0
- package/wallpaper/tessellations.js +151 -0
- package/wallpaper/tessellations.js.map +1 -0
- package/wallpaper/wallpapers.d.ts +52 -0
- package/wallpaper/wallpapers.js +28 -0
- package/wallpaper/wallpapers.js.map +1 -0
package/README.md
CHANGED
|
@@ -249,6 +249,40 @@ pointer, and not an iPad. Pass the mode to
|
|
|
249
249
|
`<html data-navigation-mode>`; the routed touch sizes key off it (see
|
|
250
250
|
[Styles](#styles)), and `useRoutedLayoutActive` reads it back.
|
|
251
251
|
|
|
252
|
+
## Desktop icons
|
|
253
|
+
|
|
254
|
+
`DesktopIcons` shows launcher apps as icons on the windowed desktop, beneath
|
|
255
|
+
its windows. Render it inside `LauncherNavigationProvider`, as the first child
|
|
256
|
+
of the positioned surface the windows lay out on, and name the apps it shows:
|
|
257
|
+
|
|
258
|
+
```tsx
|
|
259
|
+
const storage = createOpfsDesktopStateStorage({ fileName: "desktop.json" });
|
|
260
|
+
|
|
261
|
+
<div className="desktop-surface">
|
|
262
|
+
<DesktopIcons appIds={["notes", "contacts"]} storage={storage} />
|
|
263
|
+
{windows.map((entry) => (
|
|
264
|
+
<MiniAppWindow key={entry.id} windowId={entry.id} />
|
|
265
|
+
))}
|
|
266
|
+
</div>;
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
The icons are options of a listbox named "Desktop" (`label` renames it). A
|
|
270
|
+
press selects an icon and a drag moves it; double-click or Enter opens its app
|
|
271
|
+
with `openMiniApp`, or with `onOpen` when given; arrow keys, Home, and End move
|
|
272
|
+
the selection. Right-click or the context-menu key offers Open, then any items
|
|
273
|
+
`menuItems(appId)` returns. Icons without a saved position fill the free cells
|
|
274
|
+
of a grid from the top-left corner, down then across.
|
|
275
|
+
|
|
276
|
+
Positions persist through a `DesktopStateStorage` adapter, which reads and
|
|
277
|
+
writes one JSON string, so the host chooses where it lives:
|
|
278
|
+
`createOpfsDesktopStateStorage` keeps a file in the origin private file system,
|
|
279
|
+
`createMemoryDesktopStateStorage` keeps it in memory (the default), and a host
|
|
280
|
+
can wrap its own store, as Tearleads wraps each identity's encrypted blob store
|
|
281
|
+
in `packages/app/src/shell/pane/shell/usePaneDesktopStateStorage.ts`. Keep the
|
|
282
|
+
adapter stable: a new adapter reloads the layout from what it holds. Moves made
|
|
283
|
+
before the load finishes win and are saved with the stored layout; when the
|
|
284
|
+
read fails, moves stay in memory and nothing is written.
|
|
285
|
+
|
|
252
286
|
## Routed shell
|
|
253
287
|
|
|
254
288
|
`RoutedPane` is the routed shell, the iPad / phone layout: one mini-app at a
|
|
@@ -329,6 +363,66 @@ the browser paints and removes them on unmount; under a dark scheme,
|
|
|
329
363
|
`tokens.css` sets `color-scheme: dark`. Tearleads sets its themes per host
|
|
330
364
|
profile in `packages/app/src/host/AppHostConfig.ts`.
|
|
331
365
|
|
|
366
|
+
## Wallpapers and Display Properties
|
|
367
|
+
|
|
368
|
+
A host offers its own desktop wallpapers. A `WallpaperDefinition` is
|
|
369
|
+
`{ id, label, tile?, base?, ink? }`: `tile` is one cell of a repeating
|
|
370
|
+
pattern, as SVG markup with its `width` and `height` in CSS pixels. The tile's
|
|
371
|
+
SVG is a mask rather than a picture: opaque shapes paint the ink, translucent
|
|
372
|
+
ones (`fill-opacity`) a share of it, and the base shows elsewhere. Unless a
|
|
373
|
+
wallpaper names its colors, they come from the theme, resolved on the
|
|
374
|
+
wallpaper's own element: the `--desktop-wallpaper-base` and
|
|
375
|
+
`--desktop-wallpaper-ink` tokens where a host sets them, else the desk color
|
|
376
|
+
(`--desktop-background`, then `--color-light`) under a faint wash of
|
|
377
|
+
`--color-dark`. So one tile suits every theme.
|
|
378
|
+
A wallpaper without a tile is its base alone.
|
|
379
|
+
|
|
380
|
+
The package ships quiet tessellations to choose from: `BLOCKS_WALLPAPER`,
|
|
381
|
+
`HONEYCOMB_WALLPAPER`, `TRIANGLES_WALLPAPER`, `HARLEQUIN_WALLPAPER`,
|
|
382
|
+
`CHEVRON_WALLPAPER`, and `PLAIN_WALLPAPER` (no pattern). List them with the
|
|
383
|
+
host's own in a `WallpaperProvider`, and render `DesktopWallpaper` as the
|
|
384
|
+
first child of the desktop surface, before `DesktopIcons`:
|
|
385
|
+
|
|
386
|
+
```tsx
|
|
387
|
+
const BRICKS: WallpaperDefinition<"bricks"> = {
|
|
388
|
+
id: "bricks",
|
|
389
|
+
label: "Bricks",
|
|
390
|
+
tile: {
|
|
391
|
+
height: 24,
|
|
392
|
+
svg: `<svg xmlns="http://www.w3.org/2000/svg" width="48" height="24">…</svg>`,
|
|
393
|
+
width: 48,
|
|
394
|
+
},
|
|
395
|
+
};
|
|
396
|
+
const WALLPAPERS = [BRICKS, HONEYCOMB_WALLPAPER, PLAIN_WALLPAPER];
|
|
397
|
+
|
|
398
|
+
<WallpaperProvider
|
|
399
|
+
defaultWallpaper="bricks"
|
|
400
|
+
storageKey="notes.wallpaper"
|
|
401
|
+
wallpapers={WALLPAPERS}
|
|
402
|
+
>
|
|
403
|
+
<div style={{ position: "relative", isolation: "isolate", height: "100%" }}>
|
|
404
|
+
<DesktopWallpaper />
|
|
405
|
+
<DesktopIcons appIds={APP_IDS} />
|
|
406
|
+
{windows}
|
|
407
|
+
</div>
|
|
408
|
+
</WallpaperProvider>;
|
|
409
|
+
```
|
|
410
|
+
|
|
411
|
+
The wallpaper paints on a layer below everything on the surface, including
|
|
412
|
+
content laid out in flow, so the surface must be a stacking context: give it
|
|
413
|
+
`isolation: isolate` beside its `position`. The wallpaper takes no pointer
|
|
414
|
+
input, so a right-click on it reaches the surface, where a host opens its
|
|
415
|
+
desktop menu. The choice persists under
|
|
416
|
+
`storageKey` like a theme's; `useWallpaper` and `useOptionalWallpaper` read
|
|
417
|
+
and set it. `DesktopWallpaper` paints its `wallpaper` prop instead when given
|
|
418
|
+
one, and nothing without a provider.
|
|
419
|
+
|
|
420
|
+
`DisplayProperties` is the content of a host's Display Properties mini-app: a
|
|
421
|
+
Background section listing the provider's wallpapers as previews, and a Theme
|
|
422
|
+
section listing the theme provider's themes. Each choice applies at once; a
|
|
423
|
+
section whose provider is missing, or a theme list of one, is left out. Both
|
|
424
|
+
Tearleads and a2f0.net open it from their desktop's context menu.
|
|
425
|
+
|
|
332
426
|
## Styles
|
|
333
427
|
|
|
334
428
|
Each component imports its own stylesheet, so a bundler that handles CSS imports
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { MouseEvent } from "react";
|
|
2
|
+
import type { WindowingIcon } from "../icons/windowingIcon.js";
|
|
3
|
+
import type { DesktopIconPosition } from "./desktopState.js";
|
|
4
|
+
import { type ClampDesktopIcon } from "./useDesktopIconDrag.js";
|
|
5
|
+
/** Where arrow, Home, and End keys move the selection. */
|
|
6
|
+
export type DesktopIconStep = "first" | "last" | "next" | "previous";
|
|
7
|
+
interface DesktopIconProps {
|
|
8
|
+
appId: string;
|
|
9
|
+
clamp: ClampDesktopIcon;
|
|
10
|
+
icon: WindowingIcon | undefined;
|
|
11
|
+
onContextMenu: (event: MouseEvent<HTMLElement>) => void;
|
|
12
|
+
onMove: (position: DesktopIconPosition) => void;
|
|
13
|
+
onOpen: () => void;
|
|
14
|
+
onSelect: () => void;
|
|
15
|
+
onStep: (step: DesktopIconStep) => void;
|
|
16
|
+
position: DesktopIconPosition;
|
|
17
|
+
selected: boolean;
|
|
18
|
+
/** Whether Tab reaches this icon: the selected one, or else the first. */
|
|
19
|
+
tabbable: boolean;
|
|
20
|
+
title: string;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* One desktop icon, an option of the desktop's listbox: it selects on press,
|
|
24
|
+
* opens on double-click or Enter, offers its menu on right-click or the
|
|
25
|
+
* context-menu key, and moves when dragged by any pointer.
|
|
26
|
+
*/
|
|
27
|
+
export declare function DesktopIcon({ appId, clamp, icon: Icon, onContextMenu, onMove, onOpen, onSelect, onStep, position, selected, tabbable, title, }: DesktopIconProps): import("react").JSX.Element;
|
|
28
|
+
export {};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useDesktopIconDrag, } from "./useDesktopIconDrag.js";
|
|
3
|
+
const STEP_KEYS = new Map([
|
|
4
|
+
["ArrowDown", "next"],
|
|
5
|
+
["ArrowLeft", "previous"],
|
|
6
|
+
["ArrowRight", "next"],
|
|
7
|
+
["ArrowUp", "previous"],
|
|
8
|
+
["End", "last"],
|
|
9
|
+
["Home", "first"],
|
|
10
|
+
]);
|
|
11
|
+
/**
|
|
12
|
+
* One desktop icon, an option of the desktop's listbox: it selects on press,
|
|
13
|
+
* opens on double-click or Enter, offers its menu on right-click or the
|
|
14
|
+
* context-menu key, and moves when dragged by any pointer.
|
|
15
|
+
*/
|
|
16
|
+
export function DesktopIcon({ appId, clamp, icon: Icon, onContextMenu, onMove, onOpen, onSelect, onStep, position, selected, tabbable, title, }) {
|
|
17
|
+
const { dragPosition, handlers } = useDesktopIconDrag({
|
|
18
|
+
clamp,
|
|
19
|
+
onMove,
|
|
20
|
+
onSelect,
|
|
21
|
+
position,
|
|
22
|
+
});
|
|
23
|
+
const handleKeyDown = (event) => {
|
|
24
|
+
if (event.key === "Enter") {
|
|
25
|
+
event.preventDefault();
|
|
26
|
+
onOpen();
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
const step = STEP_KEYS.get(event.key);
|
|
30
|
+
if (step) {
|
|
31
|
+
event.preventDefault();
|
|
32
|
+
onStep(step);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
const shown = dragPosition ?? position;
|
|
36
|
+
return (_jsxs("button", { ...handlers, "aria-selected": selected, className: "desktop-icon", "data-app-id": appId, "data-dragging": dragPosition ? "" : undefined, onContextMenu: onContextMenu, onDoubleClick: onOpen, onFocus: onSelect, onKeyDown: handleKeyDown, role: "option", style: { left: shown.x, top: shown.y }, tabIndex: tabbable ? 0 : -1, type: "button", children: [Icon && (_jsx(Icon, { "aria-hidden": "true", className: "desktop-icon-glyph", focusable: "false", size: 32 })), _jsx("span", { className: "desktop-icon-label", children: title })] }));
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=DesktopIcon.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DesktopIcon.js","sourceRoot":"","sources":["../../../home/runner/work/tearleads/tearleads/packages/windowing/src/desktop/DesktopIcon.tsx"],"names":[],"mappings":";AAGA,OAAO,EAEL,kBAAkB,GACnB,MAAM,sBAAsB,CAAC;AAK9B,MAAM,SAAS,GAAyC,IAAI,GAAG,CAAC;IAC9D,CAAC,WAAW,EAAE,MAAM,CAAC;IACrB,CAAC,WAAW,EAAE,UAAU,CAAC;IACzB,CAAC,YAAY,EAAE,MAAM,CAAC;IACtB,CAAC,SAAS,EAAE,UAAU,CAAC;IACvB,CAAC,KAAK,EAAE,MAAM,CAAC;IACf,CAAC,MAAM,EAAE,OAAO,CAAC;CAClB,CAAC,CAAC;AAkBH;;;;GAIG;AACH,MAAM,UAAU,WAAW,CAAC,EAC1B,KAAK,EACL,KAAK,EACL,IAAI,EAAE,IAAI,EACV,aAAa,EACb,MAAM,EACN,MAAM,EACN,QAAQ,EACR,MAAM,EACN,QAAQ,EACR,QAAQ,EACR,QAAQ,EACR,KAAK,GACY;IACjB,MAAM,EAAE,YAAY,EAAE,QAAQ,EAAE,GAAG,kBAAkB,CAAC;QACpD,KAAK;QACL,MAAM;QACN,QAAQ;QACR,QAAQ;KACT,CAAC,CAAC;IAEH,MAAM,aAAa,GAAG,CAAC,KAAuC,EAAE,EAAE;QAChE,IAAI,KAAK,CAAC,GAAG,KAAK,OAAO,EAAE,CAAC;YAC1B,KAAK,CAAC,cAAc,EAAE,CAAC;YACvB,MAAM,EAAE,CAAC;YACT,OAAO;QACT,CAAC;QACD,MAAM,IAAI,GAAG,SAAS,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QACtC,IAAI,IAAI,EAAE,CAAC;YACT,KAAK,CAAC,cAAc,EAAE,CAAC;YACvB,MAAM,CAAC,IAAI,CAAC,CAAC;QACf,CAAC;IACH,CAAC,CAAC;IAEF,MAAM,KAAK,GAAG,YAAY,IAAI,QAAQ,CAAC;IACvC,OAAO,CACL,qBACM,QAAQ,mBACG,QAAQ,EACvB,SAAS,EAAC,cAAc,iBACX,KAAK,mBACH,YAAY,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS,EAC5C,aAAa,EAAE,aAAa,EAC5B,aAAa,EAAE,MAAM,EACrB,OAAO,EAAE,QAAQ,EACjB,SAAS,EAAE,aAAa,EACxB,IAAI,EAAC,QAAQ,EACb,KAAK,EAAE,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC,EAAE,EACtC,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAC3B,IAAI,EAAC,QAAQ,aAEZ,IAAI,IAAI,CACP,KAAC,IAAI,mBACS,MAAM,EAClB,SAAS,EAAC,oBAAoB,EAC9B,SAAS,EAAC,OAAO,EACjB,IAAI,EAAE,EAAE,GACR,CACH,EACD,eAAM,SAAS,EAAC,oBAAoB,YAAE,KAAK,GAAQ,IAC5C,CACV,CAAC;AACJ,CAAC","sourcesContent":["import type { KeyboardEvent, MouseEvent } from \"react\";\nimport type { WindowingIcon } from \"../icons/windowingIcon\";\nimport type { DesktopIconPosition } from \"./desktopState\";\nimport {\n type ClampDesktopIcon,\n useDesktopIconDrag,\n} from \"./useDesktopIconDrag\";\n\n/** Where arrow, Home, and End keys move the selection. */\nexport type DesktopIconStep = \"first\" | \"last\" | \"next\" | \"previous\";\n\nconst STEP_KEYS: ReadonlyMap<string, DesktopIconStep> = new Map([\n [\"ArrowDown\", \"next\"],\n [\"ArrowLeft\", \"previous\"],\n [\"ArrowRight\", \"next\"],\n [\"ArrowUp\", \"previous\"],\n [\"End\", \"last\"],\n [\"Home\", \"first\"],\n]);\n\ninterface DesktopIconProps {\n appId: string;\n clamp: ClampDesktopIcon;\n icon: WindowingIcon | undefined;\n onContextMenu: (event: MouseEvent<HTMLElement>) => void;\n onMove: (position: DesktopIconPosition) => void;\n onOpen: () => void;\n onSelect: () => void;\n onStep: (step: DesktopIconStep) => void;\n position: DesktopIconPosition;\n selected: boolean;\n /** Whether Tab reaches this icon: the selected one, or else the first. */\n tabbable: boolean;\n title: string;\n}\n\n/**\n * One desktop icon, an option of the desktop's listbox: it selects on press,\n * opens on double-click or Enter, offers its menu on right-click or the\n * context-menu key, and moves when dragged by any pointer.\n */\nexport function DesktopIcon({\n appId,\n clamp,\n icon: Icon,\n onContextMenu,\n onMove,\n onOpen,\n onSelect,\n onStep,\n position,\n selected,\n tabbable,\n title,\n}: DesktopIconProps) {\n const { dragPosition, handlers } = useDesktopIconDrag({\n clamp,\n onMove,\n onSelect,\n position,\n });\n\n const handleKeyDown = (event: KeyboardEvent<HTMLButtonElement>) => {\n if (event.key === \"Enter\") {\n event.preventDefault();\n onOpen();\n return;\n }\n const step = STEP_KEYS.get(event.key);\n if (step) {\n event.preventDefault();\n onStep(step);\n }\n };\n\n const shown = dragPosition ?? position;\n return (\n <button\n {...handlers}\n aria-selected={selected}\n className=\"desktop-icon\"\n data-app-id={appId}\n data-dragging={dragPosition ? \"\" : undefined}\n onContextMenu={onContextMenu}\n onDoubleClick={onOpen}\n onFocus={onSelect}\n onKeyDown={handleKeyDown}\n role=\"option\"\n style={{ left: shown.x, top: shown.y }}\n tabIndex={tabbable ? 0 : -1}\n type=\"button\"\n >\n {Icon && (\n <Icon\n aria-hidden=\"true\"\n className=\"desktop-icon-glyph\"\n focusable=\"false\"\n size={32}\n />\n )}\n <span className=\"desktop-icon-label\">{title}</span>\n </button>\n );\n}\n"]}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { WindowingIcon } from "../icons/windowingIcon.js";
|
|
2
|
+
import { type MenuPosition } from "../menu/Menu.js";
|
|
3
|
+
/** An item a host adds to an icon's context menu, below Open. */
|
|
4
|
+
export interface DesktopIconMenuItem {
|
|
5
|
+
disabled?: boolean | undefined;
|
|
6
|
+
icon?: WindowingIcon | undefined;
|
|
7
|
+
id: string;
|
|
8
|
+
label: string;
|
|
9
|
+
onSelect: () => void;
|
|
10
|
+
}
|
|
11
|
+
/** An icon's context menu: Open, then the host's items. Each closes it. */
|
|
12
|
+
export declare function DesktopIconMenu({ icon, items, onClose, onOpen, position, }: {
|
|
13
|
+
icon: WindowingIcon | undefined;
|
|
14
|
+
items: ReadonlyArray<DesktopIconMenuItem>;
|
|
15
|
+
onClose: () => void;
|
|
16
|
+
onOpen: () => void;
|
|
17
|
+
position: MenuPosition;
|
|
18
|
+
}): import("react").JSX.Element;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Menu } from "../menu/Menu.js";
|
|
3
|
+
import { MenuItem } from "../menu/MenuItem.js";
|
|
4
|
+
/** An icon's context menu: Open, then the host's items. Each closes it. */
|
|
5
|
+
export function DesktopIconMenu({ icon, items, onClose, onOpen, position, }) {
|
|
6
|
+
return (_jsxs(Menu, { direction: "down", onClose: onClose, position: position, children: [_jsx(MenuItem, { icon: icon, label: "Open", onClick: () => {
|
|
7
|
+
onClose();
|
|
8
|
+
onOpen();
|
|
9
|
+
} }), items.map((item) => (_jsx(MenuItem, { disabled: item.disabled, icon: item.icon, label: item.label, onClick: () => {
|
|
10
|
+
onClose();
|
|
11
|
+
item.onSelect();
|
|
12
|
+
} }, item.id)))] }));
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=DesktopIconMenu.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DesktopIconMenu.js","sourceRoot":"","sources":["../../../home/runner/work/tearleads/tearleads/packages/windowing/src/desktop/DesktopIconMenu.tsx"],"names":[],"mappings":";AACA,OAAO,EAAE,IAAI,EAAqB,MAAM,cAAc,CAAC;AACvD,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAW5C,2EAA2E;AAC3E,MAAM,UAAU,eAAe,CAAC,EAC9B,IAAI,EACJ,KAAK,EACL,OAAO,EACP,MAAM,EACN,QAAQ,GAOT;IACC,OAAO,CACL,MAAC,IAAI,IAAC,SAAS,EAAC,MAAM,EAAC,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,QAAQ,aACzD,KAAC,QAAQ,IACP,IAAI,EAAE,IAAI,EACV,KAAK,EAAC,MAAM,EACZ,OAAO,EAAE,GAAG,EAAE;oBACZ,OAAO,EAAE,CAAC;oBACV,MAAM,EAAE,CAAC;gBACX,CAAC,GACD,EACD,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CACnB,KAAC,QAAQ,IACP,QAAQ,EAAE,IAAI,CAAC,QAAQ,EACvB,IAAI,EAAE,IAAI,CAAC,IAAI,EAEf,KAAK,EAAE,IAAI,CAAC,KAAK,EACjB,OAAO,EAAE,GAAG,EAAE;oBACZ,OAAO,EAAE,CAAC;oBACV,IAAI,CAAC,QAAQ,EAAE,CAAC;gBAClB,CAAC,IALI,IAAI,CAAC,EAAE,CAMZ,CACH,CAAC,IACG,CACR,CAAC;AACJ,CAAC","sourcesContent":["import type { WindowingIcon } from \"../icons/windowingIcon\";\nimport { Menu, type MenuPosition } from \"../menu/Menu\";\nimport { MenuItem } from \"../menu/MenuItem\";\n\n/** An item a host adds to an icon's context menu, below Open. */\nexport interface DesktopIconMenuItem {\n disabled?: boolean | undefined;\n icon?: WindowingIcon | undefined;\n id: string;\n label: string;\n onSelect: () => void;\n}\n\n/** An icon's context menu: Open, then the host's items. Each closes it. */\nexport function DesktopIconMenu({\n icon,\n items,\n onClose,\n onOpen,\n position,\n}: {\n icon: WindowingIcon | undefined;\n items: ReadonlyArray<DesktopIconMenuItem>;\n onClose: () => void;\n onOpen: () => void;\n position: MenuPosition;\n}) {\n return (\n <Menu direction=\"down\" onClose={onClose} position={position}>\n <MenuItem\n icon={icon}\n label=\"Open\"\n onClick={() => {\n onClose();\n onOpen();\n }}\n />\n {items.map((item) => (\n <MenuItem\n disabled={item.disabled}\n icon={item.icon}\n key={item.id}\n label={item.label}\n onClick={() => {\n onClose();\n item.onSelect();\n }}\n />\n ))}\n </Menu>\n );\n}\n"]}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Desktop icons sit on a layer that fills the window surface beneath the
|
|
3
|
+
* windows. The layer itself takes no pointer input, so presses on the bare
|
|
4
|
+
* desktop still reach the surface (a host's own desktop menu, for one).
|
|
5
|
+
*/
|
|
6
|
+
.desktop-icons {
|
|
7
|
+
position: absolute;
|
|
8
|
+
inset: 0;
|
|
9
|
+
z-index: 0;
|
|
10
|
+
pointer-events: none;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/* 80px wide and 88px tall: DESKTOP_ICON_SIZE in desktopState.ts, which keeps
|
|
14
|
+
a stored position on the surface, and a drag measures the same box. */
|
|
15
|
+
.desktop-icon {
|
|
16
|
+
position: absolute;
|
|
17
|
+
display: flex;
|
|
18
|
+
flex-direction: column;
|
|
19
|
+
align-items: center;
|
|
20
|
+
gap: var(--space-xs);
|
|
21
|
+
box-sizing: border-box;
|
|
22
|
+
width: 80px;
|
|
23
|
+
height: 88px;
|
|
24
|
+
padding: var(--space-xs);
|
|
25
|
+
border: 0;
|
|
26
|
+
border-radius: var(--app-radius-control);
|
|
27
|
+
background: transparent;
|
|
28
|
+
color: inherit;
|
|
29
|
+
font: inherit;
|
|
30
|
+
font-size: var(--font-size-sm);
|
|
31
|
+
cursor: default;
|
|
32
|
+
pointer-events: auto;
|
|
33
|
+
touch-action: none;
|
|
34
|
+
user-select: none;
|
|
35
|
+
-webkit-user-select: none;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
.desktop-icon:hover {
|
|
39
|
+
background: color-mix(in srgb, var(--color-dark) 8%, transparent);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
.desktop-icon[aria-selected="true"] {
|
|
43
|
+
background: color-mix(in srgb, var(--emphasis-surface) 18%, transparent);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
.desktop-icon:focus-visible {
|
|
47
|
+
outline: 2px solid var(--emphasis-surface);
|
|
48
|
+
outline-offset: 1px;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
.desktop-icon[data-dragging] {
|
|
52
|
+
z-index: 1;
|
|
53
|
+
opacity: var(--opacity-subtle);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
.desktop-icon-glyph {
|
|
57
|
+
flex: 0 0 auto;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
.desktop-icon-label {
|
|
61
|
+
display: -webkit-box;
|
|
62
|
+
overflow: hidden;
|
|
63
|
+
line-height: 1.2;
|
|
64
|
+
text-align: center;
|
|
65
|
+
overflow-wrap: anywhere;
|
|
66
|
+
-webkit-box-orient: vertical;
|
|
67
|
+
-webkit-line-clamp: 2;
|
|
68
|
+
line-clamp: 2;
|
|
69
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { type DesktopIconMenuItem } from "./DesktopIconMenu.js";
|
|
2
|
+
import { type DesktopStateStorage } from "./desktopStateStorage.js";
|
|
3
|
+
export interface DesktopIconsProps<AppId extends string = string> {
|
|
4
|
+
/** The launcher apps to show, in their default grid order. */
|
|
5
|
+
appIds: ReadonlyArray<AppId>;
|
|
6
|
+
/** The icon list's accessible name. Defaults to "Desktop". */
|
|
7
|
+
label?: string | undefined;
|
|
8
|
+
/** Extra context-menu items for an icon, shown below Open. */
|
|
9
|
+
menuItems?: ((appId: AppId) => ReadonlyArray<DesktopIconMenuItem>) | undefined;
|
|
10
|
+
/** Opens an app. Defaults to the launcher's `openMiniApp`. */
|
|
11
|
+
onOpen?: ((appId: AppId) => void) | undefined;
|
|
12
|
+
/**
|
|
13
|
+
* Where the icon positions persist. Without one they last as long as the
|
|
14
|
+
* component. Keep the adapter stable: a new one reloads the layout.
|
|
15
|
+
*/
|
|
16
|
+
storage?: DesktopStateStorage | undefined;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Launcher apps as icons on the windowed desktop, behind its windows. Drag an
|
|
20
|
+
* icon to move it; double-click it, or press Enter, to open its app;
|
|
21
|
+
* right-click it for its menu. Positions persist through `storage`.
|
|
22
|
+
*
|
|
23
|
+
* Render it inside a {@link LauncherNavigationProvider}, as the first child of
|
|
24
|
+
* the positioned surface the windows lay out on.
|
|
25
|
+
*/
|
|
26
|
+
export declare function DesktopIcons<AppId extends string>({ appIds, label, menuItems, onOpen, storage, }: DesktopIconsProps<AppId>): import("react").JSX.Element;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useCallback, useMemo, useState } from "react";
|
|
3
|
+
import { useLauncherNavigationActions, useLauncherNavigationState, } from "../launcher/LauncherNavigationProvider.js";
|
|
4
|
+
import { isLauncherAppId } from "../launcher/launcherDefinition.js";
|
|
5
|
+
import { useContextMenuState } from "../menu/useContextMenuState.js";
|
|
6
|
+
import { DesktopIcon } from "./DesktopIcon.js";
|
|
7
|
+
import { DesktopIconMenu } from "./DesktopIconMenu.js";
|
|
8
|
+
import { resolveDesktopIconPositions } from "./desktopState.js";
|
|
9
|
+
import { createMemoryDesktopStateStorage, } from "./desktopStateStorage.js";
|
|
10
|
+
import { useDesktopIconSelection } from "./useDesktopIconSelection.js";
|
|
11
|
+
import { useDesktopIconState } from "./useDesktopIconState.js";
|
|
12
|
+
import { useDesktopSurface } from "./useDesktopSurface.js";
|
|
13
|
+
import "./DesktopIcons.css";
|
|
14
|
+
const NO_MENU_ITEMS = [];
|
|
15
|
+
/**
|
|
16
|
+
* Launcher apps as icons on the windowed desktop, behind its windows. Drag an
|
|
17
|
+
* icon to move it; double-click it, or press Enter, to open its app;
|
|
18
|
+
* right-click it for its menu. Positions persist through `storage`.
|
|
19
|
+
*
|
|
20
|
+
* Render it inside a {@link LauncherNavigationProvider}, as the first child of
|
|
21
|
+
* the positioned surface the windows lay out on.
|
|
22
|
+
*/
|
|
23
|
+
export function DesktopIcons({ appIds, label = "Desktop", menuItems, onOpen, storage, }) {
|
|
24
|
+
const { definition } = useLauncherNavigationState();
|
|
25
|
+
const { openMiniApp } = useLauncherNavigationActions();
|
|
26
|
+
const [memoryStorage] = useState(createMemoryDesktopStateStorage);
|
|
27
|
+
const { icons, moveIcon } = useDesktopIconState(storage ?? memoryStorage);
|
|
28
|
+
const [layer, setLayer] = useState(null);
|
|
29
|
+
const { clamp, surface } = useDesktopSurface(layer);
|
|
30
|
+
const shownIds = useMemo(() => appIds.filter((appId) => isLauncherAppId(definition, appId)), [appIds, definition]);
|
|
31
|
+
const { selected, setSelected, step, tabbableId } = useDesktopIconSelection(layer, shownIds);
|
|
32
|
+
const { closeContextMenu, contextMenu, openContextMenu } = useContextMenuState({ onOpen: setSelected });
|
|
33
|
+
const positions = useMemo(() => resolveDesktopIconPositions(shownIds, icons, surface), [icons, shownIds, surface]);
|
|
34
|
+
const open = useCallback((appId) => (onOpen ? onOpen(appId) : openMiniApp({ appId })), [onOpen, openMiniApp]);
|
|
35
|
+
const menuAppId = contextMenu?.id;
|
|
36
|
+
const menuApp = menuAppId === undefined ? null : definition.apps[menuAppId];
|
|
37
|
+
return (_jsxs("div", { "aria-label": label, "aria-orientation": "vertical", className: "desktop-icons", ref: setLayer, role: "listbox", children: [shownIds.map((appId) => {
|
|
38
|
+
const app = definition.apps[appId];
|
|
39
|
+
const position = positions[appId];
|
|
40
|
+
return app && position ? (_jsx(DesktopIcon, { appId: appId, clamp: clamp, icon: app.icon, onContextMenu: (event) => openContextMenu(event, appId), onMove: (next) => moveIcon(appId, next), onOpen: () => open(appId), onSelect: () => setSelected(appId), onStep: (direction) => step(appId, direction), position: position, selected: selected === appId, tabbable: tabbableId === appId, title: app.title }, appId)) : null;
|
|
41
|
+
}), contextMenu && menuAppId !== undefined && menuApp && (_jsx(DesktopIconMenu, { icon: menuApp.icon, items: menuItems?.(menuAppId) ?? NO_MENU_ITEMS, onClose: closeContextMenu, onOpen: () => open(menuAppId), position: contextMenu.position }))] }));
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=DesktopIcons.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DesktopIcons.js","sourceRoot":"","sources":["../../../home/runner/work/tearleads/tearleads/packages/windowing/src/desktop/DesktopIcons.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AACvD,OAAO,EACL,4BAA4B,EAC5B,0BAA0B,GAC3B,MAAM,wCAAwC,CAAC;AAChD,OAAO,EAAE,eAAe,EAAE,MAAM,gCAAgC,CAAC;AACjE,OAAO,EAAE,mBAAmB,EAAE,MAAM,6BAA6B,CAAC;AAClE,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,eAAe,EAA4B,MAAM,mBAAmB,CAAC;AAC9E,OAAO,EAAE,2BAA2B,EAAE,MAAM,gBAAgB,CAAC;AAC7D,OAAO,EACL,+BAA+B,GAEhC,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,uBAAuB,EAAE,MAAM,2BAA2B,CAAC;AACpE,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,oBAAoB,CAAC;AAoB5B,MAAM,aAAa,GAAuC,EAAE,CA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AC5B,QAAQ,EAAE,UAAU,KAAK,KAAK,EAC9B,KAAK,EAAE,GAAG,CAAC,KAAK,IATX,KAAK,CAUV,CACH,CAAC,CAAC,CAAC,IAAI,CAAC;YACX,CAAC,CAAC,EACD,WAAW,IAAI,SAAS,KAAK,SAAS,IAAI,OAAO,IAAI,CACpD,KAAC,eAAe,IACd,IAAI,EAAE,OAAO,CAAC,IAAI,EAClB,KAAK,EAAE,SAAS,EAAE,CAAC,SAAS,CAAC,IAAI,aAAa,EAC9C,OAAO,EAAE,gBAAgB,EACzB,MAAM,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,EAC7B,QAAQ,EAAE,WAAW,CAAC,QAAQ,GAC9B,CACH,IACG,CACP,CAAC;AACJ,CAAC","sourcesContent":["import { useCallback, useMemo, useState } from \"react\";\nimport {\n useLauncherNavigationActions,\n useLauncherNavigationState,\n} from \"../launcher/LauncherNavigationProvider\";\nimport { isLauncherAppId } from \"../launcher/launcherDefinition\";\nimport { useContextMenuState } from \"../menu/useContextMenuState\";\nimport { DesktopIcon } from \"./DesktopIcon\";\nimport { DesktopIconMenu, type DesktopIconMenuItem } from \"./DesktopIconMenu\";\nimport { resolveDesktopIconPositions } from \"./desktopState\";\nimport {\n createMemoryDesktopStateStorage,\n type DesktopStateStorage,\n} from \"./desktopStateStorage\";\nimport { useDesktopIconSelection } from \"./useDesktopIconSelection\";\nimport { useDesktopIconState } from \"./useDesktopIconState\";\nimport { useDesktopSurface } from \"./useDesktopSurface\";\nimport \"./DesktopIcons.css\";\n\nexport interface DesktopIconsProps<AppId extends string = string> {\n /** The launcher apps to show, in their default grid order. */\n appIds: ReadonlyArray<AppId>;\n /** The icon list's accessible name. Defaults to \"Desktop\". */\n label?: string | undefined;\n /** Extra context-menu items for an icon, shown below Open. */\n menuItems?:\n | ((appId: AppId) => ReadonlyArray<DesktopIconMenuItem>)\n | undefined;\n /** Opens an app. Defaults to the launcher's `openMiniApp`. */\n onOpen?: ((appId: AppId) => void) | undefined;\n /**\n * Where the icon positions persist. Without one they last as long as the\n * component. Keep the adapter stable: a new one reloads the layout.\n */\n storage?: DesktopStateStorage | undefined;\n}\n\nconst NO_MENU_ITEMS: ReadonlyArray<DesktopIconMenuItem> = [];\n\n/**\n * Launcher apps as icons on the windowed desktop, behind its windows. Drag an\n * icon to move it; double-click it, or press Enter, to open its app;\n * right-click it for its menu. Positions persist through `storage`.\n *\n * Render it inside a {@link LauncherNavigationProvider}, as the first child of\n * the positioned surface the windows lay out on.\n */\nexport function DesktopIcons<AppId extends string>({\n appIds,\n label = \"Desktop\",\n menuItems,\n onOpen,\n storage,\n}: DesktopIconsProps<AppId>) {\n const { definition } = useLauncherNavigationState();\n const { openMiniApp } = useLauncherNavigationActions();\n const [memoryStorage] = useState(createMemoryDesktopStateStorage);\n const { icons, moveIcon } = useDesktopIconState(storage ?? memoryStorage);\n const [layer, setLayer] = useState<HTMLDivElement | null>(null);\n const { clamp, surface } = useDesktopSurface(layer);\n const shownIds = useMemo(\n () => appIds.filter((appId) => isLauncherAppId(definition, appId)),\n [appIds, definition],\n );\n const { selected, setSelected, step, tabbableId } = useDesktopIconSelection(\n layer,\n shownIds,\n );\n const { closeContextMenu, contextMenu, openContextMenu } =\n useContextMenuState<AppId>({ onOpen: setSelected });\n const positions = useMemo(\n () => resolveDesktopIconPositions(shownIds, icons, surface),\n [icons, shownIds, surface],\n );\n const open = useCallback(\n (appId: AppId) => (onOpen ? onOpen(appId) : openMiniApp({ appId })),\n [onOpen, openMiniApp],\n );\n\n const menuAppId = contextMenu?.id;\n const menuApp = menuAppId === undefined ? null : definition.apps[menuAppId];\n return (\n <div\n aria-label={label}\n aria-orientation=\"vertical\"\n className=\"desktop-icons\"\n ref={setLayer}\n role=\"listbox\"\n >\n {shownIds.map((appId) => {\n const app = definition.apps[appId];\n const position = positions[appId];\n return app && position ? (\n <DesktopIcon\n appId={appId}\n clamp={clamp}\n icon={app.icon}\n key={appId}\n onContextMenu={(event) => openContextMenu(event, appId)}\n onMove={(next) => moveIcon(appId, next)}\n onOpen={() => open(appId)}\n onSelect={() => setSelected(appId)}\n onStep={(direction) => step(appId, direction)}\n position={position}\n selected={selected === appId}\n tabbable={tabbableId === appId}\n title={app.title}\n />\n ) : null;\n })}\n {contextMenu && menuAppId !== undefined && menuApp && (\n <DesktopIconMenu\n icon={menuApp.icon}\n items={menuItems?.(menuAppId) ?? NO_MENU_ITEMS}\n onClose={closeContextMenu}\n onOpen={() => open(menuAppId)}\n position={contextMenu.position}\n />\n )}\n </div>\n );\n}\n"]}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/** An icon's top-left corner, in pixels from the surface's top-left corner. */
|
|
2
|
+
export interface DesktopIconPosition {
|
|
3
|
+
x: number;
|
|
4
|
+
y: number;
|
|
5
|
+
}
|
|
6
|
+
export interface DesktopState {
|
|
7
|
+
/** Icon positions by app id. Icons with no entry take a free grid slot. */
|
|
8
|
+
icons: Readonly<Record<string, DesktopIconPosition>>;
|
|
9
|
+
}
|
|
10
|
+
/** The grid icons start on: its origin, and the size of one cell. */
|
|
11
|
+
export declare const DESKTOP_ICON_GRID: {
|
|
12
|
+
readonly cellHeight: 96;
|
|
13
|
+
readonly cellWidth: 88;
|
|
14
|
+
readonly originX: 12;
|
|
15
|
+
readonly originY: 12;
|
|
16
|
+
};
|
|
17
|
+
export declare const EMPTY_DESKTOP_STATE: DesktopState;
|
|
18
|
+
/**
|
|
19
|
+
* Reads stored desktop state. Anything unreadable (no text, bad JSON, another
|
|
20
|
+
* version, a malformed position) yields what can be kept, or the empty state.
|
|
21
|
+
*/
|
|
22
|
+
export declare function parseDesktopState(serialized: string | null): DesktopState;
|
|
23
|
+
export declare function serializeDesktopState(state: DesktopState): string;
|
|
24
|
+
/** The surface's size; zero while it is not laid out yet. */
|
|
25
|
+
export interface DesktopSurfaceSize {
|
|
26
|
+
height: number;
|
|
27
|
+
width: number;
|
|
28
|
+
}
|
|
29
|
+
/** An icon's box (DesktopIcons.css). */
|
|
30
|
+
export declare const DESKTOP_ICON_SIZE: DesktopSurfaceSize;
|
|
31
|
+
/**
|
|
32
|
+
* Keeps an icon of `size` on the surface. An unmeasured (zero) surface only
|
|
33
|
+
* keeps it off negative coordinates.
|
|
34
|
+
*/
|
|
35
|
+
export declare function clampDesktopIconPosition(position: DesktopIconPosition, size: DesktopSurfaceSize, surface: DesktopSurfaceSize): DesktopIconPosition;
|
|
36
|
+
/**
|
|
37
|
+
* Where every icon sits on the surface. A stored position is kept, moved onto
|
|
38
|
+
* the surface if it was saved on a larger one, unless an icon earlier in
|
|
39
|
+
* `appIds` already buries it there. Every other icon takes the first grid
|
|
40
|
+
* slot, filled top to bottom then left to right, that no icon covers. The
|
|
41
|
+
* surface height sets how many rows a column holds (at least one).
|
|
42
|
+
*/
|
|
43
|
+
export declare function resolveDesktopIconPositions(appIds: ReadonlyArray<string>, stored: DesktopState["icons"], surface: DesktopSurfaceSize): Record<string, DesktopIconPosition>;
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
// The desktop's saved state: where each icon sits on the surface. It is kept
|
|
2
|
+
// as JSON text so a host's storage adapter only moves strings, and parsed
|
|
3
|
+
// defensively, since storage can hold an older, newer, or damaged copy.
|
|
4
|
+
const DESKTOP_STATE_VERSION = 1;
|
|
5
|
+
/** The grid icons start on: its origin, and the size of one cell. */
|
|
6
|
+
export const DESKTOP_ICON_GRID = {
|
|
7
|
+
cellHeight: 96,
|
|
8
|
+
cellWidth: 88,
|
|
9
|
+
originX: 12,
|
|
10
|
+
originY: 12,
|
|
11
|
+
};
|
|
12
|
+
export const EMPTY_DESKTOP_STATE = { icons: {} };
|
|
13
|
+
function isRecord(value) {
|
|
14
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
15
|
+
}
|
|
16
|
+
function readPosition(value) {
|
|
17
|
+
if (!isRecord(value)) {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
const { x, y } = value;
|
|
21
|
+
if (typeof x !== "number" || typeof y !== "number") {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
if (!Number.isFinite(x) || !Number.isFinite(y)) {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
return { x: Math.max(0, Math.round(x)), y: Math.max(0, Math.round(y)) };
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Reads stored desktop state. Anything unreadable (no text, bad JSON, another
|
|
31
|
+
* version, a malformed position) yields what can be kept, or the empty state.
|
|
32
|
+
*/
|
|
33
|
+
export function parseDesktopState(serialized) {
|
|
34
|
+
if (serialized === null) {
|
|
35
|
+
return EMPTY_DESKTOP_STATE;
|
|
36
|
+
}
|
|
37
|
+
let parsed;
|
|
38
|
+
try {
|
|
39
|
+
parsed = JSON.parse(serialized);
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
return EMPTY_DESKTOP_STATE;
|
|
43
|
+
}
|
|
44
|
+
if (!isRecord(parsed)) {
|
|
45
|
+
return EMPTY_DESKTOP_STATE;
|
|
46
|
+
}
|
|
47
|
+
const { icons: storedIcons, version } = parsed;
|
|
48
|
+
if (version !== DESKTOP_STATE_VERSION) {
|
|
49
|
+
return EMPTY_DESKTOP_STATE;
|
|
50
|
+
}
|
|
51
|
+
const icons = {};
|
|
52
|
+
if (isRecord(storedIcons)) {
|
|
53
|
+
for (const [appId, value] of Object.entries(storedIcons)) {
|
|
54
|
+
const position = readPosition(value);
|
|
55
|
+
if (position) {
|
|
56
|
+
icons[appId] = position;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return { icons };
|
|
61
|
+
}
|
|
62
|
+
export function serializeDesktopState(state) {
|
|
63
|
+
return JSON.stringify({ icons: state.icons, version: DESKTOP_STATE_VERSION });
|
|
64
|
+
}
|
|
65
|
+
function gridSlot(index, rows) {
|
|
66
|
+
const column = Math.floor(index / rows);
|
|
67
|
+
const row = index % rows;
|
|
68
|
+
return {
|
|
69
|
+
x: DESKTOP_ICON_GRID.originX + column * DESKTOP_ICON_GRID.cellWidth,
|
|
70
|
+
y: DESKTOP_ICON_GRID.originY + row * DESKTOP_ICON_GRID.cellHeight,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
function overlaps(a, b) {
|
|
74
|
+
return (Math.abs(a.x - b.x) < DESKTOP_ICON_GRID.cellWidth &&
|
|
75
|
+
Math.abs(a.y - b.y) < DESKTOP_ICON_GRID.cellHeight);
|
|
76
|
+
}
|
|
77
|
+
// Whether one icon hides most of another: their boxes are offset by less than
|
|
78
|
+
// half a grid cell on both axes.
|
|
79
|
+
function buries(a, b) {
|
|
80
|
+
return (Math.abs(a.x - b.x) < DESKTOP_ICON_GRID.cellWidth / 2 &&
|
|
81
|
+
Math.abs(a.y - b.y) < DESKTOP_ICON_GRID.cellHeight / 2);
|
|
82
|
+
}
|
|
83
|
+
/** An icon's box (DesktopIcons.css). */
|
|
84
|
+
export const DESKTOP_ICON_SIZE = { height: 88, width: 80 };
|
|
85
|
+
function clampAxis(value, extent, surface) {
|
|
86
|
+
if (surface <= 0) {
|
|
87
|
+
return Math.max(0, value);
|
|
88
|
+
}
|
|
89
|
+
return Math.max(0, Math.min(value, surface - extent));
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Keeps an icon of `size` on the surface. An unmeasured (zero) surface only
|
|
93
|
+
* keeps it off negative coordinates.
|
|
94
|
+
*/
|
|
95
|
+
export function clampDesktopIconPosition(position, size, surface) {
|
|
96
|
+
return {
|
|
97
|
+
x: clampAxis(position.x, size.width, surface.width),
|
|
98
|
+
y: clampAxis(position.y, size.height, surface.height),
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Where every icon sits on the surface. A stored position is kept, moved onto
|
|
103
|
+
* the surface if it was saved on a larger one, unless an icon earlier in
|
|
104
|
+
* `appIds` already buries it there. Every other icon takes the first grid
|
|
105
|
+
* slot, filled top to bottom then left to right, that no icon covers. The
|
|
106
|
+
* surface height sets how many rows a column holds (at least one).
|
|
107
|
+
*/
|
|
108
|
+
export function resolveDesktopIconPositions(appIds, stored, surface) {
|
|
109
|
+
// A row fits while the icon's own box does; the cell's spacing below the
|
|
110
|
+
// last one may fall off the surface.
|
|
111
|
+
const rows = Math.max(1, Math.floor((surface.height - DESKTOP_ICON_GRID.originY - DESKTOP_ICON_SIZE.height) /
|
|
112
|
+
DESKTOP_ICON_GRID.cellHeight) + 1);
|
|
113
|
+
const positions = {};
|
|
114
|
+
const taken = [];
|
|
115
|
+
for (const appId of appIds) {
|
|
116
|
+
const saved = stored[appId];
|
|
117
|
+
if (!saved) {
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
const position = clampDesktopIconPosition(saved, DESKTOP_ICON_SIZE, surface);
|
|
121
|
+
if (!taken.some((other) => buries(other, position))) {
|
|
122
|
+
positions[appId] = position;
|
|
123
|
+
taken.push(position);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
let slot = 0;
|
|
127
|
+
for (const appId of appIds) {
|
|
128
|
+
if (positions[appId]) {
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
let candidate = gridSlot(slot, rows);
|
|
132
|
+
while (taken.some((position) => overlaps(position, candidate))) {
|
|
133
|
+
slot += 1;
|
|
134
|
+
candidate = gridSlot(slot, rows);
|
|
135
|
+
}
|
|
136
|
+
positions[appId] = candidate;
|
|
137
|
+
taken.push(candidate);
|
|
138
|
+
slot += 1;
|
|
139
|
+
}
|
|
140
|
+
return positions;
|
|
141
|
+
}
|
|
142
|
+
//# sourceMappingURL=desktopState.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"desktopState.js","sourceRoot":"","sources":["../../../home/runner/work/tearleads/tearleads/packages/windowing/src/desktop/desktopState.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,0EAA0E;AAC1E,wEAAwE;AAaxE,MAAM,qBAAqB,GAAG,CAAC,CAAC;AAEhC,qEAAqE;AACrE,MAAM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,EAAE,CAAC;YACrB,SAAS;QACX,CAAC;QACD,IAAI,SAAS,GAAG,QAAQ,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QACrC,OAAO,KAAK,CAAC,IAAI,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,QAAQ,EAAE,SAAS,CAAC,CAAC,EAAE,CAAC;YAC/D,IAAI,IAAI,CAAC,CAAC;YACV,SAAS,GAAG,QAAQ,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QACnC,CAAC;QACD,SAAS,CAAC,KAAK,CAAC,GAAG,SAAS,CAAC;QAC7B,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QACtB,IAAI,IAAI,CAAC,CAAC;IACZ,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC","sourcesContent":["// The desktop's saved state: where each icon sits on the surface. It is kept\n// as JSON text so a host's storage adapter only moves strings, and parsed\n// defensively, since storage can hold an older, newer, or damaged copy.\n\n/** An icon's top-left corner, in pixels from the surface's top-left corner. */\nexport interface DesktopIconPosition {\n x: number;\n y: number;\n}\n\nexport interface DesktopState {\n /** Icon positions by app id. Icons with no entry take a free grid slot. */\n icons: Readonly<Record<string, DesktopIconPosition>>;\n}\n\nconst DESKTOP_STATE_VERSION = 1;\n\n/** The grid icons start on: its origin, and the size of one cell. */\nexport const DESKTOP_ICON_GRID = {\n cellHeight: 96,\n cellWidth: 88,\n originX: 12,\n originY: 12,\n} as const;\n\nexport const EMPTY_DESKTOP_STATE: DesktopState = { icons: {} };\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n\nfunction readPosition(value: unknown): DesktopIconPosition | null {\n if (!isRecord(value)) {\n return null;\n }\n const { x, y } = value;\n if (typeof x !== \"number\" || typeof y !== \"number\") {\n return null;\n }\n if (!Number.isFinite(x) || !Number.isFinite(y)) {\n return null;\n }\n return { x: Math.max(0, Math.round(x)), y: Math.max(0, Math.round(y)) };\n}\n\n/**\n * Reads stored desktop state. Anything unreadable (no text, bad JSON, another\n * version, a malformed position) yields what can be kept, or the empty state.\n */\nexport function parseDesktopState(serialized: string | null): DesktopState {\n if (serialized === null) {\n return EMPTY_DESKTOP_STATE;\n }\n let parsed: unknown;\n try {\n parsed = JSON.parse(serialized);\n } catch {\n return EMPTY_DESKTOP_STATE;\n }\n if (!isRecord(parsed)) {\n return EMPTY_DESKTOP_STATE;\n }\n const { icons: storedIcons, version } = parsed;\n if (version !== DESKTOP_STATE_VERSION) {\n return EMPTY_DESKTOP_STATE;\n }\n const icons: Record<string, DesktopIconPosition> = {};\n if (isRecord(storedIcons)) {\n for (const [appId, value] of Object.entries(storedIcons)) {\n const position = readPosition(value);\n if (position) {\n icons[appId] = position;\n }\n }\n }\n return { icons };\n}\n\nexport function serializeDesktopState(state: DesktopState): string {\n return JSON.stringify({ icons: state.icons, version: DESKTOP_STATE_VERSION });\n}\n\nfunction gridSlot(index: number, rows: number): DesktopIconPosition {\n const column = Math.floor(index / rows);\n const row = index % rows;\n return {\n x: DESKTOP_ICON_GRID.originX + column * DESKTOP_ICON_GRID.cellWidth,\n y: DESKTOP_ICON_GRID.originY + row * DESKTOP_ICON_GRID.cellHeight,\n };\n}\n\nfunction overlaps(a: DesktopIconPosition, b: DesktopIconPosition): boolean {\n return (\n Math.abs(a.x - b.x) < DESKTOP_ICON_GRID.cellWidth &&\n Math.abs(a.y - b.y) < DESKTOP_ICON_GRID.cellHeight\n );\n}\n\n// Whether one icon hides most of another: their boxes are offset by less than\n// half a grid cell on both axes.\nfunction buries(a: DesktopIconPosition, b: DesktopIconPosition): boolean {\n return (\n Math.abs(a.x - b.x) < DESKTOP_ICON_GRID.cellWidth / 2 &&\n Math.abs(a.y - b.y) < DESKTOP_ICON_GRID.cellHeight / 2\n );\n}\n\n/** The surface's size; zero while it is not laid out yet. */\nexport interface DesktopSurfaceSize {\n height: number;\n width: number;\n}\n\n/** An icon's box (DesktopIcons.css). */\nexport const DESKTOP_ICON_SIZE: DesktopSurfaceSize = { height: 88, width: 80 };\n\nfunction clampAxis(value: number, extent: number, surface: number): number {\n if (surface <= 0) {\n return Math.max(0, value);\n }\n return Math.max(0, Math.min(value, surface - extent));\n}\n\n/**\n * Keeps an icon of `size` on the surface. An unmeasured (zero) surface only\n * keeps it off negative coordinates.\n */\nexport function clampDesktopIconPosition(\n position: DesktopIconPosition,\n size: DesktopSurfaceSize,\n surface: DesktopSurfaceSize,\n): DesktopIconPosition {\n return {\n x: clampAxis(position.x, size.width, surface.width),\n y: clampAxis(position.y, size.height, surface.height),\n };\n}\n\n/**\n * Where every icon sits on the surface. A stored position is kept, moved onto\n * the surface if it was saved on a larger one, unless an icon earlier in\n * `appIds` already buries it there. Every other icon takes the first grid\n * slot, filled top to bottom then left to right, that no icon covers. The\n * surface height sets how many rows a column holds (at least one).\n */\nexport function resolveDesktopIconPositions(\n appIds: ReadonlyArray<string>,\n stored: DesktopState[\"icons\"],\n surface: DesktopSurfaceSize,\n): Record<string, DesktopIconPosition> {\n // A row fits while the icon's own box does; the cell's spacing below the\n // last one may fall off the surface.\n const rows = Math.max(\n 1,\n Math.floor(\n (surface.height - DESKTOP_ICON_GRID.originY - DESKTOP_ICON_SIZE.height) /\n DESKTOP_ICON_GRID.cellHeight,\n ) + 1,\n );\n const positions: Record<string, DesktopIconPosition> = {};\n const taken: DesktopIconPosition[] = [];\n for (const appId of appIds) {\n const saved = stored[appId];\n if (!saved) {\n continue;\n }\n const position = clampDesktopIconPosition(\n saved,\n DESKTOP_ICON_SIZE,\n surface,\n );\n if (!taken.some((other) => buries(other, position))) {\n positions[appId] = position;\n taken.push(position);\n }\n }\n let slot = 0;\n for (const appId of appIds) {\n if (positions[appId]) {\n continue;\n }\n let candidate = gridSlot(slot, rows);\n while (taken.some((position) => overlaps(position, candidate))) {\n slot += 1;\n candidate = gridSlot(slot, rows);\n }\n positions[appId] = candidate;\n taken.push(candidate);\n slot += 1;\n }\n return positions;\n}\n"]}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** Reads and writes the desktop's serialized state. */
|
|
2
|
+
export interface DesktopStateStorage {
|
|
3
|
+
/** The stored text, or null when nothing is stored yet. */
|
|
4
|
+
read: () => Promise<string | null>;
|
|
5
|
+
/** Replaces the stored text. */
|
|
6
|
+
write: (serialized: string) => Promise<void>;
|
|
7
|
+
}
|
|
8
|
+
/** Keeps the state in memory, for the life of the adapter. */
|
|
9
|
+
export declare function createMemoryDesktopStateStorage(initial?: string | null): DesktopStateStorage;
|
|
10
|
+
export interface OpfsDesktopStateStorageOptions {
|
|
11
|
+
/** Directories below the origin's root that hold the file, outermost first. */
|
|
12
|
+
directory?: ReadonlyArray<string> | undefined;
|
|
13
|
+
/** The file's name. Defaults to `desktop-state.json`. */
|
|
14
|
+
fileName?: string | undefined;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Keeps the state as a file in the origin private file system (OPFS). Reading
|
|
18
|
+
* a file or directory that does not exist yet yields null; any other failure,
|
|
19
|
+
* including a browser without OPFS, rejects.
|
|
20
|
+
*/
|
|
21
|
+
export declare function createOpfsDesktopStateStorage({ directory, fileName, }?: OpfsDesktopStateStorageOptions): DesktopStateStorage;
|