diff --git a/e2e/nodes-snap.spec.ts b/e2e/nodes-snap.spec.ts index b6d0191..bc19dda 100644 --- a/e2e/nodes-snap.spec.ts +++ b/e2e/nodes-snap.spec.ts @@ -99,8 +99,10 @@ async function drawRectanglePath(): Promise { throw new Error(`rectangle not created: ${JSON.stringify(state)}`); } await page.keyboard.press('Escape'); - // Select the rectangle and turn it into an editable path. - await page.mouse.click(cx, cy); + // Select the rectangle and turn it into an editable path. Click its left + // edge: with "On the line" click selection the empty inside of an unfilled + // shape lets clicks through. + await page.mouse.click(cx - 90, cy); await expect.poll(() => page.evaluate( () => (window as unknown as { __openburn: Controller }).__openburn.canvas.getActiveObjects().length, )).toBe(1); diff --git a/src/App.tsx b/src/App.tsx index 8a988fc..81cf067 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,5 +1,5 @@ import { useEffect, useState } from 'react'; -import { store, useAppState } from '@/state/store'; +import { store, useAppState, type ClickSelectionMode } from '@/state/store'; import type { WorkspaceController } from '@/workspace/WorkspaceController'; import { MenuBar } from '@/ui/MenuBar'; import { @@ -310,6 +310,18 @@ function SettingsDialog({ onClose }: { onClose: () => void }) { Snap to grid +
+ Click select + +

Workspace

