@domicile-desktop/manganese-window-management 1.2.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (114) hide show
  1. package/package.json +29 -19
  2. package/src/AppPopup.tsx +22 -10
  3. package/src/AppWindow.tsx +146 -68
  4. package/src/BrowserWindow.tsx +393 -33
  5. package/src/FocusGlow.tsx +5 -1
  6. package/src/GroupTitleBar.tsx +80 -0
  7. package/src/GroupWell.tsx +5 -1
  8. package/src/SelectionTint.tsx +5 -1
  9. package/src/Stage.tsx +381 -106
  10. package/src/StripEnd.tsx +1 -1
  11. package/src/TabStrip.tsx +16 -1
  12. package/src/TitleBar.tsx +277 -124
  13. package/src/WindowFrame.tsx +106 -26
  14. package/src/WindowTitleBar.tsx +11 -2
  15. package/src/browser/AddressBar.tsx +46 -37
  16. package/src/browser/AppMenuBar.tsx +106 -0
  17. package/src/browser/AppPopupMenu.tsx +45 -0
  18. package/src/browser/ConnectionIndicator.tsx +2 -2
  19. package/src/browser/HistoryButton.tsx +150 -0
  20. package/src/browser/PageMenu.tsx +20 -6
  21. package/src/browser/PageProblem.tsx +280 -0
  22. package/src/browser/PageTooltip.tsx +76 -0
  23. package/src/browser/PageViews.tsx +101 -0
  24. package/src/browser/ProfileMenu.tsx +332 -0
  25. package/src/browser/app-window-shape.ts +33 -0
  26. package/src/browser/page-history.ts +54 -0
  27. package/src/browser/page-menu.ts +85 -9
  28. package/src/browser/profile-ink.ts +17 -0
  29. package/src/browser/useAppMenuBar.ts +51 -0
  30. package/src/browser/useAppMessageBox.ts +47 -0
  31. package/src/browser/useAppPopupMenu.ts +49 -0
  32. package/src/browser/useAppWindowLooks.ts +49 -0
  33. package/src/browser/useAppWindowState.ts +59 -0
  34. package/src/browser/useBrowserProfile.ts +230 -0
  35. package/src/browser/useLongPress.ts +66 -0
  36. package/src/captured-element.ts +18 -0
  37. package/src/closing.ts +33 -15
  38. package/src/crossing.ts +64 -0
  39. package/src/floating/FloatGrab.tsx +10 -0
  40. package/src/floating/FloatShadow.tsx +5 -1
  41. package/src/floating/useFloatBorderDrag.ts +17 -59
  42. package/src/floating/useFloatDrag.ts +3 -2
  43. package/src/follow-press.ts +55 -0
  44. package/src/handed-drag.ts +57 -0
  45. package/src/keyboard/command.ts +160 -9
  46. package/src/keyboard/commands.ts +62 -6
  47. package/src/keyboard/query.ts +94 -0
  48. package/src/keyboard/useKeybindings.ts +69 -13
  49. package/src/overview/OverviewBackdrop.tsx +246 -0
  50. package/src/overview/OverviewPicker.tsx +453 -0
  51. package/src/overview/fixture.ts +76 -0
  52. package/src/overview/overview.ts +112 -0
  53. package/src/overview/seen-styles.ts +132 -0
  54. package/src/overview/sight.ts +86 -0
  55. package/src/overview/steering.ts +85 -0
  56. package/src/overview/tiles.ts +209 -0
  57. package/src/overview/useOverviewPhase.ts +35 -0
  58. package/src/own-animation.ts +24 -0
  59. package/src/panda-preset.ts +91 -3
  60. package/src/picture-in-picture/PictureInPictureFloats.tsx +865 -0
  61. package/src/picture-in-picture/page-of.ts +25 -0
  62. package/src/picture-in-picture/pip-box.ts +156 -0
  63. package/src/picture-in-picture/playhead.ts +33 -0
  64. package/src/placement.ts +20 -11
  65. package/src/pointer-confinement.ts +107 -0
  66. package/src/pointer-lock.ts +113 -0
  67. package/src/popup.ts +25 -37
  68. package/src/recorded-settles.ts +6 -2
  69. package/src/restacking.ts +4 -3
  70. package/src/screens/popup-scene.ts +38 -0
  71. package/src/screens/reading-order.ts +29 -0
  72. package/src/shown.ts +10 -0
  73. package/src/sliding-screen.ts +12 -0
  74. package/src/tab-drop.ts +43 -0
  75. package/src/tab-switch.ts +5 -1
  76. package/src/tiled/TileGrab.tsx +11 -1
  77. package/src/tiled/aim.ts +77 -39
  78. package/src/tiled/borders.ts +10 -2
  79. package/src/tiled/tab-targets.ts +37 -0
  80. package/src/tiled/useBorderDrag.ts +18 -42
  81. package/src/tiled/useTileDrag.ts +8 -7
  82. package/src/tree/drop.ts +118 -23
  83. package/src/tree/frames.ts +2 -2
  84. package/src/tree/layout.ts +71 -11
  85. package/src/tree/move.ts +8 -27
  86. package/src/useHandedPress.ts +25 -0
  87. package/src/usePageProblem.ts +80 -0
  88. package/src/usePageTooltip.ts +48 -0
  89. package/src/usePointerConfinement.ts +103 -0
  90. package/src/usePointerLock.ts +12 -0
  91. package/src/useSettling.ts +38 -9
  92. package/src/useTabDrop.ts +65 -0
  93. package/src/useWindowMotion.ts +109 -10
  94. package/src/useWindows.ts +112 -19
  95. package/src/window-changes.ts +13 -3
  96. package/src/window-log.ts +106 -0
  97. package/src/window-motion.ts +34 -0
  98. package/src/window-state.ts +629 -66
  99. package/src/window-styles.ts +96 -4
  100. package/src/window.ts +14 -0
  101. package/src/workspace.ts +11 -5
  102. package/src/workspaces-context.ts +6 -1
  103. package/styled-system/patterns/aspect-ratio.mjs +2 -2
  104. package/styled-system/patterns/bleed.mjs +1 -2
  105. package/styled-system/patterns/float.mjs +5 -11
  106. package/styled-system/patterns/grid-item.mjs +1 -2
  107. package/styled-system/patterns/grid.mjs +1 -2
  108. package/styled-system/patterns/link-overlay.mjs +1 -1
  109. package/styled-system/patterns/spacer.mjs +3 -3
  110. package/styled-system/patterns/visually-hidden.mjs +1 -1
  111. package/styled-system/styles.css +2885 -2108
  112. package/styled-system/tokens/index.mjs +7 -3
  113. package/styled-system/tokens/tokens.d.ts +2 -2
  114. package/styled-system/types/prop-type.d.ts +2 -2
