@n-uf/placement 26.11.58 → 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,6 +1,7 @@
1
1
  import {
2
2
  EPS,
3
3
  clamp,
4
+ contains,
4
5
  overlap
5
6
  } from "./chunk-2UVUFEBP.js";
6
7
 
@@ -179,13 +180,13 @@ function lay(tree, area, ctx, owned) {
179
180
  const x = axis === "x";
180
181
  const extent = x ? area.width : area.height;
181
182
  const origin = x ? area.x : area.y;
182
- const cross = x ? area.height : area.width;
183
+ const cross2 = x ? area.height : area.width;
183
184
  const gutter = (tree.gap === void 0 ? ctx.gap : px(tree.gap)) + ctx.handle;
184
185
  const { fixed, fixedAcross, floor } = ctx.options;
185
186
  const lenA = fixedAcross?.(tree.a, axis);
186
187
  const lenB = fixedAcross?.(tree.b, axis);
187
- const acrossA = usable(lenA) ? lenA : cross;
188
- const acrossB = usable(lenB) ? lenB : cross;
188
+ const acrossA = usable(lenA) ? lenA : cross2;
189
+ const acrossB = usable(lenB) ? lenB : cross2;
189
190
  const fixedA = fixed?.(tree.a, axis);
190
191
  const fixedB = fixed?.(tree.b, axis);
191
192
  let lengthA;
@@ -284,11 +285,11 @@ function depths(p, c) {
284
285
  { side: "left", depth: (c.left - p.x) / Math.max(c.left, 1) }
285
286
  ];
286
287
  }
287
- function rawRegion(p, c) {
288
- 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) {
289
289
  let best = "center";
290
290
  let deepest = 0;
291
291
  for (const d of depths(p, c)) {
292
+ if (sides && !sides.includes(d.side)) continue;
292
293
  if (d.depth > deepest) {
293
294
  deepest = d.depth;
294
295
  best = d.side;
@@ -307,13 +308,388 @@ function toward(p, region, by, c) {
307
308
  if (len <= by || len === 0) return { x: c.width / 2, y: c.height / 2 };
308
309
  return { x: p.x + dx / len * by, y: p.y + dy / len * by };
309
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
+ }
310
328
  function paneRegion(pane, point, previous = null, options = {}) {
311
329
  const c = options.paneCenter ?? paneCenter(pane, options);
312
330
  const p = { x: point.x - pane.x, y: point.y - pane.y };
313
- const raw = rawRegion(p, c);
331
+ const raw = rawRegion(p, c, options.sides);
314
332
  const by = Math.max(0, options.hysteresis ?? PANE_REGION_DEFAULTS.hysteresis);
315
333
  if (previous === null || by === 0 || previous === raw) return raw;
316
- 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
+ };
317
693
  }
318
694
 
319
695
  // src/tiling/targets.ts
@@ -424,7 +800,14 @@ function tilingDrag(tree, subject, options) {
424
800
  if (!a) byAt.set(held.at, a = make(held));
425
801
  return a;
426
802
  };
427
- return { subject, lift, base, home, after };
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 };
428
811
  }
429
812
 
430
813
  // src/tiling/reference.ts
@@ -442,8 +825,25 @@ var stacksOf = (t) => t.stacks ?? [];
442
825
  var of = (split, stacks) => stacks.length ? { split, stacks } : { split };
443
826
  var membersOf = (s) => s.kind === "pane" ? [s.pane] : s.stack.members;
444
827
  var drawnOf = (s) => s.kind === "pane" ? s.pane : s.stack.active;
445
- function splitTreeOps() {
828
+ function splitTreeOps(options = {}) {
829
+ const bar = options.bar ?? 0;
830
+ const tab = options.tab ?? (() => 80);
446
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
+ } : {},
447
847
  split: (t) => t.split,
448
848
  stackOf: (t, pane) => stacksOf(t).find((s) => s.members.includes(pane)) ?? null,
449
849
  release(t, s) {
@@ -590,10 +990,20 @@ function tilingSubjects(tree, options) {
590
990
  }
591
991
  return out;
592
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
+ }
593
1004
  function tilingConformance(trees, options, limit = 20) {
594
1005
  const { ops, area } = options;
595
- const counts = Object.fromEntries(TILING_PROPERTIES.map((p) => [p, { checked: 0, failed: 0 }]));
596
- const failures = [];
1006
+ const { counts, failures, checkFor } = propertyTally(TILING_PROPERTIES, limit);
597
1007
  const norm = (t) => ops.normalize ? ops.normalize(t) : t;
598
1008
  const lay2 = (t) => layoutSplitTree(ops.split(t), area, options).panes;
599
1009
  const drawnAs = (t, k) => ops.stackOf?.(t, k)?.active ?? k;
@@ -604,13 +1014,7 @@ function tilingConformance(trees, options, limit = 20) {
604
1014
  for (const tree of trees) {
605
1015
  const snapshot = copy(tree);
606
1016
  for (const subject of tilingSubjects(tree, options)) {
607
- const name = `tree ${n}, ${subject.kind === "pane" ? `pane ${String(subject.pane)}` : `stack ${subject.stack.id}`}`;
608
- const check = (p, ok, why) => {
609
- counts[p].checked++;
610
- if (ok) return;
611
- counts[p].failed++;
612
- if (failures.length < limit) failures.push(`${p}: ${name}: ${why()}`);
613
- };
1017
+ const check = checkFor(`tree ${n}, ${subject.kind === "pane" ? `pane ${String(subject.pane)}` : `stack ${subject.stack.id}`}`);
614
1018
  const drag = tilingDrag(tree, subject, options);
615
1019
  const key = subject.kind === "pane" ? subject.pane : subject.stack.active;
616
1020
  const liftOf = (k) => drag.lift.layout.get(drag.lift.layout.has(k) ? k : drawnAs(tree, k));
@@ -700,6 +1104,487 @@ function tilingConformance(trees, options, limit = 20) {
700
1104
  return { ok: failures.length === 0 && Object.values(counts).every((c) => c.failed === 0), failures, counts };
701
1105
  }
702
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
+
703
1588
  // src/tiling/neighbor.ts
704
1589
  function neighbor(rects, from, direction) {
705
1590
  const origin = rects.get(from);
@@ -730,6 +1615,8 @@ function neighbor(rects, from, direction) {
730
1615
  export {
731
1616
  PANE_REGION_DEFAULTS,
732
1617
  PANE_SIDES,
1618
+ TILING_HIT_DEFAULTS,
1619
+ TILING_HIT_PROPERTIES,
733
1620
  TILING_PROPERTIES,
734
1621
  canDropOnPane,
735
1622
  canSplitPane,
@@ -747,6 +1634,7 @@ export {
747
1634
  orderTargets,
748
1635
  paneCenter,
749
1636
  paneRegion,
1637
+ paneRegionShapes,
750
1638
  paneTargets,
751
1639
  panesOf,
752
1640
  removePane,
@@ -756,6 +1644,7 @@ export {
756
1644
  swapPanes,
757
1645
  tilingConformance,
758
1646
  tilingDrag,
1647
+ tilingHitConformance,
759
1648
  tilingSubjects
760
1649
  };
761
1650
  //# sourceMappingURL=tiling.js.map