Skip to content

Commit 2c1f2b2

Browse files
tidy(ui): move star hotkey into own hook & use reactive state for focus
1 parent 8418e34 commit 2c1f2b2

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

invokeai/frontend/web/src/features/gallery/components/NewGallery.tsx

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ import { Box, Flex, forwardRef, Grid, GridItem, Spinner, Text } from '@invoke-ai
22
import { createSelector } from '@reduxjs/toolkit';
33
import { logger } from 'app/logging/logger';
44
import { useAppSelector, useAppStore } from 'app/store/storeHooks';
5-
import { getFocusedRegion } from 'common/hooks/focus';
5+
import { getFocusedRegion, useIsRegionFocused } from 'common/hooks/focus';
66
import { useRangeBasedImageFetching } from 'features/gallery/hooks/useRangeBasedImageFetching';
77
import type { selectGetImageNamesQueryArgs } from 'features/gallery/store/gallerySelectors';
88
import {
99
selectGalleryImageMinimumWidth,
1010
selectImageToCompare,
1111
selectLastSelectedImage,
12+
selectSelectionCount,
1213
} from 'features/gallery/store/gallerySelectors';
1314
import { imageToCompareChanged, selectionChanged } from 'features/gallery/store/gallerySlice';
1415
import { useRegisteredHotkeys } from 'features/system/components/HotkeysModal/useHotkeyData';
@@ -450,23 +451,10 @@ const useScrollableGallery = (rootRef: RefObject<HTMLDivElement>) => {
450451
return scrollerRef;
451452
};
452453

453-
export const NewGallery = memo(() => {
454-
const virtuosoRef = useRef<VirtuosoGridHandle>(null);
455-
const rangeRef = useRef<ListRange>({ startIndex: 0, endIndex: 0 });
456-
const rootRef = useRef<HTMLDivElement>(null);
457-
458-
// Get the ordered list of image names - this is our primary data source for virtualization
459-
const { queryArgs, imageNames, isLoading } = useGalleryImageNames();
460-
461-
// Use range-based fetching for bulk loading image DTOs into cache based on the visible range
462-
const { onRangeChanged } = useRangeBasedImageFetching({
463-
imageNames,
464-
enabled: !isLoading,
465-
});
466-
454+
const useStarImageHotkey = () => {
467455
const lastSelectedImage = useAppSelector(selectLastSelectedImage);
468-
const selectionCount = useAppSelector((state) => state.gallery.selection.length);
469-
const isGalleryFocused = getFocusedRegion() === 'gallery';
456+
const selectionCount = useAppSelector(selectSelectionCount);
457+
const isGalleryFocused = useIsRegionFocused('gallery');
470458
const imageDTO = useImageDTO(lastSelectedImage);
471459
const [starImages] = useStarImagesMutation();
472460
const [unstarImages] = useUnstarImagesMutation();
@@ -475,12 +463,15 @@ export const NewGallery = memo(() => {
475463
if (!imageDTO) {
476464
return;
477465
}
466+
if (!isGalleryFocused) {
467+
return;
468+
}
478469
if (imageDTO.starred) {
479470
unstarImages({ image_names: [imageDTO.image_name] });
480471
} else {
481472
starImages({ image_names: [imageDTO.image_name] });
482473
}
483-
}, [imageDTO, starImages, unstarImages]);
474+
}, [imageDTO, isGalleryFocused, starImages, unstarImages]);
484475

485476
useRegisteredHotkeys({
486477
id: 'starImage',
@@ -489,7 +480,23 @@ export const NewGallery = memo(() => {
489480
options: { enabled: !!imageDTO && selectionCount === 1 && isGalleryFocused },
490481
dependencies: [imageDTO, selectionCount, isGalleryFocused, handleStarHotkey],
491482
});
483+
};
484+
485+
export const NewGallery = memo(() => {
486+
const virtuosoRef = useRef<VirtuosoGridHandle>(null);
487+
const rangeRef = useRef<ListRange>({ startIndex: 0, endIndex: 0 });
488+
const rootRef = useRef<HTMLDivElement>(null);
489+
490+
// Get the ordered list of image names - this is our primary data source for virtualization
491+
const { queryArgs, imageNames, isLoading } = useGalleryImageNames();
492+
493+
// Use range-based fetching for bulk loading image DTOs into cache based on the visible range
494+
const { onRangeChanged } = useRangeBasedImageFetching({
495+
imageNames,
496+
enabled: !isLoading,
497+
});
492498

499+
useStarImageHotkey();
493500
useKeepSelectedImageInView(imageNames, virtuosoRef, rootRef, rangeRef);
494501
useKeyboardNavigation(imageNames, virtuosoRef, rootRef);
495502
const scrollerRef = useScrollableGallery(rootRef);

0 commit comments

Comments
 (0)