GeezerVison is showing github.com — fix: the action bar keeps clear of the selection guide badges under t… · u8array/ZPLab@6cb2201
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: e2e/selectionGuides.spec.ts +62Lines changed: 62 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -85,6 +85,10 @@ async function nearBadges(page: Page) { | 85 | 85 | }); | 86 | 86 | } | 87 | 87 |
| | 88 | +const RULER = 20; | | 89 | +const BAR_GAP = 22; | | 90 | +const BADGE_ROW_BOTTOM = 38; | | 91 | + | 88 | 92 | /** Adds a Box and waits until the guides sit on it. */ | 89 | 93 | async function addBox(page: Page) { | 90 | 94 | const before = (await guides(page))?.frame; | @@ -274,3 +278,61 @@ test('the action bar covers the guides that run behind it', async ({ page }) => | 274 | 278 | await expect.poll(covered).toEqual({ guidePixels: 0, inside: true }); | 275 | 279 | expect(pageErrors, pageErrors.map((e) => e.message).join('\n')).toHaveLength(0); | 276 | 280 | }); | | 281 | + | | 282 | +test('the action bar stays out of the badge row under the top ruler', async ({ page }) => { | | 283 | + const origin = await open(page); | | 284 | + await addBox(page); | | 285 | + const { frame } = (await guides(page)) as Guides; | | 286 | + const midY = origin.y + (frame.minY + frame.maxY) / 2; | | 287 | + await page.mouse.move(origin.x + frame.maxX, midY); | | 288 | + await page.mouse.down(); | | 289 | + await page.mouse.move(origin.x + frame.minX + 24, midY, { steps: 6 }); | | 290 | + await page.mouse.up(); | | 291 | + await expect.poll(async () => ((await guides(page))?.frame.maxX ?? Infinity) - frame.minX).toBeLessThan(40); | | 292 | + | | 293 | + // Drag up to where the bar still sits above the box and would reach into the badge row. | | 294 | + const barHeight = await page.evaluate(() => { | | 295 | + interface Node { | | 296 | + findOne: (s: string) => { getClientRect: () => { height: number } } | undefined; | | 297 | + } | | 298 | + return (window as unknown as { Konva: { stages: Node[] } }).Konva.stages[0].findOne('.action-bar')?.getClientRect().height ?? 0; | | 299 | + }); | | 300 | + const reach = { from: RULER + BAR_GAP + barHeight, to: BADGE_ROW_BOTTOM + BAR_GAP + barHeight }; | | 301 | + const grabX = origin.x + frame.minX + 12; | | 302 | + await page.mouse.move(grabX, midY); | | 303 | + await page.mouse.down(); | | 304 | + // Ctrl suspends smart snapping so the box stays in reach. | | 305 | + await page.keyboard.down('Control'); | | 306 | + await page.mouse.move(grabX, midY - (frame.minY - (reach.from + reach.to) / 2), { steps: 8 }); | | 307 | + await page.mouse.up(); | | 308 | + await page.keyboard.up('Control'); | | 309 | + await expect.poll(async () => (await guides(page))?.frame.minY ?? Infinity).toBeLessThan(reach.to - 1); | | 310 | + expect((await guides(page))?.frame.minY).toBeGreaterThan(reach.from + 1); | | 311 | + | | 312 | + const overlaps = () => | | 313 | + page.evaluate(() => { | | 314 | + interface Rect { | | 315 | + x: number; | | 316 | + y: number; | | 317 | + width: number; | | 318 | + height: number; | | 319 | + } | | 320 | + interface Node { | | 321 | + find: (s: string) => Node[]; | | 322 | + findOne: (s: string) => Node | undefined; | | 323 | + isVisible: () => boolean; | | 324 | + getClientRect: () => Rect; | | 325 | + } | | 326 | + const stage = (window as unknown as { Konva: { stages: Node[] } }).Konva.stages[0]; | | 327 | + const bar = stage.findOne('.action-bar')?.getClientRect(); | | 328 | + if (!bar) return null; | | 329 | + const badges = ['x-near', 'x-far', 'y-near', 'y-far'].flatMap((name) => stage.find(`.badge-${name}`)); | | 330 | + return badges | | 331 | + .filter((badge) => badge.isVisible()) | | 332 | + .map((badge) => badge.getClientRect()) | | 333 | + .filter((b) => b.x < bar.x + bar.width && bar.x < b.x + b.width && b.y < bar.y + bar.height && bar.y < b.y + b.height) | | 334 | + .length; | | 335 | + }); | | 336 | + await expect.poll(overlaps).toBe(0); | | 337 | + expect(pageErrors, pageErrors.map((e) => e.message).join('\n')).toHaveLength(0); | | 338 | +}); |
Collapse file Copy file name to clipboardExpand all lines: src/components/Canvas/LabelCanvas.tsx +5-1Lines changed: 5 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -46,7 +46,7 @@ import { RfidPositionGuide } from "./RfidPositionGuide"; | 46 | 46 | import { Grid } from "./Grid"; | 47 | 47 | import { GuideLines } from "./GuideLines"; | 48 | 48 | import { Ruler, RULER_SIZE, type RulerProps } from "./Ruler"; | 49 | | -import { SelectionGuideLines, SelectionGuides, type SelectionGuidesHandle } from "./SelectionGuides"; | | 49 | +import { BADGE_ROW_PX, SelectionGuideLines, SelectionGuides, type SelectionGuidesHandle } from "./SelectionGuides"; | 50 | 50 | import { SHAPE_PRIMITIVE_TYPES } from "@zplab/core/registry"; | 51 | 51 | import type { LeafObject } from "@zplab/core/registry"; | 52 | 52 | import { isImageRotatable, type ImageProps } from "@zplab/core/registry/image"; | @@ -733,6 +733,8 @@ export const LabelCanvas = forwardRef<LabelCanvasHandle, Props>(function LabelCa | 733 | 733 | : null; | 734 | 734 | // The resize proxy frames the full boxes, while the other selection frames sit on the bars. | 735 | 735 | const frameOnBars = !multiResizeBboxDots; | | 736 | + // Reserves the badge row under the top ruler, see BADGE_ROW_PX. | | 737 | + const barTopInset = selectionGuidesEnabled ? BADGE_ROW_PX : 0; | 736 | 738 | const movableBoxBase = toFramePx(movableUnionDots); | 737 | 739 | const staticBoxBase = hasSelection ? toFramePx(selectionUnionDots(objects, staticSelIds, frameCtx)) : null; | 738 | 740 | // The bar-box pair keeps the frame on the bars mid-drag as well. | @@ -905,6 +907,7 @@ export const LabelCanvas = forwardRef<LabelCanvasHandle, Props>(function LabelCa | 905 | 907 | barHalfRef.current.h, | 906 | 908 | stage.width(), | 907 | 909 | stage.height(), | | 910 | + barTopInset, | 908 | 911 | ), | 909 | 912 | ); | 910 | 913 | } | @@ -1047,6 +1050,7 @@ export const LabelCanvas = forwardRef<LabelCanvasHandle, Props>(function LabelCa | 1047 | 1050 | actionBarRef, | 1048 | 1051 | lockedFrameRef, | 1049 | 1052 | getBarBounds: () => selectionBoundsStage(), | | 1053 | + topInset: barTopInset, | 1050 | 1054 | }); | 1051 | 1055 | useSelectionGuides({ | 1052 | 1056 | linesRef: selectionGuideLinesRef, | |
Collapse file Copy file name to clipboardExpand all lines: src/components/Canvas/SelectionGuides.tsx +6-3Lines changed: 6 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -6,6 +6,10 @@ import type { Bounds } from "./nodeRect"; | 6 | 6 | import { axisGuides, badgeOffsets, type AxisGuides, type GuideAxis, type GuideReadout } from "./selectionGuideGeometry"; | 7 | 7 |
| 8 | 8 | const BADGE_GAP = 3; | | 9 | +const BADGE_FONT_SIZE = 9; | | 10 | +const BADGE_PADDING = 3; | | 11 | +/** Height of the badge row under the top ruler, gaps included. */ | | 12 | +export const BADGE_ROW_PX = 2 * BADGE_GAP + BADGE_FONT_SIZE + 2 * BADGE_PADDING; | 9 | 13 | const AXES = ["x", "y"] as const; | 10 | 14 | const SIDES = ["near", "far"] as const; | 11 | 15 | type Axis = (typeof AXES)[number]; | @@ -97,8 +101,7 @@ export function SelectionGuides({ | 97 | 101 | return edge && badge ? { edgePx: edge.px, size: badge[extent]() } : null; | 98 | 102 | }; | 99 | 103 | // The x badges sit in the row under the top ruler. The y badges start below it. | 100 | | - const rowHeight = (badges.near ?? badges.far)?.height() ?? 0; | 101 | | - const lead = key === "y" ? rowHeight + BADGE_GAP : 0; | | 104 | + const lead = key === "y" ? BADGE_ROW_PX - BADGE_GAP : 0; | 102 | 105 | const at = badgeOffsets(span("near"), span("far"), { ...axes[key], fromPx: axes[key].fromPx + lead }, BADGE_GAP); | 103 | 106 | for (const side of SIDES) { | 104 | 107 | const offset = at[side]; | @@ -138,7 +141,7 @@ export function SelectionGuides({ | 138 | 141 | SIDES.map((side) => ( | 139 | 142 | <Label key={`${key}-${side}`} name={`badge-${key}-${side}`}> | 140 | 143 | <Tag fill={colors.selection} cornerRadius={3} /> | 141 | | - <Text fill="#ffffff" fontSize={9} fontFamily={RULER_FONT} padding={3} /> | | 144 | + <Text fill="#ffffff" fontSize={BADGE_FONT_SIZE} fontFamily={RULER_FONT} padding={BADGE_PADDING} /> | 142 | 145 | </Label> | 143 | 146 | )), | 144 | 147 | )} | |
Collapse file Copy file name to clipboard +24Lines changed: 24 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { describe, it, expect } from "vitest"; | | 2 | +import { ACTION_BAR_GAP_PX, actionBarPosition } from "./useSelectionActionBar"; | | 3 | +import { RULER_SIZE } from "../Ruler"; | | 4 | + | | 5 | +const HALF = { w: 75, h: 19 }; | | 6 | +const STAGE = { width: 800, height: 600 }; | | 7 | +const at = (minY: number, maxY: number, topInset = 0) => | | 8 | + actionBarPosition({ minX: 300, maxX: 340, minY, maxY }, HALF.w, HALF.h, STAGE.width, STAGE.height, topInset); | | 9 | + | | 10 | +describe("actionBarPosition", () => { | | 11 | + it("sits above the selection when the bar clears the ruler and below when it does not", () => { | | 12 | + const fits = RULER_SIZE + ACTION_BAR_GAP_PX + 2 * HALF.h; | | 13 | + expect(at(fits, fits + 40).y).toBe(fits - ACTION_BAR_GAP_PX - HALF.h); | | 14 | + expect(at(fits - 1, fits + 39).y).toBe(fits + 39 + ACTION_BAR_GAP_PX + HALF.h); | | 15 | + }); | | 16 | + | | 17 | + it("leaves the top inset free above the selection and when neither side fits", () => { | | 18 | + const inset = 21; | | 19 | + const fits = RULER_SIZE + inset + ACTION_BAR_GAP_PX + 2 * HALF.h; | | 20 | + expect(at(fits, fits + 40, inset).y).toBe(fits - ACTION_BAR_GAP_PX - HALF.h); | | 21 | + expect(at(fits - 1, fits + 39, inset).y).toBe(fits + 39 + ACTION_BAR_GAP_PX + HALF.h); | | 22 | + expect(at(30, STAGE.height - 10, inset).y).toBe(RULER_SIZE + inset + HALF.h); | | 23 | + }); | | 24 | +}); |
Collapse file Copy file name to clipboardExpand all lines: src/components/Canvas/hooks/useSelectionActionBar.ts +15-11Lines changed: 15 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -5,25 +5,27 @@ import type { Bounds } from "../nodeRect"; | 5 | 5 |
| 6 | 6 | export const ACTION_BAR_GAP_PX = 22; | 7 | 7 |
| 8 | | -/** Shared action-bar placement: centered over the selection, clamped to the | 9 | | - * canvas, flipped below when it would clip the ruler. */ | | 8 | +/** Places the bar over the selection, clamped to the canvas. It flips below the selection | | 9 | + * when it would clip the ruler, and `topInset` keeps a strip under the ruler free. */ | 10 | 10 | export function actionBarPosition( | 11 | 11 | sel: Bounds, | 12 | 12 | halfW: number, | 13 | 13 | halfH: number, | 14 | 14 | stageWidth: number, | 15 | 15 | stageHeight: number, | | 16 | + topInset: number, | 16 | 17 | ): { x: number; y: number } { | | 18 | + const top = RULER_SIZE + topInset; | 17 | 19 | const minCx = RULER_SIZE + halfW; | 18 | 20 | const maxCx = Math.max(minCx, stageWidth - halfW); | 19 | 21 | const x = Math.min(Math.max((sel.minX + sel.maxX) / 2, minCx), maxCx); | 20 | 22 | const aboveY = sel.minY - ACTION_BAR_GAP_PX - halfH; | 21 | 23 | const belowY = sel.maxY + ACTION_BAR_GAP_PX + halfH; | 22 | 24 | const y = | 23 | | - aboveY - halfH < RULER_SIZE | | 25 | + aboveY - halfH < top | 24 | 26 | ? belowY + halfH <= stageHeight | 25 | 27 | ? belowY | 26 | | - : RULER_SIZE + halfH | | 28 | + : top + halfH | 27 | 29 | : aboveY; | 28 | 30 | return { x, y }; | 29 | 31 | } | @@ -43,14 +45,15 @@ interface Options { | 43 | 45 | /** Selection bounds in stage px from objectBoundsDots (the optical box, incl. | 44 | 46 | * the ^GD diagonal overhang) so the rest position matches the drag center. */ | 45 | 47 | getBarBounds: () => Bounds | null; | | 48 | + /** Inset under the top ruler that the bar leaves free, in px. */ | | 49 | + topInset: number; | 46 | 50 | } | 47 | 51 |
| 48 | 52 | /** | 49 | | - * Positions the floating selection action bar (centred above the selection, | 50 | | - * flipped below when it would clip past the ruler, clamped horizontally) and an | 51 | | - * amber outline per locked leaf (the transformer skips locked nodes). Imperative | 52 | | - * via beforeDraw so the chrome tracks live node positions during drags, the same | 53 | | - * timing useKonvaTransformer relies on. | | 53 | + * Positions the floating selection action bar through actionBarPosition and draws an amber | | 54 | + * outline per locked leaf, since the transformer skips locked nodes. Runs in beforeDraw so | | 55 | + * the chrome tracks live node positions during drags, the same timing useKonvaTransformer | | 56 | + * relies on. | 54 | 57 | */ | 55 | 58 | export function useSelectionActionBar({ | 56 | 59 | stageRef, | @@ -61,6 +64,7 @@ export function useSelectionActionBar({ | 61 | 64 | actionBarRef, | 62 | 65 | lockedFrameRef, | 63 | 66 | getBarBounds, | | 67 | + topInset, | 64 | 68 | }: Options) { | 65 | 69 | useLayoutEffect(() => { | 66 | 70 | if (editorFrozen || attachableIds.length === 0) return; | @@ -84,7 +88,7 @@ export function useSelectionActionBar({ | 84 | 88 | if (bar) { | 85 | 89 | const barRect = bar.getClientRect({ relativeTo: stage, skipShadow: true }); | 86 | 90 | bar.position( | 87 | | - actionBarPosition(sel, barRect.width / 2, barRect.height / 2, stage.width(), stage.height()), | | 91 | + actionBarPosition(sel, barRect.width / 2, barRect.height / 2, stage.width(), stage.height(), topInset), | 88 | 92 | ); | 89 | 93 | } | 90 | 94 |
| @@ -106,5 +110,5 @@ export function useSelectionActionBar({ | 106 | 110 | return () => { | 107 | 111 | layer?.off(".actionbar"); | 108 | 112 | }; | 109 | | - }, [stageRef, attachableIds, lockedLeafIds, editorFrozen, dragActiveRef, actionBarRef, lockedFrameRef, getBarBounds]); | | 113 | + }, [stageRef, attachableIds, lockedLeafIds, editorFrozen, dragActiveRef, actionBarRef, lockedFrameRef, getBarBounds, topInset]); | 110 | 114 | } |
|