Skip to content

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.

refactor(canvas): organize layer context menu like legacy

joshistoast07fec997ab0a
diff --git a/invokeai/frontend/webv2/public/locales/en.json b/invokeai/frontend/webv2/public/locales/en.json
index 4ddb627ab9..b851f0ff20 100644
--- a/invokeai/frontend/webv2/public/locales/en.json
+++ b/invokeai/frontend/webv2/public/locales/en.json
@@ -1289,6 +1289,14 @@
"layers": "Layers",
"regional": "Regional"
},
+ "menu": {
+ "add": "Add",
+ "addModifiers": "Add modifiers",
+ "arrange": "Arrange",
+ "booleanOperations": "Boolean operations",
+ "convertTo": "Convert to",
+ "copyTo": "Copy to"
+ },
"options": "Layer options",
"properties": "Layer properties",
"regionalGuidance": {
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/layers/LayerContextMenu.tsx b/invokeai/frontend/webv2/src/workbench/widgets/layers/LayerContextMenu.tsx
index ace7fe45f2..8067086fd5 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/layers/LayerContextMenu.tsx
+++ b/invokeai/frontend/webv2/src/workbench/widgets/layers/LayerContextMenu.tsx
@@ -15,8 +15,11 @@ import { useActiveProjectSelector } from '@workbench/WorkbenchContext';
import {
ArrowDownIcon,
ArrowDownToLineIcon,
+ ArrowRightLeftIcon,
ArrowUpIcon,
ArrowUpToLineIcon,
+ ArrowUpDownIcon,
+ ChevronRightIcon,
CopyIcon,
CropIcon,
EyeIcon,
@@ -27,6 +30,7 @@ import {
MergeIcon,
MoreVerticalIcon,
PencilIcon,
+ PlusIcon,
SaveIcon,
SlidersHorizontalIcon,
Trash2Icon,
@@ -34,9 +38,11 @@ import {
import { useCallback, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';
+import type { LayerContextMenuItem, LayerContextMenuSection, LayerContextSubmenuId } from './layerContextMenuLayout';
import type { LayerMoveKind } from './layerGroups';
import { getLayerContextActions, type LayerContextAction, type LayerContextActionId } from './layerContextActions';
+import { getLayerContextMenuLayout } from './layerContextMenuLayout';
import { copyBlobToClipboard } from './layerExportActions';
import { reorderWithinGroupByKind } from './layerGroups';
import { resolveMenuTargetForRender } from './layerMenuState';
@@ -338,15 +344,7 @@ const LayerMenu = ({
() => getLayerContextActions({ hasEngine: !!engine, index, layer, layers }),
[engine, index, layer, layers]
);
- const iconActions = useMemo(() => actions.filter((action) => action.group === 'icons'), [actions]);
- const actionGroups = useMemo(
- () =>
- LAYER_ACTION_GROUPS.map((group) => ({
- actions: actions.filter((action) => action.group === group),
- group,
- })).filter((entry) => entry.actions.length > 0),
- [actions]
- );
+ const menuLayout = useMemo(() => getLayerContextMenuLayout(actions), [actions]);
const getActionLabel = useCallback(
(id: LayerContextActionId) => {
@@ -623,13 +621,8 @@ const LayerMenu = ({
<Portal>
<Menu.Positioner>
<MenuContent minW="14rem" py="1">
- <HStack gap="1">
- {iconActions.map((action) => (
- <LayerMenuIconActionItem key={action.id} action={action} handleAction={handleAction} t={t} />
- ))}
- </HStack>
- {actionGroups.map(({ actions, group }) => (
- <LayerMenuGroup key={group} actions={actions} handleAction={handleAction} t={t} />
+ {menuLayout.map((section) => (
+ <LayerMenuSection key={section.id} handleAction={handleAction} section={section} t={t} />
))}
</MenuContent>
</Menu.Positioner>
@@ -754,14 +747,6 @@ export const CanvasLayerContextMenu = ({
);
};
-const LAYER_ACTION_GROUPS: readonly LayerContextAction['group'][] = [
- 'edit',
- 'convert',
- 'layerConfig',
- 'state',
- 'danger',
-];
-
const LAYER_ACTION_ICONS: Record<LayerContextActionId, LucideIcon> = {
'control-transparency-effect': SlidersHorizontalIcon,
'copy-to-clipboard': CopyIcon,
@@ -804,22 +789,119 @@ const LAYER_ACTION_ICONS: Record<LayerContextActionId, LucideIcon> = {
const stopPropagation = (event: { stopPropagation: () => void }): void => event.stopPropagation();
-const LayerMenuGroup = ({
- actions,
+const SUBMENU_META: Record<LayerContextSubmenuId, { defaultLabel: string; icon: LucideIcon; labelKey: string }> = {
+ 'add-modifiers': {
+ defaultLabel: 'Add modifiers',
+ icon: PlusIcon,
+ labelKey: 'widgets.layers.menu.addModifiers',
+ },
+ 'add-regional': { defaultLabel: 'Add', icon: PlusIcon, labelKey: 'widgets.layers.menu.add' },
+ arrange: { defaultLabel: 'Arrange', icon: ArrowUpDownIcon, labelKey: 'widgets.layers.menu.arrange' },
+ boolean: { defaultLabel: 'Boolean operations', icon: MergeIcon, labelKey: 'widgets.layers.menu.booleanOperations' },
+ 'convert-to': { defaultLabel: 'Convert to', icon: ArrowRightLeftIcon, labelKey: 'widgets.layers.menu.convertTo' },
+ 'copy-to': { defaultLabel: 'Copy to', icon: CopyIcon, labelKey: 'widgets.layers.menu.copyTo' },
+};
+
+const SUBMENU_POSITIONING = { placement: 'right-start' } as const;
+
+const LayerMenuSection = ({
handleAction,
+ section,
t,
}: {
- actions: readonly LayerContextAction[];
handleAction: (id: LayerContextActionId) => void;
+ section: LayerContextMenuSection;
t: (key: string, options: { defaultValue: string }) => string;
-}) => (
- <>
- <Menu.Separator borderColor="border.subtle" />
- {actions.map((action) => (
- <LayerMenuActionItem key={action.id} action={action} handleAction={handleAction} t={t} />
- ))}
- </>
-);
+}) => {
+ const content = section.items.map((item) => (
+ <LayerMenuLayoutItem
+ key={item.kind === 'action' ? item.action.id : item.id}
+ compact={section.presentation === 'row'}
+ handleAction={handleAction}
+ item={item}
+ t={t}
+ />
+ ));
+
+ if (section.presentation === 'row') {
+ return <HStack gap="1">{content}</HStack>;
+ }
+
+ return (
+ <>
+ <Menu.Separator borderColor="border.subtle" />
+ {content}
+ </>
+ );
+};
+
+const LayerMenuLayoutItem = ({
+ compact,
+ handleAction,
+ item,
+ t,
+}: {
+ compact: boolean;
+ handleAction: (id: LayerContextActionId) => void;
+ item: LayerContextMenuItem;
+ t: (key: string, options: { defaultValue: string }) => string;
+}) => {
+ if (item.kind === 'action') {
+ return compact ? (
+ <LayerMenuIconActionItem action={item.action} handleAction={handleAction} t={t} />
+ ) : (
+ <LayerMenuActionItem action={item.action} handleAction={handleAction} t={t} />
+ );
+ }
+
+ return <LayerMenuSubmenu compact={compact} handleAction={handleAction} item={item} t={t} />;
+};
+
+const LayerMenuSubmenu = ({
+ compact,
+ handleAction,
+ item,
+ t,
+}: {
+ compact: boolean;
+ handleAction: (id: LayerContextActionId) => void;
+ item: Extract<LayerContextMenuItem, { kind: 'submenu' }>;
+ t: (key: string, options: { defaultValue: string }) => string;
+}) => {
+ const meta = SUBMENU_META[item.id];
+ const label = t(meta.labelKey, { defaultValue: meta.defaultLabel });
+
+ return (
+ <Menu.Root positioning={SUBMENU_POSITIONING}>
+ <Menu.TriggerItem
+ aria-label={label}
+ flex={compact ? '1' : undefined}
+ justifyContent={compact ? 'center' : undefined}
+ >
+ {compact ? (
+ <Icon as={meta.icon} boxSize="4" color="fg.subtle" />
+ ) : (
+ <HStack gap="2" minW="0" w="full">
+ <Icon as={meta.icon} boxSize="3.5" color="fg.subtle" flexShrink={0} />
+ <Text flex="1" fontSize="xs">
+ {label}
+ </Text>
+ <Icon as={ChevronRightIcon} boxSize="3" color="fg.subtle" flexShrink={0} />
+ </HStack>
+ )}
+ </Menu.TriggerItem>
+ <Portal>
+ <Menu.Positioner>
+ <MenuContent minW="13rem" py="1">
+ {item.actions.map((action) => (
+ <LayerMenuActionItem key={action.id} action={action} handleAction={handleAction} t={t} />
+ ))}
+ </MenuContent>
+ </Menu.Positioner>
+ </Portal>
+ </Menu.Root>
+ );
+};
const LayerMenuIconActionItem = ({
action,
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/layers/layerContextMenuLayout.test.ts b/invokeai/frontend/webv2/src/workbench/widgets/layers/layerContextMenuLayout.test.ts
new file mode 100644
index 0000000000..0c37e0573b
--- /dev/null
+++ b/invokeai/frontend/webv2/src/workbench/widgets/layers/layerContextMenuLayout.test.ts
@@ -0,0 +1,110 @@
+import type { CanvasLayerContract, CanvasRasterLayerContractV2 } from '@workbench/types';
+
+import { describe, expect, it } from 'vitest';
+
+import { getLayerContextActions } from './layerContextActions';
+import { getLayerContextMenuLayout } from './layerContextMenuLayout';
+import {
+ createControlLayer,
+ createEmptyPaintLayer,
+ createInpaintMaskLayer,
+ createRegionalGuidanceLayer,
+} from './layerOps';
+
+const paintLayer = (id: string, patch: Partial<CanvasRasterLayerContractV2> = {}): CanvasLayerContract => ({
+ ...createEmptyPaintLayer(id, id),
+ ...patch,
+});
+
+const layoutFor = (layer: CanvasLayerContract, layers: readonly CanvasLayerContract[] = [layer]) => {
+ const actions = getLayerContextActions({ hasEngine: true, index: layers.indexOf(layer), layer, layers });
+ return getLayerContextMenuLayout(actions);
+};
+
+const summarize = (layer: CanvasLayerContract, layers: readonly CanvasLayerContract[] = [layer]) =>
+ layoutFor(layer, layers).map((section) => ({
+ id: section.id,
+ items: section.items.map((item) =>
+ item.kind === 'action' ? item.action.id : `${item.id}(${item.actions.map((action) => action.id).join(',')})`
+ ),
+ presentation: section.presentation,
+ }));
+
+describe('getLayerContextMenuLayout', () => {
+ it('organizes raster actions like the legacy menu and keeps delete last', () => {
+ const upper = paintLayer('upper');
+ const below = paintLayer('below');
+
+ expect(summarize(upper, [upper, below])).toEqual([
+ {
+ id: 'quick',
+ items: ['arrange(move-to-front,move-forward,move-backward,move-to-back)', 'duplicate'],
+ presentation: 'row',
+ },
+ { id: 'primary', items: ['transform', 'rename', 'fit-to-bbox'], presentation: 'list' },
+ {
+ id: 'operations',
+ items: [
+ 'merge-down',
+ 'boolean(intersect,cutout,cutaway,exclude)',
+ 'copy-to(copy-to-clipboard,copy-to-control,copy-to-inpaint-mask,copy-to-regional-guidance)',
+ 'convert-to(convert-to-control,convert-to-inpaint-mask,convert-to-regional-guidance)',
+ ],
+ presentation: 'list',
+ },
+ { id: 'output', items: ['crop-to-bbox', 'save-to-assets'], presentation: 'list' },
+ { id: 'state', items: ['toggle-visibility', 'toggle-lock'], presentation: 'list' },
+ { id: 'danger', items: ['delete'], presentation: 'list' },
+ ]);
+ });
+
+ it('groups inpaint modifiers and regional additions into type-specific submenus', () => {
+ const inpaint = createInpaintMaskLayer('Mask', 'mask');
+ const regional = createRegionalGuidanceLayer('Region', 0, 'region');
+
+ expect(summarize(inpaint)[1]).toEqual({
+ id: 'primary',
+ items: [
+ 'add-modifiers(inpaint-noise,inpaint-denoise-limit)',
+ 'transform',
+ 'rename',
+ 'fit-to-bbox',
+ 'extract-masked-area',
+ ],
+ presentation: 'list',
+ });
+ expect(summarize(regional)[1]).toEqual({
+ id: 'primary',
+ items: [
+ 'add-regional(regional-positive-prompt,regional-negative-prompt,regional-reference-image)',
+ 'transform',
+ 'rename',
+ 'fit-to-bbox',
+ 'regional-auto-negative',
+ ],
+ presentation: 'list',
+ });
+ });
+
+ it('places control filter and transparency actions before conversion operations', () => {
+ const control = {
+ ...createControlLayer('Control', 'control'),
+ source: { image: { height: 10, imageName: 'control-image', width: 10 }, type: 'image' as const },
+ };
+
+ expect(summarize(control)[1]).toEqual({
+ id: 'primary',
+ items: ['transform', 'rename', 'fit-to-bbox', 'filter', 'control-transparency-effect'],
+ presentation: 'list',
+ });
+ expect(summarize(control)[2]).toEqual({
+ id: 'operations',
+ items: [
+ 'merge-down',
+ 'copy-to(copy-to-clipboard,copy-to-raster,copy-to-inpaint-mask,copy-to-regional-guidance)',
+ 'convert-to(convert-to-raster)',
+ ],
+ presentation: 'list',
+ });
+ });
+});
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/layers/layerContextMenuLayout.ts b/invokeai/frontend/webv2/src/workbench/widgets/layers/layerContextMenuLayout.ts
new file mode 100644
index 0000000000..15d4367e0a
--- /dev/null
+++ b/invokeai/frontend/webv2/src/workbench/widgets/layers/layerContextMenuLayout.ts
@@ -0,0 +1,111 @@
+import type { LayerContextAction, LayerContextActionId } from './layerContextActions';
+
+export type LayerContextSubmenuId = 'arrange' | 'add-modifiers' | 'add-regional' | 'boolean' | 'copy-to' | 'convert-to';
+
+export type LayerContextMenuItem =
+ | { action: LayerContextAction; kind: 'action' }
+ | { actions: readonly LayerContextAction[]; id: LayerContextSubmenuId; kind: 'submenu' };
+
+export interface LayerContextMenuSection {
+ id: 'quick' | 'primary' | 'operations' | 'output' | 'state' | 'danger';
+ items: readonly LayerContextMenuItem[];
+ presentation: 'row' | 'list';
+}
+
+const ARRANGE_IDS: readonly LayerContextActionId[] = ['move-to-front', 'move-forward', 'move-backward', 'move-to-back'];
+const MODIFIER_IDS: readonly LayerContextActionId[] = ['inpaint-noise', 'inpaint-denoise-limit'];
+const REGIONAL_ADD_IDS: readonly LayerContextActionId[] = [
+ 'regional-positive-prompt',
+ 'regional-negative-prompt',
+ 'regional-reference-image',
+];
+const PRIMARY_IDS: readonly LayerContextActionId[] = [
+ 'transform',
+ 'rename',
+ 'fit-to-bbox',
+ 'filter',
+ 'control-transparency-effect',
+ 'regional-auto-negative',
+ 'extract-masked-area',
+];
+const BOOLEAN_IDS: readonly LayerContextActionId[] = ['intersect', 'cutout', 'cutaway', 'exclude'];
+const COPY_IDS: readonly LayerContextActionId[] = [
+ 'copy-to-clipboard',
+ 'copy-to-raster',
+ 'copy-to-control',
+ 'copy-to-inpaint-mask',
+ 'copy-to-regional-guidance',
+];
+const CONVERT_IDS: readonly LayerContextActionId[] = [
+ 'rasterize',
+ 'convert-to-control',
+ 'convert-to-raster',
+ 'convert-to-inpaint-mask',
+ 'convert-to-regional-guidance',
+];
+
+const orderedActions = (
+ byId: ReadonlyMap<LayerContextActionId, LayerContextAction>,
+ ids: readonly LayerContextActionId[]
+): LayerContextAction[] => ids.flatMap((id) => (byId.has(id) ? [byId.get(id)!] : []));
+
+const actionItem = (
+ byId: ReadonlyMap<LayerContextActionId, LayerContextAction>,
+ id: LayerContextActionId
+): LayerContextMenuItem[] => {
+ const action = byId.get(id);
+ return action ? [{ action, kind: 'action' }] : [];
+};
+
+const submenuItem = (
+ byId: ReadonlyMap<LayerContextActionId, LayerContextAction>,
+ id: LayerContextSubmenuId,
+ actionIds: readonly LayerContextActionId[]
+): LayerContextMenuItem[] => {
+ const actions = orderedActions(byId, actionIds);
+ return actions.length > 0 ? [{ actions, id, kind: 'submenu' }] : [];
+};
+
+/** Builds the legacy-style layer-menu hierarchy without coupling it to Chakra rendering. */
+export const getLayerContextMenuLayout = (actions: readonly LayerContextAction[]): LayerContextMenuSection[] => {
+ const byId = new Map(actions.map((action) => [action.id, action]));
+ const sections: LayerContextMenuSection[] = [
+ {
+ id: 'quick',
+ items: [...submenuItem(byId, 'arrange', ARRANGE_IDS), ...actionItem(byId, 'duplicate')],
+ presentation: 'row',
+ },
+ {
+ id: 'primary',
+ items: [
+ ...submenuItem(byId, 'add-modifiers', MODIFIER_IDS),
+ ...submenuItem(byId, 'add-regional', REGIONAL_ADD_IDS),
+ ...orderedActions(byId, PRIMARY_IDS).map((action) => ({ action, kind: 'action' }) as const),
+ ],
+ presentation: 'list',
+ },
+ {
+ id: 'operations',
+ items: [
+ ...actionItem(byId, 'merge-down'),
+ ...submenuItem(byId, 'boolean', BOOLEAN_IDS),
+ ...submenuItem(byId, 'copy-to', COPY_IDS),
+ ...submenuItem(byId, 'convert-to', CONVERT_IDS),
+ ],
+ presentation: 'list',
+ },
+ {
+ id: 'output',
+ items: [...actionItem(byId, 'crop-to-bbox'), ...actionItem(byId, 'save-to-assets')],
+ presentation: 'list',
+ },
+ {
+ id: 'state',
+ items: [...actionItem(byId, 'toggle-visibility'), ...actionItem(byId, 'toggle-lock')],
+ presentation: 'list',
+ },
+ { id: 'danger', items: actionItem(byId, 'delete'), presentation: 'list' },
+ ];
+
+ return sections.filter((section) => section.items.length > 0);
+};