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.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
refactor(webv2): consolidate preview stage chrome
joshistoast197a16f6ebae
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewCompare.tsx b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewCompare.tsxindex f829b979e2..a84d968a13 100644--- a/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewCompare.tsx+++ b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewCompare.tsx@@ -1,7 +1,7 @@import type { GeneratedImageContract } from '@features/gallery';import type { WidgetRuntimeApi } from '@workbench/widgetContracts';-import { Badge, Box, Flex, HStack, SegmentGroup, Stack, type SystemStyleObject } from '@chakra-ui/react';+import { Badge, Box, Flex, HStack, SegmentGroup, Stack } from '@chakra-ui/react';import { Button } from '@platform/ui';import { ArrowLeftRightIcon, XIcon } from 'lucide-react';import {@@ -18,6 +18,7 @@ import { useTranslation } from 'react-i18next';import type { PreviewComparisonMode } from './previewSettings';+import { getFittedFrameCss, previewGridCss } from './PreviewStage';import { useCompareLoupe, type CompareLoupePane } from './useCompareLoupe';const COMPARISON_MODES: { labelKey: string; value: PreviewComparisonMode }[] = [@@ -32,14 +33,6 @@ export const getNextPreviewComparisonMode = (mode: PreviewComparisonMode): Previreturn COMPARISON_MODES[(index + 1) % COMPARISON_MODES.length]?.value ?? 'slider';};-const getFittedFrameCss = (width: number, height: number): SystemStyleObject => ({- aspectRatio: `${width} / ${height}`,- height: 'auto',- maxHeight: '100%',- maxWidth: '100%',- width: `min(100cqw, calc(100cqh * ${width / height}))`,-});-const BASE_IMAGE_STYLE = {display: 'block',height: 'auto',@@ -57,13 +50,6 @@ const OVERLAY_IMAGE_STYLE = {width: '100%',} as const;-const previewGridCss = {- backgroundImage: 'radial-gradient(circle, currentColor 1px, transparent 1.5px)',- backgroundPosition: 'center',- backgroundRepeat: 'repeat',- backgroundSize: '24px 24px',-} as const;-const sliderTouchStyle = { touchAction: 'none' } as const;/** Three-mode, project-persisted comparison surface. */diff --git a/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewFrame.tsx b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewFrame.tsxindex 69c6ada926..f5f7a624a0 100644--- a/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewFrame.tsx+++ b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewFrame.tsx@@ -2,7 +2,7 @@ import type { GalleryItemKey, GalleryItemRef } from '@features/gallery';/* eslint-disable react/react-compiler */import type { StreamingImageSource } from '@platform/ui/streaming-image/streamingImageSource';-import { Badge, Box, Flex, Text, type SystemStyleObject } from '@chakra-ui/react';+import { Badge, Flex, Text } from '@chakra-ui/react';import { useDraggable } from '@dnd-kit/core';import { getGalleryItemDragData, getGalleryItemDragId } from '@features/gallery/utility';import { getAuthSession, refreshProtectedMediaCookie } from '@features/identity';@@ -23,42 +23,9 @@ import { useTranslation } from 'react-i18next';import { PreviewCompareDropZone } from './PreviewCompareDropZone';import { PreviewLiveOverlay } from './PreviewLiveReadout';+import { FittedFrame, PreviewStage } from './PreviewStage';import { usePreviewLoupe, type PreviewLoupeControls } from './usePreviewLoupe';-export const previewGridCss = {- backgroundImage: 'radial-gradient(circle, currentColor 1px, transparent 1.5px)',- backgroundPosition: 'center',- backgroundRepeat: 'repeat',- backgroundSize: '24px 24px',-} as const;--export const getFittedFrameCss = (width: number, height: number): SystemStyleObject => ({- aspectRatio: `${width} / ${height}`,- height: 'auto',- maxHeight: '100%',- maxWidth: '100%',- width: `min(100cqw, calc(100cqh * ${width / height}))`,-});--/**- * Room for the centre region's floating chrome islands. `CenterArea` publishes- * the variable and nothing else does, so the `0px` fallback keeps the right- * panel — which reserves a real header row instead — unpadded.- */-const CENTER_CHROME_INSET = 'var(--wb-center-chrome-inset, 0px)';--/**- * Top padding for a media stage: its own padding plus room for the islands.- *- * It belongs on the stage rather than on the widget root so the dot grid still- * runs to every edge and passes *behind* the chrome — the same arrangement that- * lets `paddingBottom` pass it behind the footer island. Because the stage is a size- * container, `getFittedFrameCss` reads the shrunken content box and refits the- * media with no further change.- */-const getStagePaddingTop = (padding: string | undefined): string =>- padding === undefined ? CENTER_CHROME_INSET : `calc(var(--chakra-spacing-${padding}) + ${CENTER_CHROME_INSET})`;-export type PreviewMediaSource =| { itemKey: GalleryItemKey; kind: 'image'; source: StreamingImageSource }| { itemKey: GalleryItemKey; kind: 'video'; label: string; poster: string; src: string };@@ -174,6 +141,16 @@ const PreviewImageFrame = ({}),[isLive, shouldAntialiasLiveImage]);+ const media = source ? (+ <img+ alt={source.alt}+ draggable={false}+ height={frameHeight}+ src={source.src}+ style={imageStyle}+ width={frameWidth}+ />+ ) : null;const liveBadge = isLive ? (typeof liveQueueItemId === 'string' ? (<PreviewLiveOverlay queueItemId={liveQueueItemId} />@@ -186,96 +163,51 @@ const PreviewImageFrame = ({if (variant === 'inset') {return (- <Flex- align="center"- backgroundColor="bg.inset"- color="fg.grid"- containerType="size"- css={previewGridCss}- h="full"- justify="center"- // Live tiles all reserve the inset, so in a multi-session grid the- // bottom row reserves room it does not need. Its own dot grid still- // fills the cell, so only the fitted frame sits a little lower.- pt={getStagePaddingTop(undefined)}- w="full"- >+ // Live tiles all reserve the chrome inset, so in a multi-session grid the+ // bottom row reserves room it does not need. Its own dot grid still fills+ // the cell, so only the fitted frame sits a little lower.+ <PreviewStage fill="parent">{source ? (- <Box+ <FittedFrameborderColor={isLive ? 'accent.solid' : 'border.emphasized'}- borderWidth="1px"- boxShadow="0 24px 80px rgba(0,0,0,0.42)"- css={getFittedFrameCss(frameWidth, frameHeight)}- overflow="hidden"- position="relative"- rounded="lg"+ frameHeight={frameHeight}+ frameWidth={frameWidth}>- <img- alt={source.alt}- draggable={false}- height={frameHeight}- src={source.src}- style={imageStyle}- width={frameWidth}- />+ {media}{liveBadge}- </Box>+ </FittedFrame>) : (children)}- </Flex>+ </PreviewStage>);}return (- <Flex+ <PreviewStageref={loupe.stageRefCallback}- align="center"- backgroundColor="bg.inset"- color="fg.grid"- containerType="size"- css={previewGridCss}cursor={loupe.isZoomed ? 'grab' : undefined}- flex="1"- justify="center"- minH="0"- overflow="hidden"- p={padding}- pb={paddingBottom}- position="relative"- pt={getStagePaddingTop(padding)}- w="full"+ fill="flex"+ padding={padding}+ paddingBottom={paddingBottom}{...loupe.stageProps}><PreviewCompareDropZone currentImageName={dragItem?.kind === 'image' ? dragItem.name : null} />- <Box+ <FittedFrameref={setContentRef}{...listeners}bg="transparent"- borderWidth="1px"borderColor={isLive ? 'accent.solid' : 'border.emphasized'}- boxShadow="0 24px 80px rgba(0,0,0,0.42)"- css={getFittedFrameCss(frameWidth, frameHeight)}cursor={isDragDisabled ? undefined : isDragging ? 'grabbing' : 'grab'}+ frameHeight={frameHeight}+ frameWidth={frameWidth}opacity={isDragging ? 0.55 : undefined}- overflow="hidden"- position="relative"- rounded="lg"touchAction={isDragDisabled ? undefined : 'none'}onContextMenu={onContextMenu ? handleContextMenu : undefined}>- {source ? (- <img- alt={source.alt}- draggable={false}- height={frameHeight}- src={source.src}- style={imageStyle}- width={frameWidth}- />- ) : null}+ {media}{liveBadge}- </Box>+ </FittedFrame>{loupe.zoomPercent !== null ? (<Badgearia-label={t('widgets.preview.resetZoom')}@@ -293,7 +225,7 @@ const PreviewImageFrame = ({{loupe.zoomPercent}%</Badge>) : null}- </Flex>+ </PreviewStage>);};@@ -473,31 +405,11 @@ const PreviewVideo = ({);return (- <Flex- align="center"- backgroundColor="bg.inset"- color="fg.grid"- containerType="size"- css={previewGridCss}- flex="1"- justify="center"- minH="0"- overflow="hidden"- p={padding}- pb={paddingBottom}- position="relative"- pt={getStagePaddingTop(padding)}- w="full"- >- <Box+ <PreviewStage fill="flex" padding={padding} paddingBottom={paddingBottom}>+ <FittedFramebg="black"- borderWidth="1px"- borderColor="border.emphasized"- boxShadow="0 24px 80px rgba(0,0,0,0.42)"- css={getFittedFrameCss(frameWidth, frameHeight)}- overflow="hidden"- position="relative"- rounded="lg"+ frameHeight={frameHeight}+ frameWidth={frameWidth}onContextMenu={onContextMenu ? handleContextMenu : undefined}>{/* User-provided gallery videos do not include a caption-track contract. */}@@ -550,8 +462,8 @@ const PreviewVideo = ({</Flex></>) : null}- </Box>- </Flex>+ </FittedFrame>+ </PreviewStage>);};diff --git a/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewLiveReadout.tsx b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewLiveReadout.tsxindex d96ddd24dc..de45bb8109 100644--- a/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewLiveReadout.tsx+++ b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewLiveReadout.tsx@@ -1,28 +1,14 @@-import { Badge, Text } from '@chakra-ui/react';-import { useQueueItemProgress, type QueueItemProgress } from '@features/queue/react';+import { Badge } from '@chakra-ui/react';+import { useQueueItemProgress } from '@features/queue/react';import { useTranslation } from 'react-i18next';/**- * Leaf subscribers for live generation state, kept out of the frame/footer- * components so per-step progress events only re-render these slivers.+ * Leaf subscriber for live generation state, kept out of the frame component+ * so per-step progress events only re-render this sliver.*/const formatPercent = (percentage: number): string => `${Math.round(percentage * 100)}%`;-const getStepMessage = (progress: QueueItemProgress | null, generatingLabel: string): string => {- if (!progress) {- return generatingLabel;- }-- const message = progress.message || generatingLabel;-- if (progress.totalItemCount && progress.totalItemCount > 1 && progress.activeItemIndex) {- return `${progress.activeItemIndex}/${progress.totalItemCount} · ${message}`;- }-- return message;-};-/*** The live badge drawn over the fitted frame.*@@ -42,15 +28,3 @@ export const PreviewLiveOverlay = ({ queueItemId }: { queueItemId: string }) =></Badge>);};--/** The footer's live status line ("2/4 · Denoising"), replacing the dimensions row while generating. */-export const PreviewLiveStatusLine = ({ queueItemId }: { queueItemId: string }) => {- const { t } = useTranslation();- const progress = useQueueItemProgress(queueItemId);-- return (- <Text color="fg.subtle" flexShrink={0} fontSize="2xs">- {getStepMessage(progress, t('common.generating'))}- </Text>- );-};diff --git a/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewStage.tsx b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewStage.tsxnew file mode 100644index 0000000000..744432907d--- /dev/null+++ b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewStage.tsx@@ -0,0 +1,103 @@+import type { Ref } from 'react';++import { Box, Flex, type BoxProps, type FlexProps, type SystemStyleObject } from '@chakra-ui/react';++/**+ * The two pieces of chrome every preview surface shares, extracted so the+ * image, video, live, and empty branches stop carrying their own copies:+ *+ * - `PreviewStage` — the dot-grid floor. Centers its content, establishes the+ * size container that `getFittedFrameCss` measures against, and reserves top+ * clearance for the centre region's floating chrome islands.+ * - `FittedFrame` — the media card: aspect-fitted, bordered, rounded, with the+ * deep drop shadow that lifts it off the grid.+ */++export const previewGridCss = {+ backgroundImage: 'radial-gradient(circle, currentColor 1px, transparent 1.5px)',+ backgroundPosition: 'center',+ backgroundRepeat: 'repeat',+ backgroundSize: '24px 24px',+} as const;++export const getFittedFrameCss = (width: number, height: number): SystemStyleObject => ({+ aspectRatio: `${width} / ${height}`,+ height: 'auto',+ maxHeight: '100%',+ maxWidth: '100%',+ width: `min(100cqw, calc(100cqh * ${width / height}))`,+});++/**+ * Room for the centre region's floating chrome islands. `CenterArea` publishes+ * the variable and nothing else does, so the `0px` fallback keeps the right+ * panel — which reserves a real header row instead — unpadded.+ */+const CENTER_CHROME_INSET = 'var(--wb-center-chrome-inset, 0px)';++/**+ * Top padding for a media stage: its own padding plus room for the islands.+ *+ * It belongs on the stage rather than on the widget root so the dot grid still+ * runs to every edge and passes *behind* the chrome — the same arrangement that+ * lets `paddingBottom` pass it behind the footer island. Because the stage is a+ * size container, `getFittedFrameCss` reads the shrunken content box and refits+ * the media with no further change.+ */+const getStagePaddingTop = (padding: string | undefined): string =>+ padding === undefined ? CENTER_CHROME_INSET : `calc(var(--chakra-spacing-${padding}) + ${CENTER_CHROME_INSET})`;++/**+ * `fill="parent"` is the inset arrangement (`h="full"`, used where the stage IS+ * the widget body); `fill="flex"` is the framed one (`flex="1" minH="0"+ * overflow="hidden"`, used where overlays float over the stage's lower edge).+ *+ * Both fills are positioned: absolutely-placed stage children (zoom badge,+ * future overlays) anchor to the stage itself, never to whatever happens to+ * contain it. (The old inset copy was static; the difference is unobservable+ * at its call sites and the uniform rule is the one worth keeping.)+ */+export const PreviewStage = ({+ fill,+ padding,+ paddingBottom,+ ...props+}: Omit<FlexProps, 'padding' | 'paddingBottom'> & {+ fill: 'flex' | 'parent';+ padding?: string;+ paddingBottom?: string;+ ref?: Ref<HTMLDivElement>;+}) => (+ <Flex+ align="center"+ backgroundColor="bg.inset"+ color="fg.grid"+ containerType="size"+ css={previewGridCss}+ justify="center"+ p={padding}+ pb={paddingBottom}+ position="relative"+ pt={getStagePaddingTop(padding)}+ w="full"+ {...(fill === 'parent' ? { h: 'full' } : { flex: '1', minH: '0', overflow: 'hidden' })}+ {...props}+ />+);++export const FittedFrame = ({+ frameHeight,+ frameWidth,+ ...props+}: BoxProps & { frameHeight: number; frameWidth: number; ref?: Ref<HTMLDivElement> }) => (+ <Box+ borderColor="border.emphasized"+ borderWidth="1px"+ boxShadow="0 24px 80px rgba(0,0,0,0.42)"+ css={getFittedFrameCss(frameWidth, frameHeight)}+ overflow="hidden"+ position="relative"+ rounded="lg"+ {...props}+ />+);