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.
- package/package.json +3 -1
- package/patches/@opentui%2Fcore@0.3.2.patch +86 -25
- package/src/api-client/equity-screener.ts +23 -0
- package/src/app/pane-runtime/index.tsx +2 -0
- package/src/app/pane-runtime/plugin-bindings.ts +10 -1
- package/src/app.tsx +63 -16
- package/src/cli/commands/command-utils.ts +4 -0
- package/src/cli/commands/market.ts +264 -33
- package/src/cli/commands/search.ts +22 -4
- package/src/cli/commands/ticker.ts +84 -39
- package/src/cli/currency-name-hint.ts +60 -0
- package/src/cli/glossary.ts +3 -3
- package/src/cli/history-rows.ts +18 -1
- package/src/cli/known-gaps.ts +25 -2
- package/src/cli/listing-arg.ts +12 -0
- package/src/cli/not-a-ticker.ts +5 -3
- package/src/cli/pane-functions/capabilities.ts +2 -0
- package/src/cli/pane-functions/catalog.ts +31 -5
- package/src/cli/pane-functions/function-help.ts +8 -5
- package/src/cli/pane-functions/headless.ts +11 -6
- package/src/cli/pane-functions/market-session.ts +6 -7
- package/src/cli/pane-functions/resolver.ts +75 -2
- package/src/cli/quote-answer.ts +9 -0
- package/src/cli/series-search.ts +4 -5
- package/src/cli/symbol-list.ts +54 -2
- package/src/cli/symbol-rows.ts +14 -2
- package/src/cli/ticker-resolution.ts +89 -20
- package/src/components/chart/composite/axis-overlays.tsx +60 -27
- package/src/components/chart/composite/composite-chart.tsx +118 -63
- package/src/components/chart/composite/cursor-source.ts +111 -0
- package/src/components/chart/composite/format.ts +168 -6
- package/src/components/chart/composite/legend.tsx +8 -4
- package/src/components/chart/composite/panel-bitmap.ts +28 -2
- package/src/components/chart/composite/panel-labels.ts +20 -1
- package/src/components/chart/composite/panel-surface.tsx +201 -92
- package/src/components/chart/composite/price-axis-labels.tsx +92 -46
- package/src/components/chart/composite/price-series.ts +31 -8
- package/src/components/chart/composite/scene.ts +154 -59
- package/src/components/chart/composite/series-axis-tags.tsx +4 -2
- package/src/components/chart/composite/time-axis.ts +30 -7
- package/src/components/chart/composite/time-scale.ts +211 -56
- package/src/components/chart/composite/types.ts +11 -1
- package/src/components/chart/composite/use-composite-chart-keyboard.ts +32 -12
- package/src/components/chart/curve/curve-surface.tsx +5 -2
- package/src/components/chart/curve/model.ts +4 -1
- package/src/components/chart/native/use-bitmap-metrics.ts +81 -0
- package/src/components/chart/static/series.ts +3 -0
- package/src/components/command-bar/commands/collection-list.ts +81 -0
- package/src/components/command-bar/commands/direct/index.ts +6 -0
- package/src/components/command-bar/commands/registry.ts +8 -0
- package/src/components/command-bar/layout-items/current-layout.ts +22 -1
- package/src/components/command-bar/layout-items/focused-pane.ts +22 -1
- package/src/components/command-bar/pane-templates/items.ts +19 -3
- package/src/components/command-bar/pane-templates/workflow.ts +5 -0
- package/src/components/command-bar/routes/root/results.ts +23 -3
- package/src/components/command-bar/routes/root/selection.ts +4 -2
- package/src/components/command-bar/routes/root/shortcut-feedback.ts +1 -1
- package/src/components/command-bar/routes/root/shortcut-items.ts +3 -4
- package/src/components/command-bar/routes/root/shortcuts.ts +58 -14
- package/src/components/command-bar/routes/root/ticker-first.ts +32 -0
- package/src/components/command-bar/surface/environment.ts +2 -0
- package/src/components/command-bar/view-model.ts +7 -0
- package/src/components/command-bar/workflow/ops.ts +2 -1
- package/src/components/command-bar/workflow/tickers.ts +20 -0
- package/src/components/layout/shell/active-drag.ts +38 -7
- package/src/components/layout/shell/drag/magnets.ts +106 -0
- package/src/components/layout/shell/menu.ts +15 -1
- package/src/components/layout/shell/pane/actions.ts +4 -0
- package/src/components/layout/shell/ticker-link-menu.ts +12 -3
- package/src/components/layout/status-bar.tsx +20 -0
- package/src/components/layout/window-controls.tsx +96 -35
- package/src/components/layout/window-fullscreen.ts +20 -0
- package/src/components/onboarding/onboarding-keyboard.ts +5 -1
- package/src/components/onboarding/onboarding-wizard.tsx +15 -4
- package/src/components/onboarding/portfolio-step/positions-panel.tsx +8 -5
- package/src/components/onboarding/use-onboarding-progress.ts +20 -1
- package/src/components/price-sparkline/view.tsx +14 -6
- package/src/components/quote-flash.ts +36 -32
- package/src/components/ticker/badge/index.tsx +8 -4
- package/src/components/ticker/badge/list.tsx +23 -7
- package/src/components/ui/column-grid.tsx +8 -2
- package/src/components/ui/data-table/opentui/model.ts +4 -1
- package/src/components/ui/tabs.tsx +5 -2
- package/src/core/state/app/layout.ts +18 -3
- package/src/core/state/app/state.ts +2 -0
- package/src/core/state/app/types.ts +5 -0
- package/src/data/config/layout.ts +3 -0
- package/src/data/config/store/normalize.ts +39 -4
- package/src/i18n/es.ts +10 -0
- package/src/i18n/ja.ts +10 -0
- package/src/i18n/ko.ts +10 -0
- package/src/i18n/zh-cn.ts +10 -0
- package/src/i18n/zh-tw.ts +10 -0
- package/src/layout/pane-manager/floating-placement.ts +7 -4
- package/src/layout/pane-manager/floating.ts +5 -2
- package/src/market-data/bond-funds.ts +19 -0
- package/src/market-data/coordinator/events.ts +17 -1
- package/src/market-data/frame-scheduler.ts +47 -10
- package/src/market-data/frame-wake.ts +74 -0
- package/src/market-data/futures-board.ts +7 -6
- package/src/market-data/futures-contract.ts +14 -3
- package/src/market-data/hooks.tsx +12 -3
- package/src/market-data/market/freshness.ts +18 -0
- package/src/market-data/market/status.ts +15 -2
- package/src/market-data/performance.ts +78 -9
- package/src/market-data/request-types.ts +6 -3
- package/src/plugins/builtin/alerts/cli.ts +2 -1
- package/src/plugins/builtin/analytics/risk-evidence.ts +1 -0
- package/src/plugins/builtin/attention/evidence.ts +1 -1
- package/src/plugins/builtin/awards/client.ts +1 -1
- package/src/plugins/builtin/awards/evidence.ts +1 -1
- package/src/plugins/builtin/awards/pane.tsx +2 -1
- package/src/plugins/builtin/bond-calculator/headless.ts +2 -1
- package/src/plugins/builtin/bond-calculator/index.tsx +1 -1
- package/src/plugins/builtin/bond-calculator/math.ts +1 -1
- package/src/plugins/builtin/bond-calculator/model.ts +14 -5
- package/src/plugins/builtin/bond-calculator/pane.tsx +1 -1
- package/src/plugins/builtin/broker-manager/footer.ts +3 -2
- package/src/plugins/builtin/broker-manager/index.tsx +2 -1
- package/src/plugins/builtin/broker-manager/keyboard.ts +2 -1
- package/src/plugins/builtin/buildout/cells.tsx +25 -45
- package/src/plugins/builtin/buildout/detail/company.tsx +3 -3
- package/src/plugins/builtin/buildout/detail/index.tsx +1 -1
- package/src/plugins/builtin/buildout/detail/intel.tsx +4 -7
- package/src/plugins/builtin/buildout/detail/site.tsx +4 -4
- package/src/plugins/builtin/buildout/detail/ui.tsx +39 -38
- package/src/plugins/builtin/buildout/format.ts +23 -1
- package/src/plugins/builtin/buildout/model/types.ts +1 -0
- package/src/plugins/builtin/buildout/pane/index.tsx +2 -8
- package/src/plugins/builtin/buildout/table-model.ts +12 -4
- package/src/plugins/builtin/catalysts/evidence.ts +1 -1
- package/src/plugins/builtin/catalysts/pane.tsx +2 -1
- package/src/plugins/builtin/cds/headless.ts +1 -0
- package/src/plugins/builtin/cds/index.tsx +1 -1
- package/src/plugins/builtin/chart-composer/headless.ts +40 -1
- package/src/plugins/builtin/cloud/plugin.tsx +2 -0
- package/src/plugins/builtin/cloud/upgrade-status-widget.tsx +28 -3
- package/src/plugins/builtin/cloud-tweets/table.tsx +3 -2
- package/src/plugins/builtin/comment-letters/client.ts +2 -1
- package/src/plugins/builtin/company-kpis/client.ts +1 -1
- package/src/plugins/builtin/company-kpis/pane.tsx +2 -1
- package/src/plugins/builtin/company-kpis/screenshot-evidence.ts +3 -2
- package/src/plugins/builtin/correlation/headless.ts +3 -2
- package/src/plugins/builtin/cot/headless.ts +9 -3
- package/src/plugins/builtin/cot/model.ts +44 -3
- package/src/plugins/builtin/credit-documents/client.ts +1 -1
- package/src/plugins/builtin/credit-documents/evidence.ts +1 -1
- package/src/plugins/builtin/credit-documents/pane.tsx +2 -1
- package/src/plugins/builtin/crypto-board/pane.tsx +5 -4
- package/src/plugins/builtin/debt-maturities/headless.ts +11 -2
- package/src/plugins/builtin/debt-maturities/model.ts +66 -0
- package/src/plugins/builtin/debt-maturities/pane.tsx +23 -4
- package/src/plugins/builtin/earnings-calls/pane.tsx +1 -1
- package/src/plugins/builtin/earnings-ripple/index.tsx +6 -1
- package/src/plugins/builtin/earnings-ripple/pane.tsx +1 -1
- package/src/plugins/builtin/econ/calendar-model.ts +30 -6
- package/src/plugins/builtin/econ/fred-catalog.ts +5 -5
- package/src/plugins/builtin/econ/print-watch.ts +3 -1
- package/src/plugins/builtin/equity-screener/client.ts +1 -1
- package/src/plugins/builtin/equity-screener/criterion-editor.tsx +4 -16
- package/src/plugins/builtin/equity-screener/headless.ts +4 -2
- package/src/plugins/builtin/equity-screener/ideas.ts +67 -3
- package/src/plugins/builtin/equity-screener/model.ts +180 -12
- package/src/plugins/builtin/equity-screener/pane.tsx +52 -12
- package/src/plugins/builtin/equity-screener/screen-check.ts +7 -4
- package/src/plugins/builtin/equity-screener/watchlist.ts +33 -0
- package/src/plugins/builtin/futures/series.ts +16 -1
- package/src/plugins/builtin/futures-curve/client.ts +19 -0
- package/src/plugins/builtin/futures-curve/headless.ts +52 -13
- package/src/plugins/builtin/futures-curve/index.tsx +1 -1
- package/src/plugins/builtin/futures-curve/model.ts +77 -6
- package/src/plugins/builtin/futures-curve/pane.tsx +19 -3
- package/src/plugins/builtin/fx-matrix/headless.ts +4 -2
- package/src/plugins/builtin/fx-matrix/index.tsx +37 -3
- package/src/plugins/builtin/fx-matrix/pairs.ts +15 -0
- package/src/plugins/builtin/gpu/headless.ts +2 -1
- package/src/plugins/builtin/help/function-card.tsx +2 -1
- package/src/plugins/builtin/help/index.tsx +2 -1
- package/src/plugins/builtin/insider/display.ts +45 -6
- package/src/plugins/builtin/ipo-calendar/model.ts +2 -0
- package/src/plugins/builtin/iv-history/client.ts +2 -1
- package/src/plugins/builtin/iv-history/headless.ts +2 -1
- package/src/plugins/builtin/jobs/pages.ts +4 -4
- package/src/plugins/builtin/kelly-sizer/headless.ts +2 -1
- package/src/plugins/builtin/macro-day/client.ts +41 -0
- package/src/plugins/builtin/macro-day/headless.ts +19 -9
- package/src/plugins/builtin/macro-day/model.ts +3 -1
- package/src/plugins/builtin/market-heatmap/index.tsx +2 -1
- package/src/plugins/builtin/market-movers/index.tsx +4 -3
- package/src/plugins/builtin/members/client.ts +1 -1
- package/src/plugins/builtin/mna/distress/client.ts +1 -0
- package/src/plugins/builtin/mna/distress/headless.ts +1 -0
- package/src/plugins/builtin/mna/pane.tsx +4 -1
- package/src/plugins/builtin/options/market-reference.ts +1 -1
- package/src/plugins/builtin/options/open-interest-as-of.ts +3 -1
- package/src/plugins/builtin/options/view.tsx +2 -1
- package/src/plugins/builtin/options-calculator/evidence.ts +1 -1
- package/src/plugins/builtin/options-scenario/csp-screen-pane.tsx +1 -1
- package/src/plugins/builtin/options-scenario/csp-screen.ts +8 -4
- package/src/plugins/builtin/options-scenario/csp.ts +28 -9
- package/src/plugins/builtin/options-scenario/evidence.ts +5 -4
- package/src/plugins/builtin/options-scenario/pane.tsx +2 -1
- package/src/plugins/builtin/perps/client.ts +1 -1
- package/src/plugins/builtin/perps/pane.tsx +3 -1
- package/src/plugins/builtin/portfolio-list/cli/bulk-symbols.ts +17 -0
- package/src/plugins/builtin/portfolio-list/cli/portfolio-command.ts +2 -1
- package/src/plugins/builtin/portfolio-list/cli/watchlist-command.ts +4 -2
- package/src/plugins/builtin/portfolio-list/pane/index.tsx +7 -3
- package/src/plugins/builtin/portfolio-list/use-throttled-memo.ts +10 -19
- package/src/plugins/builtin/rate-path/headless.ts +24 -10
- package/src/plugins/builtin/research/analyst-model.ts +1 -0
- package/src/plugins/builtin/research/equity-diagnostic-pane.tsx +2 -1
- package/src/plugins/builtin/research/event-model.ts +10 -3
- package/src/plugins/builtin/research/events-headless.ts +20 -2
- package/src/plugins/builtin/research/index.tsx +1 -0
- package/src/plugins/builtin/research/relative-valuation-currency.ts +1 -1
- package/src/plugins/builtin/research/relative-valuation-peers.ts +2 -1
- package/src/plugins/builtin/research-search/command-bar-search.ts +3 -1
- package/src/plugins/builtin/research-search/snippet.ts +1 -0
- package/src/plugins/builtin/revenue-breakdown/client.ts +3 -2
- package/src/plugins/builtin/seasonality/headless.ts +2 -1
- package/src/plugins/builtin/sec/command-bar-search.ts +3 -2
- package/src/plugins/builtin/sec/fund-facts.ts +17 -1
- package/src/plugins/builtin/sec/index.tsx +8 -5
- package/src/plugins/builtin/sec/row-summaries.ts +28 -12
- package/src/plugins/builtin/shared/members-menu.tsx +103 -38
- package/src/plugins/builtin/short-interest/publications-model.ts +22 -1
- package/src/plugins/builtin/short-interest/publications-pane.tsx +13 -4
- package/src/plugins/builtin/short-interest/watch-client.ts +2 -1
- package/src/plugins/builtin/short-interest/watch-model.ts +1 -1
- package/src/plugins/builtin/social-mentions/client.ts +2 -1
- package/src/plugins/builtin/supply-chain/client.ts +1 -1
- package/src/plugins/builtin/supply-chain/evidence.ts +1 -1
- package/src/plugins/builtin/supply-chain/graph-pane.tsx +2 -1
- package/src/plugins/builtin/supply-chain/pane.tsx +2 -1
- package/src/plugins/builtin/ticker-detail/adr-pair/headless.ts +1 -1
- package/src/plugins/builtin/ticker-detail/adr-pair/model.ts +24 -4
- package/src/plugins/builtin/ticker-detail/adr-pair/tab.tsx +1 -1
- package/src/plugins/builtin/time-sales/client.ts +1 -0
- package/src/plugins/builtin/time-sales/pane.tsx +3 -2
- package/src/plugins/builtin/trace-bonds/client.ts +7 -4
- package/src/plugins/builtin/trace-bonds/index.tsx +1 -1
- package/src/plugins/builtin/vol-surface/client.ts +2 -1
- package/src/plugins/builtin/volatility/client.ts +6 -1
- package/src/plugins/builtin/volatility/headless.ts +0 -1
- package/src/plugins/builtin/yield-curve/curves.ts +15 -0
- package/src/plugins/builtin/yield-curve/headless.ts +9 -2
- package/src/plugins/builtin/yield-curve/index.tsx +5 -8
- package/src/plugins/host-modules.ts +22 -3
- package/src/plugins/host-smoke.ts +6 -3
- package/src/portfolio/collection-name.ts +19 -0
- package/src/portfolio/column-values.ts +76 -10
- package/src/renderers/browser/http-proxy-transport.ts +78 -8
- package/src/renderers/cloudflare/http-proxy.ts +13 -2
- package/src/renderers/cloudflare/proxy-session.ts +47 -24
- package/src/renderers/cloudflare/worker.ts +15 -2
- package/src/renderers/desktop/geometry.ts +279 -0
- package/src/renderers/desktop/screen-setup.ts +246 -0
- package/src/renderers/desktop/sequential-window-host.ts +179 -0
- package/src/renderers/desktop/test-utils.ts +71 -0
- package/src/renderers/desktop/window-controller.ts +433 -0
- package/src/renderers/desktop/window-host.ts +70 -0
- package/src/renderers/desktop/window-layouts.ts +630 -0
- package/src/renderers/dom/data-frames.ts +15 -4
- package/src/renderers/dom/host/box.tsx +204 -176
- package/src/renderers/dom/host/style.ts +49 -20
- package/src/renderers/dom/host/text.tsx +3 -2
- package/src/renderers/electrobun/bun/application-menu/click.ts +1 -0
- package/src/renderers/electrobun/bun/application-menu/index.ts +4 -1
- package/src/renderers/electrobun/bun/desktop/backend-requests.ts +19 -8
- package/src/renderers/electrobun/bun/desktop/backend.ts +322 -19
- package/src/renderers/electrobun/bun/desktop/detached-windows.ts +83 -61
- package/src/renderers/electrobun/bun/desktop/host-requests.ts +29 -15
- package/src/renderers/electrobun/bun/desktop/initialization.ts +30 -11
- package/src/renderers/electrobun/bun/desktop/relaunch.ts +9 -1
- package/src/renderers/electrobun/bun/desktop/screen-setup-file.ts +55 -0
- package/src/renderers/electrobun/bun/desktop/state-broadcaster.ts +44 -8
- package/src/renderers/electrobun/bun/desktop/windows-script.ts +9 -6
- package/src/renderers/electrobun/bun/desktop/workspace/index.ts +298 -97
- package/src/renderers/electrobun/bun/desktop/workspace/requests.ts +44 -6
- package/src/renderers/electrobun/bun/index.ts +4 -97
- package/src/renderers/electrobun/bun/window/electrobun-window-host.ts +306 -0
- package/src/renderers/electrobun/bun/window/focus.ts +0 -25
- package/src/renderers/electrobun/bun/window/remembered-frame.ts +4 -82
- package/src/renderers/electrobun/bun/window/windows-native.ts +26 -1
- package/src/renderers/electrobun/shared/protocol.ts +50 -2
- package/src/renderers/electrobun/view/backend-rpc.ts +15 -0
- package/src/renderers/electrobun/view/desktop/window/bridge.ts +63 -3
- package/src/renderers/electrobun/view/main.tsx +13 -7
- package/src/renderers/electrobun/view/ui-host.tsx +7 -0
- package/src/renderers/electrobun/view/window-fullscreen.ts +10 -3
- package/src/series/fred-argument.ts +17 -0
- package/src/sources/provider-router/financial-routes.ts +7 -1
- package/src/sources/provider-router/index.ts +7 -1
- package/src/state/app/context/index.tsx +16 -2
- package/src/state/desktop-windows.ts +63 -0
- package/src/sync/other-window-tabs.ts +62 -0
- package/src/tickers/company-name-listing.ts +23 -3
- package/src/tickers/listing-fate.ts +147 -1
- package/src/tickers/terminal-codes.ts +5 -0
- package/src/time-series/price-units.ts +11 -0
- package/src/time-series/types.ts +6 -0
- package/src/types/config.ts +10 -1
- package/src/types/desktop-window.ts +71 -2
- package/src/types/financials.ts +4 -0
- package/src/types/headless.ts +19 -0
- package/src/types/plugin.ts +6 -0
- package/src/ui/host.tsx +2 -0
- package/src/ui/index.tsx +14 -13
- package/src/ui/same-props.ts +58 -0
- package/src/utils/cli-output.ts +33 -4
- package/src/utils/currency-names.ts +20 -0
- package/src/utils/currency-pair.ts +6 -0
- package/src/utils/exchange-rate-snapshot.ts +20 -2
- package/src/utils/format.ts +7 -2
- package/src/utils/futures-generic.ts +12 -0
- package/src/utils/fx-market-hours.ts +43 -0
- package/src/utils/http-proxy-upstream.ts +6 -2
- package/src/utils/plugin-proxy-hosts.json +0 -4
- package/src/utils/plugin-proxy-hosts.ts +22 -0
- package/src/utils/sec-filings.ts +7 -1
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,433 @@
|
|
|
1
|
+
import {
|
|
2
|
+
arrangementKey,
|
|
3
|
+
cascadeFrame,
|
|
4
|
+
clampFrameToScreens,
|
|
5
|
+
displayForFrame,
|
|
6
|
+
displayFromKey,
|
|
7
|
+
displayKey,
|
|
8
|
+
isUsableRect,
|
|
9
|
+
moveFrameToDisplay,
|
|
10
|
+
relocateFrame,
|
|
11
|
+
sameRect,
|
|
12
|
+
usableDisplays,
|
|
13
|
+
type DesktopDisplay,
|
|
14
|
+
type Rect,
|
|
15
|
+
} from "./geometry";
|
|
16
|
+
import {
|
|
17
|
+
MAIN_WINDOW_ID,
|
|
18
|
+
forgetWindow,
|
|
19
|
+
hasArrangement,
|
|
20
|
+
latestPlacementFor,
|
|
21
|
+
placementFor,
|
|
22
|
+
rememberPlacement,
|
|
23
|
+
restoreWindow,
|
|
24
|
+
withExtraWindows,
|
|
25
|
+
type ExtraWindowRecord,
|
|
26
|
+
type ScreenSetup,
|
|
27
|
+
type WindowPlacement,
|
|
28
|
+
} from "./screen-setup";
|
|
29
|
+
import type { DesktopWindowHost, NativeWindowEvent, WindowSize } from "./window-host";
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* A quit this soon after extra windows closed keeps them for the next launch:
|
|
33
|
+
* closing every window at once (the Windows taskbar, logging out) closes
|
|
34
|
+
* them one by one, maybe the main window, and so the quit, last.
|
|
35
|
+
*/
|
|
36
|
+
const QUIT_GRACE_MS = 1_000;
|
|
37
|
+
|
|
38
|
+
export interface ScreenSetupStore {
|
|
39
|
+
load(): ScreenSetup;
|
|
40
|
+
save(setup: ScreenSetup): void;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface DesktopWindowControllerOptions {
|
|
44
|
+
host: DesktopWindowHost;
|
|
45
|
+
store: ScreenSetupStore;
|
|
46
|
+
minSize: WindowSize;
|
|
47
|
+
/** Where the main window opens when nothing was saved for these screens. */
|
|
48
|
+
defaultMainFrame: Rect;
|
|
49
|
+
/** The main window as an older build saved it, used once when the setup has nothing for it yet. */
|
|
50
|
+
legacyMainPlacement?: WindowPlacement | null;
|
|
51
|
+
title?: (windowId: string) => string;
|
|
52
|
+
/**
|
|
53
|
+
* The person closed an extra window; its tabs go back to the main window.
|
|
54
|
+
* A quit that follows within `QUIT_GRACE_MS` keeps it for the next launch.
|
|
55
|
+
*/
|
|
56
|
+
onWindowClosed?: (windowId: string) => void;
|
|
57
|
+
onMainWindowClosed?: () => void;
|
|
58
|
+
onWindowFocused?: (windowId: string) => void;
|
|
59
|
+
onWindowVisibility?: (windowId: string, visible: boolean) => void;
|
|
60
|
+
/** Another shell's pane windows share the host; their events are theirs. */
|
|
61
|
+
onOtherWindowEvent?: (event: NativeWindowEvent) => void;
|
|
62
|
+
now?: () => number;
|
|
63
|
+
saveDelayMs?: number;
|
|
64
|
+
setTimer?: (callback: () => void, ms: number) => unknown;
|
|
65
|
+
clearTimer?: (handle: unknown) => void;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* The full app windows on screen: where each opens, keeping where each sits
|
|
70
|
+
* for the monitor arrangement in use, and putting them back when the screens
|
|
71
|
+
* change. Unplugging a display folds its windows onto the displays that
|
|
72
|
+
* remain, keeping their place on the screen in proportion; plugging it back
|
|
73
|
+
* restores the setup saved for that arrangement. Whatever the setup says,
|
|
74
|
+
* every window this side places lands on a screen that is connected: the
|
|
75
|
+
* shell does not pull back a window asked to open off screen.
|
|
76
|
+
*/
|
|
77
|
+
export class DesktopWindowController {
|
|
78
|
+
private setup: ScreenSetup;
|
|
79
|
+
private displays: DesktopDisplay[];
|
|
80
|
+
private arrangement: string;
|
|
81
|
+
private readonly open = new Set<string>();
|
|
82
|
+
/** Each window's last frame while neither maximized nor full screen, which is what a restore returns to. */
|
|
83
|
+
private readonly windowedFrames = new Map<string, Rect>();
|
|
84
|
+
private readonly saveTimers = new Map<string, unknown>();
|
|
85
|
+
private focused: string = MAIN_WINDOW_ID;
|
|
86
|
+
/** Extra windows the person closed, with the setup from just before, for a quit right after. */
|
|
87
|
+
private recentlyClosed: Array<{ windowId: string; at: number; before: ScreenSetup }> = [];
|
|
88
|
+
private readonly disposers: Array<() => void> = [];
|
|
89
|
+
private readonly now: () => number;
|
|
90
|
+
private readonly saveDelayMs: number;
|
|
91
|
+
private readonly setTimer: (callback: () => void, ms: number) => unknown;
|
|
92
|
+
private readonly clearTimer: (handle: unknown) => void;
|
|
93
|
+
/** Windows this side is moving, whose frame events are its own and not the person's. */
|
|
94
|
+
private applying = 0;
|
|
95
|
+
|
|
96
|
+
constructor(private readonly options: DesktopWindowControllerOptions) {
|
|
97
|
+
this.setup = options.store.load();
|
|
98
|
+
this.displays = usableDisplays(options.host.listDisplays());
|
|
99
|
+
this.arrangement = arrangementKey(this.displays);
|
|
100
|
+
this.now = options.now ?? Date.now;
|
|
101
|
+
this.saveDelayMs = options.saveDelayMs ?? 300;
|
|
102
|
+
this.setTimer = options.setTimer ?? ((callback, ms) => setTimeout(callback, ms));
|
|
103
|
+
this.clearTimer = options.clearTimer ?? ((handle) => clearTimeout(handle as ReturnType<typeof setTimeout>));
|
|
104
|
+
this.disposers.push(
|
|
105
|
+
options.host.onDisplaysChanged((displays) => this.handleDisplays(displays)),
|
|
106
|
+
options.host.onWindowEvent((event) => this.handleWindowEvent(event)),
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
get screenSetup(): ScreenSetup {
|
|
111
|
+
return this.setup;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
get currentArrangement(): string {
|
|
115
|
+
return this.arrangement;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
get focusedWindowId(): string {
|
|
119
|
+
return this.focused;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
isOpen(windowId: string): boolean {
|
|
123
|
+
return this.open.has(windowId);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
openWindowIds(): string[] {
|
|
127
|
+
return [...this.open];
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** The extra windows the workspace has now, kept for the next launch. */
|
|
131
|
+
rememberWindows(records: readonly ExtraWindowRecord[]): void {
|
|
132
|
+
this.commit(withExtraWindows(this.setup, records));
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Where a window opens, always on a connected screen: what was saved for
|
|
137
|
+
* these screens; else where it last sat on other screens, moved onto
|
|
138
|
+
* these (a monitor unplugged, a new resolution); else a place of its own.
|
|
139
|
+
*/
|
|
140
|
+
placementFor(windowId: string): WindowPlacement {
|
|
141
|
+
const saved = placementFor(this.setup, this.arrangement, windowId);
|
|
142
|
+
if (saved) return { ...saved, frame: this.onScreen(saved.frame) };
|
|
143
|
+
const earlier = latestPlacementFor(this.setup, windowId, this.arrangement);
|
|
144
|
+
if (earlier) return { ...earlier, frame: this.carryOver(windowId, earlier) };
|
|
145
|
+
const legacy = windowId === MAIN_WINDOW_ID ? this.options.legacyMainPlacement : null;
|
|
146
|
+
if (legacy) return { ...legacy, frame: this.onScreen(legacy.frame) };
|
|
147
|
+
if (windowId === MAIN_WINDOW_ID) {
|
|
148
|
+
return { frame: this.onScreen(this.options.defaultMainFrame), maximized: false, fullscreen: false };
|
|
149
|
+
}
|
|
150
|
+
return { frame: this.onScreen(this.freshFrame()), maximized: false, fullscreen: false };
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
openWindow(windowId: string): void {
|
|
154
|
+
if (this.open.has(windowId)) {
|
|
155
|
+
this.focus(windowId);
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
const placement = this.placementFor(windowId);
|
|
159
|
+
this.open.add(windowId);
|
|
160
|
+
this.windowedFrames.set(windowId, placement.frame);
|
|
161
|
+
this.options.host.openWindow({
|
|
162
|
+
id: windowId,
|
|
163
|
+
kind: "app",
|
|
164
|
+
title: this.options.title?.(windowId) ?? "Gloom",
|
|
165
|
+
frame: placement.frame,
|
|
166
|
+
minSize: this.options.minSize,
|
|
167
|
+
});
|
|
168
|
+
this.applyModes(windowId, placement);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Closes a window from this side: its placements stay unless `forget`, for
|
|
173
|
+
* a window closed for good. Where it was last moved is saved first.
|
|
174
|
+
*/
|
|
175
|
+
closeWindow(windowId: string, options: { forget?: boolean } = {}): void {
|
|
176
|
+
const moved = this.saveTimers.has(windowId);
|
|
177
|
+
this.cancelSave(windowId);
|
|
178
|
+
if (moved && !options.forget) this.remember(windowId);
|
|
179
|
+
if (this.open.delete(windowId)) this.options.host.closeWindow(windowId);
|
|
180
|
+
this.windowedFrames.delete(windowId);
|
|
181
|
+
if (options.forget) this.commit(forgetWindow(this.setup, windowId));
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
focus(windowId: string): void {
|
|
185
|
+
if (!this.open.has(windowId)) return;
|
|
186
|
+
this.focused = windowId;
|
|
187
|
+
this.options.host.focusWindow(windowId);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Writes every open window's placement now, for a quit: a window that was
|
|
192
|
+
* never moved has saved nothing yet, and opens where it is next time too.
|
|
193
|
+
* Extra windows closed just before come back with it (`QUIT_GRACE_MS`).
|
|
194
|
+
*/
|
|
195
|
+
flush(): void {
|
|
196
|
+
for (const windowId of [...this.saveTimers.keys()]) this.cancelSave(windowId);
|
|
197
|
+
for (const windowId of this.open) this.remember(windowId);
|
|
198
|
+
const since = this.now() - QUIT_GRACE_MS;
|
|
199
|
+
let setup = this.setup;
|
|
200
|
+
for (const closed of this.recentlyClosed.splice(0)) {
|
|
201
|
+
if (closed.at >= since) setup = restoreWindow(setup, closed.before, closed.windowId);
|
|
202
|
+
}
|
|
203
|
+
this.commit(setup);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
dispose(): void {
|
|
207
|
+
for (const windowId of [...this.saveTimers.keys()]) this.cancelSave(windowId);
|
|
208
|
+
for (const dispose of this.disposers.splice(0)) dispose();
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** A display with no window on it, for a new window; else the focused window's display. */
|
|
212
|
+
private freshFrame(): Rect {
|
|
213
|
+
const mainFrame = this.liveFrame(MAIN_WINDOW_ID) ?? this.options.defaultMainFrame;
|
|
214
|
+
const occupied = new Set(
|
|
215
|
+
[...this.open]
|
|
216
|
+
.map((id) => this.liveFrame(id))
|
|
217
|
+
.flatMap((frame) => {
|
|
218
|
+
const display = frame ? displayForFrame(frame, this.displays) : null;
|
|
219
|
+
return display ? [displayKey(display)] : [];
|
|
220
|
+
}),
|
|
221
|
+
);
|
|
222
|
+
const empty = this.displays.find((display) => !occupied.has(displayKey(display)));
|
|
223
|
+
if (empty) return moveFrameToDisplay(mainFrame, displayForFrame(mainFrame, this.displays), empty);
|
|
224
|
+
const anchor = this.liveFrame(this.focused) ?? mainFrame;
|
|
225
|
+
const display = displayForFrame(anchor, this.displays) ?? this.displays[0];
|
|
226
|
+
return display ? cascadeFrame(anchor, display) : anchor;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* A frame on a connected screen. Where the system places windows itself
|
|
231
|
+
* (Wayland), only the size is this side's, and it still fits a screen.
|
|
232
|
+
*/
|
|
233
|
+
/**
|
|
234
|
+
* A frame a window can open at: at least the minimum size, as the window
|
|
235
|
+
* cannot be made smaller, and on a connected screen. On a screen smaller
|
|
236
|
+
* than the minimum it keeps its top left corner there.
|
|
237
|
+
*/
|
|
238
|
+
private onScreen(frame: Rect): Rect {
|
|
239
|
+
const sized = this.atLeastMinimum(frame);
|
|
240
|
+
if (this.displays.length === 0) return sized;
|
|
241
|
+
const fitted = this.atLeastMinimum(clampFrameToScreens(sized, this.displays));
|
|
242
|
+
return this.options.host.canPlaceWindows ? fitted : { ...sized, width: fitted.width, height: fitted.height };
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
private atLeastMinimum(frame: Rect): Rect {
|
|
246
|
+
const { width, height } = this.options.minSize;
|
|
247
|
+
return { ...frame, width: Math.max(frame.width, width), height: Math.max(frame.height, height) };
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** The display the windows fold onto when theirs is gone: the main window's, else the primary one. */
|
|
251
|
+
private foldTarget(windowId: string, displays: readonly DesktopDisplay[]): DesktopDisplay | null {
|
|
252
|
+
const mainFrame = windowId === MAIN_WINDOW_ID ? null : this.liveFrame(MAIN_WINDOW_ID);
|
|
253
|
+
return (mainFrame ? displayForFrame(mainFrame, displays) : null) ?? displays[0] ?? null;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/** A placement saved under other screens, moved onto these. */
|
|
257
|
+
private carryOver(windowId: string, placement: WindowPlacement): Rect {
|
|
258
|
+
const from = placement.display ? displayFromKey(placement.display) : null;
|
|
259
|
+
const target = this.foldTarget(windowId, this.displays);
|
|
260
|
+
const frame = target ? relocateFrame(placement.frame, from, this.displays, target) : placement.frame;
|
|
261
|
+
return this.onScreen(frame);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
private liveFrame(windowId: string): Rect | null {
|
|
265
|
+
const state = this.open.has(windowId) ? this.options.host.getWindowState(windowId) : null;
|
|
266
|
+
if (!state) return this.windowedFrames.get(windowId) ?? null;
|
|
267
|
+
if (state.minimized || state.maximized || state.fullscreen) return this.windowedFrames.get(windowId) ?? state.frame;
|
|
268
|
+
return isUsableRect(state.frame) ? state.frame : this.windowedFrames.get(windowId) ?? null;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
private applyModes(windowId: string, placement: WindowPlacement): void {
|
|
272
|
+
const { host } = this.options;
|
|
273
|
+
if (placement.fullscreen && host.canFullscreen) host.setFullscreen(windowId, true);
|
|
274
|
+
else if (placement.maximized) host.setMaximized(windowId, true);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
private commit(next: ScreenSetup): void {
|
|
278
|
+
if (next === this.setup) return;
|
|
279
|
+
this.setup = next;
|
|
280
|
+
this.options.store.save(next);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
private cancelSave(windowId: string): void {
|
|
284
|
+
const timer = this.saveTimers.get(windowId);
|
|
285
|
+
if (timer === undefined) return;
|
|
286
|
+
this.clearTimer(timer);
|
|
287
|
+
this.saveTimers.delete(windowId);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
private scheduleSave(windowId: string): void {
|
|
291
|
+
this.cancelSave(windowId);
|
|
292
|
+
this.saveTimers.set(windowId, this.setTimer(() => {
|
|
293
|
+
this.saveTimers.delete(windowId);
|
|
294
|
+
// The system moves windows off a display it lost before anything says
|
|
295
|
+
// the display is gone; those moves belong to the new arrangement and
|
|
296
|
+
// must not overwrite where the windows sat on the old one.
|
|
297
|
+
const displays = this.options.host.listDisplays();
|
|
298
|
+
if (arrangementKey(displays) !== this.arrangement) {
|
|
299
|
+
this.handleDisplays(displays);
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
this.remember(windowId);
|
|
303
|
+
}, this.saveDelayMs));
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/** Records where a window is now under the arrangement in use. Nothing while it is minimized. */
|
|
307
|
+
private remember(windowId: string): void {
|
|
308
|
+
const state = this.open.has(windowId) ? this.options.host.getWindowState(windowId) : null;
|
|
309
|
+
if (!state || state.minimized || !isUsableRect(state.frame)) return;
|
|
310
|
+
if (!state.maximized && !state.fullscreen) this.windowedFrames.set(windowId, state.frame);
|
|
311
|
+
const frame = this.windowedFrames.get(windowId) ?? state.frame;
|
|
312
|
+
const display = displayForFrame(state.frame, this.displays);
|
|
313
|
+
this.commit(rememberPlacement(this.setup, this.arrangement, windowId, {
|
|
314
|
+
frame,
|
|
315
|
+
maximized: state.maximized && !state.fullscreen,
|
|
316
|
+
fullscreen: state.fullscreen,
|
|
317
|
+
...(display ? { display: displayKey(display) } : {}),
|
|
318
|
+
}, this.now()));
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
private handleWindowEvent(event: NativeWindowEvent): void {
|
|
322
|
+
if (!this.open.has(event.id)) {
|
|
323
|
+
this.options.onOtherWindowEvent?.(event);
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
switch (event.type) {
|
|
327
|
+
case "frame":
|
|
328
|
+
if (this.applying === 0) this.scheduleSave(event.id);
|
|
329
|
+
return;
|
|
330
|
+
case "focus":
|
|
331
|
+
this.focused = event.id;
|
|
332
|
+
this.options.onWindowFocused?.(event.id);
|
|
333
|
+
return;
|
|
334
|
+
case "visibility":
|
|
335
|
+
this.options.onWindowVisibility?.(event.id, event.visible);
|
|
336
|
+
return;
|
|
337
|
+
case "close":
|
|
338
|
+
// Read now: the host still answers for the window while its close is reported.
|
|
339
|
+
this.cancelSave(event.id);
|
|
340
|
+
this.remember(event.id);
|
|
341
|
+
this.open.delete(event.id);
|
|
342
|
+
this.windowedFrames.delete(event.id);
|
|
343
|
+
if (event.id === MAIN_WINDOW_ID) {
|
|
344
|
+
this.options.onMainWindowClosed?.();
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
this.recentlyClosed = [
|
|
348
|
+
...this.recentlyClosed.filter((closed) => closed.at >= this.now() - QUIT_GRACE_MS),
|
|
349
|
+
{ windowId: event.id, at: this.now(), before: this.setup },
|
|
350
|
+
];
|
|
351
|
+
this.commit(forgetWindow(this.setup, event.id));
|
|
352
|
+
this.options.onWindowClosed?.(event.id);
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* The screens changed. A setup saved for the new arrangement is put back
|
|
359
|
+
* as it was; otherwise each window whose display is gone moves onto one
|
|
360
|
+
* that remains, at the same place in proportion and in the same mode: the
|
|
361
|
+
* same screen at its new resolution, else the main window's display.
|
|
362
|
+
* Windows still on a display stay. All of them end up on screen.
|
|
363
|
+
*/
|
|
364
|
+
handleDisplays(nextDisplays: readonly DesktopDisplay[]): void {
|
|
365
|
+
const displays = usableDisplays(nextDisplays);
|
|
366
|
+
const key = arrangementKey(displays);
|
|
367
|
+
if (key === this.arrangement || displays.length === 0) return;
|
|
368
|
+
const previousDisplays = this.displays;
|
|
369
|
+
this.displays = displays;
|
|
370
|
+
this.arrangement = key;
|
|
371
|
+
const restoring = hasArrangement(this.setup, key);
|
|
372
|
+
const { host } = this.options;
|
|
373
|
+
const restored = new Set<string>();
|
|
374
|
+
this.applying += 1;
|
|
375
|
+
try {
|
|
376
|
+
// The main window first: the others fold onto the display it ends up on.
|
|
377
|
+
const order = [...this.open].sort((a, b) => Number(b === MAIN_WINDOW_ID) - Number(a === MAIN_WINDOW_ID));
|
|
378
|
+
for (const windowId of order) {
|
|
379
|
+
this.cancelSave(windowId);
|
|
380
|
+
const saved = restoring ? placementFor(this.setup, key, windowId) : null;
|
|
381
|
+
if (saved) {
|
|
382
|
+
this.place(windowId, { ...saved, frame: this.onScreen(saved.frame) });
|
|
383
|
+
restored.add(windowId);
|
|
384
|
+
continue;
|
|
385
|
+
}
|
|
386
|
+
// A window still waiting its turn to open has no native state yet; it opens where this puts it.
|
|
387
|
+
const waiting = this.windowedFrames.get(windowId);
|
|
388
|
+
const state = host.getWindowState(windowId)
|
|
389
|
+
?? (waiting ? { frame: waiting, maximized: false, fullscreen: false, minimized: false } : null);
|
|
390
|
+
if (!state) continue;
|
|
391
|
+
// Where the window sat before the change: the system may already have moved it.
|
|
392
|
+
const windowed = this.windowedFrames.get(windowId) ?? state.frame;
|
|
393
|
+
const from = displayForFrame(windowed, previousDisplays);
|
|
394
|
+
const stillThere = !!from && displays.some((display) => displayKey(display) === displayKey(from));
|
|
395
|
+
if (stillThere) {
|
|
396
|
+
const frame = this.onScreen(windowed);
|
|
397
|
+
this.windowedFrames.set(windowId, frame);
|
|
398
|
+
if (!sameRect(state.frame, frame) && !state.maximized && !state.fullscreen && host.canPlaceWindows) {
|
|
399
|
+
host.setFrame(windowId, frame);
|
|
400
|
+
}
|
|
401
|
+
continue;
|
|
402
|
+
}
|
|
403
|
+
const target = this.foldTarget(windowId, displays) ?? displays[0]!;
|
|
404
|
+
this.place(windowId, {
|
|
405
|
+
frame: this.onScreen(relocateFrame(windowed, from, displays, target)),
|
|
406
|
+
maximized: state.maximized,
|
|
407
|
+
fullscreen: state.fullscreen,
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
} finally {
|
|
411
|
+
this.applying -= 1;
|
|
412
|
+
}
|
|
413
|
+
// The new arrangement starts from where the windows are now; a restored one already has its entry.
|
|
414
|
+
for (const windowId of this.open) {
|
|
415
|
+
if (!restored.has(windowId)) this.remember(windowId);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
private place(windowId: string, placement: WindowPlacement): void {
|
|
420
|
+
const { host } = this.options;
|
|
421
|
+
const state = host.getWindowState(windowId);
|
|
422
|
+
this.windowedFrames.set(windowId, placement.frame);
|
|
423
|
+
if (!host.canPlaceWindows) {
|
|
424
|
+
if (state && (state.maximized !== placement.maximized)) host.setMaximized(windowId, placement.maximized);
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
427
|
+
// A maximized or full screen window takes a frame only once it is back to normal.
|
|
428
|
+
if (state?.fullscreen && host.canFullscreen) host.setFullscreen(windowId, false);
|
|
429
|
+
if (state?.maximized) host.setMaximized(windowId, false);
|
|
430
|
+
if (!state || !sameRect(state.frame, placement.frame)) host.setFrame(windowId, placement.frame);
|
|
431
|
+
this.applyModes(windowId, placement);
|
|
432
|
+
}
|
|
433
|
+
}
|