@n-uf/placement 26.12.5 → 26.12.7

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.js CHANGED
@@ -192,6 +192,9 @@ function decideSplit(extent, gutter, ratio, a, b) {
192
192
  // src/tiling/layout.ts
193
193
  var px2 = (v) => v !== void 0 && Number.isFinite(v) && v > 0 ? v : 0;
194
194
  var usable = (v) => v !== void 0 && Number.isFinite(v) && v > 0;
195
+ function refuseFold(options, name) {
196
+ if ("fold" in options) throw new Error(`${name} does not fold a split tree: use layoutSplitTree`);
197
+ }
195
198
  var slot = (maps, node) => {
196
199
  let m = maps.get(node);
197
200
  if (!m) maps.set(node, m = /* @__PURE__ */ new Map());
@@ -268,21 +271,23 @@ var Needs = class {
268
271
  }
269
272
  };
270
273
  function splitNeed(tree, axis, options = {}) {
274
+ refuseFold(options, "splitNeed");
271
275
  if (!tree || !options.floor) return 0;
272
276
  return new Needs(options, px2(options.gap), px2(options.handle)).of(tree, axis, null);
273
277
  }
274
278
  function layoutSplit(tree, area, options = {}) {
279
+ refuseFold(options, "layoutSplit");
275
280
  const gap = px2(options.gap);
276
281
  const handle = px2(options.handle);
277
- const ctx = { options, gap, handle, panes: /* @__PURE__ */ new Map(), splits: null, needs: new Needs(options, gap, handle) };
282
+ const ctx = { options, gap, handle, panes: /* @__PURE__ */ new Map(), splits: null, ratios: null, needs: new Needs(options, gap, handle) };
278
283
  lay(tree, area, ctx, false);
279
284
  return ctx.panes;
280
285
  }
281
- function layoutSplitTree(tree, area, options = {}) {
286
+ function laySplitTree(tree, area, options, ratios = null) {
282
287
  const splits = /* @__PURE__ */ new Map();
283
288
  const gap = px2(options.gap);
284
289
  const handle = px2(options.handle);
285
- const ctx = { options, gap, handle, panes: /* @__PURE__ */ new Map(), splits, needs: new Needs(options, gap, handle) };
290
+ const ctx = { options, gap, handle, panes: /* @__PURE__ */ new Map(), splits, ratios, needs: new Needs(options, gap, handle) };
286
291
  lay(tree, area, ctx, false);
287
292
  return { panes: ctx.panes, splits };
288
293
  }
@@ -336,6 +341,7 @@ function lay(tree, area, ctx, owned) {
336
341
  const a = sideOf(tree.a);
337
342
  const b = sideOf(tree.b);
338
343
  const d = decideSplit(extent, gutter, tree.ratio, a, b);
344
+ ctx.ratios?.set(tree, d.ratio);
339
345
  const startB = d.step === "no-room" ? origin + Math.max(0, extent) : d.step === "ratio" ? origin + extent * d.ratio + gutter / 2 : origin + d.a + gutter;
340
346
  if (tree.id !== void 0 && ctx.splits !== null) {
341
347
  const divider = d.divider ? strip(area, x, origin + extent * d.ratio, gutter) : null;
@@ -356,6 +362,7 @@ function lay(tree, area, ctx, owned) {
356
362
  // src/tiling/drop.ts
357
363
  var px3 = (v) => v !== void 0 && Number.isFinite(v) && v > 0 ? v : 0;
358
364
  function canDropOnPane(tree, dragged, at, region, area, options) {
365
+ refuseFold(options, "canDropOnPane");
359
366
  if (region === "center") {
360
367
  if (dragged === at) return { ok: false, reason: "center swap blocked: a pane dropped on itself" };
361
368
  return panesOf(tree).includes(at) ? { ok: true } : { ok: false, reason: "center rejected: no pane to swap" };
@@ -367,6 +374,7 @@ function canDropOnPane(tree, dragged, at, region, area, options) {
367
374
  return dropFits(layoutSplit(dropOnPane(tree, dragged, at, region), area, options), dragged, at, area, { min: options.min, side: region });
368
375
  }
369
376
  function canSplitPane(target, side, options) {
377
+ refuseFold(options, "canSplitPane");
370
378
  const min = px3(options.min);
371
379
  const gutter = px3(options.gap) + px3(options.handle);
372
380
  const horizontal = side === "left" || side === "right";
@@ -378,12 +386,567 @@ function canSplitPane(target, side, options) {
378
386
  return { ok: true };
379
387
  }
380
388
  function dropFits(projected, dragged, at, area, options) {
389
+ refuseFold(options, "dropFits");
381
390
  if (!(area.width > 0 && area.height > 0)) return { ok: true };
382
391
  const min = px3(options.min);
383
392
  const fits = (r) => !!r && r.width >= min - EPS && r.height >= min - EPS;
384
393
  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` };
385
394
  }
386
395
 
396
+ // src/tiling/fold-steps.ts
397
+ var FIT2 = 1e-6;
398
+ var NO_RAILS = /* @__PURE__ */ new Set();
399
+ var px4 = (v) => v !== void 0 && Number.isFinite(v) && v > 0 ? v : 0;
400
+ var otherAxis = (axis) => axis === "x" ? "y" : "x";
401
+ var isPane = (node) => "pane" in node || node.master !== void 0;
402
+ var sideEdges = (axis) => axis === "x" ? ["left", "right"] : ["top", "bottom"];
403
+ var railOn = (rails, edge) => rails.has(edge) ? /* @__PURE__ */ new Set([edge]) : NO_RAILS;
404
+ function stepFrontier(steps) {
405
+ const sorted = [...steps].sort((p, q) => p.length - q.length || q.panes - p.panes);
406
+ const out = [];
407
+ for (const step of sorted) {
408
+ const last = out[out.length - 1];
409
+ if (last === void 0 || step.panes > last.panes) out.push(step);
410
+ }
411
+ return out;
412
+ }
413
+ function leastLengthFor(steps, panes) {
414
+ for (const step of steps) if (step.panes >= panes) return step.length;
415
+ return Infinity;
416
+ }
417
+ function leastKeeping(steps, length) {
418
+ let panes = -1;
419
+ for (const step of steps) if (step.length <= length + FIT2) panes = Math.max(panes, step.panes);
420
+ if (panes < 0) return length;
421
+ let least = length;
422
+ for (const step of steps) if (step.panes >= panes) least = Math.min(least, step.length);
423
+ return least;
424
+ }
425
+ function bestKeptPair(a, b, extent, gutter, middle) {
426
+ const room = Math.max(0, extent - gutter) + FIT2;
427
+ let best = null;
428
+ for (const x of a) {
429
+ for (const y of b) {
430
+ if (x.length + y.length > room && x.length + y.length > 0) continue;
431
+ const high = extent - gutter - y.length;
432
+ const move = middle < x.length ? x.length - middle : middle > high ? middle - high : 0;
433
+ const panes = x.panes + y.panes;
434
+ if (best === null || panes > best.panes || panes === best.panes && move < best.move - FIT2) best = { panes, move, a: x.length, b: y.length };
435
+ }
436
+ }
437
+ return best === null ? null : { panes: best.panes, a: best.a, b: best.b };
438
+ }
439
+ function foldedPanesAt(folded, extent) {
440
+ let panes = -1;
441
+ for (const step of folded) if (step.length <= extent + FIT2) panes = Math.max(panes, step.panes);
442
+ return panes;
443
+ }
444
+ function countRule(a, b, folded, extent, gutter, middle) {
445
+ const kept = bestKeptPair(a, b, extent, gutter, middle);
446
+ if (kept === null) return "fold";
447
+ return foldedPanesAt(folded, extent) > kept.panes ? "fold" : kept;
448
+ }
449
+ var MEMOS = /* @__PURE__ */ new WeakMap();
450
+ var ABSENT = {};
451
+ var IDS = /* @__PURE__ */ new WeakMap();
452
+ var nextId = 0;
453
+ var idOf = (o) => {
454
+ if (o === void 0) return "-";
455
+ let id = IDS.get(o);
456
+ if (id === void 0) IDS.set(o, id = nextId++);
457
+ return String(id);
458
+ };
459
+ var ACROSS = /* @__PURE__ */ new WeakMap();
460
+ function memoOf(options, fold) {
461
+ let level = MEMOS;
462
+ for (const answer of [fold.floor, fold.weight, options.fixed]) {
463
+ let next = level.get(answer ?? ABSENT);
464
+ if (next === void 0) level.set(answer ?? ABSENT, next = /* @__PURE__ */ new WeakMap());
465
+ level = next;
466
+ }
467
+ let byLengths = level.get(options.fixedAcross ?? ABSENT);
468
+ if (byLengths === void 0) level.set(options.fixedAcross ?? ABSENT, byLengths = /* @__PURE__ */ new Map());
469
+ const key = `${px4(options.gap)}|${px4(options.handle)}|${px4(fold.spine)}`;
470
+ let memo = byLengths.get(key);
471
+ if (memo === void 0) byLengths.set(key, memo = { steps: /* @__PURE__ */ new WeakMap(), pinned: /* @__PURE__ */ new WeakMap() });
472
+ return memo;
473
+ }
474
+ var FoldSteps = class {
475
+ options;
476
+ fold;
477
+ memo;
478
+ parents = /* @__PURE__ */ new Map();
479
+ constructor(tree, options, fold) {
480
+ this.options = options;
481
+ this.fold = fold;
482
+ this.memo = memoOf(options, fold);
483
+ const walk = (node) => {
484
+ if (isPane(node) || "pane" in node) return;
485
+ if (node.axis === void 0) throw new Error("a folding split tree needs an axis on every split outside a master split");
486
+ for (const child of [node.a, node.b]) {
487
+ this.parents.set(child, node);
488
+ walk(child);
489
+ }
490
+ };
491
+ walk(tree);
492
+ }
493
+ /** A split's gutter: its own gap, else the layout's, plus the handle. */
494
+ gutter(split) {
495
+ return (split.gap === void 0 ? px4(this.options.gap) : px4(split.gap)) + px4(this.options.handle);
496
+ }
497
+ /** What a fold takes from its open side where its spine joins no rail: the spine and the split's gap. */
498
+ spineCost(split) {
499
+ return px4(this.fold.spine) + (split.gap === void 0 ? px4(this.options.gap) : px4(split.gap));
500
+ }
501
+ /** A pane's fold floor along `axis` at `boundary`: the host's, and at least 1 px. */
502
+ floor(node, axis, boundary) {
503
+ const v = this.fold.floor(node, axis, boundary);
504
+ return v > 1 ? v : 1;
505
+ }
506
+ /** What a pane counts: its weight, a master split the sum of its panes'. */
507
+ weight(node) {
508
+ const of2 = (pane) => this.fold.weight?.(pane) ?? 1;
509
+ if ("pane" in node) return of2(node.pane);
510
+ return panesOf(node).reduce((n, pane) => n + of2(pane), 0);
511
+ }
512
+ /**
513
+ * A node's pin along `axis`, as the fold counts it: its fixed length along its parent split (`fixed`) where that split
514
+ * runs along `axis`, else its length across it (`fixedAcross`); only at 1 px or more. The root has none.
515
+ */
516
+ pin(node, axis) {
517
+ const parent = this.parents.get(node);
518
+ if (parent === void 0) return null;
519
+ const length = parent.axis === axis ? this.options.fixed?.(node, axis)?.length : this.options.fixedAcross?.(node, parent.axis);
520
+ return length !== void 0 && Number.isFinite(length) && length >= 1 ? length : null;
521
+ }
522
+ /**
523
+ * The steps of `node` along `axis`, measured at `boundary` (the nearest split above it along `axis`; null: none is
524
+ * counted). `rails`: the edges where the node's box sits against an open rail, where a spine joins that rail and costs
525
+ * nothing. `form`: see `FoldForm`. `cross`: the most the judged box measures across `axis` (a node inside pinned across
526
+ * shorter than it doesn't span the box, so no rail on that length reaches it). `counted`: the length the node is
527
+ * counted at across `axis`, read where a split across is judged at it; Infinity counts every split across kept.
528
+ *
529
+ * - A pane: one step, its floor and its weight.
530
+ * - A split pinned along `axis`: its own steps up to the pin; one pinned shorter than its least step, that step.
531
+ * - A split across `axis`: in the form its own count rule gives it at `counted`, inside which splits back along
532
+ * `axis` are counted kept (depth one). Kept: its sides side by side, each counted across at its share. Folded: for
533
+ * each count up to the smaller of its sides' most, the larger of their least lengths, each side counted across at
534
+ * the length less the spine where it joins no rail.
535
+ * - A split along `axis`, kept: each pair of its sides' steps, the lengths plus its gutter, the panes added; a side
536
+ * keeps a rail only on the edge it touches, and is counted across at its pin where that is shorter. Folded: for each
537
+ * count from 0 up to the smaller of its sides' most, the larger of the two sides' least lengths, each side with its
538
+ * spine's cost (none where the spine's edge is a rail).
539
+ */
540
+ steps(node, axis, boundary, rails, form = "any", cross2 = Infinity, counted = Infinity) {
541
+ const acrossPin = form === "any" && rails.size > 0 ? this.pin(node, otherAxis(axis)) : null;
542
+ const reach = acrossPin !== null && acrossPin < cross2 - FIT2 ? NO_RAILS : rails;
543
+ const at = this.holdsSplitAcross(node, axis) ? Math.max(0, counted) : Infinity;
544
+ const pane = isPane(node);
545
+ const bound = !pane && node.axis === axis ? String(cross2) : "";
546
+ const parent = this.parents.get(node)?.axis ?? "";
547
+ const key = pane ? `${axis}|${idOf(boundary ?? void 0)}` : `${axis}|${idOf(boundary ?? void 0)}|${[...reach].sort().join(",")}|${form}|${bound}|${at}|${parent}`;
548
+ let byKey = this.memo.steps.get(node);
549
+ const known = byKey?.get(key);
550
+ if (known !== void 0) return known;
551
+ const steps = this.build(node, axis, boundary, reach, form, cross2, at);
552
+ if (byKey === void 0) this.memo.steps.set(node, byKey = /* @__PURE__ */ new Map());
553
+ byKey.set(key, steps);
554
+ return steps;
555
+ }
556
+ build(node, axis, boundary, rails, form, cross2, counted) {
557
+ if (isPane(node) || "pane" in node) return [{ length: this.floor(node, axis, boundary), panes: this.weight(node) }];
558
+ const pin = this.pin(node, axis);
559
+ if (pin !== null && form === "any") {
560
+ const within2 = this.steps(node, axis, boundary, rails, "open-side", cross2, counted);
561
+ const fitting = within2.filter((step) => step.length <= pin);
562
+ return fitting.length > 0 ? fitting : within2.slice(0, 1);
563
+ }
564
+ if (node.axis !== axis) return this.acrossSteps(node, axis, boundary, rails, counted);
565
+ const [aEdge, bEdge] = sideEdges(axis);
566
+ const out = [];
567
+ if (form !== "folded") {
568
+ const acrossOf = (side) => {
569
+ const p = Number.isFinite(counted) ? this.pin(side, otherAxis(axis)) : null;
570
+ return p === null ? counted : Math.min(p, counted);
571
+ };
572
+ const a = this.steps(node.a, axis, node, railOn(rails, aEdge), "any", cross2, acrossOf(node.a));
573
+ const b = this.steps(node.b, axis, node, railOn(rails, bEdge), "any", cross2, acrossOf(node.b));
574
+ const gutter = this.gutter(node);
575
+ for (const x of a) for (const y of b) out.push({ length: x.length + gutter + y.length, panes: x.panes + y.panes });
576
+ }
577
+ const shown = (side) => {
578
+ const openEdge = side === "a" ? aEdge : bEdge;
579
+ const spineEdge = side === "a" ? bEdge : aEdge;
580
+ const cost = rails.has(spineEdge) ? 0 : this.spineCost(node);
581
+ const openRails = /* @__PURE__ */ new Set([...railOn(rails, openEdge), spineEdge]);
582
+ return this.steps(node[side], axis, node, openRails, "open-side", cross2, counted).map((step) => ({ length: step.length + cost, panes: step.panes }));
583
+ };
584
+ return stepFrontier([...out, ...foldedSteps(shown("a"), shown("b"))]);
585
+ }
586
+ /**
587
+ * The steps along `axis` of `node`, a split across it, in the form its own count rule gives it at `length` (its
588
+ * length along its own axis). Its own steps are counted with no length across them: depth one.
589
+ */
590
+ acrossSteps(node, axis, boundary, rails, length) {
591
+ if (!Number.isFinite(length)) return besideSteps(this.steps(node.a, axis, boundary, NO_RAILS), this.steps(node.b, axis, boundary, NO_RAILS));
592
+ const own = otherAxis(axis);
593
+ const [aEdge, bEdge] = sideEdges(own);
594
+ const ownRails = /* @__PURE__ */ new Set([...railOn(rails, aEdge), ...railOn(rails, bEdge)]);
595
+ const aOwn = this.steps(node.a, own, node, railOn(rails, aEdge));
596
+ const bOwn = this.steps(node.b, own, node, railOn(rails, bEdge));
597
+ const gutter = this.gutter(node);
598
+ const allOpen = (aOwn[aOwn.length - 1]?.length ?? 0) + gutter + (bOwn[bOwn.length - 1]?.length ?? 0);
599
+ if (length + FIT2 >= allOpen && !this.pinnedInside(node, own)) return this.acrossSteps(node, axis, boundary, rails, Infinity);
600
+ const judged = countRule(aOwn, bOwn, this.steps(node, own, null, ownRails, "folded"), length, gutter, length / 2 - gutter / 2);
601
+ const sideRails = (side, edges) => this.pin(side, axis) === null ? edges : NO_RAILS;
602
+ if (judged !== "fold") {
603
+ const room = Math.max(0, length - gutter);
604
+ const aLength = Math.max(0, room - leastKeeping(bOwn, judged.b));
605
+ const bLength = Math.max(0, room - leastKeeping(aOwn, aLength));
606
+ return besideSteps(
607
+ this.steps(node.a, axis, boundary, sideRails(node.a, railOn(rails, aEdge)), "any", Infinity, aLength),
608
+ this.steps(node.b, axis, boundary, sideRails(node.b, railOn(rails, bEdge)), "any", Infinity, bLength)
609
+ );
610
+ }
611
+ const shown = (side) => {
612
+ const openEdge = side === "a" ? aEdge : bEdge;
613
+ const spineEdge = side === "a" ? bEdge : aEdge;
614
+ const open = Math.max(0, length - (rails.has(spineEdge) ? 0 : this.spineCost(node)));
615
+ const openRails = /* @__PURE__ */ new Set([...railOn(rails, openEdge), spineEdge]);
616
+ return this.steps(node[side], axis, boundary, sideRails(node[side], openRails), "open-side", Infinity, open);
617
+ };
618
+ return stepFrontier(foldedSteps(shown("a"), shown("b")));
619
+ }
620
+ /** Whether `node`'s subtree, `node` included, holds a split (not a master split) across `axis`. */
621
+ holdsSplitAcross(node, axis) {
622
+ if (isPane(node) || "pane" in node) return false;
623
+ let byAxis = ACROSS.get(node);
624
+ const known = byAxis?.get(axis);
625
+ if (known !== void 0) return known;
626
+ const value = node.axis !== axis || this.holdsSplitAcross(node.a, axis) || this.holdsSplitAcross(node.b, axis);
627
+ if (byAxis === void 0) ACROSS.set(node, byAxis = /* @__PURE__ */ new Map());
628
+ byAxis.set(axis, value);
629
+ return value;
630
+ }
631
+ /** Whether a node strictly inside `node` is pinned along `axis`. */
632
+ pinnedInside(node, axis) {
633
+ if ("pane" in node) return false;
634
+ let byAxis = this.memo.pinned.get(node);
635
+ const known = byAxis?.get(axis);
636
+ if (known !== void 0) return known;
637
+ const pinnedIn = (child) => this.pin(child, axis) !== null || this.pinnedInside(child, axis);
638
+ const value = pinnedIn(node.a) || pinnedIn(node.b);
639
+ if (byAxis === void 0) this.memo.pinned.set(node, byAxis = /* @__PURE__ */ new Map());
640
+ byAxis.set(axis, value);
641
+ return value;
642
+ }
643
+ };
644
+ function foldedSteps(a, b) {
645
+ const most = Math.min(a[a.length - 1]?.panes ?? 0, b[b.length - 1]?.panes ?? 0);
646
+ const out = [];
647
+ for (let panes = 0; panes <= most; panes += 1) out.push({ length: Math.max(leastLengthFor(a, panes), leastLengthFor(b, panes)), panes });
648
+ return out;
649
+ }
650
+ var besideSteps = (a, b) => stepFrontier(a.flatMap((x) => b.map((y) => ({ length: Math.max(x.length, y.length), panes: x.panes + y.panes }))));
651
+
652
+ // src/tiling/fold-plan.ts
653
+ var NO_RAILS2 = {};
654
+ var NONE = /* @__PURE__ */ new Set();
655
+ var EDGES = ["left", "right", "top", "bottom"];
656
+ var SEGMENT_GAP = 2;
657
+ var RATIO_EPSILON = 1e-9;
658
+ var px5 = (v) => v !== void 0 && Number.isFinite(v) && v > 0 ? v : 0;
659
+ var usable2 = (v) => v !== void 0 && Number.isFinite(v) && v > 0;
660
+ var otherAxis2 = (axis) => axis === "x" ? "y" : "x";
661
+ var lengthOf = (rect, axis) => axis === "x" ? rect.width : rect.height;
662
+ var isPane2 = (node) => "pane" in node || node.master !== void 0;
663
+ var sideEdges2 = (axis) => axis === "x" ? ["left", "right"] : ["top", "bottom"];
664
+ var nearPx = (a, b) => Math.abs(a - b) <= 0.5;
665
+ function joinsRail(rail, rect, edge) {
666
+ switch (edge) {
667
+ case "left":
668
+ return nearPx(rect.x, rail.boundary) && nearPx(rect.y, rail.rect.y) && nearPx(rect.height, rail.rect.height);
669
+ case "right":
670
+ return nearPx(rect.x + rect.width, rail.boundary) && nearPx(rect.y, rail.rect.y) && nearPx(rect.height, rail.rect.height);
671
+ case "top":
672
+ return nearPx(rect.y, rail.boundary) && nearPx(rect.x, rail.rect.x) && nearPx(rect.width, rail.rect.width);
673
+ case "bottom":
674
+ return nearPx(rect.y + rect.height, rail.boundary) && nearPx(rect.x, rail.rect.x) && nearPx(rect.width, rail.rect.width);
675
+ }
676
+ }
677
+ function railBoundary(openRect, edge) {
678
+ switch (edge) {
679
+ case "left":
680
+ return openRect.x;
681
+ case "right":
682
+ return openRect.x + openRect.width;
683
+ case "top":
684
+ return openRect.y;
685
+ case "bottom":
686
+ return openRect.y + openRect.height;
687
+ }
688
+ }
689
+ function foldBoxes(rect, axis, edge, spine, gap) {
690
+ if (axis === "x") {
691
+ const spineWidth = Math.max(0, Math.min(spine, rect.width));
692
+ const openWidth = Math.max(0, rect.width - spine - gap);
693
+ return edge === "left" ? { spineRect: { x: rect.x, y: rect.y, width: spineWidth, height: rect.height }, openRect: { x: rect.x + rect.width - openWidth, y: rect.y, width: openWidth, height: rect.height } } : { spineRect: { x: rect.x + rect.width - spineWidth, y: rect.y, width: spineWidth, height: rect.height }, openRect: { x: rect.x, y: rect.y, width: openWidth, height: rect.height } };
694
+ }
695
+ const spineHeight = Math.max(0, Math.min(spine, rect.height));
696
+ const openHeight = Math.max(0, rect.height - spine - gap);
697
+ return edge === "top" ? { spineRect: { x: rect.x, y: rect.y, width: rect.width, height: spineHeight }, openRect: { x: rect.x, y: rect.y + rect.height - openHeight, width: rect.width, height: openHeight } } : { spineRect: { x: rect.x, y: rect.y + rect.height - spineHeight, width: rect.width, height: spineHeight }, openRect: { x: rect.x, y: rect.y, width: rect.width, height: openHeight } };
698
+ }
699
+ function segments(rail, axis, weights) {
700
+ const along = axis === "x";
701
+ const length = along ? rail.height : rail.width;
702
+ const gap = weights.length > 1 ? Math.min(SEGMENT_GAP, length / (weights.length * 4)) : 0;
703
+ const total = weights.reduce((sum, weight) => sum + weight, 0);
704
+ const free = Math.max(0, length - gap * (weights.length - 1));
705
+ const out = [];
706
+ let offset = 0;
707
+ weights.forEach((weight, index) => {
708
+ const part = index === weights.length - 1 ? Math.max(0, length - offset) : free * weight / total;
709
+ out.push(along ? { x: rail.x, y: rail.y + offset, width: rail.width, height: part } : { x: rail.x + offset, y: rail.y, width: part, height: rail.height });
710
+ offset += part + gap;
711
+ });
712
+ return out;
713
+ }
714
+ var wholeRoom = (rect) => ({ counted: { x: rect.width, y: rect.height }, most: { x: rect.width, y: rect.height } });
715
+ var withLength = (lengths, axis, value) => axis === "x" ? { x: value, y: lengths.y } : { x: lengths.x, y: value };
716
+ function foldLayout(tree, area, options, fold) {
717
+ const plainRatios = /* @__PURE__ */ new Map();
718
+ const base = laySplitTree(tree, area, options, plainRatios);
719
+ if (!tree) return { ...base, spines: /* @__PURE__ */ new Map(), folds: [] };
720
+ const steps = new FoldSteps(tree, options, fold);
721
+ const order = fold.order;
722
+ const targetOrder = fold.targetOrder ?? order;
723
+ const weight = (pane) => fold.weight?.(pane) ?? 1;
724
+ const spine = px5(fold.spine);
725
+ const gapOf = (split) => split.gap === void 0 ? px5(options.gap) : px5(split.gap);
726
+ const panes = /* @__PURE__ */ new Map();
727
+ const splits = /* @__PURE__ */ new Map();
728
+ const folds = [];
729
+ const drawnAt = { moved: false };
730
+ const pinAcross = (child, split) => {
731
+ const length = options.fixedAcross?.(child, split.axis);
732
+ return usable2(length) ? length : null;
733
+ };
734
+ const sideAcross = (side, split, across) => {
735
+ const pin = pinAcross(side, split);
736
+ return pin === null ? across : Math.min(pin, across);
737
+ };
738
+ const layOutKept = (node, rect, a, b) => {
739
+ const axis = node.axis;
740
+ const sideOf = (child) => {
741
+ const fixed = options.fixed?.(child, axis);
742
+ const kept = !!fixed?.keep && usable2(fixed.length);
743
+ return { fixed, net: kept || (options.floor ? options.floor(child, axis, node).net !== false : true) };
744
+ };
745
+ const sides = { a: sideOf(node.a), b: sideOf(node.b) };
746
+ const one = {
747
+ id: "s",
748
+ a: { pane: 0 },
749
+ b: { pane: 1 },
750
+ axis,
751
+ ...node.ratio !== void 0 ? { ratio: node.ratio } : {},
752
+ ...node.gap !== void 0 ? { gap: node.gap } : {}
753
+ };
754
+ const second = (child) => "pane" in child && child.pane === 1;
755
+ const laid = laySplitTree(one, rect, {
756
+ gap: options.gap,
757
+ handle: options.handle,
758
+ floor: (child) => second(child) ? { min: b, net: sides.b.net } : { min: a, net: sides.a.net },
759
+ fixed: (child) => second(child) ? sides.b.fixed : sides.a.fixed,
760
+ fixedAcross: (child) => options.fixedAcross?.(second(child) ? node.b : node.a, axis)
761
+ });
762
+ const empty = () => ({ x: rect.x, y: rect.y, width: 0, height: 0 });
763
+ return { geometry: laid.splits.get("s"), a: laid.panes.get(0) ?? empty(), b: laid.panes.get(1) ?? empty() };
764
+ };
765
+ const judge = (split, rect, crossMax, rails) => {
766
+ const axis = split.axis;
767
+ const extent = Math.max(0, lengthOf(rect, axis));
768
+ const gutter = steps.gutter(split);
769
+ const [aEdge, bEdge] = sideEdges2(axis);
770
+ const onEdge = (edge) => rails.has(edge) ? /* @__PURE__ */ new Set([edge]) : NONE;
771
+ const cross2 = Math.max(0, crossMax);
772
+ const across = Math.max(0, lengthOf(rect, otherAxis2(axis)));
773
+ const a = steps.steps(split.a, axis, split, onEdge(aEdge), "any", cross2, sideAcross(split.a, split, across));
774
+ const b = steps.steps(split.b, axis, split, onEdge(bEdge), "any", cross2, sideAcross(split.b, split, across));
775
+ const folded = steps.steps(split, axis, null, rails, "folded", cross2, across);
776
+ const middle = extent / 2 - gutter / 2;
777
+ return countRule(a, b, folded, extent, gutter, middle);
778
+ };
779
+ const openSideOf = (split) => {
780
+ const a = new Set(panesOf(split.a));
781
+ const b = new Set(panesOf(split.b));
782
+ for (const pane of order) {
783
+ if (a.has(pane)) return "a";
784
+ if (b.has(pane)) return "b";
785
+ }
786
+ return "a";
787
+ };
788
+ const openLayout = (node, side, rect, room, rails, foldIndex) => {
789
+ const axis = node.axis;
790
+ const [aEdge, bEdge] = sideEdges2(axis);
791
+ const edge = side === "a" ? bEdge : aEdge;
792
+ const gap = gapOf(node);
793
+ const rail = rails[edge];
794
+ const joined = rail !== void 0 && joinsRail(rail, rect, edge);
795
+ const own = joined ? null : foldBoxes(rect, axis, edge, spine, gap);
796
+ const openRect = own === null ? { ...rect } : own.openRect;
797
+ const railRect = own === null ? { ...rail.rect } : own.spineRect;
798
+ const railOf = own === null ? rail.fold : foldIndex;
799
+ const openRails = own === null ? rails : { ...rails, [edge]: { fold: foldIndex, rect: railRect, boundary: railBoundary(openRect, edge) } };
800
+ const taken = own === null ? 0 : spine + gap;
801
+ const openRoom = {
802
+ counted: withLength(room.counted, axis, Math.max(0, room.counted[axis] - taken)),
803
+ most: withLength(room.most, axis, Math.max(0, room.most[axis] - taken))
804
+ };
805
+ return { edge, openRect, railRect, railOf, openRails, openRoom, taken };
806
+ };
807
+ const decideFold = (node, rect, room, rails) => {
808
+ const sideOf = (side) => {
809
+ const layout = openLayout(node, side, rect, room, rails, -1);
810
+ return { layout, decided: decide(node[side], layout.openRect, layout.openRoom, layout.openRails) };
811
+ };
812
+ return { kind: "fold", node, room, open: openSideOf(node), sides: { a: sideOf("a"), b: sideOf("b") } };
813
+ };
814
+ const decide = (node, rect, room, rails) => {
815
+ if (isPane2(node) || "pane" in node) return { kind: "pane", node };
816
+ const here = new Set(
817
+ EDGES.filter((edge) => {
818
+ const rail = rails[edge];
819
+ return rail !== void 0 && joinsRail(rail, rect, edge);
820
+ })
821
+ );
822
+ const judged = { x: Math.max(0, room.counted.x), y: Math.max(0, room.counted.y) };
823
+ const along = node.axis;
824
+ const across = otherAxis2(along);
825
+ const crossMax = Math.max(lengthOf(rect, across), room.most[across]);
826
+ const verdict = judge(node, { x: rect.x, y: rect.y, width: judged.x, height: judged.y }, crossMax, here);
827
+ if (verdict === "fold") return decideFold(node, rect, { counted: judged, most: room.most }, rails);
828
+ const laid = layOutKept(node, rect, verdict.a, verdict.b);
829
+ const gutter = steps.gutter(node);
830
+ const sideRoom = (child, need, otherNeed) => {
831
+ const pin = pinAcross(child, node);
832
+ const counted = withLength(withLength(judged, along, need), across, pin === null ? judged[across] : Math.min(pin, judged[across]));
833
+ return { counted, most: withLength(room.most, along, Math.max(0, room.most[along] - gutter - otherNeed)) };
834
+ };
835
+ const roomLength = Math.max(0, judged[along] - gutter);
836
+ const bEdge = along === "x" ? "right" : "bottom";
837
+ const bRails = here.has(bEdge) ? /* @__PURE__ */ new Set([bEdge]) : NONE;
838
+ const bSteps = steps.steps(node.b, along, node, bRails, "any", Math.max(0, crossMax), sideAcross(node.b, node, judged[across]));
839
+ const bLeast = leastKeeping(bSteps, verdict.b);
840
+ const a = decide(node.a, laid.a, sideRoom(node.a, Math.max(0, roomLength - bLeast), bLeast), rails);
841
+ const aNeed = chosenNeed(a, along, node);
842
+ const bRect = layOutKept(node, rect, aNeed, bLeast).b;
843
+ const b = decide(node.b, bRect, sideRoom(node.b, Math.max(0, roomLength - aNeed), aNeed), rails);
844
+ return { kind: "kept", node, pair: verdict, a, b };
845
+ };
846
+ const chosen = /* @__PURE__ */ new WeakMap();
847
+ const chosenNeed = (decided, axis, boundary) => {
848
+ if (decided.kind === "pane") return steps.floor(decided.node, axis, boundary);
849
+ let byBoundary = chosen.get(decided);
850
+ const known = byBoundary?.get(boundary)?.[axis];
851
+ if (known !== void 0) return known;
852
+ const node = decided.node;
853
+ const alongIt = node.axis === axis;
854
+ let need = 0;
855
+ if (decided.kind === "kept") {
856
+ need = alongIt ? chosenNeed(decided.a, axis, node) + steps.gutter(node) + chosenNeed(decided.b, axis, node) : Math.max(chosenNeed(decided.a, axis, boundary), chosenNeed(decided.b, axis, boundary));
857
+ } else {
858
+ for (const side of ["a", "b"]) {
859
+ const { layout, decided: open } = decided.sides[side];
860
+ need = Math.max(need, alongIt ? chosenNeed(open, axis, node) + layout.taken : chosenNeed(open, axis, boundary));
861
+ }
862
+ }
863
+ if (byBoundary === void 0) chosen.set(decided, byBoundary = /* @__PURE__ */ new Map());
864
+ byBoundary.set(boundary, { ...byBoundary.get(boundary), [axis]: need });
865
+ return need;
866
+ };
867
+ const draw = (decided, rect, enclosing, rails) => {
868
+ if (decided.kind === "fold") {
869
+ drawFold(decided, rect, enclosing, rails);
870
+ return;
871
+ }
872
+ if (decided.kind === "pane") {
873
+ const node2 = decided.node;
874
+ if ("pane" in node2) {
875
+ panes.set(node2.pane, { ...rect });
876
+ return;
877
+ }
878
+ const laid2 = laySplitTree(node2, rect, options);
879
+ for (const [pane, r] of laid2.panes) panes.set(pane, r);
880
+ for (const [id, geometry] of laid2.splits) splits.set(id, geometry);
881
+ return;
882
+ }
883
+ const node = decided.node;
884
+ const axis = node.axis;
885
+ const laid = layOutKept(node, rect, chosenNeed(decided.a, axis, node), chosenNeed(decided.b, axis, node));
886
+ if (node.id !== void 0) splits.set(node.id, laid.geometry);
887
+ if (!drawnAt.moved) {
888
+ const plain = plainRatios.get(node);
889
+ if (plain === void 0 || !(Math.abs(plain - laid.geometry.ratio) <= RATIO_EPSILON)) drawnAt.moved = true;
890
+ }
891
+ draw(decided.a, laid.a, enclosing, rails);
892
+ draw(decided.b, laid.b, enclosing, rails);
893
+ };
894
+ const drawFold = (decided, rect, enclosing, rails) => {
895
+ const node = decided.node;
896
+ const index = folds.length;
897
+ const layout = openLayout(node, decided.open, rect, decided.room, rails, index);
898
+ const folded = panesOf(node[decided.open === "a" ? "b" : "a"]);
899
+ const shown = folded.filter((pane) => weight(pane) > 0);
900
+ const target = targetOrder.find((pane) => shown.includes(pane)) ?? shown[0] ?? null;
901
+ folds.push({
902
+ id: node.id,
903
+ axis: node.axis,
904
+ open: decided.open,
905
+ edge: layout.edge,
906
+ rect: { ...rect },
907
+ openRect: layout.openRect,
908
+ spine: { ...layout.railRect },
909
+ rail: layout.railRect,
910
+ railOf: layout.railOf,
911
+ parent: enclosing.length > 0 ? enclosing[enclosing.length - 1] : null,
912
+ panes: folded,
913
+ target
914
+ });
915
+ draw(decided.sides[decided.open].decided, layout.openRect, [...enclosing, index], layout.openRails);
916
+ };
917
+ draw(decide(tree, area, wholeRoom(area), NO_RAILS2), area, [], NO_RAILS2);
918
+ const onRail = /* @__PURE__ */ new Map();
919
+ folds.forEach((each, index) => onRail.set(each.railOf, [...onRail.get(each.railOf) ?? [], index]));
920
+ const drawn = [...folds];
921
+ for (const members of onRail.values()) {
922
+ if (members.length < 2) continue;
923
+ const head = folds[members[0]];
924
+ const parts = segments(head.rail, head.axis, members.map((i) => Math.max(1, folds[i].panes.filter((pane) => weight(pane) > 0).length)));
925
+ members.forEach((i, n) => drawn[i] = { ...folds[i], spine: parts[n] });
926
+ }
927
+ const spines = /* @__PURE__ */ new Map();
928
+ for (const each of drawn) {
929
+ const shown = each.panes.filter((pane) => weight(pane) > 0);
930
+ const parts = segments(each.spine, each.axis, shown.map(() => 1));
931
+ shown.forEach((pane, n) => spines.set(pane, parts[n]));
932
+ }
933
+ if (drawn.length === 0 && !drawnAt.moved) {
934
+ const out = /* @__PURE__ */ new Map();
935
+ for (const [id, geometry] of base.splits) {
936
+ const mine = splits.get(id);
937
+ out.set(id, mine === void 0 ? geometry : { ...geometry, needs: mine.needs });
938
+ }
939
+ return { panes: base.panes, splits: out, spines, folds: drawn };
940
+ }
941
+ return { panes, splits, spines, folds: drawn };
942
+ }
943
+
944
+ // src/tiling/tree-layout.ts
945
+ function layoutSplitTree(tree, area, options = {}) {
946
+ if (options.fold) return foldLayout(tree, area, options, options.fold);
947
+ return { ...laySplitTree(tree, area, options), spines: /* @__PURE__ */ new Map(), folds: [] };
948
+ }
949
+
387
950
  // src/tiling/regions.ts
388
951
  var PANE_SIDES = ["top", "right", "bottom", "left"];
389
952
  var PANE_REGION_DEFAULTS = { center: 0.34, centerMin: 24, hysteresis: 6, pixelRatio: 1 };
@@ -738,7 +1301,7 @@ function createTilingHit(ctx, basis, options = {}) {
738
1301
  homeLikeAll ??= [...byId.values()].filter((p) => p !== homePlace && homeLike(p));
739
1302
  return [...homePlace.pieces, ...homeLikeAll.flatMap((p) => p.pieces)];
740
1303
  };
741
- const capped = (px5, extent) => Math.min(px5, cap * extent);
1304
+ const capped = (px7, extent) => Math.min(px7, cap * extent);
742
1305
  const visibleMemo = /* @__PURE__ */ new Map();
743
1306
  const visibleOf = (piece) => {
744
1307
  let v = visibleMemo.get(piece);
@@ -844,8 +1407,9 @@ function giveWay(panes, layout, min, age) {
844
1407
 
845
1408
  // src/tiling/drag.ts
846
1409
  var sameRect = (a, b) => Math.abs(a.x - b.x) <= EPS && Math.abs(a.y - b.y) <= EPS && Math.abs(a.width - b.width) <= EPS && Math.abs(a.height - b.height) <= EPS;
847
- var px4 = (v) => v !== void 0 && Number.isFinite(v) && v > 0 ? v : 0;
1410
+ var px6 = (v) => v !== void 0 && Number.isFinite(v) && v > 0 ? v : 0;
848
1411
  function tilingDrag(tree, subject, options) {
1412
+ refuseFold(options, "tilingDrag");
849
1413
  const { ops, area } = options;
850
1414
  const normalize = (t) => ops.normalize ? ops.normalize(t) : t;
851
1415
  const lay2 = (t) => {
@@ -892,8 +1456,8 @@ function tilingDrag(tree, subject, options) {
892
1456
  const r = base.layout.get(at);
893
1457
  if (canSplitPane(r, side, options).ok) return null;
894
1458
  const along = side === "left" || side === "right";
895
- const gutter = px4(options.gap) + px4(options.handle);
896
- const min = px4(options.min);
1459
+ const gutter = px6(options.gap) + px6(options.handle);
1460
+ const min = px6(options.min);
897
1461
  const span = along ? r.width : r.height;
898
1462
  return span < min * 2 + gutter ? { code: "room-along", need: min * 2 + gutter, have: span } : { code: "room-across", need: min, have: along ? r.height : r.width };
899
1463
  };
@@ -906,7 +1470,7 @@ function tilingDrag(tree, subject, options) {
906
1470
  const own = drawnAs(laid.tree, subjectKey);
907
1471
  const partner = drawnAs(laid.tree, held.at);
908
1472
  if (!dropFits(laid.layout, own, partner, area, { min: options.min, side: held.kind === "beside" ? held.side : "left" }).ok) {
909
- const small = (r) => !r || r.width < px4(options.min) || r.height < px4(options.min);
1473
+ const small = (r) => !r || r.width < px6(options.min) || r.height < px6(options.min);
910
1474
  return refused(held, { code: "fit", pane: small(laid.layout.get(own)) ? own : partner });
911
1475
  }
912
1476
  return { ...laid, held, refusal: null, landing: laid.layout.get(own) ?? null, moved: movedIn(laid) };
@@ -1124,6 +1688,7 @@ function propertyTally(properties, limit) {
1124
1688
  return { counts, failures, checkFor };
1125
1689
  }
1126
1690
  function tilingConformance(trees, options, limit = 20) {
1691
+ refuseFold(options, "tilingConformance");
1127
1692
  const { ops, area } = options;
1128
1693
  const { counts, failures, checkFor } = propertyTally(TILING_PROPERTIES, limit);
1129
1694
  const norm = (t) => ops.normalize ? ops.normalize(t) : t;
@@ -1286,6 +1851,7 @@ function inQuad(q, p) {
1286
1851
  return s.every((v) => v > 1e-9) || s.every((v) => v < -1e-9);
1287
1852
  }
1288
1853
  function tilingHitConformance(trees, options, hitOptions = {}, limit = 20) {
1854
+ refuseFold(options, "tilingHitConformance");
1289
1855
  const { ops, area } = options;
1290
1856
  const { counts, failures, checkFor } = propertyTally(TILING_HIT_PROPERTIES, limit);
1291
1857
  const reported = { swapsBeside: 0, insertsMoveOthers: 0, insertsMoveOthersBeside: 0 };