Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions e2e/nodes-snap.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,10 @@ async function drawRectanglePath(): Promise<void> {
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);
Expand Down
14 changes: 13 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -310,6 +310,18 @@ function SettingsDialog({ onClose }: { onClose: () => void }) {
Snap to grid
</label>
</div>
<div className="field-row">
<span className="field-label">Click select</span>
<select
className="input"
value={state.clickSelection}
title="How a click picks a shape on the canvas"
onChange={(e) => store.setClickSelection(e.target.value as ClickSelectionMode)}
>
<option value="stroke">On the line</option>
<option value="bounds">Inside the bounding box</option>
</select>
</div>

<h4 style={{ margin: '14px 0 8px' }}>Workspace</h4>
<div className="field-row">
Expand Down
25 changes: 25 additions & 0 deletions src/state/store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>();
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');
});
});
32 changes: 32 additions & 0 deletions src/state/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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'] {
Expand Down Expand Up @@ -146,6 +167,7 @@ function initialState(): AppState {
snapToGrid: snapSettings.enabled && snapSettings.grid,
snapSettings,
lockAspect: true,
clickSelection: loadClickSelection(),
zoom: 1,
selection: null,
cursor: null,
Expand Down Expand Up @@ -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 });
Expand Down
17 changes: 17 additions & 0 deletions src/workspace/WorkspaceController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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)',
Expand Down Expand Up @@ -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();
Expand Down