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
feat(webv2): add shared shell tools for widgets
joshistoastaf839a098c01
diff --git a/invokeai/frontend/webv2/src/workbench/components/ImageContextMenu.tsx b/invokeai/frontend/webv2/src/workbench/components/ImageContextMenu.tsxindex 641c68885b..2f772ae4aa 100644--- a/invokeai/frontend/webv2/src/workbench/components/ImageContextMenu.tsx+++ b/invokeai/frontend/webv2/src/workbench/components/ImageContextMenu.tsx@@ -25,6 +25,7 @@ import {import type { GalleryBoard, GalleryImage } from '../gallery/api';import { Button } from './ui/Button';+import { MenuContent } from './ui/Menu';import { Tooltip } from './ui/Tooltip';import { useWorkbench } from '../WorkbenchContext';import type { ImageActions } from './useImageActions';@@ -36,16 +37,11 @@ export interface ImageContextMenuTarget {y: number;}+/** Extras on top of the shared MenuContent surface styling. */const MENU_CONTENT_PROPS = {- bg: 'bg.surfaceRaised',- borderColor: 'border.emphasis',- borderWidth: '1px',- color: 'fg.default',minW: '13rem',overflow: 'hidden',p: '0',- rounded: 'lg',- shadow: 'lg',} as const;/**@@ -108,7 +104,7 @@ export const ImageContextMenu = ({<Portal><Menu.Positioner>{image && (- <Menu.Content+ <MenuContent{...MENU_CONTENT_PROPS}maxH="min(28rem, calc(100vh - 2rem))"minW="16rem"@@ -131,7 +127,7 @@ export const ImageContextMenu = ({onRequestDeletion={requestDeletion}/>)}- </Menu.Content>+ </MenuContent>)}</Menu.Positioner></Portal>@@ -375,7 +371,7 @@ const ContextSubMenu = ({</Menu.TriggerItem><Portal><Menu.Positioner>- <Menu.Content {...MENU_CONTENT_PROPS} maxH="18rem" overflowY={scrollArea ? undefined : 'auto'} py="1">+ <MenuContent {...MENU_CONTENT_PROPS} maxH="18rem" overflowY={scrollArea ? undefined : 'auto'} py="1">{scrollArea ? (<ScrollArea.Root maxH="inherit" size="xs" variant="hover" w="full"><ScrollArea.Viewport maxH="inherit" w="full">@@ -388,7 +384,7 @@ const ContextSubMenu = ({) : (children)}- </Menu.Content>+ </MenuContent></Menu.Positioner></Portal></Menu.Root>diff --git a/invokeai/frontend/webv2/src/workbench/components/ui/ConfirmDialog.tsx b/invokeai/frontend/webv2/src/workbench/components/ui/ConfirmDialog.tsxnew file mode 100644index 0000000000..98583494d1--- /dev/null+++ b/invokeai/frontend/webv2/src/workbench/components/ui/ConfirmDialog.tsx@@ -0,0 +1,89 @@+import { Dialog, Portal, Stack, Text } from '@chakra-ui/react';+import { useState, type ReactNode } from 'react';++import { Button, CloseButton } from './Button';++/**+ * Controlled confirmation dialog for consequential actions. The confirm+ * button shows a pending state while `onConfirm` runs; errors are left to the+ * caller (usually surfaced as a notification) and the dialog closes either way.+ */+export const ConfirmDialog = ({+ body,+ confirmLabel,+ isDestructive = true,+ isOpen,+ onClose,+ onConfirm,+ title,+}: {+ body: ReactNode;+ confirmLabel: string;+ isDestructive?: boolean;+ isOpen: boolean;+ onClose: () => void;+ onConfirm: () => Promise<void> | void;+ title: string;+}) => {+ const [isPending, setIsPending] = useState(false);++ const handleConfirm = async () => {+ setIsPending(true);++ try {+ await onConfirm();+ } finally {+ setIsPending(false);+ onClose();+ }+ };++ return (+ <Dialog.Root+ open={isOpen}+ placement="center"+ role="alertdialog"+ size="sm"+ onOpenChange={(event) => {+ if (!event.open) {+ onClose();+ }+ }}+ >+ <Portal>+ <Dialog.Backdrop />+ <Dialog.Positioner>+ <Dialog.Content bg="bg.surface" borderColor="border.subtle" borderWidth="1px" color="fg.default">+ <Dialog.Header>+ <Dialog.Title fontSize="sm" fontWeight="700">+ {title}+ </Dialog.Title>+ </Dialog.Header>+ <Dialog.Body>+ <Stack gap="2">{typeof body === 'string' ? <Text fontSize="xs">{body}</Text> : body}</Stack>+ </Dialog.Body>+ <Dialog.Footer gap="2">+ <Button disabled={isPending} size="xs" variant="ghost" onClick={onClose}>+ Cancel+ </Button>+ <Button+ colorPalette={isDestructive ? 'red' : 'theme'}+ loading={isPending}+ size="xs"+ variant="solid"+ onClick={() => {+ void handleConfirm();+ }}+ >+ {confirmLabel}+ </Button>+ </Dialog.Footer>+ <Dialog.CloseTrigger asChild>+ <CloseButton color="fg.muted" size="sm" />+ </Dialog.CloseTrigger>+ </Dialog.Content>+ </Dialog.Positioner>+ </Portal>+ </Dialog.Root>+ );+};diff --git a/invokeai/frontend/webv2/src/workbench/components/ui/Field.tsx b/invokeai/frontend/webv2/src/workbench/components/ui/Field.tsxindex d41735d3c9..3bdb3268e9 100644--- a/invokeai/frontend/webv2/src/workbench/components/ui/Field.tsx+++ b/invokeai/frontend/webv2/src/workbench/components/ui/Field.tsx@@ -20,13 +20,25 @@ export const FieldLabel = ({ children }: { children: ReactNode }) => {export interface FieldProps extends Omit<StackProps, 'title'> {label: string;+ /** Validation error, shown in place of `helpText`. Mark the control itself invalid via `aria-invalid`. */+ error?: string | null;+ helpText?: string;children: ReactNode;}-/** A labelled form field: an uppercase label stacked above its control. */-export const Field = ({ children, label, ...rest }: FieldProps) => (+/** A labelled form field: an uppercase label stacked above its control, with an optional help/error line below. */+export const Field = ({ children, error, helpText, label, ...rest }: FieldProps) => (<Stack flex="1" gap="1.5" minW="0" {...rest}><FieldLabel>{label}</FieldLabel>{children}+ {error ? (+ <Text color="red.400" fontSize="2xs" role="alert">+ {error}+ </Text>+ ) : helpText ? (+ <Text color="fg.subtle" fontSize="2xs">+ {helpText}+ </Text>+ ) : null}</Stack>);diff --git a/invokeai/frontend/webv2/src/workbench/components/ui/Menu.tsx b/invokeai/frontend/webv2/src/workbench/components/ui/Menu.tsxnew file mode 100644index 0000000000..1ff4896fb7--- /dev/null+++ b/invokeai/frontend/webv2/src/workbench/components/ui/Menu.tsx@@ -0,0 +1,20 @@+import { Menu } from '@chakra-ui/react';+import type { ComponentProps } from 'react';++type MenuContentProps = ComponentProps<typeof Menu.Content>;++/**+ * Menu.Content with the workbench popover surface styling applied, so every+ * menu (filter menus, context menus, action menus) renders the same surface.+ */+export const MenuContent = (props: MenuContentProps) => (+ <Menu.Content+ bg="bg.surfaceRaised"+ borderColor="border.emphasis"+ borderWidth="1px"+ color="fg.default"+ rounded="lg"+ shadow="lg"+ {...props}+ />+);diff --git a/invokeai/frontend/webv2/src/workbench/components/ui/Scrollable.tsx b/invokeai/frontend/webv2/src/workbench/components/ui/Scrollable.tsxnew file mode 100644index 0000000000..4c2a4ea779--- /dev/null+++ b/invokeai/frontend/webv2/src/workbench/components/ui/Scrollable.tsx@@ -0,0 +1,28 @@+import { ScrollArea } from '@chakra-ui/react';+import type { ComponentProps, ReactNode } from 'react';++type ScrollAreaRootProps = ComponentProps<typeof ScrollArea.Root>;++/**+ * The workbench's standard scroll container: ScrollArea with hover-revealed+ * scrollbars and the content wrapper zag needs for correct thumb sizing.+ * Layout props (h, maxH, flex, ...) go to the root.+ */+export const Scrollable = ({+ children,+ label,+ ...rootProps+}: ScrollAreaRootProps & {+ children: ReactNode;+ /** Accessible name for the scroll viewport. */+ label?: string;+}) => (+ <ScrollArea.Root size="xs" variant="hover" {...rootProps}>+ <ScrollArea.Viewport aria-label={label} h="full" w="full">+ <ScrollArea.Content w="full">{children}</ScrollArea.Content>+ </ScrollArea.Viewport>+ <ScrollArea.Scrollbar>+ <ScrollArea.Thumb />+ </ScrollArea.Scrollbar>+ </ScrollArea.Root>+);diff --git a/invokeai/frontend/webv2/src/workbench/externalStore.ts b/invokeai/frontend/webv2/src/workbench/externalStore.tsnew file mode 100644index 0000000000..7b00877087--- /dev/null+++ b/invokeai/frontend/webv2/src/workbench/externalStore.ts@@ -0,0 +1,70 @@+import { useSyncExternalStore } from 'react';++/**+ * Minimal factory for module-level stores backed by `useSyncExternalStore`.+ * For backend-owned or session-lived state shared across widget surfaces+ * (model library, install jobs, starter catalog) where the workbench reducer+ * is the wrong home. See `models/modelsStore.ts` for the canonical usage.+ */++/** A bare listener channel, for auxiliary data living beside a snapshot. */+export interface ListenerChannel {+ subscribe: (listener: () => void) => () => void;+ notify: () => void;+}++export const createListenerChannel = (): ListenerChannel => {+ const listeners = new Set<() => void>();++ return {+ notify: () => {+ for (const listener of listeners) {+ listener();+ }+ },+ subscribe: (listener) => {+ listeners.add(listener);++ return () => {+ listeners.delete(listener);+ };+ },+ };+};++export interface ExternalStore<Snapshot extends object> extends ListenerChannel {+ getSnapshot: () => Snapshot;+ /** Replace the snapshot and notify subscribers. */+ setSnapshot: (next: Snapshot) => void;+ /** Merge a partial snapshot and notify subscribers. */+ patchSnapshot: (next: Partial<Snapshot>) => void;+ /** Replace without notifying — for data read imperatively, never rendered. */+ setSnapshotSilently: (next: Snapshot) => void;+ /** Subscribe the calling component to the snapshot. */+ useSnapshot: () => Snapshot;+}++export const createExternalStore = <Snapshot extends object>(initialSnapshot: Snapshot): ExternalStore<Snapshot> => {+ let snapshot = initialSnapshot;+ const channel = createListenerChannel();++ const getSnapshot = (): Snapshot => snapshot;++ const setSnapshot = (next: Snapshot): void => {+ snapshot = next;+ channel.notify();+ };++ const useSnapshot = (): Snapshot => useSyncExternalStore(channel.subscribe, getSnapshot);++ return {+ ...channel,+ getSnapshot,+ patchSnapshot: (next) => setSnapshot({ ...snapshot, ...next }),+ setSnapshot,+ setSnapshotSilently: (next) => {+ snapshot = next;+ },+ useSnapshot,+ };+};diff --git a/invokeai/frontend/webv2/src/workbench/useNotify.ts b/invokeai/frontend/webv2/src/workbench/useNotify.tsnew file mode 100644index 0000000000..5be3b140ce--- /dev/null+++ b/invokeai/frontend/webv2/src/workbench/useNotify.ts@@ -0,0 +1,28 @@+import { useMemo } from 'react';++import type { WorkbenchNotificationKind } from './types';+import { useWorkbench } from './WorkbenchContext';++/**+ * Shell notification helper: `recordNotice` dispatch wrapped in one hook so+ * widgets never hand-roll the same notifyError/notifySuccess plumbing.+ */+export interface Notify {+ success: (title: string, message?: string) => void;+ error: (title: string, message?: string) => void;+ info: (title: string, message?: string) => void;+}++export const useNotify = (): Notify => {+ const { dispatch } = useWorkbench();++ return useMemo(() => {+ const record =+ (kind: WorkbenchNotificationKind) =>+ (title: string, message?: string): void => {+ dispatch({ kind, message, title, type: 'recordNotice' });+ };++ return { error: record('error'), info: record('info'), success: record('success') };+ }, [dispatch]);+};