gloomberb 0.19.3 → 0.19.4

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 (104) hide show
  1. package/package.json +2 -2
  2. package/src/api-client/errors.ts +8 -0
  3. package/src/api-client/index.ts +18 -0
  4. package/src/api-client/request.ts +295 -42
  5. package/src/api-client/socket.ts +95 -0
  6. package/src/app/keybindings/actions.ts +1 -1
  7. package/src/app/pane-runtime/index.tsx +1 -1
  8. package/src/app/pane-runtime/ticker-inspector-runtime.ts +1 -1
  9. package/src/app/pane-runtime/ticker-open-runtime.ts +1 -1
  10. package/src/app/pane-runtime/ticker-open-target.ts +12 -2
  11. package/src/cli/desktop-pane-shot-api-policy.ts +10 -2
  12. package/src/cli/desktop-pane-shot.ts +2 -1
  13. package/src/components/command-bar/action-runtime.ts +2 -0
  14. package/src/components/command-bar/commands/direct/runtime.ts +3 -3
  15. package/src/components/command-bar/helpers.ts +2 -0
  16. package/src/components/command-bar/list/model.ts +9 -0
  17. package/src/components/command-bar/pane-actions.ts +46 -35
  18. package/src/components/command-bar/pane-templates/workflow.ts +6 -0
  19. package/src/components/command-bar/panel/index.tsx +30 -3
  20. package/src/components/command-bar/panel/types.ts +7 -0
  21. package/src/components/command-bar/routes/root/results.ts +4 -4
  22. package/src/components/command-bar/routes/root/selection.ts +3 -2
  23. package/src/components/command-bar/routes/ticker-search/actions.ts +21 -16
  24. package/src/components/command-bar/routing/launch-request.ts +3 -2
  25. package/src/components/command-bar/routing/list-state.ts +4 -1
  26. package/src/components/command-bar/surface/index.tsx +15 -1
  27. package/src/components/layout/floating-pane.tsx +10 -0
  28. package/src/components/layout/pane/frame.tsx +6 -0
  29. package/src/components/layout/pane/header.tsx +244 -42
  30. package/src/components/layout/pane/index.tsx +20 -2
  31. package/src/components/layout/shell/active-drag.ts +128 -27
  32. package/src/components/layout/shell/drag/index.ts +99 -96
  33. package/src/components/layout/shell/drag/live.ts +3 -17
  34. package/src/components/layout/shell/drag/magnets.ts +198 -0
  35. package/src/components/layout/shell/drag/overlays.tsx +77 -75
  36. package/src/components/layout/shell/drag/runtime.ts +12 -7
  37. package/src/components/layout/shell/index.tsx +66 -32
  38. package/src/components/layout/shell/native/pointer-runtime.ts +7 -2
  39. package/src/components/layout/shell/native/surfaces.ts +1 -9
  40. package/src/components/layout/shell/native/window-state.ts +21 -25
  41. package/src/components/layout/shell/pane/layers.tsx +153 -24
  42. package/src/components/layout/shell/shortcuts.ts +4 -1
  43. package/src/components/layout/shell/terminal-pointer-runtime.ts +24 -1
  44. package/src/components/layout/shell/window-mode/overlays.tsx +14 -6
  45. package/src/components/layout/status-bar.tsx +75 -6
  46. package/src/core/app-runtime.ts +4 -1
  47. package/src/core/connection-health.ts +76 -1
  48. package/src/core/state/app/command-bar-launch.ts +12 -0
  49. package/src/core/state/app/layout.ts +4 -3
  50. package/src/core/state/app/types.ts +4 -1
  51. package/src/data/config/layout.ts +2 -2
  52. package/src/data/config/store/fold-panes.ts +6 -1
  53. package/src/i18n/es.ts +8 -1
  54. package/src/i18n/ja.ts +8 -1
  55. package/src/i18n/ko.ts +8 -1
  56. package/src/i18n/zh-cn.ts +8 -1
  57. package/src/i18n/zh-tw.ts +8 -1
  58. package/src/layout/pane-follow.ts +3 -2
  59. package/src/layout/pane-layout-move.ts +2 -1
  60. package/src/layout/pane-manager/dock-stack.ts +38 -0
  61. package/src/layout/pane-manager/dock-tree.ts +76 -15
  62. package/src/layout/pane-manager/docking.ts +48 -6
  63. package/src/layout/pane-manager/floating-actions.ts +12 -3
  64. package/src/layout/pane-manager/floating-placement.ts +189 -0
  65. package/src/layout/pane-manager/floating.ts +23 -4
  66. package/src/layout/pane-manager/gridlock.ts +16 -4
  67. package/src/layout/pane-manager/hidden-panes.ts +30 -1
  68. package/src/layout/pane-manager/index.ts +2 -0
  69. package/src/layout/pane-manager/layout-state.ts +17 -0
  70. package/src/layout/pane-manager/types.ts +4 -1
  71. package/src/layout/ticker-retarget.ts +88 -0
  72. package/src/layout-marketplace/cloud.ts +9 -1
  73. package/src/market-data/connection-follow.ts +68 -0
  74. package/src/market-data/coordinator/entries.ts +15 -3
  75. package/src/market-data/coordinator/financials.ts +20 -7
  76. package/src/market-data/coordinator/index.ts +217 -4
  77. package/src/market-data/coordinator/quotes.ts +10 -0
  78. package/src/market-data/hooks.tsx +31 -0
  79. package/src/news/aggregator.ts +12 -1
  80. package/src/plugins/builtin/buildout/company/pane.tsx +2 -1
  81. package/src/plugins/builtin/buildout/model/index.ts +6 -0
  82. package/src/plugins/builtin/buildout/pane/index.tsx +2 -1
  83. package/src/plugins/builtin/chart-composer/index.tsx +13 -0
  84. package/src/plugins/builtin/chat/controller/index.ts +10 -3
  85. package/src/plugins/builtin/chat/controller/session-runtime.ts +9 -2
  86. package/src/plugins/builtin/chat/status-widget.tsx +3 -1
  87. package/src/plugins/builtin/help/index.tsx +1 -1
  88. package/src/plugins/builtin/sectors/client.ts +18 -8
  89. package/src/plugins/builtin/sectors/index.tsx +19 -5
  90. package/src/plugins/builtin/ticker-detail/overview/pane.tsx +4 -1
  91. package/src/plugins/builtin/ticker-detail/overview-tab.tsx +25 -2
  92. package/src/react/async-resource.ts +12 -0
  93. package/src/remote/controller.ts +3 -1
  94. package/src/renderers/browser/app-services.ts +8 -1
  95. package/src/renderers/browser/cloud-transport.ts +6 -1
  96. package/src/renderers/cloudflare/worker.ts +24 -2
  97. package/src/renderers/dom/styles.css +32 -0
  98. package/src/shares/portable-layout.ts +99 -2
  99. package/src/sources/provider-router/index.ts +2 -2
  100. package/src/sources/provider-router/sources.ts +26 -1
  101. package/src/sync/core-contributors.ts +18 -4
  102. package/src/time-series/resolve.ts +12 -0
  103. package/src/types/config.ts +17 -1
  104. package/src/types/plugin.ts +6 -0
