@n-uf/placement 26.11.14 → 26.11.16

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.
@@ -0,0 +1,566 @@
1
+ import {
2
+ clampInto,
3
+ rect
4
+ } from "./chunk-QMTNB2C3.js";
5
+
6
+ // src/stack/model.ts
7
+ var DEFAULT_RAIL = 30;
8
+ var DEFAULT_MENU = 28;
9
+ var finite = (x, fallback) => Number.isFinite(x) ? x : fallback;
10
+ function limitsOf(input) {
11
+ const min = Math.max(0, finite(input.cross.min, 0));
12
+ const max = Number.isNaN(input.cross.max) ? Infinity : input.cross.max;
13
+ return { min, max: Math.max(min, max), rail: Math.max(0, finite(input.cross.rail ?? DEFAULT_RAIL, DEFAULT_RAIL)) };
14
+ }
15
+ var menuMain = (input) => Math.max(0, finite(input.menu?.main ?? DEFAULT_MENU, DEFAULT_MENU));
16
+ function spacingOf(input) {
17
+ const px = (x) => Math.max(0, finite(x ?? 0, 0));
18
+ return { gap: px(input.gap), start: px(input.pad?.start), end: px(input.pad?.end) };
19
+ }
20
+ var formSetsCross = (f) => f.setsCross ?? f.kind === "open";
21
+ function withItem(input, item, at) {
22
+ const k = Math.min(Math.max(0, Math.floor(at)), input.items.length);
23
+ return { ...input, items: [...input.items.slice(0, k), item, ...input.items.slice(k)] };
24
+ }
25
+
26
+ // src/stack/layout.ts
27
+ var EPS = 1e-6;
28
+ var isMenu = (f) => f.kind === "menu";
29
+ var GROUP_KINDS = /* @__PURE__ */ new Set(["row", "stacked", "icons", "fold"]);
30
+ var finite2 = (x, fallback = 0) => Number.isFinite(x) ? x : fallback;
31
+ var mainOf = (f, which) => isMenu(f) ? 0 : Math.max(0, finite2(f.main[which]));
32
+ var crossMin = (f) => finite2(f.cross.min ?? f.cross.natural);
33
+ var rankOf = (f) => finite2(f.rank ?? 0);
34
+ function victim(items, to) {
35
+ let best = -1;
36
+ for (let i = 0; i < items.length; i++) {
37
+ if (to[i] < 0 || items[i].fixed) continue;
38
+ if (best < 0) {
39
+ best = i;
40
+ continue;
41
+ }
42
+ const a = items[i];
43
+ const b = items[best];
44
+ const ra = rankOf(a.forms[to[i]]);
45
+ const rb = rankOf(b.forms[to[best]]);
46
+ const ka = a.kept ? 1 : 0;
47
+ const kb = b.kept ? 1 : 0;
48
+ if (ra !== rb ? ra < rb : ka !== kb ? ka < kb : a.used !== b.used ? a.used < b.used : true) best = i;
49
+ }
50
+ return best;
51
+ }
52
+ function fitStack(input, options = {}) {
53
+ const fresh = solve(input);
54
+ const previous = options.previous;
55
+ if (!previous || previous.need === void 0) return fresh;
56
+ const seed = { need: 0 };
57
+ const kept = solve(input, previous, seed);
58
+ if (kept.cross > previous.cross + EPS) return fresh;
59
+ const moreLength = Math.max(0, kept.length) - seed.need > previous.length - previous.need + EPS;
60
+ if (!moreLength) return kept;
61
+ const gain = fresh.items.every((p, i) => p.form <= kept.items[i].form) && fresh.items.some((p, i) => p.form < kept.items[i].form);
62
+ return gain ? fresh : solve(input, previous, { need: 0 }, true);
63
+ }
64
+ function solve(input, previous, seed, grow = false) {
65
+ const length = Math.max(0, finite2(input.length));
66
+ const lim = limitsOf(input);
67
+ const items = input.items.filter((it) => it.forms.length > 0);
68
+ const start = items.map((it) => Math.min(Math.max(0, Math.floor(finite2(it.from ?? 0))), it.forms.length - 1));
69
+ const at = [...start];
70
+ if (previous) {
71
+ const had = new Map(previous.items.map((p) => [p.id, p]));
72
+ const began = /* @__PURE__ */ new Map();
73
+ for (const n of previous.notes) if (n.code === "form-down" && !began.has(n.id)) began.set(n.id, n.from);
74
+ items.forEach((it, i) => {
75
+ const p = had.get(it.id);
76
+ if (it.fixed || !p || p.form < start[i] || p.form >= it.forms.length || start[i] < (began.get(it.id) ?? p.form)) return;
77
+ const f = it.forms[p.form];
78
+ if (f.kind === p.kind) at[i] = p.form;
79
+ });
80
+ }
81
+ const form = (i) => items[i].forms[at[i]];
82
+ const idx = items.map((_, i) => i);
83
+ const firstStep = [];
84
+ const menuOrder = [];
85
+ const step = (i, j) => {
86
+ if (!firstStep.includes(i)) firstStep.push(i);
87
+ at[i] = j;
88
+ if (isMenu(form(i)) && !menuOrder.includes(i)) menuOrder.push(i);
89
+ };
90
+ const named = (code) => previous ? previous.notes.filter((n) => n.code === code).map((n) => items.findIndex((it) => it.id === n.id)) : [];
91
+ for (const i of [...named("form-down"), ...idx]) if (i >= 0 && at[i] > start[i] && !firstStep.includes(i)) firstStep.push(i);
92
+ for (const i of [...named("to-menu"), ...idx]) if (i >= 0 && isMenu(form(i)) && !menuOrder.includes(i)) menuOrder.push(i);
93
+ const crossOf = () => {
94
+ const setters = idx.filter((i) => !isMenu(form(i)) && formSetsCross(form(i)));
95
+ if (setters.length === 0) return lim.rail;
96
+ const want = Math.max(...setters.map((i) => finite2(form(i).cross.natural)));
97
+ return Math.min(Math.max(want, lim.min), lim.max);
98
+ };
99
+ const inMenu = () => idx.some((i) => isMenu(form(i)));
100
+ const sp = spacingOf(input);
101
+ const gapsOf = () => {
102
+ const slots = idx.filter((i) => !isMenu(form(i))).length + (inMenu() ? 1 : 0);
103
+ return slots > 1 ? (slots - 1) * sp.gap : 0;
104
+ };
105
+ const minSum = () => idx.reduce((s, i) => s + mainOf(form(i), "min"), 0) + (inMenu() ? menuMain(input) : 0) + gapsOf() + sp.start + sp.end;
106
+ const stepTo = (i) => {
107
+ const from = at[i];
108
+ const base = minSum();
109
+ let found = -1;
110
+ for (let j = from + 1; j < items[i].forms.length && found < 0; j++) {
111
+ at[i] = j;
112
+ if (minSum() < base - EPS) found = j;
113
+ }
114
+ at[i] = from;
115
+ return found;
116
+ };
117
+ const menuFormAfter = (i) => {
118
+ for (let j = at[i] + 1; j < items[i].forms.length; j++) if (isMenu(items[i].forms[j])) return j;
119
+ return -1;
120
+ };
121
+ const menuBatch = (pool) => {
122
+ const saved = [...at];
123
+ const base = minSum();
124
+ const left = [...pool];
125
+ const batch = [];
126
+ let found = false;
127
+ for (let b = victim(items, left); b >= 0 && !found; b = victim(items, left)) {
128
+ batch.push([b, left[b]]);
129
+ at[b] = left[b];
130
+ left[b] = -1;
131
+ found = minSum() < base - EPS;
132
+ }
133
+ at.splice(0, at.length, ...saved);
134
+ return found ? batch : null;
135
+ };
136
+ const canStep = (i) => at[i] + 1 < items[i].forms.length;
137
+ const tooWide = (i, cross2) => {
138
+ const f = form(i);
139
+ if (isMenu(f)) return false;
140
+ if (formSetsCross(f)) return input.strictCross === true && crossMin(f) > lim.max + EPS;
141
+ return crossMin(f) > cross2 + EPS;
142
+ };
143
+ if (seed) seed.need = minSum();
144
+ if (grow) {
145
+ const comesBefore = (a, b) => {
146
+ const ra = rankOf(items[a].forms[at[a]]);
147
+ const rb = rankOf(items[b].forms[at[b]]);
148
+ const ka = items[a].kept ? 1 : 0;
149
+ const kb = items[b].kept ? 1 : 0;
150
+ return ra !== rb ? ra > rb : ka !== kb ? ka > kb : items[a].used !== items[b].used ? items[a].used > items[b].used : a < b;
151
+ };
152
+ for (; ; ) {
153
+ const c0 = crossOf();
154
+ const fitsWith = (changed) => minSum() <= length + EPS && crossOf() === c0 && changed.every((j) => !tooWide(j, c0));
155
+ const queue = idx.filter((i) => !items[i].fixed && at[i] > start[i]).sort((a, b) => comesBefore(a, b) ? -1 : 1);
156
+ const next = queue[0];
157
+ if (next === void 0) break;
158
+ const cur = at[next];
159
+ let to = -1;
160
+ for (let k = cur - 1; k >= start[next] && to < 0; k--) {
161
+ at[next] = k;
162
+ if (fitsWith([next])) to = k;
163
+ }
164
+ at[next] = cur;
165
+ if (to >= 0) {
166
+ at[next] = to;
167
+ continue;
168
+ }
169
+ const inside2 = [];
170
+ for (const [q, i] of queue.entries()) {
171
+ if (!isMenu(form(i))) continue;
172
+ if (!queue.slice(0, q).every((j) => isMenu(form(j)) || at[i] - 1 - start[i] >= at[j] - start[j])) break;
173
+ inside2.push(i);
174
+ }
175
+ let out = false;
176
+ for (let n = 1; n <= inside2.length && !out; n++) {
177
+ const group = inside2.slice(0, n);
178
+ const was = group.map((i) => at[i]);
179
+ for (const i of group) at[i] = at[i] - 1;
180
+ if (fitsWith(group)) out = true;
181
+ else group.forEach((i, k) => at[i] = was[k]);
182
+ }
183
+ if (!out) break;
184
+ }
185
+ }
186
+ let cross = crossOf();
187
+ let overflow = 0;
188
+ const guard = 2 * items.reduce((s, it) => s + it.forms.length, 0) + 2;
189
+ for (let n = 0; n <= guard; n++) {
190
+ cross = crossOf();
191
+ let stepped = false;
192
+ for (const i of idx) {
193
+ if (!items[i].fixed && tooWide(i, cross) && canStep(i) && !isMenu(items[i].forms[at[i] + 1])) {
194
+ step(i, at[i] + 1);
195
+ stepped = true;
196
+ }
197
+ }
198
+ if (stepped) continue;
199
+ const over = minSum() - length;
200
+ if (over <= EPS) break;
201
+ const to = idx.map((i) => items[i].fixed ? -1 : stepTo(i));
202
+ const pool = idx.map((i) => to[i] < 0 && !inMenu() && !items[i].fixed ? menuFormAfter(i) : -1);
203
+ const v = victim(
204
+ items,
205
+ idx.map((i) => to[i] >= 0 ? to[i] : pool[i])
206
+ );
207
+ if (v >= 0 && to[v] < 0) {
208
+ const batch = menuBatch(pool);
209
+ if (batch) {
210
+ for (const [i, j] of batch) step(i, j);
211
+ continue;
212
+ }
213
+ }
214
+ const w = v >= 0 && to[v] >= 0 ? v : victim(items, to);
215
+ if (w < 0) {
216
+ overflow = items.length > 0 ? over : 0;
217
+ break;
218
+ }
219
+ step(w, to[w]);
220
+ }
221
+ cross = crossOf();
222
+ const menuSize = inMenu() ? menuMain(input) : 0;
223
+ const avail = Math.max(0, length - sp.start - sp.end - gapsOf() - menuSize);
224
+ const drawn = idx.filter((i) => !isMenu(form(i)));
225
+ const min = (i) => mainOf(form(i), "min");
226
+ const nat = (i) => Math.max(min(i), mainOf(form(i), "natural"));
227
+ const sumMin = drawn.reduce((s, i) => s + min(i), 0);
228
+ const sumNat = drawn.reduce((s, i) => s + nat(i), 0);
229
+ const size = /* @__PURE__ */ new Map();
230
+ if (sumNat <= avail + EPS) {
231
+ const growers = drawn.filter((i) => form(i).main.grows);
232
+ const spare = Math.max(0, avail - sumNat);
233
+ const weight = growers.reduce((s, i) => s + nat(i), 0);
234
+ for (const i of drawn) {
235
+ const share = growers.includes(i) ? weight > EPS ? nat(i) / weight : 1 / growers.length : 0;
236
+ size.set(i, nat(i) + spare * share);
237
+ }
238
+ } else {
239
+ const t = avail > sumMin && sumNat > sumMin ? (avail - sumMin) / (sumNat - sumMin) : 0;
240
+ for (const i of drawn) size.set(i, min(i) + (nat(i) - min(i)) * t);
241
+ }
242
+ const rectAt = (from, main, across = cross) => input.axis === "column" ? { x: 0, y: from, width: across, height: main } : { x: from, y: 0, width: main, height: across };
243
+ const acrossOf = (i) => {
244
+ const f = form(i);
245
+ return input.ownCross === true && GROUP_KINDS.has(f.kind) ? Math.min(cross, Math.max(0, finite2(f.cross.natural, cross))) : cross;
246
+ };
247
+ const rects = /* @__PURE__ */ new Map();
248
+ let cursor = sp.start;
249
+ for (const i of drawn) {
250
+ const s = size.get(i);
251
+ rects.set(i, rectAt(cursor, s, acrossOf(i)));
252
+ cursor += s + sp.gap;
253
+ }
254
+ const menuRect = rectAt(Math.max(drawn.length > 0 ? cursor : sp.start, length - sp.end - menuSize), menuSize);
255
+ const inside = idx.filter((i) => isMenu(form(i)));
256
+ const menu = inside.length > 0 ? { rect: menuRect, items: inside.map((i) => items[i].id) } : null;
257
+ const placed = idx.map((i) => ({ id: items[i].id, form: at[i], kind: form(i).kind, rect: isMenu(form(i)) ? menuRect : rects.get(i) }));
258
+ const notes = [];
259
+ for (const it of input.items) if (it.forms.length === 0) notes.push({ code: "no-forms", id: it.id });
260
+ for (const i of firstStep) if (at[i] > start[i]) notes.push({ code: "form-down", id: items[i].id, from: start[i], to: at[i] });
261
+ for (const i of menuOrder) if (isMenu(form(i))) notes.push({ code: "to-menu", id: items[i].id });
262
+ for (const i of idx) if (tooWide(i, cross)) notes.push({ code: "too-wide", id: items[i].id });
263
+ if (overflow > EPS) notes.push({ code: "overflow", over: overflow });
264
+ return { axis: input.axis, length, cross, items: placed, menu, notes, need: minSum() };
265
+ }
266
+
267
+ // src/stack/store.ts
268
+ var createStack = (order) => ({ order: [...order], kept: [], closed: [], used: [...order], forms: {} });
269
+ var isForm = (f) => typeof f === "number" && Number.isInteger(f) && f >= 1;
270
+ var uniq = (ids, order) => [...new Set(ids)].filter((id) => order.includes(id));
271
+ var uniqLast = (ids, order) => ids.filter((id, i) => order.includes(id) && ids.lastIndexOf(id) === i);
272
+ var noForms = () => /* @__PURE__ */ Object.create(null);
273
+ function storeStack(state) {
274
+ const forms = noForms();
275
+ for (const id of state.order) {
276
+ const f = state.forms[id];
277
+ if (id !== "__proto__" && Object.hasOwn(state.forms, id) && isForm(f)) forms[id] = f;
278
+ }
279
+ return { v: 1, order: [...state.order], kept: uniq(state.kept, state.order), closed: uniq(state.closed, state.order), used: uniqLast(state.used, state.order), forms };
280
+ }
281
+ var isObject = (x) => typeof x === "object" && x !== null && !Array.isArray(x);
282
+ function readStack(data, known) {
283
+ try {
284
+ return read(data, known);
285
+ } catch (e) {
286
+ return { ok: false, error: `unreadable: ${e instanceof Error ? e.message : typeof e}` };
287
+ }
288
+ }
289
+ var versionWords = (v) => typeof v === "number" ? String(v) : typeof v === "string" ? JSON.stringify(v.length > 24 ? `${v.slice(0, 24)}\u2026` : v) : typeof v;
290
+ function read(data, known) {
291
+ if (!isObject(data)) return { ok: false, error: "not an object" };
292
+ if (data.v !== 1) return { ok: false, error: `unknown version ${versionWords(data.v)} (this reads version 1)` };
293
+ if (!Array.isArray(data.order)) return { ok: false, error: "order is missing" };
294
+ const dropped = [];
295
+ const drop = (what) => {
296
+ if (!dropped.includes(what)) dropped.push(what);
297
+ };
298
+ for (const k of Object.keys(data)) if (!["v", "order", "kept", "closed", "used", "forms"].includes(k)) drop(k);
299
+ const order = [];
300
+ for (const x of data.order) {
301
+ if (typeof x !== "string") drop(`a ${typeof x} where an id goes`);
302
+ else if (known && !known.includes(x) || order.includes(x)) drop(x);
303
+ else order.push(x);
304
+ }
305
+ const list = (key) => {
306
+ const out = [];
307
+ const x = data[key];
308
+ if (x === void 0) return out;
309
+ if (!Array.isArray(x)) {
310
+ drop(key);
311
+ return out;
312
+ }
313
+ for (const id of x) {
314
+ if (typeof id === "string" && order.includes(id)) {
315
+ if (key === "used" && out.includes(id)) out.splice(out.indexOf(id), 1);
316
+ if (!out.includes(id)) out.push(id);
317
+ } else drop(typeof id === "string" ? `${key}:${id}` : `a ${typeof id} in ${key}`);
318
+ }
319
+ return out;
320
+ };
321
+ const kept = list("kept");
322
+ const closed = list("closed");
323
+ const used = list("used");
324
+ const forms = noForms();
325
+ if (data.forms !== void 0) {
326
+ if (!isObject(data.forms)) drop("forms");
327
+ else {
328
+ for (const id of Object.keys(data.forms)) {
329
+ const f = data.forms[id];
330
+ if (id === "__proto__" || !Object.hasOwn(data.forms, id) || !order.includes(id) || !isForm(f)) drop(`forms:${id}`);
331
+ else forms[id] = f;
332
+ }
333
+ }
334
+ }
335
+ return { ok: true, state: { order, kept, closed, used, forms }, dropped };
336
+ }
337
+
338
+ // src/stack/floats.ts
339
+ var EPS2 = 1e-3;
340
+ var finite3 = (x, fallback) => Number.isFinite(x) ? x : fallback;
341
+ var overlaps = (a, b) => Math.min(a.x + a.width, b.x + b.width) - Math.max(a.x, b.x) > EPS2 && Math.min(a.y + a.height, b.y + b.height) - Math.max(a.y, b.y) > EPS2;
342
+ var shared = (a, b) => Math.max(0, Math.min(a.x + a.width, b.x + b.width) - Math.max(a.x, b.x)) * Math.max(0, Math.min(a.y + a.height, b.y + b.height) - Math.max(a.y, b.y));
343
+ var clean = (r, at) => rect(finite3(r.x, at.x), finite3(r.y, at.y), Math.max(0, finite3(r.width, 0)), Math.max(0, finite3(r.height, 0)));
344
+ var WORK = 3e6;
345
+ var COVERED_PLACES = 2048;
346
+ var Nearest = class {
347
+ h = [];
348
+ less(a, b) {
349
+ return a[2] !== b[2] ? a[2] < b[2] : a[0] !== b[0] ? a[0] < b[0] : a[1] < b[1];
350
+ }
351
+ get size() {
352
+ return this.h.length;
353
+ }
354
+ push(t) {
355
+ const h = this.h;
356
+ let i = h.push(t) - 1;
357
+ while (i > 0) {
358
+ const p = i - 1 >> 1;
359
+ if (!this.less(h[i], h[p])) break;
360
+ [h[i], h[p]] = [h[p], h[i]];
361
+ i = p;
362
+ }
363
+ }
364
+ pop() {
365
+ const h = this.h;
366
+ const top = h[0];
367
+ const last = h.pop();
368
+ if (h.length > 0) {
369
+ h[0] = last;
370
+ let i = 0;
371
+ for (; ; ) {
372
+ const l = 2 * i + 1;
373
+ const r = l + 1;
374
+ let m = i;
375
+ if (l < h.length && this.less(h[l], h[m])) m = l;
376
+ if (r < h.length && this.less(h[r], h[m])) m = r;
377
+ if (m === i) break;
378
+ [h[i], h[m]] = [h[m], h[i]];
379
+ i = m;
380
+ }
381
+ }
382
+ return top;
383
+ }
384
+ };
385
+ function* byDistance(xs, ys, fx, fy) {
386
+ const sx = [...xs].sort((a, b) => Math.abs(a - fx) - Math.abs(b - fx) || a - b);
387
+ const sy = [...ys].sort((a, b) => Math.abs(a - fy) - Math.abs(b - fy) || a - b);
388
+ const q = new Nearest();
389
+ const d = (i, j) => Math.hypot(sx[i] - fx, sy[j] - fy);
390
+ if (sy.length > 0) for (let i = 0; i < sx.length; i++) q.push([i, 0, d(i, 0)]);
391
+ while (q.size > 0) {
392
+ const [i, j, far] = q.pop();
393
+ yield { x: sx[i], y: sy[j], far };
394
+ if (j + 1 < sy.length) q.push([i, j + 1, d(i, j + 1)]);
395
+ }
396
+ }
397
+ function fitFloat(want, stage, avoid = []) {
398
+ const area = clean(stage, rect(0, 0, 0, 0));
399
+ const r = clean(want, area);
400
+ const inside = clampInto(r, area);
401
+ const clamped = inside.x !== want.x || inside.y !== want.y;
402
+ const reach = rect(area.x, area.y, Math.max(area.width, r.width), Math.max(area.height, r.height));
403
+ const near = [];
404
+ avoid.forEach((a, index) => {
405
+ const at = clean(a, area);
406
+ if (at.width > EPS2 && at.height > EPS2 && overlaps(at, reach)) near.push({ at, index });
407
+ });
408
+ const under = (cand) => near.filter((n) => overlaps(cand, n.at)).map((n) => n.index);
409
+ const blocked = (cand) => near.some((n) => overlaps(cand, n.at));
410
+ if (!blocked(inside)) return { rect: inside, clamped, snapped: false, covers: [], searchCapped: false };
411
+ const lo = (v, from, to) => Math.min(Math.max(v, from), Math.max(from, to));
412
+ const xs = [inside.x, area.x, area.x + area.width - r.width];
413
+ const ys = [inside.y, area.y, area.y + area.height - r.height];
414
+ for (const { at } of near) {
415
+ xs.push(at.x - r.width, at.x + at.width);
416
+ ys.push(at.y - r.height, at.y + at.height);
417
+ }
418
+ const places = (list, from, size, extent) => [...new Set(list.map((v) => lo(v, from, from + extent - size)))];
419
+ const px = places(xs, area.x, r.width, area.width);
420
+ const py = places(ys, area.y, r.height, area.height);
421
+ const ylo = area.y;
422
+ const yhi = Math.max(area.y, area.y + area.height - r.height);
423
+ const byX = [...px].sort((a, b) => Math.abs(a - inside.x) - Math.abs(b - inside.x) || a - b);
424
+ let clear = null;
425
+ let work = 0;
426
+ let searchCapped = false;
427
+ for (const x of byX) {
428
+ const dx = Math.abs(x - inside.x);
429
+ if (clear && dx >= clear.far) break;
430
+ work += near.length + 1;
431
+ if (work > WORK) {
432
+ searchCapped = true;
433
+ break;
434
+ }
435
+ const stretches = [];
436
+ for (const { at } of near) {
437
+ if (Math.min(x + r.width, at.x + at.width) - Math.max(x, at.x) > EPS2) stretches.push([at.y - r.height, at.y + at.height]);
438
+ }
439
+ stretches.sort((a, b) => a[0] - b[0]);
440
+ let cursor = ylo;
441
+ let bestY = null;
442
+ const gap = (from, to) => {
443
+ if (from > to) return;
444
+ const y = Math.min(Math.max(inside.y, from), to);
445
+ if (bestY === null || Math.abs(y - inside.y) < Math.abs(bestY - inside.y)) bestY = y;
446
+ };
447
+ for (const [a, b] of stretches) {
448
+ gap(cursor, Math.min(a, yhi));
449
+ cursor = Math.max(cursor, b);
450
+ if (cursor > yhi) break;
451
+ }
452
+ if (cursor <= yhi) gap(cursor, yhi);
453
+ if (bestY === null) continue;
454
+ const far = Math.hypot(dx, bestY - inside.y);
455
+ if (!clear || far < clear.far) clear = { x, y: bestY, far };
456
+ }
457
+ if (clear) return { rect: rect(clear.x, clear.y, r.width, r.height), clamped, snapped: clear.far > EPS2, covers: [], searchCapped };
458
+ let best = null;
459
+ let tried = 0;
460
+ for (const p of byDistance(px, py, inside.x, inside.y)) {
461
+ if (tried++ >= COVERED_PLACES) break;
462
+ const cand = rect(p.x, p.y, r.width, r.height);
463
+ const left = near.reduce((s, n) => s + shared(cand, n.at), 0);
464
+ if (!best || left < best.left - EPS2) best = { rect: cand, left, far: p.far, hits: under(cand) };
465
+ }
466
+ const out = best ?? { rect: inside, far: 0, hits: under(inside) };
467
+ return { rect: out.rect, clamped, snapped: out.far > EPS2, covers: out.hits, searchCapped };
468
+ }
469
+ var PLANE = rect(-1e9, -1e9, 2e9, 2e9);
470
+ function stackFloatTarget(input) {
471
+ const { stage, size } = input;
472
+ const avoid = input.avoid ?? [];
473
+ const w = Math.max(0, finite3(size.width, 0));
474
+ const h = Math.max(0, finite3(size.height, 0));
475
+ return {
476
+ // Not "float": `./desk` has a target of that id, and a consumer may hold both.
477
+ id: `float:${input.id}`,
478
+ kind: "float",
479
+ priority: 3,
480
+ free: true,
481
+ hit: PLANE,
482
+ label: input.label ?? "Float here",
483
+ item: input.id,
484
+ make(point, grab) {
485
+ const g = grab ?? { x: w / 2, y: h / 2 };
486
+ const fit = fitFloat(rect(point.x - g.x, point.y - g.y, w, h), stage, avoid);
487
+ return { rect: fit.rect, fit };
488
+ }
489
+ };
490
+ }
491
+ var isSize = (n) => typeof n === "number" && Number.isFinite(n) && n > 0;
492
+ var isAt = (n) => typeof n === "number" && Number.isFinite(n);
493
+ function storeFloats(floats) {
494
+ const last = /* @__PURE__ */ new Map();
495
+ floats.forEach((f, i) => {
496
+ const r = f.rect;
497
+ if (isAt(r.x) && isAt(r.y) && isSize(r.width) && isSize(r.height)) last.set(f.id, i);
498
+ });
499
+ const out = [];
500
+ floats.forEach((f, i) => {
501
+ if (last.get(f.id) === i) out.push({ id: f.id, x: f.rect.x, y: f.rect.y, width: f.rect.width, height: f.rect.height });
502
+ });
503
+ return { v: 1, floats: out };
504
+ }
505
+ var isObject2 = (x) => typeof x === "object" && x !== null && !Array.isArray(x);
506
+ var FIELDS = ["id", "x", "y", "width", "height"];
507
+ function readFloats(data, known) {
508
+ try {
509
+ return read2(data, known);
510
+ } catch (e) {
511
+ return { ok: false, error: `unreadable: ${e instanceof Error ? e.message : typeof e}` };
512
+ }
513
+ }
514
+ function read2(data, known) {
515
+ if (!isObject2(data)) return { ok: false, error: "not an object" };
516
+ if (data.v !== 1) return { ok: false, error: `unknown version ${versionWords(data.v)} (this reads version 1)` };
517
+ if (!Array.isArray(data.floats)) return { ok: false, error: "floats is missing" };
518
+ const dropped = [];
519
+ const drop = (what) => {
520
+ if (!dropped.includes(what)) dropped.push(what);
521
+ };
522
+ for (const k of Object.keys(data)) if (k !== "v" && k !== "floats") drop(k);
523
+ const floats = [];
524
+ for (const x of data.floats) {
525
+ if (!isObject2(x)) {
526
+ drop(`a ${x === null ? "null" : Array.isArray(x) ? "list" : typeof x} where a float goes`);
527
+ continue;
528
+ }
529
+ const id = x.id;
530
+ if (typeof id !== "string") {
531
+ drop("a float with no id");
532
+ continue;
533
+ }
534
+ if (known && !known.includes(id) || !isAt(x.x) || !isAt(x.y) || !isSize(x.width) || !isSize(x.height)) {
535
+ drop(`float:${id}`);
536
+ continue;
537
+ }
538
+ for (const k of Object.keys(x)) {
539
+ if (k === "pinned") drop(`pinned:${id}`);
540
+ else if (!FIELDS.includes(k)) drop(`float:${id}:${k}`);
541
+ }
542
+ const before = floats.findIndex((f) => f.id === id);
543
+ if (before >= 0) {
544
+ floats.splice(before, 1);
545
+ drop(`float:${id}`);
546
+ }
547
+ floats.push({ id, rect: rect(x.x, x.y, x.width, x.height) });
548
+ }
549
+ return { ok: true, floats, dropped };
550
+ }
551
+
552
+ export {
553
+ DEFAULT_RAIL,
554
+ DEFAULT_MENU,
555
+ formSetsCross,
556
+ withItem,
557
+ fitStack,
558
+ createStack,
559
+ storeStack,
560
+ readStack,
561
+ fitFloat,
562
+ stackFloatTarget,
563
+ storeFloats,
564
+ readFloats
565
+ };
566
+ //# sourceMappingURL=chunk-RYFQKQPQ.js.map