@n-uf/placement 26.11.54 → 26.11.55

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/dist/tiling.d.ts CHANGED
@@ -1,28 +1,6 @@
1
- import { R as Rect, S as Size, P as Point } from './geometry-L5gCoiZV.js';
1
+ import { S as Size, R as Rect, P as Point } from './geometry-L5gCoiZV.js';
2
2
  import { Target } from './index.js';
3
3
 
4
- /**
5
- * The order layouts: panes in order and an area in, a rect per pane out. Pure rect functions; a drop on one reorders
6
- * (the dragged pane inserted at an index, see `orderTargets`).
7
- */
8
-
9
- /** How a layout spaces its panes: `gap` px between neighbors (default 0), `ratio` the master's share (default 0.5). */
10
- interface TileOptions {
11
- readonly gap?: number;
12
- readonly ratio?: number;
13
- }
14
- /** An order layout: `masterStack`, `columns`, `grid`, or the app's own of the same shape. */
15
- type OrderLayout = <K>(panes: readonly K[], area: Rect, options?: TileOptions) => Map<K, Rect>;
16
- /**
17
- * Master and stack: the first pane takes the left part (`ratio` of the width less the gap), the rest stack on the
18
- * right in equal rows. One pane takes the whole area.
19
- */
20
- declare function masterStack<K>(panes: readonly K[], area: Rect, options?: TileOptions): Map<K, Rect>;
21
- /** Equal columns, full height. */
22
- declare function columns<K>(panes: readonly K[], area: Rect, options?: TileOptions): Map<K, Rect>;
23
- /** A grid as square as the count allows; the last row's cells widen to fill it. */
24
- declare function grid<K>(panes: readonly K[], area: Rect, options?: TileOptions): Map<K, Rect>;
25
-
26
4
  /**
27
5
  * A pane's drop regions: a center rectangle (a drop there swaps with the pane) and four side trapezoids (a drop there
28
6
  * splits the pane on that side), formed by the diagonals from each pane corner to the nearest corner of the center.
@@ -48,8 +26,14 @@ interface PaneRegionOptions {
48
26
  readonly hysteresis?: number;
49
27
  /** Device pixels per px: the center's edges snap to that grid, as the browser draws them. */
50
28
  readonly pixelRatio?: number;
29
+ /**
30
+ * The pane's center rectangle as the host already has it (pane-local px, as `paneCenter` gives), used as it is in
31
+ * place of measuring: `center`, `centerX`, `centerY`, `centerMin` and `pixelRatio` are not read. Only `paneRegion`
32
+ * reads it; its `width` and `height` are the pane's own size for the hold toward the middle.
33
+ */
34
+ readonly paneCenter?: PaneCenter;
51
35
  }
52
- declare const PANE_REGION_DEFAULTS: Required<Omit<PaneRegionOptions, "centerX" | "centerY">>;
36
+ declare const PANE_REGION_DEFAULTS: Required<Omit<PaneRegionOptions, "centerX" | "centerY" | "paneCenter">>;
53
37
  /** The center rectangle of a pane of `size`, in pane-local px (origin at the pane's top-left corner). */
