Skip to content
Merged
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: 6 additions & 0 deletions src-tauri/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,12 @@ impl Default for ScriblConfig {
rhs: "edit.duplicate".into(),
desc: Some("Duplicate".into()),
},
KeymapEntry {
mode: "v".into(),
lhs: "<D-S-l>".into(),
rhs: "selection.toggle_lock".into(),
desc: Some("Toggle Lock".into()),
},
],
toolbar: ToolbarConfig::default(),
theme: ThemeConfig::default(),
Expand Down
1 change: 1 addition & 0 deletions src-tauri/src/watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ scribl.keymap.set("v", "<D-c>", "edit.copy", { desc = "Copy" })
scribl.keymap.set("v", "<D-v>", "edit.paste", { desc = "Paste" })
scribl.keymap.set("v", "<D-x>", "edit.cut", { desc = "Cut" })
scribl.keymap.set("v", "<D-d>", "edit.duplicate", { desc = "Duplicate" })
scribl.keymap.set("v", "<D-S-l>", "selection.toggle_lock", { desc = "Toggle Lock" })

--------------------------------------------------------------------------------
-- 3. Floating Toolbar Layout
Expand Down
51 changes: 51 additions & 0 deletions src/components/canvas/SelectionOverlay.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,55 @@ describe('SelectionOverlay', () => {
const selectionBox = container.querySelector('[data-testid="unified-selection-box"]');
expect(selectionBox).toBeNull();
});

it('hides resize handles and renders lock badge when selected elements are locked', () => {
const lockedElements: Record<string, StrokeElement> = {
l1: {
...elements.s1,
id: 'l1',
locked: true,
},
};

const { container } = render(
<SelectionOverlay
elements={lockedElements}
selectedIds={['l1']}
camera={{ x: 0, y: 0, zoom: 1 }}
width={1000}
height={800}
marqueeBox={null}
/>,
);

const selectionBox = container.querySelector('[data-testid="unified-selection-box"]');
expect(selectionBox).not.toBeNull();

// Corner handles must NOT be rendered
const handles = container.querySelectorAll('[data-testid^="handle-"]');
expect(handles.length).toBe(0);

// Lock badge must be rendered
const lockBadge = container.querySelector('[data-testid="selection-lock-badge"]');
expect(lockBadge).not.toBeNull();
});

