@n-uf/motion 26.11.28 → 26.11.30

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.
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/shapes.ts","../src/values/step.ts","../src/values/settle.ts","../src/values/reveal.ts"],"sourcesContent":["/** A point or a 2D vector. */\nexport interface Point {\n x: number;\n y: number;\n}\n\n/** An axis-aligned rectangle. */\nexport interface Rect {\n x: number;\n y: number;\n width: number;\n height: number;\n}\n\n/** Any number of components. */\nexport type Vector = readonly number[];\n\n/** The value shapes springs and tweens move: plain numbers, nothing else. */\nexport type Animatable = number | Point | Rect | Vector;\n\n/** The shape type of an {@link Animatable}: literal numbers widen to `number`. */\nexport type Widen<T> = T extends number\n ? number\n : T extends Vector\n ? Vector\n : T extends Rect\n ? Rect\n : T extends Point\n ? Point\n : never;\n\nconst SCALAR = 0;\nconst VECTOR = 1;\nconst POINT = 2;\nconst RECT = 3;\n\n/** How a shape maps onto a flat list of numbers. Internal to values/. */\nexport interface Layout {\n readonly kind: typeof SCALAR | typeof VECTOR | typeof POINT | typeof RECT;\n readonly size: number;\n}\n\nconst SCALAR_LAYOUT: Layout = { kind: SCALAR, size: 1 };\nconst POINT_LAYOUT: Layout = { kind: POINT, size: 2 };\nconst RECT_LAYOUT: Layout = { kind: RECT, size: 4 };\n\nexport function layoutOf(value: Animatable): Layout {\n if (typeof value === \"number\") return SCALAR_LAYOUT;\n if (Array.isArray(value)) return { kind: VECTOR, size: value.length };\n if (\"width\" in value) return RECT_LAYOUT;\n return POINT_LAYOUT;\n}\n\nexport function sameLayout(a: Layout, b: Layout): boolean {\n return a.kind === b.kind && a.size === b.size;\n}\n\n/** Copy `value`'s components into `out`. */\nexport function writeComponents(value: Animatable, out: Float64Array): void {\n if (typeof value === \"number\") {\n out[0] = value;\n } else if (Array.isArray(value)) {\n for (let i = 0; i < value.length; i++) out[i] = value[i]!;\n } else if (\"width\" in value) {\n out[0] = value.x;\n out[1] = value.y;\n out[2] = value.width;\n out[3] = value.height;\n } else {\n const point = value as Point;\n out[0] = point.x;\n out[1] = point.y;\n }\n}\n\n/** A fresh value of `layout`'s shape from `components`. */\nexport function makeValue(layout: Layout, components: Float64Array): Animatable {\n switch (layout.kind) {\n case SCALAR:\n return components[0]!;\n case POINT:\n return { x: components[0]!, y: components[1]! };\n case RECT:\n return { x: components[0]!, y: components[1]!, width: components[2]!, height: components[3]! };\n default:\n return Array.from(components);\n }\n}\n\n/** Component-wise equality of two values of the same shape. */\nexport function shapeEquals(a: Animatable, b: Animatable): boolean {\n if (typeof a === \"number\" || typeof b === \"number\") return a === b;\n if (Array.isArray(a) || Array.isArray(b)) {\n const u = a as Vector;\n const v = b as Vector;\n if (u.length !== v.length) return false;\n for (let i = 0; i < u.length; i++) if (u[i] !== v[i]) return false;\n return true;\n }\n const p = a as Partial<Rect>;\n const q = b as Partial<Rect>;\n return p.x === q.x && p.y === q.y && p.width === q.width && p.height === q.height;\n}\n","import type { Animatable, Layout } from \"../shapes\";\nimport { layoutOf, makeValue, sameLayout, writeComponents } from \"../shapes\";\nimport type { Animator } from \"./animator\";\n\n/**\n * The exact critically damped step for one component, shared by the spring and the settle: displacement `d0` from the\n * target and velocity `v0` (per second) after `t` seconds, with `e` = e^(−w·t) worked out once per step. Writes into\n * out[0], out[1]: d(t) = e·(d0 + b·t), v(t) = e·(v0 − w·b·t), b = v0 + w·d0.\n */\nexport function criticalStep(w: number, e: number, d0: number, v0: number, t: number, out: Float64Array): void {\n const b = v0 + w * d0;\n out[0] = e * (d0 + b * t);\n out[1] = e * (v0 - w * b * t);\n}\n\n/**\n * What the spring and the settle share: a value of any {@link Animatable} shape kept as components (position, velocity\n * and target), its shape check, the rest test of a step, jump and snap. Each one adds only its own law: `begin` (what a\n * step works out once), `advance` (one component over `t` seconds) and `rests` (when a component counts as arrived).\n */\nexport abstract class ComponentAnimator implements Animator<Animatable> {\n readonly layout: Layout;\n readonly x: Float64Array;\n readonly v: Float64Array;\n readonly goal: Float64Array;\n readonly scratch = new Float64Array(2);\n settled = true;\n cachedValue: Animatable | null = null;\n /** How errors call it (\"spring\", \"settle\"). */\n private readonly name: string;\n\n constructor(initial: Animatable, name: string) {\n this.name = name;\n this.layout = layoutOf(initial);\n this.x = new Float64Array(this.layout.size);\n this.v = new Float64Array(this.layout.size);\n this.goal = new Float64Array(this.layout.size);\n writeComponents(initial, this.x);\n writeComponents(initial, this.goal);\n }\n\n get value(): Animatable {\n if (this.cachedValue === null) this.cachedValue = makeValue(this.layout, this.x);\n return this.cachedValue;\n }\n\n get velocity(): Animatable {\n return makeValue(this.layout, this.v);\n }\n\n get target(): Animatable {\n return makeValue(this.layout, this.goal);\n }\n\n check(value: Animatable, what: string): void {\n if (!sameLayout(layoutOf(value), this.layout)) {\n throw new TypeError(`${this.name}: ${what} has a different shape than the ${this.name}'s value`);\n }\n }\n\n /** At rest exactly when on the target and not moving. */\n updateSettled(): void {\n for (let i = 0; i < this.x.length; i++) {\n if (this.x[i] !== this.goal[i] || this.v[i] !== 0) {\n this.settled = false;\n return;\n }\n }\n this.settled = true;\n }\n\n abstract setTarget(target: Animatable): void;\n\n jumpTo(value: Animatable): void {\n this.check(value, \"the value\");\n writeComponents(value, this.x);\n writeComponents(value, this.goal);\n this.v.fill(0);\n this.cachedValue = null;\n this.settled = true;\n }\n\n snap(): void {\n this.x.set(this.goal);\n this.v.fill(0);\n this.cachedValue = null;\n this.settled = true;\n }\n\n /** Work out what every component's step of `t` seconds shares. */\n protected abstract begin(t: number): void;\n /** One component, `d0` from its target at `v0` per second, after `t` seconds: into out[0], out[1]. */\n protected abstract advance(d0: number, v0: number, t: number, out: Float64Array): void;\n /** Whether component `i`, now `d` from its target at `v` per second, counts as arrived. */\n protected abstract rests(i: number, d: number, v: number): boolean;\n\n step(dtMs: number): boolean {\n if (this.settled) return false;\n if (!(dtMs > 0)) return true;\n const t = dtMs / 1000;\n const { x, v, goal, scratch } = this;\n this.begin(t);\n let resting = true;\n for (let i = 0; i < x.length; i++) {\n this.advance(x[i]! - goal[i]!, v[i]!, t, scratch);\n x[i] = goal[i]! + scratch[0]!;\n v[i] = scratch[1]!;\n if (resting && !this.rests(i, scratch[0]!, scratch[1]!)) resting = false;\n }\n this.cachedValue = null;\n if (resting) this.snap();\n return !this.settled;\n }\n}\n","import type { Animatable, Widen } from \"../shapes\";\nimport { writeComponents } from \"../shapes\";\nimport type { Animator } from \"./animator\";\nimport { ComponentAnimator, criticalStep } from \"./step\";\n\n/**\n * Tempos for {@link createSettle}, in 1/s: a settle covers 90 % of the way in\n * ln(10)/omega seconds and is within 1 % in twice that.\n *\n * - `calm` 20: 90 % in 115 ms, done in 230 ms.\n * - `default` 25: 90 % in 92 ms, done in 184 ms.\n * - `quick` 37: 90 % in 62 ms, done in 124 ms (a fast Hyprland setup).\n */\nexport const SETTLE_TEMPO = { calm: 20, default: 25, quick: 37 } as const;\n\n/** Settings of {@link createSettle}. */\nexport interface SettleConfig {\n /** The tempo in 1/s (see {@link SETTLE_TEMPO}). Higher is faster. Default 25. */\n readonly omega?: number;\n /** Distance from the target that counts as arrived. Default 0.1 (tuned for pixels). */\n readonly restDelta?: number;\n}\n\n/**\n * The one curve for things that move to a new place: a window's rect between\n * layout targets, a drawer, a camera. Fast at the start, gentle at the end,\n * and it never passes its target.\n *\n * It is a critically damped spring (stiffness omega², damping 2·omega) with\n * one rule on its velocity, per component:\n *\n * - **Launch.** On a new target, each component gets the velocity\n * omega · distance toward it, from wherever it is drawn: standing still,\n * moving toward it, or moving away (it turns at once rather than drifting\n * further first). From there the spring follows `distance · e^(−omega·t)`\n * exactly: most of the move happens at once, the rest eases in.\n * - **Clamp.** That speed is also the most it may have toward the target, so a\n * velocity set by hand (`setVelocity`, a fling) toward the target is capped\n * there instead of shooting past.\n * - **Carry.** A velocity set by hand away from the target is kept, and the\n * value turns around; a critically damped spring turning around never\n * crosses the target either.\n *\n * So after a new target every component goes straight from where it is drawn\n * to the target and never past it: no overshoot on position, no wobble on\n * size, whatever the retargets. A new target starts from the drawn value,\n * never from the old start.\n *\n * Holding (a pointer owns the value): call `hold(value)` on each move. The\n * value sits exactly there, at rest; the next `setTarget` launches from it.\n */\nexport interface Settle<T> extends Animator<T> {\n /** The tempo in 1/s. */\n readonly omega: number;\n /** Put the value at `value`, at rest, with it as the target: a held value follows its owner 1:1. Same as `jumpTo`. */\n hold(value: T): void;\n /** Set the velocity (units per second); a component moving toward the target is clamped as in the launch rule. */\n setVelocity(velocity: T): void;\n /** Change the tempo or rest distance mid-flight; the value and velocity carry on, re-clamped. */\n configure(config: SettleConfig): void;\n}\n\n/** Defaults of {@link SettleConfig}. */\nexport const SETTLE_DEFAULTS = { omega: SETTLE_TEMPO.default, restDelta: 0.1 } as const;\n\nfunction positive(name: string, value: number): number {\n if (!(value > 0) || !Number.isFinite(value)) throw new RangeError(`settle: ${name} must be a finite number > 0, got ${value}`);\n return value;\n}\n\nclass SettleAnimator extends ComponentAnimator implements Settle<Animatable> {\n omega: number;\n restDelta: number;\n // One step's shared numbers (`begin`): e^(−omega·t) and the rest speed.\n private e = 1;\n private restSpeed = 0;\n\n constructor(initial: Animatable, config: SettleConfig) {\n super(initial, \"settle\");\n this.omega = positive(\"omega\", config.omega ?? SETTLE_DEFAULTS.omega);\n this.restDelta = positive(\"restDelta\", config.restDelta ?? SETTLE_DEFAULTS.restDelta);\n }\n\n /** The launch/clamp/carry rule on component `i`: `launch` for a new target, else clamp or carry. */\n rule(i: number, launch: boolean): void {\n const d = this.x[i]! - this.goal[i]!;\n const v = this.v[i]!;\n const limit = d === 0 ? 0 : -this.omega * d;\n if (launch) {\n this.v[i] = limit;\n return;\n }\n if (d === 0) return;\n // Moving away from the target (d and v the same sign): carry.\n if (d * v > 0) return;\n // Moving toward it: no faster than a launch from here.\n if (Math.abs(v) > Math.abs(limit)) this.v[i] = limit;\n }\n\n setTarget(target: Animatable): void {\n this.check(target, \"the target\");\n writeComponents(target, this.goal);\n for (let i = 0; i < this.x.length; i++) this.rule(i, true);\n this.updateSettled();\n }\n\n setVelocity(velocity: Animatable): void {\n this.check(velocity, \"the velocity\");\n writeComponents(velocity, this.v);\n for (let i = 0; i < this.x.length; i++) this.rule(i, false);\n this.updateSettled();\n }\n\n hold(value: Animatable): void {\n this.jumpTo(value);\n }\n\n configure(config: SettleConfig): void {\n if (config.omega !== undefined) this.omega = positive(\"omega\", config.omega);\n if (config.restDelta !== undefined) this.restDelta = positive(\"restDelta\", config.restDelta);\n for (let i = 0; i < this.x.length; i++) this.rule(i, false);\n }\n\n protected begin(t: number): void {\n this.e = Math.exp(-this.omega * t);\n // At rest when every component is within restDelta and no faster than a launch from there would be.\n this.restSpeed = this.omega * this.restDelta * 1.000001;\n }\n\n protected advance(d0: number, v0: number, t: number, out: Float64Array): void {\n criticalStep(this.omega, this.e, d0, v0, t, out);\n }\n\n protected rests(i: number, d: number, v: number): boolean {\n // Within a few float steps of the goal counts as arrived too: a restDelta finer than that would never be met.\n const near = Math.max(this.restDelta, 4 * Number.EPSILON * Math.abs(this.goal[i]!));\n return !(Math.abs(d) > near || Math.abs(v) > Math.max(this.restSpeed, this.omega * near));\n }\n}\n\n/**\n * A settle starting at rest on `initial` (a number, a vector, a point or a\n * rect; targets and velocities must have the same shape). See {@link Settle}.\n */\nexport function createSettle<T extends Animatable>(initial: T, config: SettleConfig = {}): Settle<Widen<T>> {\n return new SettleAnimator(initial, config) as unknown as Settle<Widen<T>>;\n}\n","import type { Rect } from \"../shapes\";\n\n/**\n * How to draw a window whose rect is gliding without stretching its content.\n *\n * The content is laid out once at the target size and never scaled. The\n * window's element sits at its target in the layout; to draw it at the\n * gliding rect:\n *\n * - move the element by (`dx`, `dy`), so its top-left corner is the drawn rect's;\n * - draw the frame (background, border, shadow) at `width` × `height`: the drawn size;\n * - clip the content by `clipRight` and `clipBottom` so it fits a frame\n * smaller than the target (CSS: `clip-path: inset(0 clipRight clipBottom 0)`).\n *\n * A window that shrinks shows a band of its own background closing in at the\n * right and bottom; one that grows reveals content already laid out at its\n * new size. Text is never stretched. One transform and one clip per frame, no\n * layout per frame.\n */\nexport interface RevealFrame {\n /** Move the element (laid out at the target) right by this much. */\n readonly dx: number;\n /** Move the element (laid out at the target) down by this much. */\n readonly dy: number;\n /** The frame's width: the drawn rect's. */\n readonly width: number;\n /** The frame's height: the drawn rect's. */\n readonly height: number;\n /** Clip this much off the content's right edge (0 when the frame is at least as wide as the target). */\n readonly clipRight: number;\n /** Clip this much off the content's bottom edge (0 when the frame is at least as tall as the target). */\n readonly clipBottom: number;\n}\n\n/** The {@link RevealFrame} for content laid out at `target` and drawn at `drawn`. */\nexport function reveal(drawn: Rect, target: Rect): RevealFrame {\n return {\n dx: drawn.x - target.x,\n dy: drawn.y - target.y,\n width: Math.max(0, drawn.width),\n height: Math.max(0, drawn.height),\n clipRight: Math.max(0, target.width - drawn.width),\n clipBottom: Math.max(0, target.height - drawn.height),\n };\n}\n\n/** A uniform pop-in (or pop-out, run backwards): scale about the centre and opacity. */\nexport interface PopIn {\n /** Scale about the window's centre: the same on both axes, so nothing is stretched. */\n readonly scale: number;\n readonly opacity: number;\n}\n\n/**\n * Opening and closing: the window grows from `from` of its size to full, about\n * its centre, while fading in. `progress` runs 0 to 1 (take it from a settle\n * on a number with `{ restDelta: 0.001 }`, since the default 0.1 is tuned for\n * pixels and would snap the last 10 %; or from a tween); closing runs it from\n * 1 to 0. Hyprland's default is `popin 87%`.\n */\nexport function popIn(progress: number, from = 0.87): PopIn {\n const p = Math.min(1, Math.max(0, progress));\n return { scale: from + (1 - from) * p, opacity: p };\n}\n"],"mappings":";AA+BA,IAAM,SAAS;AACf,IAAM,SAAS;AACf,IAAM,QAAQ;AACd,IAAM,OAAO;AAQb,IAAM,gBAAwB,EAAE,MAAM,QAAQ,MAAM,EAAE;AACtD,IAAM,eAAuB,EAAE,MAAM,OAAO,MAAM,EAAE;AACpD,IAAM,cAAsB,EAAE,MAAM,MAAM,MAAM,EAAE;AAE3C,SAAS,SAAS,OAA2B;AACl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A,EACzF;AACF;AAMO,SAAS,aAAmC,SAAY,SAAuB,CAAC,GAAqB;AAC1G,SAAO,IAAI,eAAe,SAAS,MAAM;AAC3C;;;AC/GO,SAAS,OAAO,OAAa,QAA2B;AAC7D,SAAO;AAAA,IACL,IAAI,MAAM,IAAI,OAAO;AAAA,IACrB,IAAI,MAAM,IAAI,OAAO;AAAA,IACrB,OAAO,KAAK,IAAI,GAAG,MAAM,KAAK;AAAA,IAC9B,QAAQ,KAAK,IAAI,GAAG,MAAM,MAAM;AAAA,IAChC,WAAW,KAAK,IAAI,GAAG,OAAO,QAAQ,MAAM,KAAK;AAAA,IACjD,YAAY,KAAK,IAAI,GAAG,OAAO,SAAS,MAAM,MAAM;AAAA,EACtD;AACF;AAgBO,SAAS,MAAM,UAAkB,OAAO,MAAa;AAC1D,QAAM,IAAI,KAAK,IAAI,GAAG,KAAK,IAAI,GAAG,QAAQ,CAAC;AAC3C,SAAO,EAAE,OAAO,QAAQ,IAAI,QAAQ,GAAG,SAAS,EAAE;AACpD;","names":[]}
1
+ {"version":3,"sources":["../src/shapes.ts","../src/values/step.ts","../src/values/settle.ts","../src/values/reveal.ts"],"sourcesContent":["/** A point or a 2D vector. */\nexport interface Point {\n x: number;\n y: number;\n}\n\n/** An axis-aligned rectangle. */\nexport interface Rect {\n x: number;\n y: number;\n width: number;\n height: number;\n}\n\n/** Any number of components. */\nexport type Vector = readonly number[];\n\n/** The value shapes springs and tweens move: plain numbers, nothing else. */\nexport type Animatable = number | Point | Rect | Vector;\n\n/** The shape type of an {@link Animatable}: literal numbers widen to `number`. */\nexport type Widen<T> = T extends number\n ? number\n : T extends Vector\n ? Vector\n : T extends Rect\n ? Rect\n : T extends Point\n ? Point\n : never;\n\nconst SCALAR = 0;\nconst VECTOR = 1;\nconst POINT = 2;\nconst RECT = 3;\n\n/** How a shape maps onto a flat list of numbers. Internal to values/. */\nexport interface Layout {\n readonly kind: typeof SCALAR | typeof VECTOR | typeof POINT | typeof RECT;\n readonly size: number;\n}\n\nconst SCALAR_LAYOUT: Layout = { kind: SCALAR, size: 1 };\nconst POINT_LAYOUT: Layout = { kind: POINT, size: 2 };\nconst RECT_LAYOUT: Layout = { kind: RECT, size: 4 };\n\nexport function layoutOf(value: Animatable): Layout {\n if (typeof value === \"number\") return SCALAR_LAYOUT;\n if (Array.isArray(value)) return { kind: VECTOR, size: value.length };\n if (\"width\" in value) return RECT_LAYOUT;\n return POINT_LAYOUT;\n}\n\nexport function sameLayout(a: Layout, b: Layout): boolean {\n return a.kind === b.kind && a.size === b.size;\n}\n\n/** Copy `value`'s components into `out`. */\nexport function writeComponents(value: Animatable, out: Float64Array): void {\n if (typeof value === \"number\") {\n out[0] = value;\n } else if (Array.isArray(value)) {\n for (let i = 0; i < value.length; i++) out[i] = value[i]!;\n } else if (\"width\" in value) {\n out[0] = value.x;\n out[1] = value.y;\n out[2] = value.width;\n out[3] = value.height;\n } else {\n const point = value as Point;\n out[0] = point.x;\n out[1] = point.y;\n }\n}\n\n/** A fresh value of `layout`'s shape from `components`. */\nexport function makeValue(layout: Layout, components: Float64Array): Animatable {\n switch (layout.kind) {\n case SCALAR:\n return components[0]!;\n case POINT:\n return { x: components[0]!, y: components[1]! };\n case RECT:\n return { x: components[0]!, y: components[1]!, width: components[2]!, height: components[3]! };\n default:\n return Array.from(components);\n }\n}\n\n/** Component-wise equality of two values of the same shape. */\nexport function shapeEquals(a: Animatable, b: Animatable): boolean {\n if (typeof a === \"number\" || typeof b === \"number\") return a === b;\n if (Array.isArray(a) || Array.isArray(b)) {\n const u = a as Vector;\n const v = b as Vector;\n if (u.length !== v.length) return false;\n for (let i = 0; i < u.length; i++) if (u[i] !== v[i]) return false;\n return true;\n }\n const p = a as Partial<Rect>;\n const q = b as Partial<Rect>;\n return p.x === q.x && p.y === q.y && p.width === q.width && p.height === q.height;\n}\n","import type { Animatable, Layout } from \"../shapes\";\nimport { layoutOf, makeValue, sameLayout, writeComponents } from \"../shapes\";\nimport type { Animator } from \"./animator\";\n\n/**\n * The exact critically damped step for one component, shared by the spring and the settle: displacement `d0` from the\n * target and velocity `v0` (per second) after `t` seconds, with `e` = e^(−w·t) worked out once per step. Writes into\n * out[0], out[1]: d(t) = e·(d0 + b·t), v(t) = e·(v0 − w·b·t), b = v0 + w·d0.\n */\nexport function criticalStep(w: number, e: number, d0: number, v0: number, t: number, out: Float64Array): void {\n const b = v0 + w * d0;\n out[0] = e * (d0 + b * t);\n out[1] = e * (v0 - w * b * t);\n}\n\n/**\n * What the spring and the settle share: a value of any {@link Animatable} shape kept as components (position, velocity\n * and target), its shape check, the rest test of a step, jump and snap. Each one adds only its own law: `begin` (what a\n * step works out once), `advance` (one component over `t` seconds) and `rests` (when a component counts as arrived).\n */\nexport abstract class ComponentAnimator implements Animator<Animatable> {\n readonly layout: Layout;\n readonly x: Float64Array;\n readonly v: Float64Array;\n readonly goal: Float64Array;\n readonly scratch = new Float64Array(2);\n settled = true;\n cachedValue: Animatable | null = null;\n /** How errors call it (\"spring\", \"settle\"). */\n private readonly name: string;\n\n constructor(initial: Animatable, name: string) {\n this.name = name;\n this.layout = layoutOf(initial);\n this.x = new Float64Array(this.layout.size);\n this.v = new Float64Array(this.layout.size);\n this.goal = new Float64Array(this.layout.size);\n writeComponents(initial, this.x);\n writeComponents(initial, this.goal);\n }\n\n get value(): Animatable {\n if (this.cachedValue === null) this.cachedValue = makeValue(this.layout, this.x);\n return this.cachedValue;\n }\n\n get velocity(): Animatable {\n return makeValue(this.layout, this.v);\n }\n\n get target(): Animatable {\n return makeValue(this.layout, this.goal);\n }\n\n check(value: Animatable, what: string): void {\n if (!sameLayout(layoutOf(value), this.layout)) {\n throw new TypeError(`${this.name}: ${what} has a different shape than the ${this.name}'s value`);\n }\n }\n\n /** At rest exactly when on the target and not moving. */\n updateSettled(): void {\n for (let i = 0; i < this.x.length; i++) {\n if (this.x[i] !== this.goal[i] || this.v[i] !== 0) {\n this.settled = false;\n return;\n }\n }\n this.settled = true;\n }\n\n abstract setTarget(target: Animatable): void;\n\n jumpTo(value: Animatable): void {\n this.check(value, \"the value\");\n writeComponents(value, this.x);\n writeComponents(value, this.goal);\n this.v.fill(0);\n this.cachedValue = null;\n this.settled = true;\n }\n\n snap(): void {\n this.x.set(this.goal);\n this.v.fill(0);\n this.cachedValue = null;\n this.settled = true;\n }\n\n /** Work out what every component's step of `t` seconds shares. */\n protected abstract begin(t: number): void;\n /** One component, `d0` from its target at `v0` per second, after `t` seconds: into out[0], out[1]. */\n protected abstract advance(d0: number, v0: number, t: number, out: Float64Array): void;\n /** Whether component `i`, now `d` from its target at `v` per second, counts as arrived. */\n protected abstract rests(i: number, d: number, v: number): boolean;\n\n step(dtMs: number): boolean {\n if (this.settled) return false;\n if (!(dtMs > 0)) return true;\n const t = dtMs / 1000;\n const { x, v, goal, scratch } = this;\n this.begin(t);\n let resting = true;\n for (let i = 0; i < x.length; i++) {\n this.advance(x[i]! - goal[i]!, v[i]!, t, scratch);\n x[i] = goal[i]! + scratch[0]!;\n v[i] = scratch[1]!;\n if (resting && !this.rests(i, scratch[0]!, scratch[1]!)) resting = false;\n }\n this.cachedValue = null;\n if (resting) this.snap();\n return !this.settled;\n }\n}\n","import type { Animatable, Widen } from \"../shapes\";\nimport { writeComponents } from \"../shapes\";\nimport type { Animator } from \"./animator\";\nimport { ComponentAnimator, criticalStep } from \"./step\";\n\n/**\n * Tempos for {@link createSettle}, in 1/s: a settle covers 90 % of the way in\n * ln(10)/omega seconds and is within 1 % in twice that.\n *\n * - `calm` 20: 90 % in 115 ms, done in 230 ms.\n * - `default` 25: 90 % in 92 ms, done in 184 ms.\n * - `quick` 37: 90 % in 62 ms, done in 124 ms (a fast Hyprland setup).\n */\nexport const SETTLE_TEMPO = { calm: 20, default: 25, quick: 37 } as const;\n\n/** Settings of {@link createSettle}. */\nexport interface SettleConfig {\n /** The tempo in 1/s (see {@link SETTLE_TEMPO}). Higher is faster. Default 25. */\n readonly omega?: number;\n /** Distance from the target that counts as arrived. Default 0.1 (tuned for pixels). */\n readonly restDelta?: number;\n}\n\n/**\n * The one curve for things that move to a new place: a window's rect between\n * layout targets, a drawer, a camera. Fast at the start, gentle at the end,\n * and it never passes its target.\n *\n * It is a critically damped spring (stiffness omega², damping 2·omega) with\n * one rule on its velocity, per component:\n *\n * - **Launch.** On a new target, each component gets the velocity\n * omega · distance toward it, from wherever it is drawn: standing still,\n * moving toward it, or moving away (it turns at once rather than drifting\n * further first). From there the spring follows `distance · e^(−omega·t)`\n * exactly: most of the move happens at once, the rest eases in.\n * - **Clamp.** That speed is also the most it may have toward the target, so a\n * velocity set by hand (`setVelocity`, a fling) toward the target is capped\n * there instead of shooting past.\n * - **Carry.** A velocity set by hand away from the target is kept, and the\n * value turns around; a critically damped spring turning around never\n * crosses the target either.\n *\n * So after a new target every component goes straight from where it is drawn\n * to the target and never past it: no overshoot on position, no wobble on\n * size, whatever the retargets. A new target starts from the drawn value,\n * never from the old start.\n *\n * Holding (a pointer owns the value): call `hold(value)` on each move. The\n * value sits exactly there, at rest; the next `setTarget` launches from it.\n */\nexport interface Settle<T> extends Animator<T> {\n /** The tempo in 1/s. */\n readonly omega: number;\n /** Put the value at `value`, at rest, with it as the target: a held value follows its owner 1:1. Same as `jumpTo`. */\n hold(value: T): void;\n /** Set the velocity (units per second); a component moving toward the target is clamped as in the launch rule. */\n setVelocity(velocity: T): void;\n /** Change the tempo or rest distance mid-flight; the value and velocity carry on, re-clamped. */\n configure(config: SettleConfig): void;\n}\n\n/** Defaults of {@link SettleConfig}. */\nexport const SETTLE_DEFAULTS = { omega: SETTLE_TEMPO.default, restDelta: 0.1 } as const;\n\nfunction positive(name: string, value: number): number {\n if (!(value > 0) || !Number.isFinite(value)) throw new RangeError(`settle: ${name} must be a finite number > 0, got ${value}`);\n return value;\n}\n\nclass SettleAnimator extends ComponentAnimator implements Settle<Animatable> {\n omega: number;\n restDelta: number;\n // One step's shared numbers (`begin`): e^(−omega·t) and the rest speed.\n private e = 1;\n private restSpeed = 0;\n\n constructor(initial: Animatable, config: SettleConfig) {\n super(initial, \"settle\");\n this.omega = positive(\"omega\", config.omega ?? SETTLE_DEFAULTS.omega);\n this.restDelta = positive(\"restDelta\", config.restDelta ?? SETTLE_DEFAULTS.restDelta);\n }\n\n /** The launch/clamp/carry rule on component `i`: `launch` for a new target, else clamp or carry. */\n rule(i: number, launch: boolean): void {\n const d = this.x[i]! - this.goal[i]!;\n const v = this.v[i]!;\n const limit = d === 0 ? 0 : -this.omega * d;\n if (launch) {\n this.v[i] = limit;\n return;\n }\n if (d === 0) return;\n // Moving away from the target (d and v the same sign): carry.\n if (d * v > 0) return;\n // Moving toward it: no faster than a launch from here.\n if (Math.abs(v) > Math.abs(limit)) this.v[i] = limit;\n }\n\n setTarget(target: Animatable): void {\n this.check(target, \"the target\");\n writeComponents(target, this.goal);\n for (let i = 0; i < this.x.length; i++) this.rule(i, true);\n this.updateSettled();\n }\n\n setVelocity(velocity: Animatable): void {\n this.check(velocity, \"the velocity\");\n writeComponents(velocity, this.v);\n for (let i = 0; i < this.x.length; i++) this.rule(i, false);\n this.updateSettled();\n }\n\n hold(value: Animatable): void {\n this.jumpTo(value);\n }\n\n configure(config: SettleConfig): void {\n if (config.omega !== undefined) this.omega = positive(\"omega\", config.omega);\n if (config.restDelta !== undefined) this.restDelta = positive(\"restDelta\", config.restDelta);\n for (let i = 0; i < this.x.length; i++) this.rule(i, false);\n }\n\n protected begin(t: number): void {\n this.e = Math.exp(-this.omega * t);\n // At rest when every component is within restDelta and no faster than a launch from there would be.\n this.restSpeed = this.omega * this.restDelta * 1.000001;\n }\n\n protected advance(d0: number, v0: number, t: number, out: Float64Array): void {\n criticalStep(this.omega, this.e, d0, v0, t, out);\n }\n\n protected rests(i: number, d: number, v: number): boolean {\n // Within a few float steps of the goal counts as arrived too: a restDelta finer than that would never be met.\n const near = Math.max(this.restDelta, 4 * Number.EPSILON * Math.abs(this.goal[i]!));\n return !(Math.abs(d) > near || Math.abs(v) > Math.max(this.restSpeed, this.omega * near));\n }\n}\n\n/**\n * A settle starting at rest on `initial` (a number, a vector, a point or a\n * rect; targets and velocities must have the same shape). See {@link Settle}.\n */\nexport function createSettle<T extends Animatable>(initial: T, config: SettleConfig = {}): Settle<Widen<T>> {\n return new SettleAnimator(initial, config) as unknown as Settle<Widen<T>>;\n}\n","import type { Rect } from \"../shapes\";\n\n/**\n * How to draw a window whose rect is gliding without stretching its content.\n *\n * The content is laid out once at the target size and never scaled. The\n * window's element sits at its target in the layout; to draw it at the\n * gliding rect:\n *\n * - move the element by (`dx`, `dy`), so its top-left corner is the drawn rect's;\n * - draw the frame (background, border, shadow) at `width` × `height`: the drawn size;\n * - clip the content by `clipRight` and `clipBottom` so it fits a frame\n * smaller than the target (CSS: `clip-path: inset(0 clipRight clipBottom 0)`).\n *\n * A window that shrinks shows a band of its own background closing in at the\n * right and bottom; one that grows reveals content already laid out at its\n * new size. Text is never stretched. One transform and one clip per frame, no\n * layout per frame.\n */\nexport interface RevealFrame {\n /** Move the element (laid out at the target) right by this much. */\n readonly dx: number;\n /** Move the element (laid out at the target) down by this much. */\n readonly dy: number;\n /** The frame's width: the drawn rect's. */\n readonly width: number;\n /** The frame's height: the drawn rect's. */\n readonly height: number;\n /** Clip this much off the content's right edge (0 when the frame is at least as wide as the target). */\n readonly clipRight: number;\n /** Clip this much off the content's bottom edge (0 when the frame is at least as tall as the target). */\n readonly clipBottom: number;\n}\n\n/** The {@link RevealFrame} for content laid out at `target` and drawn at `drawn`. */\nexport function reveal(drawn: Rect, target: Rect): RevealFrame {\n return {\n dx: drawn.x - target.x,\n dy: drawn.y - target.y,\n width: Math.max(0, drawn.width),\n height: Math.max(0, drawn.height),\n clipRight: Math.max(0, target.width - drawn.width),\n clipBottom: Math.max(0, target.height - drawn.height),\n };\n}\n\n/** A uniform pop-in (or pop-out, run backwards): scale about the center and opacity. */\nexport interface PopIn {\n /** Scale about the window's center: the same on both axes, so nothing is stretched. */\n readonly scale: number;\n readonly opacity: number;\n}\n\n/**\n * Opening and closing: the window grows from `from` of its size to full, about\n * its center, while fading in. `progress` runs 0 to 1 (take it from a settle\n * on a number with `{ restDelta: 0.001 }`, since the default 0.1 is tuned for\n * pixels and would snap the last 10 %; or from a tween); closing runs it from\n * 1 to 0. Hyprland's default is `popin 87%`.\n */\nexport function popIn(progress: number, from = 0.87): PopIn {\n const p = Math.min(1, Math.max(0, progress));\n return { scale: from + (1 - from) * p, opacity: p };\n}\n"],"mappings":";AA+BA,IAAM,SAAS;AACf,IAAM,SAAS;AACf,IAAM,QAAQ;AACd,IAAM,OAAO;AAQb,IAAM,gBAAwB,EAAE,MAAM,QAAQ,MAAM,EAAE;AACtD,IAAM,eAAuB,EAAE,MAAM,OAAO,MAAM,EAAE;AACpD,IAAM,cAAsB,EAAE,MAAM,MAAM,MAAM,EAAE;AAE3C,SAAS,SAAS,OAA2B;AAClD,MAAI,OAAO,UAAU,SAAU,QAAO;AACtC,MAAI,MAAM,QAAQ,KAAK,EAAG,QAAO,EAAE,MAAM,QAAQ,MAAM,MAAM,OAAO;AACpE,MAAI,WAAW,MAAO,QAAO;AAC7B,SAAO;AACT;AAEO,SAAS,WAAW,GAAW,GAAoB;AACxD,SAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,SAAS,EAAE;AAC3C;AAGO,SAAS,gBAAgB,OAAmB,KAAyB;AAC1E,MAAI,OAAO,UAAU,UAAU;AAC7B,QAAI,CAAC,IAAI;AAAA,EACX,WAAW,MAAM,QAAQ,KAAK,GAAG;AAC/B,aAAS,IAAI,GAAG,IAAI,MAAM,QAAQ,IAAK,KAAI,CAAC,IAAI,MAAM,CAAC;AAAA,EACzD,WAAW,WAAW,OAAO;AAC3B,QAAI,CAAC,IAAI,MAAM;AACf,QAAI,CAAC,IAAI,MAAM;AACf,QAAI,CAAC,IAAI,MAAM;AACf,QAAI,CAAC,IAAI,MAAM;AAAA,EACjB,OAAO;AACL,UAAM,QAAQ;AACd,QAAI,CAAC,IAAI,MAAM;AACf,QAAI,CAAC,IAAI,MAAM;AAA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} from "./chunk-43DOPJGH.js";
16
+ } from "./chunk-SIAZRWYD.js";
17
17
 
