@gloom-sh/terminal 0.19.5 → 0.20.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 (318) hide show
  1. package/LICENSE +0 -4
  2. package/package.json +6 -5
  3. package/patches/@opentui%2Fcore@0.3.2.patch +86 -25
  4. package/patches/electrobun-devkit@2.0.5.patch +148 -0
  5. package/src/api-client/bond-trades.ts +59 -0
  6. package/src/api-client/data.ts +21 -0
  7. package/src/api-client/equity-screener.ts +23 -0
  8. package/src/api-client/index.ts +2 -0
  9. package/src/api-client/types/account.ts +86 -0
  10. package/src/api-client/types/assist.ts +32 -0
  11. package/src/api-client/types/auth.ts +63 -0
  12. package/src/api-client/types/billing.ts +54 -0
  13. package/src/api-client/types/chat.ts +180 -0
  14. package/src/api-client/types/congress.ts +107 -0
  15. package/src/api-client/types/credit.ts +110 -0
  16. package/src/api-client/types/econ.ts +158 -0
  17. package/src/api-client/types/equity-diagnostic.ts +73 -0
  18. package/src/api-client/types/filings-and-calls.ts +350 -0
  19. package/src/api-client/types/index.ts +25 -0
  20. package/src/api-client/types/jobs.ts +147 -0
  21. package/src/api-client/types/market.ts +122 -0
  22. package/src/api-client/types/news.ts +91 -0
  23. package/src/api-client/types/notes-and-theses.ts +172 -0
  24. package/src/api-client/types/quotes.ts +28 -0
  25. package/src/api-client/types/scanners.ts +125 -0
  26. package/src/api-client/types/screener.ts +65 -0
  27. package/src/api-client/types/search.ts +107 -0
  28. package/src/api-client/types/sync.ts +14 -0
  29. package/src/api-client/types/team-collections.ts +43 -0
  30. package/src/api-client/types/team-views.ts +32 -0
  31. package/src/api-client/types/teams.ts +135 -0
  32. package/src/api-client/types/ticker-data.ts +57 -0
  33. package/src/api-client/types/tweets.ts +59 -0
  34. package/src/api-client/types/world-venues.ts +23 -0
  35. package/src/app/pane-runtime/index.tsx +2 -0
  36. package/src/app/pane-runtime/plugin-bindings.ts +10 -1
  37. package/src/app/runtime/desktop-deeplink.ts +41 -6
  38. package/src/app/star-prompt/status-line.tsx +1 -1
  39. package/src/app.tsx +64 -23
  40. package/src/architecture/ui-tokens-allowlist.json +0 -8
  41. package/src/cli/commands/market.ts +149 -10
  42. package/src/cli/commands/search.ts +21 -3
  43. package/src/cli/commands/ticker.ts +90 -36
  44. package/src/cli/desktop-pane-shot-browser.ts +69 -3
  45. package/src/cli/glossary.ts +3 -3
  46. package/src/cli/known-gaps.ts +8 -0
  47. package/src/cli/listing-arg.ts +12 -0
  48. package/src/cli/pane-functions/capabilities.ts +2 -0
  49. package/src/cli/pane-functions/catalog.ts +25 -5
  50. package/src/cli/pane-functions/function-help.ts +8 -5
  51. package/src/cli/pane-functions/headless.ts +4 -3
  52. package/src/cli/pane-functions/resolver.ts +75 -2
  53. package/src/cli/quote-answer.ts +9 -0
  54. package/src/cli/symbol-list.ts +54 -2
  55. package/src/cli/symbol-rows.ts +14 -2
  56. package/src/cli/ticker-resolution.ts +89 -20
  57. package/src/components/chart/composite/axis-overlays.tsx +60 -27
  58. package/src/components/chart/composite/composite-chart.tsx +118 -63
  59. package/src/components/chart/composite/cursor-source.ts +111 -0
  60. package/src/components/chart/composite/format.ts +168 -6
  61. package/src/components/chart/composite/legend.tsx +8 -4
  62. package/src/components/chart/composite/panel-labels.ts +20 -1
  63. package/src/components/chart/composite/panel-surface.tsx +201 -92
  64. package/src/components/chart/composite/price-axis-labels.tsx +92 -46
  65. package/src/components/chart/composite/price-series.ts +31 -8
  66. package/src/components/chart/composite/scene.ts +154 -59
  67. package/src/components/chart/composite/series-axis-tags.tsx +4 -2
  68. package/src/components/chart/composite/time-axis.ts +13 -5
  69. package/src/components/chart/composite/time-scale.ts +211 -56
  70. package/src/components/chart/composite/types.ts +11 -1
  71. package/src/components/chart/composite/use-composite-chart-keyboard.ts +43 -14
  72. package/src/components/chart/curve/curve-surface.tsx +5 -2
  73. package/src/components/chart/curve/model.ts +4 -1
  74. package/src/components/chart/native/use-bitmap-metrics.ts +81 -0
  75. package/src/components/chart/static/series.ts +3 -0
  76. package/src/components/command-bar/commands/collection-list.ts +81 -0
  77. package/src/components/command-bar/commands/direct/index.ts +6 -0
  78. package/src/components/command-bar/commands/registry.ts +8 -0
  79. package/src/components/command-bar/layout-items/current-layout.ts +22 -1
  80. package/src/components/command-bar/layout-items/focused-pane.ts +22 -1
  81. package/src/components/command-bar/pane-templates/items.ts +19 -3
  82. package/src/components/command-bar/pane-templates/workflow.ts +5 -0
  83. package/src/components/command-bar/routes/root/results.ts +23 -3
  84. package/src/components/command-bar/routes/root/selection.ts +4 -2
  85. package/src/components/command-bar/routes/root/shortcut-feedback.ts +1 -1
  86. package/src/components/command-bar/routes/root/shortcut-items.ts +3 -4
  87. package/src/components/command-bar/routes/root/shortcuts.ts +58 -14
  88. package/src/components/command-bar/routes/root/ticker-first.ts +32 -0
  89. package/src/components/command-bar/surface/environment.ts +2 -0
  90. package/src/components/command-bar/view-model.ts +7 -0
  91. package/src/components/command-bar/workflow/ops.ts +2 -1
  92. package/src/components/command-bar/workflow/tickers.ts +20 -0
  93. package/src/components/data-table/export.ts +4 -1
  94. package/src/components/layout/detached-pane-shell.tsx +10 -37
  95. package/src/components/layout/double-escape-close.ts +90 -0
  96. package/src/components/layout/header.tsx +3 -2
  97. package/src/components/layout/market-summary.ts +6 -3
  98. package/src/components/layout/pane/footer/index.tsx +2 -6
  99. package/src/components/layout/pane/footer/model.ts +6 -12
  100. package/src/components/layout/shell/active-drag.ts +53 -11
  101. package/src/components/layout/shell/drag/magnets.ts +128 -19
  102. package/src/components/layout/shell/menu.ts +15 -1
  103. package/src/components/layout/shell/pane/actions.ts +10 -2
  104. package/src/components/layout/shell/pane/management-shortcuts.ts +13 -51
  105. package/src/components/layout/shell/ticker-link-menu.ts +12 -3
  106. package/src/components/layout/status-bar.tsx +24 -78
  107. package/src/components/layout/status-widget-space.ts +5 -5
  108. package/src/components/onboarding/onboarding-keyboard.ts +5 -1
  109. package/src/components/onboarding/onboarding-wizard.tsx +15 -4
  110. package/src/components/onboarding/portfolio-step/positions-panel.tsx +9 -6
  111. package/src/components/onboarding/use-onboarding-progress.ts +20 -1
  112. package/src/components/price-sparkline/view.tsx +14 -6
  113. package/src/components/ticker/badge/index.tsx +8 -4
  114. package/src/components/ticker/badge/list.tsx +23 -7
  115. package/src/components/ui/column-grid.tsx +8 -2
  116. package/src/components/ui/data-table/index.tsx +5 -1
  117. package/src/components/ui/data-table/opentui/model.ts +4 -1
  118. package/src/components/ui/data-table/render-cell.ts +19 -0
  119. package/src/components/ui/delayed-mark.tsx +59 -7
  120. package/src/components/ui/icon.tsx +1 -8
  121. package/src/components/ui/menu.tsx +6 -1
  122. package/src/components/ui/tabs.tsx +5 -2
  123. package/src/core/state/app/layout.ts +18 -3
  124. package/src/core/state/app/state.ts +2 -0
  125. package/src/core/state/app/types.ts +5 -0
  126. package/src/data/config/layout.ts +3 -0
  127. package/src/data/config/store/normalize.ts +39 -4
  128. package/src/i18n/es.ts +13 -3
  129. package/src/i18n/ja.ts +13 -3
  130. package/src/i18n/ko.ts +13 -3
  131. package/src/i18n/zh-cn.ts +13 -3
  132. package/src/i18n/zh-tw.ts +13 -3
  133. package/src/layout/pane-manager/floating-placement.ts +7 -4
  134. package/src/layout/pane-manager/floating.ts +5 -2
  135. package/src/market-data/bond-funds.ts +19 -0
  136. package/src/market-data/futures-contract.ts +14 -3
  137. package/src/market-data/market/freshness.ts +18 -0
  138. package/src/market-data/market/status.ts +15 -2
  139. package/src/plugins/builtin/bond-calculator/headless.ts +2 -1
  140. package/src/plugins/builtin/bond-calculator/index.tsx +1 -1
  141. package/src/plugins/builtin/bond-calculator/model.ts +14 -5
  142. package/src/plugins/builtin/bond-calculator/pane.tsx +1 -1
  143. package/src/plugins/builtin/buildout/cells.tsx +25 -45
  144. package/src/plugins/builtin/buildout/detail/company.tsx +3 -3
  145. package/src/plugins/builtin/buildout/detail/index.tsx +1 -1
  146. package/src/plugins/builtin/buildout/detail/intel.tsx +4 -7
  147. package/src/plugins/builtin/buildout/detail/site.tsx +4 -4
  148. package/src/plugins/builtin/buildout/detail/ui.tsx +39 -38
  149. package/src/plugins/builtin/buildout/format.ts +23 -1
  150. package/src/plugins/builtin/buildout/model/types.ts +1 -0
  151. package/src/plugins/builtin/buildout/pane/index.tsx +2 -8
  152. package/src/plugins/builtin/buildout/table-model.ts +12 -4
  153. package/src/plugins/builtin/cds/headless.ts +1 -0
  154. package/src/plugins/builtin/cds/index.tsx +1 -1
  155. package/src/plugins/builtin/chat/content/composer.tsx +21 -27
  156. package/src/plugins/builtin/chat/content/index.tsx +31 -3
  157. package/src/plugins/builtin/chat/content/shortcuts.ts +4 -2
  158. package/src/plugins/builtin/chat/content/snapshot.ts +0 -3
  159. package/src/plugins/builtin/cloud/auth-actions.tsx +51 -23
  160. package/src/plugins/builtin/cloud/plugin.tsx +2 -0
  161. package/src/plugins/builtin/cloud/trial-offer-status-widget.tsx +3 -3
  162. package/src/plugins/builtin/cloud/upgrade-status-widget.tsx +44 -12
  163. package/src/plugins/builtin/congress-trades/model.ts +2 -1
  164. package/src/plugins/builtin/congress-trades/pane.tsx +3 -0
  165. package/src/plugins/builtin/correlation/headless.ts +3 -2
  166. package/src/plugins/builtin/crypto-board/pane.tsx +5 -4
  167. package/src/plugins/builtin/custom-view/index.tsx +1 -1
  168. package/src/plugins/builtin/custom-view/loader.ts +85 -8
  169. package/src/plugins/builtin/custom-view/view-spec.ts +28 -0
  170. package/src/plugins/builtin/debt-maturities/headless.ts +11 -2
  171. package/src/plugins/builtin/debt-maturities/model.ts +66 -0
  172. package/src/plugins/builtin/debt-maturities/pane.tsx +23 -4
  173. package/src/plugins/builtin/earnings-ripple/pane.tsx +1 -1
  174. package/src/plugins/builtin/econ/calendar-model.ts +28 -6
  175. package/src/plugins/builtin/equity-screener/client.ts +1 -1
  176. package/src/plugins/builtin/equity-screener/criterion-editor.tsx +16 -19
  177. package/src/plugins/builtin/equity-screener/headless.ts +37 -3
  178. package/src/plugins/builtin/equity-screener/ideas.ts +67 -3
  179. package/src/plugins/builtin/equity-screener/model.ts +238 -27
  180. package/src/plugins/builtin/equity-screener/pane.tsx +88 -18
  181. package/src/plugins/builtin/equity-screener/screen-check.ts +7 -4
  182. package/src/plugins/builtin/equity-screener/watchlist.ts +33 -0
  183. package/src/plugins/builtin/executives/format.ts +5 -4
  184. package/src/plugins/builtin/executives/pane.tsx +1 -1
  185. package/src/plugins/builtin/futures-curve/client.ts +19 -0
  186. package/src/plugins/builtin/futures-curve/headless.ts +52 -13
  187. package/src/plugins/builtin/futures-curve/index.tsx +1 -1
  188. package/src/plugins/builtin/futures-curve/model.ts +77 -6
  189. package/src/plugins/builtin/futures-curve/pane.tsx +19 -3
  190. package/src/plugins/builtin/help/index.tsx +55 -1
  191. package/src/plugins/builtin/insider/model.ts +30 -2
  192. package/src/plugins/builtin/jobs/model.ts +2 -2
  193. package/src/plugins/builtin/macro-day/client.ts +41 -0
  194. package/src/plugins/builtin/macro-day/headless.ts +19 -9
  195. package/src/plugins/builtin/macro-day/model.ts +3 -1
  196. package/src/plugins/builtin/market-movers/index.tsx +4 -3
  197. package/src/plugins/builtin/mna/distress/headless.ts +1 -0
  198. package/src/plugins/builtin/mna/model.ts +2 -2
  199. package/src/plugins/builtin/notes/files.ts +2 -0
  200. package/src/plugins/builtin/notes/owner.tsx +20 -1
  201. package/src/plugins/builtin/notes/quick-notes-pane.tsx +22 -4
  202. package/src/plugins/builtin/notes/store.ts +6 -1
  203. package/src/plugins/builtin/perps/pane.tsx +3 -1
  204. package/src/plugins/builtin/portfolio-list/cli/bulk-symbols.ts +17 -0
  205. package/src/plugins/builtin/portfolio-list/cli/portfolio-command.ts +2 -1
  206. package/src/plugins/builtin/portfolio-list/cli/watchlist-command.ts +2 -1
  207. package/src/plugins/builtin/portfolio-list/header.tsx +1 -1
  208. package/src/plugins/builtin/rate-path/headless.ts +24 -10
  209. package/src/plugins/builtin/research/corporate-actions-pane.tsx +29 -25
  210. package/src/plugins/builtin/research/event-model.ts +10 -3
  211. package/src/plugins/builtin/research/events-headless.ts +20 -2
  212. package/src/plugins/builtin/sec/fund-facts.ts +17 -1
  213. package/src/plugins/builtin/sec/index.tsx +1 -1
  214. package/src/plugins/builtin/series-inventory.ts +1 -0
  215. package/src/plugins/builtin/shared/earnings-report.ts +7 -2
  216. package/src/plugins/builtin/shared/members-menu.tsx +103 -38
  217. package/src/plugins/builtin/shared/use-quote-board.ts +85 -46
  218. package/src/plugins/builtin/short-interest/headless.ts +1 -1
  219. package/src/plugins/builtin/short-interest/model.ts +15 -6
  220. package/src/plugins/builtin/short-interest/publications-model.ts +22 -1
  221. package/src/plugins/builtin/short-interest/publications-pane.tsx +13 -4
  222. package/src/plugins/builtin/short-interest/types.ts +2 -0
  223. package/src/plugins/builtin/short-interest/watch-model.ts +1 -1
  224. package/src/plugins/builtin/thirteenf/overlap-pane.tsx +3 -0
  225. package/src/plugins/builtin/thirteenf/pane.tsx +3 -0
  226. package/src/plugins/builtin/ticker-detail/overview/model.ts +5 -3
  227. package/src/plugins/builtin/ticker-detail/overview-tab.tsx +4 -4
  228. package/src/plugins/builtin/time-sales/pane.tsx +3 -2
  229. package/src/plugins/builtin/trace-bonds/client.ts +7 -4
  230. package/src/plugins/builtin/trace-bonds/detail.tsx +198 -0
  231. package/src/plugins/builtin/trace-bonds/headless.ts +62 -4
  232. package/src/plugins/builtin/trace-bonds/history.ts +155 -0
  233. package/src/plugins/builtin/trace-bonds/index.tsx +1 -1
  234. package/src/plugins/builtin/trace-bonds/pane.tsx +64 -5
  235. package/src/plugins/builtin/volatility/client.ts +6 -1
  236. package/src/plugins/builtin/volatility/headless.ts +0 -1
  237. package/src/plugins/builtin/yield-curve/curves.ts +15 -0
  238. package/src/plugins/builtin/yield-curve/headless.ts +6 -1
  239. package/src/plugins/builtin/yield-curve/index.tsx +5 -8
  240. package/src/plugins/host-modules.ts +22 -3
  241. package/src/plugins/host-smoke.ts +6 -3
  242. package/src/plugins/installer.ts +16 -3
  243. package/src/plugins/plugin-names.ts +11 -2
  244. package/src/portfolio/collection-name.ts +19 -0
  245. package/src/remote/controller.ts +9 -2
  246. package/src/remote/schema.ts +2 -1
  247. package/src/renderers/browser/http-proxy-transport.ts +78 -8
  248. package/src/renderers/cloudflare/http-proxy.ts +73 -3
  249. package/src/renderers/cloudflare/proxy-session.ts +47 -24
  250. package/src/renderers/cloudflare/worker.ts +44 -4
  251. package/src/renderers/desktop/geometry.ts +279 -0
  252. package/src/renderers/desktop/screen-setup.ts +246 -0
  253. package/src/renderers/desktop/sequential-window-host.ts +179 -0
  254. package/src/renderers/desktop/test-utils.ts +71 -0
  255. package/src/renderers/desktop/window-controller.ts +433 -0
  256. package/src/renderers/desktop/window-host.ts +70 -0
  257. package/src/renderers/desktop/window-layouts.ts +630 -0
  258. package/src/renderers/dom/data-frames.ts +15 -4
  259. package/src/renderers/dom/desktop/icons.tsx +0 -2
  260. package/src/renderers/dom/styles.css +0 -3
  261. package/src/renderers/electrobun/bun/application-menu/click.ts +1 -0
  262. package/src/renderers/electrobun/bun/application-menu/index.ts +4 -1
  263. package/src/renderers/electrobun/bun/desktop/backend-requests.ts +19 -8
  264. package/src/renderers/electrobun/bun/desktop/backend.ts +320 -19
  265. package/src/renderers/electrobun/bun/desktop/detached-windows.ts +83 -61
  266. package/src/renderers/electrobun/bun/desktop/host-requests.ts +4 -14
  267. package/src/renderers/electrobun/bun/desktop/initialization.ts +30 -11
  268. package/src/renderers/electrobun/bun/desktop/install-records.ts +59 -0
  269. package/src/renderers/electrobun/bun/desktop/installer-folder.ts +208 -592
  270. package/src/renderers/electrobun/bun/desktop/macos-objc.ts +14 -0
  271. package/src/renderers/electrobun/bun/desktop/screen-setup-file.ts +55 -0
  272. package/src/renderers/electrobun/bun/desktop/state-broadcaster.ts +44 -8
  273. package/src/renderers/electrobun/bun/desktop/update.ts +1 -1
  274. package/src/renderers/electrobun/bun/desktop/workspace/index.ts +298 -97
  275. package/src/renderers/electrobun/bun/desktop/workspace/requests.ts +44 -6
  276. package/src/renderers/electrobun/bun/index.ts +22 -109
  277. package/src/renderers/electrobun/bun/window/create.ts +23 -3
  278. package/src/renderers/electrobun/bun/window/electrobun-window-host.ts +306 -0
  279. package/src/renderers/electrobun/bun/window/events.ts +4 -4
  280. package/src/renderers/electrobun/bun/window/focus.ts +0 -25
  281. package/src/renderers/electrobun/bun/window/frame.ts +2 -1
  282. package/src/renderers/electrobun/bun/window/native-min-size.ts +322 -0
  283. package/src/renderers/electrobun/bun/window/remembered-frame.ts +4 -82
  284. package/src/renderers/electrobun/shared/protocol.ts +44 -2
  285. package/src/renderers/electrobun/view/backend-rpc.ts +16 -1
  286. package/src/renderers/electrobun/view/desktop/window/bridge.ts +63 -3
  287. package/src/renderers/electrobun/view/main.tsx +13 -7
  288. package/src/series/fred-argument.ts +17 -0
  289. package/src/sources/gloomberb-cloud/index.ts +5 -7
  290. package/src/sources/provider-router/batches.ts +18 -3
  291. package/src/sources/provider-router/financial-routes.ts +7 -1
  292. package/src/sources/provider-router/financials.ts +18 -4
  293. package/src/state/app/context/index.tsx +16 -2
  294. package/src/state/desktop-windows.ts +63 -0
  295. package/src/sync/other-window-tabs.ts +62 -0
  296. package/src/tickers/company-name-listing.ts +23 -3
  297. package/src/tickers/listing-fate.ts +147 -1
  298. package/src/tickers/terminal-codes.ts +5 -0
  299. package/src/time-series/types.ts +6 -0
  300. package/src/types/config.ts +10 -1
  301. package/src/types/data-provider.ts +14 -0
  302. package/src/types/desktop-window.ts +71 -2
  303. package/src/types/financials.ts +19 -3
  304. package/src/types/headless.ts +21 -0
  305. package/src/types/plugin.ts +6 -0
  306. package/src/ui/index.tsx +14 -13
  307. package/src/ui/same-props.ts +58 -0
  308. package/src/utils/cli-output.ts +33 -4
  309. package/src/utils/double-escape-close.ts +38 -16
  310. package/src/utils/format.ts +30 -5
  311. package/src/utils/futures-generic.ts +12 -0
  312. package/src/utils/http-proxy-upstream.ts +6 -2
  313. package/src/utils/plugin-proxy-hosts.json +0 -4
  314. package/src/utils/plugin-proxy-hosts.ts +22 -0
  315. package/src/utils/price-earnings.ts +18 -0
  316. package/src/utils/sec-filings.ts +7 -1
  317. package/patches/electrobun@1.18.1.patch +0 -633
  318. package/src/api-client/types.ts +0 -2441