@@ -0,0 +1,38 @@
1
+ import type { DockLayoutNode, DockSplitNode } from "../../types/config";
2
+
3
+ /**
4
+ * Panes stacked as tabs in one spot of the dock, one shown at a time: the
5
+ * panes of the subtree, first to last, as a chain of splits that each carry
6
+ * `tabs`. A build from before tabs reads the chain as a plain split and shows
7
+ * the panes side by side instead of losing them, and a split that loses one
8
+ * side is still a stack.
9
+ */
10
+ export type DockStackNode = DockSplitNode & { tabs: { active: string } };
11
+
12
+ export function isDockStack(node: DockLayoutNode | null | undefined): node is DockStackNode {
13
+ return node?.kind === "split" && !!node.tabs;
14
+ }
15
+
16
+ /**
17
+ * The stack of `instanceIds` showing `active` (the first when it is not one
18
+ * of them), in its stored form; one pane is a plain pane, none is nothing.
19
+ */
20
+ export function buildDockStack(instanceIds: readonly string[], active?: string | null): DockLayoutNode | null {
21
+ const ids = [...new Set(instanceIds)];
22
+ if (ids.length === 0) return null;
23
+ const shown = active && ids.includes(active) ? active : ids[0]!;
24
+ const chain = (index: number): DockLayoutNode => {
25
+ if (index === ids.length - 1) return { kind: "pane", instanceId: ids[index]! };
26
+ return {
27
+ kind: "split",
28
+ axis: "horizontal",
29
+ // Even widths where a build from before tabs shows them side by side
30
+ // (as far as a split's 0.1 to 0.9 allows).
31
+ ratio: Math.max(0.1, Math.min(0.9, 1 / (ids.length - index))),
32
+ first: { kind: "pane", instanceId: ids[index]! },
33
+ second: chain(index + 1),
34
+ tabs: { active: shown },
35
+ };
36
+ };
37
+ return chain(0);
38
+ }
@@ -1,8 +1,5 @@
1
- import type {
2
- DockLayoutNode,
3
- DockSplitNode,
4
- LayoutConfig,
5
- } from "../../types/config";
1
+ import type { DockLayoutNode, DockSplitNode, LayoutConfig } from "../../types/config";
2
+ import { buildDockStack, isDockStack } from "./dock-stack";
6
3
 
7
4
  const MIN_PANE_WIDTH = 20;
8
5
  const MIN_DOCKED_HEIGHT = 5;