18
18
  // src/values/easing.ts
19
19
  var linear = (t) => t;
@@ -442,4 +442,4 @@ export {
442
442
  settleValue,
443
443
  tweenValue
444
444
  };
445
- //# sourceMappingURL=chunk-G3CMPDOL.js.map
445
+ //# sourceMappingURL=chunk-V7DEXKBZ.js.map
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * A double click: two presses within `ms` of each other (press to press) and `px` of the first. One value for the whole
3
- * package: the gesture recogniser's `clickCount` (`RECOGNIZER_DEFAULTS.multiClick`) and a grip's double-activate
3
+ * package: the gesture recognizer's `clickCount` (`RECOGNIZER_DEFAULTS.multiClick`) and a grip's double-activate
4
4
  * (`createGripPress`) both default to it.
5
5
  */
6
6
  interface DoubleClick {
package/dist/drag.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { R as Rect, P as Point } from './shapes-EbQlwbCu.js';
2
- import { a as DoubleClick } from './double-click-BIMG1pzp.js';
3
- export { D as DOUBLE_CLICK } from './double-click-BIMG1pzp.js';
2
+ import { a as DoubleClick } from './double-click-jtUSNuHL.js';
3
+ export { D as DOUBLE_CLICK } from './double-click-jtUSNuHL.js';
4
4
 
5
5
  /**
6
6
  * The drag intent model's tuning. Distances in px, speeds in px/ms, times in
@@ -28,7 +28,7 @@ interface DragTuning {
28
28
  /**
29
29
  * The band and the breakthrough scale with the zones: along each axis they are at most this share of the held
30
30
  * zone's extent and of the extent of the zone under the pointer (its `hit`), so a small zone (a 28 px row) can be
31
- * entered by a direct glide from its neighbour. Below a quarter, the pointer at a quarter of a zone is past them.
31
+ * entered by a direct glide from its neighbor. Below a quarter, the pointer at a quarter of a zone is past them.
32
32
  */
33
33
  readonly bandShare: number;
34
34
  /** A gap between zones shorter than this keeps the current target (for a pointer that aims). */
@@ -39,13 +39,50 @@ interface DragTuning {
39
39
  readonly roomAfter: number;
40
40
  /** The pointer has left the item's home once it is outside the home shrunk by this much on every side. */
41
41
  readonly homeInset: number;
42
+ /**
43
+ * Spring-loading: a target marked `arm: "dwell"` takes the drop only once the pointer has stayed in its zone this long
44
+ * (ms); before that it is no target, so a fast pass, or a drop before it, lands on what lies beneath. 0 arms it at once.
45
+ * Armed, its zone is its `armedHit` (the magnet: its band); moving out past that band's outer edge disarms it, and
46
+ * re-entering needs a new full dwell. It arms only after the pointer has ENTERED its zone during the drag: the point
47
+ * the drag starts at counts as not entered, so a drag that starts inside the zone (a panel's grip under the strip at
48
+ * the window's edge) must leave it and come back before it can arm; entering from over home counts.
49
+ */
50
+ readonly railArmDwellMs: number;
51
+ /** The keys that win over geometry while held (see {@link DragModifiers}). */
52
+ readonly modifiers: DragModifiers;
53
+ }
54
+ /**
55
+ * A modifier key, by its `KeyboardEvent.key` name, or `"Mod"`: ⌘ (Meta) on macOS and Ctrl elsewhere. `"Mod"` counts
56
+ * either key as held, so no platform detection is needed.
57
+ */
58
+ type ModifierKey = "Shift" | "Alt" | "Control" | "Meta" | "Mod";
59
+ /** The modifier keys held on a sample (a pointer event's `shiftKey`, `altKey`, `ctrlKey`, `metaKey`). */
60
+ interface DragKeys {
61
+ readonly shift?: boolean;
62
+ readonly alt?: boolean;
63
+ readonly ctrl?: boolean;
64
+ readonly meta?: boolean;
65
+ }
66
+ /**
67
+ * Keys that win over geometry: while `floatKey` is held the drop floats (the `free` target) wherever the pointer is;
68
+ * while `railKey` is held it goes to the nearest target marked `arm` (a side's rail), without reaching it. `floatKey`
69
+ * wins when both are held. `on: false` turns both off.
70
+ */
71
+ interface DragModifiers {
72
+ readonly on: boolean;
73
+ readonly floatKey: ModifierKey;
74
+ readonly railKey: ModifierKey;
42
75
  }
43
76
  /**
44
77
  * The defaults: a mouse lifts after 5 px, a touch after a 180 ms hold and
45
78
  * 8 px; aiming is slower than 0.45 px/ms over 50 ms, or within 12 px for
46
79
  * 90 ms; a 10 px band and a 4 px breakthrough, each at most a fifth of the
47
80
  * zones' extents, and a 120 ms gap grace; a switch waits 150 ms (the shortest
48
- * an indicator may show); room after 160 ms.
81
+ * an indicator may show); room after 160 ms. A spring-loaded target arms after
82
+ * 200 ms of dwell (long enough that a pass at travelling speed never arms it,
83
+ * short enough to feel like the platforms' spring-loading); Shift floats, ⌘/Ctrl
84
+ * (`"Mod"`) goes to the nearest rail. Alt is left free: an app with a frozen
85
+ * layout uses it to unfreeze temporarily, and a key should have one meaning.
49
86
  */
50
87
  declare const DRAG_DEFAULTS: DragTuning;
51
88
  /**
@@ -56,6 +93,10 @@ declare const DRAG_DEFAULTS: DragTuning;
56
93
  * floats where it is dropped): no room is made for it. `hits`, when given, is
57
94
  * the whole zone as several rects (a zone that isn't one rect, `hit` the
58
95
  * largest of them): the pointer is in the zone when it is in any of them.
96
+ *
97
+ * `arm: "dwell"` makes it spring-loaded: it is no target until the pointer has
98
+ * dwelt in its zone for `railArmDwellMs`; once armed its zone is `armedHit`
99
+ * when given (a magnet: the zone grown), and leaving that disarms it.
59
100
  */
60
101
  interface DragTarget {
61
102
  readonly id: string;
@@ -65,6 +106,8 @@ interface DragTarget {
65
106
  readonly slot?: Rect;
66
107
  readonly label?: string;
67
108
  readonly free?: boolean;
109
+ readonly arm?: "dwell";
110
+ readonly armedHit?: Rect;
68
111
  }
69
112
  /** Whether `point` lies in `rect` grown by `margin` px on every side (shrunk, for a negative margin). */
70
113
  declare function inRect(rect: Rect, point: Point, margin?: number): boolean;
@@ -105,6 +148,13 @@ interface DragFrame<T extends DragTarget> {
105
148
  * rect); the intent still shows no target.
106
149
  */
107
150
  readonly atHome: boolean;
151
+ /**
152
+ * A spring-loaded target (`arm: "dwell"`) the pointer is in and dwelling on, not armed yet (a host highlights it), or
153
+ * null. Once it arms it is a target like any other and may show.
154
+ */
155
+ readonly arming: T | null;
156
+ /** A key held wins over geometry this frame: `"float"` (the float key) or `"rail"` (the rail key), else null. */
157
+ readonly forced: "float" | "rail" | null;
108
158
  }
109
159
  /** What a drag intent knows now. */
110
160
  interface DragIntentState<T extends DragTarget> {
@@ -150,17 +200,17 @@ interface DragIntent<T extends DragTarget> {
150
200
  * A pointer sample at `time` (ms), with the targets as they stand (they may
151
201
  * be rebuilt between calls: the model follows them by id). Call it once a
152
202
  * frame while the item is held, also when the pointer doesn't move: a
153
- * dwell is time passing.
203
+ * dwell is time passing. `keys`: the modifier keys held now (see {@link DragModifiers}).
154
204
  */
155
- update(point: Point, time: number, targets: readonly T[]): DragFrame<T>;
205
+ update(point: Point, time: number, targets: readonly T[], keys?: DragKeys): DragFrame<T>;
156
206
  /**
157
207
  * What a release at `point` lands on: the shown target whenever one shows
158
208
  * (a drop lands where the indicator was), else the target under the
159
209
  * pointer, else null (the item goes home). A release over home is null: it
160
210
  * does nothing. Feed the release point to `update` first, so a last jump
161
- * of the pointer counts.
211
+ * of the pointer counts. With a modifier key held (`keys`), the target the key picks, even over home.
162
212
  */
163
- release(point: Point, targets: readonly T[]): T | null;
213
+ release(point: Point, targets: readonly T[], keys?: DragKeys): T | null;
164
214
  /**
165
215
  * Hide the indicator (the view is scrolling under the pointer, say). The
166
216
  * aim starts afresh: the next sample never counts as aiming, and a dwell
@@ -195,6 +245,13 @@ interface DragIntent<T extends DragTarget> {
195
245
  * free target, and closes at once when it changes.
196
246
  * - Where `placeOf` gives a target no place (null), it is no target: it is
197
247
  * neither shown nor landed on there.
248
+ * - Spring-loading: a target marked `arm: "dwell"` is no target until the
249
+ * pointer has stayed in its zone `railArmDwellMs` (the frame's `arming` says
250
+ * which one it dwells on); then it is armed: its zone is `armedHit` (the
251
+ * magnet) and it shows at once. Leaving that zone disarms it.
252
+ * - Keys win over geometry (`modifiers`): the float key shows the `free` target
253
+ * wherever the pointer is, the rail key the nearest target marked `arm`; a
254
+ * release with the key held lands there.
198
255
  */
199
256
  declare function createDragIntent<T extends DragTarget>(options: DragIntentOptions<T>): DragIntent<T>;
200
257
 
@@ -274,4 +331,4 @@ declare function createGripPress(options?: {
274
331
  readonly double?: Partial<DoubleClick>;
275
332
  }): GripPress;
276
333
 
277
- export { DRAG_DEFAULTS, DRAG_PREVIEW_DEFAULT, DoubleClick, type DragFrame, type DragIntent, type DragIntentOptions, type DragIntentState, type DragPreview, type DragTarget, type DragTuning, type GripPress, type GripRelease, type LiftDecision, type PreviewPlan, createDragIntent, createGripPress, inRect, liftDecision, previewPlan, targetAt };
334
+ export { DRAG_DEFAULTS, DRAG_PREVIEW_DEFAULT, DoubleClick, type DragFrame, type DragIntent, type DragIntentOptions, type DragIntentState, type DragKeys, type DragModifiers, type DragPreview, type DragTarget, type DragTuning, type GripPress, type GripRelease, type LiftDecision, type ModifierKey, type PreviewPlan, createDragIntent, createGripPress, inRect, liftDecision, previewPlan, targetAt };
package/dist/drag.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  DOUBLE_CLICK
3
- } from "./chunk-D2AGIYFP.js";
3
+ } from "./chunk-IJSIYH2D.js";
4
4
 
5
5
  // src/drag/intent.ts
6
6
  var DRAG_DEFAULTS = {
@@ -17,11 +17,16 @@ var DRAG_DEFAULTS = {
17
17
  gapGrace: 120,
18
18
  switchHold: 150,
19
19
  roomAfter: 160,
20
- homeInset: 6
20
+ homeInset: 6,
21
+ railArmDwellMs: 200,
22
+ modifiers: { on: true, floatKey: "Shift", railKey: "Mod" }
21
23
  };
24
+ var KEY_FIELD = { Shift: "shift", Alt: "alt", Control: "ctrl", Meta: "meta" };
25
+ var held = (keys, key) => !!keys && (key === "Mod" ? !!(keys.meta || keys.ctrl) : !!keys[KEY_FIELD[key]]);
22
26
  function inRect(rect, point, margin = 0) {
23
27
  return point.x >= rect.x - margin && point.x <= rect.x + rect.width + margin && point.y >= rect.y - margin && point.y <= rect.y + rect.height + margin;
24
28
  }
29
+ var distanceTo = (rect, point) => Math.hypot(Math.max(rect.x - point.x, 0, point.x - (rect.x + rect.width)), Math.max(rect.y - point.y, 0, point.y - (rect.y + rect.height)));
25
30
  var inZone = (t, point, margin = 0) => t.hits ? t.hits.some((r) => inRect(r, point, margin)) : inRect(t.hit, point, margin);
26
31
  var inZoneBy = (t, point, m) => (t.hits ?? [t.hit]).some((r) => point.x >= r.x - m.x && point.x <= r.x + r.width + m.x && point.y >= r.y - m.y && point.y <= r.y + r.height + m.y);
27
32
  var scaled = (px, share, zones) => {
@@ -71,14 +76,44 @@ function createDragIntent(options) {
71
76
  let anchor = null;
72
77
  let stillSince = 0;
73
78
  let restart = false;
79
+ let armedId = null;
80
+ let armingId = null;
81
+ let armingSince = 0;
82
+ const wasOutside = /* @__PURE__ */ new Set();
74
83
  const same = (a, b) => a === null || b === null ? a === b : a.id === b.id;
75
84
  const follow = (t, targets) => t ? targets.find((x) => x.id === t.id) ?? t : null;
85
+ const asArmed = (x) => {
86
+ if (!x.armedHit) return x;
87
+ const { hits: _pieces, ...rest } = x;
88
+ return { ...rest, hit: x.armedHit };
89
+ };
90
+ const acting = (targets) => targets.filter((x) => x.arm !== "dwell" || x.id === armedId).map((x) => x.id === armedId ? asArmed(x) : x);
91
+ const forcedBy = (keys, p, targets, ok) => {
92
+ const m = tuning.modifiers;
93
+ if (!m.on || !keys) return null;
94
+ if (held(keys, m.floatKey)) {
95
+ const f = targets.find((x) => x.free && ok(x));
96
+ return f ? { kind: "float", target: f } : null;
97
+ }
98
+ if (!held(keys, m.railKey)) return null;
99
+ let best = null;
100
+ let bestD = Infinity;
101
+ for (const x of targets) {
102
+ if (x.arm !== "dwell" || !ok(x)) continue;
103
+ const d = distanceTo(asArmed(x).hit, p);
104
+ if (d < bestD) {
105
+ best = x;
106
+ bestD = d;
107
+ }
108
+ }
109
+ return best ? { kind: "rail", target: asArmed(best) } : null;
110
+ };
76
111
  return {
77
112
  tuning,
78
113
  get state() {
79
114
  return { speed, current: cur, shown, leftHome };
80
115
  },
81
- update(point, time, targets) {
116
+ update(point, time, given, keys) {
82
117
  const p = point;
83
118
  const t = time;
84
119
  samples.push({ x: p.x, y: p.y, t });
@@ -98,18 +133,42 @@ function createDragIntent(options) {
98
133
  return places.get(x.id);
99
134
  };
100
135
  const live = (x) => x !== null && placeAt(x) !== null;
101
- const raw = atHome ? null : targetAt(targets.filter((x) => inZone(x, p) && live(x)), p);
102
- cur = follow(cur, targets);
103
- shown = follow(shown, targets);
136
+ const forced = forcedBy(keys, p, given, () => true);
137
+ if (armedId !== null) {
138
+ const was = given.find((x) => x.id === armedId);
139
+ if (!was || forced || !inZone(asArmed(was), p) || !live(was)) armedId = null;
140
+ }
141
+ for (const x of given) if (x.arm === "dwell" && !inZone(x, p)) wasOutside.add(x.id);
142
+ const dwelling = forced ? null : targetAt(given.filter((x) => x.arm === "dwell" && x.id !== armedId && wasOutside.has(x.id) && inZone(x, p) && live(x)), p);
143
+ let armedNow = false;
144
+ if (!dwelling || armedId !== null) armingId = null;
145
+ else {
146
+ if (armingId !== dwelling.id) {
147
+ armingId = dwelling.id;
148
+ armingSince = t;
149
+ }
150
+ if (t - armingSince >= tuning.railArmDwellMs) {
151
+ armedId = dwelling.id;
152
+ armingId = null;
153
+ armedNow = true;
154
+ }
155
+ }
156
+ const arming = armingId === null ? null : given.find((x) => x.id === armingId) ?? null;
157
+ const targets = acting(given);
158
+ const pickable = (x) => !atHome || x.arm === "dwell" && x.id === armedId;
159
+ const raw = targetAt(targets.filter((x) => pickable(x) && inZone(x, p) && live(x)), p);
160
+ const disarmed = (x) => x?.arm === "dwell" && x.id !== armedId;
161
+ cur = disarmed(cur) ? null : follow(cur, targets);
162
+ shown = disarmed(shown) ? null : follow(shown, targets);
104
163
  if (!live(cur)) cur = null;
105
- const bandOf = (held) => scaled(tuning.band, tuning.bandShare, [held, raw && !same(raw, held) ? raw : null]);
164
+ const bandOf = (held2) => scaled(tuning.band, tuning.bandShare, [held2, raw && !same(raw, held2) ? raw : null]);
106
165
  const pastBreakthrough = (x) => inZoneBy(x, p, negate(scaled(tuning.breakthrough, tuning.bandShare, [x])));
107
166
  let next = raw;
108
- if (cur && !atHome && inZoneBy(cur, p, bandOf(cur))) {
109
- const breaks = raw !== null && !same(raw, cur) && raw.priority < cur.priority && pastBreakthrough(raw);
167
+ if (cur && pickable(cur) && inZoneBy(cur, p, bandOf(cur))) {
168
+ const breaks = raw !== null && !same(raw, cur) && raw.priority < cur.priority && (cur.free === true || pastBreakthrough(raw));
110
169
  next = breaks ? raw : cur;
111
170
  }
112
- if (!next && cur && !atHome && t - lastInside < tuning.gapGrace) next = cur;
171
+ if (!next && cur && pickable(cur) && t - lastInside < tuning.gapGrace) next = cur;
113
172
  if (next && inZone(next, p)) lastInside = t;
114
173
  if (!same(next, cur)) curSince = t;
115
174
  cur = next;
@@ -128,9 +187,15 @@ function createDragIntent(options) {
128
187
  else shown = cur;
129
188
  } else if (!cur || !aiming) {
130
189
  show(null);
131
- } else if (cur.priority < shown.priority && pastBreakthrough(cur) || under && t - curSince >= tuning.switchHold) {
190
+ } else if (cur.priority < shown.priority && (shown.free === true || pastBreakthrough(cur)) || under && t - curSince >= tuning.switchHold) {
132
191
  show(cur);
133
192
  }
193
+ if (armedNow && cur?.id === armedId && under) show(cur);
194
+ if (forced) {
195
+ if (!same(forced.target, cur)) curSince = t;
196
+ cur = forced.target;
197
+ show(forced.target);
198
+ }
134
199
  const at = shown ? placeAt(shown) : null;
135
200
  if (at !== place) {
136
201
  place = at;
@@ -140,20 +205,29 @@ function createDragIntent(options) {
140
205
  else if (t - shownSince >= tuning.roomAfter && t - placeSince >= tuning.roomAfter) roomPlace = place;
141
206
  else if (shownSince === t) roomPlace = null;
142
207
  lastPlaces = places;
143
- return { shown, room: roomPlace !== null, roomPlace, aiming, atHome: wentOut && atHome };
208
+ return { shown, room: roomPlace !== null, roomPlace, aiming, atHome: !forced && wentOut && atHome, arming, forced: forced?.kind ?? null };
144
209
  },
145
- release(point, targets) {
146
- if (inRect(home, point)) return null;
147
- const s = follow(shown, targets);
210
+ release(point, given, keys) {
211
+ const forced = forcedBy(keys, point, given, () => true);
212
+ if (forced) return given.find((x) => x.id === forced.target.id) ?? forced.target;
213
+ if (inRect(home, point)) {
214
+ const armed = armedId === null ? null : given.find((x) => x.id === armedId);
215
+ return armed && inZone(asArmed(armed), point) && placeOf(armed, point, lastPlaces.get(armed.id) ?? null) !== null ? armed : null;
216
+ }
217
+ const targets = acting(given);
218
+ const s = shown && !(shown.arm === "dwell" && shown.id !== armedId) ? follow(shown, given) : null;
148
219
  if (s) return s;
149
- return targetAt(
220
+ const under = targetAt(
150
221
  targets.filter((x) => inZone(x, point) && placeOf(x, point, lastPlaces.get(x.id) ?? null) !== null),
151
222
  point
152
223
  );
224
+ return under ? given.find((x) => x.id === under.id) ?? under : null;
153
225
  },
154
226
  hide(time) {
155
227
  shown = null;
156
228
  cur = null;
229
+ armedId = null;
230
+ armingId = null;
157
231
  shownSince = time;
158
232
  place = null;
159
233
  roomPlace = null;