Skip to content

Commit 4dc1945

Browse files
psychedelicioushipsterusername
authored andcommitted
fix(ui): delete hotkey operating on image and layer at same time
1 parent 384abab commit 4dc1945

File tree

4 files changed

+42
-13
lines changed

4 files changed

+42
-13
lines changed

invokeai/frontend/web/src/features/controlLayers/components/CanvasRightPanel.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ import { CanvasLayersPanelContent } from 'features/controlLayers/components/Canv
77
import { selectEntityCountActive } from 'features/controlLayers/store/selectors';
88
import GalleryPanelContent from 'features/gallery/components/GalleryPanelContent';
99
import { useImageViewer } from 'features/gallery/components/ImageViewer/useImageViewer';
10-
import { atom } from 'nanostores';
10+
import { atom, computed } from 'nanostores';
1111
import { memo, useCallback, useMemo, useRef } from 'react';
1212
import { useHotkeys } from 'react-hotkeys-hook';
1313
import { useTranslation } from 'react-i18next';
1414

1515
const $tabIndex = atom(0);
16+
export const $canvasRightPanelTab = computed($tabIndex, (index) => (index === 0 ? 'layers' : 'gallery'));
1617
export const setRightPanelTabToLayers = () => $tabIndex.set(0);
1718
export const setRightPanelTabToGallery = () => $tabIndex.set(1);
1819

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
1+
import { useStore } from '@nanostores/react';
12
import { useAppDispatch, useAppSelector } from 'app/store/storeHooks';
23
import { useAssertSingleton } from 'common/hooks/useAssertSingleton';
4+
import { $canvasRightPanelTab } from 'features/controlLayers/components/CanvasRightPanel';
35
import { useCanvasIsBusy } from 'features/controlLayers/hooks/useCanvasIsBusy';
46
import { entityDeleted } from 'features/controlLayers/store/canvasSlice';
5-
import { selectIsStaging } from 'features/controlLayers/store/canvasStagingAreaSlice';
67
import { selectSelectedEntityIdentifier } from 'features/controlLayers/store/selectors';
8+
import { selectActiveTab } from 'features/ui/store/uiSelectors';
79
import { useCallback, useMemo } from 'react';
810
import { useHotkeys } from 'react-hotkeys-hook';
911