it('does not render lock badge when selected elements are unlocked', () => {
const { container } = render(
<SelectionOverlay
elements={elements}
selectedIds={['s1']}
camera={{ x: 0, y: 0, zoom: 1 }}
width={1000}
height={800}
marqueeBox={null}
/>,
);

const handles = container.querySelectorAll('[data-testid^="handle-"]');
expect(handles.length).toBe(4);

const lockBadge = container.querySelector('[data-testid="selection-lock-badge"]');
expect(lockBadge).toBeNull();
});
});
122 changes: 78 additions & 44 deletions src/components/canvas/SelectionOverlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ export const SelectionOverlay: React.FC<SelectionOverlayProps> = ({
const boxW = Math.max(1, screenBR.x - screenTL.x);
const boxH = Math.max(1, screenBR.y - screenTL.y);
const screenCenter = worldToScreen(bounds.centerX, bounds.centerY, camera, width, height);
const isAllLocked = activeSelectedIds.length > 0
&& activeSelectedIds.every((id) => elements[id]?.locked);

return (
<g key='unified-selection'>
Expand All @@ -61,53 +63,85 @@ export const SelectionOverlay: React.FC<SelectionOverlayProps> = ({
fill='var(--color-selection-fill, rgba(2, 132, 199, 0.04))'
stroke='var(--color-selection-stroke, #0284c7)'
strokeWidth={1.5}
strokeDasharray={isAllLocked ? '4 3' : undefined}
/>

{/* Lock Badge HUD */}
{isAllLocked && (
<g
data-testid='selection-lock-badge'
transform={`translate(${screenBR.x - 20}, ${screenTL.y - 24})`}
>
<rect
x={0}
y={0}
width={20}
height={20}
rx={4}
fill='var(--color-panel-bg, #0f172a)'
stroke='var(--color-selection-stroke, #0284c7)'
strokeWidth={1}
/>
<path
d='M7 9V7a3 3 0 0 1 6 0v2m-7 0h8a1 1 0 0 1 1 1v6a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1v-6a1 1 0 0 1 1-1z'
fill='none'
stroke='var(--color-selection-stroke, #0284c7)'
strokeWidth={1.4}
strokeLinecap='round'
strokeLinejoin='round'
/>
</g>
)}

{/* 4 Corner Resize Handles */}
<rect
data-testid='handle-nw'
x={screenTL.x - 4.5}
y={screenTL.y - 4.5}
width={9}
height={9}
fill='#ffffff'
stroke='var(--color-selection-stroke, #0284c7)'
strokeWidth={1.5}
rx={1.5}
/>
<rect
data-testid='handle-ne'
x={screenBR.x - 4.5}
y={screenTL.y - 4.5}
width={9}
height={9}
fill='#ffffff'
stroke='var(--color-selection-stroke, #0284c7)'
strokeWidth={1.5}
rx={1.5}
/>
<rect
data-testid='handle-se'
x={screenBR.x - 4.5}
y={screenBR.y - 4.5}
width={9}
height={9}
fill='#ffffff'
stroke='var(--color-selection-stroke, #0284c7)'
strokeWidth={1.5}
rx={1.5}
/>
<rect
data-testid='handle-sw'
x={screenTL.x - 4.5}
y={screenBR.y - 4.5}
width={9}
height={9}
fill='#ffffff'
stroke='var(--color-selection-stroke, #0284c7)'
strokeWidth={1.5}
rx={1.5}
/>
{!isAllLocked && (
<>
<rect
data-testid='handle-nw'
x={screenTL.x - 4.5}
y={screenTL.y - 4.5}
width={9}
height={9}
fill='#ffffff'
stroke='var(--color-selection-stroke, #0284c7)'
strokeWidth={1.5}
rx={1.5}
/>
<rect
data-testid='handle-ne'
x={screenBR.x - 4.5}
y={screenTL.y - 4.5}
width={9}
height={9}
fill='#ffffff'
stroke='var(--color-selection-stroke, #0284c7)'
strokeWidth={1.5}
rx={1.5}
/>
<rect
data-testid='handle-se'
x={screenBR.x - 4.5}
y={screenBR.y - 4.5}
width={9}
height={9}
fill='#ffffff'
stroke='var(--color-selection-stroke, #0284c7)'
strokeWidth={1.5}
rx={1.5}
/>
<rect
data-testid='handle-sw'
x={screenTL.x - 4.5}
y={screenBR.y - 4.5}
width={9}
height={9}
fill='#ffffff'
stroke='var(--color-selection-stroke, #0284c7)'
strokeWidth={1.5}
rx={1.5}
/>
</>
)}

{/* Rotation Center Pivot and HUD Badge */}
{rotationAngle !== undefined && rotationAngle !== null && (
Expand Down
8 changes: 8 additions & 0 deletions src/components/settings/KeybindingsTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,14 @@ const ALL_ACTIONS: ActionDefinition[] = [
defaultLhs: '<D-a>',
mode: 'v',
},
{
id: 'selection.toggle_lock_visual',
targetId: 'selection.toggle_lock',
label: 'Toggle Lock (Visual Mode)',
category: 'Visual Mode (Selection)',
defaultLhs: '<D-S-l>',
mode: 'v',
},
];

export const KeybindingsTab: React.FC = () => {
Expand Down
32 changes: 32 additions & 0 deletions src/core/commands/CommandRegistry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,36 @@ describe('CommandRegistry', () => {
expect(commandRegistry.execute('file.copy_png')).toBe(true);
expect(onCopyPng).toHaveBeenCalledTimes(2);
});

it('executes selection.toggle_lock and toggles element lock state', () => {
const dummyStroke: StrokeElement = {
id: 'lock-item',
type: 'stroke',
x: 0,
y: 0,
width: 10,
height: 10,
zIndex: 1,
color: '#000',
size: 2,
thinning: 0.6,
smoothing: 0.5,
points: [{ x: 0, y: 0, pressure: 0.5 }],
locked: false,
};
useDocumentStore.setState({
elements: { 'lock-item': dummyStroke },
elementIds: ['lock-item'],
selectedIds: ['lock-item'],
});

const cmd = commandRegistry.get('selection.toggle_lock');
expect(cmd).toBeDefined();

commandRegistry.execute('selection.toggle_lock');
expect(useDocumentStore.getState().elements['lock-item'].locked).toBe(true);

commandRegistry.execute('selection.toggle_lock');
expect(useDocumentStore.getState().elements['lock-item'].locked).toBe(false);
});
});
10 changes: 10 additions & 0 deletions src/core/commands/CommandRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,16 @@ export function registerDefaultCommands(handlers?: {
execute: () => {},
});

commandRegistry.register({
id: 'selection.toggle_lock',
label: 'Toggle Lock',
category: 'selection',
description: 'Lock or unlock selected elements',
execute: () => {
useDocumentStore.getState().toggleLockSelected();
},
});

// Clipboard & Duplicate
commandRegistry.register({
id: 'edit.copy',
Expand Down
88 changes: 88 additions & 0 deletions src/core/engine/HitTester.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,4 +498,92 @@ describe('HitTester', () => {
expect(hitTestShape(unknown, { x: 5, y: 5 })).toBe(false);
});
});

describe('Locked elements hit testing', () => {
const lockedShape: ShapeElement = {
id: 'locked_shape',
type: 'shape',
shapeType: 'rectangle',
x: 50,
y: 50,
width: 100,
height: 100,
zIndex: 2,
strokeColor: '#000',
fillColor: '#fff',
strokeWidth: 2,
strokeStyle: 'solid',
locked: true,
};

const unlockedStroke: StrokeElement = {
id: 'unlocked_stroke',
type: 'stroke',
x: 60,
y: 60,
width: 50,
height: 50,
zIndex: 1,
color: '#ff0000',
size: 4,
thinning: 0.6,
smoothing: 0.5,
points: [
{ x: 60, y: 60, pressure: 0.5 },
{ x: 100, y: 100, pressure: 0.5 },
],
locked: false,
};

const lockedElements = {
unlocked_stroke: unlockedStroke,
locked_shape: lockedShape,
};
const lockedElementIds = ['unlocked_stroke', 'locked_shape'];

it('hits locked element directly when ignoreLocked is omitted or false', () => {
const hit = hitTestPoint(lockedElements, lockedElementIds, { x: 80, y: 80 });
expect(hit).toBe('locked_shape');
});

it('passes through locked element to hit underlying unlocked element when ignoreLocked is true', () => {
// Point (80, 80) is on both the locked_shape (zIndex 2) and unlocked_stroke (zIndex 1)
const hit = hitTestPoint(lockedElements, lockedElementIds, { x: 80, y: 80 }, 8, {
ignoreLocked: true,
});
expect(hit).toBe('unlocked_stroke');
});

it('returns null when ignoreLocked is true and no unlocked elements intersect point', () => {
const hit = hitTestPoint(lockedElements, lockedElementIds, { x: 140, y: 140 }, 8, {
ignoreLocked: true,
});
expect(hit).toBeNull();
});

it('skips locked elements in marquee selection', () => {
const selected = hitTestMarquee(lockedElements, lockedElementIds, {
minX: 0,
minY: 0,
maxX: 200,
maxY: 200,
});
expect(selected).toEqual(['unlocked_stroke']);
});

it('includes locked elements in marquee selection when includeLocked is true', () => {
const selected = hitTestMarquee(
lockedElements,
lockedElementIds,
{
minX: 0,
minY: 0,
maxX: 200,
maxY: 200,
},
{ includeLocked: true },
);
expect(selected).toEqual(['unlocked_stroke', 'locked_shape']);
});
});
});
Loading
Loading