@tearleads/windowing 0.2.22 → 0.2.23
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 +34 -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/index.d.ts +4 -0
- package/index.js +5 -2
- package/index.js.map +1 -1
- package/package.json +1 -1
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
|
|
@@ -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":";AAG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AAS,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":"Des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AAC;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,CAAC,MAAM,iBAAiB,GAAG;IAC/B,UAAU,EAAE,EAAE;IACd,SAAS,EAAE,EAAE;IACb,OAAO,EAAE,EAAE;IACX,OAAO,EAAE,EAAE;CACH,CAAC;AAEX,MAAM,CAAC,MAAM,mBAAmB,GAAiB,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC;AAE/D,SAAS,QAAQ,CAAC,KAAc;IAC9B,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAC9E,CAAC;AAED,SAAS,YAAY,CAAC,KAAc;IAClC,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;QACrB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,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;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// Where the desktop keeps its state. The desktop reads and writes one JSON
|
|
2
|
+
// document through an adapter, so each host chooses the medium: an encrypted
|
|
3
|
+
// store, the origin private file system, or memory.
|
|
4
|
+
/** Keeps the state in memory, for the life of the adapter. */
|
|
5
|
+
export function createMemoryDesktopStateStorage(initial = null) {
|
|
6
|
+
let stored = initial;
|
|
7
|
+
return {
|
|
8
|
+
read: async () => stored,
|
|
9
|
+
write: async (serialized) => {
|
|
10
|
+
stored = serialized;
|
|
11
|
+
},
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
function opfsRoot() {
|
|
15
|
+
const storage = typeof navigator === "object" ? navigator.storage : undefined;
|
|
16
|
+
if (typeof storage?.getDirectory !== "function") {
|
|
17
|
+
return Promise.reject(new Error("The origin private file system is not available."));
|
|
18
|
+
}
|
|
19
|
+
return storage.getDirectory();
|
|
20
|
+
}
|
|
21
|
+
function isNotFound(error) {
|
|
22
|
+
return error instanceof DOMException && error.name === "NotFoundError";
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Keeps the state as a file in the origin private file system (OPFS). Reading
|
|
26
|
+
* a file or directory that does not exist yet yields null; any other failure,
|
|
27
|
+
* including a browser without OPFS, rejects.
|
|
28
|
+
*/
|
|
29
|
+
export function createOpfsDesktopStateStorage({ directory = [], fileName = "desktop-state.json", } = {}) {
|
|
30
|
+
async function openDirectory(create) {
|
|
31
|
+
let handle = await opfsRoot();
|
|
32
|
+
for (const name of directory) {
|
|
33
|
+
handle = await handle.getDirectoryHandle(name, { create });
|
|
34
|
+
}
|
|
35
|
+
return handle;
|
|
36
|
+
}
|
|
37
|
+
return {
|
|
38
|
+
read: async () => {
|
|
39
|
+
try {
|
|
40
|
+
const parent = await openDirectory(false);
|
|
41
|
+
const file = await (await parent.getFileHandle(fileName)).getFile();
|
|
42
|
+
return await file.text();
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
if (isNotFound(error)) {
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
throw error;
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
write: async (serialized) => {
|
|
52
|
+
const parent = await openDirectory(true);
|
|
53
|
+
const handle = await parent.getFileHandle(fileName, { create: true });
|
|
54
|
+
const writable = await handle.createWritable();
|
|
55
|
+
try {
|
|
56
|
+
await writable.write(serialized);
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
await writable.abort().catch(() => { });
|
|
60
|
+
throw error;
|
|
61
|
+
}
|
|
62
|
+
await writable.close();
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=desktopStateStorage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"desktopStateStorage.js","sourceRoot":"","sources":["../../../home/runner/work/tearleads/tearleads/packages/windowing/src/desktop/desktopStateStorage.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,6EAA6E;AAC7E,oDAAoD;AAUpD,8DAA8D;AAC9D,MAAM,UAAU,+BAA+B,CAC7C,UAAyB,IAAI;IAE7B,IAAI,MAAM,GAAG,OAAO,CAAC;IACrB,OAAO;QACL,IAAI,EAAE,KAAK,IAAI,EAAE,CAAC,MAAM;QACxB,KAAK,EAAE,KAAK,EAAE,UAAU,EAAE,EAAE;YAC1B,MAAM,GAAG,UAAU,CAAC;QACtB,CAAC;KACF,CAAC;AACJ,CAAC;AAaD,SAAS,QAA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AC;gBACD,MAAM,KAAK,CAAC;YACd,CAAC;QACH,CAAC;QACD,KAAK,EAAE,KAAK,EAAE,UAAU,EAAE,EAAE;YAC1B,MAAM,MAAM,GAAG,MAAM,aAAa,CAAC,IAAI,CAAC,CAAC;YACzC,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,aAAa,CAAC,QAAQ,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC;YACtE,MAAM,QAAQ,GAAG,MAAM,MAAM,CAAC,cAAc,EAAE,CAAC;YAC/C,IAAI,CAAC;gBACH,MAAM,QAAQ,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;YACnC,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,MAAM,QAAQ,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;gBACvC,MAAM,KAAK,CAAC;YACd,CAAC;YACD,MAAM,QAAQ,CAAC,KAAK,EAAE,CAAC;QACzB,CAAC;KACF,CAAC;AACJ,CAAC","sourcesContent":["// Where the desktop keeps its state. The desktop reads and writes one JSON\n// document through an adapter, so each host chooses the medium: an encrypted\n// store, the origin private file system, or memory.\n\n/** Reads and writes the desktop's serialized state. */\nexport interface DesktopStateStorage {\n /** The stored text, or null when nothing is stored yet. */\n read: () => Promise<string | null>;\n /** Replaces the stored text. */\n write: (serialized: string) => Promise<void>;\n}\n\n/** Keeps the state in memory, for the life of the adapter. */\nexport function createMemoryDesktopStateStorage(\n initial: string | null = null,\n): DesktopStateStorage {\n let stored = initial;\n return {\n read: async () => stored,\n write: async (serialized) => {\n stored = serialized;\n },\n };\n}\n\nexport interface OpfsDesktopStateStorageOptions {\n /** Directories below the origin's root that hold the file, outermost first. */\n directory?: ReadonlyArray<string> | undefined;\n /** The file's name. Defaults to `desktop-state.json`. */\n fileName?: string | undefined;\n}\n\ninterface StorageWithDirectory {\n getDirectory: () => Promise<FileSystemDirectoryHandle>;\n}\n\nfunction opfsRoot(): Promise<FileSystemDirectoryHandle> {\n const storage: Partial<StorageWithDirectory> | undefined =\n typeof navigator === \"object\" ? navigator.storage : undefined;\n if (typeof storage?.getDirectory !== \"function\") {\n return Promise.reject(\n new Error(\"The origin private file system is not available.\"),\n );\n }\n return storage.getDirectory();\n}\n\nfunction isNotFound(error: unknown): boolean {\n return error instanceof DOMException && error.name === \"NotFoundError\";\n}\n\n/**\n * Keeps the state as a file in the origin private file system (OPFS). Reading\n * a file or directory that does not exist yet yields null; any other failure,\n * including a browser without OPFS, rejects.\n */\nexport function createOpfsDesktopStateStorage({\n directory = [],\n fileName = \"desktop-state.json\",\n}: OpfsDesktopStateStorageOptions = {}): DesktopStateStorage {\n async function openDirectory(create: boolean) {\n let handle = await opfsRoot();\n for (const name of directory) {\n handle = await handle.getDirectoryHandle(name, { create });\n }\n return handle;\n }\n\n return {\n read: async () => {\n try {\n const parent = await openDirectory(false);\n const file = await (await parent.getFileHandle(fileName)).getFile();\n return await file.text();\n } catch (error) {\n if (isNotFound(error)) {\n return null;\n }\n throw error;\n }\n },\n write: async (serialized) => {\n const parent = await openDirectory(true);\n const handle = await parent.getFileHandle(fileName, { create: true });\n const writable = await handle.createWritable();\n try {\n await writable.write(serialized);\n } catch (error) {\n await writable.abort().catch(() => {});\n throw error;\n }\n await writable.close();\n },\n };\n}\n"]}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type PointerEvent } from "react";
|
|
2
|
+
import type { DesktopIconPosition, DesktopSurfaceSize } from "./desktopState.js";
|
|
3
|
+
/** Keeps a position on the surface, given the icon's rendered size. */
|
|
4
|
+
export type ClampDesktopIcon = (position: DesktopIconPosition, size: DesktopSurfaceSize) => DesktopIconPosition;
|
|
5
|
+
/**
|
|
6
|
+
* Moves an icon with any pointer: the primary press selects it and captures
|
|
7
|
+
* the pointer, the icon follows once the pointer passes the drag threshold,
|
|
8
|
+
* and the release commits where it landed. A cancelled gesture snaps back.
|
|
9
|
+
*/
|
|
10
|
+
export declare function useDesktopIconDrag({ clamp, onMove, onSelect, position, }: {
|
|
11
|
+
clamp: ClampDesktopIcon;
|
|
12
|
+
onMove: (position: DesktopIconPosition) => void;
|
|
13
|
+
onSelect: () => void;
|
|
14
|
+
position: DesktopIconPosition;
|
|
15
|
+
}): {
|
|
16
|
+
dragPosition: DesktopIconPosition | null;
|
|
17
|
+
handlers: {
|
|
18
|
+
onLostPointerCapture: () => void;
|
|
19
|
+
onPointerCancel: () => void;
|
|
20
|
+
onPointerDown: (event: PointerEvent<HTMLElement>) => void;
|
|
21
|
+
onPointerMove: (event: PointerEvent<HTMLElement>) => void;
|
|
22
|
+
onPointerUp: (event: PointerEvent<HTMLElement>) => void;
|
|
23
|
+
};
|
|
24
|
+
};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { useRef, useState } from "react";
|
|
2
|
+
// A press becomes a drag once the pointer travels this far, so a slightly
|
|
3
|
+
// shaky click or double-click does not move the icon.
|
|
4
|
+
const DRAG_THRESHOLD_PX = 4;
|
|
5
|
+
function sizeOf(element) {
|
|
6
|
+
return { height: element.offsetHeight, width: element.offsetWidth };
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Moves an icon with any pointer: the primary press selects it and captures
|
|
10
|
+
* the pointer, the icon follows once the pointer passes the drag threshold,
|
|
11
|
+
* and the release commits where it landed. A cancelled gesture snaps back.
|
|
12
|
+
*/
|
|
13
|
+
export function useDesktopIconDrag({ clamp, onMove, onSelect, position, }) {
|
|
14
|
+
const dragRef = useRef(null);
|
|
15
|
+
const [dragPosition, setDragPosition] = useState(null);
|
|
16
|
+
const dragTo = (event, drag) => clamp({
|
|
17
|
+
x: Math.round(drag.origin.x + event.clientX - drag.startX),
|
|
18
|
+
y: Math.round(drag.origin.y + event.clientY - drag.startY),
|
|
19
|
+
}, sizeOf(event.currentTarget));
|
|
20
|
+
const activeDrag = (event) => {
|
|
21
|
+
const drag = dragRef.current;
|
|
22
|
+
return drag && drag.pointerId === event.pointerId ? drag : null;
|
|
23
|
+
};
|
|
24
|
+
const cancel = () => {
|
|
25
|
+
dragRef.current = null;
|
|
26
|
+
setDragPosition(null);
|
|
27
|
+
};
|
|
28
|
+
const handlers = {
|
|
29
|
+
onLostPointerCapture: cancel,
|
|
30
|
+
onPointerCancel: cancel,
|
|
31
|
+
onPointerDown: (event) => {
|
|
32
|
+
if (event.button !== 0) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
onSelect();
|
|
36
|
+
dragRef.current = {
|
|
37
|
+
moved: false,
|
|
38
|
+
origin: position,
|
|
39
|
+
pointerId: event.pointerId,
|
|
40
|
+
startX: event.clientX,
|
|
41
|
+
startY: event.clientY,
|
|
42
|
+
};
|
|
43
|
+
event.currentTarget.setPointerCapture?.(event.pointerId);
|
|
44
|
+
},
|
|
45
|
+
onPointerMove: (event) => {
|
|
46
|
+
const drag = activeDrag(event);
|
|
47
|
+
if (!drag) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
const distance = Math.hypot(event.clientX - drag.startX, event.clientY - drag.startY);
|
|
51
|
+
if (!drag.moved && distance < DRAG_THRESHOLD_PX) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
drag.moved = true;
|
|
55
|
+
setDragPosition(dragTo(event, drag));
|
|
56
|
+
},
|
|
57
|
+
onPointerUp: (event) => {
|
|
58
|
+
const drag = activeDrag(event);
|
|
59
|
+
if (!drag) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
cancel();
|
|
63
|
+
if (drag.moved) {
|
|
64
|
+
onMove(dragTo(event, drag));
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
return { dragPosition, handlers };
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=useDesktopIconDrag.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useDesktopIconDrag.js","sourceRoot":"","sources":["../../../home/runner/work/tearleads/tearleads/packages/windowing/src/desktop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,IAAI,EAAE,CAAC;gBACV,OAAO;YACT,CAAC;YACD,MAAM,EAAE,CAAC;YACT,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;gBACf,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC,CAAC;YAC9B,CAAC;QACH,CAAC;KACF,CAAC;IAEF,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,CAAC;AACpC,CAAC","sourcesContent":["import { type PointerEvent, useRef, useState } from \"react\";\nimport type { DesktopIconPosition, DesktopSurfaceSize } from \"./desktopState\";\n\n// A press becomes a drag once the pointer travels this far, so a slightly\n// shaky click or double-click does not move the icon.\nconst DRAG_THRESHOLD_PX = 4;\n\ninterface DragStart {\n moved: boolean;\n origin: DesktopIconPosition;\n pointerId: number;\n startX: number;\n startY: number;\n}\n\n/** Keeps a position on the surface, given the icon's rendered size. */\nexport type ClampDesktopIcon = (\n position: DesktopIconPosition,\n size: DesktopSurfaceSize,\n) => DesktopIconPosition;\n\nfunction sizeOf(element: HTMLElement): DesktopSurfaceSize {\n return { height: element.offsetHeight, width: element.offsetWidth };\n}\n\n/**\n * Moves an icon with any pointer: the primary press selects it and captures\n * the pointer, the icon follows once the pointer passes the drag threshold,\n * and the release commits where it landed. A cancelled gesture snaps back.\n */\nexport function useDesktopIconDrag({\n clamp,\n onMove,\n onSelect,\n position,\n}: {\n clamp: ClampDesktopIcon;\n onMove: (position: DesktopIconPosition) => void;\n onSelect: () => void;\n position: DesktopIconPosition;\n}) {\n const dragRef = useRef<DragStart | null>(null);\n const [dragPosition, setDragPosition] = useState<DesktopIconPosition | null>(\n null,\n );\n\n const dragTo = (event: PointerEvent<HTMLElement>, drag: DragStart) =>\n clamp(\n {\n x: Math.round(drag.origin.x + event.clientX - drag.startX),\n y: Math.round(drag.origin.y + event.clientY - drag.startY),\n },\n sizeOf(event.currentTarget),\n );\n\n const activeDrag = (event: PointerEvent<HTMLElement>) => {\n const drag = dragRef.current;\n return drag && drag.pointerId === event.pointerId ? drag : null;\n };\n\n const cancel = () => {\n dragRef.current = null;\n setDragPosition(null);\n };\n\n const handlers = {\n onLostPointerCapture: cancel,\n onPointerCancel: cancel,\n onPointerDown: (event: PointerEvent<HTMLElement>) => {\n if (event.button !== 0) {\n return;\n }\n onSelect();\n dragRef.current = {\n moved: false,\n origin: position,\n pointerId: event.pointerId,\n startX: event.clientX,\n startY: event.clientY,\n };\n event.currentTarget.setPointerCapture?.(event.pointerId);\n },\n onPointerMove: (event: PointerEvent<HTMLElement>) => {\n const drag = activeDrag(event);\n if (!drag) {\n return;\n }\n const distance = Math.hypot(\n event.clientX - drag.startX,\n event.clientY - drag.startY,\n );\n if (!drag.moved && distance < DRAG_THRESHOLD_PX) {\n return;\n }\n drag.moved = true;\n setDragPosition(dragTo(event, drag));\n },\n onPointerUp: (event: PointerEvent<HTMLElement>) => {\n const drag = activeDrag(event);\n if (!drag) {\n return;\n }\n cancel();\n if (drag.moved) {\n onMove(dragTo(event, drag));\n }\n },\n };\n\n return { dragPosition, handlers };\n}\n"]}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { DesktopIconStep } from "./DesktopIcon.js";
|
|
2
|
+
/**
|
|
3
|
+
* Which icon is selected. A press anywhere but an icon clears it, and steps
|
|
4
|
+
* move it (and focus) along the icons' order. One icon is tabbable: the
|
|
5
|
+
* selected one, or else the first.
|
|
6
|
+
*/
|
|
7
|
+
export declare function useDesktopIconSelection<AppId extends string>(layer: HTMLElement | null, appIds: ReadonlyArray<AppId>): {
|
|
8
|
+
selected: AppId | null;
|
|
9
|
+
setSelected: import("react").Dispatch<import("react").SetStateAction<AppId | null>>;
|
|
10
|
+
step: (from: AppId, direction: DesktopIconStep) => void;
|
|
11
|
+
tabbableId: AppId | undefined;
|
|
12
|
+
};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { useCallback, useEffect, useState } from "react";
|
|
2
|
+
/**
|
|
3
|
+
* Which icon is selected. A press anywhere but an icon clears it, and steps
|
|
4
|
+
* move it (and focus) along the icons' order. One icon is tabbable: the
|
|
5
|
+
* selected one, or else the first.
|
|
6
|
+
*/
|
|
7
|
+
export function useDesktopIconSelection(layer, appIds) {
|
|
8
|
+
const [selected, setSelected] = useState(null);
|
|
9
|
+
useEffect(() => {
|
|
10
|
+
if (!layer || selected === null) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
const handlePointerDown = (event) => {
|
|
14
|
+
const target = event.target;
|
|
15
|
+
if (target instanceof Element &&
|
|
16
|
+
layer.contains(target) &&
|
|
17
|
+
target.closest(".desktop-icon")) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
setSelected(null);
|
|
21
|
+
};
|
|
22
|
+
document.addEventListener("pointerdown", handlePointerDown);
|
|
23
|
+
return () => document.removeEventListener("pointerdown", handlePointerDown);
|
|
24
|
+
}, [layer, selected]);
|
|
25
|
+
const step = useCallback((from, direction) => {
|
|
26
|
+
const index = appIds.indexOf(from);
|
|
27
|
+
const last = appIds.length - 1;
|
|
28
|
+
const target = {
|
|
29
|
+
first: 0,
|
|
30
|
+
last,
|
|
31
|
+
next: Math.min(index + 1, last),
|
|
32
|
+
previous: Math.max(index - 1, 0),
|
|
33
|
+
}[direction];
|
|
34
|
+
const appId = appIds[target];
|
|
35
|
+
if (appId === undefined) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
setSelected(appId);
|
|
39
|
+
layer
|
|
40
|
+
?.querySelector(`[data-app-id="${CSS.escape(appId)}"]`)
|
|
41
|
+
?.focus();
|
|
42
|
+
}, [appIds, layer]);
|
|
43
|
+
const tabbableId = selected !== null && appIds.includes(selected) ? selected : appIds[0];
|
|
44
|
+
return { selected, setSelected, step, tabbableId };
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=useDesktopIconSelection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useDesktopIconSelection.js","sourceRoot":"","sources":["../../../home/runner/work/tearleads/tearleads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EAAE,IAAI,CAAC;YAC/B,QAAQ,EAAE,IAAI,CAAC,GAAG,CAAC,KAAK,GAAG,CAAC,EAAE,CAAC,CAAC;SACjC,CAAC,SAAS,CAAC,CAAC;QACb,MAAM,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC;QAC7B,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;YACxB,OAAO;QACT,CAAC;QACD,WAAW,CAAC,KAAK,CAAC,CAAC;QACnB,KAAK;YACH,EAAE,aAAa,CAAc,iBAAiB,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC;YACpE,EAAE,KAAK,EAAE,CAAC;IACd,CAAC,EACD,CAAC,MAAM,EAAE,KAAK,CAAC,CAChB,CAAC;IAEF,MAAM,UAAU,GACd,QAAQ,KAAK,IAAI,IAAI,MAAM,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;IAExE,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,IAAI,EAAE,UAAU,EAAE,CAAC;AACrD,CAAC","sourcesContent":["import { useCallback, useEffect, useState } from \"react\";\nimport type { DesktopIconStep } from \"./DesktopIcon\";\n\n/**\n * Which icon is selected. A press anywhere but an icon clears it, and steps\n * move it (and focus) along the icons' order. One icon is tabbable: the\n * selected one, or else the first.\n */\nexport function useDesktopIconSelection<AppId extends string>(\n layer: HTMLElement | null,\n appIds: ReadonlyArray<AppId>,\n) {\n const [selected, setSelected] = useState<AppId | null>(null);\n\n useEffect(() => {\n if (!layer || selected === null) {\n return;\n }\n const handlePointerDown = (event: PointerEvent) => {\n const target = event.target;\n if (\n target instanceof Element &&\n layer.contains(target) &&\n target.closest(\".desktop-icon\")\n ) {\n return;\n }\n setSelected(null);\n };\n document.addEventListener(\"pointerdown\", handlePointerDown);\n return () => document.removeEventListener(\"pointerdown\", handlePointerDown);\n }, [layer, selected]);\n\n const step = useCallback(\n (from: AppId, direction: DesktopIconStep) => {\n const index = appIds.indexOf(from);\n const last = appIds.length - 1;\n const target = {\n first: 0,\n last,\n next: Math.min(index + 1, last),\n previous: Math.max(index - 1, 0),\n }[direction];\n const appId = appIds[target];\n if (appId === undefined) {\n return;\n }\n setSelected(appId);\n layer\n ?.querySelector<HTMLElement>(`[data-app-id=\"${CSS.escape(appId)}\"]`)\n ?.focus();\n },\n [appIds, layer],\n );\n\n const tabbableId =\n selected !== null && appIds.includes(selected) ? selected : appIds[0];\n\n return { selected, setSelected, step, tabbableId };\n}\n"]}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type DesktopIconPosition } from "./desktopState.js";
|
|
2
|
+
import type { DesktopStateStorage } from "./desktopStateStorage.js";
|
|
3
|
+
/**
|
|
4
|
+
* The desktop's icon positions, loaded from `storage` and written back after
|
|
5
|
+
* every move. A new adapter (another account's store, say) starts over from
|
|
6
|
+
* what it holds.
|
|
7
|
+
*
|
|
8
|
+
* Moves made before the load finishes win over the stored positions, and are
|
|
9
|
+
* written only once the load has merged them, so an early move never replaces
|
|
10
|
+
* the stored state with a partial one. When the read fails the positions stay
|
|
11
|
+
* in memory and nothing is written, so a passing failure cannot erase a saved
|
|
12
|
+
* layout.
|
|
13
|
+
*/
|
|
14
|
+
export declare function useDesktopIconState(storage: DesktopStateStorage): {
|
|
15
|
+
icons: Readonly<Record<string, DesktopIconPosition>>;
|
|
16
|
+
moveIcon: (appId: string, position: DesktopIconPosition) => void;
|
|
17
|
+
};
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
2
|
+
import { parseDesktopState, serializeDesktopState, } from "./desktopState.js";
|
|
3
|
+
const NO_ICONS = {};
|
|
4
|
+
// Each adapter's last queued write. A desktop that remounts (a layout switch,
|
|
5
|
+
// say) while a write is pending reads only after it lands, so it never loads
|
|
6
|
+
// the layout from before the last move.
|
|
7
|
+
const pendingWrites = new WeakMap();
|
|
8
|
+
function queueWrite(storage, serialized) {
|
|
9
|
+
const write = (pendingWrites.get(storage) ?? Promise.resolve())
|
|
10
|
+
.then(() => storage.write(serialized))
|
|
11
|
+
.catch(() => { });
|
|
12
|
+
pendingWrites.set(storage, write);
|
|
13
|
+
}
|
|
14
|
+
function readAfterWrites(storage) {
|
|
15
|
+
return (pendingWrites.get(storage) ?? Promise.resolve()).then(() => storage.read());
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The desktop's icon positions, loaded from `storage` and written back after
|
|
19
|
+
* every move. A new adapter (another account's store, say) starts over from
|
|
20
|
+
* what it holds.
|
|
21
|
+
*
|
|
22
|
+
* Moves made before the load finishes win over the stored positions, and are
|
|
23
|
+
* written only once the load has merged them, so an early move never replaces
|
|
24
|
+
* the stored state with a partial one. When the read fails the positions stay
|
|
25
|
+
* in memory and nothing is written, so a passing failure cannot erase a saved
|
|
26
|
+
* layout.
|
|
27
|
+
*/
|
|
28
|
+
export function useDesktopIconState(storage) {
|
|
29
|
+
const [icons, setIcons] = useState(NO_ICONS);
|
|
30
|
+
const iconsRef = useRef(NO_ICONS);
|
|
31
|
+
const movedRef = useRef(new Set());
|
|
32
|
+
const writableRef = useRef(false);
|
|
33
|
+
const publish = useCallback((next) => {
|
|
34
|
+
iconsRef.current = next;
|
|
35
|
+
setIcons(next);
|
|
36
|
+
}, []);
|
|
37
|
+
// Writes run one at a time, in order, so the last move is the one kept.
|
|
38
|
+
const save = useCallback((next) => queueWrite(storage, serializeDesktopState({ icons: next })), [storage]);
|
|
39
|
+
useEffect(() => {
|
|
40
|
+
let current = true;
|
|
41
|
+
movedRef.current = new Set();
|
|
42
|
+
writableRef.current = false;
|
|
43
|
+
publish(NO_ICONS);
|
|
44
|
+
readAfterWrites(storage).then((serialized) => {
|
|
45
|
+
if (!current) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
const merged = {
|
|
49
|
+
...parseDesktopState(serialized).icons,
|
|
50
|
+
};
|
|
51
|
+
for (const appId of movedRef.current) {
|
|
52
|
+
const position = iconsRef.current[appId];
|
|
53
|
+
if (position) {
|
|
54
|
+
merged[appId] = position;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
writableRef.current = true;
|
|
58
|
+
publish(merged);
|
|
59
|
+
if (movedRef.current.size > 0) {
|
|
60
|
+
save(merged);
|
|
61
|
+
}
|
|
62
|
+
}, () => { });
|
|
63
|
+
return () => {
|
|
64
|
+
current = false;
|
|
65
|
+
};
|
|
66
|
+
}, [publish, save, storage]);
|
|
67
|
+
const moveIcon = useCallback((appId, position) => {
|
|
68
|
+
movedRef.current.add(appId);
|
|
69
|
+
const next = { ...iconsRef.current, [appId]: position };
|
|
70
|
+
publish(next);
|
|
71
|
+
if (writableRef.current) {
|
|
72
|
+
save(next);
|
|
73
|
+
}
|
|
74
|
+
}, [publish, save]);
|
|
75
|
+
return { icons, moveIcon };
|
|
76
|
+
}
|
|
77
|
+
//# sourceMappingURL=useDesktopIconState.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useDesktopIconState.js","sourceRoot":"","sources":["../../../home/runner/work/tearleads/tearleads/packages/windowing/src/desktop/useDesktopIconState.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AACjE,OAAO,EAGL,iBAAiB,EACjB,qBAAqB,GACtB,MAAM,gBAAgB,CAAC;AAKxB,MAAM,QAAQ,GAAkB,EAAE,CAAC;AAEnC,8EAA8E;AAC9E,6EAA6E;AAC7E,wCAAwC;AACxC,MAAM,aAAa,GAAG,IAAI,OAAO,EAAsC,CAAC;AAExE,SAAS,UAAU,CAAC,OAA4B,EAAE,UAAkB;IAClE,MAAM,KAAK,GAAG,CAAC,aAAa,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC;SAC5D,IAAI,CAAC,GAAG,EAAE,CAAC,OA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CAAC,CAAC;QACb,CAAC;IACH,CAAC,EACD,CAAC,OAAO,EAAE,IAAI,CAAC,CAChB,CAAC;IAEF,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,CAAC;AAC7B,CAAC","sourcesContent":["import { useCallback, useEffect, useRef, useState } from \"react\";\nimport {\n type DesktopIconPosition,\n type DesktopState,\n parseDesktopState,\n serializeDesktopState,\n} from \"./desktopState\";\nimport type { DesktopStateStorage } from \"./desktopStateStorage\";\n\ntype IconPositions = DesktopState[\"icons\"];\n\nconst NO_ICONS: IconPositions = {};\n\n// Each adapter's last queued write. A desktop that remounts (a layout switch,\n// say) while a write is pending reads only after it lands, so it never loads\n// the layout from before the last move.\nconst pendingWrites = new WeakMap<DesktopStateStorage, Promise<void>>();\n\nfunction queueWrite(storage: DesktopStateStorage, serialized: string) {\n const write = (pendingWrites.get(storage) ?? Promise.resolve())\n .then(() => storage.write(serialized))\n .catch(() => {});\n pendingWrites.set(storage, write);\n}\n\nfunction readAfterWrites(storage: DesktopStateStorage) {\n return (pendingWrites.get(storage) ?? Promise.resolve()).then(() =>\n storage.read(),\n );\n}\n\n/**\n * The desktop's icon positions, loaded from `storage` and written back after\n * every move. A new adapter (another account's store, say) starts over from\n * what it holds.\n *\n * Moves made before the load finishes win over the stored positions, and are\n * written only once the load has merged them, so an early move never replaces\n * the stored state with a partial one. When the read fails the positions stay\n * in memory and nothing is written, so a passing failure cannot erase a saved\n * layout.\n */\nexport function useDesktopIconState(storage: DesktopStateStorage) {\n const [icons, setIcons] = useState<IconPositions>(NO_ICONS);\n const iconsRef = useRef<IconPositions>(NO_ICONS);\n const movedRef = useRef(new Set<string>());\n const writableRef = useRef(false);\n\n const publish = useCallback((next: IconPositions) => {\n iconsRef.current = next;\n setIcons(next);\n }, []);\n\n // Writes run one at a time, in order, so the last move is the one kept.\n const save = useCallback(\n (next: IconPositions) =>\n queueWrite(storage, serializeDesktopState({ icons: next })),\n [storage],\n );\n\n useEffect(() => {\n let current = true;\n movedRef.current = new Set();\n writableRef.current = false;\n publish(NO_ICONS);\n readAfterWrites(storage).then(\n (serialized) => {\n if (!current) {\n return;\n }\n const merged: Record<string, DesktopIconPosition> = {\n ...parseDesktopState(serialized).icons,\n };\n for (const appId of movedRef.current) {\n const position = iconsRef.current[appId];\n if (position) {\n merged[appId] = position;\n }\n }\n writableRef.current = true;\n publish(merged);\n if (movedRef.current.size > 0) {\n save(merged);\n }\n },\n () => {},\n );\n return () => {\n current = false;\n };\n }, [publish, save, storage]);\n\n const moveIcon = useCallback(\n (appId: string, position: DesktopIconPosition) => {\n movedRef.current.add(appId);\n const next = { ...iconsRef.current, [appId]: position };\n publish(next);\n if (writableRef.current) {\n save(next);\n }\n },\n [publish, save],\n );\n\n return { icons, moveIcon };\n}\n"]}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type DesktopIconPosition, type DesktopSurfaceSize } from "./desktopState.js";
|
|
2
|
+
/**
|
|
3
|
+
* The surface the icons lie on, measured from the icon layer, which fills
|
|
4
|
+
* it, and a clamp that keeps an icon of a given size on it. Before the layer
|
|
5
|
+
* is laid out (a zero size) positions are only kept off negative coordinates.
|
|
6
|
+
*/
|
|
7
|
+
export declare function useDesktopSurface(layer: HTMLElement | null): {
|
|
8
|
+
clamp: (position: DesktopIconPosition, size: DesktopSurfaceSize) => DesktopIconPosition;
|
|
9
|
+
surface: DesktopSurfaceSize;
|
|
10
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { useCallback, useLayoutEffect, useState } from "react";
|
|
2
|
+
import { clampDesktopIconPosition, } from "./desktopState.js";
|
|
3
|
+
const UNMEASURED = { height: 0, width: 0 };
|
|
4
|
+
/**
|
|
5
|
+
* The surface the icons lie on, measured from the icon layer, which fills
|
|
6
|
+
* it, and a clamp that keeps an icon of a given size on it. Before the layer
|
|
7
|
+
* is laid out (a zero size) positions are only kept off negative coordinates.
|
|
8
|
+
*/
|
|
9
|
+
export function useDesktopSurface(layer) {
|
|
10
|
+
const [surface, setSurface] = useState(UNMEASURED);
|
|
11
|
+
useLayoutEffect(() => {
|
|
12
|
+
if (!layer) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
const measure = () => setSurface((current) => current.width === layer.clientWidth &&
|
|
16
|
+
current.height === layer.clientHeight
|
|
17
|
+
? current
|
|
18
|
+
: { height: layer.clientHeight, width: layer.clientWidth });
|
|
19
|
+
measure();
|
|
20
|
+
const observer = new ResizeObserver(measure);
|
|
21
|
+
observer.observe(layer);
|
|
22
|
+
return () => observer.disconnect();
|
|
23
|
+
}, [layer]);
|
|
24
|
+
const clamp = useCallback((position, size) => clampDesktopIconPosition(position, size, surface), [surface]);
|
|
25
|
+
return { clamp, surface };
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=useDesktopSurface.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useDesktopSurface.js","sourceRoot":"","sources":["../../../home/runner/work/tearleads/tearleads/packages/windowing/src/desktop/useDesktopSurface.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAC/D,OAAO,EACL,wBAAwB,GAGzB,MAAM,gBAAgB,CAAC;AAExB,MAAM,UAAU,GAAuB,EAAE,MAAM,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC;AAE/D;;;;GAIG;AACH,MAAM,UAAU,iBAAiB,CAAC,KAAyB;IACzD,MAAM,CAAC,OAAO,EAAE,UAAU,CAAC,GAAG,QAAQ,CAAC,UAAU,CAAC,CAAC;IACnD,eAAe,CAAC,GAAG,EAAE;QACnB,IAAI,CAAC,KAAK,EAAE,CAAC;YACX,OAAO;QACT,CAAC;QACD,MAAM,OAAO,GAAG,GAAG,EAAE,CACnB,UAAU,CAAC,CAAC,OAAO,EAAE,EAAE,CACrB,OAAO,CAAC,KAAK,KAAK,KAAK,CAAC,WAAW;YACnC,OAAO,CAAC,MAAM,KAAK,KAAK,CAAC,YAAY;YACnC,CAAC,CAAC,OAAO;YACT,CAAC,CAAC,EAAE,MAAM,EAAE,KAAK,CAAC,YAAY,EAAE,KAAK,EAAE,KAAK,CAAC,WAAW,EAAE,CAC7D,CAAC;QACJ,OAAO,EAAE,CAAC;QACV,MAAM,QAAQ,GAAG,IAAI,cAAc,CAAC,OAAO,CAAC,CAAC;QAC7C,QAAQ,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;QACxB,OAAO,GAAG,EAAE,CAAC,QAAQ,CAAC,UAAU,EAAE,CAAC;IACrC,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;IAEZ,MAAM,KAAK,GAAG,WAAW,CACvB,CAAC,QAA6B,EAAE,IAAwB,EAAE,EAAE,CAC1D,wBAAwB,CAAC,QAAQ,EAAE,IAAI,EAAE,OAAO,CAAC,EACnD,CAAC,OAAO,CAAC,CACV,CAAC;IAEF,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC;AAC5B,CAAC","sourcesContent":["import { useCallback, useLayoutEffect, useState } from \"react\";\nimport {\n clampDesktopIconPosition,\n type DesktopIconPosition,\n type DesktopSurfaceSize,\n} from \"./desktopState\";\n\nconst UNMEASURED: DesktopSurfaceSize = { height: 0, width: 0 };\n\n/**\n * The surface the icons lie on, measured from the icon layer, which fills\n * it, and a clamp that keeps an icon of a given size on it. Before the layer\n * is laid out (a zero size) positions are only kept off negative coordinates.\n */\nexport function useDesktopSurface(layer: HTMLElement | null) {\n const [surface, setSurface] = useState(UNMEASURED);\n useLayoutEffect(() => {\n if (!layer) {\n return;\n }\n const measure = () =>\n setSurface((current) =>\n current.width === layer.clientWidth &&\n current.height === layer.clientHeight\n ? current\n : { height: layer.clientHeight, width: layer.clientWidth },\n );\n measure();\n const observer = new ResizeObserver(measure);\n observer.observe(layer);\n return () => observer.disconnect();\n }, [layer]);\n\n const clamp = useCallback(\n (position: DesktopIconPosition, size: DesktopSurfaceSize) =>\n clampDesktopIconPosition(position, size, surface),\n [surface],\n );\n\n return { clamp, surface };\n}\n"]}
|
package/index.d.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
export { createRequiredContext, type RequiredContext, } from "./createRequiredContext.js";
|
|
2
|
+
export type { DesktopIconMenuItem } from "./desktop/DesktopIconMenu.js";
|
|
3
|
+
export { DesktopIcons, type DesktopIconsProps, } from "./desktop/DesktopIcons.js";
|
|
4
|
+
export type { DesktopIconPosition, DesktopState, } from "./desktop/desktopState.js";
|
|
5
|
+
export { createMemoryDesktopStateStorage, createOpfsDesktopStateStorage, type DesktopStateStorage, type OpfsDesktopStateStorageOptions, } from "./desktop/desktopStateStorage.js";
|
|
2
6
|
export { type WindowingIcons, WindowingIconsProvider, } from "./icons/WindowingIcons.js";
|
|
3
7
|
export type { WindowingIcon, WindowingIconProps } from "./icons/windowingIcon.js";
|
|
4
8
|
export { ROUTED_TABLET_BREAKPOINT_PX, ROUTED_TABLET_QUERY, WINDOWED_LAYOUT_MIN_WIDTH_PX, } from "./launcher/breakpoints.js";
|
package/index.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import "./tokens.css";
|
|
2
2
|
// The public windowing API: window state, window chrome and its registration
|
|
3
3
|
// hooks, the menu and sidebar primitives the chrome renders with, a taskbar's
|
|
4
|
-
// start menu and theme switch, the icons the chrome draws,
|
|
5
|
-
// mini-apps that shows them in windows or in the routed (iPad / phone) shell
|
|
4
|
+
// start menu and theme switch, the icons the chrome draws, a launcher of
|
|
5
|
+
// mini-apps that shows them in windows or in the routed (iPad / phone) shell,
|
|
6
|
+
// and desktop icons that open them, with a storage adapter for their layout.
|
|
6
7
|
export { createRequiredContext, } from "./createRequiredContext.js";
|
|
8
|
+
export { DesktopIcons, } from "./desktop/DesktopIcons.js";
|
|
9
|
+
export { createMemoryDesktopStateStorage, createOpfsDesktopStateStorage, } from "./desktop/desktopStateStorage.js";
|
|
7
10
|
export { WindowingIconsProvider, } from "./icons/WindowingIcons.js";
|
|
8
11
|
export { ROUTED_TABLET_BREAKPOINT_PX, ROUTED_TABLET_QUERY, WINDOWED_LAYOUT_MIN_WIDTH_PX, } from "./launcher/breakpoints.js";
|
|
9
12
|
export { DEFAULT_MINI_APP_POSITION, LauncherNavigationProvider, useLauncherNavigationActions, useLauncherNavigationState, useMiniAppRouteSegments, useOptionalLauncherNavigationState, } from "./launcher/LauncherNavigationProvider.js";
|
package/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../home/runner/work/tearleads/tearleads/packages/windowing/src/index.ts"],"names":[],"mappings":"AAAA,OAAO,cAAc,CAAC;AAEtB,6EAA6E;AAC7E,8EAA8E;AAC9E,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../home/runner/work/tearleads/tearleads/packages/windowing/src/index.ts"],"names":[],"mappings":"AAAA,OAAO,cAAc,CAAC;AAEtB,6EAA6E;AAC7E,8EAA8E;AAC9E,yEAAyE;AACzE,8EAA8E;AAC9E,6EAA6E;AAC7E,OAAO,EACL,qBAAqB,GAEtB,MAAM,yBAAyB,CAAC;AAEjC,OAAO,EACL,YAAY,GAEb,MAAM,wBAAwB,CAAC;AAKhC,OAAO,EACL,+BAA+B,EAC/B,6BAA6B,GAG9B,MAAM,+BAA+B,CAAC;AACvC,OAAO,EAEL,sBAAsB,GACvB,MAAM,wBAAwB,CAAC;AAEhC,OAAO,EACL,2BAA2B,EAC3B,mBAAmB,EACnB,4BAA4B,GAC7B,MAAM,wBAAwB,CAAC;AAChC,OAAO,EACL,yBAAyB,EAEzB,0BAA0B,EAG1B,4BAA4B,EAC5B,0BAA0B,EAC1B,uBAAuB,EACvB,kCAAkC,GACnC,MAAM,uCAAuC,CAAC;AAC/C,OAAO,EACL,eAAe,EAGf,wBAAwB,EACxB,oBAAoB,GACrB,MAAM,+BAA+B,CAAC;AACvC,OAAO,EACL,4BAA4B,EAC5B,6BAA6B,GAC9B,MAAM,wC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C;AAC/B,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAMlD,OAAO,EACL,qBAAqB,EACrB,gBAAgB,EAChB,wBAAwB,EACxB,mBAAmB,EACnB,oBAAoB,GACrB,MAAM,+BAA+B,CAAC;AACvC,OAAO,EAAE,MAAM,EAA8B,MAAM,iBAAiB,CAAC;AACrE,OAAO,EAAE,2BAA2B,EAAE,MAAM,8BAA8B,CAAC;AAC3E,OAAO,EAAE,iBAAiB,EAAE,MAAM,4BAA4B,CAAC;AAC/D,OAAO,EACL,mBAAmB,EACnB,wBAAwB,EACxB,qBAAqB,EACrB,wBAAwB,EACxB,6BAA6B,EAC7B,uBAAuB,EACvB,wBAAwB,EACxB,2BAA2B,EAC3B,qBAAqB,EACrB,kBAAkB,GACnB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EACL,gBAAgB,EAChB,0BAA0B,EAC1B,gBAAgB,EAChB,qBAAqB,GACtB,MAAM,+BAA+B,CAAC;AACvC,OAAO,EACL,aAAa,EACb,gBAAgB,EAChB,kBAAkB,EAQlB,mBAAmB,GACpB,MAAM,8BAA8B,CAAC;AACtC,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC","sourcesContent":["import \"./tokens.css\";\n\n// The public windowing API: window state, window chrome and its registration\n// hooks, the menu and sidebar primitives the chrome renders with, a taskbar's\n// start menu and theme switch, the icons the chrome draws, a launcher of\n// mini-apps that shows them in windows or in the routed (iPad / phone) shell,\n// and desktop icons that open them, with a storage adapter for their layout.\nexport {\n createRequiredContext,\n type RequiredContext,\n} from \"./createRequiredContext\";\nexport type { DesktopIconMenuItem } from \"./desktop/DesktopIconMenu\";\nexport {\n DesktopIcons,\n type DesktopIconsProps,\n} from \"./desktop/DesktopIcons\";\nexport type {\n DesktopIconPosition,\n DesktopState,\n} from \"./desktop/desktopState\";\nexport {\n createMemoryDesktopStateStorage,\n createOpfsDesktopStateStorage,\n type DesktopStateStorage,\n type OpfsDesktopStateStorageOptions,\n} from \"./desktop/desktopStateStorage\";\nexport {\n type WindowingIcons,\n WindowingIconsProvider,\n} from \"./icons/WindowingIcons\";\nexport type { WindowingIcon, WindowingIconProps } from \"./icons/windowingIcon\";\nexport {\n ROUTED_TABLET_BREAKPOINT_PX,\n ROUTED_TABLET_QUERY,\n WINDOWED_LAYOUT_MIN_WIDTH_PX,\n} from \"./launcher/breakpoints\";\nexport {\n DEFAULT_MINI_APP_POSITION,\n type LauncherNavigationActions,\n LauncherNavigationProvider,\n type LauncherNavigationState,\n type OpenMiniAppRequest,\n useLauncherNavigationActions,\n useLauncherNavigationState,\n useMiniAppRouteSegments,\n useOptionalLauncherNavigationState,\n} from \"./launcher/LauncherNavigationProvider\";\nexport {\n isLauncherAppId,\n type LauncherDefinition,\n type MiniAppDefinition,\n resolveLauncherHomeAppId,\n resolveLauncherOrder,\n} from \"./launcher/launcherDefinition\";\nexport {\n MiniAppRouteSegmentsProvider,\n useMiniAppWindowRouteSegments,\n} from \"./launcher/MiniAppRouteSegmentsContext\";\nexport { type MiniAppBoundary, MiniAppWindow } from \"./launcher/MiniAppWindow\";\nexport {\n NavigationModeOverrideProvider,\n useNavigationModeOverride,\n useOptionalNavigationModeOverride,\n} from \"./launcher/NavigationModeOverrideProvider\";\nexport { NavigationModeSwitch } from \"./launcher/NavigationModeSwitch\";\nexport {\n isWindowedLayoutEligible,\n type NavigationEnvironment,\n type NavigationMode,\n readNavigationEnvironment,\n resolveNavigationMode,\n} from \"./launcher/navigationMode\";\nexport {\n buildMiniAppPath,\n type LauncherRoute,\n parseLauncherRoute,\n} from \"./launcher/routePaths\";\nexport {\n resolveActiveLauncherRoute,\n useActiveLauncherRoute,\n} from \"./launcher/useActiveLauncherRoute\";\nexport { useCompactRoutedMode } from \"./launcher/useCompactRoutedMode\";\nexport {\n type MiniAppRouteSetOptions,\n useMiniAppRouteState,\n} from \"./launcher/useMiniAppRouteState\";\nexport { useNavigationMode } from \"./launcher/useNavigationMode\";\nexport { useNavigationModeDocumentAttribute } from \"./launcher/useNavigationModeDocumentAttribute\";\nexport {\n useRoutedLayoutActive,\n useWindowedLayoutActive,\n} from \"./launcher/useRoutedLayoutActive\";\nexport {\n type RoutedLayoutTier,\n useRoutedLayoutTier,\n} from \"./launcher/useRoutedLayoutTier\";\nexport { Menu, type MenuPosition } from \"./menu/Menu\";\nexport { MenuItem, type MenuItemProps } from \"./menu/MenuItem\";\nexport {\n StartMenu,\n type StartMenuItem,\n type StartMenuProps,\n} from \"./menu/StartMenu\";\nexport {\n type ContextMenuState,\n useContextMenuPositionState,\n useContextMenuState,\n} from \"./menu/useContextMenuState\";\nexport { RoutedPane, type RoutedPaneProps } from \"./routed/RoutedPane\";\nexport {\n RoutedPaneOverlayHostProvider,\n type RoutedPaneOverlayHostValue,\n useRoutedPaneOverlayHost,\n} from \"./routed/RoutedPaneOverlayHost\";\nexport type { SubscribeKeyboardVisibility } from \"./routed/useMobileKeyboardVisible\";\nexport { SidebarResizeHandle, useSidebarResize } from \"./sidebar/SidebarResize\";\nexport {\n ThemeProvider,\n useOptionalTheme,\n useTheme,\n} from \"./theme/ThemeProvider\";\nexport { ThemeSwitch } from \"./theme/ThemeSwitch\";\nexport type {\n DefaultTheme,\n ThemeDefinition,\n ThemeScheme,\n} from \"./theme/themes\";\nexport {\n CurrentWindowProvider,\n useCurrentWindow,\n useSuppressWindowToolbar,\n useWindowBackground,\n useWindowContentSize,\n} from \"./window/CurrentWindowContext\";\nexport { Window, type WindowContentBoundary } from \"./window/Window\";\nexport { WindowTitleBarActionButtons } from \"./window/WindowChromeActions\";\nexport { WindowCloseButton } from \"./window/WindowCloseButton\";\nexport {\n useWindowBackAction,\n useWindowBackActionValue,\n useWindowFileMenuItem,\n useWindowRefreshMenuItem,\n useWindowRefreshMenuItemValue,\n useWindowTitleBarAction,\n useWindowTitleBarActions,\n useWindowToolbarReservation,\n useWindowViewMenuItem,\n WindowMenuProvider,\n} from \"./window/WindowMenuContext\";\nexport {\n hasWindowSidebar,\n useRegisteredWindowSidebar,\n useWindowSidebar,\n WindowSidebarProvider,\n} from \"./window/WindowSidebarContext\";\nexport {\n findTopWindow,\n useWindowActions,\n useWindowStateData,\n type WindowCreateOptions,\n type WindowEntry,\n type WindowGeometry,\n type WindowPosition,\n type WindowSize,\n type WindowStateActions,\n type WindowStateData,\n WindowStateProvider,\n} from \"./window/WindowStateProvider\";\nexport { WindowToolBar } from \"./window/WindowToolBar\";\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tearleads/windowing",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.23",
|
|
4
4
|
"description": "App-agnostic window management for React: window state, window chrome, and the menu and sidebar primitives it renders with.",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"repository": {
|