54
38
  interface PaneCenter {
55
39
  readonly width: number;
@@ -64,16 +48,68 @@ declare function paneCenter(size: Size, options?: PaneRegionOptions): PaneCenter
64
48
  /**
65
49
  * The drop region of `pane` (in the same coordinates as `point`) the point is in. With a `previous` region, the point
66
50
  * has to be `hysteresis` px past that region's boundary before the region changes: no time-based debounce, only
67
- * geometry.
51
+ * geometry. A host that has measured the center itself passes it as `options.paneCenter`; the same center gives the
52
+ * same region as the one measured here.
68
53
  */
69
54
  declare function paneRegion(pane: Rect, point: Point, previous?: PaneRegion | null, options?: PaneRegionOptions): PaneRegion;
70
55
 
56
+ /**
57
+ * The order layouts: panes in order and an area in, a rect per pane out. Pure rect functions; a drop on one reorders
58
+ * (the dragged pane inserted at an index, see `orderTargets`).
59
+ */
60
+
61
+ /**
62
+ * How a layout spaces its panes: `gap` px between neighbors (default 0), `ratio` the master's share (default 0.5),
63
+ * `handle` px a split's divider takes beside the gap (default 0; only a split tree reads it).
64
+ */
65
+ interface TileOptions {
66
+ readonly gap?: number;
67
+ readonly ratio?: number;
68
+ readonly handle?: number;
69
+ }
70
+ /**
71
+ * The master area: `count` panes (rounded, kept within 1..n; default 1) on `side` (default `"left"`). Left and right
72
+ * make it a column of panes stacked vertically; top and bottom a row of panes side by side. The one home of the
73
+ * concept: `masterStack` takes it in its options (`OrderOptions`), a split that lays out as master and stack in its
74
+ * `master` field.
75
+ */
76
+ interface MasterOptions {
77
+ readonly count?: number;
78
+ readonly side?: PaneSide;
79
+ }
80
+ /** What the order layouts take: `TileOptions` and the master area's `count` and `side` (`columns` and `grid` ignore them). */
81
+ type OrderOptions = TileOptions & MasterOptions;
82
+ /** An order layout: `masterStack`, `columns`, `grid`, or the app's own of the same shape. */
83
+ type OrderLayout = <K>(panes: readonly K[], area: Rect, options?: OrderOptions) => Map<K, Rect>;
84
+ /** The master's share in effect: clamped to 0.05..0.95; one that is not finite is 0.5. */
85
+ declare const masterRatio: (v: number | undefined) => number;
86
+ /** How many of `n` panes are masters for a requested `count`: rounded, within 1..n; NaN is 1. */
87
+ declare function masterCount(count: number | undefined, n: number): number;
88
+ /**
89
+ * Master and stack: the first `count` panes are the masters, in an area on `side` that takes `ratio` of the room the
90
+ * gap leaves (clamped to 0.05..0.95; default 0.5); the rest are the stack, in the other area. Left and right make
91
+ * each area a column of panes stacked vertically; top and bottom a row of panes side by side. Right and bottom put
92
+ * the stack first in space. With no stack (n at most `count`) all the panes fill the area, stacked along the side's
93
+ * cross axis. Each area divides its length equally between its panes, the gap between them.
94
+ */
95
+ declare function masterStack<K>(panes: readonly K[], area: Rect, options?: OrderOptions): Map<K, Rect>;
96
+ /** Equal columns, full height. */
97
+ declare function columns<K>(panes: readonly K[], area: Rect, options?: TileOptions): Map<K, Rect>;
98
+ /** A grid as square as the count allows; the last row's cells widen to fill it. */
99
+ declare function grid<K>(panes: readonly K[], area: Rect, options?: TileOptions): Map<K, Rect>;
100
+
71
101
  /**
72
102
  * The split tree: how panes divide an area. A node is a pane, or a split of its rect in two (`a` then `b`). A split
73
103
  * with an `axis` divides along it (`"x"`: `a` left of `b`; `"y"`: `a` above `b`) at `ratio`; one without divides
74
104
  * along the rect's longer side in halves, which is dwindle (split along the longer side). Pure: trees in, trees out.
75
105
  */
76
106
 
107
+ /**
108
+ * A pane, or a split of its rect in two, `a` then `b`. A split takes its `axis` and its `ratio` (the first side's
109
+ * share); `gap` replaces the layout's gap for this split (the handle stays global); `id` names the split so
110
+ * `layoutSplitTree` reports its geometry. With `master`, the split lays its subtree out flat: its panes, in order,
111
+ * as master and stack (`masterStack`) in its rect, by its `ratio` and gap; the splits under it are not laid out.
112
+ */
77
113
  type Split<K> = {
78
114
  readonly pane: K;
79
115
  } | {
@@ -81,14 +117,10 @@ type Split<K> = {
81
117
  readonly b: Split<K>;
82
118
  readonly axis?: "x" | "y";
83
119
  readonly ratio?: number;
120
+ readonly gap?: number;
121
+ readonly id?: string;
122
+ readonly master?: MasterOptions;
84
123
  };
85
- /**
86
- * The rect of every pane of `tree` in `area`, `gap` px between the two sides of each split. A split's `ratio` is
87
- * clamped to 0..1; each side gets its share of the room the gap leaves (none when the gap is wider than the rect), so
88
- * no size is ever negative, and at ratio 0.5 both sides are exactly equal. A split with no room lays both sides at no
89
- * size inside its rect: the first at its start, the second at its end, never past it.
90
- */
91
- declare function layoutSplit<K>(tree: Split<K> | null, area: Rect, options?: TileOptions): Map<K, Rect>;
92
124
  /** The panes of `tree` in order (`a` before `b`). */
93
125
  declare const panesOf: <K>(tree: Split<K> | null) => K[];
94
126
  /**
@@ -111,6 +143,132 @@ declare function splitFromOrder<K>(panes: readonly K[]): Split<K> | null;
111
143
  */
112
144
  declare function dropOnPane<K>(tree: Split<K> | null, dragged: K, at: K, region: PaneRegion): Split<K>;
113
145
 
146
+ /**
147
+ * The split tree laid out: the rect of every pane of a `Split` tree in an area, and the geometry of the splits that
148
+ * have an id. See docs/placement.md, Entry: tiling.
149
+ */
150
+
151
+ /** A split child's fixed length along its split. `keep` marks one that holds its length even beside another. */
152
+ interface FixedLength {
153
+ readonly length: number;
154
+ readonly keep?: boolean;
155
+ }
156
+ /** A split child's least length along its split. `net` (default true) keeps the split's ratio within 5..95 %. */
157
+ interface MinLength {
158
+ readonly min: number;
159
+ readonly net?: boolean;
160
+ }
161
+ /**
162
+ * What a split tree's layout reads of its host, each per split, of the split's two direct children.
163
+ *
164
+ * - `fixed(child, axis)`: the child's fixed length along the split, if it has one. The first child that reports one
165
+ * decides: it takes its length when the length is above 0 and, with the gutter, less than the extent, and the other
166
+ * side fills the rest (the gutter between). Otherwise, or with a length that does not fit, the split falls back to
167
+ * its ratio, and the other child's length is not read. When both report a length with `keep`, each keeps its own,
168
+ * in order, and the room left over stays empty; if the two (and the gutter) do not fit the extent, the second runs
169
+ * past the end. A split a fixed length decides has no divider and no floor.
170
+ * - `fixedAcross(child, axis)`: the child's fixed length across the split; the child takes it, when positive and
171
+ * finite, from the start of the cross axis instead of the whole cross extent. It is not checked against the cross
172
+ * extent: a length larger than the cross extent makes a pane larger than the split's box.
173
+ * - `floor(child, axis, split)`: the child's least length along the split. The ratio is clamped so each side keeps
174
+ * its floor of the room less the gutter, within the net (5..95 %) unless either side says `net: false`; where the
175
+ * two floors cannot both fit, the ratio is 0.5. With no `floor`, the ratio is only clamped to 0..1. `giveWay` is
176
+ * the other policy of the same minimum: it floats the newest pane instead of squeezing the ratio, and a host
177
+ * chooses one for a pane.
178
+ */
179
+ interface SplitOptions<K> extends TileOptions {
180
+ readonly fixed?: (child: Split<K>, axis: "x" | "y") => FixedLength | undefined;
181
+ readonly fixedAcross?: (child: Split<K>, axis: "x" | "y") => number | undefined;
182
+ readonly floor?: (child: Split<K>, axis: "x" | "y", split: Split<K>) => MinLength;
183
+ }
184
+ /** A laid-out split: the box it divides, its axis and ratio in effect, and the strip between its two sides. */
185
+ interface SplitGeometry {
186
+ readonly rect: Rect;
187
+ readonly axis: "x" | "y";
188
+ /**
189
+ * The first side's share in effect: the middle of the gutter lies at the box's start plus its extent times this
190
+ * (for a fixed-length child, the middle of the gutter after the first side over the extent). For a master split
191
+ * it is the master area's share (clamped to 0.05..0.95) of the room less the gap, measured from the master's own
192
+ * side: with the master on the right, ratio 0.3 in 1000 with a gap of 10, the divider lies at 693..703.
193
+ */
194
+ readonly ratio: number;
195
+ /** The gutter between the two sides, as a rect inside `rect`; null where the split has none. */
196
+ readonly divider: Rect | null;
197
+ }
198
+ /** A tree laid out: every pane's rect, and the geometry of each split that has an `id`. */
199
+ interface SplitLayout<K> {
200
+ readonly panes: Map<K, Rect>;
201
+ readonly splits: Map<string, SplitGeometry>;
202
+ }
203
+ /**
204
+ * The rect of every pane of `tree` in `area`. Each split's gutter is its gap (its own `gap`, else `options.gap`) plus
205
+ * `options.handle`. A split's `ratio` is clamped to 0..1 (one that is not finite is 0.5), and the middle of the gutter
206
+ * sits at `extent * ratio`: the first side is `extent * ratio` less half the gutter, the second starts half a gutter
207
+ * past it and is the rest less half the gutter. No size is ever negative: a side too short for half the gutter is 0
208
+ * along the axis, and the second side of a split with no room may then start up to half a gutter past the rect's end.
209
+ * At ratio 0.5 both sides are equal. `options.floor`, `fixed` and `fixedAcross` (see `SplitOptions`) read the host's
210
+ * minimums and fixed lengths per split. A negative or non-finite `gap` or `handle` is 0. The area is taken as given:
211
+ * with a negative width or height every length along that axis is 0 and a pane's size across it is copied as it
212
+ * is, so pass an area with no negative side.
213
+ */
214
+ declare function layoutSplit<K>(tree: Split<K> | null, area: Rect, options?: SplitOptions<K>): Map<K, Rect>;
215
+ /**
216
+ * `layoutSplit` and the geometry of every split that has an `id` (a repeated id keeps the one laid out last: a split under another, or a later one beside it): the box it divides,
217
+ * its axis and ratio in effect (after the floors), and its divider, the gutter strip between its two sides. A split
218
+ * that lays out as master and stack is reported too, its nested splits are not.
219
+ */
220
+ declare function layoutSplitTree<K>(tree: Split<K> | null, area: Rect, options?: SplitOptions<K>): SplitLayout<K>;
221
+
222
+ /**
223
+ * Whether a drop on a pane leaves room: the rule a host asks before it offers a side of a pane as a drop place. See
224
+ * docs/placement.md, Entry: tiling.
225
+ */
226
+
227
+ /** The layout options a drop is judged under, and `min`: the least width and height, px, a pane must keep. */
228
+ interface DropOptions<K> extends SplitOptions<K> {
229
+ readonly min: number;
230
+ }
231
+ /** A drop's verdict: `ok`, or not, with its `reason`. */
232
+ interface DropVerdict {
233
+ readonly ok: boolean;
234
+ readonly reason?: string;
235
+ }
236
+ /**
237
+ * Whether dropping `dragged` on pane `at`'s `region` leaves both at least `options.min` wide and high.
238
+ *
239
+ * - The center is fine unless `dragged` is `at`, or `at` is not in the tree (nothing to swap with).
240
+ * - A side needs, in the layout of the tree without `dragged` (so `at` is measured where it will be when `dragged` is
241
+ * lifted out), a span of `at` along the split of at least `2 * min` plus the gutter (the options' gap plus handle)
242
+ * and across it of at least `min`; and in the layout of `dropOnPane(tree, dragged, at, region)` both `dragged` and
243
+ * `at` at least `min` wide and high. That check is skipped when `area` has no width or no height. A pane that is
244
+ * not in the tree counts as one being added; an `at` that is not in it (or is `dragged`) has no place to split.
245
+ */
246
+ declare function canDropOnPane<K>(tree: Split<K> | null, dragged: K, at: K, region: PaneRegion, area: Rect, options: DropOptions<K>): DropVerdict;
247
+ /** What `canSplitPane` reads: `min` (as in `DropOptions`) and the gutter's parts, `gap` and `handle`, px. */
248
+ interface SplitRoomOptions {
249
+ readonly min: number;
250
+ readonly gap?: number;
251
+ readonly handle?: number;
252
+ }
253
+ /**
254
+ * The room rule: whether a pane of rect `target` can take another on `side`, both at least `options.min` wide and
255
+ * high. Along the split (width for left and right, height for top and bottom) the pane needs `2 * min` plus the
256
+ * gutter (`gap` plus `handle`), across it `min`. The rect is the pane's as the host measures it, where it will be
257
+ * when the dragged pane is lifted out. `canDropOnPane`'s first rule, for a side.
258
+ */
259
+ declare function canSplitPane(target: Rect, side: PaneSide, options: SplitRoomOptions): DropVerdict;
260
+ /** What `dropFits` reads: the `min` (as in `DropOptions`) and the `side` of `at` the drop is on, for the reason. */
261
+ interface DropFitsOptions {
262
+ readonly min: number;
263
+ readonly side: PaneSide;
264
+ }
265
+ /**
266
+ * The fit rule: in `projected`, the rect of every pane in the layout the drop makes (the host's own projection of
267
+ * `dropOnPane`'s tree), whether `dragged` and `at` are both there and at least `options.min` wide and high. Fine
268
+ * when `area` has no width or no height (there is nothing to measure). `canDropOnPane`'s last rule.
269
+ */
270
+ declare function dropFits<K>(projected: ReadonlyMap<K, Rect>, dragged: K, at: K, area: Rect, options: DropFitsOptions): DropVerdict;
271
+
114
272
  /**
115
273
  * Drop targets for tiling, in the root `Target` shape (what `@n-uf/motion/drag` takes). Both kinds come from one
116
274
  * helper: a pane target (`pane:<id>`, its hit the pane, the place in it its drop region) for the split tree, and an
@@ -142,7 +300,7 @@ declare function paneTargets<K>(layout: ReadonlyMap<K, Rect>, options?: PaneTarg
142
300
  * `area`. Each target's hit and slot is `dragged`'s rect with it there, so the targets cover the area as the layout
143
301
  * does. Ids are `at:<index>`, unique whatever the panes are.
144
302
  */
145
- declare function orderTargets<K>(arrange: OrderLayout, panes: readonly K[], dragged: K, area: Rect, options?: TileOptions): OrderTarget<K>[];
303
+ declare function orderTargets<K>(arrange: OrderLayout, panes: readonly K[], dragged: K, area: Rect, options?: OrderOptions): OrderTarget<K>[];
146
304
 
147
305
  /**
148
306
  * The minimum-size give-way: while a pane is laid out below its minimum, the newest pane (highest `age`) floats and
@@ -154,4 +312,24 @@ declare function giveWay<K>(panes: readonly K[], layout: (kept: readonly K[]) =>
154
312
  floats: K[];
155
313
  };
156
314
 
157
- export { type OrderLayout, type OrderTarget, PANE_REGION_DEFAULTS, PANE_SIDES, type PaneCenter, type PaneRegion, type PaneRegionOptions, type PaneSide, type PaneTarget, type PaneTargetsOptions, type Split, type TileOptions, columns, dropOnPane, giveWay, grid, layoutSplit, masterStack, orderTargets, paneCenter, paneRegion, paneTargets, panesOf, removePane, splitFromOrder, splitPane, swapPanes };
315
+ /**
316
+ * The nearest pane in a direction: the rule a host moves focus by. See docs/placement.md, Entry: tiling.
317
+ */
318
+
319
+ /** A direction on the plane: `"up"` is toward smaller y. */
320
+ type Direction = "left" | "right" | "up" | "down";
321
+ /**
322
+ * The pane nearest `from` in `direction`, or null when none lies that way. A candidate's center has to be strictly on
323
+ * that side of `from`'s center. Its score is the center distance along the direction, plus a quarter of the center
324
+ * distance across it, plus 100 when it does not overlap `from` across the direction (the overlap strictly above 0);
325
+ * the lowest score wins, ties to the first in the map's order. A pane that is not in `rects` has no neighbor.
326
+ *
327
+ * The host chooses which rects to pass. One option: lay the tree out in a unit square (`{ x: 0, y: 0, width: 1,
328
+ * height: 1 }`) with the host's px floors. A floor of tens of px cannot fit in a unit square, so a split's two floors
329
+ * cannot both fit and it lays out at 0.5 whatever its ratio; fixed lengths in px along a split do not fit either, and
330
+ * the area's size and the gap drop out. Fixed lengths across a split are not checked against the cross extent, so
331
+ * they still apply in a unit square, and a host leaves `fixedAcross` out there. The rects of the real layout work too.
332
+ */
333
+ declare function neighbor<K>(rects: ReadonlyMap<K, Rect>, from: K, direction: Direction): K | null;
334
+
335
+ export { type Direction, type DropFitsOptions, type DropOptions, type DropVerdict, type FixedLength, type MasterOptions, type MinLength, type OrderLayout, type OrderOptions, type OrderTarget, PANE_REGION_DEFAULTS, PANE_SIDES, type PaneCenter, type PaneRegion, type PaneRegionOptions, type PaneSide, type PaneTarget, type PaneTargetsOptions, Rect, type Split, type SplitGeometry, type SplitLayout, type SplitOptions, type SplitRoomOptions, type TileOptions, canDropOnPane, canSplitPane, columns, dropFits, dropOnPane, giveWay, grid, layoutSplit, layoutSplitTree, masterCount, masterRatio, masterStack, neighbor, orderTargets, paneCenter, paneRegion, paneTargets, panesOf, removePane, splitFromOrder, splitPane, swapPanes };
package/dist/tiling.js CHANGED
@@ -3,19 +3,51 @@ import {
3
3
  } from "./chunk-2UVUFEBP.js";
4
4
 
5
5
  // src/tiling/order.ts
6
+ var masterRatio = (v) => v === void 0 || !Number.isFinite(v) ? 0.5 : Math.min(0.95, Math.max(0.05, v));
7
+ function masterCount(count, n) {
8
+ const c = Math.round(count ?? 1);
9
+ return Math.min(Math.max(Number.isNaN(c) ? 1 : c, 1), Math.max(n, 1));
10
+ }
11
+ function divide(items, x, y, width, height, vertical, gap, out) {
12
+ const n = items.length;
13
+ const total = gap * (n - 1);
14
+ if (vertical) {
15
+ const each = Math.max(0, (height - total) / n);
16
+ items.forEach((pane, i) => out.set(pane, { x, y: y + i * (each + gap), width, height: each }));
17
+ } else {
18
+ const each = Math.max(0, (width - total) / n);
19
+ items.forEach((pane, i) => out.set(pane, { x: x + i * (each + gap), y, width: each, height }));
20
+ }
21
+ }
6
22
  function masterStack(panes, area, options = {}) {
7
- const gap = options.gap ?? 0;
23
+ const gap = options.gap !== void 0 && Number.isFinite(options.gap) && options.gap > 0 ? options.gap : 0;
8
24
  const out = /* @__PURE__ */ new Map();
9
25
  if (panes.length === 0) return out;
10
- if (panes.length === 1) {
11
- out.set(panes[0], { ...area });
12
- return out;
26
+ const side = options.side ?? "left";
27
+ const ratio = masterRatio(options.ratio);
28
+ const count = masterCount(options.count, panes.length);
29
+ const masters = panes.slice(0, count);
30
+ const stack = panes.slice(count);
31
+ const column = side === "left" || side === "right";
32
+ if (stack.length === 0) {
33
+ divide(masters, area.x, area.y, area.width, area.height, column, gap, out);
34
+ } else if (column) {
35
+ const room = Math.max(0, area.width - gap);
36
+ const masterWidth = room * ratio;
37
+ const stackWidth = room * (1 - ratio);
38
+ const masterX = side === "left" ? area.x : area.x + stackWidth + gap;
39
+ const stackX = side === "left" ? area.x + masterWidth + gap : area.x;
40
+ divide(masters, masterX, area.y, masterWidth, area.height, true, gap, out);
41
+ divide(stack, stackX, area.y, stackWidth, area.height, true, gap, out);
42
+ } else {
43
+ const room = Math.max(0, area.height - gap);
44
+ const masterHeight = room * ratio;
45
+ const stackHeight = room * (1 - ratio);
46
+ const masterY = side === "top" ? area.y : area.y + stackHeight + gap;
47
+ const stackY = side === "top" ? area.y + masterHeight + gap : area.y;
48
+ divide(masters, area.x, masterY, area.width, masterHeight, false, gap, out);
49
+ divide(stack, area.x, stackY, area.width, stackHeight, false, gap, out);
13
50
  }
14
- const mw = (area.width - gap) * (options.ratio ?? 0.5);
15
- out.set(panes[0], { x: area.x, y: area.y, width: mw, height: area.height });
16
- const k = panes.length - 1;
17
- const sh = (area.height - gap * (k - 1)) / k;
18
- panes.slice(1).forEach((id, i) => out.set(id, { x: area.x + mw + gap, y: area.y + i * (sh + gap), width: area.width - mw - gap, height: sh }));
19
51
  return out;
20
52
  }
21
53
  function columns(panes, area, options = {}) {
@@ -45,32 +77,6 @@ function grid(panes, area, options = {}) {
45
77
  }
46
78
 
47
79
  // src/tiling/split.ts
48
- function layoutSplit(tree, area, options = {}) {
49
- const out = /* @__PURE__ */ new Map();
50
- lay(tree, area, Math.max(0, options.gap ?? 0), out);
51
- return out;
52
- }
53
- function lay(tree, area, gap, out) {
54
- if (!tree) return;
55
- if ("pane" in tree) {
56
- out.set(tree.pane, { ...area });
57
- return;
58
- }
59
- const ratio = Math.min(1, Math.max(0, tree.ratio ?? 0.5));
60
- if ((tree.axis ?? (area.width >= area.height ? "x" : "y")) === "x") {
61
- const room = Math.max(0, area.width - gap);
62
- const w = room * ratio;
63
- lay(tree.a, { x: area.x, y: area.y, width: w, height: area.height }, gap, out);
64
- const bx = room > 0 ? area.x + w + gap : area.x + Math.max(0, area.width);
65
- lay(tree.b, { x: bx, y: area.y, width: room * (1 - ratio), height: area.height }, gap, out);
66
- } else {
67
- const room = Math.max(0, area.height - gap);
68
- const h = room * ratio;
69
- lay(tree.a, { x: area.x, y: area.y, width: area.width, height: h }, gap, out);
70
- const by = room > 0 ? area.y + h + gap : area.y + Math.max(0, area.height);
71
- lay(tree.b, { x: area.x, y: by, width: area.width, height: room * (1 - ratio) }, gap, out);
72
- }
73
- }
74
80
  var panesOf = (tree) => !tree ? [] : "pane" in tree ? [tree.pane] : [...panesOf(tree.a), ...panesOf(tree.b)];
75
81
  var has = (tree, pane) => panesOf(tree).includes(pane);
76
82
  function mapPanes(tree, fn) {
@@ -115,6 +121,139 @@ function dropOnPane(tree, dragged, at, region) {
115
121
  return mapPanes(tree, (p) => p === at ? { a: { pane: dragged }, b: { pane: p } } : { pane: p });
116
122
  }
117
123
 
124
+ // src/tiling/layout.ts
125
+ var px = (v) => v !== void 0 && Number.isFinite(v) && v > 0 ? v : 0;
126
+ var unit = (v) => v === void 0 || !Number.isFinite(v) ? 0.5 : Math.min(1, Math.max(0, v));
127
+ var usable = (v) => v !== void 0 && Number.isFinite(v) && v > 0;
128
+ var NET = [0.05, 0.95];
129
+ function layoutSplit(tree, area, options = {}) {
130
+ return layoutSplitTree(tree, area, options).panes;
131
+ }
132
+ function layoutSplitTree(tree, area, options = {}) {
133
+ const ctx = { options, gap: px(options.gap), handle: px(options.handle), panes: /* @__PURE__ */ new Map(), splits: /* @__PURE__ */ new Map() };
134
+ lay(tree, area, ctx);
135
+ return { panes: ctx.panes, splits: ctx.splits };
136
+ }
137
+ function strip(box, x, mid, gutter) {
138
+ const lo = Math.max(x ? box.x : box.y, mid - gutter / 2);
139
+ const hi = Math.min(x ? box.x + box.width : box.y + box.height, mid + gutter / 2);
140
+ const length = Math.max(0, hi - lo);
141
+ return x ? { x: lo, y: box.y, width: length, height: box.height } : { x: box.x, y: lo, width: box.width, height: length };
142
+ }
143
+ function clampByMin(ratio, extent, gutter, minA, minB, net) {
144
+ const room = Math.max(extent - gutter, 1);
145
+ const boundedMin = Math.min(Math.max(minA / room, net[0]), net[1]);
146
+ const boundedMax = Math.max(Math.min(1 - minB / room, net[1]), net[0]);
147
+ return boundedMin > boundedMax ? 0.5 : Math.min(Math.max(ratio, boundedMin), boundedMax);
148
+ }
149
+ function layMaster(tree, master, area, ctx) {
150
+ const gap = tree.gap === void 0 ? ctx.gap : px(tree.gap);
151
+ const panes = panesOf(tree);
152
+ const ratio = masterRatio(tree.ratio);
153
+ const side = master.side ?? "left";
154
+ for (const [pane, r] of masterStack(panes, area, { gap, ratio, count: master.count, side })) ctx.panes.set(pane, r);
155
+ if (tree.id === void 0) return;
156
+ const x = side === "left" || side === "right";
157
+ const stacked = panes.length > masterCount(master.count, panes.length);
158
+ const room = Math.max(0, (x ? area.width : area.height) - gap);
159
+ const first = side === "left" || side === "top" ? room * ratio : room * (1 - ratio);
160
+ const mid = (x ? area.x : area.y) + first + gap / 2;
161
+ ctx.splits.set(tree.id, { rect: { ...area }, axis: x ? "x" : "y", ratio, divider: stacked ? strip(area, x, mid, gap) : null });
162
+ }
163
+ function lay(tree, area, ctx) {
164
+ if (!tree) return;
165
+ if ("pane" in tree) {
166
+ ctx.panes.set(tree.pane, { ...area });
167
+ return;
168
+ }
169
+ if (tree.master) {
170
+ layMaster(tree, tree.master, area, ctx);
171
+ return;
172
+ }
173
+ const axis = tree.axis ?? (area.width >= area.height ? "x" : "y");
174
+ const x = axis === "x";
175
+ const extent = x ? area.width : area.height;
176
+ const origin = x ? area.x : area.y;
177
+ const cross = x ? area.height : area.width;
178
+ const gutter = (tree.gap === void 0 ? ctx.gap : px(tree.gap)) + ctx.handle;
179
+ const { fixed, fixedAcross, floor } = ctx.options;
180
+ const across = (child) => {
181
+ const length = fixedAcross?.(child, axis);
182
+ return usable(length) ? length : cross;
183
+ };
184
+ const acrossA = across(tree.a);
185
+ const acrossB = across(tree.b);
186
+ const fixedA = fixed?.(tree.a, axis);
187
+ const fixedB = fixed?.(tree.b, axis);
188
+ let lengthA;
189
+ let lengthB;
190
+ let startB;
191
+ let ratio = unit(tree.ratio);
192
+ let divider = null;
193
+ const holds = fixedA && fixedB && fixedA.keep && fixedB.keep && usable(fixedA.length) && usable(fixedB.length) ? [fixedA.length, fixedB.length] : null;
194
+ const lead = fixedA ?? fixedB;
195
+ if (holds) {
196
+ [lengthA, lengthB] = holds;
197
+ startB = origin + lengthA + gutter;
198
+ ratio = extent > 0 ? (lengthA + gutter / 2) / extent : ratio;
199
+ } else if (lead && usable(lead.length) && lead.length + gutter < extent) {
200
+ const rest = Math.max(0, extent - lead.length - gutter);
201
+ [lengthA, lengthB] = lead === fixedA ? [lead.length, rest] : [rest, lead.length];
202
+ startB = origin + lengthA + gutter;
203
+ ratio = (lengthA + gutter / 2) / extent;
204
+ } else {
205
+ if (floor) {
206
+ const a = floor(tree.a, axis, tree);
207
+ const b = floor(tree.b, axis, tree);
208
+ const net = a.net === false || b.net === false ? [0, 1] : NET;
209
+ ratio = clampByMin(Number.isFinite(tree.ratio ?? 0.5) ? tree.ratio ?? 0.5 : 0.5, extent, gutter, px(a.min), px(b.min), net);
210
+ }
211
+ lengthA = Math.max(0, extent * ratio - gutter / 2);
212
+ lengthB = Math.max(0, extent * (1 - ratio) - gutter / 2);
213
+ startB = origin + extent * ratio + gutter / 2;
214
+ divider = strip(area, x, origin + extent * ratio, gutter);
215
+ }
216
+ if (tree.id !== void 0) ctx.splits.set(tree.id, { rect: { ...area }, axis, ratio, divider });
217
+ if (x) {
218
+ lay(tree.a, { x: area.x, y: area.y, width: lengthA, height: acrossA }, ctx);
219
+ lay(tree.b, { x: startB, y: area.y, width: lengthB, height: acrossB }, ctx);
220
+ } else {
221
+ lay(tree.a, { x: area.x, y: area.y, width: acrossA, height: lengthA }, ctx);
222
+ lay(tree.b, { x: area.x, y: startB, width: acrossB, height: lengthB }, ctx);
223
+ }
224
+ }
225
+
226
+ // src/tiling/drop.ts
227
+ var px2 = (v) => v !== void 0 && Number.isFinite(v) && v > 0 ? v : 0;
228
+ function canDropOnPane(tree, dragged, at, region, area, options) {
229
+ if (region === "center") {
230
+ if (dragged === at) return { ok: false, reason: "center swap blocked: a pane dropped on itself" };
231
+ return panesOf(tree).includes(at) ? { ok: true } : { ok: false, reason: "center rejected: no pane to swap" };
232
+ }
233
+ const target = layoutSplit(removePane(tree, dragged), area, options).get(at);
234
+ if (!target) return { ok: false, reason: `${region} rejected: no pane to split` };
235
+ const room = canSplitPane(target, region, options);
236
+ if (!room.ok) return room;
237
+ return dropFits(layoutSplit(dropOnPane(tree, dragged, at, region), area, options), dragged, at, area, { min: options.min, side: region });
238
+ }
239
+ function canSplitPane(target, side, options) {
240
+ const min = px2(options.min);
241
+ const gutter = px2(options.gap) + px2(options.handle);
242
+ const horizontal = side === "left" || side === "right";
243
+ const along = horizontal ? target.width : target.height;
244
+ const across = horizontal ? target.height : target.width;
245
+ const need = min * 2 + gutter;
246
+ if (along < need) return { ok: false, reason: `${side} rejected: axis span ${along.toFixed(1)}px < min ${need.toFixed(1)}px (2 panes + gutter ${gutter}px)` };
247
+ if (across < min) return { ok: false, reason: `${side} rejected: cross span ${across.toFixed(1)}px < min ${min.toFixed(1)}px` };
248
+ return { ok: true };
249
+ }
250
+ function dropFits(projected, dragged, at, area, options) {
251
+ if (!(area.width > 0 && area.height > 0)) return { ok: true };
252
+ const min = px2(options.min);
253
+ const fits = (r) => !!r && r.width >= min && r.height >= min;
254
+ return fits(projected.get(dragged)) && fits(projected.get(at)) ? { ok: true } : { ok: false, reason: `${options.side} rejected: projected footprint of the two panes violates min ${min}px` };
255
+ }
256
+
118
257
  // src/tiling/regions.ts
119
258
  var PANE_SIDES = ["top", "right", "bottom", "left"];
120
259
  var PANE_REGION_DEFAULTS = { center: 0.34, centerMin: 24, hysteresis: 6, pixelRatio: 1 };
@@ -162,7 +301,7 @@ function toward(p, region, by, c) {
162
301
  return { x: p.x + dx / len * by, y: p.y + dy / len * by };
163
302
  }
164
303
  function paneRegion(pane, point, previous = null, options = {}) {
165
- const c = paneCenter(pane, options);
304
+ const c = options.paneCenter ?? paneCenter(pane, options);
166
305
  const p = { x: point.x - pane.x, y: point.y - pane.y };
167
306
  const raw = rawRegion(p, c);
168
307
  const by = Math.max(0, options.hysteresis ?? PANE_REGION_DEFAULTS.hysteresis);
@@ -197,15 +336,50 @@ function giveWay(panes, layout, min, age) {
197
336
  floats.push(newest);
198
337
  }
199
338
  }
339
+
340
+ // src/tiling/neighbor.ts
341
+ function neighbor(rects, from, direction) {
342
+ const origin = rects.get(from);
343
+ if (!origin) return null;
344
+ const horizontal = direction === "left" || direction === "right";
345
+ const center = (r, x) => x ? (r.x + (r.x + r.width)) / 2 : (r.y + (r.y + r.height)) / 2;
346
+ const reach = (r, x) => x ? [r.x, r.x + r.width] : [r.y, r.y + r.height];
347
+ const fromAlong = center(origin, horizontal);
348
+ const fromAcross = center(origin, !horizontal);
349
+ const [originStart, originEnd] = reach(origin, !horizontal);
350
+ const sign = direction === "left" || direction === "up" ? -1 : 1;
351
+ let best = null;
352
+ let bestScore = Number.POSITIVE_INFINITY;
353
+ for (const [key, r] of rects) {
354
+ if (key === from) continue;
355
+ const along = center(r, horizontal);
356
+ if (sign < 0 ? along >= fromAlong : along <= fromAlong) continue;
357
+ const [start, end] = reach(r, !horizontal);
358
+ const overlap = Math.max(0, Math.min(originEnd, end) - Math.max(originStart, start));
359
+ const score = Math.abs(along - fromAlong) + Math.abs(center(r, !horizontal) - fromAcross) * 0.25 + (overlap > 0 ? 0 : 100);
360
+ if (score < bestScore) {
361
+ bestScore = score;
362
+ best = key;
363
+ }
364
+ }
365
+ return best;
366
+ }
200
367
  export {
201
368
  PANE_REGION_DEFAULTS,
202
369
  PANE_SIDES,
370
+ canDropOnPane,
371
+ canSplitPane,
203
372
  columns,
373
+ dropFits,
204
374
  dropOnPane,
205
375
  giveWay,
206
376
  grid,
207
377
  layoutSplit,
378
+ layoutSplitTree,
379
+ masterCount,
380
+ masterRatio,
208
381
  masterStack,
382
+ neighbor,
209
383
  orderTargets,
210
384
  paneCenter,
211
385
  paneRegion,