@@ -0,0 +1,246 @@
1
+ import { isUsableRect, type Rect } from "./geometry";
2
+
3
+ /**
4
+ * The desktop app's windows as this machine keeps them, beside the config
5
+ * and never synced: which layout tabs each extra window shows, and for each
6
+ * monitor arrangement where every window sat. A laptop that docks to two
7
+ * screens and undocks to its own has two arrangements, and each comes back
8
+ * as it was left.
9
+ */
10
+ export const MAIN_WINDOW_ID = "main";
11
+
12
+ export interface WindowPlacement {
13
+ frame: Rect;
14
+ maximized: boolean;
15
+ fullscreen: boolean;
16
+ /** The display the window was on (geometry.ts `displayKey`), when it was on one. */
17
+ display?: string;
18
+ }
19
+
20
+ interface ArrangementSetup {
21
+ /** When a window in this arrangement last moved; the oldest arrangements are dropped first. */
22
+ savedAt: number;
23
+ windows: Record<string, WindowPlacement>;
24
+ }
25
+
26
+ /** A window besides the main one: the layout tabs it shows, by saved layout id. The main window shows every other tab. */
27
+ export interface ExtraWindowRecord {
28
+ id: string;
29
+ layoutIds: string[];
30
+ activeLayoutId?: string;
31
+ }
32
+
33
+ export interface ScreenSetup {
34
+ version: 1;
35
+ windows: ExtraWindowRecord[];
36
+ arrangements: Record<string, ArrangementSetup>;
37
+ }
38
+
39
+ /** Arrangements kept: a home desk, an office, a projector or two. */
40
+ const MAX_ARRANGEMENTS = 12;
41
+ const MAX_EXTRA_WINDOWS = 16;
42
+
43
+ export function emptyScreenSetup(): ScreenSetup {
44
+ return { version: 1, windows: [], arrangements: {} };
45
+ }
46
+
47
+ function parsePlacement(raw: unknown): WindowPlacement | null {
48
+ if (!raw || typeof raw !== "object") return null;
49
+ const record = raw as Record<string, unknown>;
50
+ const frame = record.frame as Rect | undefined;
51
+ if (!isUsableRect(frame)) return null;
52
+ return {
53
+ frame: { x: frame.x, y: frame.y, width: frame.width, height: frame.height },
54
+ maximized: record.maximized === true,
55
+ fullscreen: record.fullscreen === true,
56
+ ...(typeof record.display === "string" && record.display ? { display: record.display } : {}),
57
+ };
58
+ }
59
+
60
+ function parseWindowRecord(raw: unknown): ExtraWindowRecord | null {
61
+ if (!raw || typeof raw !== "object") return null;
62
+ const record = raw as Record<string, unknown>;
63
+ if (typeof record.id !== "string" || !record.id || record.id === MAIN_WINDOW_ID) return null;
64
+ const layoutIds = Array.isArray(record.layoutIds)
65
+ ? [...new Set(record.layoutIds.filter((id): id is string => typeof id === "string" && id.length > 0))]
66
+ : [];
67
+ return {
68
+ id: record.id,
69
+ layoutIds,
70
+ ...(typeof record.activeLayoutId === "string" && record.activeLayoutId ? { activeLayoutId: record.activeLayoutId } : {}),
71
+ };
72
+ }
73
+
74
+ /** Reads what was saved, keeping every part that is well formed. Anything else starts empty. */
75
+ export function parseScreenSetup(raw: unknown): ScreenSetup {
76
+ if (!raw || typeof raw !== "object") return emptyScreenSetup();
77
+ const record = raw as Record<string, unknown>;
78
+ const seen = new Set<string>();
79
+ const windows = (Array.isArray(record.windows) ? record.windows : [])
80
+ .map(parseWindowRecord)
81
+ .filter((entry): entry is ExtraWindowRecord => {
82
+ if (!entry || seen.has(entry.id)) return false;
83
+ seen.add(entry.id);
84
+ return true;
85
+ })
86
+ .slice(0, MAX_EXTRA_WINDOWS);
87
+ const arrangements: Record<string, ArrangementSetup> = {};
88
+ const rawArrangements = record.arrangements && typeof record.arrangements === "object"
89
+ ? record.arrangements as Record<string, unknown>
90
+ : {};
91
+ for (const [key, value] of Object.entries(rawArrangements)) {
92
+ if (!value || typeof value !== "object") continue;
93
+ const entry = value as Record<string, unknown>;
94
+ const placements: Record<string, WindowPlacement> = {};
95
+ const rawWindows = entry.windows && typeof entry.windows === "object" ? entry.windows as Record<string, unknown> : {};
96
+ for (const [windowId, placement] of Object.entries(rawWindows)) {
97
+ const parsed = parsePlacement(placement);
98
+ if (parsed) placements[windowId] = parsed;
99
+ }
100
+ if (Object.keys(placements).length === 0) continue;
101
+ arrangements[key] = {
102
+ savedAt: typeof entry.savedAt === "number" && Number.isFinite(entry.savedAt) ? entry.savedAt : 0,
103
+ windows: placements,
104
+ };
105
+ }
106
+ return withoutStrayPlacements({ version: 1, windows, arrangements: pruneArrangements(arrangements) });
107
+ }
108
+
109
+ function pruneArrangements(arrangements: Record<string, ArrangementSetup>): Record<string, ArrangementSetup> {
110
+ const entries = Object.entries(arrangements);
111
+ if (entries.length <= MAX_ARRANGEMENTS) return arrangements;
112
+ return Object.fromEntries(
113
+ entries.sort(([, a], [, b]) => b.savedAt - a.savedAt).slice(0, MAX_ARRANGEMENTS),
114
+ );
115
+ }
116
+
117
+ export function placementFor(setup: ScreenSetup, arrangement: string, windowId: string): WindowPlacement | null {
118
+ return setup.arrangements[arrangement]?.windows[windowId] ?? null;
119
+ }
120
+
121
+ /**
122
+ * Where a window last sat under any other arrangement, for screens the setup
123
+ * has not seen: the most recently saved arrangement that has it.
124
+ */
125
+ export function latestPlacementFor(setup: ScreenSetup, windowId: string, except: string): WindowPlacement | null {
126
+ let latest: { savedAt: number; placement: WindowPlacement } | null = null;
127
+ for (const [key, entry] of Object.entries(setup.arrangements)) {
128
+ const placement = entry.windows[windowId];
129
+ if (key === except || !placement) continue;
130
+ if (!latest || entry.savedAt > latest.savedAt) latest = { savedAt: entry.savedAt, placement };
131
+ }
132
+ return latest?.placement ?? null;
133
+ }
134
+
135
+ export function hasArrangement(setup: ScreenSetup, arrangement: string): boolean {
136
+ return !!setup.arrangements[arrangement];
137
+ }
138
+
139
+ function samePlacement(a: WindowPlacement | null, b: WindowPlacement): boolean {
140
+ return !!a
141
+ && a.frame.x === b.frame.x
142
+ && a.frame.y === b.frame.y
143
+ && a.frame.width === b.frame.width
144
+ && a.frame.height === b.frame.height
145
+ && a.maximized === b.maximized
146
+ && a.fullscreen === b.fullscreen
147
+ && a.display === b.display;
148
+ }
149
+
150
+ /** Records where a window is in this arrangement. Returns the same object when nothing changed. */
151
+ export function rememberPlacement(
152
+ setup: ScreenSetup,
153
+ arrangement: string,
154
+ windowId: string,
155
+ placement: WindowPlacement,
156
+ now: number,
157
+ ): ScreenSetup {
158
+ const current = setup.arrangements[arrangement];
159
+ if (samePlacement(current?.windows[windowId] ?? null, placement)) return setup;
160
+ return {
161
+ ...setup,
162
+ arrangements: pruneArrangements({
163
+ ...setup.arrangements,
164
+ [arrangement]: {
165
+ savedAt: now,
166
+ windows: { ...current?.windows, [windowId]: placement },
167
+ },
168
+ }),
169
+ };
170
+ }
171
+
172
+ /** A window that closed for good: its tabs went back to the main window, and no arrangement opens it again. */
173
+ export function forgetWindow(setup: ScreenSetup, windowId: string): ScreenSetup {
174
+ if (windowId === MAIN_WINDOW_ID) return setup;
175
+ const arrangements: Record<string, ArrangementSetup> = {};
176
+ for (const [key, entry] of Object.entries(setup.arrangements)) {
177
+ const { [windowId]: _removed, ...rest } = entry.windows;
178
+ if (Object.keys(rest).length > 0) arrangements[key] = { ...entry, windows: rest };
179
+ }
180
+ return {
181
+ ...setup,
182
+ windows: setup.windows.filter((entry) => entry.id !== windowId),
183
+ arrangements,
184
+ };
185
+ }
186
+
187
+ /**
188
+ * Puts back a window `forgetWindow` took out: its tab list and its
189
+ * placements, as `before` had them. Placements saved since stay.
190
+ */
191
+ export function restoreWindow(setup: ScreenSetup, before: ScreenSetup, windowId: string): ScreenSetup {
192
+ const record = before.windows.find((entry) => entry.id === windowId);
193
+ if (!record) return setup;
194
+ const arrangements = { ...setup.arrangements };
195
+ for (const [key, entry] of Object.entries(before.arrangements)) {
196
+ const placement = entry.windows[windowId];
197
+ if (!placement) continue;
198
+ const current = arrangements[key] ?? { ...entry, windows: {} };
199
+ if (current.windows[windowId]) continue;
200
+ arrangements[key] = { ...current, windows: { ...current.windows, [windowId]: placement } };
201
+ }
202
+ return {
203
+ ...setup,
204
+ windows: setup.windows.some((entry) => entry.id === windowId) ? setup.windows : [...setup.windows, record],
205
+ arrangements,
206
+ };
207
+ }
208
+
209
+ /**
210
+ * Only the placements of windows that can open: the main window and the
211
+ * extra windows listed. One whose tabs are gone was not restored, and its
212
+ * placements would stay for good.
213
+ */
214
+ function withoutStrayPlacements(setup: ScreenSetup): ScreenSetup {
215
+ const kept = new Set([MAIN_WINDOW_ID, ...setup.windows.map((entry) => entry.id)]);
216
+ let changed = false;
217
+ const arrangements: Record<string, ArrangementSetup> = {};
218
+ for (const [key, entry] of Object.entries(setup.arrangements)) {
219
+ const windows = Object.fromEntries(Object.entries(entry.windows).filter(([windowId]) => kept.has(windowId)));
220
+ const count = Object.keys(windows).length;
221
+ if (count === Object.keys(entry.windows).length) arrangements[key] = entry;
222
+ else {
223
+ changed = true;
224
+ if (count > 0) arrangements[key] = { ...entry, windows };
225
+ }
226
+ }
227
+ return changed ? { ...setup, arrangements } : setup;
228
+ }
229
+
230
+ /** Replaces the extra windows' tab lists, as the workspace has them now; a window no longer listed loses its placements. */
231
+ export function withExtraWindows(setup: ScreenSetup, windows: readonly ExtraWindowRecord[]): ScreenSetup {
232
+ const next = windows.slice(0, MAX_EXTRA_WINDOWS).map((entry) => ({
233
+ id: entry.id,
234
+ layoutIds: [...entry.layoutIds],
235
+ ...(entry.activeLayoutId ? { activeLayoutId: entry.activeLayoutId } : {}),
236
+ }));
237
+ return withoutStrayPlacements(JSON.stringify(next) === JSON.stringify(setup.windows) ? setup : { ...setup, windows: next });
238
+ }
239
+
240
+ let windowSeq = 0;
241
+
242
+ /** A new extra window's id: unique on this machine, and never `main`. */
243
+ export function createExtraWindowId(now = Date.now()): string {
244
+ windowSeq += 1;
245
+ return `window:${now.toString(36)}${windowSeq.toString(36)}${Math.random().toString(36).slice(2, 6)}`;
246
+ }
@@ -0,0 +1,179 @@
1
+ import type { DesktopDisplay, Rect } from "./geometry";
2
+ import type { DesktopWindowHost, NativeWindowEvent, NativeWindowState, OpenWindowSpec } from "./window-host";
3
+
4
+ /** How long a new window has to draw before it is opened again, and then before the next one opens anyway. */
5
+ const DRAW_TIMEOUT_MS = 15_000;
6
+
7
+ interface WaitingWindow {
8
+ spec: OpenWindowSpec;
9
+ maximized?: boolean;
10
+ fullscreen?: boolean;
11
+ focus?: boolean;
12
+ }
13
+
14
+ interface OpeningWindow {
15
+ spec: OpenWindowSpec;
16
+ reopened: boolean;
17
+ timer: unknown;
18
+ }
19
+
20
+ export interface SequentialWindowHostOptions {
21
+ /**
22
+ * Whether a window that has not drawn may be closed and opened once more.
23
+ * Not the main window: on some systems closing it ends the app.
24
+ */
25
+ canReopen?: (spec: OpenWindowSpec) => boolean;
26
+ /** A window that drew nothing, even opened again; the next one opens anyway. */
27
+ onNeverDrew?: (id: string) => void;
28
+ drawTimeoutMs?: number;
29
+ setTimer?: (callback: () => void, ms: number) => unknown;
30
+ clearTimer?: (handle: unknown) => void;
31
+ }
32
+
33
+ /**
34
+ * Opens native windows one at a time: the next once the last one's page has
35
+ * drawn (`windowDrawn`). Asked for three new windows at once, the shell can
36
+ * leave one blank for good; one that has not drawn in time is closed and
37
+ * opened once more, and after that the next window opens anyway.
38
+ *
39
+ * A window waiting its turn already counts as open: a frame, mode, title or
40
+ * focus asked for it is kept for when it opens. Everything else goes
41
+ * straight to the shell, and its events come from the shell.
42
+ */
43
+ export class SequentialWindowHost implements DesktopWindowHost {
44
+ private readonly waiting: WaitingWindow[] = [];
45
+ private opening: OpeningWindow | null = null;
46
+ private readonly drawTimeoutMs: number;
47
+ private readonly setTimer: (callback: () => void, ms: number) => unknown;
48
+ private readonly clearTimer: (handle: unknown) => void;
49
+
50
+ constructor(private readonly shell: DesktopWindowHost, private readonly options: SequentialWindowHostOptions = {}) {
51
+ this.drawTimeoutMs = options.drawTimeoutMs ?? DRAW_TIMEOUT_MS;
52
+ this.setTimer = options.setTimer ?? ((callback, ms) => setTimeout(callback, ms));
53
+ this.clearTimer = options.clearTimer ?? ((handle) => clearTimeout(handle as ReturnType<typeof setTimeout>));
54
+ // The person closed the window that was opening: the next one need not wait for it.
55
+ shell.onWindowEvent((event) => {
56
+ if (event.type === "close" && this.opening?.spec.id === event.id) this.openNext();
57
+ });
58
+ }
59
+
60
+ get canPlaceWindows(): boolean {
61
+ return this.shell.canPlaceWindows;
62
+ }
63
+
64
+ get canFullscreen(): boolean {
65
+ return this.shell.canFullscreen;
66
+ }
67
+
68
+ /** A window's page has drawn: the next window may open. */
69
+ windowDrawn(id: string): void {
70
+ if (this.opening?.spec.id === id) this.openNext();
71
+ }
72
+
73
+ listDisplays(): DesktopDisplay[] {
74
+ return this.shell.listDisplays();
75
+ }
76
+
77
+ openWindow(spec: OpenWindowSpec): void {
78
+ if (this.hasWindow(spec.id)) return;
79
+ this.waiting.push({ spec: { ...spec, frame: { ...spec.frame } } });
80
+ if (!this.opening) this.openNext();
81
+ }
82
+
83
+ closeWindow(id: string): void {
84
+ const index = this.waiting.findIndex((entry) => entry.spec.id === id);
85
+ if (index >= 0) {
86
+ this.waiting.splice(index, 1);
87
+ return;
88
+ }
89
+ this.shell.closeWindow(id);
90
+ if (this.opening?.spec.id === id) this.openNext();
91
+ }
92
+
93
+ hasWindow(id: string): boolean {
94
+ return this.shell.hasWindow(id) || !!this.waitingWindow(id);
95
+ }
96
+
97
+ getWindowState(id: string): NativeWindowState | null {
98
+ return this.shell.getWindowState(id);
99
+ }
100
+
101
+ setFrame(id: string, frame: Rect): void {
102
+ const waiting = this.waitingWindow(id);
103
+ if (waiting) waiting.spec = { ...waiting.spec, frame: { ...frame } };
104
+ else this.shell.setFrame(id, frame);
105
+ }
106
+
107
+ setMaximized(id: string, maximized: boolean): void {
108
+ const waiting = this.waitingWindow(id);
109
+ if (waiting) waiting.maximized = maximized;
110
+ else this.shell.setMaximized(id, maximized);
111
+ }
112
+
113
+ setFullscreen(id: string, fullscreen: boolean): void {
114
+ const waiting = this.waitingWindow(id);
115
+ if (waiting) waiting.fullscreen = fullscreen;
116
+ else this.shell.setFullscreen(id, fullscreen);
117
+ }
118
+
119
+ setTitle(id: string, title: string): void {
120
+ const waiting = this.waitingWindow(id);
121
+ if (waiting) waiting.spec = { ...waiting.spec, title };
122
+ else this.shell.setTitle(id, title);
123
+ }
124
+
125
+ focusWindow(id: string): void {
126
+ const waiting = this.waitingWindow(id);
127
+ if (waiting) waiting.focus = true;
128
+ else this.shell.focusWindow(id);
129
+ }
130
+
131
+ onDisplaysChanged(listener: (displays: DesktopDisplay[]) => void): () => void {
132
+ return this.shell.onDisplaysChanged(listener);
133
+ }
134
+
135
+ onWindowEvent(listener: (event: NativeWindowEvent) => void): () => void {
136
+ return this.shell.onWindowEvent(listener);
137
+ }
138
+
139
+ private waitingWindow(id: string): WaitingWindow | undefined {
140
+ return this.waiting.find((entry) => entry.spec.id === id);
141
+ }
142
+
143
+ /** The window that was opening is done with, drawn or not: the next in line opens. */
144
+ private openNext(): void {
145
+ if (this.opening) this.clearTimer(this.opening.timer);
146
+ this.opening = null;
147
+ const next = this.waiting.shift();
148
+ if (next) this.open(next, false);
149
+ }
150
+
151
+ private open(entry: WaitingWindow, reopened: boolean): void {
152
+ const { spec } = entry;
153
+ // Set before the shell opens it, in case the page reports in at once.
154
+ this.opening = { spec, reopened, timer: this.setTimer(() => this.timedOut(spec.id), this.drawTimeoutMs) };
155
+ this.shell.openWindow(spec);
156
+ if (entry.fullscreen && this.shell.canFullscreen) this.shell.setFullscreen(spec.id, true);
157
+ else if (entry.maximized) this.shell.setMaximized(spec.id, true);
158
+ if (entry.focus) this.shell.focusWindow(spec.id);
159
+ }
160
+
161
+ private timedOut(id: string): void {
162
+ const opening = this.opening;
163
+ if (!opening || opening.spec.id !== id) return;
164
+ if (!opening.reopened && this.shell.hasWindow(id) && (this.options.canReopen?.(opening.spec) ?? true)) {
165
+ // Opened again where it is now and in the mode it is in.
166
+ const state = this.shell.getWindowState(id);
167
+ const windowed = !!state && !state.maximized && !state.fullscreen && !state.minimized;
168
+ this.shell.closeWindow(id);
169
+ this.open({
170
+ spec: { ...opening.spec, frame: windowed ? { ...state.frame } : opening.spec.frame },
171
+ maximized: state?.maximized,
172
+ fullscreen: state?.fullscreen,
173
+ }, true);
174
+ return;
175
+ }
176
+ this.options.onNeverDrew?.(id);
177
+ this.openNext();
178
+ }
179
+ }
@@ -0,0 +1,71 @@
1
+ import type { DesktopDisplay, Rect } from "./geometry";
2
+ import type { DesktopWindowHost, NativeWindowEvent, NativeWindowState, OpenWindowSpec } from "./window-host";
3
+
4
+ /** A shell in memory: windows are frames and flags, and every change is synchronous. */
5
+ export class FakeHost implements DesktopWindowHost {
6
+ canPlaceWindows = true;
7
+ canFullscreen = true;
8
+ displays: DesktopDisplay[];
9
+ readonly windows = new Map<string, NativeWindowState & { spec: OpenWindowSpec }>();
10
+ /** Every window the shell was asked to open, in order, opened again ones included. */
11
+ readonly opened: string[] = [];
12
+ private readonly closing = new Map<string, NativeWindowState & { spec: OpenWindowSpec }>();
13
+ private displayListeners = new Set<(displays: DesktopDisplay[]) => void>();
14
+ private windowListeners = new Set<(event: NativeWindowEvent) => void>();
15
+
16
+ constructor(displays: DesktopDisplay[]) {
17
+ this.displays = displays;
18
+ }
19
+
20
+ listDisplays() { return this.displays; }
21
+ openWindow(spec: OpenWindowSpec) {
22
+ this.opened.push(spec.id);
23
+ this.windows.set(spec.id, { spec, frame: spec.frame, maximized: false, fullscreen: false, minimized: false });
24
+ }
25
+ closeWindow(id: string) { this.windows.delete(id); }
26
+ hasWindow(id: string) { return this.windows.has(id); }
27
+ getWindowState(id: string) {
28
+ const window = this.windows.get(id) ?? this.closing.get(id);
29
+ return window ? { frame: window.frame, maximized: window.maximized, fullscreen: window.fullscreen, minimized: window.minimized } : null;
30
+ }
31
+ setFrame(id: string, frame: Rect) {
32
+ const window = this.windows.get(id);
33
+ if (window) window.frame = frame;
34
+ }
35
+ setMaximized(id: string, maximized: boolean) {
36
+ const window = this.windows.get(id);
37
+ if (window) window.maximized = maximized;
38
+ }
39
+ setFullscreen(id: string, fullscreen: boolean) {
40
+ const window = this.windows.get(id);
41
+ if (window) window.fullscreen = fullscreen;
42
+ }
43
+ setTitle() {}
44
+ focusWindow() {}
45
+ onDisplaysChanged(listener: (displays: DesktopDisplay[]) => void) {
46
+ this.displayListeners.add(listener);
47
+ return () => this.displayListeners.delete(listener);
48
+ }
49
+ onWindowEvent(listener: (event: NativeWindowEvent) => void) {
50
+ this.windowListeners.add(listener);
51
+ return () => this.windowListeners.delete(listener);
52
+ }
53
+
54
+ /** The person or the system moved a window. */
55
+ move(id: string, frame: Rect, flags: Partial<NativeWindowState> = {}) {
56
+ Object.assign(this.windows.get(id)!, { frame, ...flags });
57
+ for (const listener of this.windowListeners) listener({ type: "frame", id });
58
+ }
59
+ /** Closed from outside: gone, though its last state answers while the close is reported. */
60
+ close(id: string) {
61
+ const window = this.windows.get(id);
62
+ this.windows.delete(id);
63
+ if (window) this.closing.set(id, window);
64
+ for (const listener of this.windowListeners) listener({ type: "close", id });
65
+ this.closing.delete(id);
66
+ }
67
+ setDisplays(displays: DesktopDisplay[]) {
68
+ this.displays = displays;
69
+ for (const listener of this.displayListeners) listener(displays);
70
+ }
71
+ }