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(preview): stale-while-loading metadata, remount-free zoom reset, filmstrip ScrollArea

joshistoast446f18c261d2
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewFilmstrip.tsx b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewFilmstrip.tsx
index 20c2377f6a..14f1889ba5 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewFilmstrip.tsx
+++ b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewFilmstrip.tsx
@@ -1,6 +1,6 @@
import type { GeneratedImageContract } from '@workbench/types';
-import { Box, HStack } from '@chakra-ui/react';
+import { Box, HStack, ScrollArea } from '@chakra-ui/react';
import { useDraggable } from '@dnd-kit/core';
import { getGalleryImageDragData } from '@workbench/widgets/gallery/galleryDnd';
import { useCallback, useMemo } from 'react';
@@ -35,17 +35,26 @@ export const PreviewFilmstrip = ({
}
return (
- <HStack flexShrink={0} gap="1" overflowX="auto" overflowY="hidden" pb="1" w="full">
- {images.map((image) => (
- <FilmstripThumb
- key={image.imageName}
- image={image}
- isSelected={image.imageName === selectedImageName}
- size={thumbSize}
- onSelect={onSelect}
- />
- ))}
- </HStack>
+ <ScrollArea.Root flexShrink={0} minW="0" size="xs" variant="hover" w="full">
+ <ScrollArea.Viewport w="full">
+ <ScrollArea.Content asChild>
+ <HStack gap="1" pb="1">
+ {images.map((image) => (
+ <FilmstripThumb
+ key={image.imageName}
+ image={image}
+ isSelected={image.imageName === selectedImageName}
+ size={thumbSize}
+ onSelect={onSelect}
+ />
+ ))}
+ </HStack>
+ </ScrollArea.Content>
+ </ScrollArea.Viewport>
+ <ScrollArea.Scrollbar orientation="horizontal">
+ <ScrollArea.Thumb />
+ </ScrollArea.Scrollbar>
+ </ScrollArea.Root>
);
};
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewFooter.tsx b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewFooter.tsx
index 3fe9498727..d04e3c4525 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewFooter.tsx
+++ b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewFooter.tsx
@@ -105,7 +105,6 @@ export const PreviewFooter = ({
</Stack>
{actionImage ? (
<PreviewMetadataPanel
- key={actionImage.imageName}
actions={actions}
image={actionImage}
isOpen={isMetadataOpen}
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewFrame.tsx b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewFrame.tsx
index 0ed79b54d3..9fd1df2323 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewFrame.tsx
+++ b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewFrame.tsx
@@ -68,6 +68,10 @@ export const PreviewFrame = ({
enabled: variant === 'framed' && !isLive,
naturalWidth: frameWidth,
});
+
+ // Reset zoom in place when the displayed image changes (or goes live) — a
+ // remount would flash the frame on every selection.
+ loupe.syncDisplayedSource(variant === 'framed' && !isLive && source ? source.src : null);
const handleContextMenu = useCallback(
(event: MouseEvent<HTMLDivElement>) => {
if (onContextMenu) {
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewMetadataPanel.tsx b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewMetadataPanel.tsx
index f7fef55f11..ecac91ffad 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewMetadataPanel.tsx
+++ b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewMetadataPanel.tsx
@@ -48,14 +48,17 @@ export const PreviewMetadataPanel = ({
const { t } = useTranslation();
const [loaded, setLoaded] = useState<{
capabilities: ImageRecallCapabilities;
+ imageName: string;
metadata: GalleryImageMetadata | null;
} | null>(null);
+ // Stale-while-loading: when the selection changes, the previous image's rows
+ // stay in place (slightly dimmed) until the new metadata arrives, so
+ // navigating never collapses the panel or shifts the layout.
+ const isCurrent = loaded?.imageName === image.imageName;
const isLoading = isOpen && loaded === null;
- // Metadata is immutable per image and this component is keyed per image, so
- // fetch exactly once, on first expand.
useEffect(() => {
- if (!isOpen || loaded !== null) {
+ if (!isOpen || loaded?.imageName === image.imageName) {
return;
}
@@ -64,22 +67,22 @@ export const PreviewMetadataPanel = ({
Promise.all([getGalleryImageMetadata(image.imageName), actions.getImageRecallCapabilities(image)])
.then(([metadata, capabilities]) => {
if (!isStale) {
- setLoaded({ capabilities, metadata });
+ setLoaded({ capabilities, imageName: image.imageName, metadata });
}
})
.catch(() => {
if (!isStale) {
- setLoaded({ capabilities: EMPTY_IMAGE_RECALL_CAPABILITIES, metadata: null });
+ setLoaded({ capabilities: EMPTY_IMAGE_RECALL_CAPABILITIES, imageName: image.imageName, metadata: null });
}
});
return () => {
isStale = true;
};
- }, [actions, image, isOpen, loaded]);
+ }, [actions, image, isOpen, loaded?.imageName]);
const entries = parsePreviewMetadata(loaded?.metadata ?? null);
- const recallItems = loaded ? RECALL_ITEMS.filter((item) => loaded.capabilities[item.capability]) : [];
+ const recallItems = loaded && isCurrent ? RECALL_ITEMS.filter((item) => loaded.capabilities[item.capability]) : [];
return (
<Stack gap="2">
@@ -98,7 +101,15 @@ export const PreviewMetadataPanel = ({
</Text>
</HStack>
{isOpen ? (
- <Stack gap="2" maxH="40cqh" overflowY="auto" pe="1">
+ <Stack
+ gap="2"
+ maxH="40cqh"
+ opacity={isCurrent || loaded === null ? 1 : 0.6}
+ overflowY="auto"
+ pe="1"
+ transitionDuration="var(--wb-motion-duration-fast)"
+ transitionProperty="opacity"
+ >
{isLoading ? (
<Text color="fg.subtle" fontSize="2xs">
{t('widgets.preview.loadingMetadata')}
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewWidgetView.tsx b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewWidgetView.tsx
index 60ff304014..7516ea00d1 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewWidgetView.tsx
+++ b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewWidgetView.tsx
@@ -623,7 +623,6 @@ const SelectedImagePreview = ({
return (
<Stack gap="3" h="full" minH="0" outline="none" tabIndex={0} w="full" onKeyDown={handleKeyDown}>
<PreviewFrame
- key={`${image.imageName}:${isProgressImage ? 'live' : 'still'}`}
frameHeight={previewHeight}
frameWidth={previewWidth}
isLive={isProgressImage}
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/preview/useCompareLoupe.ts b/invokeai/frontend/webv2/src/workbench/widgets/preview/useCompareLoupe.ts
new file mode 100644
index 0000000000..9618999c32
--- /dev/null
+++ b/invokeai/frontend/webv2/src/workbench/widgets/preview/useCompareLoupe.ts
@@ -0,0 +1,235 @@
+/* eslint-disable react/react-compiler */
+import { useEffectEvent, useRef, useState, type MouseEvent, type PointerEvent } from 'react';
+
+/**
+ * Shared zoom/pan for the side-by-side comparison panes: one transform, kept
+ * in image-fraction space (scale is unitless; translation is a fraction of the
+ * pane's rendered size), applied imperatively to every registered pane. Zoom
+ * into the left eye and the right pane's left eye follows. Gated by the caller
+ * to matching-dimension pairs, where fraction space is exact.
+ */
+
+const MAX_ACTUAL_ZOOM = 8;
+const WHEEL_ZOOM_FACTOR = 0.0015;
+const PIXELATED_ACTUAL_ZOOM = 2;
+
+interface FractionTransform {
+ scale: number;
+ fx: number;
+ fy: number;
+}
+
+interface PaneElements {
+ frame: HTMLDivElement | null;
+ image: HTMLImageElement | null;
+}
+
+export interface CompareLoupePane {
+ frameProps: {
+ onDoubleClick: (event: MouseEvent<HTMLDivElement>) => void;
+ onLostPointerCapture: (event: PointerEvent<HTMLDivElement>) => void;
+ onPointerCancel: (event: PointerEvent<HTMLDivElement>) => void;
+ onPointerDown: (event: PointerEvent<HTMLDivElement>) => void;
+ onPointerMove: (event: PointerEvent<HTMLDivElement>) => void;
+ onPointerUp: (event: PointerEvent<HTMLDivElement>) => void;
+ };
+ frameRefCallback: (node: HTMLDivElement | null) => void;
+ imageRefCallback: (node: HTMLImageElement | null) => void;
+}
+
+const clampFraction = (f: number, scale: number): number =>
+ scale <= 1 ? (1 - scale) / 2 : Math.min(0, Math.max(1 - scale, f));
+
+export const useCompareLoupe = ({
+ enabled,
+ naturalWidth,
+}: {
+ enabled: boolean;
+ naturalWidth: number;
+}): { getPane: (index: 0 | 1) => CompareLoupePane | null; isZoomed: boolean } => {
+ const transformRef = useRef<FractionTransform>({ fx: 0, fy: 0, scale: 1 });
+ const panesRef = useRef<[PaneElements, PaneElements]>([
+ { frame: null, image: null },
+ { frame: null, image: null },
+ ]);
+ const rafRef = useRef<number | null>(null);
+ const panRef = useRef<{ pointerId: number; startX: number; startY: number; fx: number; fy: number } | null>(null);
+ const [isZoomed, setIsZoomed] = useState(false);
+
+ const apply = useEffectEvent(() => {
+ if (rafRef.current !== null) {
+ return;
+ }
+
+ rafRef.current = requestAnimationFrame(() => {
+ rafRef.current = null;
+ const { fx, fy, scale } = transformRef.current;
+ const isFit = scale === 1;
+
+ for (const pane of panesRef.current) {
+ const { frame, image } = pane;
+
+ if (!frame || !image) {
+ continue;
+ }
+
+ image.style.transform = isFit
+ ? ''
+ : `translate(${fx * frame.clientWidth}px, ${fy * frame.clientHeight}px) scale(${scale})`;
+ image.style.transformOrigin = '0 0';
+
+ const actualZoom = (scale * frame.clientWidth) / Math.max(1, naturalWidth);
+
+ image.style.imageRendering = !isFit && actualZoom >= PIXELATED_ACTUAL_ZOOM ? 'pixelated' : '';
+ }
+
+ setIsZoomed(!isFit);
+ });
+ });
+
+ const setTransform = useEffectEvent((next: FractionTransform) => {
+ const firstFrame = panesRef.current.find((pane) => pane.frame)?.frame ?? null;
+ const maxScale = firstFrame
+ ? Math.max(1, (MAX_ACTUAL_ZOOM * naturalWidth) / Math.max(1, firstFrame.clientWidth))
+ : MAX_ACTUAL_ZOOM;
+ const scale = Math.max(1, Math.min(next.scale, maxScale));
+
+ transformRef.current =
+ scale === 1
+ ? { fx: 0, fy: 0, scale: 1 }
+ : { fx: clampFraction(next.fx, scale), fy: clampFraction(next.fy, scale), scale };
+ apply();
+ });
+
+ const zoomAroundFraction = useEffectEvent((pfx: number, pfy: number, nextScale: number) => {
+ const { fx, fy, scale } = transformRef.current;
+ const ratio = nextScale / scale;
+
+ setTransform({
+ fx: pfx - (pfx - fx) * ratio,
+ fy: pfy - (pfy - fy) * ratio,
+ scale: nextScale,
+ });
+ });
+
+ const handleWheel = useEffectEvent((node: HTMLDivElement, event: WheelEvent) => {
+ event.preventDefault();
+ const rect = node.getBoundingClientRect();
+
+ if (rect.width === 0 || rect.height === 0) {
+ return;
+ }
+
+ const sensitivity = event.ctrlKey ? WHEEL_ZOOM_FACTOR * 4 : WHEEL_ZOOM_FACTOR;
+
+ zoomAroundFraction(
+ (event.clientX - rect.left) / rect.width,
+ (event.clientY - rect.top) / rect.height,
+ transformRef.current.scale * Math.exp(-event.deltaY * sensitivity)
+ );
+ });
+
+ // One stable set of callbacks per pane index; wheel listeners need manual
+ // attachment (`passive: false`) so ref callbacks with cleanup own them.
+ const [panes] = useState<[CompareLoupePane, CompareLoupePane]>(() => {
+ const createPane = (index: 0 | 1): CompareLoupePane => ({
+ frameProps: {
+ onDoubleClick: (event) => {
+ const frame = panesRef.current[index].frame;
+
+ if (!frame || frame.clientWidth === 0) {
+ return;
+ }
+
+ if (transformRef.current.scale !== 1) {
+ setTransform({ fx: 0, fy: 0, scale: 1 });
+ return;
+ }
+
+ const rect = frame.getBoundingClientRect();
+
+ zoomAroundFraction(
+ (event.clientX - rect.left) / rect.width,
+ (event.clientY - rect.top) / rect.height,
+ Math.max(1, naturalWidth / frame.clientWidth)
+ );
+ },
+ onLostPointerCapture: (event) => endPan(event),
+ onPointerCancel: (event) => endPan(event),
+ onPointerDown: (event) => {
+ if (event.button !== 0 || transformRef.current.scale === 1) {
+ return;
+ }
+
+ event.preventDefault();
+ event.currentTarget.setPointerCapture(event.pointerId);
+ panRef.current = {
+ fx: transformRef.current.fx,
+ fy: transformRef.current.fy,
+ pointerId: event.pointerId,
+ startX: event.clientX,
+ startY: event.clientY,
+ };
+ },
+ onPointerMove: (event) => {
+ const pan = panRef.current;
+ const frame = panesRef.current[index].frame;
+
+ if (!pan || pan.pointerId !== event.pointerId || !frame || frame.clientWidth === 0) {
+ return;
+ }
+
+ event.preventDefault();
+ setTransform({
+ fx: pan.fx + (event.clientX - pan.startX) / frame.clientWidth,
+ fy: pan.fy + (event.clientY - pan.startY) / frame.clientHeight,
+ scale: transformRef.current.scale,
+ });
+ },
+ onPointerUp: (event) => endPan(event),
+ },
+ frameRefCallback: (node) => {
+ panesRef.current[index].frame = node;
+
+ if (!node) {
+ return;
+ }
+
+ const onWheel = (event: WheelEvent): void => handleWheel(node, event);
+
+ node.addEventListener('wheel', onWheel, { passive: false });
+ apply();
+
+ return () => {
+ node.removeEventListener('wheel', onWheel);
+ };
+ },
+ imageRefCallback: (node) => {
+ panesRef.current[index].image = node;
+
+ if (node) {
+ apply();
+ }
+ },
+ });
+ const endPan = (event: PointerEvent<HTMLDivElement>): void => {
+ if (panRef.current?.pointerId !== event.pointerId) {
+ return;
+ }
+
+ if (event.currentTarget.hasPointerCapture(event.pointerId)) {
+ event.currentTarget.releasePointerCapture(event.pointerId);
+ }
+
+ panRef.current = null;
+ };
+
+ return [createPane(0), createPane(1)];
+ });
+
+ if (!enabled) {
+ return { getPane: () => null, isZoomed: false };
+ }
+
+ return { getPane: (index) => panes[index], isZoomed };
+};
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/preview/usePreviewLoupe.ts b/invokeai/frontend/webv2/src/workbench/widgets/preview/usePreviewLoupe.ts
index 4fe12cac43..b7cd637c7c 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/preview/usePreviewLoupe.ts
+++ b/invokeai/frontend/webv2/src/workbench/widgets/preview/usePreviewLoupe.ts
@@ -55,8 +55,44 @@ export const usePreviewLoupe = ({
const panPointerRef = useRef<{ pointerId: number; startX: number; startY: number; tx: number; ty: number } | null>(
null
);
+ const lastSourceTokenRef = useRef<string | null | undefined>(undefined);
const [zoomPercent, setZoomPercent] = useState<number | null>(null);
+ /**
+ * Called during render with a token identifying the displayed image (or null
+ * while the loupe is inapplicable, e.g. live frames). A token change resets
+ * the transform in place — no remount, so the img element (and its decoded
+ * pixels) survive selection changes without a flash.
+ */
+ const syncDisplayedSource = (token: string | null): void => {
+ if (lastSourceTokenRef.current === token) {
+ return;
+ }
+
+ lastSourceTokenRef.current = token;
+
+ if (transformRef.current.scale === 1) {
+ return;
+ }
+
+ transformRef.current = { scale: 1, tx: 0, ty: 0 };
+ panPointerRef.current = null;
+
+ if (rafRef.current === null) {
+ rafRef.current = requestAnimationFrame(() => {
+ rafRef.current = null;
+ const image = imageRef.current;
+
+ if (image) {
+ image.style.transform = '';
+ image.style.imageRendering = '';
+ }
+
+ setZoomPercent(null);
+ });
+ }
+ };
+
const getActualZoom = (scale: number): number => {
const renderedWidth = frameRef.current?.clientWidth ?? 0;
@@ -242,6 +278,7 @@ export const usePreviewLoupe = ({
imageRef: null,
isZoomed: false,
reset,
+ syncDisplayedSource,
zoomPercent: null,
};
}
@@ -259,6 +296,7 @@ export const usePreviewLoupe = ({
imageRef,
isZoomed: zoomPercent !== null,
reset,
+ syncDisplayedSource,
zoomPercent,
};
};