diff --git a/src/state/store.test.ts b/src/state/store.test.ts index 790b506..08d990e 100644 --- a/src/state/store.test.ts +++ b/src/state/store.test.ts @@ -64,3 +64,28 @@ describe('state/store device migration', () => { expect(JSON.parse(localStorage.getItem(DEVICE_KEY) ?? '{}').origin).toBe('top-right'); }); }); + +describe('click selection preference', () => { + it('defaults to stroke picking and survives a reload', async () => { + const { loadClickSelection, CLICK_SELECTION_KEY } = await import('./store'); + const mem = new Map(); + const storage = { + getItem: (k: string) => mem.get(k) ?? null, + setItem: (k: string, v: string) => void mem.set(k, v), + } as unknown as Storage; + expect(loadClickSelection(storage)).toBe('stroke'); + mem.set(CLICK_SELECTION_KEY, 'bounds'); + expect(loadClickSelection(storage)).toBe('bounds'); + mem.set(CLICK_SELECTION_KEY, 'garbage'); + expect(loadClickSelection(storage)).toBe('stroke'); + }); + + it('setClickSelection updates state and storage', async () => { + const { store, CLICK_SELECTION_KEY } = await import('./store'); + store.setClickSelection('bounds'); + expect(store.getState().clickSelection).toBe('bounds'); + expect(localStorage.getItem(CLICK_SELECTION_KEY)).toBe('bounds'); + store.setClickSelection('stroke'); + expect(store.getState().clickSelection).toBe('stroke'); + }); +}); diff --git a/src/state/store.ts b/src/state/store.ts index d220889..5e42b85 100644 --- a/src/state/store.ts +++ b/src/state/store.ts @@ -65,6 +65,12 @@ export interface AppState { snapSettings: SnapSettings; /** Keep the aspect ratio when scaling from the W/H fields or the corners */ lockAspect: boolean; + /** + * How a click picks a shape: `stroke` only when it lands on drawn pixels + * (lets you reach shapes nested inside a bigger one's box), + * `bounds` anywhere inside the shape's bounding box (older behaviour). + */ + clickSelection: ClickSelectionMode; zoom: number; selection: SelectionSnapshot | null; @@ -89,6 +95,21 @@ export interface AppState { const DEVICE_KEY = 'openburn.device'; const OPTIMIZATION_KEY = 'openburn.optimization'; const LAYER_DEFAULTS_KEY = 'openburn.layerDefaults'; +export const CLICK_SELECTION_KEY = 'openburn.clickSelection'; + +export type ClickSelectionMode = 'stroke' | 'bounds'; +export const DEFAULT_CLICK_SELECTION: ClickSelectionMode = 'stroke'; + +export function loadClickSelection( + storage: Storage | null = typeof localStorage !== 'undefined' ? localStorage : null, +): ClickSelectionMode { + try { + const raw = storage?.getItem(CLICK_SELECTION_KEY); + return raw === 'stroke' || raw === 'bounds' ? raw : DEFAULT_CLICK_SELECTION; + } catch { + return DEFAULT_CLICK_SELECTION; + } +} /** Older documents only knew two corners, and a corrupted value must not break the job. */ function normaliseDeviceOrigin(value: unknown): DeviceConfig['origin'] { @@ -146,6 +167,7 @@ function initialState(): AppState { snapToGrid: snapSettings.enabled && snapSettings.grid, snapSettings, lockAspect: true, + clickSelection: loadClickSelection(), zoom: 1, selection: null, cursor: null, @@ -193,6 +215,16 @@ class AppStore { }); } + /** Choose how a click picks shapes on the canvas; remembered across launches. */ + setClickSelection(mode: ClickSelectionMode): void { + try { + localStorage.setItem(CLICK_SELECTION_KEY, mode); + } catch { + /* storage may be unavailable */ + } + this.setState({ clickSelection: mode }); + } + /** Flip the master snap switch (status-bar button). */ toggleSnap(): void { this.setSnapSettings({ enabled: !this.state.snapSettings.enabled }); diff --git a/src/workspace/WorkspaceController.ts b/src/workspace/WorkspaceController.ts index 189ea53..893c7e8 100644 --- a/src/workspace/WorkspaceController.ts +++ b/src/workspace/WorkspaceController.ts @@ -29,6 +29,7 @@ import { type TextAlign, type ToolId, type WorkspaceApi, + type ClickSelectionMode, } from '@/state/store'; import { buildDocument, parseDocument, serializeDocument } from '@/core/serialize'; import { HistoryBuffer } from './history'; @@ -109,6 +110,9 @@ const SELECTION_ACCENT = '#3f8fdd'; */ const HAIRLINE_PX = 1; +/** How close (screen pixels) a click must land to a stroke in 'stroke' click selection. */ +const CLICK_SELECTION_TOLERANCE_PX = 5; + /** Space left for the viewport rulers (top band + left band) when fitting. */ const RULER_INSET = 34; @@ -215,6 +219,12 @@ export class WorkspaceController implements WorkspaceApi { this.canvas = new Canvas(this.fabricEl, { backgroundColor: 'transparent', preserveObjectStacking: true, + // 'stroke' click selection picks shapes by their drawn pixels (strokes, + // fills, letters), so a large shape or group does not + // swallow clicks aimed at smaller shapes nested inside its box. + // 'bounds' keeps the older bounding-box picking. See applyClickSelection. + perPixelTargetFind: state.clickSelection === 'stroke', + targetFindTolerance: CLICK_SELECTION_TOLERANCE_PX, selection: true, selectionColor: 'rgba(63,143,221,0.10)', selectionBorderColor: 'rgba(63,143,221,0.75)', @@ -280,9 +290,16 @@ export class WorkspaceController implements WorkspaceApi { this.canvas.requestRenderAll(); } + /** Switch between stroke (per-pixel) and bounding-box click picking. */ + private applyClickSelection(mode: ClickSelectionMode): void { + const perPixel = mode === 'stroke'; + if (this.canvas.perPixelTargetFind !== perPixel) this.canvas.perPixelTargetFind = perPixel; + } + private onStoreChange = (): void => { const s = store.getState(); this.applyAspectLock(); + this.applyClickSelection(s.clickSelection); if (s.doc.layers !== this.lastLayers) { this.lastLayers = s.doc.layers; this.applyLayerStyles();