@@ -0,0 +1,150 @@
1
+ import { Button } from "@domicile-desktop/component-library/Button";
2
+ import type { Point } from "@domicile-desktop/component-library/ContextMenu";
3
+ import { ContextMenu } from "@domicile-desktop/component-library/ContextMenu";
4
+ import { newTabClick } from "@domicile-desktop/component-library/new-tab-click";
5
+ import { ClockCounterClockwiseIcon } from "@phosphor-icons/react/dist/ssr/ClockCounterClockwise";
6
+ import { GlobeSimpleIcon } from "@phosphor-icons/react/dist/ssr/GlobeSimple";
7
+ import type { MouseEvent, ReactElement } from "react";
8
+ import { useState } from "react";
9
+ import { css } from "../../styled-system/css";
10
+ import type { PageHistoryChoice, PageHistoryEntry } from "./page-history";
11
+ import { useLongPress } from "./useLongPress";
12
+
13
+ /** How many pages a menu lists, as Chrome's. */
14
+ const MENU_LENGTH = 12;
15
+
16
+ type Props = {
17
+ /** The arrow. */
18
+ children: ReactElement;
19
+ disabled: boolean;
20
+ /** The history, or nothing from an engine without history entries. */
21
+ history: PageHistoryChoice | undefined;
22
+ label: "Back" | "Forward";
23
+ /** Takes one step, for a click. */
24
+ onStep: () => void;
25
+ /** Takes one step in a new window, for a middle click or a Ctrl+click. */
26
+ onStepInNewWindow: () => void;
27
+ /** Opens the History app. */
28
+ onShowAllHistory: () => void;
29
+ };
30
+
31
+ /**
32
+ * Back or Forward: a click takes one step, and a long press or a right click
33
+ * lists the pages that way to jump to, as Chrome's buttons do.
34
+ */
35
+ export const HistoryButton = ({
36
+ children,
37
+ disabled,
38
+ history,
39
+ label,
40
+ onShowAllHistory,
41
+ onStep,
42
+ onStepInNewWindow,
43
+ }: Props) => {
44
+ const [menu, setMenu] = useState<Menu | undefined>(undefined);
45
+ const [button, setButton] = useState<HTMLElement | undefined>(undefined);
46
+
47
+ /** Opens the menu, answering whether it will open. */
48
+ const openMenu = (): boolean => {
49
+ if (disabled || history === undefined || button === undefined) {
50
+ return false;
51
+ } else {
52
+ // Under the button, as Chrome's, wherever the pointer is.
53
+ const box = button.getBoundingClientRect();
54
+ const at = { x: box.left, y: box.bottom };
55
+ history
56
+ .list()
57
+ .then((listed) => {
58
+ setMenu({
59
+ at,
60
+ pages: (label === "Back" ? listed.back : listed.forward).slice(
61
+ 0,
62
+ MENU_LENGTH,
63
+ ),
64
+ });
65
+ })
66
+ .catch((error: unknown) => {
67
+ // biome-ignore lint/suspicious/noConsole: the menu stays shut; the button still steps
68
+ console.error(`Couldn't list the ${label} history`, error);
69
+ });
70
+ return true;
71
+ }
72
+ };
73
+
74
+ const step = newTabClick(onStep, onStepInNewWindow);
75
+ const press = useLongPress(openMenu, step.onClick);
76
+
77
+ return (
78
+ <>
79
+ <Button
80
+ disabled={disabled}
81
+ label={label}
82
+ onAuxClick={step.onAuxClick}
83
+ onContextMenu={(event: MouseEvent) => {
84
+ event.preventDefault();
85
+ openMenu();
86
+ }}
87
+ // React passes `null` on unmount.
88
+ ref={(element: HTMLButtonElement | null) => {
89
+ setButton(element ?? undefined);
90
+ }}
91
+ rounded
92
+ size="sm"
93
+ variant="ghost"
94
+ {...press}
95
+ >
96
+ {children}
97
+ </Button>
98
+ {menu !== undefined && history !== undefined && (
99
+ <ContextMenu
100
+ at={menu.at}
101
+ label={`${label} history`}
102
+ onOpenChange={(open) => {
103
+ if (!open) {
104
+ setMenu(undefined);
105
+ }
106
+ }}
107
+ open
108
+ >
109
+ {menu.pages.map((page) => (
110
+ <ContextMenu.Item
111
+ icon={<PageIcon page={page} />}
112
+ key={page.id}
113
+ {...newTabClick(
114
+ () => {
115
+ history.go(page.id);
116
+ },
117
+ () => {
118
+ setMenu(undefined);
119
+ history.goInNewWindow(page.id);
120
+ },
121
+ )}
122
+ >
123
+ {page.title}
124
+ </ContextMenu.Item>
125
+ ))}
126
+ <ContextMenu.Separator />
127
+ <ContextMenu.Item
128
+ icon={<ClockCounterClockwiseIcon />}
129
+ onClick={onShowAllHistory}
130
+ >
131
+ Show full history
132
+ </ContextMenu.Item>
133
+ </ContextMenu>
134
+ )}
135
+ </>
136
+ );
137
+ };
138
+
139
+ /** An open menu: where, and the pages it lists. */
140
+ type Menu = { at: Point; pages: readonly PageHistoryEntry[] };
141
+
142
+ /** A page's favicon, or a globe for a page with none, as the tabs draw. */
143
+ const PageIcon = ({ page }: { page: PageHistoryEntry }) =>
144
+ page.favicon === "" ? (
145
+ <GlobeSimpleIcon />
146
+ ) : (
147
+ <img alt="" className={faviconStyles} src={page.favicon} />
148
+ );
149
+
150
+ const faviconStyles = css({ objectFit: "contain" });
@@ -1,14 +1,15 @@
1
1
  import { ContextMenu } from "@domicile-desktop/component-library/ContextMenu";
