|
| 1 | +// Aspect-ratio guide — the diagonal dashed line drawn across the resize |
| 2 | +// preview box while Shift aspect-locks the gesture. Mirrors the main editor's |
| 3 | +// `AspectRatioGuide`; geometry comes from `cmath.ui.diagonalForDirection` |
| 4 | +// (the corner opposite the dragged handle). Lives in `decoration.lines`, |
| 5 | +// alongside the dashed preview rect/polyline. |
| 6 | + |
| 7 | +import { describe, it, expect } from "vitest"; |
| 8 | +import cmath from "@grida/cmath"; |
| 9 | +import { SurfaceState } from "../event/state"; |
| 10 | +import { buildChrome } from "../surface/chrome"; |
| 11 | +import { DEFAULT_STYLE } from "../surface/style"; |
| 12 | +import type { NodeId, Rect } from "../event/gesture"; |
| 13 | +import type { SelectionShape, ResizeDirection } from "../event"; |
| 14 | + |
| 15 | +const IDENTITY_CAMERA: cmath.Transform = [ |
| 16 | + [1, 0, 0], |
| 17 | + [0, 1, 0], |
| 18 | +]; |
| 19 | + |
| 20 | +function resizeChrome( |
| 21 | + shape: SelectionShape, |
| 22 | + direction: ResizeDirection, |
| 23 | + opts: { shift: boolean } |
| 24 | +) { |
| 25 | + const state = new SurfaceState(); |
| 26 | + state.setSelection(["a"]); |
| 27 | + state.setTransform(IDENTITY_CAMERA); |
| 28 | + state.modifiers.shift = opts.shift; |
| 29 | + state.gesture = { |
| 30 | + kind: "resize", |
| 31 | + ids: ["a"], |
| 32 | + direction, |
| 33 | + initial_shape: shape, |
| 34 | + anchor_doc: [0, 0], |
| 35 | + last_doc: [0, 0], |
| 36 | + current_shape: shape, |
| 37 | + preview_shape: shape, |
| 38 | + }; |
| 39 | + const shapeOf = (_: NodeId): SelectionShape | null => null; |
| 40 | + const { decoration } = buildChrome({ |
| 41 | + state, |
| 42 | + shapeOf, |
| 43 | + style: DEFAULT_STYLE, |
| 44 | + width: 1000, |
| 45 | + height: 1000, |
| 46 | + }); |
| 47 | + return decoration; |
| 48 | +} |
| 49 | + |
| 50 | +const RECT: Rect = { x: 0, y: 0, width: 100, height: 50 }; |
| 51 | + |
| 52 | +describe("aspect-ratio guide — Shift draws the diagonal of the preview box", () => { |
| 53 | + it("is absent without Shift", () => { |
| 54 | + const decoration = resizeChrome({ kind: "rect", rect: RECT }, "se", { |
| 55 | + shift: false, |
| 56 | + }); |
| 57 | + expect(decoration.lines ?? []).toHaveLength(0); |
| 58 | + }); |
| 59 | + |
| 60 | + it("'se' draws TL→BR (the corner opposite the dragged handle)", () => { |
| 61 | + const decoration = resizeChrome({ kind: "rect", rect: RECT }, "se", { |
| 62 | + shift: true, |
| 63 | + }); |
| 64 | + const lines = decoration.lines ?? []; |
| 65 | + expect(lines).toHaveLength(1); |
| 66 | + const l = lines[0]; |
| 67 | + expect(l.dashed).toBe(true); |
| 68 | + // toCorners = [TL, TR, BR, BL]; "se" → TL→BR. |
| 69 | + expect([l.x1, l.y1]).toEqual([0, 0]); |
| 70 | + expect([l.x2, l.y2]).toEqual([100, 50]); |
| 71 | + }); |
| 72 | + |
| 73 | + it("'e' edge uses the same SE diagonal (TL→BR)", () => { |
| 74 | + const decoration = resizeChrome({ kind: "rect", rect: RECT }, "e", { |
| 75 | + shift: true, |
| 76 | + }); |
| 77 | + const l = (decoration.lines ?? [])[0]; |
| 78 | + expect([l.x1, l.y1, l.x2, l.y2]).toEqual([0, 0, 100, 50]); |
| 79 | + }); |
| 80 | + |
| 81 | + it("'ne' draws BL→TR", () => { |
| 82 | + const decoration = resizeChrome({ kind: "rect", rect: RECT }, "ne", { |
| 83 | + shift: true, |
| 84 | + }); |
| 85 | + const l = (decoration.lines ?? [])[0]; |
| 86 | + // "ne" → BL(0,50) → TR(100,0). |
| 87 | + expect([l.x1, l.y1, l.x2, l.y2]).toEqual([0, 50, 100, 0]); |
| 88 | + }); |
| 89 | + |
| 90 | + it("transformed shape: the diagonal is projected through the matrix", () => { |
| 91 | + const local: Rect = { x: 0, y: 0, width: 100, height: 100 }; |
| 92 | + const matrix = cmath.transform.rotate( |
| 93 | + cmath.transform.identity, |
| 94 | + 30, |
| 95 | + [50, 50] |
| 96 | + ); |
| 97 | + const decoration = resizeChrome( |
| 98 | + { kind: "transformed", local, matrix }, |
| 99 | + "se", |
| 100 | + { shift: true } |
| 101 | + ); |
| 102 | + const l = (decoration.lines ?? [])[0]; |
| 103 | + expect(l.dashed).toBe(true); |
| 104 | + // "se" → local TL(0,0) → BR(100,100), each projected by the matrix. |
| 105 | + const [ex1, ey1] = cmath.vector2.transform([0, 0], matrix); |
| 106 | + const [ex2, ey2] = cmath.vector2.transform([100, 100], matrix); |
| 107 | + expect(l.x1).toBeCloseTo(ex1, 9); |
| 108 | + expect(l.y1).toBeCloseTo(ey1, 9); |
| 109 | + expect(l.x2).toBeCloseTo(ex2, 9); |
| 110 | + expect(l.y2).toBeCloseTo(ey2, 9); |
| 111 | + }); |
| 112 | + |
| 113 | + it("coexists with the dashed preview rect (both are emitted)", () => { |
| 114 | + const decoration = resizeChrome({ kind: "rect", rect: RECT }, "se", { |
| 115 | + shift: true, |
| 116 | + }); |
| 117 | + expect((decoration.rects ?? []).filter((r) => r.dashed)).toHaveLength(1); |
| 118 | + expect(decoration.lines ?? []).toHaveLength(1); |
| 119 | + }); |
| 120 | +}); |
0 commit comments