@n-uf/placement 26.11.57 → 26.11.59

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
@@ -1,5 +1,8 @@
1
1
  import {
2
- clamp
2
+ EPS,
3
+ clamp,
4
+ contains,
5
+ overlap
3
6
  } from "./chunk-2UVUFEBP.js";
4
7
 
5
8
  // src/tiling/order.ts
@@ -177,13 +180,13 @@ function lay(tree, area, ctx, owned) {
177
180
  const x = axis === "x";
178
181
  const extent = x ? area.width : area.height;
179
182
  const origin = x ? area.x : area.y;
180
- const cross = x ? area.height : area.width;
183
+ const cross2 = x ? area.height : area.width;
181
184
  const gutter = (tree.gap === void 0 ? ctx.gap : px(tree.gap)) + ctx.handle;
182
185
  const { fixed, fixedAcross, floor } = ctx.options;
183
186
  const lenA = fixedAcross?.(tree.a, axis);
184
187
  const lenB = fixedAcross?.(tree.b, axis);
185
- const acrossA = usable(lenA) ? lenA : cross;
186
- const acrossB = usable(lenB) ? lenB : cross;
188
+ const acrossA = usable(lenA) ? lenA : cross2;
189
+ const acrossB = usable(lenB) ? lenB : cross2;
187
190
  const fixedA = fixed?.(tree.a, axis);
188
191
  const fixedB = fixed?.(tree.b, axis);
189
192
  let lengthA;
@@ -282,11 +285,11 @@ function depths(p, c) {
282
285
  { side: "left", depth: (c.left - p.x) / Math.max(c.left, 1) }
283
286
  ];
284
287
  }
