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.

fix(layers): delete layers with Delete/Backspace from the layers panel

Josh Corbettcc891870228a
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/layers/LayersTree.browser.test.tsx b/invokeai/frontend/webv2/src/workbench/widgets/layers/LayersTree.browser.test.tsx
index 1307576a05..c2c9ff5270 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/layers/LayersTree.browser.test.tsx
+++ b/invokeai/frontend/webv2/src/workbench/widgets/layers/LayersTree.browser.test.tsx
@@ -488,6 +488,24 @@ describe('LayersTree selection, surfaces and structure', () => {
expect(output('selected-layers')).toBe('first,second,third');
});
+ it('deletes the focused row with Delete and the selection with Backspace, keeping focus in the tree', async () => {
+ await renderTree(trio());
+ treeitem('Second').focus();
+ await act(() => userEvent.keyboard('{Delete}'));
+ expect(output('layer-order')).toBe('first,third');
+ expect(document.activeElement).toBe(treeitem('Third'));
+
+ // The last row falls back to the row above it.
+ treeitem('Third').focus();
+ await act(() => userEvent.keyboard('{Delete}'));
+ expect(output('layer-order')).toBe('first');
+ expect(document.activeElement).toBe(treeitem('First'));
+
+ await act(() => userEvent.click(treeitem('First')));
+ await act(() => userEvent.keyboard('{Backspace}'));
+ expect(output('layer-order')).toBe('');
+ });
+
it('keeps the visibility dot isolated from row selection', async () => {
await renderTree(trio());
const dot = host!.querySelector<HTMLButtonElement>(
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/layers/LayersTree.tsx b/invokeai/frontend/webv2/src/workbench/widgets/layers/LayersTree.tsx
index f2963e9f15..9df1597e21 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/layers/LayersTree.tsx
+++ b/invokeai/frontend/webv2/src/workbench/widgets/layers/LayersTree.tsx
@@ -372,6 +372,39 @@ export const LayersTree = ({
[runStructural, t]
);
+ // Deletes the focused row (or the selection it belongs to) and keeps keyboard
+ // focus in the tree: the nearest surviving row below, else above.
+ const removeRows = useCallback(
+ (key: string) => {
+ const { document: currentDocument, engine: currentEngine, panel: current, visibleRowIds: rows } = latest.current;
+ const vm = lookupDocumentNodeState(currentDocument, key);
+ if (!vm) {
+ return;
+ }
+ // The menu's rule: the selection when the row is part of it, else the row alone.
+ const ids = current.selectedIds.includes(key) ? current.selectedIds : [key];
+ // A stray key on a locked row is ignored, as the menu disables its Delete.
+ if (currentEngine?.document.model()?.refusalFor({ ids, type: 'remove' })) {
+ return;
+ }
+ const removed = new Set(ids);
+ const survives = (id: string): boolean => {
+ const state = lookupDocumentNodeState(currentDocument, id);
+ return !!state && !removed.has(id) && !state.parentIds.some((ancestor) => removed.has(ancestor));
+ };
+ const index = rows.indexOf(key);
+ const next = rows.slice(index + 1).find(survives) ?? rows.slice(0, Math.max(0, index)).reverse().find(survives);
+ const outcome = runStructural(
+ t(ids.length > 1 ? 'widgets.layers.actions.deleteSelected' : 'widgets.layers.actions.delete'),
+ { ids, type: 'remove' }
+ );
+ if (outcome.status === 'committed' && next) {
+ focusItem(next);
+ }
+ },
+ [focusItem, runStructural, t]
+ );
+
const commands = useMemo<LayerRowCommands>(
() => ({
endRename: () => setRenamingId(null),
@@ -428,6 +461,13 @@ export const LayersTree = ({
}
return;
}
+ if (!isHeaderKey(key) && (event.key === 'Delete' || event.key === 'Backspace')) {
+ event.preventDefault();
+ if (!latest.current.editingLocked) {
+ removeRows(key);
+ }
+ return;
+ }
if (isHeaderKey(key)) {
if (event.key === 'Enter' || event.key === ' ') {
event.preventDefault();
@@ -623,7 +663,7 @@ export const LayersTree = ({
}
},
}),
- [focusItem, movingIds, removeChildRow, runStructural, t]
+ [focusItem, movingIds, removeChildRow, removeRows, runStructural, t]
);
const closeSurface = useCallback(() => {