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.

Feat(UI): Reintroduce paged gallery view as option (#8772)

DustyShoe9566f9a23f9e
diff --git a/invokeai/frontend/web/public/locales/en.json b/invokeai/frontend/web/public/locales/en.json
index 532204e3e3..b1ab43456e 100644
--- a/invokeai/frontend/web/public/locales/en.json
+++ b/invokeai/frontend/web/public/locales/en.json
@@ -389,6 +389,7 @@
"oldestFirst": "Oldest First",
"sortDirection": "Sort Direction",
"showStarredImagesFirst": "Show Starred Images First",
+ "usePagedGalleryView": "Use Paged Gallery View",
"noImageSelected": "No Image Selected",
"noImagesInGallery": "No Images to Display",
"starImage": "Star",
diff --git a/invokeai/frontend/web/src/features/gallery/components/GalleryImageGrid.tsx b/invokeai/frontend/web/src/features/gallery/components/GalleryImageGrid.tsx
index f2c50f786e..3fb610498d 100644
--- a/invokeai/frontend/web/src/features/gallery/components/GalleryImageGrid.tsx
+++ b/invokeai/frontend/web/src/features/gallery/components/GalleryImageGrid.tsx
@@ -273,6 +273,10 @@ const useKeepSelectedImageInView = (
return;
}
+ if (!imageNames.includes(targetImageName)) {
+ return;
+ }
+
setTimeout(() => {
scrollIntoView(targetImageName, imageNames, rootEl, virtuosoGridHandle, range);
}, 0);
@@ -310,75 +314,89 @@ const useStarImageHotkey = () => {
});
};
-export const GalleryImageGrid = memo(() => {
- const virtuosoRef = useRef<VirtuosoGridHandle>(null);
- const rangeRef = useRef<ListRange>({ startIndex: 0, endIndex: 0 });
- const rootRef = useRef<HTMLDivElement>(null);
-
- // Get the ordered list of image names - this is our primary data source for virtualization
- const { queryArgs, imageNames, isLoading } = useGalleryImageNames();
+type GalleryImageGridContentProps = {
+ imageNames: string[];
+ isLoading: boolean;
+ queryArgs: ListImageNamesQueryArgs;
+ rootRef?: React.RefObject<HTMLDivElement>;
+};
- // Use range-based fetching for bulk loading image DTOs into cache based on the visible range
- const { onRangeChanged } = useRangeBasedImageFetching({
- imageNames,
- enabled: !isLoading,
- });
+export const GalleryImageGridContent = memo(
+ ({ imageNames, isLoading, queryArgs, rootRef: rootRefProp }: GalleryImageGridContentProps) => {
+ const virtuosoRef = useRef<VirtuosoGridHandle>(null);
+ const rangeRef = useRef<ListRange>({ startIndex: 0, endIndex: 0 });
+ const internalRootRef = useRef<HTMLDivElement>(null);
+ const rootRef = rootRefProp ?? internalRootRef;
+
+ // Use range-based fetching for bulk loading image DTOs into cache based on the visible range
+ const { onRangeChanged } = useRangeBasedImageFetching({
+ imageNames,
+ enabled: !isLoading,
+ });
+
+ useStarImageHotkey();
+ useKeepSelectedImageInView(imageNames, virtuosoRef, rootRef, rangeRef);
+ useKeyboardNavigation(imageNames, virtuosoRef, rootRef);
+ const scrollerRef = useScrollableGallery(rootRef);
+
+ /*
+ * We have to keep track of the visible range for keep-selected-image-in-view functionality and push the range to
+ * the range-based image fetching hook.
+ */
+ const handleRangeChanged = useCallback(
+ (range: ListRange) => {
+ rangeRef.current = range;
+ onRangeChanged(range);
+ },
+ [onRangeChanged]
+ );
- useStarImageHotkey();
- useKeepSelectedImageInView(imageNames, virtuosoRef, rootRef, rangeRef);
- useKeyboardNavigation(imageNames, virtuosoRef, rootRef);
- const scrollerRef = useScrollableGallery(rootRef);
+ const context = useMemo<GridContext>(() => ({ imageNames, queryArgs }), [imageNames, queryArgs]);
- /*
- * We have to keep track of the visible range for keep-selected-image-in-view functionality and push the range to
- * the range-based image fetching hook.
- */
- const handleRangeChanged = useCallback(
- (range: ListRange) => {
- rangeRef.current = range;
- onRangeChanged(range);
- },
- [onRangeChanged]
- );
+ if (isLoading) {
+ return (
+ <Flex w="full" h="full" alignItems="center" justifyContent="center" gap={4}>
+ <Spinner size="lg" opacity={0.3} />
+ <Text color="base.300">Loading gallery...</Text>
+ </Flex>
+ );
+ }
- const context = useMemo<GridContext>(() => ({ imageNames, queryArgs }), [imageNames, queryArgs]);
+ if (imageNames.length === 0) {
+ return (
+ <Flex w="full" h="full" alignItems="center" justifyContent="center">
+ <Text color="base.300">No images found</Text>
+ </Flex>
+ );
+ }
- if (isLoading) {
return (
- <Flex w="full" h="full" alignItems="center" justifyContent="center" gap={4}>
- <Spinner size="lg" opacity={0.3} />
- <Text color="base.300">Loading gallery...</Text>
- </Flex>
+ // This wrapper component is necessary to initialize the overlay scrollbars!
+ <Box data-overlayscrollbars-initialize="" ref={rootRef} position="relative" w="full" h="full">
+ <VirtuosoGrid<string, GridContext>
+ ref={virtuosoRef}
+ context={context}
+ data={imageNames}
+ increaseViewportBy={4096}
+ itemContent={itemContent}
+ computeItemKey={computeItemKey}
+ components={components}
+ style={style}
+ scrollerRef={scrollerRef}
+ scrollSeekConfiguration={scrollSeekConfiguration}
+ rangeChanged={handleRangeChanged}
+ />
+ <GallerySelectionCountTag imageNames={imageNames} />
+ </Box>
);
}
+);
- if (imageNames.length === 0) {
- return (
- <Flex w="full" h="full" alignItems="center" justifyContent="center">
- <Text color="base.300">No images found</Text>
- </Flex>
- );
- }
+GalleryImageGridContent.displayName = 'GalleryImageGridContent';
- return (
- // This wrapper component is necessary to initialize the overlay scrollbars!
- <Box data-overlayscrollbars-initialize="" ref={rootRef} position="relative" w="full" h="full">
- <VirtuosoGrid<string, GridContext>
- ref={virtuosoRef}
- context={context}
- data={imageNames}
- increaseViewportBy={4096}
- itemContent={itemContent}
- computeItemKey={computeItemKey}
- components={components}
- style={style}
- scrollerRef={scrollerRef}
- scrollSeekConfiguration={scrollSeekConfiguration}
- rangeChanged={handleRangeChanged}
- />
- <GallerySelectionCountTag />
- </Box>
- );
+export const GalleryImageGrid = memo(() => {
+ const { queryArgs, imageNames, isLoading } = useGalleryImageNames();
+ return <GalleryImageGridContent imageNames={imageNames} isLoading={isLoading} queryArgs={queryArgs} />;
});
GalleryImageGrid.displayName = 'GalleryImageGrid';
diff --git a/invokeai/frontend/web/src/features/gallery/components/GalleryImageGridPaged.tsx b/invokeai/frontend/web/src/features/gallery/components/GalleryImageGridPaged.tsx
new file mode 100644
index 0000000000..af6101d85a
--- /dev/null
+++ b/invokeai/frontend/web/src/features/gallery/components/GalleryImageGridPaged.tsx
@@ -0,0 +1,193 @@
+import { Flex } from '@invoke-ai/ui-library';
+import { useAppSelector } from 'app/store/storeHooks';
+import { GalleryImageGridContent } from 'features/gallery/components/GalleryImageGrid';
+import { GalleryPaginationPaged } from 'features/gallery/components/ImageGrid/GalleryPaginationPaged';
+import { useGalleryImageNames } from 'features/gallery/components/use-gallery-image-names';
+import { selectGalleryImageMinimumWidth, selectLastSelectedItem } from 'features/gallery/store/gallerySelectors';
+import { memo, useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';
+
+import { getItemsPerPage } from './getItemsPerPage';
+
+const FALLBACK_PAGE_SIZE = 200;
+
+export const GalleryImageGridPaged = memo(() => {
+ const { queryArgs, imageNames, isLoading } = useGalleryImageNames();
+ const lastSelectedItem = useAppSelector(selectLastSelectedItem);
+ const galleryImageMinimumWidth = useAppSelector(selectGalleryImageMinimumWidth);
+ const [pageIndex, setPageIndex] = useState(0);
+ const [pageSize, setPageSize] = useState(FALLBACK_PAGE_SIZE);
+ const gridRootRef = useRef<HTMLDivElement>(null);
+ const lastSelectedRef = useRef<string | null>(null);
+ const lastPageSizeRef = useRef<number | null>(null);
+ const lastImageNamesRef = useRef<string[] | null>(null);
+
+ const pageCount = Math.ceil(imageNames.length / pageSize);
+ const pageImageNames = useMemo(() => {
+ const start = pageIndex * pageSize;
+ return imageNames.slice(start, start + pageSize);
+ }, [imageNames, pageIndex, pageSize]);
+
+ useEffect(() => {
+ if (pageIndex >= pageCount && pageCount > 0) {
+ setPageIndex(pageCount - 1);
+ }
+ }, [pageCount, pageIndex]);
+
+ useEffect(() => {
+ if (!lastSelectedItem) {
+ lastSelectedRef.current = null;
+ lastPageSizeRef.current = null;
+ lastImageNamesRef.current = imageNames;
+ return;
+ }
+
+ const shouldRecompute =
+ lastSelectedRef.current !== lastSelectedItem ||
+ lastPageSizeRef.current !== pageSize ||
+ lastImageNamesRef.current !== imageNames;
+
+ if (!shouldRecompute) {
+ return;
+ }
+
+ lastSelectedRef.current = lastSelectedItem;
+ lastPageSizeRef.current = pageSize;
+ lastImageNamesRef.current = imageNames;
+
+ const selectedIndex = imageNames.indexOf(lastSelectedItem);
+ if (selectedIndex === -1) {
+ return;
+ }
+ const nextPageIndex = Math.floor(selectedIndex / pageSize);
+ if (nextPageIndex !== pageIndex) {
+ setPageIndex(nextPageIndex);
+ }
+ }, [imageNames, lastSelectedItem, pageIndex, pageSize]);
+
+ const recalculatePageSize = useCallback(() => {
+ const rootEl = gridRootRef.current;
+ if (!rootEl) {
+ return;
+ }
+ const nextPageSize = getItemsPerPage(rootEl);
+ if (nextPageSize > 0 && nextPageSize !== pageSize) {
+ setPageSize(nextPageSize);
+ }
+ }, [pageSize]);
+
+ useLayoutEffect(() => {
+ if (isLoading) {
+ return;
+ }
+ let frame = 0;
+ let attempts = 0;
+ const tick = () => {
+ const rootEl = gridRootRef.current;
+ if (!rootEl) {
+ frame = requestAnimationFrame(tick);
+ return;
+ }
+ const nextPageSize = getItemsPerPage(rootEl);
+ if (nextPageSize > 0) {
+ if (nextPageSize !== pageSize) {
+ setPageSize(nextPageSize);
+ }
+ return;
+ }
+ if (attempts < 10) {
+ attempts += 1;
+ frame = requestAnimationFrame(tick);
+ }
+ };
+ frame = requestAnimationFrame(tick);
+ return () => cancelAnimationFrame(frame);
+ }, [galleryImageMinimumWidth, imageNames.length, isLoading, pageIndex, pageSize]);
+
+ useEffect(() => {
+ if (isLoading) {
+ return;
+ }
+ const timeout = setTimeout(() => {
+ recalculatePageSize();
+ requestAnimationFrame(recalculatePageSize);
+ }, 350);
+ return () => clearTimeout(timeout);
+ }, [galleryImageMinimumWidth, isLoading, recalculatePageSize]);
+
+ useEffect(() => {
+ const rootEl = gridRootRef.current;
+ if (!rootEl || typeof ResizeObserver === 'undefined') {
+ return;
+ }
+ const observer = new ResizeObserver(() => {
+ recalculatePageSize();
+ });
+ observer.observe(rootEl);
+ return () => observer.disconnect();
+ }, [galleryImageMinimumWidth, recalculatePageSize]);
+
+ useEffect(() => {
+ const rootEl = gridRootRef.current;
+ if (!rootEl || typeof MutationObserver === 'undefined') {
+ return;
+ }
+ const observer = new MutationObserver(() => {
+ recalculatePageSize();
+ });
+ observer.observe(rootEl, { childList: true, subtree: true });
+ return () => observer.disconnect();
+ }, [recalculatePageSize]);
+
+ const handleTabChange = useCallback((index: number) => {
+ setPageIndex(index);
+ }, []);
+
+ const handlePreviousPage = useCallback(() => {
+ setPageIndex((prev) => Math.max(0, prev - 1));
+ }, []);
+
+ const handleNextPage = useCallback(() => {
+ setPageIndex((prev) => Math.min(pageCount - 1, prev + 1));
+ }, [pageCount]);
+
+ const handlePageInputChange = useCallback(
+ (valueAsString: string, valueAsNumber: number) => {
+ if (!valueAsString) {
+ return;
+ }
+ if (Number.isNaN(valueAsNumber)) {
+ return;
+ }
+ const nextIndex = Math.min(Math.max(valueAsNumber, 1), pageCount) - 1;
+ setPageIndex(nextIndex);
+ },
+ [pageCount]
+ );
+
+ if (isLoading || imageNames.length === 0) {
+ return <GalleryImageGridContent imageNames={imageNames} isLoading={isLoading} queryArgs={queryArgs} />;
+ }
+
+ return (
+ <Flex w="full" h="full" flexDir="column" gap={2}>
+ <GalleryPaginationPaged
+ pageIndex={pageIndex}
+ pageCount={pageCount}
+ onPrev={handlePreviousPage}
+ onNext={handleNextPage}
+ onGoToPage={handleTabChange}
+ onPageInputChange={handlePageInputChange}
+ />
+ <Flex w="full" h="full">
+ <GalleryImageGridContent
+ imageNames={pageImageNames}
+ isLoading={false}
+ queryArgs={queryArgs}
+ rootRef={gridRootRef}
+ />
+ </Flex>
+ </Flex>
+ );
+});
+
+GalleryImageGridPaged.displayName = 'GalleryImageGridPaged';
diff --git a/invokeai/frontend/web/src/features/gallery/components/GalleryPanel.tsx b/invokeai/frontend/web/src/features/gallery/components/GalleryPanel.tsx
index 874561e204..7c57aa0849 100644
--- a/invokeai/frontend/web/src/features/gallery/components/GalleryPanel.tsx
+++ b/invokeai/frontend/web/src/features/gallery/components/GalleryPanel.tsx
@@ -8,6 +8,7 @@ import { selectSelectedBoardId } from 'features/gallery/store/gallerySelectors';
import { galleryViewChanged, selectGallerySlice } from 'features/gallery/store/gallerySlice';
import { useAutoLayoutContext } from 'features/ui/layouts/auto-layout-context';
import { useGalleryPanel } from 'features/ui/layouts/use-gallery-panel';
+import { selectShouldUsePagedGalleryView } from 'features/ui/store/uiSelectors';
import type { CSSProperties } from 'react';
import { memo, useCallback } from 'react';
import { useTranslation } from 'react-i18next';
@@ -15,6 +16,7 @@ import { PiCaretDownBold, PiCaretUpBold, PiMagnifyingGlassBold } from 'react-ico
import { useBoardName } from 'services/api/hooks/useBoardName';
import { GalleryImageGrid } from './GalleryImageGrid';
+import { GalleryImageGridPaged } from './GalleryImageGridPaged';
import { GallerySettingsPopover } from './GallerySettingsPopover/GallerySettingsPopover';
import { GalleryUploadButton } from './GalleryUploadButton';
import { GallerySearch } from './ImageGrid/GallerySearch';
@@ -32,6 +34,7 @@ export const GalleryPanel = memo(() => {
const isCollapsed = useStore(galleryPanel.$isCollapsed);
const galleryView = useAppSelector(selectGalleryView);
const initialSearchTerm = useAppSelector(selectSearchTerm);
+ const shouldUsePagedGalleryView = useAppSelector(selectShouldUsePagedGalleryView);
const searchDisclosure = useDisclosure(!!initialSearchTerm);
const [searchTerm, onChangeSearchTerm, onResetSearchTerm] = useGallerySearchTerm();
const handleClickImages = useCallback(() => {
@@ -110,7 +113,7 @@ export const GalleryPanel = memo(() => {
</Collapse>
<Divider pt={2} />
<Flex w="full" h="full" pt={2}>
- <GalleryImageGrid />
+ {shouldUsePagedGalleryView ? <GalleryImageGridPaged /> : <GalleryImageGrid />}
</Flex>
</Flex>
);
diff --git a/invokeai/frontend/web/src/features/gallery/components/GallerySettingsPopover/GallerySettingsPopover.tsx b/invokeai/frontend/web/src/features/gallery/components/GallerySettingsPopover/GallerySettingsPopover.tsx
index f7c8591cd2..d03a07b191 100644
--- a/invokeai/frontend/web/src/features/gallery/components/GallerySettingsPopover/GallerySettingsPopover.tsx
+++ b/invokeai/frontend/web/src/features/gallery/components/GallerySettingsPopover/GallerySettingsPopover.tsx
@@ -15,6 +15,7 @@ import AutoSwitchCheckbox from 'features/gallery/components/GallerySettingsPopov
import ImageMinimumWidthSlider from 'features/gallery/components/GallerySettingsPopover/ImageMinimumWidthSlider';
import ShowStarredFirstCheckbox from 'features/gallery/components/GallerySettingsPopover/ShowStarredFirstCheckbox';
import SortDirectionCombobox from 'features/gallery/components/GallerySettingsPopover/SortDirectionCombobox';
+import UsePagedGalleryViewCheckbox from 'features/gallery/components/GallerySettingsPopover/UsePagedGalleryViewCheckbox';
import { memo } from 'react';
import { useTranslation } from 'react-i18next';
import { PiGearSixFill } from 'react-icons/pi';
@@ -46,6 +47,7 @@ export const GallerySettingsPopover = memo(() => {
<Divider />
<ImageMinimumWidthSlider />
+ <UsePagedGalleryViewCheckbox />
<AutoSwitchCheckbox />
<AlwaysShowImageSizeCheckbox />
diff --git a/invokeai/frontend/web/src/features/gallery/components/GallerySettingsPopover/UsePagedGalleryViewCheckbox.tsx b/invokeai/frontend/web/src/features/gallery/components/GallerySettingsPopover/UsePagedGalleryViewCheckbox.tsx
new file mode 100644
index 0000000000..ea3360c755
--- /dev/null
+++ b/invokeai/frontend/web/src/features/gallery/components/GallerySettingsPopover/UsePagedGalleryViewCheckbox.tsx
@@ -0,0 +1,29 @@
+import { Checkbox, FormControl, FormLabel } from '@invoke-ai/ui-library';
+import { useAppDispatch, useAppSelector } from 'app/store/storeHooks';
+import { selectShouldUsePagedGalleryView } from 'features/ui/store/uiSelectors';
+import { setShouldUsePagedGalleryView } from 'features/ui/store/uiSlice';
+import type { ChangeEvent } from 'react';
+import { memo, useCallback } from 'react';
+import { useTranslation } from 'react-i18next';
+
+const UsePagedGalleryViewCheckbox = () => {
+ const dispatch = useAppDispatch();
+ const { t } = useTranslation();
+ const shouldUsePagedGalleryView = useAppSelector(selectShouldUsePagedGalleryView);
+
+ const onChange = useCallback(
+ (e: ChangeEvent<HTMLInputElement>) => {
+ dispatch(setShouldUsePagedGalleryView(e.target.checked));
+ },
+ [dispatch]
+ );
+
+ return (
+ <FormControl>
+ <FormLabel flexGrow={1}>{t('gallery.usePagedGalleryView')}</FormLabel>
+ <Checkbox isChecked={shouldUsePagedGalleryView} onChange={onChange} />
+ </FormControl>
+ );
+};
+
+export default memo(UsePagedGalleryViewCheckbox);
diff --git a/invokeai/frontend/web/src/features/gallery/components/ImageGrid/GalleryPaginationPaged.tsx b/invokeai/frontend/web/src/features/gallery/components/ImageGrid/GalleryPaginationPaged.tsx
new file mode 100644
index 0000000000..a32b10e3ee
--- /dev/null
+++ b/invokeai/frontend/web/src/features/gallery/components/ImageGrid/GalleryPaginationPaged.tsx
@@ -0,0 +1,137 @@
+import { Button, Flex, IconButton, Spacer } from '@invoke-ai/ui-library';
+import { memo, useCallback, useMemo } from 'react';
+import { PiCaretLeftBold, PiCaretRightBold } from 'react-icons/pi';
+
+import { JumpToPaged } from './JumpToPaged';
+
+// Logic adapted from v5.10.0 pagination utilities.
+const range = (start: number, end: number) => {
+ const length = end - start + 1;
+ return Array.from({ length }, (_, idx) => idx + start);
+};
+
+const ELLIPSIS = 'ellipsis' as const;
+
+const getRange = (currentPage: number, totalPages: number, siblingCount: number) => {
+ const totalPageNumbers = Math.min(2 * siblingCount + 5, totalPages);
+
+ const firstPageIndex = 1;
+ const lastPageIndex = totalPages;
+
+ const leftSiblingIndex = Math.max(currentPage - siblingCount, firstPageIndex);
+ const rightSiblingIndex = Math.min(currentPage + siblingCount, lastPageIndex);
+
+ const showLeftEllipsis = leftSiblingIndex > firstPageIndex + 1;
+ const showRightEllipsis = rightSiblingIndex < lastPageIndex - 1;
+
+ const itemCount = totalPageNumbers - 2;
+
+ if (!showLeftEllipsis && showRightEllipsis) {
+ const leftRange = range(1, itemCount);
+ return [...leftRange, ELLIPSIS, lastPageIndex];
+ }
+
+ if (showLeftEllipsis && !showRightEllipsis) {
+ const rightRange = range(lastPageIndex - itemCount + 1, lastPageIndex);
+ return [firstPageIndex, ELLIPSIS, ...rightRange];
+ }
+
+ if (showLeftEllipsis && showRightEllipsis) {
+ const middleRange = range(leftSiblingIndex, rightSiblingIndex);
+ return [firstPageIndex, ELLIPSIS, ...middleRange, ELLIPSIS, lastPageIndex];
+ }
+
+ return range(firstPageIndex, lastPageIndex) as (number | 'ellipsis')[];
+};
+
+type GalleryPaginationPagedProps = {
+ pageIndex: number;
+ pageCount: number;
+ onPrev: () => void;
+ onNext: () => void;
+ onGoToPage: (pageIndex: number) => void;
+ onPageInputChange: (valueAsString: string, valueAsNumber: number) => void;
+};
+
+export const GalleryPaginationPaged = memo(
+ ({ pageIndex, pageCount, onPrev, onNext, onGoToPage, onPageInputChange }: GalleryPaginationPagedProps) => {
+ const pageButtons = useMemo(() => {
+ if (pageCount > 7) {
+ return getRange(pageIndex + 1, pageCount, 1);
+ }
+ return range(1, pageCount);
+ }, [pageCount, pageIndex]);
+
+ const onClickPrev = useCallback(() => {
+ onPrev();
+ }, [onPrev]);
+
+ const onClickNext = useCallback(() => {
+ onNext();
+ }, [onNext]);
+
+ if (!pageCount) {
+ return null;
+ }
+
+ return (
+ <Flex justifyContent="center" alignItems="center" w="full" gap={1} pt={2}>
+ <IconButton
+ size="sm"
+ aria-label="prev"
+ icon={<PiCaretLeftBold />}
+ onClick={onClickPrev}
+ isDisabled={pageIndex === 0}
+ variant="ghost"
+ />
+ <Spacer />
+ {pageButtons.map((page, i) => (
+ <PageButton key={`${page}_${i}`} page={page} currentPage={pageIndex} goToPage={onGoToPage} />
+ ))}
+ <Spacer />
+ <IconButton
+ size="sm"
+ aria-label="next"
+ icon={<PiCaretRightBold />}
+ onClick={onClickNext}
+ isDisabled={pageIndex >= pageCount - 1}
+ variant="ghost"
+ />
+ <JumpToPaged pageIndex={pageIndex} pageCount={pageCount} onChange={onPageInputChange} />
+ </Flex>
+ );
+ }
+);
+
+GalleryPaginationPaged.displayName = 'GalleryPaginationPaged';
+
+type PageButtonProps = {
+ page: number | typeof ELLIPSIS;
+ currentPage: number;
+ goToPage: (pageIndex: number) => void;
+};
+
+const PageButton = memo(({ page, currentPage, goToPage }: PageButtonProps) => {
+ const onClick = useCallback(() => {
+ if (page === ELLIPSIS) {
+ return;
+ }
+ goToPage(page - 1);
+ }, [goToPage, page]);
+
+ if (page === ELLIPSIS) {
+ return (
+ <Button size="sm" variant="link" isDisabled>
+ ...
+ </Button>
+ );
+ }
+
+ return (
+ <Button size="sm" onClick={onClick} variant={currentPage === page - 1 ? 'solid' : 'outline'}>
+ {page}
+ </Button>
+ );
+});
+
+PageButton.displayName = 'PageButton';
diff --git a/invokeai/frontend/web/src/features/gallery/components/ImageGrid/GallerySelectionCountTag.tsx b/invokeai/frontend/web/src/features/gallery/components/ImageGrid/GallerySelectionCountTag.tsx
index 28c1c68939..c1078f058c 100644
--- a/invokeai/frontend/web/src/features/gallery/components/ImageGrid/GallerySelectionCountTag.tsx
+++ b/invokeai/frontend/web/src/features/gallery/components/ImageGrid/GallerySelectionCountTag.tsx
@@ -1,17 +1,19 @@
import { Tag, TagCloseButton, TagLabel } from '@invoke-ai/ui-library';
import { useAppDispatch, useAppSelector, useAppStore } from 'app/store/storeHooks';
import { useIsRegionFocused } from 'common/hooks/focus';
-import { useGalleryImageNames } from 'features/gallery/components/use-gallery-image-names';
import { selectFirstSelectedItem, selectSelectionCount } from 'features/gallery/store/gallerySelectors';
import { selectionChanged } from 'features/gallery/store/gallerySlice';
import { useRegisteredHotkeys } from 'features/system/components/HotkeysModal/useHotkeyData';
import { memo, useCallback } from 'react';
import { useTranslation } from 'react-i18next';
-export const GallerySelectionCountTag = memo(() => {
+type GallerySelectionCountTagProps = {
+ imageNames: string[];
+};
+
+export const GallerySelectionCountTag = memo(({ imageNames }: GallerySelectionCountTagProps) => {
const { dispatch } = useAppStore();
const selectionCount = useAppSelector(selectSelectionCount);
- const { imageNames } = useGalleryImageNames();
const isGalleryFocused = useIsRegionFocused('gallery');
const onSelectPage = useCallback(() => {
diff --git a/invokeai/frontend/web/src/features/gallery/components/ImageGrid/JumpToPaged.tsx b/invokeai/frontend/web/src/features/gallery/components/ImageGrid/JumpToPaged.tsx
new file mode 100644
index 0000000000..c9e96d0ccc
--- /dev/null
+++ b/invokeai/frontend/web/src/features/gallery/components/ImageGrid/JumpToPaged.tsx
@@ -0,0 +1,110 @@
+import {
+ Button,
+ Flex,
+ NumberInput,
+ NumberInputField,
+ Popover,
+ PopoverArrow,
+ PopoverBody,
+ PopoverContent,
+ PopoverTrigger,
+ useDisclosure,
+} from '@invoke-ai/ui-library';
+import { memo, useCallback, useEffect, useRef, useState } from 'react';
+import { useHotkeys } from 'react-hotkeys-hook';
+import { useTranslation } from 'react-i18next';
+
+type JumpToPagedProps = {
+ pageIndex: number;
+ pageCount: number;
+ onChange: (valueAsString: string, valueAsNumber: number) => void;
+};
+
+export const JumpToPaged = memo(({ pageIndex, pageCount, onChange }: JumpToPagedProps) => {
+ const { t } = useTranslation();
+ const disclosure = useDisclosure();
+ const [newPageInput, setNewPageInput] = useState(String(pageIndex + 1));
+ const inputWrapperRef = useRef<HTMLDivElement | null>(null);
+
+ useEffect(() => {
+ if (disclosure.isOpen) {
+ const nextPage = pageIndex + 1;
+ setNewPageInput(String(nextPage));
+ setTimeout(() => {
+ const input = inputWrapperRef.current?.querySelector('input');
+ input?.focus();
+ input?.select();
+ }, 0);
+ }
+ }, [disclosure.isOpen, pageIndex]);
+
+ const onChangeJumpTo = useCallback((valueAsString: string, _valueAsNumber: number) => {
+ setNewPageInput(valueAsString);
+ }, []);
+
+ const onClickGo = useCallback(() => {
+ if (!newPageInput) {
+ return;
+ }
+ const parsed = Number.parseInt(newPageInput, 10);
+ if (Number.isNaN(parsed)) {
+ return;
+ }
+ onChange(String(parsed), parsed);
+ disclosure.onClose();
+ }, [disclosure, newPageInput, onChange]);
+
+ useHotkeys(
+ 'enter',
+ () => {
+ onClickGo();
+ },
+ { enabled: disclosure.isOpen, enableOnFormTags: ['input'] },
+ [disclosure.isOpen, onClickGo]
+ );
+
+ useHotkeys(
+ 'esc',
+ () => {
+ const nextPage = pageIndex + 1;
+ setNewPageInput(String(nextPage));
+ disclosure.onClose();
+ },
+ { enabled: disclosure.isOpen, enableOnFormTags: ['input'] },
+ [disclosure.isOpen, pageIndex, disclosure.onClose]
+ );
+
+ return (
+ <Popover isOpen={disclosure.isOpen} onClose={disclosure.onClose} isLazy lazyBehavior="unmount">
+ <PopoverTrigger>
+ <Button aria-label={t('gallery.jump')} size="sm" onClick={disclosure.onToggle} variant="outline">
+ {t('gallery.jump')}
+ </Button>
+ </PopoverTrigger>
+ <PopoverContent>
+ <PopoverArrow />
+ <PopoverBody>
+ <Flex gap={2} alignItems="center">
+ <NumberInput
+ ref={inputWrapperRef}
+ min={1}
+ max={pageCount}
+ value={newPageInput}
+ onChange={onChangeJumpTo}
+ size="sm"
+ w="72px"
+ clampValueOnBlur
+ >
+ <NumberInputField title="" textAlign="center" />
+ </NumberInput>
+ <Button h="full" size="sm" onClick={onClickGo}>
+ {t('gallery.go')}
+ </Button>
+ </Flex>
+ </PopoverBody>
+ </PopoverContent>
+ </Popover>
+ );
+});
+
+JumpToPaged.displayName = 'JumpToPaged';
diff --git a/invokeai/frontend/web/src/features/gallery/components/getItemsPerPage.ts b/invokeai/frontend/web/src/features/gallery/components/getItemsPerPage.ts
new file mode 100644
index 0000000000..b06ec89644
--- /dev/null
+++ b/invokeai/frontend/web/src/features/gallery/components/getItemsPerPage.ts
@@ -0,0 +1,46 @@
+import { getItemsPerRow } from './getItemsPerRow';
+
+/**
+ * Calculate how many images fit in the visible gallery area.
+ */
+export const getItemsPerPage = (rootEl: HTMLDivElement): number => {
+ const gridElement = rootEl.querySelector('.virtuoso-grid-list');
+
+ if (!gridElement) {
+ return 0;
+ }
+
+ const firstGridItem = gridElement.querySelector('.virtuoso-grid-item');
+
+ if (!firstGridItem) {
+ return 0;
+ }
+
+ const itemRect = firstGridItem.getBoundingClientRect();
+ const containerRect = rootEl.getBoundingClientRect();
+ const gridStyle = window.getComputedStyle(gridElement);
+ const gap = parseFloat(gridStyle.gap);
+
+ if (isNaN(gap) || !itemRect.width || !itemRect.height || !containerRect.width || !containerRect.height) {
+ return 0;
+ }
+
+ const itemsPerRow = getItemsPerRow(rootEl);
+
+ if (itemsPerRow === 0) {
+ return 0;
+ }
+
+ let rowsPerPage = 0;
+ let spaceUsed = 0;
+
+ while (spaceUsed + itemRect.height <= containerRect.height + 1) {
+ rowsPerPage++;
+ spaceUsed += itemRect.height;
+ if (spaceUsed + gap <= containerRect.height) {
+ spaceUsed += gap;
+ }
+ }
+
+ return Math.max(1, rowsPerPage) * itemsPerRow;
+};
diff --git a/invokeai/frontend/web/src/features/ui/store/uiSelectors.ts b/invokeai/frontend/web/src/features/ui/store/uiSelectors.ts
index abaef76c55..4287a8a08b 100644
--- a/invokeai/frontend/web/src/features/ui/store/uiSelectors.ts
+++ b/invokeai/frontend/web/src/features/ui/store/uiSelectors.ts
@@ -4,4 +4,5 @@ import { selectUiSlice } from 'features/ui/store/uiSlice';
export const selectActiveTab = createSelector(selectUiSlice, (ui) => ui.activeTab);
export const selectShouldShowItemDetails = createSelector(selectUiSlice, (ui) => ui.shouldShowItemDetails);
export const selectShouldShowProgressInViewer = createSelector(selectUiSlice, (ui) => ui.shouldShowProgressInViewer);
+export const selectShouldUsePagedGalleryView = createSelector(selectUiSlice, (ui) => ui.shouldUsePagedGalleryView);
export const selectPickerCompactViewStates = createSelector(selectUiSlice, (ui) => ui.pickerCompactViewStates);
diff --git a/invokeai/frontend/web/src/features/ui/store/uiSlice.ts b/invokeai/frontend/web/src/features/ui/store/uiSlice.ts
index b1f3fe9fae..67036928a9 100644
--- a/invokeai/frontend/web/src/features/ui/store/uiSlice.ts
+++ b/invokeai/frontend/web/src/features/ui/store/uiSlice.ts
@@ -20,6 +20,9 @@ const slice = createSlice({
setShouldShowProgressInViewer: (state, action: PayloadAction<UIState['shouldShowProgressInViewer']>) => {
state.shouldShowProgressInViewer = action.payload;
},
+ setShouldUsePagedGalleryView: (state, action: PayloadAction<UIState['shouldUsePagedGalleryView']>) => {
+ state.shouldUsePagedGalleryView = action.payload;
+ },
accordionStateChanged: (
state,
action: PayloadAction<{
@@ -77,6 +80,7 @@ export const {
setActiveTab,
setShouldShowItemDetails,
setShouldShowProgressInViewer,
+ setShouldUsePagedGalleryView,
accordionStateChanged,
expanderStateChanged,
shouldShowNotificationChanged,
@@ -109,6 +113,10 @@ export const uiSliceConfig: SliceConfig<typeof slice> = {
state.panels = {};
state._version = 4;
}
+ if (state._version === 4) {
+ state.shouldUsePagedGalleryView = false;
+ state._version = 5;
+ }
return zUIState.parse(state);
},
persistDenylist: ['shouldShowItemDetails'],
diff --git a/invokeai/frontend/web/src/features/ui/store/uiTypes.ts b/invokeai/frontend/web/src/features/ui/store/uiTypes.ts
index b86ba3fa18..4e6e851ed1 100644
--- a/invokeai/frontend/web/src/features/ui/store/uiTypes.ts
+++ b/invokeai/frontend/web/src/features/ui/store/uiTypes.ts
@@ -13,10 +13,11 @@ const zSerializable = z.any().refine(isPlainObject);
export type Serializable = z.infer<typeof zSerializable>;
export const zUIState = z.object({
- _version: z.literal(4),
+ _version: z.literal(5),
activeTab: zTabName,
shouldShowItemDetails: z.boolean(),
shouldShowProgressInViewer: z.boolean(),
+ shouldUsePagedGalleryView: z.boolean(),
accordions: z.record(z.string(), z.boolean()),
expanders: z.record(z.string(), z.boolean()),
textAreaSizes: z.record(z.string(), zPartialDimensions),
@@ -26,10 +27,11 @@ export const zUIState = z.object({
});
export type UIState = z.infer<typeof zUIState>;
export const getInitialUIState = (): UIState => ({
- _version: 4 as const,
+ _version: 5 as const,
activeTab: 'generate' as const,
shouldShowItemDetails: false,
shouldShowProgressInViewer: true,
+ shouldUsePagedGalleryView: false,
accordions: {},
expanders: {},
textAreaSizes: {},