@@ -22,10 +19,18 @@ export interface LayoutBounds {
22
19
  export interface DockLeafRef {
23
20
  instanceId: string;
24
21
  path: Array<0 | 1>;
22
+ /** A tab of a stack: where the stack is, the spot the pane shares. */
23
+ stackPath?: Array<0 | 1>;
25
24
  }
26
25
 
26
+ /**
27
+ * A spot of the dock on screen: one pane, or a stack of tabs showing
28
+ * `instanceId` (its `path` is then the stack's).
29
+ */
27
30
  export interface DockLeafLayout extends DockLeafRef {
28
31
  rect: LayoutBounds;
32
+ /** A stack: its tabs in order. */
33
+ stack?: readonly string[];
29
34
  }
30
35
 
31
36
  export interface DockDividerLayout {
@@ -44,6 +49,12 @@ export interface DockGeometryOptions {
44
49
  precise?: boolean;
45
50
  dividerSize?: number | { horizontal: number; vertical: number };
46
51
  reserveDividerGutters?: boolean;
52
+ /**
53
+ * Precise only: the space left between two panes, in cells per axis (the
54
+ * desktop's 4px). Each pane gives up half of it on the side it shares with
55
+ * the other; the split and its divider stay where the ratio puts them.
56
+ */
57
+ gap?: number | { horizontal: number; vertical: number };
47
58
  }
48
59
 
49
60
  export function clampRatio(ratio: number): number {
@@ -51,23 +62,37 @@ export function clampRatio(ratio: number): number {
51
62
  return Math.max(0.1, Math.min(0.9, ratio));
52
63
  }
53
64
 
54
- export function collectDockLeafRefs(node: DockLayoutNode | null, path: Array<0 | 1> = [], result: DockLeafRef[] = []): DockLeafRef[] {
65
+ /** Every docked pane, tabs of a stack included, and where it is in the tree. */
66
+ export function collectDockLeafRefs(
67
+ node: DockLayoutNode | null,
68
+ path: Array<0 | 1> = [],
69
+ result: DockLeafRef[] = [],
70
+ stackPath?: Array<0 | 1>,
71
+ ): DockLeafRef[] {
55
72
  if (!node) return result;
56
73
  if (node.kind === "pane") {
57
- result.push({ instanceId: node.instanceId, path });
74
+ result.push(stackPath ? { instanceId: node.instanceId, path, stackPath } : { instanceId: node.instanceId, path });
58
75
  return result;
59
76
  }
60
- collectDockLeafRefs(node.first, [...path, 0], result);
61
- collectDockLeafRefs(node.second, [...path, 1], result);
77
+ const inStack = stackPath ?? (isDockStack(node) ? path : undefined);
78
+ collectDockLeafRefs(node.first, [...path, 0], result, inStack);
79
+ collectDockLeafRefs(node.second, [...path, 1], result, inStack);
62
80
  return result;
63
81
  }
64
82
 
83
+ /** The pane a stack shows: its `active` tab, or its first when that is gone. */
84
+ export function dockStackShown(node: DockLayoutNode): string {
85
+ const members = getDockedInstanceIds(node);
86
+ return isDockStack(node) && members.includes(node.tabs.active) ? node.tabs.active : members[0]!;
87
+ }
88
+
65
89
  export function getDockedInstanceIds(node: DockLayoutNode | null): string[] {
66
90
  return collectDockLeafRefs(node).map((entry) => entry.instanceId);
67
91
  }
68
92
 
69
93
  export function getRepresentativeLeafId(node: DockLayoutNode, preferStart: boolean): string {
70
94
  if (node.kind === "pane") return node.instanceId;
95
+ if (isDockStack(node)) return dockStackShown(node);
71
96
  return getRepresentativeLeafId(preferStart ? node.first : node.second, preferStart);
72
97
  }
73
98
 
@@ -100,6 +125,20 @@ export function removeNodeAtPath(node: DockLayoutNode | null, path: Array<0 | 1>
100
125
  if (!node) return null;
101
126
  if (path.length === 0) return null;
102
127
  if (node.kind !== "split") return node;
128
+ if (isDockStack(node)) {
129
+ // A tab leaves its stack; the stack shows the tab next to it if that was the one shown.
130
+ const members = getDockedInstanceIds(node);
131
+ const removed = getNodeAtPath(node, path);
132
+ if (!removed) return node;
133
+ const leaving = new Set(getDockedInstanceIds(removed));
134
+ const kept = members.filter((id) => !leaving.has(id));
135
+ const shown = dockStackShown(node);
136
+ const index = members.indexOf(shown);
137
+ const next = leaving.has(shown)
138
+ ? members.slice(index + 1).find((id) => !leaving.has(id)) ?? kept.at(-1)
139
+ : shown;
140
+ return buildDockStack(kept, next);
141
+ }
103
142
 
104
143
  const [head, ...rest] = path;
105
144
  if (head === 0) {
@@ -167,27 +206,36 @@ function collectDockGeometry(
167
206
  leaves.push({ instanceId: node.instanceId, path, rect: { ...bounds } });
168
207
  return { leaves, dividers };
169
208
  }
209
+ if (isDockStack(node)) {
210
+ // One spot, the tab shown; the splits that hold the tabs are not laid out.
211
+ leaves.push({ instanceId: dockStackShown(node), path, rect: { ...bounds }, stack: getDockedInstanceIds(node) });
212
+ return { leaves, dividers };
213
+ }
170
214
 
171
215
  const precise = options.precise === true;
172
216
  const dividerSize = typeof options.dividerSize === "object"
173
217
  ? options.dividerSize[node.axis]
174
218
  : options.dividerSize ?? 1;
175
219
  const reserveDividerGutters = options.reserveDividerGutters === true && !precise;
220
+ const gap = precise
221
+ ? Math.max(0, typeof options.gap === "object" ? options.gap[node.axis] : options.gap ?? 0)
222
+ : 0;
176
223
 
177
224
  if (node.axis === "horizontal") {
178
225
  const reserveDividerGutter = reserveDividerGutters && bounds.width > dividerSize;
179
226
  const splitWidth = reserveDividerGutter ? bounds.width - dividerSize : bounds.width;
180
227
  const [firstWidth, secondWidth] = resolveSplitSizes(splitWidth, node.ratio, MIN_PANE_WIDTH, precise);
181
- const firstBounds = { x: bounds.x, y: bounds.y, width: firstWidth, height: bounds.height };
228
+ const halfGap = Math.min(gap / 2, firstWidth / 2, secondWidth / 2);
229
+ const firstBounds = { x: bounds.x, y: bounds.y, width: firstWidth - halfGap, height: bounds.height };
182
230
  const dividerX = reserveDividerGutter
183
231
  ? bounds.x + firstWidth
184
232
  : precise
185
233
  ? bounds.x + firstWidth - (dividerSize / 2)
186
234
  : bounds.x + firstWidth - 1;
187
235
  const secondBounds = {
188
- x: reserveDividerGutter ? bounds.x + firstWidth + dividerSize : bounds.x + firstWidth,
236
+ x: reserveDividerGutter ? bounds.x + firstWidth + dividerSize : bounds.x + firstWidth + halfGap,
189
237
  y: bounds.y,
190
- width: secondWidth,
238
+ width: secondWidth - halfGap,
191
239
  height: bounds.height,
192
240
  };
193
241
  dividers.push({
@@ -210,7 +258,8 @@ function collectDockGeometry(
210
258
  const reserveDividerGutter = reserveDividerGutters && bounds.height > dividerSize;
211
259
  const splitHeight = reserveDividerGutter ? bounds.height - dividerSize : bounds.height;
212
260
  const [firstHeight, secondHeight] = resolveSplitSizes(splitHeight, node.ratio, MIN_DOCKED_HEIGHT, precise);
213
- const firstBounds = { x: bounds.x, y: bounds.y, width: bounds.width, height: firstHeight };
261
+ const halfGap = Math.min(gap / 2, firstHeight / 2, secondHeight / 2);
262
+ const firstBounds = { x: bounds.x, y: bounds.y, width: bounds.width, height: firstHeight - halfGap };
214
263
  const dividerY = reserveDividerGutter
215
264
  ? bounds.y + firstHeight
216
265
  : precise
@@ -218,9 +267,9 @@ function collectDockGeometry(
218
267
  : bounds.y + firstHeight - 1;
219
268
  const secondBounds = {
220
269
  x: bounds.x,
221
- y: reserveDividerGutter ? bounds.y + firstHeight + dividerSize : bounds.y + firstHeight,
270
+ y: reserveDividerGutter ? bounds.y + firstHeight + dividerSize : bounds.y + firstHeight + halfGap,
222
271
  width: bounds.width,
223
- height: secondHeight,
272
+ height: secondHeight - halfGap,
224
273
  };
225
274
  dividers.push({
226
275
  path,
@@ -300,3 +349,15 @@ export function getDockResizeTargets(
300
349
  }))
301
350
  .sort((left, right) => right.path.length - left.path.length);
302
351
  }
352
+
353
+ /** The layout with `instanceId` shown in its stack: the same layout when it is not a tab, or is shown already. */
354
+ export function showDockTab(layout: LayoutConfig, instanceId: string): LayoutConfig {
355
+ const leaf = findDockLeaf(layout, instanceId);
356
+ if (!leaf?.stackPath || !layout.dockRoot) return layout;
357
+ const stack = getNodeAtPath(layout.dockRoot, leaf.stackPath);
358
+ if (!stack || !isDockStack(stack) || dockStackShown(stack) === instanceId) return layout;
359
+ return {
360
+ ...layout,
361
+ dockRoot: replaceNodeAtPath(layout.dockRoot, leaf.stackPath, buildDockStack(getDockedInstanceIds(stack), instanceId)!),
362
+ };
363
+ }
@@ -1,9 +1,11 @@
1
- import type { LayoutConfig, PaneInstanceConfig } from "../../types/config";
1
+ import type { DockLayoutNode, LayoutConfig, PaneInstanceConfig } from "../../types/config";
2
2
  import { findPaneInstance } from "../../types/config";
3
+ import { buildDockStack } from "./dock-stack";
3
4
  import {
4
5
  buildSplitAroundNode,
5
6
  clampRatio,
6
7
  findDockLeaf,
8
+ getDockedInstanceIds,
7
9
  getNodeAtPath,
8
10
  replaceNodeAtPath,
9
11
  type DockGeometryOptions,
@@ -64,11 +66,28 @@ function insertRelativeToLeaf(layout: LayoutConfig, instanceId: string, targetId
64
66
  const base = detachPane(layout, instanceId);
65
67
  const targetLeaf = findDockLeaf(base, targetId);
66
68
  if (!targetLeaf || !base.dockRoot) return layout;
67
- const targetNode = getNodeAtPath(base.dockRoot, targetLeaf.path);
69
+ // Beside a tab is beside its whole stack.
70
+ const spotPath = targetLeaf.stackPath ?? targetLeaf.path;
71
+ const targetNode = getNodeAtPath(base.dockRoot, spotPath);
68
72
  if (!targetNode) return layout;
69
73
  return finalizeLayout({
70
74
  ...base,
71
- dockRoot: replaceNodeAtPath(base.dockRoot, targetLeaf.path, buildSplitAroundNode(targetNode, instanceId, position)),
75
+ dockRoot: replaceNodeAtPath(base.dockRoot, spotPath, buildSplitAroundNode(targetNode, instanceId, position)),
76
+ });
77
+ }
78
+
79
+ /** `instanceId` as the last tab of the spot `targetId` is in, shown: the spot becomes a stack if it was one pane. */
80
+ function stackOnto(layout: LayoutConfig, instanceId: string, targetId: string): LayoutConfig {
81
+ if (instanceId === targetId) return layout;
82
+ const base = detachPane(layout, instanceId);
83
+ const targetLeaf = findDockLeaf(base, targetId);
84
+ if (!targetLeaf || !base.dockRoot) return layout;
85
+ const spotPath = targetLeaf.stackPath ?? targetLeaf.path;
86
+ const spot = getNodeAtPath(base.dockRoot, spotPath);
87
+ if (!spot) return layout;
88
+ return finalizeLayout({
89
+ ...base,
90
+ dockRoot: replaceNodeAtPath(base.dockRoot, spotPath, buildDockStack([...getDockedInstanceIds(spot), instanceId], instanceId)!),
72
91
  });
73
92
  }
74
93
 
@@ -92,6 +111,12 @@ export function dockPane(layout: LayoutConfig, instanceId: string, target?: Dock
92
111
 
93
112
  const instance = findPaneInstance(layout, instanceId);
94
113
  const dockedMemory = instance?.placementMemory?.docked;
114
+ if (dockedMemory?.position === "tab") {
115
+ // A tab goes back into its stack while a pane of it is still docked.
116
+ const anchor = dockedMemory.anchorInstanceId;
117
+ if (anchor && anchor !== instanceId && findDockLeaf(layout, anchor)) return stackOnto(layout, instanceId, anchor);
118
+ return insertAtRootEdge(layout, instanceId, "right");
119
+ }
95
120
  if (dockedMemory?.position && dockedMemory.path && dockedMemory.path.length > 0) {
96
121
  return insertRelativeToSubtreePath(layout, instanceId, dockedMemory.path.slice(0, -1), dockedMemory.position);
97
122
  }
@@ -108,10 +133,13 @@ export function applyDrop(layout: LayoutConfig, draggedId: string, dropTarget: D
108
133
  if (dropTarget.kind === "frame") {
109
134
  return insertAtRootEdge(layout, draggedId, dropTarget.edge);
110
135
  }
136
+ if (dropTarget.kind === "tab") {
137
+ return stackOnto(layout, draggedId, dropTarget.targetId);
138
+ }
111
139
 
112
140
  const normalized = normalizeDropPosition(dropTarget.position);
113
141
  if (normalized === "center") {
114
- return swapPanes(layout, draggedId, dropTarget.targetId);
142
+ return stackOnto(layout, draggedId, dropTarget.targetId);
115
143
  }
116
144
  const direction = normalized === "top" ? "above" : normalized === "bottom" ? "below" : normalized;
117
145
  return insertRelativeToLeaf(layout, draggedId, dropTarget.targetId, direction);
@@ -131,6 +159,18 @@ export function simulateDrop(
131
159
  };
132
160
  }
133
161
 
162
+ /** `node` with each stack that showed a pane in `moved` showing the pane that took its place. */
163
+ function withShownTabsMoved(node: DockLayoutNode | null, moved: ReadonlyMap<string, string>): DockLayoutNode | null {
164
+ if (!node || node.kind === "pane") return node;
165
+ const active = node.tabs ? moved.get(node.tabs.active) : undefined;
166
+ return {
167
+ ...node,
168
+ first: withShownTabsMoved(node.first, moved)!,
169
+ second: withShownTabsMoved(node.second, moved)!,
170
+ ...(node.tabs && active ? { tabs: { active } } : {}),
171
+ };
172
+ }
173
+
134
174
  export function swapPanes(layout: LayoutConfig, firstId: string, secondId: string): LayoutConfig {
135
175
  if (firstId === secondId) return layout;
136
176
  const firstLeaf = findDockLeaf(layout, firstId);
@@ -146,7 +186,9 @@ export function swapPanes(layout: LayoutConfig, firstId: string, secondId: strin
146
186
  if (!firstNode || !secondNode || firstNode.kind !== "pane" || secondNode.kind !== "pane") return layout;
147
187
  const withFirst = replaceNodeAtPath(layout.dockRoot, firstLeaf.path, { kind: "pane", instanceId: secondId });
148
188
  const withSecond = replaceNodeAtPath(withFirst, secondLeaf.path, { kind: "pane", instanceId: firstId });
149
- return finalizeLayout({ ...layout, dockRoot: withSecond });
189
+ // A tab trades places too, and is still the one shown where it was.
190
+ const moved = new Map([[firstId, secondId], [secondId, firstId]]);
191
+ return finalizeLayout({ ...layout, dockRoot: withShownTabsMoved(withSecond, moved) });
150
192
  }
151
193
 
152
194
  if (firstFloating && secondFloating) {
@@ -165,7 +207,7 @@ export function swapPanes(layout: LayoutConfig, firstId: string, secondId: strin
165
207
  const dockedLeaf = firstLeaf ?? secondLeaf!;
166
208
  const floatingEntry = firstFloating ?? secondFloating!;
167
209
  const base = layout.dockRoot && dockedLeaf
168
- ? replaceNodeAtPath(layout.dockRoot, dockedLeaf.path, { kind: "pane", instanceId: floatingId })
210
+ ? withShownTabsMoved(replaceNodeAtPath(layout.dockRoot, dockedLeaf.path, { kind: "pane", instanceId: floatingId }), new Map([[dockedId, floatingId]]))
169
211
  : layout.dockRoot;
170
212
 
171
213
  return finalizeLayout({
@@ -9,6 +9,7 @@ import {
9
9
  clampFloatingRect,
10
10
  clampFloatingRectWithinBounds,
11
11
  defaultFloatingRect,
12
+ defaultFloatingSize,
12
13
  compactFloatingZ,
13
14
  maxFloatingZ,
14
15
  MIN_FLOAT_HEIGHT,
@@ -19,6 +20,7 @@ import {
19
20
  import type { LayoutBounds } from "./dock-tree";
20
21
  import type { FloatingResizeCorner } from "./types";
21
22
  import { detachPane, ensurePaneInstance, finalizeLayout } from "./layout-state";
23
+ import { avoidStackedRect, placeFloatingRect, type FloatingPlacementOptions } from "./floating-placement";
22
24
 
23
25
  export function detachPaneToFrame(
24
26
  layout: LayoutConfig,
@@ -112,19 +114,25 @@ export function resizeFloatingPaneFromCorner(
112
114
  }));
113
115
  }
114
116
 
117
+ /**
118
+ * Floats a pane where it floated last, or, the first time, where there is
119
+ * room (`placeFloatingRect`). Either way it never lands exactly on another
120
+ * window.
121
+ */
115
122
  export function floatPane(
116
123
  layout: LayoutConfig,
117
124
  instanceId: string,
118
125
  termWidth: number,
119
126
  termHeight: number,
120
127
  def?: PaneDef,
128
+ options?: FloatingPlacementOptions,
121
129
  ): LayoutConfig {
122
130
  const instance = findPaneInstance(layout, instanceId);
123
131
  if (!instance) return layout;
124
132
  const remembered = instance.placementMemory?.floating;
125
133
  const rect = remembered
126
- ? clampFloatingRect(remembered, termWidth, termHeight)
127
- : defaultFloatingRect(termWidth, termHeight, def);
134
+ ? avoidStackedRect(layout, instanceId, clampFloatingRect(remembered, termWidth, termHeight), termWidth, termHeight)
135
+ : placeFloatingRect(layout, instanceId, defaultFloatingSize(termWidth, termHeight, def), termWidth, termHeight, options);
128
136
  return floatAtRect(layout, instanceId, rect);
129
137
  }
130
138
 
@@ -134,10 +142,11 @@ export function addPaneFloating(
134
142
  termWidth: number,
135
143
  termHeight: number,
136
144
  def?: PaneDef,
145
+ options?: FloatingPlacementOptions,
137
146
  ): LayoutConfig {
138
147
  const resolvedInstance = typeof instance === "string" ? createPaneInstance(instance) : instance;
139
148
  const withInstance = ensurePaneInstance(layout, resolvedInstance);
140
- return floatPane(withInstance, resolvedInstance.instanceId, termWidth, termHeight, def);
149
+ return floatPane(withInstance, resolvedInstance.instanceId, termWidth, termHeight, def, options);
141
150
  }
142
151
 
143
152
  export function bringToFront(layout: LayoutConfig, instanceId: string): LayoutConfig {
@@ -0,0 +1,189 @@
1
+ import type { LayoutConfig } from "../../types/config";
2
+ import { getDockLeafLayouts, type LayoutBounds } from "./dock-tree";
3
+ import { clampFloatingRect, MIN_FLOAT_HEIGHT, MIN_FLOAT_WIDTH, type FloatingRect } from "./floating";
4
+
5
+ /** What a new floating pane keeps clear of beyond the other floating panes. */
6
+ export interface FloatingPlacementOptions {
7
+ /** The pane being worked in: a new window never covers all of it. */
8
+ focusedPaneId?: string | null;
9
+ }
10
+
11
+ /** How far each window of a cascade sits from the one before it, in cells: past its header and its left edge. */
12
+ const CASCADE_STEP = { x: 2, y: 1 };
13
+ /** A free spot has to hold at least this share of the window's size on each axis to be worth shrinking into. */
14
+ const MIN_FREE_SHARE = 0.5;
15
+ /** Past this many windows to keep clear of, the free spots are slivers: cascade instead of searching them. */
16
+ const MAX_FREE_SEARCH_OBSTACLES = 12;
17
+
18
+ function overlaps(a: LayoutBounds, b: LayoutBounds): boolean {
19
+ return a.x < b.x + b.width && b.x < a.x + a.width && a.y < b.y + b.height && b.y < a.y + a.height;
20
+ }
21
+
22
+ function contains(outer: LayoutBounds, inner: LayoutBounds): boolean {
23
+ return outer.x <= inner.x
24
+ && outer.y <= inner.y
25
+ && outer.x + outer.width >= inner.x + inner.width
26
+ && outer.y + outer.height >= inner.y + inner.height;
27
+ }
28
+
29
+ /**
30
+ * Every rectangle of `area` that touches none of `obstacles` and runs between
31
+ * the lines their edges and the area's draw. Every largest empty rectangle
32
+ * lies on those lines, so the largest are among them; fine for the few
33
+ * windows a layout floats.
34
+ */
35
+ function forEachFreeRect(area: LayoutBounds, obstacles: readonly LayoutBounds[], visit: (rect: LayoutBounds) => void): void {
36
+ const clip = (value: number, start: number, length: number) => Math.max(start, Math.min(start + length, value));
37
+ const xs = [...new Set([area.x, area.x + area.width, ...obstacles.flatMap((rect) => [rect.x, rect.x + rect.width].map((x) => clip(x, area.x, area.width)))])].sort((a, b) => a - b);
38
+ const ys = [...new Set([area.y, area.y + area.height, ...obstacles.flatMap((rect) => [rect.y, rect.y + rect.height].map((y) => clip(y, area.y, area.height)))])].sort((a, b) => a - b);
39
+ for (let left = 0; left < xs.length; left += 1) {
40
+ for (let right = left + 1; right < xs.length; right += 1) {
41
+ for (let top = 0; top < ys.length; top += 1) {
42
+ for (let bottom = top + 1; bottom < ys.length; bottom += 1) {
43
+ const rect = { x: xs[left]!, y: ys[top]!, width: xs[right]! - xs[left]!, height: ys[bottom]! - ys[top]! };
44
+ // Taller ones from this corner only overlap more.
45
+ if (obstacles.some((obstacle) => overlaps(rect, obstacle))) break;
46
+ visit(rect);
47
+ }
48
+ }
49
+ }
50
+ }
51
+ }
52
+
53
+ /** Where a pane sits on screen: its floating rect, or its docked one laid out over `area`. */
54
+ function screenRect(layout: LayoutConfig, instanceId: string, area: LayoutBounds): LayoutBounds | null {
55
+ const floating = layout.floating.find((entry) => entry.instanceId === instanceId);
56
+ if (floating) return floating;
57
+ return getDockLeafLayouts(layout, area).find((leaf) => leaf.instanceId === instanceId)?.rect ?? null;
58
+ }
59
+
60
+ /**
61
+ * The rect a new floating window of `size` opens at in a `termWidth` by
62
+ * `termHeight` area: in the largest spot clear of the other floating windows
63
+ * and of the focused pane, shrunk to it when it is a little small, and centred
64
+ * in it; otherwise one step down and right of the frontmost window, a cascade.
65
+ * It never lands exactly on another window, and never covers all of the
66
+ * focused pane when there is any other way.
67
+ */
68
+ export function placeFloatingRect(
69
+ layout: LayoutConfig,
70
+ instanceId: string,
71
+ size: { width: number; height: number },
72
+ termWidth: number,
73
+ termHeight: number,
74
+ options: FloatingPlacementOptions = {},
75
+ ): FloatingRect {
76
+ const area = { x: 0, y: 0, width: Math.max(1, termWidth), height: Math.max(1, termHeight) };
77
+ const others = layout.floating.filter((entry) => entry.instanceId !== instanceId);
78
+ const focusedId = options.focusedPaneId && options.focusedPaneId !== instanceId ? options.focusedPaneId : null;
79
+ const focused = focusedId ? screenRect(layout, focusedId, area) : null;
80
+ const obstacles: LayoutBounds[] = [...others];
81
+ if (focused && !others.some((entry) => entry.instanceId === focusedId)) obstacles.push(focused);
82
+ const width = Math.min(size.width, area.width);
83
+ const height = Math.min(size.height, area.height);
84
+
85
+ if (obstacles.length <= MAX_FREE_SEARCH_OBSTACLES) {
86
+ const spot = bestFreeSpot(area, obstacles, width, height);
87
+ if (spot) return clampFloatingRect(spot, termWidth, termHeight);
88
+ }
89
+ return cascadeRect(area, others, focused, width, height, termWidth, termHeight);
90
+ }
91
+
92
+ /** The free rect that holds the most of a `width` by `height` window, with the window centred in it; null when none holds enough. */
93
+ function bestFreeSpot(area: LayoutBounds, obstacles: readonly LayoutBounds[], width: number, height: number): FloatingRect | null {
94
+ const minWidth = Math.max(Math.min(MIN_FLOAT_WIDTH, width), Math.ceil(width * MIN_FREE_SHARE));
95
+ const minHeight = Math.max(Math.min(MIN_FLOAT_HEIGHT, height), Math.ceil(height * MIN_FREE_SHARE));
96
+ const found: { best: { rect: LayoutBounds; fit: number } | null } = { best: null };
97
+ forEachFreeRect(area, obstacles, (rect) => {
98
+ const fitWidth = Math.min(width, rect.width);
99
+ const fitHeight = Math.min(height, rect.height);
100
+ if (fitWidth < minWidth || fitHeight < minHeight) return;
101
+ const fit = fitWidth * fitHeight;
102
+ const best = found.best;
103
+ const room = rect.width * rect.height;
104
+ const bestRoom = best ? best.rect.width * best.rect.height : 0;
105
+ // Ties go to the roomier spot, then to the right and to the top, where a new window reads as new.
106
+ if (!best || fit > best.fit
107
+ || (fit === best.fit && (room > bestRoom
108
+ || (room === bestRoom && (rect.x > best.rect.x || (rect.x === best.rect.x && rect.y < best.rect.y)))))) {
109
+ found.best = { rect, fit };
110
+ }
111
+ });
112
+ const best = found.best;
113
+ if (!best) return null;
114
+ const fitWidth = Math.min(width, best.rect.width);
115
+ const fitHeight = Math.min(height, best.rect.height);
116
+ return {
117
+ x: Math.floor(best.rect.x + (best.rect.width - fitWidth) / 2),
118
+ y: Math.floor(best.rect.y + (best.rect.height - fitHeight) / 2),
119
+ width: Math.floor(fitWidth),
120
+ height: Math.floor(fitHeight),
121
+ };
122
+ }
123
+
124
+ /**
125
+ * One step down and right of the frontmost window (or centred, with no
126
+ * window to follow), wrapping to the top left when it runs off the area, and
127
+ * stepping on while it would sit exactly on another window or cover all of
128
+ * the focused pane.
129
+ */
130
+ function cascadeRect(
131
+ area: LayoutBounds,
132
+ others: readonly { x: number; y: number; width: number; height: number; zIndex?: number }[],
133
+ focused: LayoutBounds | null,
134
+ width: number,
135
+ height: number,
136
+ termWidth: number,
137
+ termHeight: number,
138
+ ): FloatingRect {
139
+ const front = others.reduce<(typeof others)[number] | null>(
140
+ (top, entry) => (!top || (entry.zIndex ?? 0) >= (top.zIndex ?? 0) ? entry : top),
141
+ null,
142
+ );
143
+ const maxX = Math.max(0, area.width - width);
144
+ const maxY = Math.max(0, area.height - height);
145
+ let x = front ? front.x + CASCADE_STEP.x : Math.floor(maxX / 2);
146
+ let y = front ? front.y + CASCADE_STEP.y : Math.floor(maxY / 2);
147
+ const taken = (left: number, top: number) => others.some((entry) => Math.round(entry.x) === left && Math.round(entry.y) === top);
148
+ const coversFocus = (left: number, top: number) => !!focused && contains({ x: left, y: top, width, height }, focused);
149
+ const attempts = Math.max(1, Math.ceil(Math.max(maxX / CASCADE_STEP.x, maxY / CASCADE_STEP.y)) + others.length + 1);
150
+ let fallback: { x: number; y: number } | null = null;
151
+ for (let attempt = 0; attempt < attempts; attempt += 1) {
152
+ if (x > maxX || y > maxY) {
153
+ // Off the edge: start a new run from the top left, a step in.
154
+ x = Math.min(maxX, CASCADE_STEP.x * (attempt % 4));
155
+ y = 0;
156
+ }
157
+ x = Math.round(x);
158
+ y = Math.round(y);
159
+ if (!taken(x, y)) {
160
+ if (!coversFocus(x, y)) return clampFloatingRect({ x, y, width, height }, termWidth, termHeight);
161
+ fallback ??= { x, y };
162
+ }
163
+ x += CASCADE_STEP.x;
164
+ y += CASCADE_STEP.y;
165
+ }
166
+ const at = fallback ?? { x: Math.min(maxX, x), y: Math.min(maxY, y) };
167
+ return clampFloatingRect({ ...at, width, height }, termWidth, termHeight);
168
+ }
169
+
170
+ /**
171
+ * A remembered or chosen rect moved off any window it would sit exactly on:
172
+ * stepped down and right until it lands on no other window's corner.
173
+ */
174
+ export function avoidStackedRect(layout: LayoutConfig, instanceId: string, rect: FloatingRect, termWidth: number, termHeight: number): FloatingRect {
175
+ const others = layout.floating.filter((entry) => entry.instanceId !== instanceId);
176
+ let next = rect;
177
+ for (let attempt = 0; attempt < others.length + 1; attempt += 1) {
178
+ const stacked = others.some((entry) => Math.round(entry.x) === Math.round(next.x) && Math.round(entry.y) === Math.round(next.y));
179
+ if (!stacked) return next;
180
+ const moved = clampFloatingRect({ ...next, x: next.x + CASCADE_STEP.x, y: next.y + CASCADE_STEP.y }, termWidth, termHeight);
181
+ if (moved.x === next.x && moved.y === next.y) {
182
+ // Against the far corner: start again from the top left.
183
+ next = clampFloatingRect({ ...next, x: 0, y: 0 }, termWidth, termHeight);
184
+ } else {
185
+ next = moved;
186
+ }
187
+ }
188
+ return next;
189
+ }
@@ -53,9 +53,19 @@ export function clampFloatingRectWithinBounds(rect: FloatingRect, bounds: Layout
53
53
  };
54
54
  }
55
55
 
56
+ /** A new floating pane's size: its own default, or 60% of the area each way. */
57
+ export function defaultFloatingSize(termWidth: number, termHeight: number, def?: PaneDef): { width: number; height: number } {
58
+ const { width, height } = clampFloatingRect({
59
+ x: 0,
60
+ y: 0,
61
+ width: def?.defaultFloatingSize?.width ?? Math.floor(termWidth * 0.6),
62
+ height: def?.defaultFloatingSize?.height ?? Math.floor(termHeight * 0.6),
63
+ }, termWidth, termHeight);
64
+ return { width, height };
65
+ }
66
+
56
67
  export function defaultFloatingRect(termWidth: number, termHeight: number, def?: PaneDef): FloatingRect {
57
- const width = def?.defaultFloatingSize?.width ?? Math.floor(termWidth * 0.6);
58
- const height = def?.defaultFloatingSize?.height ?? Math.floor(termHeight * 0.6);
68
+ const { width, height } = defaultFloatingSize(termWidth, termHeight, def);
59
69
  return clampFloatingRect({
60
70
  x: Math.floor((termWidth - width) / 2),
61
71
  y: Math.floor((termHeight - height) / 2),
@@ -71,8 +81,17 @@ export function maxFloatingZ(layout: LayoutConfig): number {
71
81
  /** Where floating panes stack from, above the docked panes and the dividers between them. */
72
82
  const FLOATING_PANE_BASE_Z = 50;
73
83
 
74
- /** The lowest of the shell's fixed overlays over every pane: the drag outline (95; the drop preview is 96). */
75
- const SHELL_PREVIEW_Z = 95;
84
+ /**
85
+ * The shell's fixed overlays of a pane move, over every pane. A move draws a
86
+ * pair at `under` and `over`: on the desktop the landing preview under the
87
+ * pane on the move (lifted to `over` while it is over a target, or its outline
88
+ * when it was docked); in the terminal, which cannot see through a pane, the
89
+ * docked pane's outline under the landing outline.
90
+ */
91
+ export const SHELL_DRAG_Z = { under: 95, over: 96 } as const;
92
+
93
+ /** The lowest of the shell's fixed overlays over every pane. */
94
+ const SHELL_PREVIEW_Z = SHELL_DRAG_Z.under;
76
95
 
77
96
  /**
78
97
  * How far above its own z a floating pane draws: its resize edges one level
@@ -1,4 +1,5 @@
1
- import type { FloatingPaneEntry, LayoutConfig } from "../../types/config";
1
+ import type { DockLayoutNode, FloatingPaneEntry, LayoutConfig } from "../../types/config";
2
+ import { buildDockStack } from "./dock-stack";
2
3
  import type { AppNotificationRequest } from "../../types/plugin";
3
4
  import {
4
5
  boundsForRects,
@@ -91,6 +92,15 @@ export function shouldShowTidyWindows(layout: LayoutConfig): boolean {
91
92
  || analyzeFloatingPaneVisibility(layout).some((pane) => pane.buried);
92
93
  }
93
94
 
95
+ function withStacks(node: DockLayoutNode | null, stacks: ReadonlyMap<string, readonly string[]>): DockLayoutNode | null {
96
+ if (!node || stacks.size === 0) return node;
97
+ if (node.kind === "pane") {
98
+ const tabs = stacks.get(node.instanceId);
99
+ return tabs ? buildDockStack(tabs, node.instanceId) : node;
100
+ }
101
+ return { ...node, first: withStacks(node.first, stacks)!, second: withStacks(node.second, stacks)! };
102
+ }
103
+
94
104
  export function gridlockAllPanes(
95
105
  layout: LayoutConfig,
96
106
  bounds: LayoutBounds = { x: 0, y: 0, width: 120, height: 40 },
@@ -99,8 +109,10 @@ export function gridlockAllPanes(
99
109
  const visibleLayout = paneTypes
100
110
  ? removeUnavailablePaneTypes(layout, paneTypes)
101
111
  : layout;
102
- const dockedRects: GridlockRect[] = getDockLeafLayouts(visibleLayout, bounds)
103
- .map((leaf) => ({ instanceId: leaf.instanceId, ...leaf.rect }));
112
+ const leaves = getDockLeafLayouts(visibleLayout, bounds);
113
+ const dockedRects: GridlockRect[] = leaves.map((leaf) => ({ instanceId: leaf.instanceId, ...leaf.rect }));
114
+ // A stack is tiled as the tab it shows and keeps its other tabs.
115
+ const stacks = new Map(leaves.flatMap((leaf) => (leaf.stack ? [[leaf.instanceId, leaf.stack] as const] : [])));
104
116
  const floatingRects: GridlockRect[] = visibleLayout.floating.map((entry) => ({
105
117
  instanceId: entry.instanceId,
106
118
  x: entry.x,
@@ -113,7 +125,7 @@ export function gridlockAllPanes(
113
125
 
114
126
  return finalizeLayout({
115
127
  ...visibleLayout,
116
- dockRoot: inferDockTreeFromRects(allRects, boundsForRects(allRects)),
128
+ dockRoot: withStacks(inferDockTreeFromRects(allRects, boundsForRects(allRects)), stacks),
117
129
  floating: [],
118
130
  });
119
131
  }