2
2
  import { Fragment } from "react";
3
+ import { css } from "../../styled-system/css";
3
4
 
4
- import type { PageMenuCommand, PageMenuItem } from "./page-menu";
5
+ import type { PageMenuItem } from "./page-menu";
5
6
 
6
7
  type Props = {
7
8
  /** Where the click was, in the viewport's CSS pixels. */
8
9
  at: { x: number; y: number };
9
10
  /** The menu, in groups a line goes between — see `pageMenuFor`. */
10
11
  items: PageMenuItem[][];
11
- onChoose: (command: PageMenuCommand) => void;
12
+ onChoose: (item: PageMenuItem) => void;
12
13
  /** It closed: chosen from, or put away with Escape or a press outside. */
13
14
  onClose: () => void;
14
15
  };
@@ -26,21 +27,34 @@ export const PageMenu = ({ at, items, onChoose, onClose }: Props) => (
26
27
  open
27
28
  >
28
29
  {items.map((group, index) => (
29
- <Fragment key={group.map((item) => item.command).join()}>
30
+ <Fragment key={group.map(keyOf).join()}>
30
31
  {index > 0 && <ContextMenu.Separator />}
31
32
  {group.map((item) => (
32
33
  <ContextMenu.Item
33
34
  disabled={item.disabled}
34
- key={item.command}
35
+ key={keyOf(item)}
35
36
  onClick={() => {
36
- onChoose(item.command);
37
+ onChoose(item);
37
38
  }}
38
39
  shortcut={item.shortcut}
39
40
  >
40
- {item.label}
41
+ {item.replacement === undefined ? (
42
+ item.label
43
+ ) : (
44
+ <span className={suggestionStyles}>{item.label}</span>
45
+ )}
41
46
  </ContextMenu.Item>
42
47
  ))}
43
48
  </Fragment>
44
49
  ))}
45
50
  </ContextMenu>
46
51
  );