285
- function rawRegion(p, c) {
286
- if (p.x >= c.left && p.x <= c.right && p.y >= c.top && p.y <= c.bottom) return "center";
288
+ function rawRegion(p, c, sides) {
287
289
  let best = "center";
288
290
  let deepest = 0;
289
291
  for (const d of depths(p, c)) {
292
+ if (sides && !sides.includes(d.side)) continue;
290
293
  if (d.depth > deepest) {
291
294
  deepest = d.depth;
292
295
  best = d.side;
@@ -305,13 +308,388 @@ function toward(p, region, by, c) {
305
308
  if (len <= by || len === 0) return { x: c.width / 2, y: c.height / 2 };
306
309
  return { x: p.x + dx / len * by, y: p.y + dy / len * by };
307
310
  }
311
+ function paneRegionShapes(pane, options = {}) {
312
+ const c = options.paneCenter ?? paneCenter(pane, options);
313
+ const at = (x, y) => ({ x: pane.x + x, y: pane.y + y });
314
+ const [w, h] = [c.width, c.height];
315
+ const has2 = (s) => !options.sides || options.sides.includes(s);
316
+ const l = has2("left") ? c.left : 0;
317
+ const r = has2("right") ? c.right : w;
318
+ const t = has2("top") ? c.top : 0;
319
+ const b = has2("bottom") ? c.bottom : h;
320
+ return {
321
+ center: [at(l, t), at(r, t), at(r, b), at(l, b)],
322
+ top: has2("top") ? [at(0, 0), at(w, 0), has2("right") ? at(c.right, c.top) : at(w, c.top), has2("left") ? at(c.left, c.top) : at(0, c.top)] : [],
323
+ right: has2("right") ? [at(w, 0), at(w, h), has2("bottom") ? at(c.right, c.bottom) : at(c.right, h), has2("top") ? at(c.right, c.top) : at(c.right, 0)] : [],
324
+ bottom: has2("bottom") ? [at(w, h), at(0, h), has2("left") ? at(c.left, c.bottom) : at(0, c.bottom), has2("right") ? at(c.right, c.bottom) : at(w, c.bottom)] : [],
325
+ left: has2("left") ? [at(0, h), at(0, 0), has2("top") ? at(c.left, c.top) : at(c.left, 0), has2("bottom") ? at(c.left, c.bottom) : at(c.left, h)] : []
326
+ };
327
+ }
308
328
  function paneRegion(pane, point, previous = null, options = {}) {
309
329
  const c = options.paneCenter ?? paneCenter(pane, options);
310
330
  const p = { x: point.x - pane.x, y: point.y - pane.y };
311
- const raw = rawRegion(p, c);
331
+ const raw = rawRegion(p, c, options.sides);
312
332
  const by = Math.max(0, options.hysteresis ?? PANE_REGION_DEFAULTS.hysteresis);
313
333
  if (previous === null || by === 0 || previous === raw) return raw;
314
- return rawRegion(toward(p, previous, by, c), c) === previous ? previous : raw;
334
+ return rawRegion(toward(p, previous, by, c), c, options.sides) === previous ? previous : raw;
335
+ }
336
+
337
+ // src/tiling/hit.ts
338
+ var TILING_HIT_DEFAULTS = {
339
+ edge: 6,
340
+ travel: 24,
341
+ cap: 0.25,
342
+ neighborSwap: 0.56
343
+ };
344
+ var HOME = { kind: "home" };
345
+ var SIDES = ["top", "right", "bottom", "left"];
346
+ var inside = (r, p) => contains(r, p, 0);
347
+ var rectPoly = (r) => [
348
+ { x: r.x, y: r.y },
349
+ { x: r.x + r.width, y: r.y },
350
+ { x: r.x + r.width, y: r.y + r.height },
351
+ { x: r.x, y: r.y + r.height }
352
+ ];
353
+ function boxOf(poly) {
354
+ const xs = poly.map((p) => p.x);
355
+ const ys = poly.map((p) => p.y);
356
+ const x = Math.min(...xs);
357
+ const y = Math.min(...ys);
358
+ return { x, y, width: Math.max(...xs) - x, height: Math.max(...ys) - y };
359
+ }
360
+ function inConvex(poly, p) {
361
+ let pos = false;
362
+ let neg = false;
363
+ for (let i = 0; i < poly.length; i++) {
364
+ const a = poly[i];
365
+ const b = poly[(i + 1) % poly.length];
366
+ const cross2 = (b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x);
367
+ if (cross2 > 1e-9) pos = true;
368
+ else if (cross2 < -1e-9) neg = true;
369
+ if (pos && neg) return false;
370
+ }
371
+ return true;
372
+ }
373
+ var segDist = (p, a, b) => {
374
+ const dx = b.x - a.x;
375
+ const dy = b.y - a.y;
376
+ const len = dx * dx + dy * dy;
377
+ const t = len > 0 ? Math.max(0, Math.min(1, ((p.x - a.x) * dx + (p.y - a.y) * dy) / len)) : 0;
378
+ return Math.hypot(p.x - (a.x + t * dx), p.y - (a.y + t * dy));
379
+ };
380
+ function nearPoly(poly, p, ex, ey) {
381
+ if (inConvex(poly, p)) return true;
382
+ const sx = ex > 0 ? 1 / ex : 1e9;
383
+ const sy = ey > 0 ? 1 / ey : 1e9;
384
+ const q = { x: p.x * sx, y: p.y * sy };
385
+ for (let i = 0; i < poly.length; i++) {
386
+ const a = poly[i];
387
+ const b = poly[(i + 1) % poly.length];
388
+ if (segDist(q, { x: a.x * sx, y: a.y * sy }, { x: b.x * sx, y: b.y * sy }) <= 1) return true;
389
+ }
390
+ return false;
391
+ }
392
+ function clipHalf(poly, a, b, c) {
393
+ const out = [];
394
+ for (let i = 0; i < poly.length; i++) {
395
+ const p = poly[i];
396
+ const q = poly[(i + 1) % poly.length];
397
+ const fp = a * p.x + b * p.y - c;
398
+ const fq = a * q.x + b * q.y - c;
399
+ if (fp <= 0) out.push(p);
400
+ if (fp < 0 && fq > 0 || fp > 0 && fq < 0) {
401
+ const t = fp / (fp - fq);
402
+ out.push({ x: p.x + (q.x - p.x) * t, y: p.y + (q.y - p.y) * t });
403
+ }
404
+ }
405
+ return out;
406
+ }
407
+ var areaOf = (poly) => Math.abs(poly.reduce((s, p, i) => s + p.x * poly[(i + 1) % poly.length].y - poly[(i + 1) % poly.length].x * p.y, 0)) / 2;
408
+ function minusRect(poly, r) {
409
+ const [x0, x1, y0, y1] = [r.x, r.x + r.width, r.y, r.y + r.height];
410
+ const pieces = [
411
+ clipHalf(poly, 1, 0, x0),
412
+ clipHalf(poly, -1, 0, -x1),
413
+ clipHalf(clipHalf(clipHalf(poly, -1, 0, -x0), 1, 0, x1), 0, 1, y0),
414
+ clipHalf(clipHalf(clipHalf(poly, -1, 0, -x0), 1, 0, x1), 0, -1, -y1)
415
+ ];
416
+ return pieces.filter((q) => q.length >= 3 && areaOf(q) > 1e-6);
417
+ }
418
+ var half = (r, side) => side === "left" ? { x: r.x, y: r.y, width: r.width / 2, height: r.height } : side === "right" ? { x: r.x + r.width / 2, y: r.y, width: r.width / 2, height: r.height } : side === "top" ? { x: r.x, y: r.y, width: r.width, height: r.height / 2 } : { x: r.x, y: r.y + r.height / 2, width: r.width, height: r.height / 2 };
419
+ function createTilingHit(ctx, basis, options = {}) {
420
+ const { ops, lift, base, home, after } = ctx;
421
+ const key = options.key ?? String;
422
+ const edge = Math.max(0, options.edge ?? TILING_HIT_DEFAULTS.edge);
423
+ const travel = Math.max(0, options.travel ?? TILING_HIT_DEFAULTS.travel);
424
+ const cap = Math.max(0, options.cap ?? TILING_HIT_DEFAULTS.cap);
425
+ const neighborSwap = options.neighborSwap ?? TILING_HIT_DEFAULTS.neighborSwap;
426
+ const given = options.regions ?? {};
427
+ const regionOptions = {
428
+ ...given.center !== void 0 ? { center: given.center } : {},
429
+ ...given.centerX !== void 0 ? { centerX: given.centerX } : {},
430
+ ...given.centerY !== void 0 ? { centerY: given.centerY } : {},
431
+ ...given.centerMin !== void 0 ? { centerMin: given.centerMin } : {},
432
+ ...given.pixelRatio !== void 0 ? { pixelRatio: given.pixelRatio } : {},
433
+ hysteresis: 0
434
+ };
435
+ const laid = basis === "lift" ? lift : base;
436
+ const stackOf = (t, k) => ops.stackOf?.(t, k) ?? null;
437
+ const drawnAs = (t, k) => stackOf(t, k)?.active ?? k;
438
+ const subjectDrawn = drawnAs(lift.tree, ctx.subjectKey);
439
+ const atOf = (pane) => {
440
+ if (base.layout.has(pane)) return pane;
441
+ const members = stackOf(laid.tree, pane)?.members ?? [];
442
+ for (const m of members) {
443
+ if (ctx.inSubject(m)) continue;
444
+ const d = drawnAs(base.tree, m);
445
+ if (base.layout.has(d)) return d;
446
+ }
447
+ return null;
448
+ };
449
+ const byId = /* @__PURE__ */ new Map();
450
+ const homePlace = { id: "home", held: HOME, pane: null, pieces: home ? [{ poly: rectPoly(home), box: home, cuts: () => [] }] : [], owners: /* @__PURE__ */ new Set(["home"]), slot: null };
451
+ byId.set("home", homePlace);
452
+ const placeFor = (id, held, pane, owner, piece, slot = null) => {
453
+ let p = byId.get(id);
454
+ if (!p) byId.set(id, p = { id, held, pane, pieces: [], owners: /* @__PURE__ */ new Set(), slot });
455
+ p.pieces.push(piece);
456
+ p.owners.add(owner);
457
+ return p;
458
+ };
459
+ const liveMemo = /* @__PURE__ */ new Map();
460
+ const live = (p) => {
461
+ if (p === homePlace) return true;
462
+ let v = liveMemo.get(p.id);
463
+ if (v === void 0) liveMemo.set(p.id, v = after(p.held).refusal === null);
464
+ return v;
465
+ };
466
+ const unionOf = (node, layout) => {
467
+ const rs = panesOf(node).map((k) => layout.get(k)).filter((r) => !!r);
468
+ if (!rs.length) return null;
469
+ const x = Math.min(...rs.map((r) => r.x));
470
+ const y = Math.min(...rs.map((r) => r.y));
471
+ return { x, y, width: Math.max(...rs.map((r) => r.x + r.width)) - x, height: Math.max(...rs.map((r) => r.y + r.height)) - y };
472
+ };
473
+ const axisOf = (node, layout) => {
474
+ if (node.axis) return node.axis;
475
+ const a = unionOf(node.a, layout);
476
+ const b = unionOf(node.b, layout);
477
+ return a && b && (a.x + a.width <= b.x + 0.5 || b.x + b.width <= a.x + 0.5) ? "x" : "y";
478
+ };
479
+ const runOf = (node, axis, layout) => "pane" in node || node.master || axisOf(node, layout) !== axis ? [node] : [...runOf(node.a, axis, layout), ...runOf(node.b, axis, layout)];
480
+ const arrangementOf = (t) => {
481
+ const layout = ctx.lay(t).layout;
482
+ const rec = (node) => {
483
+ if ("pane" in node) {
484
+ const s = stackOf(t, node.pane);
485
+ return s ? `[${s.members.map(String).join(",")}]` : String(node.pane);
486
+ }
487
+ if (node.master) return `m${JSON.stringify(node.master)}(${rec(node.a)}|${rec(node.b)})`;
488
+ const axis = axisOf(node, layout);
489
+ return `${axis}(${runOf(node, axis, layout).map(rec).join(",")})`;
490
+ };
491
+ const split = ops.split(t);
492
+ return split ? rec(split) : "";
493
+ };
494
+ const liftArrangement = arrangementOf(ops.normalize ? ops.normalize(lift.tree) : lift.tree);
495
+ const homeMemo = /* @__PURE__ */ new Map();
496
+ const homeLike = (p) => {
497
+ if (p === homePlace) return true;
498
+ let v = homeMemo.get(p.id);
499
+ if (v === void 0) {
500
+ v = live(p) && (after(p.held).tree === lift.tree || arrangementOf(after(p.held).tree) === liftArrangement);
501
+ homeMemo.set(p.id, v);
502
+ }
503
+ return v;
504
+ };
505
+ const goneSides = /* @__PURE__ */ new Map();
506
+ const liftSplit = ops.split(lift.tree);
507
+ if (home && liftSplit) {
508
+ const find = (node) => {
509
+ if ("pane" in node) return null;
510
+ if (node.master) return find(node.a) ?? find(node.b);
511
+ const axis = axisOf(node, lift.layout);
512
+ const run = runOf(node, axis, lift.layout);
513
+ const i = run.findIndex((e) => "pane" in e && e.pane === subjectDrawn);
514
+ if (i >= 0) return { axis, run, i };
515
+ for (const e of run) {
516
+ const f = find(e);
517
+ if (f) return f;
518
+ }
519
+ return null;
520
+ };
521
+ const found = find(liftSplit);
522
+ if (found) {
523
+ const { axis, run, i } = found;
524
+ for (const [j, facing, far] of [
525
+ [i - 1, axis === "x" ? "right" : "bottom", axis === "x" ? "left" : "top"],
526
+ [i + 1, axis === "x" ? "left" : "top", axis === "x" ? "right" : "bottom"]
527
+ ]) {
528
+ const e = run[j];
529
+ if (!e || !("pane" in e)) continue;
530
+ const r = lift.layout.get(e.pane);
531
+ const same = (a, b) => Math.abs(a - b) <= 0.5;
532
+ if (!r || (axis === "y" ? !same(r.x, home.x) || !same(r.width, home.width) : !same(r.y, home.y) || !same(r.height, home.height))) continue;
533
+ const swap = arrangementOf(ctx.changeOf({ kind: "swap", at: e.pane }));
534
+ const sides = [facing, far].filter((side) => {
535
+ const a = arrangementOf(ctx.changeOf({ kind: "beside", at: e.pane, side }));
536
+ return a === liftArrangement || a === swap;
537
+ });
538
+ if (sides.length) goneSides.set(e.pane, { axis, sides });
539
+ }
540
+ }
541
+ }
542
+ const targets = [];
543
+ const panes = [];
544
+ if (home) targets.push({ id: "home", priority: 1, hit: { ...home }, pane: subjectDrawn, at: subjectDrawn, home: true });
545
+ for (const [pane, rect] of laid.layout) {
546
+ if (basis === "lift" && home && pane === subjectDrawn) continue;
547
+ const at = atOf(pane);
548
+ if (at === null || ctx.still?.(at)) continue;
549
+ const k = key(at);
550
+ const body = { id: `pane:${key(pane)}`, priority: 2, hit: { ...rect }, pane, at };
551
+ const parts = [];
552
+ const regions = {};
553
+ const gone = goneSides.get(lift.layout.has(pane) ? pane : at);
554
+ const paneOptions = gone ? { ...regionOptions, sides: SIDES.filter((s) => !gone.sides.includes(s)), ...gone.axis === "y" ? { centerX: neighborSwap } : { centerY: neighborSwap } } : regionOptions;
555
+ const ph = { body, parts, regions, options: paneOptions };
556
+ const members = stackOf(laid.tree, pane)?.members ?? [pane];
557
+ const indexOf = (i) => i < members.length ? members.slice(0, i).filter((m) => !ctx.inSubject(m)).length : void 0;
558
+ const seen = /* @__PURE__ */ new Set();
559
+ for (const part of ops.parts?.(laid.tree, pane, rect) ?? []) {
560
+ let pid = `part:${key(pane)}:${part.key}`;
561
+ for (let n = 2; seen.has(pid); n++) pid = `part:${key(pane)}:${part.key}#${n}`;
562
+ seen.add(pid);
563
+ const target = { id: pid, priority: 1, hit: { ...part.rect }, pane, at, part: part.key };
564
+ targets.push(target);
565
+ const slotRects = part.slots ?? [];
566
+ const slots = slotRects.map((s, i) => {
567
+ const index = indexOf(i);
568
+ const held = index === void 0 ? { kind: "join", at } : { kind: "join", at, index };
569
+ const place = placeFor(`join:${k}:${index ?? ""}`, held, ph, pid, { poly: rectPoly(s), box: s, cuts: () => home ? [home] : [] }, s);
570
+ return { rect: s, place };
571
+ });
572
+ const whole = placeFor(`join:${k}:`, { kind: "join", at }, ph, pid, {
573
+ poly: rectPoly(part.rect),
574
+ box: part.rect,
575
+ cuts: () => [...home ? [home] : [], ...slots.filter((s) => live(s.place)).map((s) => s.rect)]
576
+ });
577
+ parts.push({ target, place: whole, slots });
578
+ }
579
+ const shapes = paneRegionShapes(rect, paneOptions);
580
+ const above = () => [
581
+ ...home ? [home] : [],
582
+ ...parts.flatMap((p) => live(p.place) ? [p.target.hit] : p.slots.filter((s) => live(s.place)).map((s) => s.rect))
583
+ ];
584
+ for (const region of ["center", ...SIDES]) {
585
+ if (region !== "center" && paneOptions.sides && !paneOptions.sides.includes(region)) continue;
586
+ const held = region === "center" ? { kind: "swap", at } : { kind: "beside", at, side: region };
587
+ const id = region === "center" ? `swap:${k}` : `beside:${region}:${k}`;
588
+ regions[region] = placeFor(id, held, ph, body.id, { poly: shapes[region], box: boxOf(shapes[region]), cuts: above });
589
+ }
590
+ targets.push(body);
591
+ panes.push(ph);
592
+ }
593
+ Object.freeze(targets);
594
+ const rawAt = (p) => {
595
+ if (home && inside(home, p)) return { place: homePlace, refused: null };
596
+ const pane = panes.find((ph) => inside(ph.body.hit, p));
597
+ if (!pane) return { place: null, refused: null };
598
+ let refused = null;
599
+ for (const part of pane.parts) {
600
+ if (!inside(part.target.hit, p)) continue;
601
+ const slot = part.slots.find((s) => inside(s.rect, p));
602
+ if (slot) {
603
+ if (live(slot.place)) return { place: slot.place, refused: null };
604
+ refused ??= slot.place;
605
+ }
606
+ if (live(part.place)) return { place: part.place, refused: null };
607
+ refused ??= part.place;
608
+ }
609
+ const place = pane.regions[paneRegion(pane.body.hit, p, null, pane.options)];
610
+ return live(place) ? { place, refused: null } : { place: null, refused: refused ?? place };
611
+ };
612
+ const effective = (p) => p && homeLike(p) ? homePlace : p;
613
+ const placeAt = (p) => effective(rawAt(p).place)?.id ?? null;
614
+ let homeLikeAll = null;
615
+ const homePieces = () => {
616
+ homeLikeAll ??= [...byId.values()].filter((p) => p !== homePlace && homeLike(p));
617
+ return [...homePlace.pieces, ...homeLikeAll.flatMap((p) => p.pieces)];
618
+ };
619
+ const capped = (px4, extent) => Math.min(px4, cap * extent);
620
+ const visibleMemo = /* @__PURE__ */ new Map();
621
+ const visibleOf = (piece) => {
622
+ let v = visibleMemo.get(piece);
623
+ if (!v) visibleMemo.set(piece, v = piece.cuts().reduce((polys, r) => polys.flatMap((q) => minusRect(q, r)), [[...piece.poly]]));
624
+ return v;
625
+ };
626
+ const near2 = (place, p) => (place === homePlace ? homePieces() : place.pieces).some((piece) => {
627
+ const ex = capped(edge, piece.box.width);
628
+ const ey = capped(edge, piece.box.height);
629
+ return nearPoly(piece.poly, p, ex, ey) && visibleOf(piece).some((q) => nearPoly(q, p, ex, ey));
630
+ });
631
+ const keepsByTravel = (place, from, p) => {
632
+ if (!place.pane || !inside(place.pane.body.hit, p) || home && inside(home, p)) return false;
633
+ const box = boxOf(place.pieces.flatMap((piece) => rectPoly(piece.box)));
634
+ const rx = capped(travel, box.width);
635
+ const ry = capped(travel, box.height);
636
+ if (rx <= 0 || ry <= 0) return false;
637
+ return ((p.x - from.x) / rx) ** 2 + ((p.y - from.y) / ry) ** 2 < 1;
638
+ };
639
+ const hold = (state2, p) => {
640
+ const now = effective(rawAt(p).place);
641
+ if (state2) {
642
+ if (now && now.id === state2.place) return state2;
643
+ const held = byId.get(state2.place);
644
+ if (held && (near2(held, p) || keepsByTravel(held, state2.from, p))) return state2;
645
+ }
646
+ return now ? { place: now.id, from: { x: p.x, y: p.y } } : null;
647
+ };
648
+ let state = null;
649
+ let last = null;
650
+ const current = (p) => {
651
+ if (!last || last.x !== p.x || last.y !== p.y) {
652
+ state = hold(state, p);
653
+ last = { x: p.x, y: p.y };
654
+ }
655
+ return state;
656
+ };
657
+ const placeOf = (target, p) => {
658
+ const s = current(p);
659
+ return s && byId.get(s.place)?.owners.has(target.id) ? s.place : null;
660
+ };
661
+ const markOf = (place) => {
662
+ const r = place.pane?.body.hit;
663
+ const h = place.held;
664
+ if (!r || h.kind === "home") return null;
665
+ if (h.kind === "beside") return { kind: "beside", at: h.at, side: h.side, rect: half(r, h.side) };
666
+ if (h.kind === "swap") return { kind: "swap", at: h.at, rect: { ...r }, wash: home ? { ...home } : null };
667
+ return { kind: "join", at: h.at, rect: { ...r }, slot: place.slot ? { ...place.slot } : null };
668
+ };
669
+ const read = (frame, p) => {
670
+ const s = current(p);
671
+ const place = s ? byId.get(s.place) ?? null : null;
672
+ const shown = place && place !== homePlace && frame.shown && place.owners.has(frame.shown.id) ? place : null;
673
+ const held = place === homePlace ? HOME : shown?.held ?? null;
674
+ const refused = held ? null : rawAt(p).refused?.held ?? null;
675
+ return { held, refused, mark: basis === "lift" && shown ? markOf(shown) : null };
676
+ };
677
+ return {
678
+ basis,
679
+ home: home ? { ...home } : null,
680
+ targets,
681
+ intent: {
682
+ tuning: { aimSpeed: Infinity, dwell: 0, switchHold: 0, roomAfter: 0, band: edge, bandShare: 1, breakthrough: 0, gapGrace: 0 },
683
+ placeOf: (t, p) => placeOf(t, p)
684
+ },
685
+ placeAt,
686
+ hold,
687
+ heldOf: (id) => byId.get(id)?.held ?? null,
688
+ placeOf: (t, p) => placeOf(t, p),
689
+ read,
690
+ drawn: (r) => basis === "lift" ? lift : r.held === null ? base : r.held.kind === "home" ? lift : after(r.held),
691
+ release: (r) => after(r.held ?? HOME)
692
+ };
315
693
  }
316
694
 
317
695
  // src/tiling/targets.ts
@@ -342,6 +720,871 @@ function giveWay(panes, layout, min, age) {
342
720
  }
343
721
  }
344
722
 
723
+ // src/tiling/drag.ts
724
+ 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;
725
+ var px3 = (v) => v !== void 0 && Number.isFinite(v) && v > 0 ? v : 0;
726
+ function tilingDrag(tree, subject, options) {
727
+ const { ops, area } = options;
728
+ const normalize = (t) => ops.normalize ? ops.normalize(t) : t;
729
+ const lay2 = (t) => {
730
+ const { panes, splits } = layoutSplitTree(ops.split(t), area, options);
731
+ return { tree: t, layout: panes, splits };
732
+ };
733
+ const stackOf = (t, pane) => ops.stackOf?.(t, pane) ?? null;
734
+ const drawnAs = (t, pane) => stackOf(t, pane)?.active ?? pane;
735
+ const subjectKey = subject.kind === "pane" ? subject.pane : subject.stack.active;
736
+ const lift = lay2(tree);
737
+ const liftRect = lift.layout.get(drawnAs(tree, subjectKey));
738
+ if (!liftRect) {
739
+ const what = subject.kind === "pane" ? `pane ${String(subject.pane)}` : `stack ${subject.stack.id}`;
740
+ throw new Error(`tilingDrag: the subject (${what}) is not in the tree: it is neither drawn nor a member of a drawn stack`);
741
+ }
742
+ const base = lay2(ops.release(tree, subject));
743
+ const takesPane = panesOf(ops.split(base.tree)).length < panesOf(ops.split(tree)).length;
744
+ const home = takesPane ? { ...liftRect } : null;
745
+ const movedIn = (laid) => {
746
+ const own = drawnAs(laid.tree, subjectKey);
747
+ const out = [];
748
+ for (const [pane, to] of laid.layout) {
749
+ if (pane === own) continue;
750
+ const from = lift.layout.get(lift.layout.has(pane) ? pane : drawnAs(tree, pane));
751
+ if (from && !sameRect(from, to)) out.push({ pane, from, to });
752
+ }
753
+ return out;
754
+ };
755
+ const inSubject = (pane) => subject.kind === "pane" ? pane === subject.pane : subject.stack.members.includes(pane);
756
+ const refusalBefore = (held) => {
757
+ const at = held.at;
758
+ if (inSubject(at)) return { code: "self" };
759
+ if (held.kind === "swap") {
760
+ const own = stackOf(tree, subjectKey);
761
+ if (own && stackOf(tree, at)?.id === own.id) return { code: "self" };
762
+ }
763
+ if (!base.layout.has(at)) return { code: "missing" };
764
+ if (options.still?.(at)) return { code: "still" };
765
+ if (held.kind === "join" && !ops.join) return { code: "join" };
766
+ return null;
767
+ };
768
+ const refused = (held, refusal) => ({ ...lift, held, refusal, landing: null, moved: [] });
769
+ const roomFor = (at, side) => {
770
+ const r = base.layout.get(at);
771
+ if (canSplitPane(r, side, options).ok) return null;
772
+ const along = side === "left" || side === "right";
773
+ const gutter = px3(options.gap) + px3(options.handle);
774
+ const min = px3(options.min);
775
+ const span = along ? r.width : r.height;
776
+ 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 };
777
+ };
778
+ const make = (held) => {
779
+ if (held.kind === "home") return { ...lift, held, refusal: null, landing: liftRect, moved: [] };
780
+ const before = refusalBefore(held) ?? (held.kind === "beside" ? roomFor(held.at, held.side) : null);
781
+ if (before) return refused(held, before);
782
+ 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);
783
+ const laid = lay2(normalize(changed));
784
+ const own = drawnAs(laid.tree, subjectKey);
785
+ const partner = drawnAs(laid.tree, held.at);
786
+ if (!dropFits(laid.layout, own, partner, area, { min: options.min, side: held.kind === "beside" ? held.side : "left" }).ok) {
787
+ const small = (r) => !r || r.width < px3(options.min) || r.height < px3(options.min);
788
+ return refused(held, { code: "fit", pane: small(laid.layout.get(own)) ? own : partner });
789
+ }
790
+ return { ...laid, held, refusal: null, landing: laid.layout.get(own) ?? null, moved: movedIn(laid) };
791
+ };
792
+ const homeAfter = make({ kind: "home" });
793
+ const memo = /* @__PURE__ */ new Map();
794
+ const after = (held) => {
795
+ if (held.kind === "home") return homeAfter;
796
+ const k = held.kind === "beside" ? `beside:${held.side}` : held.kind === "join" ? `join:${held.index ?? ""}` : "swap";
797
+ let byAt = memo.get(k);
798
+ if (!byAt) memo.set(k, byAt = /* @__PURE__ */ new Map());
799
+ let a = byAt.get(held.at);
800
+ if (!a) byAt.set(held.at, a = make(held));
801
+ return a;
802
+ };
803
+ const changeOf = (held) => normalize(held.kind === "swap" ? ops.swap(tree, subject, held.at) : ops.beside(base.tree, subject, held.at, held.side, { kind: "half" }));
804
+ const hits = /* @__PURE__ */ new Map();
805
+ const hit = (basis, hitOptions) => {
806
+ let h = hits.get(basis);
807
+ if (!h) hits.set(basis, h = createTilingHit({ ops, lift, base, home, subjectKey, inSubject, ...options.still ? { still: options.still } : {}, after, lay: lay2, changeOf }, basis, hitOptions));
808
+ return h;
809
+ };
810
+ return { subject, lift, base, home, after, hit };
811
+ }
812
+
813
+ // src/tiling/reference.ts
814
+ function mapKeys(split, fn) {
815
+ if (!split) return split;
816
+ if ("pane" in split) {
817
+ const k = fn(split.pane);
818
+ return k === split.pane ? split : { pane: k };
819
+ }
820
+ const a = mapKeys(split.a, fn);
821
+ const b = mapKeys(split.b, fn);
822
+ return a === split.a && b === split.b ? split : { ...split, a, b };
823
+ }
824
+ var stacksOf = (t) => t.stacks ?? [];
825
+ var of = (split, stacks) => stacks.length ? { split, stacks } : { split };
826
+ var membersOf = (s) => s.kind === "pane" ? [s.pane] : s.stack.members;
827
+ var drawnOf = (s) => s.kind === "pane" ? s.pane : s.stack.active;
828
+ function splitTreeOps(options = {}) {
829
+ const bar = options.bar ?? 0;
830
+ const tab = options.tab ?? (() => 80);
831
+ return {
832
+ ...bar > 0 ? {
833
+ parts(t, pane, r) {
834
+ const rect = { x: r.x, y: r.y, width: r.width, height: Math.min(bar, r.height) };
835
+ const stack = stacksOf(t).find((s) => s.members.includes(pane));
836
+ if (!stack) return [{ key: "bar", rect }];
837
+ const slots = [];
838
+ let x = rect.x;
839
+ for (const m of stack.members) {
840
+ const width = Math.max(0, Math.min(tab(m), rect.x + rect.width - x));
841
+ slots.push({ x, y: rect.y, width, height: rect.height });
842
+ x += width;
843
+ }
844
+ return [{ key: "bar", rect, slots }];
845
+ }
846
+ } : {},
847
+ split: (t) => t.split,
848
+ stackOf: (t, pane) => stacksOf(t).find((s) => s.members.includes(pane)) ?? null,
849
+ release(t, s) {
850
+ if (s.kind === "stack") {
851
+ const stack2 = stacksOf(t).find((x) => x.id === s.stack.id);
852
+ return of(removePane(t.split, stack2?.active ?? s.stack.active), stacksOf(t).filter((x) => x !== stack2));
853
+ }
854
+ const stack = stacksOf(t).find((x) => x.members.includes(s.pane));
855
+ if (!stack) return of(removePane(t.split, s.pane), stacksOf(t));
856
+ const i = stack.members.indexOf(s.pane);
857
+ const members = stack.members.filter((m) => m !== s.pane);
858
+ if (members.length === 0) return of(removePane(t.split, s.pane), stacksOf(t).filter((x) => x !== stack));
859
+ const active = stack.active === s.pane ? members[i] ?? members[i - 1] : stack.active;
860
+ const was = stack.active;
861
+ return of(
862
+ mapKeys(t.split, (k) => k === was ? active : k),
863
+ stacksOf(t).map((x) => x === stack ? { id: stack.id, members, active } : x)
864
+ );
865
+ },
866
+ beside(released, s, at, side) {
867
+ const split = splitPane(released.split, at, drawnOf(s), side);
868
+ return of(split, s.kind === "pane" ? stacksOf(released) : [...stacksOf(released), s.stack]);
869
+ },
870
+ swap(t, s, at) {
871
+ if (s.kind === "stack") {
872
+ const x2 = stacksOf(t).find((st) => st.id === s.stack.id)?.active ?? s.stack.active;
873
+ return of(mapKeys(t.split, (k) => k === x2 ? at : k === at ? x2 : k), stacksOf(t));
874
+ }
875
+ const x = s.pane;
876
+ const trade = (k) => k === x ? at : k === at ? x : k;
877
+ return of(
878
+ mapKeys(t.split, trade),
879
+ stacksOf(t).map((st) => ({ id: st.id, members: st.members.map(trade), active: trade(st.active) }))
880
+ );
881
+ },
882
+ join(released, s, at, index) {
883
+ const moving = membersOf(s);
884
+ const target = stacksOf(released).find((x) => x.members.includes(at));
885
+ const into = target ? target.members : [at];
886
+ const i = Math.max(0, Math.min(into.length, index ?? into.length));
887
+ const members = [...into.slice(0, i), ...moving, ...into.slice(i)];
888
+ const active = moving[0];
889
+ const drawn = target ? target.active : at;
890
+ const ids = new Set(stacksOf(released).map((x) => x.id));
891
+ let id = target?.id ?? `stack:${String(at)}`;
892
+ for (let n = 2; !target && ids.has(id); n++) id = `stack:${String(at)}:${n}`;
893
+ const stack = { id, members, active };
894
+ return of(
895
+ mapKeys(released.split, (k) => k === drawn ? active : k),
896
+ target ? stacksOf(released).map((x) => x === target ? stack : x) : [...stacksOf(released), stack]
897
+ );
898
+ },
899
+ normalize(t) {
900
+ const kept = stacksOf(t).filter((s) => s.members.length >= 2);
901
+ return kept.length === stacksOf(t).length ? t : of(t.split, kept);
902
+ }
903
+ };
904
+ }
905
+
906
+ // src/tiling/conformance.ts
907
+ var TILING_PROPERTIES = [
908
+ /** `after(home).tree` is the lift tree itself, its layout the lift's, never normalized. */
909
+ "home-is-lift",
910
+ /** `home` is the subject's lift rect when the release takes a drawn pane away, else null. */
911
+ "home-is-the-gap",
912
+ /** Home lies only over base panes whose rect changed when the subject was released. */
913
+ "home-over-changed",
914
+ /** `after(held).layout` is `after(held).tree` laid out; the landing is the subject's rect there; a refused place is the lift. */
915
+ "one-layout",
916
+ /** 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. */
917
+ "swap-exchanges",
918
+ /** 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. */
919
+ "swap-trades-slots",
920
+ /** A pane swapped with a stack's drawn member takes its place in that stack, and the stack's other members stay, in order; a member torn out of a stack leaves the partner in its place there. */
921
+ "swap-keeps-stack-mates",
922
+ /**
923
+ * The subject is in every tree exactly once: no key (a drawn pane, or a member of a stack) is in an `after(held).tree`
924
+ * twice; the base holds none of the subject's members (a torn member's stack mates stay); and every `after(held).tree`
925
+ * holds the base's keys and the subject's, and no other. Catches an adapter whose `release` leaves the subject in.
926
+ */
927
+ "subject-once",
928
+ /** `beside` and `join` commit `normalize(change(release(tree)))`: the subject is released first, then placed. */
929
+ "release-first",
930
+ /** A whole stack moves as one: after any change its members are in one stack, in their order. */
931
+ "stack-moves-whole",
932
+ /** `normalize` changes nothing on any `after(held).tree`. */
933
+ "normalize-idempotent",
934
+ /** The drag never changes the tree it is given, and remembers each held place (the same place, the same object). */
935
+ "nothing-changes"
936
+ ];
937
+ function sameData(a, b) {
938
+ if (Object.is(a, b)) return true;
939
+ if (typeof a !== "object" || typeof b !== "object" || a === null || b === null) return false;
940
+ if (a instanceof Map || b instanceof Map) {
941
+ if (!(a instanceof Map && b instanceof Map) || a.size !== b.size) return false;
942
+ for (const [k, v] of a) if (!b.has(k) || !sameData(v, b.get(k))) return false;
943
+ return true;
944
+ }
945
+ if (a instanceof Set || b instanceof Set) {
946
+ if (!(a instanceof Set && b instanceof Set) || a.size !== b.size) return false;
947
+ for (const v of a) if (!b.has(v)) return false;
948
+ return true;
949
+ }
950
+ if (Array.isArray(a) !== Array.isArray(b)) return false;
951
+ const ka = Object.keys(a).filter((k) => a[k] !== void 0);
952
+ const kb = Object.keys(b).filter((k) => b[k] !== void 0);
953
+ if (ka.length !== kb.length) return false;
954
+ return ka.every((k) => sameData(a[k], b[k]));
955
+ }
956
+ function copy(v) {
957
+ if (typeof v !== "object" || v === null) return v;
958
+ if (v instanceof Map) return new Map([...v].map(([k, x]) => [k, copy(x)]));
959
+ if (v instanceof Set) return new Set([...v].map(copy));
960
+ if (Array.isArray(v)) return v.map(copy);
961
+ return Object.fromEntries(Object.entries(v).map(([k, x]) => [k, copy(x)]));
962
+ }
963
+ var near = (a, b, by = 0.5) => !!a && !!b && Math.abs(a.x - b.x) <= by && Math.abs(a.y - b.y) <= by && Math.abs(a.width - b.width) <= by && Math.abs(a.height - b.height) <= by;
964
+ function pathOf(split, key) {
965
+ if (!split) return null;
966
+ if ("pane" in split) return split.pane === key ? [] : null;
967
+ for (const side of ["a", "b"]) {
968
+ const rest = pathOf(split[side], key);
969
+ if (rest) return [side, ...rest];
970
+ }
971
+ return null;
972
+ }
973
+ function paneAt(split, path) {
974
+ let node = split;
975
+ for (const side of path) {
976
+ if (!node || "pane" in node) return void 0;
977
+ node = node[side];
978
+ }
979
+ return node && "pane" in node ? node.pane : void 0;
980
+ }
981
+ var sameList = (a, b) => a.length === b.length && a.every((k, i) => Object.is(k, b[i]));
982
+ 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}` : ""}`;
983
+ function tilingSubjects(tree, options) {
984
+ const { ops } = options;
985
+ const out = [];
986
+ for (const pane of panesOf(ops.split(tree))) {
987
+ const stack = ops.stackOf?.(tree, pane) ?? null;
988
+ if (!stack) out.push({ kind: "pane", pane });
989
+ else out.push({ kind: "stack", stack }, ...stack.members.map((m) => ({ kind: "pane", pane: m })));
990
+ }
991
+ return out;
992
+ }
993
+ function propertyTally(properties, limit) {
994
+ const counts = Object.fromEntries(properties.map((p) => [p, { checked: 0, failed: 0 }]));
995
+ const failures = [];
996
+ const checkFor = (name) => (p, ok, why) => {
997
+ counts[p].checked++;
998
+ if (ok) return;
999
+ counts[p].failed++;
1000
+ if (failures.length < limit) failures.push(`${p}: ${name}: ${why()}`);
1001
+ };
1002
+ return { counts, failures, checkFor };
1003
+ }
1004
+ function tilingConformance(trees, options, limit = 20) {
1005
+ const { ops, area } = options;
1006
+ const { counts, failures, checkFor } = propertyTally(TILING_PROPERTIES, limit);
1007
+ const norm = (t) => ops.normalize ? ops.normalize(t) : t;
1008
+ const lay2 = (t) => layoutSplitTree(ops.split(t), area, options).panes;
1009
+ const drawnAs = (t, k) => ops.stackOf?.(t, k)?.active ?? k;
1010
+ const callbacks = !!(options.fixed || options.fixedAcross || options.floor);
1011
+ const keysOf = (t) => panesOf(ops.split(t)).flatMap((p) => ops.stackOf?.(t, p)?.members ?? [p]);
1012
+ const twice = (keys) => keys.filter((k, i) => keys.indexOf(k) !== i);
1013
+ let n = 0;
1014
+ for (const tree of trees) {
1015
+ const snapshot = copy(tree);
1016
+ for (const subject of tilingSubjects(tree, options)) {
1017
+ const check = checkFor(`tree ${n}, ${subject.kind === "pane" ? `pane ${String(subject.pane)}` : `stack ${subject.stack.id}`}`);
1018
+ const drag = tilingDrag(tree, subject, options);
1019
+ const key = subject.kind === "pane" ? subject.pane : subject.stack.active;
1020
+ const liftOf = (k) => drag.lift.layout.get(drag.lift.layout.has(k) ? k : drawnAs(tree, k));
1021
+ const subjectLift = liftOf(key);
1022
+ const members = subject.kind === "pane" ? [subject.pane] : subject.stack.members;
1023
+ const baseKeys = keysOf(drag.base.tree);
1024
+ const kept = baseKeys.filter((k) => members.includes(k));
1025
+ check("subject-once", kept.length === 0 && twice(baseKeys).length === 0, () => `the base holds ${kept.length ? `the subject's ${kept.map(String).join(", ")}` : `${twice(baseKeys).map(String).join(", ")} twice`}`);
1026
+ const want = /* @__PURE__ */ new Set([...baseKeys, ...members]);
1027
+ const home = drag.after({ kind: "home" });
1028
+ 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");
1029
+ const takes = panesOf(ops.split(drag.base.tree)).length < panesOf(ops.split(tree)).length;
1030
+ 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`);
1031
+ if (drag.home) {
1032
+ for (const [pane, r] of drag.base.layout) {
1033
+ if (overlap(drag.home, r) <= EPS) continue;
1034
+ check("home-over-changed", !near(r, liftOf(pane), EPS), () => `home lies over ${String(pane)}, which the release left at ${JSON.stringify(r)}`);
1035
+ }
1036
+ }
1037
+ const held = [{ kind: "home" }];
1038
+ for (const at of drag.base.layout.keys()) {
1039
+ held.push({ kind: "swap", at }, ...PANE_SIDES.map((side) => ({ kind: "beside", at, side })));
1040
+ if (ops.join) held.push({ kind: "join", at }, { kind: "join", at, index: 0 });
1041
+ }
1042
+ for (const h of held) {
1043
+ const a = drag.after(h);
1044
+ check("nothing-changes", drag.after(h) === a, () => `${show(h)}: after() gave a new object for the same place`);
1045
+ check("normalize-idempotent", sameData(norm(a.tree), a.tree), () => `${show(h)}: normalize changes after().tree`);
1046
+ const keys = keysOf(a.tree);
1047
+ const extra = keys.filter((k) => !want.has(k));
1048
+ const lost = [...want].filter((k) => !keys.includes(k));
1049
+ check(
1050
+ "subject-once",
1051
+ twice(keys).length === 0 && extra.length === 0 && lost.length === 0,
1052
+ () => `${show(h)}: after().tree holds ${[twice(keys).length ? `${twice(keys).map(String).join(", ")} twice` : "", extra.length ? `${extra.map(String).join(", ")} it should not` : "", lost.length ? `no ${lost.map(String).join(", ")}` : ""].filter(Boolean).join("; ")}`
1053
+ );
1054
+ if (a.refusal) {
1055
+ check("one-layout", a.tree === tree && a.landing === null && a.moved.length === 0, () => `${show(h)}: refused (${a.refusal.code}) but not the lift`);
1056
+ continue;
1057
+ }
1058
+ const own = drawnAs(a.tree, key);
1059
+ 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`);
1060
+ if (h.kind === "swap" && !callbacks) {
1061
+ const partner = drawnAs(a.tree, h.at);
1062
+ const exchanged = near(a.layout.get(partner), subjectLift) && near(a.layout.get(own), liftOf(h.at));
1063
+ const others = [...a.layout].every(([p, r]) => p === own || p === partner || near(r, liftOf(p)));
1064
+ check("swap-exchanges", exchanged && others, () => `${show(h)}: partner at ${JSON.stringify(a.layout.get(partner))} (subject was ${JSON.stringify(subjectLift)}), subject at ${JSON.stringify(a.layout.get(own))}${others ? "" : ", and another pane moved"}`);
1065
+ }
1066
+ if (h.kind === "swap") {
1067
+ const liftSplit = ops.split(tree);
1068
+ const afterSplit = ops.split(a.tree);
1069
+ const subjectSlot = pathOf(liftSplit, drawnAs(tree, key));
1070
+ const partnerSlot = pathOf(liftSplit, h.at);
1071
+ const partnerThere = !!subjectSlot && paneAt(afterSplit, subjectSlot) === drawnAs(a.tree, h.at);
1072
+ const subjectThere = !!partnerSlot && paneAt(afterSplit, partnerSlot) === drawnAs(a.tree, key);
1073
+ check("swap-trades-slots", partnerThere && subjectThere, () => `${show(h)}: ${partnerThere ? "" : `the partner is not in the subject's old slot (${String(subjectSlot && paneAt(afterSplit, subjectSlot))} is)`}${partnerThere || subjectThere ? "" : "; "}${subjectThere ? "" : `the subject is not in the partner's old slot (${String(partnerSlot && paneAt(afterSplit, partnerSlot))} is)`}`);
1074
+ if (subject.kind === "pane" && ops.stackOf) {
1075
+ for (const [from, to] of [
1076
+ [h.at, subject.pane],
1077
+ [subject.pane, h.at]
1078
+ ]) {
1079
+ const was = ops.stackOf(tree, from);
1080
+ if (!was) continue;
1081
+ const now = ops.stackOf(a.tree, to);
1082
+ const expect = was.members.map((m) => m === from ? to : m);
1083
+ check("swap-keeps-stack-mates", !!now && sameList(now.members, expect), () => `${show(h)}: ${String(to)} is in ${now ? `[${now.members.map(String).join(", ")}]` : "no stack"}, expected [${expect.map(String).join(", ")}]`);
1084
+ }
1085
+ }
1086
+ }
1087
+ if (h.kind === "beside" || h.kind === "join") {
1088
+ const released = ops.release(tree, subject);
1089
+ const changed = h.kind === "beside" ? ops.beside(released, subject, h.at, h.side, { kind: "half" }) : ops.join(released, subject, h.at, h.index);
1090
+ check("release-first", sameData(a.tree, norm(changed)), () => `${show(h)}: after().tree is not normalize(change(release(tree)))`);
1091
+ }
1092
+ if (subject.kind === "stack" && h.kind !== "home") {
1093
+ const ms = subject.stack.members;
1094
+ const st = ops.stackOf?.(a.tree, ms[0]) ?? null;
1095
+ const i = st ? st.members.indexOf(ms[0]) : -1;
1096
+ const whole = !!st && ms.every((m, j) => st.members[i + j] === m);
1097
+ check("stack-moves-whole", whole, () => `${show(h)}: the stack's members are not together in one stack`);
1098
+ }
1099
+ }
1100
+ check("nothing-changes", sameData(tree, snapshot), () => "the drag changed the tree it was given");
1101
+ }
1102
+ n++;
1103
+ }
1104
+ return { ok: failures.length === 0 && Object.values(counts).every((c) => c.failed === 0), failures, counts };
1105
+ }
1106
+
1107
+ // src/tiling/hit-conformance.ts
1108
+ var TILING_HIT_PROPERTIES = [
1109
+ /** The targets after any sequence of frames are the ones built at the lift; with nothing held the place at a point is the same before and after holding every other place. */
1110
+ "fixed-map",
1111
+ /** The place at a point is the one a second copy of the rule names: home inside its rect, a part over the body, a slot over its part, else the pane's region; a refused zone falls to the one beneath; a place that gives the lift back is home. */
1112
+ "zone-rule",
1113
+ /** A still pointer changes nothing: the same point again reads the same frame. */
1114
+ "stillness",
1115
+ /**
1116
+ * A path within the edge measure of the held zone's visible part, or inside its pane (not over home) within the travel
1117
+ * measure of where it was taken, never changes the place; and a point past both (over home's rim on the base included,
1118
+ * where a zone runs on beneath home) reads the zone map.
1119
+ */
1120
+ "hold-measures",
1121
+ /** A straight sweep meets each place but home (which shows nothing) at most once. */
1122
+ "one-pass",
1123
+ /** 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. */
1124
+ "existence",
1125
+ /** Every place whose release gives the lift back reads as home, and its release commits the lift tree. */
1126
+ "lift-equals-home",
1127
+ /** 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). */
1128
+ "shown-under-pointer",
1129
+ /** At every frame of a gesture a release commits `after(held)`, else the lift tree; the base basis draws `after(held)` for a held place, the lift at home, the base with nothing held. */
1130
+ "shown-equals-landed",
1131
+ /**
1132
+ * Among panes with the subject's bounds across its run, a side takes an insert exactly when that insert gives a new
1133
+ * arrangement, neither the lift's nor the swap's; a pane with other bounds across (a fixed length) keeps its sides. A
1134
+ * second copy judges it from the lift tree and the lift rects only, never the basis, and both bases are checked
1135
+ * against it, so a pane has the same sides on both.
1136
+ */
1137
+ "new-arrangement-sides",
1138
+ /** Every zone of every pane is convex: the midpoint of two points in one zone is in it too. */
1139
+ "zones-convex",
1140
+ /**
1141
+ * Room does not move the map: the same drag with every room check passing (`min: 0`) has the same sides on every
1142
+ * pane, every point reads the same place where both are live, and where a place is refused the point falls to the
1143
+ * target beneath (nothing, or a part's pane body), never to a neighboring zone.
1144
+ */
1145
+ "room-free-map"
1146
+ ];
1147
+ function seeded(seed) {
1148
+ let s = seed >>> 0 || 1;
1149
+ return () => {
1150
+ s = s + 1831565813 >>> 0;
1151
+ let t = s;
1152
+ t = Math.imul(t ^ t >>> 15, t | 1);
1153
+ t ^= t + Math.imul(t ^ t >>> 7, t | 61);
1154
+ return ((t ^ t >>> 14) >>> 0) / 4294967296;
1155
+ };
1156
+ }
1157
+ var inside2 = (r, p) => contains(r, p, 0);
1158
+ 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;
1159
+ 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;
1160
+ 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}` : ""}`;
1161
+ var cross = (a, b, p) => (b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x);
1162
+ function inQuad(q, p) {
1163
+ const s = q.map((a, i) => cross(a, q[(i + 1) % q.length], p));
1164
+ return s.every((v) => v > 1e-9) || s.every((v) => v < -1e-9);
1165
+ }
1166
+ function tilingHitConformance(trees, options, hitOptions = {}, limit = 20) {
1167
+ const { ops, area } = options;
1168
+ const { counts, failures, checkFor } = propertyTally(TILING_HIT_PROPERTIES, limit);
1169
+ const reported = { swapsBeside: 0, insertsMoveOthers: 0, insertsMoveOthersBeside: 0 };
1170
+ const edge = hitOptions.edge ?? TILING_HIT_DEFAULTS.edge;
1171
+ const travel = hitOptions.travel ?? TILING_HIT_DEFAULTS.travel;
1172
+ const cap = hitOptions.cap ?? TILING_HIT_DEFAULTS.cap;
1173
+ const neighborSwap = hitOptions.neighborSwap ?? 0.56;
1174
+ const reg = { ...PANE_REGION_DEFAULTS, ...hitOptions.regions };
1175
+ const stackOf = (t, k) => ops.stackOf?.(t, k) ?? null;
1176
+ const drawnAs = (t, k) => stackOf(t, k)?.active ?? k;
1177
+ const keyOf = hitOptions.key ?? String;
1178
+ const sidesOnMap = (h, at) => ["top", "right", "bottom", "left"].filter((s) => h.heldOf(`beside:${s}:${keyOf(at)}`) !== null).join(",");
1179
+ let n = 0;
1180
+ for (const tree of trees) {
1181
+ for (const subject of tilingSubjects(tree, options)) {
1182
+ const drag = tilingDrag(tree, subject, options);
1183
+ const name = `tree ${n}, ${subject.kind === "pane" ? `pane ${String(subject.pane)}` : `stack ${subject.stack.id}`}`;
1184
+ for (const basis of ["lift", "base"]) {
1185
+ const check2 = checkFor(`${name}, ${basis}`);
1186
+ runOne(drag, subject, basis, check2, seeded(1 + n * 7919 + (basis === "base" ? 1 : 0)));
1187
+ const roomy = tilingDrag(tree, subject, { ...options, min: 0 });
1188
+ const [tight, loose] = [drag.hit(basis, hitOptions), roomy.hit(basis, hitOptions)];
1189
+ for (const t of tight.targets) {
1190
+ if (t.part || t.home) continue;
1191
+ check2("room-free-map", sidesOnMap(tight, t.at) === sidesOnMap(loose, t.at), () => `${String(t.at)} has sides ${sidesOnMap(tight, t.at)}, with all the room ${sidesOnMap(loose, t.at)}`);
1192
+ }
1193
+ const rnd = seeded(7 + n * 131 + (basis === "base" ? 1 : 0));
1194
+ const samples = [];
1195
+ for (let i = 0; i < 120; i++) samples.push({ x: area.x + rnd() * area.width, y: area.y + rnd() * area.height });
1196
+ for (const t of tight.targets) if (t.part) for (let i = 0; i < 4; i++) samples.push({ x: t.hit.x + rnd() * t.hit.width, y: t.hit.y + rnd() * t.hit.height });
1197
+ for (const p of samples) {
1198
+ const [z, wide] = [tight.placeAt(p), loose.placeAt(p)];
1199
+ if (z === wide) continue;
1200
+ const onPart = tight.targets.find((t) => t.part && inside2(t.hit, p));
1201
+ const h = z === null ? null : tight.heldOf(z);
1202
+ const beneath = z === null || !!onPart && !!h && h.kind !== "home" && h.kind !== "join" && h.at === onPart.at;
1203
+ const wideHeld = wide === null ? null : loose.heldOf(wide);
1204
+ const refusedHere = wideHeld === null || wideHeld.kind === "home" || drag.after(wideHeld).refusal !== null;
1205
+ check2("room-free-map", beneath && refusedHere, () => `at ${p.x.toFixed(1)},${p.y.toFixed(1)}: ${z}, with all the room ${wide}`);
1206
+ }
1207
+ }
1208
+ const [onLift, onBase] = [drag.hit("lift", hitOptions), drag.hit("base", hitOptions)];
1209
+ const check = checkFor(name);
1210
+ for (const t of onBase.targets) {
1211
+ if (t.part || t.home || !onLift.targets.some((u) => !u.part && !u.home && u.at === t.at)) continue;
1212
+ check("new-arrangement-sides", sidesOnMap(onLift, t.at) === sidesOnMap(onBase, t.at), () => `${String(t.at)} has ${sidesOnMap(onLift, t.at)} on the lift, ${sidesOnMap(onBase, t.at)} on the base`);
1213
+ }
1214
+ }
1215
+ n++;
1216
+ }
1217
+ return { ok: failures.length === 0 && Object.values(counts).every((c) => c.failed === 0), failures, counts, reported };
1218
+ function runOne(drag, subject, basis, check, rnd) {
1219
+ const hit = drag.hit(basis, hitOptions);
1220
+ const laid = basis === "lift" ? drag.lift : drag.base;
1221
+ const home = drag.home;
1222
+ const subjectKey = subject.kind === "pane" ? subject.pane : subject.stack.active;
1223
+ const inSubject = (k) => subject.kind === "pane" ? k === subject.pane : subject.stack.members.includes(k);
1224
+ const HOME2 = { kind: "home" };
1225
+ const targetsAtLift = hit.targets;
1226
+ const snapshot = JSON.stringify(hit.targets);
1227
+ const subjectDrawn = drawnAs(drag.lift.tree, subjectKey);
1228
+ const atOf = (pane) => {
1229
+ if (drag.base.layout.has(pane)) return pane;
1230
+ for (const m of stackOf(laid.tree, pane)?.members ?? []) {
1231
+ if (inSubject(m)) continue;
1232
+ const d = drawnAs(drag.base.tree, m);
1233
+ if (drag.base.layout.has(d)) return d;
1234
+ }
1235
+ return null;
1236
+ };
1237
+ const flatOf = (t) => {
1238
+ const placed = layoutSplitTree(ops.split(t), area, options).panes;
1239
+ const keysIn = (n2) => "pane" in n2 ? [n2.pane] : [...keysIn(n2.a), ...keysIn(n2.b)];
1240
+ const box = (n2) => {
1241
+ const rs = keysIn(n2).map((k) => placed.get(k)).filter((r) => !!r);
1242
+ return { x0: Math.min(...rs.map((r) => r.x)), x1: Math.max(...rs.map((r) => r.x + r.width)) };
1243
+ };
1244
+ const dir = (n2) => {
1245
+ if (n2.axis) return n2.axis;
1246
+ const [a, b] = [box(n2.a), box(n2.b)];
1247
+ return a.x1 <= b.x0 + 0.5 || b.x1 <= a.x0 + 0.5 ? "x" : "y";
1248
+ };
1249
+ const items = (n2, d) => {
1250
+ if ("pane" in n2) {
1251
+ const s2 = stackOf(t, n2.pane);
1252
+ return [s2 ? `<${s2.members.map(String).join(" ")}>` : String(n2.pane)];
1253
+ }
1254
+ if (n2.master) return [`M${JSON.stringify(n2.master)}{${items(n2.a, null).join(";")}|${items(n2.b, null).join(";")}}`];
1255
+ const own = dir(n2);
1256
+ if (own === d) return [...items(n2.a, own), ...items(n2.b, own)];
1257
+ return [`${own}{${[...items(n2.a, own), ...items(n2.b, own)].join(";")}}`];
1258
+ };
1259
+ const s = ops.split(t);
1260
+ return s ? items(s, null).join(";") : "";
1261
+ };
1262
+ const liftFlat = flatOf(ops.normalize ? ops.normalize(drag.lift.tree) : drag.lift.tree);
1263
+ const changed = (h) => {
1264
+ const t = h.kind === "swap" ? ops.swap(drag.lift.tree, subject, h.at) : ops.beside(ops.release(drag.lift.tree, subject), subject, h.at, h.side, { kind: "half" });
1265
+ return ops.normalize ? ops.normalize(t) : t;
1266
+ };
1267
+ const masterIn = (n2) => !!n2 && !("pane" in n2) && (!!n2.master || masterIn(n2.a) || masterIn(n2.b));
1268
+ const hasMaster = masterIn(ops.split(drag.lift.tree));
1269
+ const meetInMaster = (x, y) => {
1270
+ let node = ops.split(drag.lift.tree);
1271
+ let master = false;
1272
+ while (node && !("pane" in node)) {
1273
+ const has2 = (n2, k) => "pane" in n2 ? n2.pane === k : has2(n2.a, k) || has2(n2.b, k);
1274
+ master = !!node.master;
1275
+ if (has2(node.a, x) && has2(node.a, y)) node = node.a;
1276
+ else if (has2(node.b, x) && has2(node.b, y)) node = node.b;
1277
+ else break;
1278
+ }
1279
+ return master && !!node && !("pane" in node);
1280
+ };
1281
+ const sidesOf = (at) => {
1282
+ const swap = flatOf(changed({ kind: "swap", at }));
1283
+ const r = drag.lift.layout.get(at);
1284
+ const same = (a, b) => Math.abs(a - b) <= 0.5;
1285
+ return ["top", "right", "bottom", "left"].filter((side) => {
1286
+ const f = flatOf(changed({ kind: "beside", at, side }));
1287
+ if (f !== liftFlat && f !== swap) return true;
1288
+ if (!home || !r || meetInMaster(subjectDrawn, at)) return true;
1289
+ const across = side === "top" || side === "bottom" ? same(r.x, home.x) && same(r.width, home.width) : same(r.y, home.y) && same(r.height, home.height);
1290
+ return !across;
1291
+ });
1292
+ };
1293
+ const panes = [];
1294
+ for (const [pane, rect] of laid.layout) {
1295
+ if (basis === "lift" && home && pane === subjectDrawn) continue;
1296
+ const at = atOf(pane);
1297
+ if (at === null || options.still?.(at)) continue;
1298
+ panes.push({ pane, at, rect, sides: sidesOf(at) });
1299
+ }
1300
+ const centerOf = (r, sides = ["top", "right", "bottom", "left"]) => {
1301
+ const clamp2 = (v) => Math.min(0.95, Math.max(0.05, v));
1302
+ const ratio = reg.pixelRatio > 0 ? reg.pixelRatio : 1;
1303
+ const alongGone = !sides.includes("top") || !sides.includes("bottom");
1304
+ const acrossGone = !sides.includes("left") || !sides.includes("right");
1305
+ const sx = alongGone ? neighborSwap : reg.centerX ?? reg.center;
1306
+ const sy = acrossGone ? neighborSwap : reg.centerY ?? reg.center;
1307
+ const cw = Math.min(r.width, Math.max(r.width * clamp2(sx), Math.min(reg.centerMin, r.width)));
1308
+ const ch = Math.min(r.height, Math.max(r.height * clamp2(sy), Math.min(reg.centerMin, r.height)));
1309
+ const g = (v) => Math.round(v * ratio) / ratio;
1310
+ return {
1311
+ l: sides.includes("left") ? r.x + g((r.width - cw) / 2) : r.x,
1312
+ t: sides.includes("top") ? r.y + g((r.height - ch) / 2) : r.y,
1313
+ r: sides.includes("right") ? r.x + g((r.width + cw) / 2) : r.x + r.width,
1314
+ b: sides.includes("bottom") ? r.y + g((r.height + ch) / 2) : r.y + r.height,
1315
+ // The edges a side that exists is cut at (the band it would have had).
1316
+ cl: r.x + g((r.width - cw) / 2),
1317
+ ct: r.y + g((r.height - ch) / 2),
1318
+ cr: r.x + g((r.width + cw) / 2),
1319
+ cb: r.y + g((r.height + ch) / 2)
1320
+ };
1321
+ };
1322
+ const regionOf = (r, p, sides = ["top", "right", "bottom", "left"]) => {
1323
+ const c = centerOf(r, sides);
1324
+ if (p.x >= c.l && p.x <= c.r && p.y >= c.t && p.y <= c.b) return "center";
1325
+ const past = sides.filter((s) => s === "top" ? p.y < c.ct : s === "bottom" ? p.y > c.cb : s === "left" ? p.x < c.cl : p.x > c.cr);
1326
+ if (past.length === 1) return past[0];
1327
+ const [x0, y0, x1, y1] = [r.x, r.y, r.x + r.width, r.y + r.height];
1328
+ const quads = {
1329
+ top: [{ x: x0, y: y0 }, { x: x1, y: y0 }, { x: c.cr, y: c.ct }, { x: c.cl, y: c.ct }],
1330
+ right: [{ x: x1, y: y0 }, { x: x1, y: y1 }, { x: c.cr, y: c.cb }, { x: c.cr, y: c.ct }],
1331
+ bottom: [{ x: x1, y: y1 }, { x: x0, y: y1 }, { x: c.cl, y: c.cb }, { x: c.cr, y: c.cb }],
1332
+ left: [{ x: x0, y: y1 }, { x: x0, y: y0 }, { x: c.cl, y: c.ct }, { x: c.cl, y: c.cb }]
1333
+ };
1334
+ return past.find((s) => inQuad(quads[s], p)) ?? null;
1335
+ };
1336
+ const regionBox = (r, region, sides) => {
1337
+ const c = centerOf(r, sides);
1338
+ if (region === "center") return { x: c.l, y: c.t, width: c.r - c.l, height: c.b - c.t };
1339
+ if (region === "top") return { x: r.x, y: r.y, width: r.width, height: c.ct - r.y };
1340
+ if (region === "bottom") return { x: r.x, y: c.cb, width: r.width, height: r.y + r.height - c.cb };
1341
+ if (region === "left") return { x: r.x, y: r.y, width: c.cl - r.x, height: r.height };
1342
+ return { x: c.cr, y: r.y, width: r.x + r.width - c.cr, height: r.height };
1343
+ };
1344
+ const live = (h) => h.kind === "home" || drag.after(h).refusal === null;
1345
+ const givesLift = (h) => {
1346
+ if (h.kind === "home") return true;
1347
+ const a = drag.after(h);
1348
+ return !a.refusal && (a.tree === drag.lift.tree || flatOf(a.tree) === liftFlat);
1349
+ };
1350
+ const expectAt = (p) => {
1351
+ if (home && inside2(home, p)) return { raw: HOME2, refused: null, box: home, pane: null, tie: false };
1352
+ const pane = panes.find((x) => inside2(x.rect, p));
1353
+ if (!pane) return { raw: null, refused: null, box: null, pane: null, tie: false };
1354
+ const members = stackOf(laid.tree, pane.pane)?.members ?? [pane.pane];
1355
+ let refused = null;
1356
+ for (const part of ops.parts?.(laid.tree, pane.pane, pane.rect) ?? []) {
1357
+ if (!inside2(part.rect, p)) continue;
1358
+ const i = (part.slots ?? []).findIndex((s) => inside2(s, p));
1359
+ if (i >= 0) {
1360
+ const index = i < members.length ? members.slice(0, i).filter((m) => !inSubject(m)).length : void 0;
1361
+ const held3 = index === void 0 ? { kind: "join", at: pane.at } : { kind: "join", at: pane.at, index };
1362
+ if (live(held3)) return { raw: held3, refused: null, box: part.slots[i], pane, tie: false };
1363
+ refused ??= held3;
1364
+ }
1365
+ const held2 = { kind: "join", at: pane.at };
1366
+ if (live(held2)) return { raw: held2, refused: null, box: part.rect, pane, tie: false };
1367
+ refused ??= held2;
1368
+ }
1369
+ const region = regionOf(pane.rect, p, pane.sides);
1370
+ if (region === null) return { raw: null, refused: null, box: null, pane, tie: true };
1371
+ const held = region === "center" ? { kind: "swap", at: pane.at } : { kind: "beside", at: pane.at, side: region };
1372
+ return live(held) ? { raw: held, refused: null, box: regionBox(pane.rect, region, pane.sides), pane, tie: false } : { raw: null, refused: refused ?? held, box: null, pane, tie: false };
1373
+ };
1374
+ const expectedPlace = (e) => e.raw && givesLift(e.raw) ? HOME2 : e.raw;
1375
+ const placeAt = (p) => {
1376
+ const id = hit.placeAt(p);
1377
+ return id === null ? null : hit.heldOf(id);
1378
+ };
1379
+ const frame = (p) => {
1380
+ const shown = hit.targets.find((t) => hit.placeOf(t, p) !== null) ?? null;
1381
+ return hit.read({ shown }, p);
1382
+ };
1383
+ const FAR = { x: area.x - 1e5, y: area.y - 1e5 };
1384
+ const fresh = (p) => {
1385
+ frame(FAR);
1386
+ return frame(p);
1387
+ };
1388
+ const randomPoint = (r) => ({ x: r.x + rnd() * r.width, y: r.y + rnd() * r.height });
1389
+ const samples = [];
1390
+ for (let i = 0; i < 300; i++) samples.push(randomPoint(area));
1391
+ for (const t of hit.targets) for (let i = 0; i < 6; i++) samples.push(randomPoint(t.hit));
1392
+ const zonesBefore = samples.map((p) => hit.placeAt(p));
1393
+ for (const p of samples) {
1394
+ const e = expectAt(p);
1395
+ if (e.tie) continue;
1396
+ const want = expectedPlace(e);
1397
+ const got = placeAt(p);
1398
+ check("zone-rule", show2(got) === show2(want), () => `at ${p.x.toFixed(1)},${p.y.toFixed(1)}: ${show2(got)}, the rule says ${show2(want)}`);
1399
+ if (got && got.kind !== "home") check("existence", drag.after(got).refusal === null, () => `${show2(got)} is offered but refused (${drag.after(got).refusal?.code})`);
1400
+ const r = fresh(p);
1401
+ if (e.raw === null && e.refused) check("existence", r.held === null && show2(r.refused) === show2(e.refused), () => `over refused ${show2(e.refused)}: held ${show2(r.held)}, refused ${show2(r.refused)}`);
1402
+ if (e.raw && e.raw.kind !== "home" && givesLift(e.raw)) {
1403
+ check("lift-equals-home", r.held?.kind === "home" && hit.release(r).tree === drag.lift.tree, () => `${show2(e.raw)} gives the lift back but reads ${show2(r.held)}`);
1404
+ }
1405
+ if (basis === "lift" && r.mark && e.pane) {
1406
+ const m = r.mark;
1407
+ check("shown-under-pointer", inside2(m.rect, p) && within(m.rect, e.pane.rect) && (m.kind !== "swap" || sameRect2(m.wash ?? void 0, home ?? void 0) || m.wash === null && home === null), () => `${show2(r.held)}: mark ${JSON.stringify(m.rect)} at ${p.x.toFixed(1)},${p.y.toFixed(1)}, pane ${JSON.stringify(e.pane.rect)}`);
1408
+ }
1409
+ if (basis === "base" && r.held && r.held.kind !== "home") {
1410
+ const a = drag.after(r.held);
1411
+ if (r.held.kind === "swap") {
1412
+ if (!a.landing || !inside2(a.landing, p)) reported.swapsBeside++;
1413
+ } else {
1414
+ const at = drawnAs(a.tree, r.held.at);
1415
+ const own = drawnAs(a.tree, subjectKey);
1416
+ const others = [...a.layout].some(([k, rect]) => k !== at && k !== own && drag.base.layout.has(k) && !sameRect2(rect, drag.base.layout.get(k)));
1417
+ const under = !!a.landing && inside2(a.landing, p);
1418
+ if (others || options.fixed || options.fixedAcross || hasMaster) {
1419
+ reported.insertsMoveOthers++;
1420
+ if (!under) reported.insertsMoveOthersBeside++;
1421
+ } 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)}`);
1422
+ }
1423
+ }
1424
+ }
1425
+ for (const pn of panes) {
1426
+ const r = pn.rect;
1427
+ if (r.width < 4 || r.height < 4) continue;
1428
+ const partRects = (ops.parts?.(laid.tree, pn.pane, r) ?? []).map((q) => q.rect);
1429
+ const free = (p) => !(home && inside2(home, p)) && !partRects.some((q) => inside2(q, p));
1430
+ const probes2 = [
1431
+ ["top", { x: r.x + r.width / 2, y: r.y + 1 }],
1432
+ ["bottom", { x: r.x + r.width / 2, y: r.y + r.height - 1 }],
1433
+ ["left", { x: r.x + 1, y: r.y + r.height / 2 }],
1434
+ ["right", { x: r.x + r.width - 1, y: r.y + r.height / 2 }]
1435
+ ];
1436
+ for (const [side, p] of probes2) {
1437
+ if (!free(p)) continue;
1438
+ const got = placeAt(p);
1439
+ const want = { kind: "beside", at: pn.at, side };
1440
+ const isSide = got?.kind === "beside" && got.side === side;
1441
+ if (pn.sides.includes(side)) {
1442
+ if (live(want) && regionOf(r, p, pn.sides) === side) check("new-arrangement-sides", isSide || givesLift(want) && got?.kind === "home", () => `${String(pn.at)} reads ${show2(got)} at its ${side} edge, a new arrangement`);
1443
+ } else {
1444
+ check("new-arrangement-sides", !isSide, () => `${String(pn.at)} offers its ${side} insert, which gives the ${flatOf(changed(want)) === liftFlat ? "lift's" : "swap's"} arrangement`);
1445
+ }
1446
+ }
1447
+ }
1448
+ for (const pn of panes) {
1449
+ const partRects = (ops.parts?.(laid.tree, pn.pane, pn.rect) ?? []).map((q) => q.rect);
1450
+ const free = (p) => !(home && inside2(home, p)) && !partRects.some((q) => inside2(q, p));
1451
+ for (let k = 0; k < 24; k++) {
1452
+ const a = randomPoint(pn.rect);
1453
+ const b = randomPoint(pn.rect);
1454
+ const m = { x: (a.x + b.x) / 2, y: (a.y + b.y) / 2 };
1455
+ if (!free(a) || !free(b) || !free(m)) continue;
1456
+ const za = hit.placeAt(a);
1457
+ if (za === null || za === "home" || za !== hit.placeAt(b)) continue;
1458
+ check("zones-convex", hit.placeAt(m) === za, () => `${za} at ${a.x.toFixed(1)},${a.y.toFixed(1)} and ${b.x.toFixed(1)},${b.y.toFixed(1)}, ${hit.placeAt(m)} between`);
1459
+ }
1460
+ }
1461
+ const checkFrame = (p, r) => {
1462
+ const ok = r.held === null || r.held.kind === "home" || drag.after(r.held).refusal === null;
1463
+ const rel = hit.release(r);
1464
+ const drawn = hit.drawn(r);
1465
+ const wantDrawn = basis === "lift" ? drag.lift : r.held === null ? drag.base : r.held.kind === "home" ? drag.lift : drag.after(r.held);
1466
+ check(
1467
+ "shown-equals-landed",
1468
+ ok && rel === drag.after(r.held ?? HOME2) && drawn === wantDrawn && (r.held !== null || rel.tree === drag.lift.tree),
1469
+ () => `at ${p.x.toFixed(1)},${p.y.toFixed(1)}: held ${show2(r.held)}${ok ? "" : " (refused)"}, release ${show2(rel.held)}`
1470
+ );
1471
+ };
1472
+ const sameHold = (a, b) => JSON.stringify(a) === JSON.stringify(b);
1473
+ const holdName = (s) => s ? `${s.place} from ${s.from.x.toFixed(1)},${s.from.y.toFixed(1)}` : "nothing";
1474
+ for (let w = 0; w < 4; w++) {
1475
+ frame(FAR);
1476
+ let p = randomPoint(area);
1477
+ let state = null;
1478
+ for (let i = 0; i < 40; i++) {
1479
+ p = { x: Math.min(area.x + area.width, Math.max(area.x, p.x + (rnd() - 0.5) * 60)), y: Math.min(area.y + area.height, Math.max(area.y, p.y + (rnd() - 0.5) * 60)) };
1480
+ checkFrame(p, frame(p));
1481
+ state = hit.hold(state, p);
1482
+ const still = hit.hold(state, p);
1483
+ check("stillness", sameHold(still, state), () => `a still pointer at ${p.x.toFixed(1)},${p.y.toFixed(1)} changed ${holdName(state)} to ${holdName(still)}`);
1484
+ const fresh0 = hit.hold(null, p);
1485
+ const twice = hit.hold(fresh0, p);
1486
+ check("stillness", sameHold(twice, fresh0), () => `at ${p.x.toFixed(1)},${p.y.toFixed(1)} hold(hold(null, p), p) is ${holdName(twice)}, hold(null, p) ${holdName(fresh0)}`);
1487
+ const back = hit.hold(hit.hold(state, FAR), p);
1488
+ check("stillness", sameHold(back, fresh0), () => `away and back to ${p.x.toFixed(1)},${p.y.toFixed(1)} reads ${holdName(back)}, nothing held gives ${holdName(fresh0)}`);
1489
+ }
1490
+ }
1491
+ for (let s = 0; s < 6; s++) {
1492
+ const a = randomPoint(area);
1493
+ const b = randomPoint(area);
1494
+ const steps = Math.max(2, Math.ceil(Math.hypot(b.x - a.x, b.y - a.y) / 1.5));
1495
+ let state = null;
1496
+ const met = [];
1497
+ for (let i = 0; i <= steps; i++) {
1498
+ const p = { x: a.x + (b.x - a.x) * i / steps, y: a.y + (b.y - a.y) * i / steps };
1499
+ state = hit.hold(state, p);
1500
+ if (state && met[met.length - 1] !== state.place) met.push(state.place);
1501
+ }
1502
+ const twice = met.filter((m, i) => m !== "home" && met.indexOf(m) !== i);
1503
+ check("one-pass", twice.length === 0, () => `a sweep from ${a.x.toFixed(0)},${a.y.toFixed(0)} to ${b.x.toFixed(0)},${b.y.toFixed(0)} met ${met.join(" > ")}`);
1504
+ }
1505
+ for (let k = 0; k < 12; k++) {
1506
+ const a = samples[Math.floor(rnd() * samples.length)];
1507
+ const e = expectAt(a);
1508
+ if (e.tie || !e.raw || e.raw.kind === "home" || givesLift(e.raw) || !e.box || !e.pane) continue;
1509
+ const start = hit.hold(null, a);
1510
+ if (!start) continue;
1511
+ const box = e.box;
1512
+ const ex = Math.min(edge, cap * box.width);
1513
+ const ey = Math.min(edge, cap * box.height);
1514
+ const zoneOf = (p) => show2(expectedPlace(expectAt(p)));
1515
+ const want = show2(e.raw);
1516
+ const path = [];
1517
+ for (let i = 0; i < 20 && path.length < 10; i++) {
1518
+ const z = { x: a.x + (rnd() - 0.5) * box.width * 0.5, y: a.y + (rnd() - 0.5) * box.height * 0.5 };
1519
+ if (zoneOf(z) !== want || e.box && !inside2(e.box, z)) continue;
1520
+ const ang = rnd() * Math.PI * 2;
1521
+ const len = Math.sqrt(rnd()) * 0.9;
1522
+ path.push({ x: z.x + Math.cos(ang) * ex * len, y: z.y + Math.sin(ang) * ey * len });
1523
+ }
1524
+ const tb = hit.heldOf(start.place)?.kind === "join" ? null : box;
1525
+ if (tb) {
1526
+ const rx = Math.min(travel, cap * tb.width);
1527
+ const ry = Math.min(travel, cap * tb.height);
1528
+ for (let i = 0; i < 10; i++) {
1529
+ const ang = rnd() * Math.PI * 2;
1530
+ const len = Math.sqrt(rnd()) * 0.9;
1531
+ const p = { x: a.x + Math.cos(ang) * rx * len, y: a.y + Math.sin(ang) * ry * len };
1532
+ if (inside2(e.pane.rect, p) && !(home && inside2(home, p))) path.push(p);
1533
+ }
1534
+ }
1535
+ let state = start;
1536
+ for (const p of path) {
1537
+ state = hit.hold(state, p);
1538
+ check("hold-measures", state?.place === start.place, () => `${want} taken at ${a.x.toFixed(1)},${a.y.toFixed(1)} went to ${state ? show2(hit.heldOf(state.place)) : "nothing"} at ${p.x.toFixed(1)},${p.y.toFixed(1)}`);
1539
+ if (state?.place !== start.place) break;
1540
+ }
1541
+ }
1542
+ const shows = (p, place) => {
1543
+ for (let dx = -edge; dx <= edge; dx += 0.5) for (let dy = -edge; dy <= edge; dy += 0.5) if (hit.placeAt({ x: p.x + dx, y: p.y + dy }) === place) return true;
1544
+ return false;
1545
+ };
1546
+ const rim = [];
1547
+ if (home && home.width > 2 * edge && home.height > 2 * edge) {
1548
+ const d = Math.min(edge / 2, 2);
1549
+ for (const f of [0.15, 0.5, 0.85]) {
1550
+ rim.push({ x: home.x + home.width * f, y: home.y + d }, { x: home.x + home.width * f, y: home.y + home.height - d });
1551
+ rim.push({ x: home.x + d, y: home.y + home.height * f }, { x: home.x + home.width - d, y: home.y + home.height * f });
1552
+ }
1553
+ }
1554
+ let probed = 0;
1555
+ const pairs = [];
1556
+ for (const p of rim) for (let i = 0; i < 4; i++) pairs.push([randomPoint(area), p]);
1557
+ for (let i = 0; i < 40; i++) pairs.push([randomPoint(area), randomPoint(area)]);
1558
+ for (const [q, p] of pairs) {
1559
+ const taken = hit.hold(null, q);
1560
+ if (!taken || taken.place === "home") continue;
1561
+ const kept = hit.hold(taken, p);
1562
+ if (kept?.place !== taken.place || hit.placeAt(p) === taken.place) continue;
1563
+ if (!(home && inside2(home, p)) && Math.abs(p.x - taken.from.x) < travel && Math.abs(p.y - taken.from.y) < travel) continue;
1564
+ if (probed++ >= 24) break;
1565
+ 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)})`);
1566
+ }
1567
+ check("fixed-map", hit.targets === targetsAtLift && Object.isFrozen(hit.targets) && JSON.stringify(hit.targets) === snapshot, () => "the targets changed during the drag");
1568
+ const zonesAfter = samples.map((p) => hit.placeAt(p));
1569
+ const moved = zonesAfter.findIndex((z, i) => z !== zonesBefore[i]);
1570
+ check("fixed-map", moved < 0, () => `the zone at ${samples[moved].x.toFixed(1)},${samples[moved].y.toFixed(1)} changed from ${zonesBefore[moved]} to ${zonesAfter[moved]}`);
1571
+ const places = /* @__PURE__ */ new Map();
1572
+ samples.forEach((p, i) => {
1573
+ const z = zonesBefore[i];
1574
+ if (z !== null && !places.has(z)) places.set(z, p);
1575
+ });
1576
+ const probes = samples.slice(0, 12);
1577
+ for (const [, at] of [...places].slice(0, 10)) {
1578
+ for (const q of probes) {
1579
+ frame(at);
1580
+ const r = fresh(q);
1581
+ const z = hit.placeAt(q);
1582
+ check("fixed-map", show2(r.held) === show2(z === null ? null : hit.heldOf(z)), () => `after holding the place at ${at.x.toFixed(1)},${at.y.toFixed(1)}, the point ${q.x.toFixed(1)},${q.y.toFixed(1)} reads ${show2(r.held)}, nothing held gives ${z}`);
1583
+ }
1584
+ }
1585
+ }
1586
+ }
1587
+
345
1588
  // src/tiling/neighbor.ts