1012
export function useCanvasDeleteLayerHotkey() {
1113
useAssertSingleton(useCanvasDeleteLayerHotkey.name);
1214
const dispatch = useAppDispatch();
1315
const selectedEntityIdentifier = useAppSelector(selectSelectedEntityIdentifier);
14-
const isStaging = useAppSelector(selectIsStaging);
1516
const isBusy = useCanvasIsBusy();
17+
const canvasRightPanelTab = useStore($canvasRightPanelTab);
18+
const appTab = useAppSelector(selectActiveTab);
1619

1720
const deleteSelectedLayer = useCallback(() => {
1821
if (selectedEntityIdentifier === null) {
@@ -22,13 +25,12 @@ export function useCanvasDeleteLayerHotkey() {
2225
}, [dispatch, selectedEntityIdentifier]);
2326

2427
const isDeleteEnabled = useMemo(
25-
() => selectedEntityIdentifier !== null && !isStaging,
26-
[selectedEntityIdentifier, isStaging]
28+
() => selectedEntityIdentifier !== null && !isBusy && canvasRightPanelTab === 'layers' && appTab === 'canvas',
29+
[selectedEntityIdentifier, isBusy, canvasRightPanelTab, appTab]
2730
);
2831

29-
useHotkeys(['delete', 'backspace'], deleteSelectedLayer, { enabled: isDeleteEnabled && !isBusy }, [
32+
useHotkeys(['delete', 'backspace'], deleteSelectedLayer, { enabled: isDeleteEnabled }, [
3033
isDeleteEnabled,
31-
isBusy,
3234
deleteSelectedLayer,
3335
]);
3436
}

invokeai/frontend/web/src/features/gallery/components/ImageViewer/CurrentImageButtons.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ const CurrentImageButtons = () => {
3636
const isConnected = useStore($isConnected);
3737
const lastSelectedImage = useAppSelector(selectLastSelectedImage);
3838
const progressImage = useStore($progressImage);
39-
const selection = useAppSelector((s) => s.gallery.selection);
4039
const shouldDisableToolbarButtons = useMemo(() => {
4140
return Boolean(progressImage) || !lastSelectedImage;
4241
}, [lastSelectedImage, progressImage]);
@@ -73,8 +72,8 @@ const CurrentImageButtons = () => {
7372
if (!imageDTO) {
7473
return;
7574
}
76-
dispatch(imagesToDeleteSelected(selection));
77-
}, [dispatch, imageDTO, selection]);
75+
dispatch(imagesToDeleteSelected([imageDTO]));
76+
}, [dispatch, imageDTO]);
7877

7978
useHotkeys('w', handleLoadWorkflow, { enabled: isImageViewerActive }, [lastSelectedImage, isImageViewerActive]);
8079
useHotkeys('a', recallAll, { enabled: isImageViewerActive }, [recallAll, isImageViewerActive]);
@@ -89,8 +88,6 @@ const CurrentImageButtons = () => {
8988
[isUpscalingEnabled, imageDTO, shouldDisableToolbarButtons, isConnected, isImageViewerActive]
9089
);
9190

92-
useHotkeys(['delete', 'backspace'], handleDelete, { enabled: isImageViewerActive }, [imageDTO, isImageViewerActive]);
93-
9491
return (
9592
<>
9693
<ButtonGroup isDisabled={shouldDisableToolbarButtons}>

invokeai/frontend/web/src/features/gallery/hooks/useGalleryHotkeys.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import { useStore } from '@nanostores/react';
2-
import { useAppSelector } from 'app/store/storeHooks';
2+
import { useAppDispatch, useAppSelector } from 'app/store/storeHooks';
33
import { $activeScopes } from 'common/hooks/interactionScopes';
44
import { useAssertSingleton } from 'common/hooks/useAssertSingleton';
5+
import { $canvasRightPanelTab } from 'features/controlLayers/components/CanvasRightPanel';
6+
import { imagesToDeleteSelected } from 'features/deleteImageModal/store/slice';
57
import { useGalleryNavigation } from 'features/gallery/hooks/useGalleryNavigation';
68
import { useGalleryPagination } from 'features/gallery/hooks/useGalleryPagination';
79
import { selectListImagesQueryArgs } from 'features/gallery/store/gallerySelectors';
10+
import { selectActiveTab } from 'features/ui/store/uiSelectors';
811
import { $isRightPanelOpen } from 'features/ui/store/uiSlice';
912
import { computed } from 'nanostores';
13+
import { useCallback, useMemo } from 'react';
1014
import { useHotkeys } from 'react-hotkeys-hook';
1115
import { useListImagesQuery } from 'services/api/endpoints/images';
1216

@@ -26,10 +30,23 @@ const $upDownHotkeysEnabled = computed([$activeScopes, $isRightPanelOpen], (acti
2630
export const useGalleryHotkeys = () => {
2731
useAssertSingleton('useGalleryHotkeys');
2832
const { goNext, goPrev, isNextEnabled, isPrevEnabled } = useGalleryPagination();
33+
const dispatch = useAppDispatch();
34+
const selection = useAppSelector((s) => s.gallery.selection);
2935
const queryArgs = useAppSelector(selectListImagesQueryArgs);
3036
const queryResult = useListImagesQuery(queryArgs);
3137
const leftRightHotkeysEnabled = useStore($leftRightHotkeysEnabled);
3238
const upDownHotkeysEnabled = useStore($upDownHotkeysEnabled);
39+
const canvasRightPanelTab = useStore($canvasRightPanelTab);
40+
const appTab = useAppSelector(selectActiveTab);
41+
42+
// When we are on the canvas tab, we need to disable the delete hotkey when the user is focused on the layers tab in
43+
// the right hand panel, because the same hotkey is used to delete layers.
44+
const isDeleteEnabledByTab = useMemo(() => {
45+
if (appTab !== 'canvas') {
46+
return true;
47+
}
48+
return canvasRightPanelTab === 'gallery';
49+
}, [appTab, canvasRightPanelTab]);
3350

3451
const {
3552
handleLeftImage,
@@ -95,4 +112,16 @@ export const useGalleryHotkeys = () => {
95112
{ preventDefault: true, enabled: upDownHotkeysEnabled },
96113
[isOnLastRow, goNext, isNextEnabled, queryResult.isFetching, handleDownImage, upDownHotkeysEnabled]
97114
);
115+
116+
const handleDelete = useCallback(() => {
117+
if (!selection.length) {
118+
return;
119+
}
120+
dispatch(imagesToDeleteSelected(selection));
121+
}, [dispatch, selection]);
122+
123+
useHotkeys(['delete', 'backspace'], handleDelete, { enabled: leftRightHotkeysEnabled && isDeleteEnabledByTab }, [
124+
leftRightHotkeysEnabled,
125+
isDeleteEnabledByTab,
126+
]);
98127
};

0 commit comments

Comments
 (0)