diff --git a/docs/web.md b/docs/web.md index 7dc09d7c8..aa52a4ddd 100644 --- a/docs/web.md +++ b/docs/web.md @@ -94,6 +94,15 @@ and signs every browser out. Hold Ctrl+Shift and roll the wheel down or up to switch boards. Turn on **Focus on hover** in browser settings to select a tile under the pointer without moving the view. +- **Resize a tile**: hold Ctrl and right-drag from the left or right third of + the window. The opposite edge stays put, and neighbouring columns keep their + widths. Drag near the board edge to align with it, or near half-width to fit + two equal columns and their gutter. Keep dragging to move past a snap. In a + stack, grab the top or bottom third to change height, or a corner for both. + Drag the horizontal divider near one in the column beside it to line them up. + A labeled guide marks the edge or divider while it is snapped. Escape cancels. + Drag the ordinary handle between two tiles to snap while trading space only + between those two. Double-click the handle to even out the split. - **Reviews**: read a turn's changes as highlighted diffs, tie notes to lines, a message, or a window, and send them back as one message. - **Artifacts**: [pages Albedo makes for you](artifacts.md) open beside the chat diff --git a/robot-docs/web.md b/robot-docs/web.md index e3951d53f..e20f3f9b1 100644 --- a/robot-docs/web.md +++ b/robot-docs/web.md @@ -314,6 +314,40 @@ the mouse moves into it, without revealing its column. Clicking the same tile still reveals it. An editor keeps its typing focus. Moving the view under a stationary pointer does not take selection back from the wheel. +[A Ctrl + right-button grab](../web/src/lib/tiles/resize-grab.ts) fixes the +edges from the initial press. [The web client](../web/src/main.ts) reserves +Ctrl + right-click throughout the app and popout documents, even outside a +board. An unmodified right-click keeps its menu. +[`planResize`](../web/src/lib/tiles/resize.ts) measures the outer column's width +and the nearest vertical split's height once. Horizontal grabs snap the column +to half the viewport minus half a gutter, or align the moving edge with either +visible board edge. A reachable board edge wins over a half-width stop within +its release distance. Offscreen half-width stops are not offered. The targets +are measured at press time, before a left-edge drag moves the camera. A +left-edge drag offsets the view by the width change to keep the opposite edge +still. The root row can be narrower than the viewport, even with only one +window. Stacked siblings share the remaining height subject to their minimum +sizes. When a grab moves a horizontal divider, it can snap to the height of a +visible divider in the branch immediately to its left or right. Only the grabbed +stack changes. BoardView draws a labeled guide from the snap held by the resize +planner. Both axes use the visible board width at the start of the grab. Snaps +engage within 1.2% of that width (up to 12 CSS pixels) and release past 2% (up +to 20). The guides are part of the drag preview, never the saved layout. Release +records one undo step in `Board.finishResize`. Escape or lost capture discards +the preview. + +Plain drags on [a tiled split handle](../web/src/lib/tiles/SplitView.svelte) use +the same guides and snap distances. `planGutter` changes only the two shares +beside the handle. Their total, the strip width, and other shares stay fixed. +Either side can reach half-width. A stop that would squeeze its neighbor under +its minimum is not offered. Arrow keys on a focused separator still nudge it +without snapping, and a double-click still evens the whole split. + +For a menu that appears on a physical mouse but not in a browser test, add +`?input-trace` to the app URL. The [local readout](../web/src/lib/menu-trace.ts) +shows the loaded entry bundle, delivered button and modifier events, and whether +the page canceled the menu. It does not send or store the trace. + ## Window zoom A window's own zoom (Alt with the wheel or a pinch on a canvas, ⌥+, ⌥−, ⌥0, or diff --git a/web/scripts/resize.test.mjs b/web/scripts/resize.test.mjs new file mode 100644 index 000000000..9e23a749c --- /dev/null +++ b/web/scripts/resize.test.mjs @@ -0,0 +1,710 @@ +// Pure resize plans: preview geometry, reachable stops, and drag latch history. +import assert from 'node:assert/strict'; +import test from 'node:test'; +import { check, fc } from './property.mjs'; +import { + close, + column, + leaf, + makeRow, + resize, + stack, + widths, + tree, +} from './tiles-fixture.mjs'; + +const { planResize, planGutter } = resize; + +const resizedWidth = fc.integer({ min: 192, max: 660 }); +const direction = fc.constantFrom(-1, 1); +const delta = fc.integer({ min: -1200, max: 1200 }); + +test('horizontal drag changes only its column and anchors the left edge with the actual change', () => { + check( + fc.property( + fc.array(resizedWidth, { minLength: 2, maxLength: 6 }), + fc.integer({ min: 480, max: 1800 }), + fc.nat(), + direction, + delta, + (lengths, viewport, index, x, dx) => { + const at = index % lengths.length; + const length = lengths.reduce((sum, width) => sum + width, 0); + const span = (length + tree.GUTTER * (lengths.length - 1)) / viewport; + const root = makeRow(lengths, span, column); + const { sample: plan } = planResize( + root, + `bottom-${at}`, + { x, y: 0 }, + viewport, + () => { + assert.fail('horizontal drag does not measure height'); + }, + ); + const preview = plan(dx, 0); + const expected = Math.max(tree.LEAST, lengths[at] + x * dx); + assert.equal(preview.root.id, root.id); + assert.strictEqual(preview.root.children, root.children); + assert.ok(tree.valid(preview.root)); + const nextWidths = widths(preview.root, viewport); + for (let i = 0; i < lengths.length; i++) + close( + nextWidths.get(root.children[i].id), + i === at ? expected : lengths[i], + ); + close(preview.shift, x === -1 ? expected - lengths[at] : 0); + assert.deepEqual(plan(dx, 0), preview); + assert.strictEqual(plan(0, 0).root, root); + }, + ), + ); +}); + +test('resize snaps to the visible board edge or an equal pair, then releases to free drag', () => { + check( + fc.property( + fc.integer({ min: 500, max: 2000 }), + direction, + fc.boolean(), + fc.integer({ min: -7, max: 7 }), + (viewport, x, half, near) => { + const w = viewport * 0.38; + const root = makeRow( + [w, w, w], + (3 * w + 2 * tree.GUTTER) / viewport, + column, + ); + const fixed = viewport * (x === 1 ? 0.3 : 0.7); + const offset = w + tree.GUTTER + (x === -1 ? w : 0) - fixed; + const { sample: plan } = planResize( + root, + 'bottom-1', + { x, y: 0 }, + viewport, + () => 0, + { viewOffset: offset }, + ); + const target = half ? (viewport - tree.GUTTER) / 2 : 0.7 * viewport; + for (const [wideDistance, held] of [ + [13, false], + [11.75, true], + [near, true], + [0, true], + [19.75, true], + [20.25, false], + [13, false], + ]) { + const distance = wideDistance * Math.min(viewport / 1000, 1); + const expected = held ? target : target + distance; + const preview = plan(x * (target + distance - w), 0); + if (held) { + assert.equal(preview.snapX?.kind, half ? 'half' : 'edge'); + close(preview.snapX?.at, fixed + x * target); + } else assert.equal(preview.snapX, undefined); + assert.equal(preview.snapY, undefined); + const cols = [...widths(preview.root, viewport).values()]; + close(cols[0], w); + close(cols[1], expected); + close(cols[2], w); + close(preview.shift, x === -1 ? expected - w : 0); + } + assert.deepEqual(plan(0, 0), { root, shift: 0 }); + }, + ), + ); +}); + +test('vertical grabs align laid-out dividers, then release without moving the other column', () => { + check( + fc.property( + fc.array(fc.integer({ min: 420, max: 580 }), { + minLength: 4, + maxLength: 4, + }), + fc.integer({ min: 1, max: 5 }), + direction, + fc.nat(), + (base, nudge, y, index) => { + const at = (index % 3) + (y === -1 ? 1 : 0); + const before = y === -1 ? at : at + 1; + // A sibling pins early when the grabbed part grows. Keep the outer + // siblings tall so the line can still move after it pins. + const heights = base.map((height, i) => + i === (at === 1 ? 2 : 1) ? tree.LEAST + nudge : height, + ); + const length = heights.reduce((sum, height) => sum + height, 0); + const height = length + 3 * tree.GUTTER; + const dragged = stack( + 'dragged', + heights.map((_, i) => leaf(`part-${i}`)), + heights, + ); + const line = (column, count) => + column.sizes + .slice(0, count) + .reduce( + (top, share) => + top + + share * (height - tree.GUTTER * (column.sizes.length - 1)), + (count - 0.5) * tree.GUTTER, + ); + const rowWith = (neighbor) => + makeRow([1, 1], 1.2, (i) => (i === 0 ? neighbor : dragged)); + const plan = (root, options, part = at, edge = y) => + planResize( + root, + `part-${part}`, + { x: 0, y: edge }, + 1000, + () => height, + options, + )?.sample ?? null; + const free = plan(rowWith(column('unused'))); + const center = (dy) => line(free(0, dy).root.children[1], before); + // Pick a neighbor line from a reachable *laid-out* free position, not + // from dy: the grabbed part and the visible line travel different distances. + for (const sign of [-1, 1]) { + const dy = sign * 115; + const stop = center(dy); + const neighbor = stack( + 'neighbor', + [leaf('above'), leaf('below')], + [stop - tree.GUTTER / 2, height - stop - tree.GUTTER / 2], + ); + const root = rowWith(neighbor); + const stopsMeasured = []; + const snap = plan(root, { + viewOffset: 0, + dividerStops: (id) => { + stopsMeasured.push(id); + return [line(neighbor, 1)]; + }, + }); + const distanceFrom = (line, lo, hi) => { + for (let move = dy; Math.abs(move) < 1000; move += sign) { + const distance = Math.abs(center(move) - line); + if (distance >= lo && distance <= hi) return move; + } + assert.fail(`no free divider ${lo}..${hi}px from ${line}`); + }; + assert.deepEqual(snap(0, 0), { root, shift: 0 }); + const outsideCapture = distanceFrom(stop, 12.1, 14); + const missed = snap(0, outsideCapture); + assert.equal(missed.snapY, undefined); + close(line(missed.root.children[1], before), center(outsideCapture)); + for (const move of [ + distanceFrom(stop, 10, 11.9), + dy, + distanceFrom(stop, 18.1, 19.9), + ]) { + const { root: after, shift, snapX, snapY } = snap(0, move); + assert.equal(snapX, undefined); + close(snapY, line(neighbor, 1)); + close(line(after.children[1], before), line(after.children[0], 1)); + assert.equal(shift, 0); + assert.strictEqual(after.children[0], neighbor); + assert.strictEqual(after.sizes, root.sizes); + assert.equal(after.span, root.span); + assert.ok(tree.valid(after)); + } + const past = distanceFrom(stop, 20.1, 22); + const released = snap(0, past); + assert.equal(released.snapY, undefined); + close(line(released.root.children[1], before), center(past)); + assert.deepEqual(stopsMeasured, [dragged.id]); + + const limit = center(sign * 1000); + const unreachable = plan(root, { + viewOffset: 0, + dividerStops: () => [limit + 3 * sign], + }); + const nearLimit = distanceFrom(limit, 1, 4); + const freeAtLimit = unreachable(0, nearLimit); + assert.equal(freeAtLimit.snapY, undefined); + close(line(freeAtLimit.root.children[1], before), center(nearLimit)); + const reachable = plan(root, { + viewOffset: 0, + dividerStops: () => [limit - 4 * sign], + }); + const caught = reachable(0, nearLimit); + close(caught.snapY, limit - 4 * sign); + close(line(caught.root.children[1], before), limit - 4 * sign); + const floor = reachable(0, sign * 1000); + assert.equal(floor.snapY, undefined); + close(line(floor.root.children[1], before), limit); + } + const root = rowWith(column('neighbor')); + const external = { + viewOffset: 0, + dividerStops: () => assert.fail('no outer divider'), + }; + assert.equal(plan(root, external, 0, -1), null); + assert.deepEqual( + plan(root, external, 3, 1)(0, 60), + plan(root, undefined, 3, 1)(0, 60), + ); + }, + ), + ); +}); + +test('corner cues combine and stay off on a stationary axis', () => { + check( + fc.property( + fc.integer({ min: 500, max: 2000 }), + fc.integer({ min: 240, max: 640 }), + direction, + direction, + fc.integer({ min: 4, max: 11 }), + (viewport, partHeight, x, y, wideNudge) => { + const scale = Math.min(1, viewport / 1000); + const nudge = wideNudge * scale; + const root = makeRow([1, 1], undefined, column); + const width = (viewport - tree.GUTTER) / 2; + const center = partHeight + tree.GUTTER / 2; + const { sample: drag } = planResize( + root, + y === 1 ? 'top-0' : 'bottom-0', + { x, y }, + viewport, + () => 2 * center, + { viewOffset: -65, dividerStops: () => [center] }, + ); + const h = { at: 65 + (x === 1 ? width : 0), kind: 'half' }; + assert.deepEqual(drag(0, 0), { root, shift: 0 }); + const both = drag(nudge, nudge); + assert.equal(both.shift, 0); + assert.deepEqual(both.snapX, h); + assert.equal(both.snapY, center); + close(both.root.children[0].sizes[0], 0.5); + const onlyY = drag(0, nudge); + assert.equal(onlyY.snapX, undefined); + assert.equal(onlyY.snapY, center); + const onlyX = drag(nudge, 0); + assert.strictEqual(onlyX.root, root); + assert.deepEqual(onlyX, { root, shift: 0, snapX: h }); + }, + ), + ); +}); + +test('solo resize keeps one stable width-bearing wrapper and ignores vertical drag', () => { + check( + fc.property( + fc.integer({ min: 350, max: 2000 }), + direction, + (viewport, x) => { + const root = leaf('alone'); + const measure = () => assert.fail('solo drag has no height to measure'); + assert.equal( + planResize(root, root.window, { x: 0, y: x }, viewport, measure), + null, + ); + const { edges, sample: plan } = planResize( + root, + root.window, + { x, y: -x }, + viewport, + measure, + ); + assert.deepEqual(edges, { x, y: 0 }); + const shrunk = plan(-x * viewport, 90); + const grown = plan(x * 70, 90); + const { sample: shrunkAgain } = planResize( + shrunk.root, + root.window, + { x, y: 0 }, + viewport, + measure, + ); + assert.strictEqual(shrunkAgain(-x * 999, 0).root, shrunk.root); + assert.strictEqual(plan(0, 90).root, root); + assert.equal(shrunk.root.id, grown.root.id); + assert.strictEqual(shrunk.root.children[0], root); + close(widths(shrunk.root, viewport).get(root.window), tree.LEAST); + close(widths(grown.root, viewport).get(root.window), viewport + 70); + assert.strictEqual(tree.normalize(shrunk.root), shrunk.root); + }, + ), + ); +}); + +test('corner resize combines outer width and the nearest stack, pinning weighted sibling floors', () => { + check( + fc.property( + fc.array(fc.integer({ min: 180, max: 750 }), { + minLength: 3, + maxLength: 7, + }), + direction, + direction, + delta, + fc.boolean(), + (heights, x, y, dy, solo) => { + const pane = leaf('gripped'); + const sidecar = leaf('sidecar'); + const branch = { + ...makeRow([1, 1], undefined, (at) => (at === 0 ? pane : sidecar)), + id: 'branch', + }; + const at = 1; + const inner = stack( + 'inner', + heights.map((_, i) => (i === at ? branch : leaf(`peer-${i}`))), + heights, + ); + const outer = stack('outer', [leaf('above'), inner], [400, 2000]); + const root = solo + ? outer + : makeRow([1, 1], undefined, (i) => + i === 0 ? leaf('neighbor') : outer, + ); + const viewport = 1000; + const available = heights.reduce((sum, height) => sum + height, 0); + const splitHeight = available + tree.GUTTER * (heights.length - 1); + const sizesSeen = []; + const { edges, sample: plan } = planResize( + root, + pane.window, + { x, y }, + viewport, + (id) => { + sizesSeen.push(id); + return splitHeight; + }, + ); + const after = plan(-x * viewport, dy).root; + assert.deepEqual(edges, { x, y }); + assert.deepEqual(sizesSeen, [inner.id]); + assert.ok(tree.valid(after)); + assert.strictEqual(tree.find(after, outer.id).sizes, outer.sizes); + assert.strictEqual(tree.find(after, branch.id), branch); + close( + widths(after, viewport).get(outer.id), + 2 * tree.LEAST + tree.GUTTER, + ); + const innerAfter = tree.find(after, inner.id); + const newHeights = innerAfter.sizes.map((weight) => weight * available); + close( + newHeights.reduce((sum, height) => sum + height, 0), + available, + ); + close( + newHeights[at], + Math.min( + available - (heights.length - 1) * tree.LEAST, + Math.max(tree.LEAST, heights[at] + y * dy), + ), + ); + const scales = newHeights.flatMap((height, i) => + i !== at && height > tree.LEAST + 1e-8 ? [height / heights[i]] : [], + ); + for (const [i, height] of newHeights.entries()) { + assert.ok(height >= tree.LEAST - 1e-8); + if (i === at || !scales.length) continue; + close(height, Math.max(tree.LEAST, heights[i] * scales[0])); + } + assert.equal( + planResize(root, 'peer-0', { x: 0, y: -1 }, viewport, () => { + assert.fail('topmost branch cannot resize by its top edge'); + }), + null, + ); + }, + ), + ); +}); + +// 160 / 525 rounded back into CSS pixels is just above the 160px floor. +test('a solo left edge already at its minimum does not produce a roundoff-only resize', () => { + const pane = leaf('alone'); + const root = makeRow([1], tree.LEAST / 525, () => pane); + const { sample: plan } = planResize( + root, + pane.window, + { x: -1, y: 0 }, + 525, + () => 0, + ); + assert.strictEqual(plan(1, 0).root, root); + assert.equal(plan(1, 0).shift, 0); +}); + +// Construct a split from layout pixels so the oracle can inspect actual pane edges. +function gutterFixture( + axis, + lengths, + children = lengths.map((_, i) => leaf(`gutter-${i}`)), +) { + const length = lengths.reduce((sum, width) => sum + width, 0); + return { + length, + split: { + kind: 'split', + id: 'gutter', + axis, + children, + sizes: lengths.map((width) => width / length), + ...(axis === 'row' ? { span: 2.5 } : {}), + }, + }; +} + +// Starting widths and divider positions lie just inside capture distance. +// A 5px move away crosses capture but not release distance. +for (const axis of ['x', 'y']) { + test(`returning to the press on ${axis} clears only that axis's snap`, () => { + const viewport = 1000; + const dragged = column('dragged'); + const root = makeRow([486, 508], 1, (at) => + at === 0 ? dragged : leaf('neighbor'), + ); + const { sample } = planResize( + root, + 'top-dragged', + { x: 1, y: 1 }, + viewport, + () => 606, + { viewOffset: 0, dividerStops: () => [314] }, + ); + const first = sample(11, 11); + assert.deepEqual(first.snapX, { kind: 'half', at: 497 }); + assert.equal(first.snapY, 314); + const zero = axis === 'x' ? sample(0, -5) : sample(-5, 0); + assert.deepEqual(zero.snapX, axis === 'x' ? undefined : first.snapX); + assert.equal(zero.snapY, axis === 'y' ? undefined : first.snapY); + const away = sample(-5, -5); + assert.deepEqual(away.snapX, axis === 'x' ? undefined : first.snapX); + assert.equal(away.snapY, axis === 'y' ? undefined : first.snapY); + }); +} + +const axis = fc.constantFrom('row', 'column'); +const boardWidth = fc.integer({ min: 420, max: 1600 }); + +test('a near-edge half width snaps to the actual board boundary, never a clipped width guide', () => { + check( + fc.property(boardWidth, direction, (viewport, x) => { + const half = (viewport - tree.GUTTER) / 2; + const edge = x === 1 ? viewport : 0; + // Place half-width just inside, just outside, then well outside the viewport. + for (const drift of [-1.25, 1.25, 40]) { + const fixed = edge - x * (half - drift); + const expected = drift === 40 ? undefined : { kind: 'edge', at: edge }; + const solo = leaf('dragged'); + const { sample } = planResize( + solo, + solo.window, + { x, y: 0 }, + viewport, + () => 0, + { viewOffset: x === 1 ? -fixed : viewport - fixed }, + ); + const preview = sample(x * (half - viewport), 0); + assert.deepEqual(preview.snapX, expected); + if (expected) close(fixed + x * preview.root.span * viewport, edge); + + const { split, length } = gutterFixture('row', [half + 45, half + 55]); + const start = x === 1 ? fixed : fixed - length - tree.GUTTER; + const gutter = planGutter(split, 0, length, { viewport, start }); + const result = gutter(x === 1 ? -45 : 55); + assert.deepEqual(result.snapX, expected); + if (expected) + close( + start + length * result.sizes[0] + (x === 1 ? 0 : tree.GUTTER), + edge, + ); + } + }), + ); +}); + +test('gutter moves from the grabbed seam, trades only its pair, and respects nested minima', () => { + check( + fc.property( + axis, + fc.array(fc.integer({ min: 50, max: 600 }), { + minLength: 3, + maxLength: 7, + }), + fc.nat(), + fc.integer({ min: 1, max: 700 }), + direction, + (axis, lengths, choice, distance, direction) => { + const at = choice % (lengths.length - 1); + const children = lengths.map((_, i) => leaf(`gutter-${i}`)); + children[at] = { ...column('nested'), axis }; + const { split, length } = gutterFixture(axis, lengths, children); + Object.freeze(split.sizes); + Object.freeze(split); + const plan = planGutter(split, at, length); + const delta = distance * direction; + const preview = plan(delta); + const first = lengths[at]; + const second = lengths[at + 1]; + const room = + direction < 0 + ? first - 2 * tree.LEAST - tree.GUTTER + : second - tree.LEAST; + const moved = direction * Math.min(distance, Math.max(0, room)); + assert.equal(preview.snapX, undefined); + assert.equal(preview.snapY, undefined); + split.sizes.forEach((size, i) => { + if (i === at) close(preview.sizes[i] * length, first + moved); + else if (i === at + 1) + close(preview.sizes[i] * length, second - moved); + else assert.equal(preview.sizes[i], size); + }); + if (moved === 0) assert.equal(preview.sizes, split.sizes); + assert.deepEqual(plan(delta), preview); + }, + ), + 250, + ); +}); + +test('gutter half-width and divider snaps capture, hold, release and reset', () => { + check( + fc.property( + axis, + boardWidth, + direction, + direction, + (axis, viewport, pane, sign) => { + const half = (viewport - tree.GUTTER) / 2; + const pair = 2 * half + 100; + const target = pane < 0 ? half : pair - half; + // The press is inside capture range, but zero delta must never latch. + const first = target + sign * 2; + const { split, length } = gutterFixture(axis, [ + 230, + first, + pair - first, + ]); + const start = axis === 'row' ? 0 : 230; + const guideX = start + target + (pane < 0 ? 0 : tree.GUTTER); + const guideY = start + target + tree.GUTTER / 2; + const plan = planGutter(split, 1, length, { + viewport, + start, + dividerStops: [guideY], + }); + const capture = Math.min(12, 0.012 * viewport); + const release = Math.min(20, 0.02 * viewport); + const between = (capture + release) / 2; + const checkAt = (offset, snapped) => { + const free = target + sign * offset; + const expected = snapped ? target : free; + const result = plan(free - first); + close(result.sizes[1] * length, expected); + close(result.sizes[2] * length, pair - expected); + assert.equal(result.sizes[0], split.sizes[0]); + if (snapped && axis === 'row') { + assert.equal(result.snapX?.kind, 'half'); + close(result.snapX.at, guideX); + } else assert.equal(result.snapX, undefined); + assert.equal( + result.snapY, + snapped && axis === 'column' ? guideY : undefined, + ); + }; + const checkOrigin = () => { + const result = plan(0); + assert.strictEqual(result.sizes, split.sizes); + assert.equal(result.snapX, undefined); + assert.equal(result.snapY, undefined); + }; + checkOrigin(); + checkAt(capture + 0.01, false); + checkAt(capture - 0.01, true); + checkAt(release - 0.01, true); + checkAt(release + 0.01, false); + checkAt(between, false); + checkAt(0, true); + checkOrigin(); + checkAt(between, false); + }, + ), + 250, + ); +}); + +test('gutter aligns the moving edge of either row pane to either visible board edge', () => { + check( + fc.property(boardWidth, direction, direction, (viewport, pane, edge) => { + const half = (viewport - tree.GUTTER) / 2; + const first = half + 35; + const target = half + 60; + const { split, length } = gutterFixture('row', [first, half + 95]); + const x = edge < 0 ? 0 : viewport; + const gap = pane < 0 ? 0 : tree.GUTTER; + const start = x - target - gap; + const result = planGutter(split, 0, length, { viewport, start })( + target - first + 2 * pane, + ); + assert.deepEqual(result.snapX, { kind: 'edge', at: x }); + assert.equal(result.snapY, undefined); + close(start + result.sizes[0] * length + gap, x); + }), + 120, + ); +}); + +test('gutter rejects stops just past either pane’s minimum', () => { + check( + fc.property( + axis, + boardWidth, + direction, + fc.boolean(), + (axis, viewport, direction, halfSnap) => { + const half = (viewport - tree.GUTTER) / 2; + const minimum = tree.LEAST; + const first = half + 50; + const pair = first + minimum + 50; + const limit = direction < 0 ? minimum : pair - minimum; + // To test a half stop just past a limit, make that pane's nested minimum + // greater than half; it started just above half and cannot shrink at all. + const children = [leaf('gutter-before'), leaf('gutter-after')]; + const shrinking = direction < 0 ? 0 : 1; + let lengths = [first, pair - first]; + let target = limit + direction * 3; + let clamp = limit; + let start = 37; + if (axis === 'row' && halfSnap) { + const count = Math.ceil( + (half + 3 + tree.GUTTER) / (minimum + tree.GUTTER), + ); + children[shrinking] = { + ...column('minimum'), + axis, + children: Array.from({ length: count }, (_, i) => + leaf(`minimum-${i}`), + ), + sizes: Array(count).fill(1 / count), + }; + lengths = [half + 80, half + 80]; + lengths[shrinking] = half + 3; + clamp = lengths[0]; + target = clamp + direction * 3; + // Keep both candidate half-width guides onscreen; reachability, + // not clipping or edge precedence, must reject the stop. + start = (viewport - lengths[0] - lengths[1] - tree.GUTTER) / 2; + } else if (axis === 'row') { + // Put even the nearer pane edge on the unreachable side of the limit. + start = -target - (direction < 0 ? 0 : tree.GUTTER); + } + const { split, length } = gutterFixture(axis, lengths, children); + const { sizes, snapX, snapY } = planGutter(split, 0, length, { + viewport, + start, + dividerStops: [start + target + tree.GUTTER / 2], + })(target - lengths[0]); + assert.equal(snapX, undefined); + assert.equal(snapY, undefined); + close(sizes[0] * length, clamp); + }, + ), + 180, + ); +}); diff --git a/web/scripts/tiles-fixture.mjs b/web/scripts/tiles-fixture.mjs index 6d5ac969d..1ce7d1531 100644 --- a/web/scripts/tiles-fixture.mjs +++ b/web/scripts/tiles-fixture.mjs @@ -33,6 +33,10 @@ async function tileURL(name, imports = {}) { const treeURL = await tileURL('tree', { '../api': await tileURL('../api') }); export const tree = await import(treeURL); +export const resize = await import( + await tileURL('resize', { './tree': treeURL }) +); + export async function loadMoves() { const imports = { './tree': treeURL, '../board.svelte': boards }; return import( diff --git a/web/src/lib/Keys.svelte b/web/src/lib/Keys.svelte index 22adf26da..a8ac29f56 100644 --- a/web/src/lib/Keys.svelte +++ b/web/src/lib/Keys.svelte @@ -19,6 +19,7 @@ keys: [ [['click'], 'a header selects its section, group or canvas'], [[option('←→↑↓')], 'select the window that way'], + [['Ctrl', 'right-drag'], 'resize a tiled column or stacked window'], [['enter'], 'type in it'], [[option('⇧←→↑↓')], 'move it'], [[option('⌃←→↑↓')], 'swap a window'], diff --git a/web/src/lib/board.svelte.ts b/web/src/lib/board.svelte.ts index 10a8b2444..b4cf5300c 100644 --- a/web/src/lib/board.svelte.ts +++ b/web/src/lib/board.svelte.ts @@ -2873,6 +2873,14 @@ export class Board { return next; } + /** Records a finished tile resize, unless another change replaced the layout the grab measured. */ + finishResize(before: Tile, next: Tile) { + if (this.root !== before || next === before) return false; + this.#remember(); + this.root = next; + return true; + } + /** Sets a split's sizes, which sum to 1. */ resize(split: string, sizes: number[]) { this.#remember(); diff --git a/web/src/lib/menu-trace.ts b/web/src/lib/menu-trace.ts new file mode 100644 index 000000000..f3097c196 --- /dev/null +++ b/web/src/lib/menu-trace.ts @@ -0,0 +1,74 @@ +/** Opt-in input diagnostics. Never record content, field values, URLs, or typed keys. */ +export function traceSecondaryClick(build: string) { + const panel = document.createElement('pre'); + panel.style.cssText = + 'position:fixed;bottom:6px;right:6px;z-index:2147483647;margin:0;padding:10px;max-width:95vw;white-space:pre-wrap;background:#211c28;color:#fff;border-radius:6px;font:12px/1.45 monospace;pointer-events:none'; + const rows: string[] = []; + const title = `albedo input trace · ${build}\n`; + panel.textContent = title + 'waiting for ctrl + right-click'; + document.body.append(panel); + function record(text: string) { + rows.push(text); + if (rows.length > 9) rows.shift(); + panel.textContent = title + rows.join('\n'); + } + for (const type of ['keydown', 'keyup'] as const) { + window.addEventListener( + type, + (event) => { + if (event.key === 'Control' && !event.repeat) + record(`${type}: Control`); + }, + true, + ); + } + for (const type of [ + 'pointerdown', + 'mousedown', + 'pointerup', + 'contextmenu', + ] as const) { + window.addEventListener( + type, + (event) => { + if (event.button === 0 && !event.ctrlKey && type !== 'contextmenu') + return; + const under = event.target instanceof Element ? event.target : null; + const region = + under?.closest('.window-slot')?.getAttribute('data-place') ?? + under?.tagName ?? + '?'; + const mods = + [ + event.shiftKey && 'shift', + event.ctrlKey && 'ctrl', + event.altKey && 'alt', + event.metaKey && 'meta', + ] + .filter(Boolean) + .join('+') || 'none'; + const button = event.button; + // Observe the final state without changing or intercepting the gesture. + setTimeout( + () => + record( + `${type}: button=${button} mods=${mods} ${region} ${event.cancelable ? (event.defaultPrevented ? 'canceled' : 'allowed') : 'not cancelable'}`, + ), + 0, + ); + }, + true, + ); + } + window.addEventListener('blur', () => + setTimeout( + () => + record( + document.activeElement instanceof HTMLIFrameElement + ? 'focus left for an iframe' + : 'window lost focus', + ), + 0, + ), + ); +} diff --git a/web/src/lib/tiles/BoardView.svelte b/web/src/lib/tiles/BoardView.svelte index 0ba927203..d16629da0 100644 --- a/web/src/lib/tiles/BoardView.svelte +++ b/web/src/lib/tiles/BoardView.svelte @@ -9,6 +9,8 @@ import TileView from './TileView.svelte'; import { hosts, windowOfHost } from './hosts'; import { columnRow, find, paneTrail, shape, windows } from './tree'; + import type { ResizePreview } from './resize'; + import { resizeGrab } from './resize-grab'; import { focusColumn } from './focus'; import { prefs } from '../prefs.svelte'; import WindowSlot from './WindowSlot.svelte'; @@ -38,15 +40,60 @@ const empty = $derived(windows(board.root).length === 0); let alt = $state(false); let viewport = $state(); + let sizing = $state.raw(null); + let cancelResize: (() => void) | null = null; + let origin = $state(0); + const shownRoot = $derived(sizing?.root ?? board.root); const span = $derived( - board.root.kind === 'split' ? board.root.span : undefined, + shownRoot.kind === 'split' ? shownRoot.span : undefined, + ); + // A resize only rebuilds slots when it wraps the root. Other samples change sizes alone. + const shownId = $derived(shownRoot.id); + const arrangement = $derived( + shape( + shownId === board.root.id ? board.root : untrack(() => shownRoot), + false, + ), ); - const arrangement = $derived(shape(board.root, false)); let track = $state(); let viewWidth = $state(0); - const positioned = $derived(span !== undefined); + const positioned = $derived(span !== undefined || sizing !== null); function press(event: PointerEvent) { + if (sizing) return; + const gutter = + event.button === 0 && + (event.target as Element).matches('.split > .gutter'); + if ( + (gutter || (event.button === 2 && event.ctrlKey)) && + viewport && + !carry + ) { + const at = + positioned && track + ? viewport.getBoundingClientRect().left - + track.getBoundingClientRect().left + : 0; + const cancel = resizeGrab( + event, + board, + viewport, + at, + (preview) => { + sizing = preview; + if (!preview) cancelResize = null; + }, + (base, next) => { + if (board.finishResize(base, next)) + board.stripOffset = at + (sizing?.shift ?? 0); + }, + ); + if (cancel) { + origin = at; + cancelResize = cancel; + return; + } + } if (!pick(event)) dock.press(event); } @@ -60,6 +107,7 @@ event.altKey || !delta || carry || + sizing || board.lifted || (event.target as Element).closest('[role="dialog"], [popover]') ) @@ -121,7 +169,7 @@ // Use the destination offset, not an intermediate frame of the CSS transition. $effect(() => { - if (board.lifted || board.expanded) return; + if (sizing || board.lifted || board.expanded) return; if (span === undefined) { board.stripOffset = 0; return; @@ -150,6 +198,7 @@ }); $effect(() => () => { + cancelResize?.(); dock.cancel(); }); @@ -189,7 +238,7 @@ if (next === arranged) return; arranged = next; untrack(() => { - reflow(board.id); + if (!sizing) reflow(board.id); hosts.shelter(board.id); }); }); @@ -434,6 +483,7 @@ class:tiled class:expanding={!!board.expanded} class:alt + class:sizing={!!sizing} class:carrying={carry !== null && !carry.flow} bind:this={element} {@attach boardWheel} @@ -456,11 +506,33 @@
- +
+ {#if sizing?.snapX || sizing?.snapY !== undefined} + + {/if} {/if} {#if board.expanded && board.expandedOver} @@ -675,10 +747,62 @@ height: 100%; } @media (prefers-reduced-motion: no-preference) { - .scrolling > .strip-content { + .board-view:not(.sizing) .scrolling > .strip-content { transition: left 220ms cubic-bezier(0.22, 1, 0.36, 1); } } + .sizing, + .sizing :global(*) { + cursor: var(--resize-grab-cursor, ew-resize) !important; + user-select: none; + } + .sizing :global(iframe) { + pointer-events: none; + } + .snap-guides { + position: absolute; + inset: 0; + z-index: var(--z-docking); + overflow: clip; + pointer-events: none; + } + .snap-x, + .snap-y { + position: absolute; + background: var(--accent); + box-shadow: 0 0 0 1px color-mix(in oklab, var(--surface) 65%, transparent); + } + .snap-x { + inset-block: 0; + width: 2px; + } + .snap-y { + inset-inline: 0; + height: 2px; + } + .snap-guides span { + position: absolute; + padding: 2px 8px; + border-radius: 999px; + color: var(--surface); + background: var(--accent); + box-shadow: var(--shadow); + font-size: calc(var(--type-xs) * var(--ui-scale)); + font-weight: var(--weight-strong); + white-space: nowrap; + } + .snap-x span { + top: 7px; + left: 5px; + } + .snap-x.flip span { + right: 5px; + left: auto; + } + .snap-y span { + bottom: 5px; + left: 7px; + } /* Behind what fills the board everything stays laid out as it was, unseen and out of reach. */ .expanding { background: var(--bg); diff --git a/web/src/lib/tiles/SplitView.svelte b/web/src/lib/tiles/SplitView.svelte index 8a777b269..3664e1ea2 100644 --- a/web/src/lib/tiles/SplitView.svelte +++ b/web/src/lib/tiles/SplitView.svelte @@ -1,105 +1,84 @@ @@ -117,6 +96,7 @@