gloomberb 0.19.4 → 0.19.6

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 (321) hide show
  1. package/package.json +3 -1
  2. package/patches/@opentui%2Fcore@0.3.2.patch +86 -25
  3. package/src/api-client/equity-screener.ts +23 -0
  4. package/src/app/pane-runtime/index.tsx +2 -0
  5. package/src/app/pane-runtime/plugin-bindings.ts +10 -1
  6. package/src/app.tsx +63 -16
  7. package/src/cli/commands/command-utils.ts +4 -0
  8. package/src/cli/commands/market.ts +264 -33
  9. package/src/cli/commands/search.ts +22 -4
  10. package/src/cli/commands/ticker.ts +84 -39
  11. package/src/cli/currency-name-hint.ts +60 -0
  12. package/src/cli/glossary.ts +3 -3
  13. package/src/cli/history-rows.ts +18 -1
  14. package/src/cli/known-gaps.ts +25 -2
  15. package/src/cli/listing-arg.ts +12 -0
  16. package/src/cli/not-a-ticker.ts +5 -3
  17. package/src/cli/pane-functions/capabilities.ts +2 -0
  18. package/src/cli/pane-functions/catalog.ts +31 -5
  19. package/src/cli/pane-functions/function-help.ts +8 -5
  20. package/src/cli/pane-functions/headless.ts +11 -6
  21. package/src/cli/pane-functions/market-session.ts +6 -7
  22. package/src/cli/pane-functions/resolver.ts +75 -2
  23. package/src/cli/quote-answer.ts +9 -0
  24. package/src/cli/series-search.ts +4 -5
  25. package/src/cli/symbol-list.ts +54 -2
  26. package/src/cli/symbol-rows.ts +14 -2
  27. package/src/cli/ticker-resolution.ts +89 -20
  28. package/src/components/chart/composite/axis-overlays.tsx +60 -27
  29. package/src/components/chart/composite/composite-chart.tsx +118 -63
  30. package/src/components/chart/composite/cursor-source.ts +111 -0
  31. package/src/components/chart/composite/format.ts +168 -6
  32. package/src/components/chart/composite/legend.tsx +8 -4
  33. package/src/components/chart/composite/panel-bitmap.ts +28 -2
  34. package/src/components/chart/composite/panel-labels.ts +20 -1
  35. package/src/components/chart/composite/panel-surface.tsx +201 -92
  36. package/src/components/chart/composite/price-axis-labels.tsx +92 -46
  37. package/src/components/chart/composite/price-series.ts +31 -8
  38. package/src/components/chart/composite/scene.ts +154 -59
  39. package/src/components/chart/composite/series-axis-tags.tsx +4 -2
  40. package/src/components/chart/composite/time-axis.ts +30 -7
  41. package/src/components/chart/composite/time-scale.ts +211 -56
  42. package/src/components/chart/composite/types.ts +11 -1
  43. package/src/components/chart/composite/use-composite-chart-keyboard.ts +32 -12
  44. package/src/components/chart/curve/curve-surface.tsx +5 -2
  45. package/src/components/chart/curve/model.ts +4 -1
  46. package/src/components/chart/native/use-bitmap-metrics.ts +81 -0
  47. package/src/components/chart/static/series.ts +3 -0
  48. package/src/components/command-bar/commands/collection-list.ts +81 -0
  49. package/src/components/command-bar/commands/direct/index.ts +6 -0
  50. package/src/components/command-bar/commands/registry.ts +8 -0
  51. package/src/components/command-bar/layout-items/current-layout.ts +22 -1
  52. package/src/components/command-bar/layout-items/focused-pane.ts +22 -1
  53. package/src/components/command-bar/pane-templates/items.ts +19 -3
  54. package/src/components/command-bar/pane-templates/workflow.ts +5 -0
  55. package/src/components/command-bar/routes/root/results.ts +23 -3
  56. package/src/components/command-bar/routes/root/selection.ts +4 -2
  57. package/src/components/command-bar/routes/root/shortcut-feedback.ts +1 -1
  58. package/src/components/command-bar/routes/root/shortcut-items.ts +3 -4
  59. package/src/components/command-bar/routes/root/shortcuts.ts +58 -14
  60. package/src/components/command-bar/routes/root/ticker-first.ts +32 -0
  61. package/src/components/command-bar/surface/environment.ts +2 -0
  62. package/src/components/command-bar/view-model.ts +7 -0
  63. package/src/components/command-bar/workflow/ops.ts +2 -1
  64. package/src/components/command-bar/workflow/tickers.ts +20 -0
  65. package/src/components/layout/shell/active-drag.ts +38 -7
  66. package/src/components/layout/shell/drag/magnets.ts +106 -0
  67. package/src/components/layout/shell/menu.ts +15 -1
  68. package/src/components/layout/shell/pane/actions.ts +4 -0
  69. package/src/components/layout/shell/ticker-link-menu.ts +12 -3
  70. package/src/components/layout/status-bar.tsx +20 -0
  71. package/src/components/layout/window-controls.tsx +96 -35
  72. package/src/components/layout/window-fullscreen.ts +20 -0
  73. package/src/components/onboarding/onboarding-keyboard.ts +5 -1
  74. package/src/components/onboarding/onboarding-wizard.tsx +15 -4
  75. package/src/components/onboarding/portfolio-step/positions-panel.tsx +8 -5
  76. package/src/components/onboarding/use-onboarding-progress.ts +20 -1
  77. package/src/components/price-sparkline/view.tsx +14 -6
  78. package/src/components/quote-flash.ts +36 -32
  79. package/src/components/ticker/badge/index.tsx +8 -4
  80. package/src/components/ticker/badge/list.tsx +23 -7
  81. package/src/components/ui/column-grid.tsx +8 -2
  82. package/src/components/ui/data-table/opentui/model.ts +4 -1
  83. package/src/components/ui/tabs.tsx +5 -2
  84. package/src/core/state/app/layout.ts +18 -3
  85. package/src/core/state/app/state.ts +2 -0
  86. package/src/core/state/app/types.ts +5 -0
  87. package/src/data/config/layout.ts +3 -0
  88. package/src/data/config/store/normalize.ts +39 -4
  89. package/src/i18n/es.ts +10 -0
  90. package/src/i18n/ja.ts +10 -0
  91. package/src/i18n/ko.ts +10 -0
  92. package/src/i18n/zh-cn.ts +10 -0
  93. package/src/i18n/zh-tw.ts +10 -0
  94. package/src/layout/pane-manager/floating-placement.ts +7 -4
  95. package/src/layout/pane-manager/floating.ts +5 -2
  96. package/src/market-data/bond-funds.ts +19 -0
  97. package/src/market-data/coordinator/events.ts +17 -1
  98. package/src/market-data/frame-scheduler.ts +47 -10
  99. package/src/market-data/frame-wake.ts +74 -0
  100. package/src/market-data/futures-board.ts +7 -6
  101. package/src/market-data/futures-contract.ts +14 -3
  102. package/src/market-data/hooks.tsx +12 -3
  103. package/src/market-data/market/freshness.ts +18 -0
  104. package/src/market-data/market/status.ts +15 -2
  105. package/src/market-data/performance.ts +78 -9
  106. package/src/market-data/request-types.ts +6 -3
  107. package/src/plugins/builtin/alerts/cli.ts +2 -1
  108. package/src/plugins/builtin/analytics/risk-evidence.ts +1 -0
  109. package/src/plugins/builtin/attention/evidence.ts +1 -1
  110. package/src/plugins/builtin/awards/client.ts +1 -1
  111. package/src/plugins/builtin/awards/evidence.ts +1 -1
  112. package/src/plugins/builtin/awards/pane.tsx +2 -1
  113. package/src/plugins/builtin/bond-calculator/headless.ts +2 -1
  114. package/src/plugins/builtin/bond-calculator/index.tsx +1 -1
  115. package/src/plugins/builtin/bond-calculator/math.ts +1 -1
  116. package/src/plugins/builtin/bond-calculator/model.ts +14 -5
  117. package/src/plugins/builtin/bond-calculator/pane.tsx +1 -1
  118. package/src/plugins/builtin/broker-manager/footer.ts +3 -2
  119. package/src/plugins/builtin/broker-manager/index.tsx +2 -1
  120. package/src/plugins/builtin/broker-manager/keyboard.ts +2 -1
  121. package/src/plugins/builtin/buildout/cells.tsx +25 -45
  122. package/src/plugins/builtin/buildout/detail/company.tsx +3 -3
  123. package/src/plugins/builtin/buildout/detail/index.tsx +1 -1
  124. package/src/plugins/builtin/buildout/detail/intel.tsx +4 -7
  125. package/src/plugins/builtin/buildout/detail/site.tsx +4 -4
  126. package/src/plugins/builtin/buildout/detail/ui.tsx +39 -38
  127. package/src/plugins/builtin/buildout/format.ts +23 -1
  128. package/src/plugins/builtin/buildout/model/types.ts +1 -0
  129. package/src/plugins/builtin/buildout/pane/index.tsx +2 -8
  130. package/src/plugins/builtin/buildout/table-model.ts +12 -4
  131. package/src/plugins/builtin/catalysts/evidence.ts +1 -1
  132. package/src/plugins/builtin/catalysts/pane.tsx +2 -1
  133. package/src/plugins/builtin/cds/headless.ts +1 -0
  134. package/src/plugins/builtin/cds/index.tsx +1 -1
  135. package/src/plugins/builtin/chart-composer/headless.ts +40 -1
  136. package/src/plugins/builtin/cloud/plugin.tsx +2 -0
  137. package/src/plugins/builtin/cloud/upgrade-status-widget.tsx +28 -3
  138. package/src/plugins/builtin/cloud-tweets/table.tsx +3 -2
  139. package/src/plugins/builtin/comment-letters/client.ts +2 -1
  140. package/src/plugins/builtin/company-kpis/client.ts +1 -1
  141. package/src/plugins/builtin/company-kpis/pane.tsx +2 -1
  142. package/src/plugins/builtin/company-kpis/screenshot-evidence.ts +3 -2
  143. package/src/plugins/builtin/correlation/headless.ts +3 -2
  144. package/src/plugins/builtin/cot/headless.ts +9 -3
  145. package/src/plugins/builtin/cot/model.ts +44 -3
  146. package/src/plugins/builtin/credit-documents/client.ts +1 -1
  147. package/src/plugins/builtin/credit-documents/evidence.ts +1 -1
  148. package/src/plugins/builtin/credit-documents/pane.tsx +2 -1
  149. package/src/plugins/builtin/crypto-board/pane.tsx +5 -4
  150. package/src/plugins/builtin/debt-maturities/headless.ts +11 -2
  151. package/src/plugins/builtin/debt-maturities/model.ts +66 -0
  152. package/src/plugins/builtin/debt-maturities/pane.tsx +23 -4
  153. package/src/plugins/builtin/earnings-calls/pane.tsx +1 -1
  154. package/src/plugins/builtin/earnings-ripple/index.tsx +6 -1
  155. package/src/plugins/builtin/earnings-ripple/pane.tsx +1 -1
  156. package/src/plugins/builtin/econ/calendar-model.ts +30 -6
  157. package/src/plugins/builtin/econ/fred-catalog.ts +5 -5
  158. package/src/plugins/builtin/econ/print-watch.ts +3 -1
  159. package/src/plugins/builtin/equity-screener/client.ts +1 -1
  160. package/src/plugins/builtin/equity-screener/criterion-editor.tsx +4 -16
  161. package/src/plugins/builtin/equity-screener/headless.ts +4 -2
  162. package/src/plugins/builtin/equity-screener/ideas.ts +67 -3
  163. package/src/plugins/builtin/equity-screener/model.ts +180 -12
  164. package/src/plugins/builtin/equity-screener/pane.tsx +52 -12
  165. package/src/plugins/builtin/equity-screener/screen-check.ts +7 -4
  166. package/src/plugins/builtin/equity-screener/watchlist.ts +33 -0
  167. package/src/plugins/builtin/futures/series.ts +16 -1
  168. package/src/plugins/builtin/futures-curve/client.ts +19 -0
  169. package/src/plugins/builtin/futures-curve/headless.ts +52 -13
  170. package/src/plugins/builtin/futures-curve/index.tsx +1 -1
  171. package/src/plugins/builtin/futures-curve/model.ts +77 -6
  172. package/src/plugins/builtin/futures-curve/pane.tsx +19 -3
  173. package/src/plugins/builtin/fx-matrix/headless.ts +4 -2
  174. package/src/plugins/builtin/fx-matrix/index.tsx +37 -3
  175. package/src/plugins/builtin/fx-matrix/pairs.ts +15 -0
  176. package/src/plugins/builtin/gpu/headless.ts +2 -1
  177. package/src/plugins/builtin/help/function-card.tsx +2 -1
  178. package/src/plugins/builtin/help/index.tsx +2 -1
  179. package/src/plugins/builtin/insider/display.ts +45 -6
  180. package/src/plugins/builtin/ipo-calendar/model.ts +2 -0
  181. package/src/plugins/builtin/iv-history/client.ts +2 -1
  182. package/src/plugins/builtin/iv-history/headless.ts +2 -1
  183. package/src/plugins/builtin/jobs/pages.ts +4 -4
  184. package/src/plugins/builtin/kelly-sizer/headless.ts +2 -1
  185. package/src/plugins/builtin/macro-day/client.ts +41 -0
  186. package/src/plugins/builtin/macro-day/headless.ts +19 -9
  187. package/src/plugins/builtin/macro-day/model.ts +3 -1
  188. package/src/plugins/builtin/market-heatmap/index.tsx +2 -1
  189. package/src/plugins/builtin/market-movers/index.tsx +4 -3
  190. package/src/plugins/builtin/members/client.ts +1 -1
  191. package/src/plugins/builtin/mna/distress/client.ts +1 -0
  192. package/src/plugins/builtin/mna/distress/headless.ts +1 -0
  193. package/src/plugins/builtin/mna/pane.tsx +4 -1
  194. package/src/plugins/builtin/options/market-reference.ts +1 -1
  195. package/src/plugins/builtin/options/open-interest-as-of.ts +3 -1
  196. package/src/plugins/builtin/options/view.tsx +2 -1
  197. package/src/plugins/builtin/options-calculator/evidence.ts +1 -1
  198. package/src/plugins/builtin/options-scenario/csp-screen-pane.tsx +1 -1
  199. package/src/plugins/builtin/options-scenario/csp-screen.ts +8 -4
  200. package/src/plugins/builtin/options-scenario/csp.ts +28 -9
  201. package/src/plugins/builtin/options-scenario/evidence.ts +5 -4
  202. package/src/plugins/builtin/options-scenario/pane.tsx +2 -1
  203. package/src/plugins/builtin/perps/client.ts +1 -1
  204. package/src/plugins/builtin/perps/pane.tsx +3 -1
  205. package/src/plugins/builtin/portfolio-list/cli/bulk-symbols.ts +17 -0
  206. package/src/plugins/builtin/portfolio-list/cli/portfolio-command.ts +2 -1
  207. package/src/plugins/builtin/portfolio-list/cli/watchlist-command.ts +4 -2
  208. package/src/plugins/builtin/portfolio-list/pane/index.tsx +7 -3
  209. package/src/plugins/builtin/portfolio-list/use-throttled-memo.ts +10 -19
  210. package/src/plugins/builtin/rate-path/headless.ts +24 -10
  211. package/src/plugins/builtin/research/analyst-model.ts +1 -0
  212. package/src/plugins/builtin/research/equity-diagnostic-pane.tsx +2 -1
  213. package/src/plugins/builtin/research/event-model.ts +10 -3
  214. package/src/plugins/builtin/research/events-headless.ts +20 -2
  215. package/src/plugins/builtin/research/index.tsx +1 -0
  216. package/src/plugins/builtin/research/relative-valuation-currency.ts +1 -1
  217. package/src/plugins/builtin/research/relative-valuation-peers.ts +2 -1
  218. package/src/plugins/builtin/research-search/command-bar-search.ts +3 -1
  219. package/src/plugins/builtin/research-search/snippet.ts +1 -0
  220. package/src/plugins/builtin/revenue-breakdown/client.ts +3 -2
  221. package/src/plugins/builtin/seasonality/headless.ts +2 -1
  222. package/src/plugins/builtin/sec/command-bar-search.ts +3 -2
  223. package/src/plugins/builtin/sec/fund-facts.ts +17 -1
  224. package/src/plugins/builtin/sec/index.tsx +8 -5
  225. package/src/plugins/builtin/sec/row-summaries.ts +28 -12
  226. package/src/plugins/builtin/shared/members-menu.tsx +103 -38
  227. package/src/plugins/builtin/short-interest/publications-model.ts +22 -1
  228. package/src/plugins/builtin/short-interest/publications-pane.tsx +13 -4
  229. package/src/plugins/builtin/short-interest/watch-client.ts +2 -1
  230. package/src/plugins/builtin/short-interest/watch-model.ts +1 -1
  231. package/src/plugins/builtin/social-mentions/client.ts +2 -1
  232. package/src/plugins/builtin/supply-chain/client.ts +1 -1
  233. package/src/plugins/builtin/supply-chain/evidence.ts +1 -1
  234. package/src/plugins/builtin/supply-chain/graph-pane.tsx +2 -1
  235. package/src/plugins/builtin/supply-chain/pane.tsx +2 -1
  236. package/src/plugins/builtin/ticker-detail/adr-pair/headless.ts +1 -1
  237. package/src/plugins/builtin/ticker-detail/adr-pair/model.ts +24 -4
  238. package/src/plugins/builtin/ticker-detail/adr-pair/tab.tsx +1 -1
  239. package/src/plugins/builtin/time-sales/client.ts +1 -0
  240. package/src/plugins/builtin/time-sales/pane.tsx +3 -2
  241. package/src/plugins/builtin/trace-bonds/client.ts +7 -4
  242. package/src/plugins/builtin/trace-bonds/index.tsx +1 -1
  243. package/src/plugins/builtin/vol-surface/client.ts +2 -1
  244. package/src/plugins/builtin/volatility/client.ts +6 -1
  245. package/src/plugins/builtin/volatility/headless.ts +0 -1
  246. package/src/plugins/builtin/yield-curve/curves.ts +15 -0
  247. package/src/plugins/builtin/yield-curve/headless.ts +9 -2
  248. package/src/plugins/builtin/yield-curve/index.tsx +5 -8
  249. package/src/plugins/host-modules.ts +22 -3
  250. package/src/plugins/host-smoke.ts +6 -3
  251. package/src/portfolio/collection-name.ts +19 -0
  252. package/src/portfolio/column-values.ts +76 -10
  253. package/src/renderers/browser/http-proxy-transport.ts +78 -8
  254. package/src/renderers/cloudflare/http-proxy.ts +13 -2
  255. package/src/renderers/cloudflare/proxy-session.ts +47 -24
  256. package/src/renderers/cloudflare/worker.ts +15 -2
  257. package/src/renderers/desktop/geometry.ts +279 -0
  258. package/src/renderers/desktop/screen-setup.ts +246 -0
  259. package/src/renderers/desktop/sequential-window-host.ts +179 -0
  260. package/src/renderers/desktop/test-utils.ts +71 -0
  261. package/src/renderers/desktop/window-controller.ts +433 -0
  262. package/src/renderers/desktop/window-host.ts +70 -0
  263. package/src/renderers/desktop/window-layouts.ts +630 -0
  264. package/src/renderers/dom/data-frames.ts +15 -4
  265. package/src/renderers/dom/host/box.tsx +204 -176
  266. package/src/renderers/dom/host/style.ts +49 -20
  267. package/src/renderers/dom/host/text.tsx +3 -2
  268. package/src/renderers/electrobun/bun/application-menu/click.ts +1 -0
  269. package/src/renderers/electrobun/bun/application-menu/index.ts +4 -1
  270. package/src/renderers/electrobun/bun/desktop/backend-requests.ts +19 -8
  271. package/src/renderers/electrobun/bun/desktop/backend.ts +322 -19
  272. package/src/renderers/electrobun/bun/desktop/detached-windows.ts +83 -61
  273. package/src/renderers/electrobun/bun/desktop/host-requests.ts +29 -15
  274. package/src/renderers/electrobun/bun/desktop/initialization.ts +30 -11
  275. package/src/renderers/electrobun/bun/desktop/relaunch.ts +9 -1
  276. package/src/renderers/electrobun/bun/desktop/screen-setup-file.ts +55 -0
  277. package/src/renderers/electrobun/bun/desktop/state-broadcaster.ts +44 -8
  278. package/src/renderers/electrobun/bun/desktop/windows-script.ts +9 -6
  279. package/src/renderers/electrobun/bun/desktop/workspace/index.ts +298 -97
  280. package/src/renderers/electrobun/bun/desktop/workspace/requests.ts +44 -6
  281. package/src/renderers/electrobun/bun/index.ts +4 -97
  282. package/src/renderers/electrobun/bun/window/electrobun-window-host.ts +306 -0
  283. package/src/renderers/electrobun/bun/window/focus.ts +0 -25
  284. package/src/renderers/electrobun/bun/window/remembered-frame.ts +4 -82
  285. package/src/renderers/electrobun/bun/window/windows-native.ts +26 -1
  286. package/src/renderers/electrobun/shared/protocol.ts +50 -2
  287. package/src/renderers/electrobun/view/backend-rpc.ts +15 -0
  288. package/src/renderers/electrobun/view/desktop/window/bridge.ts +63 -3
  289. package/src/renderers/electrobun/view/main.tsx +13 -7
  290. package/src/renderers/electrobun/view/ui-host.tsx +7 -0
  291. package/src/renderers/electrobun/view/window-fullscreen.ts +10 -3
  292. package/src/series/fred-argument.ts +17 -0
  293. package/src/sources/provider-router/financial-routes.ts +7 -1
  294. package/src/sources/provider-router/index.ts +7 -1
  295. package/src/state/app/context/index.tsx +16 -2
  296. package/src/state/desktop-windows.ts +63 -0
  297. package/src/sync/other-window-tabs.ts +62 -0
  298. package/src/tickers/company-name-listing.ts +23 -3
  299. package/src/tickers/listing-fate.ts +147 -1
  300. package/src/tickers/terminal-codes.ts +5 -0
  301. package/src/time-series/price-units.ts +11 -0
  302. package/src/time-series/types.ts +6 -0
  303. package/src/types/config.ts +10 -1
  304. package/src/types/desktop-window.ts +71 -2
  305. package/src/types/financials.ts +4 -0
  306. package/src/types/headless.ts +19 -0
  307. package/src/types/plugin.ts +6 -0
  308. package/src/ui/host.tsx +2 -0
  309. package/src/ui/index.tsx +14 -13
  310. package/src/ui/same-props.ts +58 -0
  311. package/src/utils/cli-output.ts +33 -4
  312. package/src/utils/currency-names.ts +20 -0
  313. package/src/utils/currency-pair.ts +6 -0
  314. package/src/utils/exchange-rate-snapshot.ts +20 -2
  315. package/src/utils/format.ts +7 -2
  316. package/src/utils/futures-generic.ts +12 -0
  317. package/src/utils/fx-market-hours.ts +43 -0
  318. package/src/utils/http-proxy-upstream.ts +6 -2
  319. package/src/utils/plugin-proxy-hosts.json +0 -4
  320. package/src/utils/plugin-proxy-hosts.ts +22 -0
  321. package/src/utils/sec-filings.ts +7 -1