346
1589
  function neighbor(rects, from, direction) {
347
1590
  const origin = rects.get(from);
@@ -360,8 +1603,8 @@ function neighbor(rects, from, direction) {
360
1603
  const along = center(r, horizontal);
361
1604
  if (sign < 0 ? along >= fromAlong : along <= fromAlong) continue;
362
1605
  const [start, end] = reach(r, !horizontal);
363
- const overlap = Math.max(0, Math.min(originEnd, end) - Math.max(originStart, start));
364
- const score = Math.abs(along - fromAlong) + Math.abs(center(r, !horizontal) - fromAcross) * 0.25 + (overlap > 0 ? 0 : 100);
1606
+ const overlap2 = Math.max(0, Math.min(originEnd, end) - Math.max(originStart, start));
1607
+ const score = Math.abs(along - fromAlong) + Math.abs(center(r, !horizontal) - fromAcross) * 0.25 + (overlap2 > 0 ? 0 : 100);
365
1608
  if (score < bestScore) {
366
1609
  bestScore = score;
367
1610
  best = key;
@@ -372,6 +1615,9 @@ function neighbor(rects, from, direction) {
372
1615
  export {
373
1616
  PANE_REGION_DEFAULTS,
374
1617
  PANE_SIDES,
1618
+ TILING_HIT_DEFAULTS,
1619
+ TILING_HIT_PROPERTIES,
1620
+ TILING_PROPERTIES,
375
1621
  canDropOnPane,
376
1622
  canSplitPane,
377
1623
  columns,
@@ -388,11 +1634,17 @@ export {
388
1634
  orderTargets,
389
1635
  paneCenter,
390
1636
  paneRegion,
1637
+ paneRegionShapes,
391
1638
  paneTargets,
392
1639
  panesOf,
393
1640
  removePane,
394
1641
  splitFromOrder,
395
1642
  splitPane,
396
- swapPanes
1643
+ splitTreeOps,
1644
+ swapPanes,
1645
+ tilingConformance,
1646
+ tilingDrag,
1647
+ tilingHitConformance,
1648
+ tilingSubjects
397
1649
  };
398
1650
  //# sourceMappingURL=tiling.js.map