Skip to content

Commit

Permalink
feat(shortcuts): add annotation tool actions
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulHax authored and floryst committed Oct 6, 2023
1 parent e68012f commit 3cda313
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 13 deletions.
23 changes: 19 additions & 4 deletions src/composables/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useRulerStore } from '../store/tools/rulers';
import { usePolygonStore } from '../store/tools/polygons';
import { Action } from '../constants';

const applyLabelOffset = (offset: number) => {
const applyLabelOffset = (offset: number) => () => {
const toolToStore = {
[Tools.Rectangle]: useRectangleStore(),
[Tools.Ruler]: useRulerStore(),
Expand All @@ -26,7 +26,22 @@ const applyLabelOffset = (offset: number) => {
activeToolStore.setActiveLabel(nextLabel);
};

const setTool = (tool: Tools) => () => {
useToolStore().setCurrentTool(tool);
};

export const ACTION_TO_FUNC = {
'decrement-label': () => applyLabelOffset(-1),
'increment-label': () => applyLabelOffset(1),
} satisfies Record<Action, () => void>;
windowLevel: setTool(Tools.WindowLevel),
pan: setTool(Tools.Pan),
zoom: setTool(Tools.Zoom),
ruler: setTool(Tools.Ruler),
paint: setTool(Tools.Paint),
rectangle: setTool(Tools.Rectangle),
crosshairs: setTool(Tools.Crosshairs),
crop: setTool(Tools.Crop),
polygon: setTool(Tools.Polygon),
select: setTool(Tools.Select),

decrementLabel: applyLabelOffset(-1),
incrementLabel: applyLabelOffset(1),
} as const satisfies Record<Action, () => void>;
6 changes: 3 additions & 3 deletions src/composables/useKeyboardShortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ export const actionToKey = ref(ACTION_TO_KEY);

export function useKeyboardShortcuts() {
const keys = useMagicKeys();
const unwatchFuncs = ref([] as Array<ReturnType<typeof whenever>>);
let unwatchFuncs = [] as Array<ReturnType<typeof whenever>>;

watch(
actionToKey,
(actionMap) => {
unwatchFuncs.value.forEach((unwatch) => unwatch());
unwatchFuncs.forEach((unwatch) => unwatch());

unwatchFuncs.value = getEntries(actionMap).map(([action, key]) => {
unwatchFuncs = getEntries(actionMap).map(([action, key]) => {
return whenever(keys[key], ACTION_TO_FUNC[action]);
});
},
Expand Down
17 changes: 14 additions & 3 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,19 @@ export const DEFAULT_PRESET_BY_MODALITY: Record<string, string> = {
US: 'US-Fetal',
};

// Keyboard shortcuts/hotkeys
// Keyboard shortcuts/hotkeys. Can add modifiers: 'Shift+Ctrl+A'
export const ACTION_TO_KEY = {
'decrement-label': 'q',
'increment-label': 'w',
windowLevel: 'l',
pan: 'n',
zoom: 'z',
ruler: 'm',
paint: 'p',
rectangle: 'r',
crosshairs: 'c',
crop: 'b',
polygon: 'g',
select: 's',

decrementLabel: 'q',
incrementLabel: 'w',
} satisfies Record<Action, string>;
18 changes: 17 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@ export const Messages = {

export const ANNOTATION_TOOL_HANDLE_RADIUS = 10; // pixels

export const ACTIONS = ['decrement-label', 'increment-label'] as const;
export const ACTIONS = [
// set the current tool
'windowLevel',
'pan',
'zoom',
'ruler',
'paint',
'rectangle',
'crosshairs',
'crop',
'polygon',
'select',

// change the current label for the current tool
'decrementLabel',
'incrementLabel',
] as const;

export type Action = (typeof ACTIONS)[number];
2 changes: 1 addition & 1 deletion src/io/import/processors/handleConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const dataBrowser = z
})
.optional();

const shortcuts = z.record(z.enum(ACTIONS)).optional();
const shortcuts = z.record(z.enum(ACTIONS), z.string()).optional();

// --------------------------------------------------------------------------
// Labels
Expand Down
2 changes: 1 addition & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,6 @@ type Entries<T> = {
[K in keyof T]-?: [K, T[K]];
}[keyof T][];

// Object.entries with keys preserved
// Object.entries with keys preserved rather as string
export const getEntries = <T extends object>(obj: T) =>
Object.entries(obj) as Entries<T>;

0 comments on commit 3cda313

Please sign in to comment.