@@ -0,0 +1,279 @@
1
+ /**
2
+ * Screens and window frames for the desktop app, in the coordinates the
3
+ * shell reports them in: one virtual desktop where every display has its
4
+ * place. Nothing here talks to a shell; the window host does (window-host.ts).
5
+ */
6
+
7
+ export interface Rect {
8
+ x: number;
9
+ y: number;
10
+ width: number;
11
+ height: number;
12
+ }
13
+
14
+ export interface DesktopDisplay {
15
+ /** The shell's id. It can change when a display is plugged in again, so placements never key on it. */
16
+ id: string;
17
+ bounds: Rect;
18
+ /** The bounds less the menu bar, Dock or task bar. */
19
+ workArea: Rect;
20
+ scaleFactor: number;
21
+ primary: boolean;
22
+ }
23
+
24
+ /** How much of a title bar has to sit on a screen for a window to count as reachable: enough to grab and drag. */
25
+ const REACHABLE_STRIP_HEIGHT = 24;
26
+ const REACHABLE_STRIP_WIDTH = 96;
27
+ /**
28
+ * How far past a screen's edge a frame may reach and still count as on it:
29
+ * shadows and the invisible resize borders Windows counts in a frame.
30
+ */
31
+ const EDGE_SLACK = 8;
32
+
33
+ function finite(value: number): number {
34
+ return Number.isFinite(value) ? Math.round(value) : 0;
35
+ }
36
+
37
+ export function sameRect(a: Rect | null | undefined, b: Rect | null | undefined): boolean {
38
+ if (!a || !b) return a === b;
39
+ return a.x === b.x && a.y === b.y && a.width === b.width && a.height === b.height;
40
+ }
41
+
42
+ export function isUsableRect(rect: Rect | null | undefined): rect is Rect {
43
+ return !!rect
44
+ && [rect.x, rect.y, rect.width, rect.height].every((value) => typeof value === "number" && Number.isFinite(value))
45
+ && rect.width >= 1
46
+ && rect.height >= 1;
47
+ }
48
+
49
+ /** Displays the shell reported with a real size, primary first and then left to right, top to bottom. */
50
+ export function usableDisplays(displays: readonly DesktopDisplay[]): DesktopDisplay[] {
51
+ return displays
52
+ .filter((display) => isUsableRect(display.bounds) && isUsableRect(display.workArea))
53
+ .sort((a, b) => (
54
+ Number(b.primary) - Number(a.primary)
55
+ || a.bounds.x - b.bounds.x
56
+ || a.bounds.y - b.bounds.y
57
+ ));
58
+ }
59
+
60
+ /**
61
+ * One display, as placements remember it: where it sits, its size and its
62
+ * scale. A display unplugged and plugged back in the same place gets the
63
+ * same key, whatever id the shell gives it.
64
+ */
65
+ export function displayKey(display: DesktopDisplay): string {
66
+ const { x, y, width, height } = display.bounds;
67
+ const scale = Math.round((display.scaleFactor || 1) * 100) / 100;
68
+ return `${finite(x)},${finite(y)} ${finite(width)}x${finite(height)}@${scale}`;
69
+ }
70
+
71
+ /**
72
+ * The monitor arrangement a screen setup is saved under: every display's
73
+ * key, in a fixed order. Moving, adding or removing a display, or changing
74
+ * its resolution or scale, makes another arrangement.
75
+ */
76
+ export function arrangementKey(displays: readonly DesktopDisplay[]): string {
77
+ const keys = usableDisplays(displays).map(displayKey).sort();
78
+ return keys.length > 0 ? keys.join(" | ") : "none";
79
+ }
80
+
81
+ function overlapArea(a: Rect, b: Rect): number {
82
+ const width = Math.min(a.x + a.width, b.x + b.width) - Math.max(a.x, b.x);
83
+ const height = Math.min(a.y + a.height, b.y + b.height) - Math.max(a.y, b.y);
84
+ return width > 0 && height > 0 ? width * height : 0;
85
+ }
86
+
87
+ function clamp(value: number, min: number, max: number): number {
88
+ return Math.min(max, Math.max(min, value));
89
+ }
90
+
91
+ /** The display a frame is on: the one holding its centre, else the one it overlaps most. Null when it is on none. */
92
+ export function displayForFrame(frame: Rect, displays: readonly DesktopDisplay[]): DesktopDisplay | null {
93
+ const usable = usableDisplays(displays);
94
+ const centerX = frame.x + frame.width / 2;
95
+ const centerY = frame.y + frame.height / 2;
96
+ const containing = usable.find(({ bounds }) => (
97
+ centerX >= bounds.x && centerX < bounds.x + bounds.width
98
+ && centerY >= bounds.y && centerY < bounds.y + bounds.height
99
+ ));
100
+ if (containing) return containing;
101
+ let best: DesktopDisplay | null = null;
102
+ let bestArea = 0;
103
+ for (const display of usable) {
104
+ const area = overlapArea(frame, display.bounds);
105
+ if (area > bestArea) {
106
+ best = display;
107
+ bestArea = area;
108
+ }
109
+ }
110
+ return best;
111
+ }
112
+
113
+ /**
114
+ * Whether a person can reach the window to move it: a stretch of its title
115
+ * bar lies in some display's work area, not under the menu bar or off the
116
+ * edge of every screen.
117
+ */
118
+ export function isFrameReachable(frame: Rect, displays: readonly DesktopDisplay[]): boolean {
119
+ const titleBar: Rect = { x: frame.x, y: frame.y, width: frame.width, height: REACHABLE_STRIP_HEIGHT };
120
+ return usableDisplays(displays).some(({ workArea }) => {
121
+ const width = Math.min(titleBar.x + titleBar.width, workArea.x + workArea.width) - Math.max(titleBar.x, workArea.x);
122
+ const height = Math.min(titleBar.y + titleBar.height, workArea.y + workArea.height) - Math.max(titleBar.y, workArea.y);
123
+ return width >= Math.min(REACHABLE_STRIP_WIDTH, frame.width) && height >= Math.min(REACHABLE_STRIP_HEIGHT, frame.height);
124
+ });
125
+ }
126
+
127
+ /**
128
+ * Whether a window is on screen: all of it lies on the connected displays
129
+ * (across two of them counts), give or take its shadow, and its title bar
130
+ * sits in a work area where it can be grabbed.
131
+ */
132
+ export function isFrameVisible(frame: Rect, displays: readonly DesktopDisplay[]): boolean {
133
+ const usable = usableDisplays(displays);
134
+ const insetX = Math.min(EDGE_SLACK, Math.floor(frame.width / 4));
135
+ const insetY = Math.min(EDGE_SLACK, Math.floor(frame.height / 4));
136
+ const inner: Rect = {
137
+ x: frame.x + insetX,
138
+ y: frame.y + insetY,
139
+ width: frame.width - insetX * 2,
140
+ height: frame.height - insetY * 2,
141
+ };
142
+ // Mirrored displays share their bounds: count each place once.
143
+ const places = new Set<string>();
144
+ let covered = 0;
145
+ for (const { bounds } of usable) {
146
+ const place = `${bounds.x},${bounds.y},${bounds.width},${bounds.height}`;
147
+ if (places.has(place)) continue;
148
+ places.add(place);
149
+ covered += overlapArea(inner, bounds);
150
+ }
151
+ return covered >= inner.width * inner.height - 1 && isFrameReachable(frame, usable);
152
+ }
153
+
154
+ /** The display closest to a frame on none: the shortest way from its centre to a work area. */
155
+ function nearestDisplay(frame: Rect, displays: readonly DesktopDisplay[]): DesktopDisplay | null {
156
+ const centerX = frame.x + frame.width / 2;
157
+ const centerY = frame.y + frame.height / 2;
158
+ let best: DesktopDisplay | null = null;
159
+ let bestDistance = Infinity;
160
+ for (const display of displays) {
161
+ const area = display.workArea;
162
+ const dx = Math.max(area.x - centerX, 0, centerX - (area.x + area.width));
163
+ const dy = Math.max(area.y - centerY, 0, centerY - (area.y + area.height));
164
+ const distance = dx * dx + dy * dy;
165
+ if (distance < bestDistance) {
166
+ best = display;
167
+ bestDistance = distance;
168
+ }
169
+ }
170
+ return best;
171
+ }
172
+
173
+ /** A frame inside a work area, moved as little as it takes and shrunk to it when bigger. */
174
+ function fitFrameInArea(frame: Rect, area: Rect): Rect {
175
+ const width = Math.min(Math.round(frame.width), area.width);
176
+ const height = Math.min(Math.round(frame.height), area.height);
177
+ return {
178
+ x: clamp(Math.round(frame.x), area.x, area.x + area.width - width),
179
+ y: clamp(Math.round(frame.y), area.y, area.y + area.height - height),
180
+ width,
181
+ height,
182
+ };
183
+ }
184
+
185
+ /**
186
+ * Puts a window on a screen. One already on screen stays as it is. Any
187
+ * other moves onto the display it is mostly on, or the nearest one, by as
188
+ * little as it takes to lie in that display's work area, shrinking to it
189
+ * when it is bigger. Shells do not do this themselves: asked to open a
190
+ * window past a screen's edge, Electrobun leaves it there.
191
+ */
192
+ export function clampFrameToScreens(frame: Rect, displays: readonly DesktopDisplay[]): Rect {
193
+ const usable = usableDisplays(displays);
194
+ if (usable.length === 0 || isFrameVisible(frame, usable)) return frame;
195
+ const display = displayForFrame(frame, usable) ?? nearestDisplay(frame, usable) ?? usable[0]!;
196
+ return fitFrameInArea(frame, display.workArea);
197
+ }
198
+
199
+ /** A display as `displayKey` wrote it, for a placement saved under screens that are not all connected now. */
200
+ export function displayFromKey(key: string): DesktopDisplay | null {
201
+ const match = /^(-?\d+),(-?\d+) (\d+)x(\d+)@([\d.]+)$/.exec(key);
202
+ if (!match) return null;
203
+ const bounds = { x: Number(match[1]), y: Number(match[2]), width: Number(match[3]), height: Number(match[4]) };
204
+ if (!isUsableRect(bounds)) return null;
205
+ return { id: "", bounds, workArea: bounds, scaleFactor: Number(match[5]) || 1, primary: false };
206
+ }
207
+
208
+ /**
209
+ * The connected display a display that is gone became: the one at the same
210
+ * place (a new resolution keeps a screen's corner), else the one with its id.
211
+ */
212
+ function sameScreen(display: DesktopDisplay, displays: readonly DesktopDisplay[]): DesktopDisplay | null {
213
+ const { x, y } = display.bounds;
214
+ return displays.find(({ bounds }) => bounds.x === x && bounds.y === y)
215
+ ?? (display.id ? displays.find(({ id }) => id === display.id) : undefined)
216
+ ?? null;
217
+ }
218
+
219
+ /**
220
+ * Where a window that sat on `from` goes on the displays connected now,
221
+ * always on screen. Its display still there: where it was. The same screen
222
+ * at another resolution or place: the same place on it, in proportion.
223
+ * Its display gone: the same place in proportion on `fallback`. On no
224
+ * display at all: onto the nearest one.
225
+ */
226
+ export function relocateFrame(
227
+ frame: Rect,
228
+ from: DesktopDisplay | null,
229
+ displays: readonly DesktopDisplay[],
230
+ fallback: DesktopDisplay,
231
+ ): Rect {
232
+ const usable = usableDisplays(displays);
233
+ if (!from || usable.some((display) => displayKey(display) === displayKey(from))) {
234
+ return clampFrameToScreens(frame, usable);
235
+ }
236
+ const target = sameScreen(from, usable) ?? fallback;
237
+ return clampFrameToScreens(moveFrameToDisplay(frame, from, target), usable);
238
+ }
239
+
240
+ /**
241
+ * A window moving from one display to another keeps its place on the screen
242
+ * in proportion: a window on the right half of the old display lands on the
243
+ * right half of the new one, at the same size where it fits.
244
+ */
245
+ export function moveFrameToDisplay(frame: Rect, from: DesktopDisplay | null, to: DesktopDisplay): Rect {
246
+ const area = to.workArea;
247
+ const width = Math.min(frame.width, area.width);
248
+ const height = Math.min(frame.height, area.height);
249
+ if (!from) {
250
+ return {
251
+ x: area.x + Math.round((area.width - width) / 2),
252
+ y: area.y + Math.round((area.height - height) / 2),
253
+ width,
254
+ height,
255
+ };
256
+ }
257
+ const source = from.workArea;
258
+ const slackX = Math.max(0, source.width - frame.width);
259
+ const slackY = Math.max(0, source.height - frame.height);
260
+ const fractionX = slackX > 0 ? clamp((frame.x - source.x) / slackX, 0, 1) : 0.5;
261
+ const fractionY = slackY > 0 ? clamp((frame.y - source.y) / slackY, 0, 1) : 0;
262
+ return {
263
+ x: area.x + Math.round((area.width - width) * fractionX),
264
+ y: area.y + Math.round((area.height - height) * fractionY),
265
+ width,
266
+ height,
267
+ };
268
+ }
269
+
270
+ /** A frame `step` points down and to the right of `frame`, wrapped back to the work area's corner when it would leave it. */
271
+ export function cascadeFrame(frame: Rect, display: DesktopDisplay, step = 32): Rect {
272
+ const area = display.workArea;
273
+ const width = Math.min(frame.width, area.width);
274
+ const height = Math.min(frame.height, area.height);
275
+ const x = frame.x + step;
276
+ const y = frame.y + step;
277
+ const fits = x + width <= area.x + area.width && y + height <= area.y + area.height;
278
+ return fitFrameInArea(fits ? { x, y, width, height } : { x: area.x + step, y: area.y + step, width, height }, area);
279
+ }
@@ -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
+ }