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(webv2): mount singleton widget hosts

joshistoaste87da4d48d04
diff --git a/invokeai/frontend/webv2/src/WorkbenchApp.tsx b/invokeai/frontend/webv2/src/WorkbenchApp.tsx
index 401f05ebcb..fcd3fdd6b8 100644
--- a/invokeai/frontend/webv2/src/WorkbenchApp.tsx
+++ b/invokeai/frontend/webv2/src/WorkbenchApp.tsx
@@ -4,6 +4,8 @@ import { WorkbenchShell } from '@workbench/shell';
import type { WorkbenchSearch } from './workbench/projects/session';
import { SessionExpiryGuard } from './workbench/auth/components/SessionExpiryGuard';
+import { WorkbenchHotkeyRuntime } from './workbench/hotkeys/WorkbenchHotkeyRuntime';
+import { WidgetHosts } from './workbench/widget-frame/WidgetHosts';
import { WorkbenchProvider } from './workbench/WorkbenchContext';
import { WorkbenchRuntime } from './workbench/WorkbenchRuntime';
import { WorkbenchSessionController } from './workbench/WorkbenchSessionController';
diff --git a/invokeai/frontend/webv2/src/workbench/types.ts b/invokeai/frontend/webv2/src/workbench/types.ts
index 497fd9eb18..593b63f21d 100644
--- a/invokeai/frontend/webv2/src/workbench/types.ts
+++ b/invokeai/frontend/webv2/src/workbench/types.ts
@@ -295,7 +295,7 @@ export interface WidgetManifest {
* Extra entries for the widget's shared header actions menu. Rendered inside
* the same menu as the universal graph actions, so widgets extend the frame
* menu instead of stacking their own. Render `Menu.Item`/`Menu.ItemGroup`
- * children only; own any dialogs from `headerActions` (always mounted).
+ * children only; use `host` for stable dialogs or file inputs.
*/
headerMenu?: WidgetHeaderMenu;
/** When set, the frame header shows a gear that opens this settings dialog section. */
diff --git a/invokeai/frontend/webv2/src/workbench/widget-frame/WidgetHosts.tsx b/invokeai/frontend/webv2/src/workbench/widget-frame/WidgetHosts.tsx
new file mode 100644
index 0000000000..29bf828f63
--- /dev/null
+++ b/invokeai/frontend/webv2/src/workbench/widget-frame/WidgetHosts.tsx
@@ -0,0 +1,11 @@
+import { getWidgetHosts } from '@workbench/widgetRegistry';
+
+export const WidgetHosts = () => (
+ <>
+ {getWidgetHosts().map((widget) => {
+ const Host = widget.manifest.host;
+
+ return Host ? <Host key={widget.manifest.id} /> : null;
+ })}
+ </>
+);
diff --git a/invokeai/frontend/webv2/src/workbench/widget-frame/createWidgetRuntime.ts b/invokeai/frontend/webv2/src/workbench/widget-frame/createWidgetRuntime.ts
index ba0d55b2b8..cb85dee190 100644
--- a/invokeai/frontend/webv2/src/workbench/widget-frame/createWidgetRuntime.ts
+++ b/invokeai/frontend/webv2/src/workbench/widget-frame/createWidgetRuntime.ts
@@ -1,4 +1,5 @@
import type {
+ WidgetHotkeyApi,
OpenWorkbenchWidgetOptions,
RegisteredWidget,
WidgetInstanceContract,
@@ -39,38 +40,49 @@ export const createWidgetRuntime = ({
instance: WidgetInstanceContract;
project: WidgetPlacementProject;
region: WorkbenchRegion;
-}): WidgetRuntimeApi => ({
- commands: commandApi,
- hotkeys: hotkeyApi,
- instanceId: instance.id,
- menus: menuApi,
- palette: commandPaletteApi,
- patchState: (values) => dispatch({ instanceId: instance.id, type: 'patchWidgetInstanceValues', values }),
- region,
- search: searchApi,
- setState: (values) => dispatch({ instanceId: instance.id, type: 'setWidgetInstanceValues', values }),
- state: instance.state.values,
- toolbars: toolbarApi,
- typeId: instance.typeId,
- workbench: {
- closeWidgetInstance: (instanceId) => {
- if (!isWidgetRegion(region)) {
- return { ok: false, reason: 'unsupported' };
- }
+}): WidgetRuntimeApi => {
+ const hotkeys: WidgetHotkeyApi = {
+ register: (hotkey) =>
+ hotkeyApi.register({
+ ...hotkey,
+ scope: hotkey.scope ?? 'widget',
+ source: { instanceId: instance.id, region, typeId: instance.typeId },
+ }),
+ };
- return closeWidgetPlacement({ dispatch, getWidgetById, instanceId, project, region });
- },
- openWidget: (typeId: WidgetTypeId, options?: OpenWorkbenchWidgetOptions) =>
- openWidgetPlacement({ dispatch, getWidgetsForRegion, options, typeId }),
- revealWidgetInstance: (instanceId) => {
- if (!isWidgetRegion(region)) {
- return { ok: false, reason: 'unsupported' };
- }
+ return {
+ commands: commandApi,
+ hotkeys,
+ instanceId: instance.id,
+ menus: menuApi,
+ palette: commandPaletteApi,
+ patchState: (values) => dispatch({ instanceId: instance.id, type: 'patchWidgetInstanceValues', values }),
+ region,
+ search: searchApi,
+ setState: (values) => dispatch({ instanceId: instance.id, type: 'setWidgetInstanceValues', values }),
+ state: instance.state.values,
+ toolbars: toolbarApi,
+ typeId: instance.typeId,
+ workbench: {
+ closeWidgetInstance: (instanceId) => {
+ if (!isWidgetRegion(region)) {
+ return { ok: false, reason: 'unsupported' };
+ }
+
+ return closeWidgetPlacement({ dispatch, getWidgetById, instanceId, project, region });
+ },
+ openWidget: (typeId: WidgetTypeId, options?: OpenWorkbenchWidgetOptions) =>
+ openWidgetPlacement({ dispatch, getWidgetsForRegion, options, typeId }),
+ revealWidgetInstance: (instanceId) => {
+ if (!isWidgetRegion(region)) {
+ return { ok: false, reason: 'unsupported' };
+ }
- return revealWidgetPlacement({ dispatch, instanceId, project, region });
+ return revealWidgetPlacement({ dispatch, instanceId, project, region });
+ },
},
- },
-});
+ };
+};
export const useWidgetRuntime = ({
dispatch,
diff --git a/invokeai/frontend/webv2/src/workbench/widgetRegistry.test.ts b/invokeai/frontend/webv2/src/workbench/widgetRegistry.test.ts
index 1971db5dfe..d01723a036 100644
--- a/invokeai/frontend/webv2/src/workbench/widgetRegistry.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/widgetRegistry.test.ts
@@ -2,7 +2,7 @@ import { describe, expect, it } from 'vitest';
import type { WidgetManifest } from './types';
-import { registerFirstPartyWidgets, registerWidgets } from './widgetRegistry';
+import { getWidgetHosts, registerFirstPartyWidgets, registerWidgets } from './widgetRegistry';
const TestIcon = () => null;
const TestView = () => null;
@@ -38,6 +38,10 @@ describe('widget registry', () => {
expect(widget.manifest.state.createInitial()).toEqual({});
});
+ it('exposes enabled widget singleton hosts', () => {
+ expect(getWidgetHosts().map((widget) => widget.manifest.id)).toContain('workflow');
+ });
+
it('disables invalid icon manifests', () => {
const [widget] = registerWidgets([createManifest({ icon: 'invalid' as unknown as WidgetManifest['icon'] })]);
diff --git a/invokeai/frontend/webv2/src/workbench/widgetRegistry.ts b/invokeai/frontend/webv2/src/workbench/widgetRegistry.ts
index 37c7de0161..2bc727239d 100644
--- a/invokeai/frontend/webv2/src/workbench/widgetRegistry.ts
+++ b/invokeai/frontend/webv2/src/workbench/widgetRegistry.ts
@@ -126,6 +126,11 @@ export const getWidgetsForRegion = (region: WidgetRegion): RegisteredWidget[] =>
widget.status !== 'hidden' && widget.manifest.allowedRegions.includes(region) && isWidgetAvailable(widget)
);
+export const getWidgetHosts = (): RegisteredWidget[] =>
+ registeredWidgets.filter(
+ (widget) => widget.status === 'enabled' && widget.manifest.host && isWidgetAvailable(widget)
+ );
+
export const getWidgetById = (widgetId: WidgetTypeId): RegisteredWidget | undefined =>
registeredWidgets.find((widget) => widget.manifest.id === widgetId);
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/workflow/WorkflowWidgetChrome.tsx b/invokeai/frontend/webv2/src/workbench/widgets/workflow/WorkflowWidgetChrome.tsx
index cdd732ad78..6c3d41806a 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/workflow/WorkflowWidgetChrome.tsx
+++ b/invokeai/frontend/webv2/src/workbench/widgets/workflow/WorkflowWidgetChrome.tsx
@@ -24,8 +24,6 @@ import { getWorkflowFlowInstance } from './editor/flowInstanceStore';
import { WorkflowLibraryDialog } from './library/WorkflowLibraryDialog';
import { copyWorkflowJson, downloadWorkflowJson } from './workflowTransfer';
import {
- claimWorkflowDialogHost,
- releaseWorkflowDialogHost,
requestWorkflowImport,
setAddNodeOpen,
setNewWorkflowConfirmOpen,
@@ -129,42 +127,103 @@ export const WorkflowMenuItems = (_props: WidgetViewProps) => {
export const WorkflowHeaderActions = ({ region }: WidgetViewProps) => {
const graphHistory = useActiveProjectSelector((project) => project.graphHistory);
+ const dispatch = useWorkbenchDispatch();
+ const restorableHistory = graphHistory.filter((entry) => entry.document);
+ const historyTriggerId = useId();
+
+ return (
+ <HStack gap="0.5">
+ {region === 'center' ? (
+ <Tooltip content="Add node">
+ <IconButton
+ aria-label="Add node"
+ color="fg.muted"
+ size="2xs"
+ variant="ghost"
+ onClick={() => setAddNodeOpen(true)}
+ >
+ <Icon as={PlusIcon} boxSize="3.5" />
+ </IconButton>
+ </Tooltip>
+ ) : null}
+ <Tooltip content="Workflow library">
+ <IconButton
+ aria-label="Workflow library"
+ color="fg.muted"
+ size="2xs"
+ variant="ghost"
+ onClick={() => setWorkflowLibraryOpen(true)}
+ >
+ <Icon as={LibraryIcon} boxSize="3.5" />
+ </IconButton>
+ </Tooltip>
+ <Menu.Root ids={{ trigger: historyTriggerId }} positioning={{ placement: 'bottom-end' }}>
+ <Tooltip content="Graph history snapshots" ids={{ trigger: historyTriggerId }}>
+ <Menu.Trigger asChild>
+ <IconButton
+ aria-label="Graph history snapshots"
+ color="fg.muted"
+ disabled={restorableHistory.length === 0}
+ size="2xs"
+ variant="ghost"
+ >
+ <Icon as={HistoryIcon} boxSize="3.5" />
+ </IconButton>
+ </Menu.Trigger>
+ </Tooltip>
+ <Portal>
+ <Menu.Positioner>
+ <Menu.Content maxH="18rem" minW="16rem" overflowY="auto">
+ <Menu.ItemGroup>
+ <Menu.ItemGroupLabel color="fg.subtle" fontSize="2xs" textTransform="uppercase">
+ Graph History Snapshots
+ </Menu.ItemGroupLabel>
+ {restorableHistory.map((entry) => (
+ <Menu.Item
+ key={entry.id}
+ value={entry.id}
+ onClick={() => dispatch({ snapshotId: entry.id, type: 'restoreProjectGraphSnapshot' })}
+ >
+ <Stack gap="0" minW="0">
+ <Menu.ItemText fontSize="xs" truncate>
+ {entry.label}
+ </Menu.ItemText>
+ <Text color="fg.subtle" fontSize="2xs">
+ {new Date(entry.createdAt).toLocaleString()}
+ </Text>
+ </Stack>
+ </Menu.Item>
+ ))}
+ </Menu.ItemGroup>
+ </Menu.Content>
+ </Menu.Positioner>
+ </Portal>
+ </Menu.Root>
+ </HStack>
+ );
+};
+
+export const WorkflowDialogHost = () => {
const dispatch = useWorkbenchDispatch();
const notify = useNotify();
const fileInputRef = useRef<HTMLInputElement | null>(null);
const {
addNodeConnection,
addNodePosition,
- dialogHostId,
importRequestCount,
isAddNodeOpen,
isLibraryOpen,
isNewWorkflowConfirmOpen,
} = workflowUiStore.useSnapshot();
- const restorableHistory = graphHistory.filter((entry) => entry.document);
-
- // Both workflow surfaces can be mounted at once; exactly one (the first to
- // mount) hosts the shared dialogs and the import file input.
- const hostId = useId();
- const historyTriggerId = useId();
- const isDialogHost = dialogHostId === hostId;
const lastImportRequestRef = useRef(importRequestCount);
useEffect(() => {
- claimWorkflowDialogHost(hostId);
-
- return () => {
- releaseWorkflowDialogHost(hostId);
- };
- }, [hostId]);
-
- useEffect(() => {
- if (isDialogHost && importRequestCount > lastImportRequestRef.current) {
+ if (importRequestCount > lastImportRequestRef.current) {
fileInputRef.current?.click();
}
lastImportRequestRef.current = importRequestCount;
- }, [importRequestCount, isDialogHost]);
+ }, [importRequestCount]);
const getInsertPosition = (): XYPosition => {
if (addNodePosition) {
@@ -320,117 +379,46 @@ export const WorkflowHeaderActions = ({ region }: WidgetViewProps) => {
};
return (
- <HStack gap="0.5">
- {region === 'center' ? (
- <Tooltip content="Add node">
- <IconButton
- aria-label="Add node"
- color="fg.muted"
- size="2xs"
- variant="ghost"
- onClick={() => setAddNodeOpen(true)}
- >
- <Icon as={PlusIcon} boxSize="3.5" />
- </IconButton>
- </Tooltip>
- ) : null}
- <Tooltip content="Workflow library">
- <IconButton
- aria-label="Workflow library"
- color="fg.muted"
- size="2xs"
- variant="ghost"
- onClick={() => setWorkflowLibraryOpen(true)}
- >
- <Icon as={LibraryIcon} boxSize="3.5" />
- </IconButton>
- </Tooltip>
- <Menu.Root ids={{ trigger: historyTriggerId }} positioning={{ placement: 'bottom-end' }}>
- <Tooltip content="Graph history snapshots" ids={{ trigger: historyTriggerId }}>
- <Menu.Trigger asChild>
- <IconButton
- aria-label="Graph history snapshots"
- color="fg.muted"
- disabled={restorableHistory.length === 0}
- size="2xs"
- variant="ghost"
- >
- <Icon as={HistoryIcon} boxSize="3.5" />
- </IconButton>
- </Menu.Trigger>
- </Tooltip>
- <Portal>
- <Menu.Positioner>
- <Menu.Content maxH="18rem" minW="16rem" overflowY="auto">
- <Menu.ItemGroup>
- <Menu.ItemGroupLabel color="fg.subtle" fontSize="2xs" textTransform="uppercase">
- Graph History Snapshots
- </Menu.ItemGroupLabel>
- {restorableHistory.map((entry) => (
- <Menu.Item
- key={entry.id}
- value={entry.id}
- onClick={() => dispatch({ snapshotId: entry.id, type: 'restoreProjectGraphSnapshot' })}
- >
- <Stack gap="0" minW="0">
- <Menu.ItemText fontSize="xs" truncate>
- {entry.label}
- </Menu.ItemText>
- <Text color="fg.subtle" fontSize="2xs">
- {new Date(entry.createdAt).toLocaleString()}
- </Text>
- </Stack>
- </Menu.Item>
- ))}
- </Menu.ItemGroup>
- </Menu.Content>
- </Menu.Positioner>
- </Portal>
- </Menu.Root>
-
- {isDialogHost ? (
- <>
- <input
- ref={fileInputRef}
- accept=".json,application/json"
- hidden
- type="file"
- onChange={(event: ChangeEvent<HTMLInputElement>) => {
- const file = event.currentTarget.files?.[0];
-
- event.currentTarget.value = '';
-
- if (file) {
- importFile(file);
- }
- }}
- />
- <AddNodeDialog
- connectionFilter={addNodeConnection}
- isOpen={isAddNodeOpen}
- onAddCurrentImage={addCurrentImage}
- onAddConnector={addConnector}
- onAddNode={addNode}
- onAddNote={addNote}
- onOpenChange={setAddNodeOpen}
- />
- <WorkflowLibraryDialog isOpen={isLibraryOpen} onOpenChange={setWorkflowLibraryOpen} />
- <ConfirmDialog
- body="Replace the project graph with an empty workflow? The current graph is saved to graph history first."
- confirmLabel="New workflow"
- isOpen={isNewWorkflowConfirmOpen}
- title="New workflow"
- onClose={() => setNewWorkflowConfirmOpen(false)}
- onConfirm={() => {
- dispatch({
- document: createProjectGraph(createWorkflowId('project-graph')),
- label: 'New workflow',
- type: 'replaceProjectGraph',
- });
- }}
- />
- </>
- ) : null}
- </HStack>
+ <>
+ <input
+ ref={fileInputRef}
+ accept=".json,application/json"
+ hidden
+ type="file"
+ onChange={(event: ChangeEvent<HTMLInputElement>) => {
+ const file = event.currentTarget.files?.[0];
+
+ event.currentTarget.value = '';
+
+ if (file) {
+ importFile(file);
+ }
+ }}
+ />
+ <AddNodeDialog
+ connectionFilter={addNodeConnection}
+ isOpen={isAddNodeOpen}
+ onAddCurrentImage={addCurrentImage}
+ onAddConnector={addConnector}
+ onAddNode={addNode}
+ onAddNote={addNote}
+ onOpenChange={setAddNodeOpen}
+ />
+ <WorkflowLibraryDialog isOpen={isLibraryOpen} onOpenChange={setWorkflowLibraryOpen} />
+ <ConfirmDialog
+ body="Replace the project graph with an empty workflow? The current graph is saved to graph history first."
+ confirmLabel="New workflow"
+ isOpen={isNewWorkflowConfirmOpen}
+ title="New workflow"
+ onClose={() => setNewWorkflowConfirmOpen(false)}
+ onConfirm={() => {
+ dispatch({
+ document: createProjectGraph(createWorkflowId('project-graph')),
+ label: 'New workflow',
+ type: 'replaceProjectGraph',
+ });
+ }}
+ />
+ </>
);
};
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/workflow/index.ts b/invokeai/frontend/webv2/src/workbench/widgets/workflow/index.ts
index b8f7de6d4a..327bbe10e1 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/workflow/index.ts
+++ b/invokeai/frontend/webv2/src/workbench/widgets/workflow/index.ts
@@ -2,7 +2,12 @@ import type { WidgetManifest } from '@workbench/types';
import { WorkflowIcon } from 'lucide-react';
-import { WorkflowHeaderActions, WorkflowMenuItems, WorkflowWidgetLabel } from './WorkflowWidgetChrome';
+import {
+ WorkflowDialogHost,
+ WorkflowHeaderActions,
+ WorkflowMenuItems,
+ WorkflowWidgetLabel,
+} from './WorkflowWidgetChrome';
import { WorkflowWidgetView } from './WorkflowWidgetView';
export const workflowWidgetManifest: WidgetManifest = {
@@ -15,6 +20,7 @@ export const workflowWidgetManifest: WidgetManifest = {
sourceId: 'project-graph',
surfaces: ['center', 'left', 'bottom'],
},
+ host: WorkflowDialogHost,
headerActions: WorkflowHeaderActions,
headerMenu: WorkflowMenuItems,
icon: WorkflowIcon,
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/workflow/workflowUiStore.ts b/invokeai/frontend/webv2/src/workbench/widgets/workflow/workflowUiStore.ts
index 4a11ca35ca..b0f53e52ac 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/workflow/workflowUiStore.ts
+++ b/invokeai/frontend/webv2/src/workbench/widgets/workflow/workflowUiStore.ts
@@ -31,41 +31,17 @@ export interface WorkflowUiSnapshot {
isNewWorkflowConfirmOpen: boolean;
/** Bumped to ask the dialog host to open the JSON file picker. */
importRequestCount: number;
- /**
- * The workflow surface currently responsible for rendering the shared
- * dialogs and file input. Both surfaces (center editor, left panel) can be
- * mounted at once; exactly one — the first to mount — hosts them.
- */
- dialogHostId: string | null;
}
export const workflowUiStore = createExternalStore<WorkflowUiSnapshot>({
addNodeConnection: null,
addNodePosition: null,
- dialogHostId: null,
importRequestCount: 0,
isAddNodeOpen: false,
isLibraryOpen: false,
isNewWorkflowConfirmOpen: false,
});
-const dialogHostIds: string[] = [];
-
-export const claimWorkflowDialogHost = (hostId: string): void => {
- dialogHostIds.push(hostId);
- workflowUiStore.patchSnapshot({ dialogHostId: dialogHostIds[0] ?? null });
-};
-
-export const releaseWorkflowDialogHost = (hostId: string): void => {
- const index = dialogHostIds.indexOf(hostId);
-
- if (index !== -1) {
- dialogHostIds.splice(index, 1);
- }
-
- workflowUiStore.patchSnapshot({ dialogHostId: dialogHostIds[0] ?? null });
-};
-
export const setWorkflowLibraryOpen = (isOpen: boolean): void => {
workflowUiStore.patchSnapshot({ isLibraryOpen: isOpen });
};