52
+
53
+ /** Spelling suggestions share a command, so their words tell them apart. */
54
+ const keyOf = (item: PageMenuItem): string =>
55
+ item.replacement === undefined
56
+ ? item.command
57
+ : `${item.command}:${item.replacement}`;
58
+
59
+ /** A misspelled word's suggestions stand out from the commands below them. */
60
+ const suggestionStyles = css({ fontWeight: "semibold" });
@@ -0,0 +1,280 @@
1
+ import { Button } from "@domicile-desktop/component-library/Button";
2
+ import type { Icon } from "@phosphor-icons/react";
3
+ import { ShieldWarningIcon } from "@phosphor-icons/react/dist/ssr/ShieldWarning";
4
+ import { SmileyXEyesIcon } from "@phosphor-icons/react/dist/ssr/SmileyXEyes";
5
+ import { WarningIcon } from "@phosphor-icons/react/dist/ssr/Warning";
6
+ import { WifiSlashIcon } from "@phosphor-icons/react/dist/ssr/WifiSlash";
7
+ import type { RefCallback } from "react";
8
+ import { useState } from "react";
9
+
10
+ import { css } from "../../styled-system/css";
11
+ import { center, flex } from "../../styled-system/patterns";
12
+ import type { PageProblem as Problem } from "../usePageProblem";
13
+
14
+ type Props = {
15
+ problem: Problem;
16
+ /** Whether the page has history to go back to. */
17
+ canGoBack: boolean;
18
+ onGoBack: () => void;
19
+ onReload: () => void;
20
+ onProceed: () => void;
21
+ /** Gets the element to focus: the first answer. */
22
+ ref?: RefCallback<HTMLElement> | undefined;
23
+ };
24
+
25
+ /**
26
+ * What a browser window shows in place of a page that failed to load or
27
+ * crashed, as Chrome's error, warning and "Aw, Snap!" pages do. Covers the
28
+ * page, which the engine leaves blank.
29
+ *
30
+ * A warning leads with Go back; Continue anyway waits behind Details, and is
31
+ * offered only where the browser allows it.
32
+ */
33
+ export const PageProblem = ({
34
+ canGoBack,
35
+ onGoBack,
36
+ onProceed,
37
+ onReload,
38
+ problem,
39
+ ref,
40
+ }: Props) => {
41
+ const [details, setDetails] = useState(false);
42
+ const words = wordsFor(problem);
43
+ const Glyph = words.icon;
44
+ const warns = problem.kind === "certificate" || problem.kind === "warning";
45
+ return (
46
+ <section aria-label={words.title} className={rootStyles}>
47
+ <span className={discStyles}>
48
+ <Glyph aria-hidden size={28} weight="bold" />
49
+ </span>
50
+ <h2 className={titleStyles}>{words.title}</h2>
51
+ <p className={messageStyles}>{words.message}</p>
52
+ {problem.kind !== "gone" && (
53
+ <p className={codeStyles}>
54
+ {problem.kind === "http"
55
+ ? `HTTP ${problem.httpStatus}`
56
+ : problem.error}
57
+ </p>
58
+ )}
59
+ <div className={answersStyles}>
60
+ {warns ? (
61
+ <>
62
+ {canGoBack && (
63
+ <Button onClick={onGoBack} ref={ref} variant="accent">
64
+ Go back
65
+ </Button>
66
+ )}
67
+ {problem.canProceed && !details && (
68
+ <Button
69
+ onClick={() => {
70
+ setDetails(true);
71
+ }}
72
+ ref={canGoBack ? undefined : ref}
73
+ variant="outline"
74
+ >
75
+ Details
76
+ </Button>
77
+ )}
78
+ </>
79
+ ) : (
80
+ <>
81
+ <Button onClick={onReload} ref={ref} variant="accent">
82
+ {problem.kind === "gone" ? "Reload" : "Try again"}
83
+ </Button>
84
+ {canGoBack && problem.kind !== "gone" && (
85
+ <Button onClick={onGoBack} variant="outline">
86
+ Go back
87
+ </Button>
88
+ )}
89
+ </>
90
+ )}
91
+ </div>
92
+ {warns && problem.canProceed && details && (
93
+ <div className={detailsStyles}>
94
+ <p className={messageStyles}>{words.risk}</p>
95
+ <Button onClick={onProceed} size="sm" variant="outline">
96
+ Continue anyway
97
+ </Button>
98
+ </div>
99
+ )}
100
+ </section>
101
+ );
102
+ };
103
+
104
+ type Words = { icon: Icon; message: string; risk?: string; title: string };
105
+
106
+ /** The page's icon, title and text. */
107
+ const wordsFor = (problem: Problem): Words => {
108
+ if (problem.kind === "gone") {
109
+ return problem.reason === "oom"
110
+ ? {
111
+ icon: SmileyXEyesIcon,
112
+ message: "It used more memory than this computer could give it.",
113
+ title: "This page ran out of memory",
114
+ }
115
+ : {
116
+ icon: SmileyXEyesIcon,
117
+ message: "Something went wrong while showing it.",
118
+ title: "This page crashed",
119
+ };
120
+ }
121
+ const host = hostOf(problem.url);
122
+ switch (problem.kind) {
123
+ case "http": {
124
+ return {
125
+ icon: WarningIcon,
126
+ message: "The site answered with an error and nothing to show.",
127
+ title: `${host} isn't working`,
128
+ };
129
+ }
130
+ case "certificate": {
131
+ return {
132
+ icon: ShieldWarningIcon,
133
+ message: `Its certificate isn't trusted, so someone could be pretending to be ${host} to read what you send.`,
134
+ risk: "Only continue if you know why this site's certificate isn't trusted, such as a device on your own network.",
135
+ title: "Your connection isn't private",
136
+ };
137
+ }
138
+ case "warning": {
139
+ return {
140
+ icon: ShieldWarningIcon,
141
+ message:
142
+ "The browser stopped it before it loaded. It may try to trick you or harm your computer.",
143
+ risk: "Only continue if you trust this site.",
144
+ title: `${host} may not be safe`,
145
+ };
146
+ }
147
+ case "network": {
148
+ return networkWords(problem.error, host);
149
+ }
150
+ }
151
+ };
152
+
153
+ /** A network failure's title and text, by Chromium's name for it. */
154
+ const networkWords = (error: string, host: string): Words => {
155
+ switch (error) {
156
+ case "ERR_INTERNET_DISCONNECTED": {
157
+ return {
158
+ icon: WifiSlashIcon,
159
+ message: "Connect to the internet and try again.",
160
+ title: "You're offline",
161
+ };
162
+ }
163
+ case "ERR_NAME_NOT_RESOLVED":
164
+ case "ERR_NAME_RESOLUTION_FAILED": {
165
+ return {
166
+ icon: WifiSlashIcon,
167
+ message:
168
+ "The address couldn't be found. Check it for typos, or try again once you're online.",
169
+ title: `Can't reach ${host}`,
170
+ };
171
+ }
172
+ case "ERR_CONNECTION_REFUSED": {
173
+ return {
174
+ icon: WifiSlashIcon,
175
+ message: `${host} refused to connect.`,
176
+ title: `Can't reach ${host}`,
177
+ };
178
+ }
179
+ case "ERR_CONNECTION_TIMED_OUT":
180
+ case "ERR_TIMED_OUT": {
181
+ return {
182
+ icon: WifiSlashIcon,
183
+ message: `${host} took too long to respond.`,
184
+ title: `Can't reach ${host}`,
185
+ };
186
+ }
187
+ case "ERR_TOO_MANY_REDIRECTS": {
188
+ return {
189
+ icon: WarningIcon,
190
+ message: `${host} sent the browser around in circles.`,
191
+ title: `${host} isn't working`,
192
+ };
193
+ }
194
+ case "ERR_FILE_NOT_FOUND": {
195
+ return {
196
+ icon: WarningIcon,
197
+ message: "It may have been moved, renamed or deleted.",
198
+ title: "File not found",
199
+ };
200
+ }
201
+ case "ERR_BLOCKED_BY_CLIENT":
202
+ case "ERR_BLOCKED_BY_ADMINISTRATOR": {
203
+ return {
204
+ icon: ShieldWarningIcon,
205
+ message: "It can't be opened here.",
206
+ title: "This page is blocked",
207
+ };
208
+ }
209
+ default: {
210
+ return {
211
+ icon: WifiSlashIcon,
212
+ message: "Something went wrong reaching it.",
213
+ title: `Can't reach ${host}`,
214
+ };
215
+ }
216
+ }
217
+ };
218
+
219
+ /** The address's host, or the address itself when it has none. */
220
+ const hostOf = (url: string): string => {
221
+ const parsed = URL.parse(url);
222
+ return parsed === null || parsed.host === "" ? url : parsed.host;
223
+ };
224
+
225
+ // Covers the blank page, in the window's own surface.
226
+ const rootStyles = flex({
227
+ align: "center",
228
+ backgroundColor: "background",
229
+ direction: "column",
230
+ gap: 3,
231
+ inset: 0,
232
+ justify: "center",
233
+ overflowY: "auto",
234
+ padding: 8,
235
+ position: "absolute",
236
+ textAlign: "center",
237
+ });
238
+
239
+ const discStyles = center({
240
+ backgroundColor: "fill",
241
+ blockSize: 16,
242
+ borderRadius: "full",
243
+ color: "muted",
244
+ inlineSize: 16,
245
+ marginBlockEnd: 2,
246
+ });
247
+
248
+ const titleStyles = css({
249
+ color: "foreground",
250
+ fontSize: "xl",
251
+ fontWeight: "semibold",
252
+ margin: 0,
253
+ maxInlineSize: "32rem",
254
+ overflowWrap: "anywhere",
255
+ textWrap: "balance",
256
+ });
257
+
258
+ const messageStyles = css({
259
+ color: "muted",
260
+ fontSize: "sm",
261
+ margin: 0,
262
+ maxInlineSize: "28rem",
263
+ textWrap: "balance",
264
+ });
265
+
266
+ const codeStyles = css({
267
+ color: "muted",
268
+ fontFamily: "mono",
269
+ fontSize: "xs",
270
+ margin: 0,
271
+ });
272
+
273
+ const answersStyles = flex({ gap: 2, marginBlockStart: 2 });
274
+
275
+ const detailsStyles = flex({
276
+ align: "center",
277
+ direction: "column",
278
+ gap: 2,
279
+ marginBlockStart: 2,
280
+ });
@@ -0,0 +1,76 @@
1
+ import { useLayoutEffect, useRef, useState } from "react";
2
+
3
+ import { css } from "../../styled-system/css";
4
+ import { usePageTooltip } from "../usePageTooltip";
5
+
6
+ type Props = {
7
+ /**
8
+ * The page whose tooltip it shows. Read here, so a tooltip redraws only
9
+ * itself.
10
+ */
11
+ view: HTMLWebViewElement | null;
12
+ };
13
+
14
+ /** How far below the pointer it sits, clear of the cursor, in CSS pixels. */
15
+ export const BELOW_POINTER = 20;
16
+
17
+ /**
18
+ * Shows the page's tooltip, such as a `title`, below the pointer and inside
19
+ * the page, styled like the component library's `Tooltip`. The engine draws
20
+ * none; see `tooltip` on `<webview>`.
21
+ */
22
+ export const PageTooltip = ({ view }: Props) => {
23
+ const { text, x, y } = usePageTooltip(view);
24
+ const ref = useRef<HTMLDivElement>(null);
25
+ // Kept inside the page: moved left at its right edge, and above the
26
+ // pointer at its bottom.
27
+ const [place, setPlace] = useState({ left: x, top: y + BELOW_POINTER });
28
+
29
+ useLayoutEffect(() => {
30
+ const tip = ref.current;
31
+ const page = tip?.offsetParent;
32
+ // Measured again for each text, since the tip's size follows it.
33
+ if (text === "" || tip === null || !(page instanceof HTMLElement)) {
34
+ setPlace({ left: x, top: y + BELOW_POINTER });
35
+ } else {
36
+ const below = y + BELOW_POINTER;
37
+ setPlace({
38
+ left: Math.max(0, Math.min(x, page.clientWidth - tip.offsetWidth)),
39
+ top:
40
+ below + tip.offsetHeight > page.clientHeight
41
+ ? Math.max(0, y - tip.offsetHeight - 4)
42
+ : below,
43
+ });
44
+ }
45
+ }, [text, x, y]);
46
+
47
+ return text === "" ? undefined : (
48
+ <div
49
+ className={tooltipStyles}
50
+ ref={ref}
51
+ role="tooltip"
52
+ style={{ insetBlockStart: place.top, insetInlineStart: place.left }}
53
+ >
54
+ {text}
55
+ </div>
56
+ );
57
+ };
58
+
59
+ // As the component library's `Tooltip`. Ignores the pointer so it never
60
+ // blocks the page; keeps the page's line breaks, as Chrome does.
61
+ const tooltipStyles = css({
62
+ backgroundColor: "card",
63
+ border: "1px solid {colors.border}",
64
+ borderRadius: "md",
65
+ boxShadow: "lifted",
66
+ color: "foreground",
67
+ fontSize: "xs",
68
+ lineHeight: "normal",
69
+ maxInlineSize: "min({spacing.88}, 90%)",
70
+ overflowWrap: "anywhere",
71
+ paddingBlock: 1.5,
72
+ paddingInline: 2.5,
73
+ pointerEvents: "none",
74
+ position: "absolute",
75
+ whiteSpace: "pre-line",
76
+ });
@@ -0,0 +1,101 @@
1
+ import type { BrowserWindow } from "@domicile-desktop/sdk/desktop/browser";
2
+ import type { Domicile } from "@domicile-desktop/sdk/desktop/desktop";
3
+ import { WEBVIEW_GUEST_FOCUS_EVENT } from "@domicile-desktop/sdk/webview-element";
4
+ import {
5
+ useCallback,
6
+ useEffect,
7
+ useMemo,
8
+ useState,
9
+ useSyncExternalStore,
10
+ } from "react";
11
+ import { css } from "../../styled-system/css";
12
+
13
+ type Props = {
14
+ domicile: Pick<Domicile, "browser">;
15
+ /** A click in a view's page: see `BrowserWindow`'s `onReach`. */
16
+ onReach: () => void;
17
+ /** Nothing can click the window, so a view taking focus is no reach. */
18
+ unclickable: boolean;
19
+ /** The engine id of the window whose views these are. */
20
+ window: string;
21
+ };
22
+
23
+ /**
24
+ * An Electron app's views (`WebContentsView`, `BrowserView`) in its window:
25
+ * each a `<webview>` over the window's page, where the app put it, bottom
26
+ * first. Manganese honors the app's layout; the engine's bounds are a hint
27
+ * another shell may lay out its own way. See "Views" in
28
+ * docs/SHELL-BROWSER-WINDOWS.md.
29
+ */
30
+ export const PageViews = ({
31
+ domicile,
32
+ onReach,
33
+ unclickable,
34
+ window,
35
+ }: Props) => {
36
+ const subscribe = useCallback(
37
+ (changed: () => void) => domicile.browser.on("change", changed),
38
+ [domicile],
39
+ );
40
+ // The SDK keeps the same list until the engine's changes, so this settles.
41
+ const all = useSyncExternalStore(subscribe, () => domicile.browser.views);
42
+ const views = useMemo(
43
+ () => all?.filter(({ parent }) => parent === window) ?? [],
44
+ [all, window],
45
+ );
46
+ return views.map((view) => (
47
+ <PageView
48
+ key={view.id}
49
+ onReach={onReach}
50
+ unclickable={unclickable}
51
+ view={view}
52
+ />
53
+ ));
54
+ };
55
+
56
+ const PageView = ({
57
+ onReach,
58
+ unclickable,
59
+ view,
60
+ }: Omit<Props, "domicile" | "window"> & { view: BrowserWindow }) => {
61
+ const [element, setElement] = useState<HTMLWebViewElement | null>(null);
62
+
63
+ useEffect(() => {
64
+ if (element === null) {
65
+ return undefined;
66
+ } else {
67
+ const reached = () => {
68
+ if (!unclickable) {
69
+ onReach();
70
+ }
71
+ };
72
+ element.addEventListener(WEBVIEW_GUEST_FOCUS_EVENT, reached);
73
+ return () => {
74
+ element.removeEventListener(WEBVIEW_GUEST_FOCUS_EVENT, reached);
75
+ };
76
+ }
77
+ }, [element, onReach, unclickable]);
78
+
79
+ return (
80
+ <webview
81
+ className={viewStyles}
82
+ ref={setElement}
83
+ style={{
84
+ blockSize: view.height,
85
+ inlineSize: view.width,
86
+ insetBlockStart: view.y,
87
+ insetInlineStart: view.x,
88
+ // Hidden, not removed: the page stays, as the app's hidden view does.
89
+ visibility: view.isHidden ? "hidden" : undefined,
90
+ }}
91
+ // Read once, at insertion. The key keeps one element per view.
92
+ window={view.id}
93
+ />
94
+ );
95
+ };
96
+
97
+ // The app's bounds are in the page's CSS pixels, from its top left corner.
98
+ const viewStyles = css({
99
+ borderStyle: "none",
100
+ position: "absolute",
101
+ });