Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
import type { CanvasEngine, CanvasInteractionState, ToolId } from '@workbench/canvas-engine/api';
import type { ShortcutHint, ShortcutHintSnapshot, ShortcutHintSource } from '@workbench/hotkeys/hintSources';
import { getCanvasOperations } from '@workbench/canvas-operations/api';
import { MOD_KEY_LABEL } from '@workbench/hotkeys/keys';
const gesture = (label: string, parts: readonly string[] = [], pointerKey?: string): ShortcutHint => ({
labelKey: `workbench.shortcuts.actions.${label}`,
parts,
pointerKey: pointerKey ? `workbench.shortcuts.pointer.${pointerKey}` : undefined,
});
const PAN = gesture('pan', ['space'], 'drag');
const ZOOM = gesture('zoom', [], 'scroll');
const SAMPLE = gesture('sample', ['alt'], 'click');
const CANCEL = gesture('cancel', ['esc']);
const POLYGON_HINTS = [gesture('placePoint', [], 'click'), gesture('closePolygon', ['enter']), CANCEL];
/** Alt held before or during a drag skips snapping to the grid (bbox, move, transform). */
const FREE_OF_GRID = gesture('ignoreGrid', ['alt'], 'drag');
/** C: hold for the Frame tool until release; a quick tap keeps it selected. */
const FRAME_KEY = gesture('frameKey', ['c']);
/** Fixed gestures follow engine tools; configurable commands are resolved by the Workbench host. */
export const getCanvasShortcutHints = ({
activeTool,
lassoShape,
shapeKind,
hasTransform,
hasSelection,
canUndo,
operation,
samVisual,
}: {
activeTool: ToolId;
lassoShape: string;
shapeKind: string;
hasTransform: boolean;
hasSelection: boolean;
canUndo: boolean;
operation: 'filter' | 'select-object' | null;
samVisual: boolean;
}): ShortcutHintSnapshot => {
const hints: ShortcutHint[] = [];
if (operation === 'filter' || (operation === 'select-object' && !samVisual)) {
return { hints: [PAN, ZOOM], titleKey: 'widgets.canvas.tools.view' };
}
const tool = operation === 'select-object' ? 'sam' : activeTool;
if (tool === 'sam') {
hints.push(
gesture('samPoint', [], 'click'),
gesture('samOppositePoint', ['shift'], 'click'),
gesture('samRemovePoint', [], 'point'),
gesture('samBox', [], 'drag')
);
} else if (tool === 'transform' && hasTransform) {
hints.push(
gesture('apply', ['enter']),
CANCEL,
gesture('constrainTransform', ['shift'], 'drag'),
gesture('scaleCenter', ['alt'], 'handleDrag')
);
} else if (tool === 'marquee') {
hints.push(
gesture('select', [], 'drag'),
gesture('addBeforeDrag', ['shift'], 'drag'),
gesture('subtractBeforeDrag', ['alt'], 'drag'),
gesture('constrainDuringDrag', ['shift']),
gesture('centerDuringDrag', ['alt']),
CANCEL
);
} else if (tool === 'lasso') {
hints.push(
...(lassoShape === 'polygon' ? POLYGON_HINTS : [gesture('select', [], 'drag'), CANCEL]),
gesture('addSelection', ['shift']),
gesture('subtractSelection', ['alt'])
);
} else if (tool === 'shape') {
const isBox = shapeKind !== 'polygon' && shapeKind !== 'freehand';
hints.push(
...(shapeKind === 'polygon' ? POLYGON_HINTS : [gesture('draw', [], 'drag'), CANCEL]),
...(isBox ? [gesture('constrain', ['shift'], 'drag'), gesture('drawFromCenter', ['alt'], 'drag')] : []),
// The platform's mod key cuts out: ⌘ on macOS, Ctrl elsewhere.
gesture('cutOutShape', [MOD_KEY_LABEL])
);
} else if (tool === 'bbox') {
hints.push(
gesture('moveFrame', [], 'drag'),
gesture('resizeFrame', [], 'handleDrag'),
gesture('constrainFrame', ['shift'], 'drag'),
gesture('resizeFromCenter', ['ctrl'], 'handleDrag'),
FREE_OF_GRID,
FRAME_KEY
);
} else if (tool === 'move') {
hints.push(gesture('moveLayer', [], 'drag'), gesture('constrainMove', ['shift'], 'drag'), FREE_OF_GRID, {
commandId: 'canvas.nudgeRight',
labelKey: 'workbench.shortcuts.actions.nudge',
});
} else if (tool === 'view') {
hints.push(gesture('pan', [], 'drag'), ZOOM);
} else if (tool === 'colorPicker') {
hints.push(gesture('sample', [], 'click'));
} else if (tool === 'brush' || tool === 'eraser') {
hints.push(
{ commandId: 'canvas.brushSizeDown', labelKey: 'workbench.shortcuts.actions.smaller' },
{ commandId: 'canvas.brushSizeUp', labelKey: 'workbench.shortcuts.actions.larger' },
gesture('straightLine', ['shift'], 'click'),
SAMPLE
);
}
if (tool !== 'view') {
hints.push(PAN, ZOOM);
}
if (hasSelection && !operation) {
hints.push({ commandId: 'canvas.deselect', labelKey: 'workbench.shortcuts.actions.deselect' });
}
if (canUndo && !operation) {
hints.push({ commandId: 'canvas.undo', labelKey: 'workbench.shortcuts.actions.undo' });
}
return { hints, titleKey: `widgets.canvas.tools.${tool}` };
};
const HINT_STATE_KEYS: readonly (keyof CanvasInteractionState)[] = [
'activeTool',
'lassoOptions',
'shapeOptions',
'transformSession',
'hasFloatingSelection',
'hasSelection',
'canUndo',
];
/** Observe an existing engine only. No lease, pixel/document epoch, pointer coordinate or parameter subscriptions. */
export const createCanvasShortcutHintSource = (
engine: CanvasEngine,
projectId: string,
instanceId: string
): ShortcutHintSource => {
const operations = getCanvasOperations(engine);
let snapshot: ShortcutHintSnapshot | null = null;
let inputs: Parameters<typeof getCanvasShortcutHints>[0] | null = null;
return {
projectId,
instanceId,
getSnapshot: () => {
const operation = operations.getOperationState();
const sam = operations.getSamSessionState();
const nextInputs = {
activeTool: engine.interaction.get('activeTool'),
canUndo: engine.interaction.get('canUndo'),
hasSelection: engine.interaction.get('hasSelection'),
hasTransform:
engine.interaction.get('transformSession') !== null || engine.interaction.get('hasFloatingSelection'),
lassoShape: engine.interaction.get('lassoOptions').shape,
operation: operation.status === 'active' ? operation.identity.kind : null,
samVisual: sam?.input.type === 'visual' && sam.status !== 'committing',
shapeKind: engine.interaction.get('shapeOptions').kind,
} satisfies Parameters<typeof getCanvasShortcutHints>[0];
if (
!snapshot ||
!inputs ||
(Object.keys(nextInputs) as (keyof typeof nextInputs)[]).some((key) => inputs![key] !== nextInputs[key])
) {
snapshot = getCanvasShortcutHints(nextInputs);
inputs = nextInputs;
}
return snapshot;
},
subscribe: (listener) => {
const unsubscribes = [
...HINT_STATE_KEYS.map((key) => engine.interaction.subscribe(key, listener)),
operations.subscribeOperation(listener),
operations.subscribeSamSession(listener),
];
return () => unsubscribes.forEach((unsubscribe) => unsubscribe());
},
};
};