@n-uf/placement 26.12.6 → 26.12.8

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,10 +386,632 @@ 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);
392
+ const folded = options.folded;
383
393
  const fits = (r) => !!r && r.width >= min - EPS && r.height >= min - EPS;
384
- 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` };
394
+ return fits(projected.get(dragged)) && (fits(projected.get(at)) || folded?.has(at) === true) ? { ok: true } : { ok: false, reason: `${options.side} rejected: projected footprint of the two panes violates min ${min}px` };
395
+ }
396
+
397
+ // src/tiling/fold-steps.ts
398
+ var FIT2 = 1e-6;
399
+ var NO_RAILS = /* @__PURE__ */ new Set();
400
+ var px4 = (v) => v !== void 0 && Number.isFinite(v) && v > 0 ? v : 0;
401
+ var otherAxis = (axis) => axis === "x" ? "y" : "x";
402
+ var isPane = (node) => "pane" in node || node.master !== void 0;
403
+ var sideEdges = (axis) => axis === "x" ? ["left", "right"] : ["top", "bottom"];
404
+ var railOn = (rails, edge) => rails.has(edge) ? /* @__PURE__ */ new Set([edge]) : NO_RAILS;
405
+ function stepFrontier(steps) {
406
+ const sorted = [...steps].sort((p, q) => p.length - q.length || q.panes - p.panes);
407
+ const out = [];
408
+ for (const step of sorted) {
409
+ const last = out[out.length - 1];
410
+ if (last === void 0 || step.panes > last.panes) out.push(step);
411
+ }
412
+ return out;
413
+ }
414
+ function leastLengthFor(steps, panes) {
415
+ for (const step of steps) if (step.panes >= panes) return step.length;
416
+ return Infinity;
417
+ }
418
+ function leastKeeping(steps, length) {
419
+ let panes = -1;
420
+ for (const step of steps) if (step.length <= length + FIT2) panes = Math.max(panes, step.panes);
421
+ if (panes < 0) return length;
422
+ let least = length;
423
+ for (const step of steps) if (step.panes >= panes) least = Math.min(least, step.length);
424
+ return least;
425
+ }
426
+ function bestKeptPair(a, b, extent, gutter, middle) {
427
+ const room = Math.max(0, extent - gutter) + FIT2;
428
+ let best = null;
429
+ for (const x of a) {
430
+ for (const y of b) {
431
+ if (x.length + y.length > room && x.length + y.length > 0) continue;
432
+ const high = extent - gutter - y.length;
433
+ const move = middle < x.length ? x.length - middle : middle > high ? middle - high : 0;
434
+ const panes = x.panes + y.panes;
435
+ if (best === null || panes > best.panes || panes === best.panes && move < best.move - FIT2) best = { panes, move, a: x.length, b: y.length };
436
+ }
437
+ }
438
+ return best === null ? null : { panes: best.panes, a: best.a, b: best.b };
439
+ }
440
+ function foldedPanesAt(folded, extent) {
441
+ let panes = -1;
442
+ for (const step of folded) if (step.length <= extent + FIT2) panes = Math.max(panes, step.panes);
443
+ return panes;
444
+ }
445
+ function countRule(a, b, folded, extent, gutter, middle) {
446
+ const kept = bestKeptPair(a, b, extent, gutter, middle);
447
+ if (kept === null) return "fold";
448
+ return foldedPanesAt(folded, extent) > kept.panes ? "fold" : kept;
449
+ }
450
+ var MEMOS = /* @__PURE__ */ new WeakMap();
451
+ var ABSENT = {};
452
+ var IDS = /* @__PURE__ */ new WeakMap();
453
+ var nextId = 0;
454
+ var idOf = (o) => {
455
+ if (o === void 0) return "-";
456
+ let id = IDS.get(o);
457
+ if (id === void 0) IDS.set(o, id = nextId++);
458
+ return String(id);
459
+ };
460
+ var ACROSS = /* @__PURE__ */ new WeakMap();
461
+ function memoOf(options, fold) {
462
+ let level = MEMOS;
463
+ for (const answer of [fold.floor, fold.weight, options.fixed]) {
464
+ let next = level.get(answer ?? ABSENT);
465
+ if (next === void 0) level.set(answer ?? ABSENT, next = /* @__PURE__ */ new WeakMap());
466
+ level = next;
467
+ }
468
+ let byLengths = level.get(options.fixedAcross ?? ABSENT);
469
+ if (byLengths === void 0) level.set(options.fixedAcross ?? ABSENT, byLengths = /* @__PURE__ */ new Map());
470
+ const key = `${px4(options.gap)}|${px4(options.handle)}|${px4(fold.spine)}`;
471
+ let memo = byLengths.get(key);
472
+ if (memo === void 0) byLengths.set(key, memo = { steps: /* @__PURE__ */ new WeakMap(), pinned: /* @__PURE__ */ new WeakMap() });
473
+ return memo;
474
+ }
475
+ var FoldSteps = class {
476
+ options;
477
+ fold;
478
+ memo;
479
+ parents = /* @__PURE__ */ new Map();
480
+ constructor(tree, options, fold) {
481
+ this.options = options;
482
+ this.fold = fold;
483
+ this.memo = memoOf(options, fold);
484
+ const walk = (node) => {
485
+ if (isPane(node) || "pane" in node) return;
486
+ if (node.axis === void 0) throw new Error("a folding split tree needs an axis on every split outside a master split");
487
+ for (const child of [node.a, node.b]) {
488
+ this.parents.set(child, node);
489
+ walk(child);
490
+ }
491
+ };
492
+ walk(tree);
493
+ }
494
+ /** A split's gutter: its own gap, else the layout's, plus the handle. */
495
+ gutter(split) {
496
+ return (split.gap === void 0 ? px4(this.options.gap) : px4(split.gap)) + px4(this.options.handle);
497
+ }
498
+ /** What a fold takes from its open side where its spine joins no rail: the spine and the split's gap. */
499
+ spineCost(split) {
500
+ return px4(this.fold.spine) + (split.gap === void 0 ? px4(this.options.gap) : px4(split.gap));
501
+ }
502
+ /** A pane's fold floor along `axis` at `boundary`: the host's, and at least 1 px. */
503
+ floor(node, axis, boundary) {
504
+ const v = this.fold.floor(node, axis, boundary);
505
+ return v > 1 ? v : 1;
506
+ }
507
+ /** What a pane counts: its weight, a master split the sum of its panes'. */
508
+ weight(node) {
509
+ const of2 = (pane) => this.fold.weight?.(pane) ?? 1;
510
+ if ("pane" in node) return of2(node.pane);
511
+ return panesOf(node).reduce((n, pane) => n + of2(pane), 0);
512
+ }
513
+ /**
514
+ * A node's pin along `axis`, as the fold counts it: its fixed length along its parent split (`fixed`) where that split
515
+ * runs along `axis`, else its length across it (`fixedAcross`); only at 1 px or more. The root has none.
516
+ */
517
+ pin(node, axis) {
518
+ const parent = this.parents.get(node);
519
+ if (parent === void 0) return null;
520
+ const length = parent.axis === axis ? this.options.fixed?.(node, axis)?.length : this.options.fixedAcross?.(node, parent.axis);
521
+ return length !== void 0 && Number.isFinite(length) && length >= 1 ? length : null;
522
+ }
523
+ /**
524
+ * The steps of `node` along `axis`, measured at `boundary` (the nearest split above it along `axis`; null: none is
525
+ * counted). `rails`: the edges where the node's box sits against an open rail, where a spine joins that rail and costs
526
+ * nothing. `form`: see `FoldForm`. `cross`: the most the judged box measures across `axis` (a node inside pinned across
527
+ * shorter than it doesn't span the box, so no rail on that length reaches it). `counted`: the length the node is
528
+ * counted at across `axis`, read where a split across is judged at it; Infinity counts every split across kept.
529
+ *
530
+ * - A pane: one step, its floor and its weight.
531
+ * - A split pinned along `axis`: its own steps up to the pin; one pinned shorter than its least step, that step.
532
+ * - A split across `axis`: in the form its own count rule gives it at `counted`, inside which splits back along
533
+ * `axis` are counted kept (depth one). Kept: its sides side by side, each counted across at its share. Folded: for
534
+ * each count up to the smaller of its sides' most, the larger of their least lengths, each side counted across at
535
+ * the length less the spine where it joins no rail.
536
+ * - A split along `axis`, kept: each pair of its sides' steps, the lengths plus its gutter, the panes added; a side
537
+ * keeps a rail only on the edge it touches, and is counted across at its pin where that is shorter. Folded: for each
538
+ * count from 0 up to the smaller of its sides' most, the larger of the two sides' least lengths, each side with its
539
+ * spine's cost (none where the spine's edge is a rail).
540
+ */
541
+ steps(node, axis, boundary, rails, form = "any", cross2 = Infinity, counted = Infinity) {
542
+ const acrossPin = form === "any" && rails.size > 0 ? this.pin(node, otherAxis(axis)) : null;
543
+ const reach = acrossPin !== null && acrossPin < cross2 - FIT2 ? NO_RAILS : rails;
544
+ const at = this.holdsSplitAcross(node, axis) ? Math.max(0, counted) : Infinity;
545
+ const pane = isPane(node);
546
+ const bound = !pane && node.axis === axis ? String(cross2) : "";
547
+ const parent = this.parents.get(node)?.axis ?? "";
548
+ const key = pane ? `${axis}|${idOf(boundary ?? void 0)}` : `${axis}|${idOf(boundary ?? void 0)}|${[...reach].sort().join(",")}|${form}|${bound}|${at}|${parent}`;
549
+ let byKey = this.memo.steps.get(node);
550
+ const known = byKey?.get(key);
551
+ if (known !== void 0) return known;
552
+ const steps = this.build(node, axis, boundary, reach, form, cross2, at);
553
+ if (byKey === void 0) this.memo.steps.set(node, byKey = /* @__PURE__ */ new Map());
554
+ byKey.set(key, steps);
555
+ return steps;
556
+ }
557
+ build(node, axis, boundary, rails, form, cross2, counted) {
558
+ if (isPane(node) || "pane" in node) return [{ length: this.floor(node, axis, boundary), panes: this.weight(node) }];
559
+ const pin = this.pin(node, axis);
560
+ if (pin !== null && form === "any") {
561
+ const within2 = this.steps(node, axis, boundary, rails, "open-side", cross2, counted);
562
+ const fitting = within2.filter((step) => step.length <= pin);
563
+ return fitting.length > 0 ? fitting : within2.slice(0, 1);
564
+ }
565
+ if (node.axis !== axis) return this.acrossSteps(node, axis, boundary, rails, counted);
566
+ const [aEdge, bEdge] = sideEdges(axis);
567
+ const out = [];
568
+ if (form !== "folded") {
569
+ const acrossOf = (side) => {
570
+ const p = Number.isFinite(counted) ? this.pin(side, otherAxis(axis)) : null;
571
+ return p === null ? counted : Math.min(p, counted);
572
+ };
573
+ const a = this.steps(node.a, axis, node, railOn(rails, aEdge), "any", cross2, acrossOf(node.a));
574
+ const b = this.steps(node.b, axis, node, railOn(rails, bEdge), "any", cross2, acrossOf(node.b));
575
+ const gutter = this.gutter(node);
576
+ for (const x of a) for (const y of b) out.push({ length: x.length + gutter + y.length, panes: x.panes + y.panes });
577
+ }
578
+ const shown = (side) => {
579
+ const openEdge = side === "a" ? aEdge : bEdge;
580
+ const spineEdge = side === "a" ? bEdge : aEdge;
581
+ const cost = rails.has(spineEdge) ? 0 : this.spineCost(node);
582
+ const openRails = /* @__PURE__ */ new Set([...railOn(rails, openEdge), spineEdge]);
583
+ return this.steps(node[side], axis, node, openRails, "open-side", cross2, counted).map((step) => ({ length: step.length + cost, panes: step.panes }));
584
+ };
585
+ return stepFrontier([...out, ...foldedSteps(shown("a"), shown("b"))]);
586
+ }
587
+ /**
588
+ * The steps along `axis` of `node`, a split across it, in the form its own count rule gives it at `length` (its
589
+ * length along its own axis). Its own steps are counted with no length across them: depth one.
590
+ */
591
+ acrossSteps(node, axis, boundary, rails, length) {
592
+ if (!Number.isFinite(length)) return besideSteps(this.steps(node.a, axis, boundary, NO_RAILS), this.steps(node.b, axis, boundary, NO_RAILS));
593
+ const own = otherAxis(axis);
594
+ const [aEdge, bEdge] = sideEdges(own);
595
+ const ownRails = /* @__PURE__ */ new Set([...railOn(rails, aEdge), ...railOn(rails, bEdge)]);
596
+ const aOwn = this.steps(node.a, own, node, railOn(rails, aEdge));
597
+ const bOwn = this.steps(node.b, own, node, railOn(rails, bEdge));
598
+ const gutter = this.gutter(node);
599
+ const allOpen = (aOwn[aOwn.length - 1]?.length ?? 0) + gutter + (bOwn[bOwn.length - 1]?.length ?? 0);
600
+ if (length + FIT2 >= allOpen && !this.pinnedInside(node, own)) return this.acrossSteps(node, axis, boundary, rails, Infinity);
601
+ const judged = countRule(aOwn, bOwn, this.steps(node, own, null, ownRails, "folded"), length, gutter, length / 2 - gutter / 2);
602
+ const sideRails = (side, edges) => this.pin(side, axis) === null ? edges : NO_RAILS;
603
+ if (judged !== "fold") {
604
+ const room = Math.max(0, length - gutter);
605
+ const aLength = Math.max(0, room - leastKeeping(bOwn, judged.b));
606
+ const bLength = Math.max(0, room - leastKeeping(aOwn, aLength));
607
+ return besideSteps(
608
+ this.steps(node.a, axis, boundary, sideRails(node.a, railOn(rails, aEdge)), "any", Infinity, aLength),
609
+ this.steps(node.b, axis, boundary, sideRails(node.b, railOn(rails, bEdge)), "any", Infinity, bLength)
610
+ );
611
+ }
612
+ const shown = (side) => {
613
+ const openEdge = side === "a" ? aEdge : bEdge;
614
+ const spineEdge = side === "a" ? bEdge : aEdge;
615
+ const open = Math.max(0, length - (rails.has(spineEdge) ? 0 : this.spineCost(node)));
616
+ const openRails = /* @__PURE__ */ new Set([...railOn(rails, openEdge), spineEdge]);
617
+ return this.steps(node[side], axis, boundary, sideRails(node[side], openRails), "open-side", Infinity, open);
618
+ };
619
+ return stepFrontier(foldedSteps(shown("a"), shown("b")));
620
+ }
621
+ /** Whether `node`'s subtree, `node` included, holds a split (not a master split) across `axis`. */
622
+ holdsSplitAcross(node, axis) {
623
+ if (isPane(node) || "pane" in node) return false;
624
+ let byAxis = ACROSS.get(node);
625
+ const known = byAxis?.get(axis);
626
+ if (known !== void 0) return known;
627
+ const value = node.axis !== axis || this.holdsSplitAcross(node.a, axis) || this.holdsSplitAcross(node.b, axis);
628
+ if (byAxis === void 0) ACROSS.set(node, byAxis = /* @__PURE__ */ new Map());
629
+ byAxis.set(axis, value);
630
+ return value;
631
+ }
632
+ /** Whether a node strictly inside `node` is pinned along `axis`. */
633
+ pinnedInside(node, axis) {
634
+ if ("pane" in node) return false;
635
+ let byAxis = this.memo.pinned.get(node);
636
+ const known = byAxis?.get(axis);
637
+ if (known !== void 0) return known;
638
+ const pinnedIn = (child) => this.pin(child, axis) !== null || this.pinnedInside(child, axis);
639
+ const value = pinnedIn(node.a) || pinnedIn(node.b);
640
+ if (byAxis === void 0) this.memo.pinned.set(node, byAxis = /* @__PURE__ */ new Map());
641
+ byAxis.set(axis, value);
642
+ return value;
643
+ }
644
+ };
645
+ function foldedSteps(a, b) {
646
+ const most = Math.min(a[a.length - 1]?.panes ?? 0, b[b.length - 1]?.panes ?? 0);
647
+ const out = [];
648
+ for (let panes = 0; panes <= most; panes += 1) out.push({ length: Math.max(leastLengthFor(a, panes), leastLengthFor(b, panes)), panes });
649
+ return out;
650
+ }
651
+ var besideSteps = (a, b) => stepFrontier(a.flatMap((x) => b.map((y) => ({ length: Math.max(x.length, y.length), panes: x.panes + y.panes }))));
652
+
653
+ // src/tiling/fold-plan.ts
654
+ var NO_RAILS2 = {};
655
+ var NONE = /* @__PURE__ */ new Set();
656
+ var EDGES = ["left", "right", "top", "bottom"];
657
+ var SEGMENT_GAP = 2;
658
+ var RATIO_EPSILON = 1e-9;
659
+ var FLOOR_EPSILON = 1e-9;
660
+ var px5 = (v) => v !== void 0 && Number.isFinite(v) && v > 0 ? v : 0;
661
+ var usable2 = (v) => v !== void 0 && Number.isFinite(v) && v > 0;
662
+ var otherAxis2 = (axis) => axis === "x" ? "y" : "x";
663
+ var lengthOf = (rect, axis) => axis === "x" ? rect.width : rect.height;
664
+ var isPane2 = (node) => "pane" in node || node.master !== void 0;
665
+ var sideEdges2 = (axis) => axis === "x" ? ["left", "right"] : ["top", "bottom"];
666
+ var nearPx = (a, b) => Math.abs(a - b) <= 0.5;
667
+ function joinsRail(rail, rect, edge) {
668
+ switch (edge) {
669
+ case "left":
670
+ return nearPx(rect.x, rail.boundary) && nearPx(rect.y, rail.rect.y) && nearPx(rect.height, rail.rect.height);
671
+ case "right":
672
+ return nearPx(rect.x + rect.width, rail.boundary) && nearPx(rect.y, rail.rect.y) && nearPx(rect.height, rail.rect.height);
673
+ case "top":
674
+ return nearPx(rect.y, rail.boundary) && nearPx(rect.x, rail.rect.x) && nearPx(rect.width, rail.rect.width);
675
+ case "bottom":
676
+ return nearPx(rect.y + rect.height, rail.boundary) && nearPx(rect.x, rail.rect.x) && nearPx(rect.width, rail.rect.width);
677
+ }
678
+ }
679
+ function railBoundary(openRect, edge) {
680
+ switch (edge) {
681
+ case "left":
682
+ return openRect.x;
683
+ case "right":
684
+ return openRect.x + openRect.width;
685
+ case "top":
686
+ return openRect.y;
687
+ case "bottom":
688
+ return openRect.y + openRect.height;
689
+ }
690
+ }
691
+ function foldBoxes(rect, axis, edge, spine, gap) {
692
+ if (axis === "x") {
693
+ const spineWidth = Math.max(0, Math.min(spine, rect.width));
694
+ const openWidth = Math.max(0, rect.width - spine - gap);
695
+ 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 } };
696
+ }
697
+ const spineHeight = Math.max(0, Math.min(spine, rect.height));
698
+ const openHeight = Math.max(0, rect.height - spine - gap);
699
+ 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 } };
700
+ }
701
+ function segments(rail, axis, weights) {
702
+ const along = axis === "x";
703
+ const length = along ? rail.height : rail.width;
704
+ const gap = weights.length > 1 ? Math.min(SEGMENT_GAP, length / (weights.length * 4)) : 0;
705
+ const total = weights.reduce((sum, weight) => sum + weight, 0);
706
+ const free = Math.max(0, length - gap * (weights.length - 1));
707
+ const out = [];
708
+ let offset = 0;
709
+ weights.forEach((weight, index) => {
710
+ const part = index === weights.length - 1 ? Math.max(0, length - offset) : free * weight / total;
711
+ 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 });
712
+ offset += part + gap;
713
+ });
714
+ return out;
715
+ }
716
+ var wholeRoom = (rect) => ({ counted: { x: rect.width, y: rect.height }, most: { x: rect.width, y: rect.height } });
717
+ var withLength = (lengths, axis, value) => axis === "x" ? { x: value, y: lengths.y } : { x: lengths.x, y: value };
718
+ function foldLayout(tree, area, options, fold) {
719
+ const plainRatios = /* @__PURE__ */ new Map();
720
+ const base = laySplitTree(tree, area, options, plainRatios);
721
+ if (!tree) return { ...base, spines: /* @__PURE__ */ new Map(), folds: [] };
722
+ const steps = new FoldSteps(tree, options, fold);
723
+ const order = fold.order;
724
+ const targetOrder = fold.targetOrder ?? order;
725
+ const weight = (pane) => fold.weight?.(pane) ?? 1;
726
+ const spine = px5(fold.spine);
727
+ const gapOf = (split) => split.gap === void 0 ? px5(options.gap) : px5(split.gap);
728
+ const panes = /* @__PURE__ */ new Map();
729
+ const splits = /* @__PURE__ */ new Map();
730
+ const folds = [];
731
+ const drawnAt = { moved: false };
732
+ const pinAcross = (child, split) => {
733
+ const length = options.fixedAcross?.(child, split.axis);
734
+ return usable2(length) ? length : null;
735
+ };
736
+ const sideAcross = (side, split, across) => {
737
+ const pin = pinAcross(side, split);
738
+ return pin === null ? across : Math.min(pin, across);
739
+ };
740
+ const layOutKept = (node, rect, a, b) => {
741
+ const axis = node.axis;
742
+ const sideOf = (child) => {
743
+ const fixed = options.fixed?.(child, axis);
744
+ const kept = !!fixed?.keep && usable2(fixed.length);
745
+ return { fixed, net: kept || (options.floor ? options.floor(child, axis, node).net !== false : true) };
746
+ };
747
+ const sides = { a: sideOf(node.a), b: sideOf(node.b) };
748
+ const one = {
749
+ id: "s",
750
+ a: { pane: 0 },
751
+ b: { pane: 1 },
752
+ axis,
753
+ ...node.ratio !== void 0 ? { ratio: node.ratio } : {},
754
+ ...node.gap !== void 0 ? { gap: node.gap } : {}
755
+ };
756
+ const second = (child) => "pane" in child && child.pane === 1;
757
+ const laid = laySplitTree(one, rect, {
758
+ gap: options.gap,
759
+ handle: options.handle,
760
+ floor: (child) => second(child) ? { min: b, net: sides.b.net } : { min: a, net: sides.a.net },
761
+ fixed: (child) => second(child) ? sides.b.fixed : sides.a.fixed,
762
+ fixedAcross: (child) => options.fixedAcross?.(second(child) ? node.b : node.a, axis)
763
+ });
764
+ const empty = () => ({ x: rect.x, y: rect.y, width: 0, height: 0 });
765
+ return { geometry: laid.splits.get("s"), a: laid.panes.get(0) ?? empty(), b: laid.panes.get(1) ?? empty() };
766
+ };
767
+ const judge = (split, rect, crossMax, rails) => {
768
+ const axis = split.axis;
769
+ const extent = Math.max(0, lengthOf(rect, axis));
770
+ const gutter = steps.gutter(split);
771
+ const [aEdge, bEdge] = sideEdges2(axis);
772
+ const onEdge = (edge) => rails.has(edge) ? /* @__PURE__ */ new Set([edge]) : NONE;
773
+ const cross2 = Math.max(0, crossMax);
774
+ const across = Math.max(0, lengthOf(rect, otherAxis2(axis)));
775
+ const a = steps.steps(split.a, axis, split, onEdge(aEdge), "any", cross2, sideAcross(split.a, split, across));
776
+ const b = steps.steps(split.b, axis, split, onEdge(bEdge), "any", cross2, sideAcross(split.b, split, across));
777
+ const folded = steps.steps(split, axis, null, rails, "folded", cross2, across);
778
+ const middle = extent / 2 - gutter / 2;
779
+ return countRule(a, b, folded, extent, gutter, middle);
780
+ };
781
+ const openSideOf = (split) => {
782
+ const a = new Set(panesOf(split.a));
783
+ const b = new Set(panesOf(split.b));
784
+ for (const pane of order) {
785
+ if (a.has(pane)) return "a";
786
+ if (b.has(pane)) return "b";
787
+ }
788
+ return "a";
789
+ };
790
+ const openLayout = (node, side, rect, room, rails, foldIndex) => {
791
+ const axis = node.axis;
792
+ const [aEdge, bEdge] = sideEdges2(axis);
793
+ const edge = side === "a" ? bEdge : aEdge;
794
+ const gap = gapOf(node);
795
+ const rail = rails[edge];
796
+ const joined = rail !== void 0 && joinsRail(rail, rect, edge);
797
+ const own = joined ? null : foldBoxes(rect, axis, edge, spine, gap);
798
+ const openRect = own === null ? { ...rect } : own.openRect;
799
+ const railRect = own === null ? { ...rail.rect } : own.spineRect;
800
+ const railOf = own === null ? rail.fold : foldIndex;
801
+ const openRails = own === null ? rails : { ...rails, [edge]: { fold: foldIndex, rect: railRect, boundary: railBoundary(openRect, edge) } };
802
+ const taken = own === null ? 0 : spine + gap;
803
+ const openRoom = {
804
+ counted: withLength(room.counted, axis, Math.max(0, room.counted[axis] - taken)),
805
+ most: withLength(room.most, axis, Math.max(0, room.most[axis] - taken))
806
+ };
807
+ return { edge, openRect, railRect, railOf, openRails, openRoom, taken };
808
+ };
809
+ const decideFold = (node, rect, room, rails) => {
810
+ const sideOf = (side) => {
811
+ const layout = openLayout(node, side, rect, room, rails, -1);
812
+ return { layout, decided: decide(node[side], layout.openRect, layout.openRoom, layout.openRails) };
813
+ };
814
+ return { kind: "fold", node, room, open: openSideOf(node), sides: { a: sideOf("a"), b: sideOf("b") } };
815
+ };
816
+ const decide = (node, rect, room, rails) => {
817
+ if (isPane2(node) || "pane" in node) return { kind: "pane", node };
818
+ const here = new Set(
819
+ EDGES.filter((edge) => {
820
+ const rail = rails[edge];
821
+ return rail !== void 0 && joinsRail(rail, rect, edge);
822
+ })
823
+ );
824
+ const judged = { x: Math.max(0, room.counted.x), y: Math.max(0, room.counted.y) };
825
+ const along = node.axis;
826
+ const across = otherAxis2(along);
827
+ const crossMax = Math.max(lengthOf(rect, across), room.most[across]);
828
+ const verdict = judge(node, { x: rect.x, y: rect.y, width: judged.x, height: judged.y }, crossMax, here);
829
+ if (verdict === "fold") return decideFold(node, rect, { counted: judged, most: room.most }, rails);
830
+ const laid = layOutKept(node, rect, verdict.a, verdict.b);
831
+ const gutter = steps.gutter(node);
832
+ const sideRoom = (child, need, otherNeed) => {
833
+ const pin = pinAcross(child, node);
834
+ const counted = withLength(withLength(judged, along, need), across, pin === null ? judged[across] : Math.min(pin, judged[across]));
835
+ return { counted, most: withLength(room.most, along, Math.max(0, room.most[along] - gutter - otherNeed)) };
836
+ };
837
+ const roomLength = Math.max(0, judged[along] - gutter);
838
+ const bEdge = along === "x" ? "right" : "bottom";
839
+ const bRails = here.has(bEdge) ? /* @__PURE__ */ new Set([bEdge]) : NONE;
840
+ const bSteps = steps.steps(node.b, along, node, bRails, "any", Math.max(0, crossMax), sideAcross(node.b, node, judged[across]));
841
+ const bLeast = leastKeeping(bSteps, verdict.b);
842
+ const a = decide(node.a, laid.a, sideRoom(node.a, Math.max(0, roomLength - bLeast), bLeast), rails);
843
+ const aNeed = chosenNeed(a, along, node);
844
+ const bRect = layOutKept(node, rect, aNeed, bLeast).b;
845
+ const b = decide(node.b, bRect, sideRoom(node.b, Math.max(0, roomLength - aNeed), aNeed), rails);
846
+ return { kind: "kept", node, pair: verdict, a, b };
847
+ };
848
+ const chosen = /* @__PURE__ */ new WeakMap();
849
+ const chosenNeed = (decided, axis, boundary) => {
850
+ if (decided.kind === "pane") return steps.floor(decided.node, axis, boundary);
851
+ let byBoundary = chosen.get(decided);
852
+ const known = byBoundary?.get(boundary)?.[axis];
853
+ if (known !== void 0) return known;
854
+ const node = decided.node;
855
+ const alongIt = node.axis === axis;
856
+ let need = 0;
857
+ if (decided.kind === "kept") {
858
+ 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));
859
+ } else {
860
+ for (const side of ["a", "b"]) {
861
+ const { layout, decided: open } = decided.sides[side];
862
+ need = Math.max(need, alongIt ? chosenNeed(open, axis, node) + layout.taken : chosenNeed(open, axis, boundary));
863
+ }
864
+ }
865
+ if (byBoundary === void 0) chosen.set(decided, byBoundary = /* @__PURE__ */ new Map());
866
+ byBoundary.set(boundary, { ...byBoundary.get(boundary), [axis]: need });
867
+ return need;
868
+ };
869
+ const draw = (decided, rect, enclosing, rails) => {
870
+ if (decided.kind === "fold") {
871
+ drawFold(decided, rect, enclosing, rails);
872
+ return;
873
+ }
874
+ if (decided.kind === "pane") {
875
+ const node2 = decided.node;
876
+ if ("pane" in node2) {
877
+ panes.set(node2.pane, { ...rect });
878
+ return;
879
+ }
880
+ const laid2 = laySplitTree(node2, rect, options);
881
+ for (const [pane, r] of laid2.panes) panes.set(pane, r);
882
+ for (const [id, geometry] of laid2.splits) splits.set(id, geometry);
883
+ return;
884
+ }
885
+ const node = decided.node;
886
+ const axis = node.axis;
887
+ const laid = layOutKept(node, rect, chosenNeed(decided.a, axis, node), chosenNeed(decided.b, axis, node));
888
+ if (node.id !== void 0) splits.set(node.id, laid.geometry);
889
+ if (!drawnAt.moved) {
890
+ const plain = plainRatios.get(node);
891
+ if (plain === void 0 || !(Math.abs(plain - laid.geometry.ratio) <= RATIO_EPSILON)) drawnAt.moved = true;
892
+ }
893
+ draw(decided.a, laid.a, enclosing, rails);
894
+ draw(decided.b, laid.b, enclosing, rails);
895
+ };
896
+ const drawFold = (decided, rect, enclosing, rails) => {
897
+ const node = decided.node;
898
+ const index = folds.length;
899
+ const layout = openLayout(node, decided.open, rect, decided.room, rails, index);
900
+ const folded = panesOf(node[decided.open === "a" ? "b" : "a"]);
901
+ const shown = folded.filter((pane) => weight(pane) > 0);
902
+ const target = targetOrder.find((pane) => shown.includes(pane)) ?? shown[0] ?? null;
903
+ folds.push({
904
+ id: node.id,
905
+ axis: node.axis,
906
+ open: decided.open,
907
+ edge: layout.edge,
908
+ rect: { ...rect },
909
+ openRect: layout.openRect,
910
+ spine: { ...layout.railRect },
911
+ rail: layout.railRect,
912
+ // Set with the rail's segments, below.
913
+ railContentLength: 0,
914
+ railOf: layout.railOf,
915
+ parent: enclosing.length > 0 ? enclosing[enclosing.length - 1] : null,
916
+ panes: folded,
917
+ target
918
+ });
919
+ draw(decided.sides[decided.open].decided, layout.openRect, [...enclosing, index], layout.openRails);
920
+ };
921
+ draw(decide(tree, area, wholeRoom(area), NO_RAILS2), area, [], NO_RAILS2);
922
+ const onRail = /* @__PURE__ */ new Map();
923
+ folds.forEach((each, index) => onRail.set(each.railOf, [...onRail.get(each.railOf) ?? [], index]));
924
+ const drawn = [...folds];
925
+ const paneParts = folds.map(() => []);
926
+ const asked = px5(fold.segmentFloor);
927
+ for (const members of onRail.values()) {
928
+ const head = folds[members[0]];
929
+ const along = head.axis === "x" ? "height" : "width";
930
+ const length = head.rail[along];
931
+ const shownOf = (i) => folds[i].panes.filter((pane) => weight(pane) > 0);
932
+ const shares = members.length < 2 ? [head.spine] : segments(head.rail, head.axis, members.map((i) => Math.max(1, shownOf(i).length)));
933
+ let spinesOf = shares;
934
+ let partsOf = members.map((i, n) => segments(shares[n], head.axis, shownOf(i).map(() => 1)));
935
+ let content = length;
936
+ const floor = Math.min(asked, length);
937
+ const shortest = Math.min(...members.map((i, n) => shownOf(i).length > 0 ? Math.min(...partsOf[n].map((r) => r[along])) : shares[n][along]));
938
+ if (floor > 0 && shortest < floor - FLOOR_EPSILON) {
939
+ const unit2 = (at2, size) => head.axis === "x" ? { ...head.rail, y: head.rail.y + at2, height: size } : { ...head.rail, x: head.rail.x + at2, width: size };
940
+ let at = 0;
941
+ const floored = [];
942
+ partsOf = members.map((i) => {
943
+ const start = at;
944
+ const parts = [];
945
+ for (let u = 0; u < Math.max(1, shownOf(i).length); u++, at += floor + SEGMENT_GAP) parts.push(unit2(at, floor));
946
+ floored.push(unit2(start, at - SEGMENT_GAP - start));
947
+ return shownOf(i).length > 0 ? parts : [];
948
+ });
949
+ spinesOf = floored;
950
+ content = Math.max(length, at - SEGMENT_GAP);
951
+ }
952
+ members.forEach((i, n) => {
953
+ drawn[i] = { ...folds[i], spine: { ...spinesOf[n] }, railContentLength: content };
954
+ paneParts[i] = partsOf[n];
955
+ });
956
+ }
957
+ const spines = /* @__PURE__ */ new Map();
958
+ drawn.forEach((each, i) => each.panes.filter((pane) => weight(pane) > 0).forEach((pane, n) => spines.set(pane, paneParts[i][n])));
959
+ if (drawn.length === 0 && !drawnAt.moved) {
960
+ const out = /* @__PURE__ */ new Map();
961
+ for (const [id, geometry] of base.splits) {
962
+ const mine = splits.get(id);
963
+ out.set(id, mine === void 0 ? geometry : { ...geometry, needs: mine.needs });
964
+ }
965
+ return { panes: base.panes, splits: out, spines, folds: drawn };
966
+ }
967
+ return { panes, splits, spines, folds: drawn };
968
+ }
969
+
970
+ // src/tiling/tree-layout.ts
971
+ function layoutSplitTree(tree, area, options = {}) {
972
+ if (options.fold) return foldLayout(tree, area, options, options.fold);
973
+ return { ...laySplitTree(tree, area, options), spines: /* @__PURE__ */ new Map(), folds: [] };
974
+ }
975
+ var withOrder = (options, order) => options.fold ? { ...options, fold: { ...options.fold, order } } : options;
976
+
977
+ // src/tiling/painted.ts
978
+ function offsetOf(folds, index, railOffset) {
979
+ const v = railOffset?.(folds[index], index);
980
+ return typeof v === "number" && Number.isFinite(v) ? v : 0;
981
+ }
982
+ function paint(r, axis, offset, box) {
983
+ if (axis === "x") {
984
+ const y0 = Math.max(r.y - offset, box.y);
985
+ const y1 = Math.min(r.y - offset + r.height, box.y + box.height);
986
+ return y1 > y0 ? { x: r.x, y: y0, width: r.width, height: y1 - y0 } : null;
987
+ }
988
+ const x0 = Math.max(r.x - offset, box.x);
989
+ const x1 = Math.min(r.x - offset + r.width, box.x + box.width);
990
+ return x1 > x0 ? { x: x0, y: r.y, width: x1 - x0, height: r.height } : null;
991
+ }
992
+ function paintedSpines(layout, railOffset) {
993
+ const out = /* @__PURE__ */ new Map();
994
+ const offsets = /* @__PURE__ */ new Map();
995
+ for (const fold of layout.folds) {
996
+ const head = layout.folds[fold.railOf];
997
+ let offset = offsets.get(fold.railOf);
998
+ if (offset === void 0) offsets.set(fold.railOf, offset = offsetOf(layout.folds, fold.railOf, railOffset));
999
+ for (const pane of fold.panes) {
1000
+ const r = layout.spines.get(pane);
1001
+ if (!r) continue;
1002
+ const painted = paint(r, head.axis, offset, head.rail);
1003
+ if (painted) out.set(pane, painted);
1004
+ }
1005
+ }
1006
+ return out;
1007
+ }
1008
+ function railAt(layout, point, railOffset) {
1009
+ const rail = layout.folds.findIndex((f, i) => f.railOf === i && contains(f.rail, point, 0));
1010
+ if (rail < 0) return null;
1011
+ for (const [pane, r] of paintedSpines(layout, railOffset)) {
1012
+ if (layout.folds.some((f) => f.railOf === rail && f.panes.includes(pane)) && contains(r, point, 0)) return { rail, pane };
1013
+ }
1014
+ return { rail, pane: null };
385
1015
  }
386
1016
 
387
1017
  // src/tiling/regions.ts
@@ -558,13 +1188,18 @@ function createTilingHit(ctx, basis, options = {}) {
558
1188
  const stackOf = (t, k) => ops.stackOf?.(t, k) ?? null;
559
1189
  const drawnAs = (t, k) => stackOf(t, k)?.active ?? k;
560
1190
  const subjectDrawn = drawnAs(lift.tree, ctx.subjectKey);
1191
+ const baseFolded = new Set(base.folds.flatMap((f) => f.panes));
1192
+ const railBoxes = laid.folds.filter((f, i) => f.railOf === i).map((f) => f.rail);
1193
+ const onRail = (p) => railBoxes.some((r) => inside(r, p));
1194
+ const offsets = basis === "lift" ? ctx.railOffset : void 0;
1195
+ const foldedAt = (p) => onRail(p) ? railAt(laid, p, offsets) : null;
561
1196
  const atOf = (pane) => {
562
- if (base.layout.has(pane)) return pane;
1197
+ if (base.layout.has(pane) || baseFolded.has(pane)) return pane;
563
1198
  const members = stackOf(laid.tree, pane)?.members ?? [];
564
1199
  for (const m of members) {
565
1200
  if (ctx.inSubject(m)) continue;
566
1201
  const d = drawnAs(base.tree, m);
567
- if (base.layout.has(d)) return d;
1202
+ if (base.layout.has(d) || baseFolded.has(d)) return d;
568
1203
  }
569
1204
  return null;
570
1205
  };
@@ -615,8 +1250,10 @@ function createTilingHit(ctx, basis, options = {}) {
615
1250
  };
616
1251
  const liftArrangement = arrangementOf(ops.normalize ? ops.normalize(lift.tree) : lift.tree);
617
1252
  const homeMemo = /* @__PURE__ */ new Map();
1253
+ const carriedIn = lift.spines.has(subjectDrawn);
618
1254
  const homeLike = (p) => {
619
1255
  if (p === homePlace) return true;
1256
+ if (carriedIn) return false;
620
1257
  let v = homeMemo.get(p.id);
621
1258
  if (v === void 0) {
622
1259
  v = live(p) && (after(p.held).tree === lift.tree || arrangementOf(after(p.held).tree) === liftArrangement);
@@ -714,6 +1351,7 @@ function createTilingHit(ctx, basis, options = {}) {
714
1351
  }
715
1352
  Object.freeze(targets);
716
1353
  const rawAt = (p) => {
1354
+ if (onRail(p)) return { place: null, refused: null };
717
1355
  if (home && inside(home, p)) return { place: homePlace, refused: null };
718
1356
  const pane = panes.find((ph) => inside(ph.body.hit, p));
719
1357
  if (!pane) return { place: null, refused: null };
@@ -738,7 +1376,7 @@ function createTilingHit(ctx, basis, options = {}) {
738
1376
  homeLikeAll ??= [...byId.values()].filter((p) => p !== homePlace && homeLike(p));
739
1377
  return [...homePlace.pieces, ...homeLikeAll.flatMap((p) => p.pieces)];
740
1378
  };
741
- const capped = (px5, extent) => Math.min(px5, cap * extent);
1379
+ const capped = (px7, extent) => Math.min(px7, cap * extent);
742
1380
  const visibleMemo = /* @__PURE__ */ new Map();
743
1381
  const visibleOf = (piece) => {
744
1382
  let v = visibleMemo.get(piece);
@@ -759,6 +1397,7 @@ function createTilingHit(ctx, basis, options = {}) {
759
1397
  return ((p.x - from.x) / rx) ** 2 + ((p.y - from.y) / ry) ** 2 < 1;
760
1398
  };
761
1399
  const hold = (state2, p) => {
1400
+ if (onRail(p)) return null;
762
1401
  const now = effective(rawAt(p).place);
763
1402
  if (state2) {
764
1403
  if (now && now.id === state2.place) return state2;
@@ -794,7 +1433,7 @@ function createTilingHit(ctx, basis, options = {}) {
794
1433
  const shown = place && place !== homePlace && frame.shown && place.owners.has(frame.shown.id) ? place : null;
795
1434
  const held = place === homePlace ? HOME : shown?.held ?? null;
796
1435
  const refused = held ? null : rawAt(p).refused?.held ?? null;
797
- return { held, refused, mark: basis === "lift" && shown ? markOf(shown) : null };
1436
+ return { held, refused, mark: basis === "lift" && shown ? markOf(shown) : null, folded: foldedAt(p) };
798
1437
  };
799
1438
  return {
800
1439
  basis,
@@ -809,6 +1448,7 @@ function createTilingHit(ctx, basis, options = {}) {
809
1448
  heldOf: (id) => byId.get(id)?.held ?? null,
810
1449
  placeOf: (t, p) => placeOf(t, p),
811
1450
  read,
1451
+ foldedAt,
812
1452
  drawn: (r) => basis === "lift" ? lift : r.held === null ? base : r.held.kind === "home" ? lift : after(r.held),
813
1453
  release: (r) => after(r.held ?? HOME)
814
1454
  };
@@ -844,26 +1484,40 @@ function giveWay(panes, layout, min, age) {
844
1484
 
845
1485
  // src/tiling/drag.ts
846
1486
  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;
1487
+ var px6 = (v) => v !== void 0 && Number.isFinite(v) && v > 0 ? v : 0;
1488
+ var foldedOf = (folds) => new Set(folds.flatMap((f) => f.panes));
1489
+ function keyed(lists) {
1490
+ const out = /* @__PURE__ */ new Set();
1491
+ for (const list of lists) for (const k of list) out.add(k);
1492
+ return [...out];
1493
+ }
1494
+ var openFirst = (laid) => keyed([laid.order.filter((k) => laid.layout.has(k)), [...laid.layout.keys()], laid.order]);
848
1495
  function tilingDrag(tree, subject, options) {
849
1496
  const { ops, area } = options;
850
1497
  const normalize = (t) => ops.normalize ? ops.normalize(t) : t;
851
- const lay2 = (t) => {
852
- const { panes, splits } = layoutSplitTree(ops.split(t), area, options);
853
- return { tree: t, layout: panes, splits };
1498
+ const lay2 = (t, order) => {
1499
+ const { panes, splits, spines, folds } = layoutSplitTree(ops.split(t), area, withOrder(options, order));
1500
+ return { tree: t, layout: panes, splits, spines, folds, order: options.fold ? order : [] };
854
1501
  };
855
1502
  const stackOf = (t, pane) => ops.stackOf?.(t, pane) ?? null;
856
1503
  const drawnAs = (t, pane) => stackOf(t, pane)?.active ?? pane;
857
1504
  const subjectKey = subject.kind === "pane" ? subject.pane : subject.stack.active;
858
- const lift = lay2(tree);
859
- const liftRect = lift.layout.get(drawnAs(tree, subjectKey));
1505
+ const lift = lay2(tree, options.fold?.order ?? []);
1506
+ const liftKey = drawnAs(tree, subjectKey);
1507
+ const liftRect = lift.layout.get(liftKey) ?? lift.spines.get(liftKey);
860
1508
  if (!liftRect) {
861
1509
  const what = subject.kind === "pane" ? `pane ${String(subject.pane)}` : `stack ${subject.stack.id}`;
862
- throw new Error(`tilingDrag: the subject (${what}) is not in the tree: it is neither drawn nor a member of a drawn stack`);
1510
+ const drawn = options.fold ? "drawn (open or on a spine)" : "drawn";
1511
+ throw new Error(`tilingDrag: the subject (${what}) is not in the tree: it is neither ${drawn} nor a member of a drawn stack`);
863
1512
  }
864
- const base = lay2(ops.release(tree, subject));
1513
+ const mates = subject.kind === "pane" ? (stackOf(tree, subjectKey)?.members ?? []).filter((m) => m !== subjectKey) : [];
1514
+ const liftOrder = openFirst(lift);
1515
+ const baseOrder = keyed([liftOrder.flatMap((k) => k === liftKey ? [k, ...mates] : [k]), mates]);
1516
+ const base = lay2(ops.release(tree, subject), baseOrder);
865
1517
  const takesPane = panesOf(ops.split(base.tree)).length < panesOf(ops.split(tree)).length;
866
- const home = takesPane ? { ...liftRect } : null;
1518
+ const home = takesPane && lift.layout.has(liftKey) ? { ...liftRect } : null;
1519
+ const liftFolded = foldedOf(lift.folds);
1520
+ const baseFolded = foldedOf(base.folds);
867
1521
  const movedIn = (laid) => {
868
1522
  const own = drawnAs(laid.tree, subjectKey);
869
1523
  const out = [];
@@ -874,6 +1528,11 @@ function tilingDrag(tree, subject, options) {
874
1528
  }
875
1529
  return out;
876
1530
  };
1531
+ const foldChangesIn = (laid) => {
1532
+ if (!options.fold) return { opened: [], closed: [] };
1533
+ const folded = foldedOf(laid.folds);
1534
+ return { opened: [...laid.layout.keys()].filter((k) => liftFolded.has(k)), closed: [...lift.layout.keys()].filter((k) => folded.has(k)) };
1535
+ };
877
1536
  const inSubject = (pane) => subject.kind === "pane" ? pane === subject.pane : subject.stack.members.includes(pane);
878
1537
  const refusalBefore = (held) => {
879
1538
  const at = held.at;
@@ -882,34 +1541,41 @@ function tilingDrag(tree, subject, options) {
882
1541
  const own = stackOf(tree, subjectKey);
883
1542
  if (own && stackOf(tree, at)?.id === own.id) return { code: "self" };
884
1543
  }
885
- if (!base.layout.has(at)) return { code: "missing" };
1544
+ if (!base.layout.has(at)) return baseFolded.has(at) ? { code: "folded" } : { code: "missing" };
886
1545
  if (options.still?.(at)) return { code: "still" };
887
1546
  if (held.kind === "join" && !ops.join) return { code: "join" };
888
1547
  return null;
889
1548
  };
890
- const refused = (held, refusal) => ({ ...lift, held, refusal, landing: null, moved: [] });
1549
+ const refused = (held, refusal) => ({ ...lift, held, refusal, landing: null, moved: [], opened: [], closed: [] });
891
1550
  const roomFor = (at, side) => {
892
1551
  const r = base.layout.get(at);
893
1552
  if (canSplitPane(r, side, options).ok) return null;
894
1553
  const along = side === "left" || side === "right";
895
- const gutter = px4(options.gap) + px4(options.handle);
896
- const min = px4(options.min);
1554
+ const gutter = px6(options.gap) + px6(options.handle);
1555
+ const min = px6(options.min);
897
1556
  const span = along ? r.width : r.height;
898
1557
  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
1558
  };
900
1559
  const make = (held) => {
901
- if (held.kind === "home") return { ...lift, held, refusal: null, landing: liftRect, moved: [] };
902
- const before = refusalBefore(held) ?? (held.kind === "beside" ? roomFor(held.at, held.side) : null);
1560
+ if (held.kind === "home") return { ...lift, held, refusal: null, landing: liftRect, moved: [], opened: [], closed: [] };
1561
+ const before = refusalBefore(held) ?? (held.kind === "beside" && !options.fold ? roomFor(held.at, held.side) : null);
903
1562
  if (before) return refused(held, before);
904
1563
  const changed = held.kind === "swap" ? ops.swap(tree, subject, held.at) : held.kind === "beside" ? ops.beside(base.tree, subject, held.at, held.side, { kind: "half" }) : ops.join(base.tree, subject, held.at, held.index);
905
- const laid = lay2(normalize(changed));
906
- const own = drawnAs(laid.tree, subjectKey);
1564
+ const changedTree = normalize(changed);
1565
+ const own = drawnAs(changedTree, subjectKey);
1566
+ const candidateOrder = keyed([[own], baseOrder]);
1567
+ const laid = lay2(changedTree, candidateOrder);
907
1568
  const partner = drawnAs(laid.tree, held.at);
908
- 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);
1569
+ const fits = dropFits(laid.layout, own, partner, area, {
1570
+ min: options.min,
1571
+ side: held.kind === "beside" ? held.side : "left",
1572
+ ...options.fold ? { folded: foldedOf(laid.folds) } : {}
1573
+ });
1574
+ if (!fits.ok) {
1575
+ const small = (r) => !r || r.width < px6(options.min) || r.height < px6(options.min);
910
1576
  return refused(held, { code: "fit", pane: small(laid.layout.get(own)) ? own : partner });
911
1577
  }
912
- return { ...laid, held, refusal: null, landing: laid.layout.get(own) ?? null, moved: movedIn(laid) };
1578
+ return { ...laid, held, refusal: null, landing: laid.layout.get(own) ?? null, moved: movedIn(laid), ...foldChangesIn(laid) };
913
1579
  };
914
1580
  const homeAfter = make({ kind: "home" });
915
1581
  const memo = /* @__PURE__ */ new Map();
@@ -926,7 +1592,7 @@ function tilingDrag(tree, subject, options) {
926
1592
  const hits = /* @__PURE__ */ new Map();
927
1593
  const hit = (basis, hitOptions) => {
928
1594
  let h = hits.get(basis);
929
- if (!h) hits.set(basis, h = createTilingHit({ ops, lift, base, home, subjectKey, inSubject, ...options.still ? { still: options.still } : {}, after, lay: lay2, changeOf }, basis, hitOptions));
1595
+ if (!h) hits.set(basis, h = createTilingHit({ ops, lift, base, home, subjectKey, inSubject, ...options.still ? { still: options.still } : {}, ...options.railOffset ? { railOffset: options.railOffset } : {}, after, lay: (t) => lay2(t, baseOrder), changeOf }, basis, hitOptions));
930
1596
  return h;
931
1597
  };
932
1598
  return { subject, lift, base, home, after, hit };
@@ -1029,13 +1695,23 @@ function splitTreeOps(options = {}) {
1029
1695
  var TILING_PROPERTIES = [
1030
1696
  /** `after(home).tree` is the lift tree itself, its layout the lift's, never normalized. */
1031
1697
  "home-is-lift",
1032
- /** `home` is the subject's lift rect when the release takes a drawn pane away, else null. */
1698
+ /** `home` is the subject's lift rect when the release takes a drawn pane away and the lift draws the subject open, else null (a subject on a spine has none). */
1033
1699
  "home-is-the-gap",
1034
1700
  /** Home lies only over base panes whose rect changed when the subject was released. */
1035
1701
  "home-over-changed",
1036
- /** `after(held).layout` is `after(held).tree` laid out; the landing is the subject's rect there; a refused place is the lift. */
1702
+ /**
1703
+ * `after(held).layout` is `after(held).tree` laid out (under a fold with `after(held).order`, its spines and folds
1704
+ * too); the landing is the subject's rect there, drawn open (home's, for a subject on a spine at the lift: its
1705
+ * segment); a refused place is the lift.
1706
+ */
1037
1707
  "one-layout",
1038
- /** A swap puts the partner in the subject's lift rect and the subject in the partner's, and the others keep theirs. Not checked under per-child layout callbacks (`fixed`, `fixedAcross`, `floor`), which may move the ratios. */
1708
+ /** Under a fold, every pane the base folds is refused `folded` for every kind of place (a swap with the stack a tab is torn from stays `self`). */
1709
+ "folded-refused",
1710
+ /**
1711
+ * A swap puts the partner in the subject's lift rect and the subject in the partner's, and the others keep theirs.
1712
+ * Not checked under per-child layout callbacks (`fixed`, `fixedAcross`, `floor`), which may move the ratios, nor
1713
+ * under a fold (a swap can change which pane comes first, and so which side of a fold shows).
1714
+ */
1039
1715
  "swap-exchanges",
1040
1716
  /** A swap puts the partner in the subject's old slot of the split (as it is drawn there) and the subject in the partner's. Checked under every option, the per-child callbacks included. */
1041
1717
  "swap-trades-slots",
@@ -1101,7 +1777,16 @@ function paneAt(split, path) {
1101
1777
  return node && "pane" in node ? node.pane : void 0;
1102
1778
  }
1103
1779
  var sameList = (a, b) => a.length === b.length && a.every((k, i) => Object.is(k, b[i]));
1104
- var show = (h) => h.kind === "home" ? "home" : `${h.kind} ${String(h.at)}${h.kind === "beside" ? ` ${h.side}` : h.kind === "join" && h.index !== void 0 ? ` @${h.index}` : ""}`;
1780
+ var showHeld = (h) => h.kind === "home" ? "home" : `${h.kind} ${String(h.at)}${h.kind === "beside" ? ` ${h.side}` : h.kind === "join" && h.index !== void 0 ? ` @${h.index}` : ""}`;
1781
+ var show = showHeld;
1782
+ function liftable(tree, subject, options) {
1783
+ if (!options.fold) return true;
1784
+ const { ops } = options;
1785
+ const key = subject.kind === "pane" ? subject.pane : subject.stack.active;
1786
+ const drawn = ops.stackOf?.(tree, key)?.active ?? key;
1787
+ const lift = layoutSplitTree(ops.split(tree), options.area, options);
1788
+ return lift.panes.has(drawn) || lift.spines.has(drawn);
1789
+ }
1105
1790
  function tilingSubjects(tree, options) {
1106
1791
  const { ops } = options;
1107
1792
  const out = [];
@@ -1127,7 +1812,7 @@ function tilingConformance(trees, options, limit = 20) {
1127
1812
  const { ops, area } = options;
1128
1813
  const { counts, failures, checkFor } = propertyTally(TILING_PROPERTIES, limit);
1129
1814
  const norm = (t) => ops.normalize ? ops.normalize(t) : t;
1130
- const lay2 = (t) => layoutSplitTree(ops.split(t), area, options).panes;
1815
+ const lay2 = (t, order) => layoutSplitTree(ops.split(t), area, withOrder(options, order));
1131
1816
  const drawnAs = (t, k) => ops.stackOf?.(t, k)?.active ?? k;
1132
1817
  const callbacks = !!(options.fixed || options.fixedAcross || options.floor);
1133
1818
  const keysOf = (t) => panesOf(ops.split(t)).flatMap((p) => ops.stackOf?.(t, p)?.members ?? [p]);
@@ -1136,6 +1821,7 @@ function tilingConformance(trees, options, limit = 20) {
1136
1821
  for (const tree of trees) {
1137
1822
  const snapshot = copy(tree);
1138
1823
  for (const subject of tilingSubjects(tree, options)) {
1824
+ if (!liftable(tree, subject, options)) continue;
1139
1825
  const check = checkFor(`tree ${n}, ${subject.kind === "pane" ? `pane ${String(subject.pane)}` : `stack ${subject.stack.id}`}`);
1140
1826
  const drag = tilingDrag(tree, subject, options);
1141
1827
  const key = subject.kind === "pane" ? subject.pane : subject.stack.active;
@@ -1149,7 +1835,8 @@ function tilingConformance(trees, options, limit = 20) {
1149
1835
  const home = drag.after({ kind: "home" });
1150
1836
  check("home-is-lift", home.tree === tree && sameData(home.layout, drag.lift.layout) && home.refusal === null, () => "after(home) is not the lift tree laid out");
1151
1837
  const takes = panesOf(ops.split(drag.base.tree)).length < panesOf(ops.split(tree)).length;
1152
- check("home-is-the-gap", takes ? near(drag.home, subjectLift, EPS) : drag.home === null, () => `home ${JSON.stringify(drag.home)}, the release ${takes ? "takes" : "takes no"} drawn pane`);
1838
+ const gap = takes && subjectLift !== void 0;
1839
+ check("home-is-the-gap", gap ? near(drag.home, subjectLift, EPS) : drag.home === null, () => `home ${JSON.stringify(drag.home)}, the release ${takes ? "takes" : "takes no"} drawn pane${subjectLift ? "" : " (the subject is folded at the lift)"}`);
1153
1840
  if (drag.home) {
1154
1841
  for (const [pane, r] of drag.base.layout) {
1155
1842
  if (overlap(drag.home, r) <= EPS) continue;
@@ -1161,6 +1848,14 @@ function tilingConformance(trees, options, limit = 20) {
1161
1848
  held.push({ kind: "swap", at }, ...PANE_SIDES.map((side) => ({ kind: "beside", at, side })));
1162
1849
  if (ops.join) held.push({ kind: "join", at }, { kind: "join", at, index: 0 });
1163
1850
  }
1851
+ for (const at of new Set(drag.base.folds.flatMap((f) => f.panes))) {
1852
+ const kinds = [{ kind: "swap", at }, ...PANE_SIDES.map((side) => ({ kind: "beside", at, side })), ...ops.join ? [{ kind: "join", at }] : []];
1853
+ for (const h of kinds) {
1854
+ const code = drag.after(h).refusal?.code;
1855
+ const own = h.kind === "swap" && subject.kind === "pane" && !!ops.stackOf && ops.stackOf(tree, subject.pane)?.id === ops.stackOf(tree, at)?.id && !!ops.stackOf(tree, at);
1856
+ check("folded-refused", code === "folded" || own && code === "self", () => `${show(h)}: the base folds ${String(at)}, refused ${code ?? "nothing"}`);
1857
+ }
1858
+ }
1164
1859
  for (const h of held) {
1165
1860
  const a = drag.after(h);
1166
1861
  check("nothing-changes", drag.after(h) === a, () => `${show(h)}: after() gave a new object for the same place`);
@@ -1178,8 +1873,14 @@ function tilingConformance(trees, options, limit = 20) {
1178
1873
  continue;
1179
1874
  }
1180
1875
  const own = drawnAs(a.tree, key);
1181
- check("one-layout", sameData(a.layout, lay2(a.tree)) && near(a.landing, a.layout.get(own), EPS), () => `${show(h)}: the layout or the landing is not after().tree's`);
1182
- if (h.kind === "swap" && !callbacks) {
1876
+ const again = lay2(a.tree, a.order);
1877
+ const lands = near(a.landing, h.kind === "home" ? a.layout.get(own) ?? a.spines.get(own) : a.layout.get(own), EPS);
1878
+ check(
1879
+ "one-layout",
1880
+ sameData(a.layout, again.panes) && sameData(a.spines, again.spines) && sameData(a.folds, again.folds) && lands,
1881
+ () => `${show(h)}: the layout or the landing is not after().tree's, laid out with after().order`
1882
+ );
1883
+ if (h.kind === "swap" && !callbacks && !options.fold) {
1183
1884
  const partner = drawnAs(a.tree, h.at);
1184
1885
  const exchanged = near(a.layout.get(partner), subjectLift) && near(a.layout.get(own), liftOf(h.at));
1185
1886
  const others = [...a.layout].every(([p, r]) => p === own || p === partner || near(r, liftOf(p)));
@@ -1244,7 +1945,7 @@ var TILING_HIT_PROPERTIES = [
1244
1945
  "one-pass",
1245
1946
  /** A place on the map is offered exactly when `after(place).refusal` is null (a side the run-neighbor rule removed is neither); over a refused zone nothing is held and the refusal is named. */
1246
1947
  "existence",
1247
- /** Every place whose release gives the lift back reads as home, and its release commits the lift tree. */
1948
+ /** Every place whose release gives the lift back reads as home, and its release commits the lift tree (never for a subject folded at the lift: any drop opens it). */
1248
1949
  "lift-equals-home",
1249
1950
  /** With no hold: lift basis, the mark contains the pointer and lies inside the target pane as drawn; base basis, the landing contains the pointer for every insert and join (swaps whose landing lies beside the pointer are counted, not failed). */
1250
1951
  "shown-under-pointer",
@@ -1264,7 +1965,18 @@ var TILING_HIT_PROPERTIES = [
1264
1965
  * pane, every point reads the same place where both are live, and where a place is refused the point falls to the
1265
1966
  * target beneath (nothing, or a part's pane body), never to a neighboring zone.
1266
1967
  */
1267
- "room-free-map"
1968
+ "room-free-map",
1969
+ /**
1970
+ * Under a fold, a rail takes no drop: every point of a rail's box (its edges included) has no place, holds nothing
1971
+ * (whatever was held), and reads `folded` with that rail and the pane painted there (`paintedSpines` at the drag's
1972
+ * rail offsets on the lift basis, none on the base), null in a gap.
1973
+ */
1974
+ "spine-no-place",
1975
+ /**
1976
+ * Under a fold, on the lift basis, a pane the lift draws open and the base folds (the named case) is a target, and
1977
+ * every place on it is refused `folded`: the pointer over it holds nothing and names the refused place.
1978
+ */
1979
+ "named-case-refused"
1268
1980
  ];
1269
1981
  function seeded(seed) {
1270
1982
  let s = seed >>> 0 || 1;
@@ -1279,7 +1991,7 @@ function seeded(seed) {
1279
1991
  var inside2 = (r, p) => contains(r, p, 0);
1280
1992
  var within = (a, b, e = 1e-6) => a.x >= b.x - e && a.y >= b.y - e && a.x + a.width <= b.x + b.width + e && a.y + a.height <= b.y + b.height + e;
1281
1993
  var sameRect2 = (a, b, e = 1e-3) => !!a && !!b && Math.abs(a.x - b.x) <= e && Math.abs(a.y - b.y) <= e && Math.abs(a.width - b.width) <= e && Math.abs(a.height - b.height) <= e;
1282
- var show2 = (h) => h === null ? "nothing" : h.kind === "home" ? "home" : `${h.kind} ${String(h.at)}${h.kind === "beside" ? ` ${h.side}` : h.kind === "join" && h.index !== void 0 ? ` @${h.index}` : ""}`;
1994
+ var show2 = (h) => h === null ? "nothing" : showHeld(h);
1283
1995
  var cross = (a, b, p) => (b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x);
1284
1996
  function inQuad(q, p) {
1285
1997
  const s = q.map((a, i) => cross(a, q[(i + 1) % q.length], p));
@@ -1301,6 +2013,7 @@ function tilingHitConformance(trees, options, hitOptions = {}, limit = 20) {
1301
2013
  let n = 0;
1302
2014
  for (const tree of trees) {
1303
2015
  for (const subject of tilingSubjects(tree, options)) {
2016
+ if (!liftable(tree, subject, options)) continue;
1304
2017
  const drag = tilingDrag(tree, subject, options);
1305
2018
  const name = `tree ${n}, ${subject.kind === "pane" ? `pane ${String(subject.pane)}` : `stack ${subject.stack.id}`}`;
1306
2019
  for (const basis of ["lift", "base"]) {
@@ -1347,17 +2060,20 @@ function tilingHitConformance(trees, options, hitOptions = {}, limit = 20) {
1347
2060
  const targetsAtLift = hit.targets;
1348
2061
  const snapshot = JSON.stringify(hit.targets);
1349
2062
  const subjectDrawn = drawnAs(drag.lift.tree, subjectKey);
2063
+ const baseFolded = new Set(drag.base.folds.flatMap((f) => f.panes));
2064
+ const rails = laid.folds.map((f, i) => ({ f, i })).filter(({ f, i }) => f.railOf === i);
2065
+ const railOf = (p) => rails.find(({ f }) => inside2(f.rail, p))?.i ?? null;
1350
2066
  const atOf = (pane) => {
1351
- if (drag.base.layout.has(pane)) return pane;
2067
+ if (drag.base.layout.has(pane) || baseFolded.has(pane)) return pane;
1352
2068
  for (const m of stackOf(laid.tree, pane)?.members ?? []) {
1353
2069
  if (inSubject(m)) continue;
1354
2070
  const d = drawnAs(drag.base.tree, m);
1355
- if (drag.base.layout.has(d)) return d;
2071
+ if (drag.base.layout.has(d) || baseFolded.has(d)) return d;
1356
2072
  }
1357
2073
  return null;
1358
2074
  };
1359
2075
  const flatOf = (t) => {
1360
- const placed = layoutSplitTree(ops.split(t), area, options).panes;
2076
+ const placed = layoutSplitTree(ops.split(t), area, withOrder(options, laid.order)).panes;
1361
2077
  const keysIn = (n2) => "pane" in n2 ? [n2.pane] : [...keysIn(n2.a), ...keysIn(n2.b)];
1362
2078
  const box = (n2) => {
1363
2079
  const rs = keysIn(n2).map((k) => placed.get(k)).filter((r) => !!r);
@@ -1464,12 +2180,15 @@ function tilingHitConformance(trees, options, hitOptions = {}, limit = 20) {
1464
2180
  return { x: c.cr, y: r.y, width: r.x + r.width - c.cr, height: r.height };
1465
2181
  };
1466
2182
  const live = (h) => h.kind === "home" || drag.after(h).refusal === null;
2183
+ const carriedIn = drag.lift.spines.has(subjectDrawn);
1467
2184
  const givesLift = (h) => {
1468
2185
  if (h.kind === "home") return true;
2186
+ if (carriedIn) return false;
1469
2187
  const a = drag.after(h);
1470
2188
  return !a.refusal && (a.tree === drag.lift.tree || flatOf(a.tree) === liftFlat);
1471
2189
  };
1472
2190
  const expectAt = (p) => {
2191
+ if (railOf(p) !== null) return { raw: null, refused: null, box: null, pane: null, tie: false };
1473
2192
  if (home && inside2(home, p)) return { raw: HOME2, refused: null, box: home, pane: null, tie: false };
1474
2193
  const pane = panes.find((x) => inside2(x.rect, p));
1475
2194
  if (!pane) return { raw: null, refused: null, box: null, pane: null, tie: false };
@@ -1537,7 +2256,7 @@ function tilingHitConformance(trees, options, hitOptions = {}, limit = 20) {
1537
2256
  const own = drawnAs(a.tree, subjectKey);
1538
2257
  const others = [...a.layout].some(([k, rect]) => k !== at && k !== own && drag.base.layout.has(k) && !sameRect2(rect, drag.base.layout.get(k)));
1539
2258
  const under = !!a.landing && inside2(a.landing, p);
1540
- if (others || options.fixed || options.fixedAcross || hasMaster) {
2259
+ if (others || options.fixed || options.fixedAcross || hasMaster || options.fold) {
1541
2260
  reported.insertsMoveOthers++;
1542
2261
  if (!under) reported.insertsMoveOthersBeside++;
1543
2262
  } else check("shown-under-pointer", under, () => `${show2(r.held)}: landing ${JSON.stringify(a.landing)} does not contain ${p.x.toFixed(1)},${p.y.toFixed(1)}`);
@@ -1641,7 +2360,8 @@ function tilingHitConformance(trees, options, hitOptions = {}, limit = 20) {
1641
2360
  if (zoneOf(z) !== want || !inside2(e.box, z)) continue;
1642
2361
  const ang = rnd() * Math.PI * 2;
1643
2362
  const len = Math.sqrt(rnd()) * 0.9;
1644
- path.push({ x: z.x + Math.cos(ang) * ex * len, y: z.y + Math.sin(ang) * ey * len });
2363
+ const p = { x: z.x + Math.cos(ang) * ex * len, y: z.y + Math.sin(ang) * ey * len };
2364
+ if (railOf(p) === null) path.push(p);
1645
2365
  }
1646
2366
  const tb = hit.heldOf(start.place)?.kind === "join" ? null : box;
1647
2367
  if (tb) {
@@ -1651,7 +2371,7 @@ function tilingHitConformance(trees, options, hitOptions = {}, limit = 20) {
1651
2371
  const ang = rnd() * Math.PI * 2;
1652
2372
  const len = Math.sqrt(rnd()) * 0.9;
1653
2373
  const p = { x: a.x + Math.cos(ang) * rx * len, y: a.y + Math.sin(ang) * ry * len };
1654
- if (inside2(e.pane.rect, p) && !(home && inside2(home, p))) path.push(p);
2374
+ if (inside2(e.pane.rect, p) && !(home && inside2(home, p)) && railOf(p) === null) path.push(p);
1655
2375
  }
1656
2376
  }
1657
2377
  let state = start;
@@ -1686,6 +2406,52 @@ function tilingHitConformance(trees, options, hitOptions = {}, limit = 20) {
1686
2406
  if (probed++ >= 24) break;
1687
2407
  check("hold-measures", shows(p, taken.place), () => `${taken.place} taken at ${q.x.toFixed(1)},${q.y.toFixed(1)} is kept at ${p.x.toFixed(1)},${p.y.toFixed(1)}, past both measures (the map reads ${hit.placeAt(p)})`);
1688
2408
  }
2409
+ if (rails.length > 0) {
2410
+ const painted = paintedSpines(laid, basis === "lift" ? options.railOffset : void 0);
2411
+ const taken = samples.map((p) => hit.hold(null, p)).find((s) => s !== null) ?? null;
2412
+ for (const { f, i } of rails) {
2413
+ const r = f.rail;
2414
+ const points = [
2415
+ { x: r.x, y: r.y },
2416
+ { x: r.x + r.width, y: r.y + r.height },
2417
+ { x: r.x + r.width / 2, y: r.y },
2418
+ { x: r.x, y: r.y + r.height / 2 }
2419
+ ];
2420
+ for (let k = 0; k < 16; k++) points.push(randomPoint(r));
2421
+ for (const p of points) {
2422
+ const want = [...painted].find(([pane, s]) => laid.folds.some((g) => g.railOf === i && g.panes.includes(pane)) && inside2(s, p))?.[0] ?? null;
2423
+ const read = fresh(p);
2424
+ const at = hit.foldedAt(p);
2425
+ check(
2426
+ "spine-no-place",
2427
+ hit.placeAt(p) === null && hit.hold(null, p) === null && hit.hold(taken, p) === null && read.held === null && read.refused === null,
2428
+ () => `rail ${i} at ${p.x.toFixed(1)},${p.y.toFixed(1)}: place ${hit.placeAt(p)}, held ${show2(read.held)}, refused ${show2(read.refused)}`
2429
+ );
2430
+ check(
2431
+ "spine-no-place",
2432
+ read.folded?.rail === i && read.folded.pane === want && at?.rail === i && at.pane === want,
2433
+ () => `rail ${i} at ${p.x.toFixed(1)},${p.y.toFixed(1)}: reads ${JSON.stringify(read.folded)}, foldedAt ${JSON.stringify(at)}, painted ${String(want)}`
2434
+ );
2435
+ }
2436
+ }
2437
+ }
2438
+ if (basis === "lift") {
2439
+ for (const [pane, r] of drag.lift.layout) {
2440
+ const at = atOf(pane);
2441
+ if (at === null || !baseFolded.has(at)) continue;
2442
+ const target = hit.targets.find((t) => !t.part && !t.home && t.pane === pane);
2443
+ const torn = !!stackOf(drag.lift.tree, at) && stackOf(drag.lift.tree, at)?.id === stackOf(drag.lift.tree, subjectKey)?.id;
2444
+ const places2 = [...torn ? [] : [{ kind: "swap", at }], ...["top", "right", "bottom", "left"].map((side) => ({ kind: "beside", at, side }))];
2445
+ const codes = places2.map((h) => drag.after(h).refusal?.code);
2446
+ const c = { x: r.x + r.width / 2, y: r.y + r.height / 2 };
2447
+ const read = fresh(c);
2448
+ check(
2449
+ "named-case-refused",
2450
+ target?.at === at && codes.every((code) => code === "folded") && hit.placeAt(c) === null && read.held === null && read.refused !== null,
2451
+ () => `${String(pane)}, open at the lift and folded in the base as ${String(at)}: target ${target ? String(target.at) : "none"}, refusals ${codes.join(",")}, place ${hit.placeAt(c)}, refused ${show2(read.refused)}`
2452
+ );
2453
+ }
2454
+ }
1689
2455
  check("fixed-map", hit.targets === targetsAtLift && Object.isFrozen(hit.targets) && JSON.stringify(hit.targets) === snapshot, () => "the targets changed during the drag");
1690
2456
  const zonesAfter = samples.map((p) => hit.placeAt(p));
1691
2457
  const moved = zonesAfter.findIndex((z, i) => z !== zonesBefore[i]);
@@ -1755,6 +2521,7 @@ export {
1755
2521
  masterStack,
1756
2522
  neighbor,
1757
2523
  orderTargets,
2524
+ paintedSpines,
1758
2525
  paneCenter,
1759
2526
  paneRegion,
1760
2527
  paneRegionShapes,