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): unify layout widget set
joshistoast5fab9d084a93
diff --git a/invokeai/frontend/webv2/src/app/BootWidgetHintController.tsx b/invokeai/frontend/webv2/src/app/BootWidgetHintController.tsxindex e64261dfdf..5c4ddf4e7a 100644--- a/invokeai/frontend/webv2/src/app/BootWidgetHintController.tsx+++ b/invokeai/frontend/webv2/src/app/BootWidgetHintController.tsx@@ -1,4 +1,5 @@-import { getBootWidgetTypeIds, writeBootWidgetHint } from '@workbench/bootWidgetPreload';+import { writeBootWidgetHint } from '@workbench/bootWidgetPreload';+import { getLayoutWidgetTypeIds } from '@workbench/layoutWidgetSet';import { shallowEqual, useActiveProjectSelector } from '@workbench/WorkbenchContext';import { useEffect } from 'react';@@ -10,7 +11,7 @@ import { useEffect } from 'react';* Renders nothing.*/export const BootWidgetHintController = () => {- const typeIds = useActiveProjectSelector(getBootWidgetTypeIds, shallowEqual);+ const typeIds = useActiveProjectSelector(getLayoutWidgetTypeIds, shallowEqual);useEffect(() => {writeBootWidgetHint(typeIds);diff --git a/invokeai/frontend/webv2/src/architecture/workbenchOwnershipManifest.json b/invokeai/frontend/webv2/src/architecture/workbenchOwnershipManifest.jsonindex 40594cb5f6..20ab0504b0 100644--- a/invokeai/frontend/webv2/src/architecture/workbenchOwnershipManifest.json+++ b/invokeai/frontend/webv2/src/architecture/workbenchOwnershipManifest.json@@ -114,6 +114,7 @@"layoutPresetRouting.ts": "workbench","layoutPresetSnapshots.ts": "workbench","layoutPresets.ts": "workbench",+ "layoutWidgetSet.ts": "workbench","panZoom.ts": "workbench","performanceMarks.ts": "workbench","persistence.ts": "workbench",diff --git a/invokeai/frontend/webv2/src/workbench/bootWidgetPreload.test.ts b/invokeai/frontend/webv2/src/workbench/bootWidgetPreload.test.tsindex 4f51816a15..de0d76b4f7 100644--- a/invokeai/frontend/webv2/src/workbench/bootWidgetPreload.test.ts+++ b/invokeai/frontend/webv2/src/workbench/bootWidgetPreload.test.ts@@ -1,7 +1,8 @@import { describe, expect, it, vi } from 'vitest';-import { getBootWidgetTypeIds, readBootWidgetHint, writeBootWidgetHint } from './bootWidgetPreload';+import { readBootWidgetHint, writeBootWidgetHint } from './bootWidgetPreload';import { defaultLayoutPreset } from './layoutPresets';+import { getLayoutWidgetTypeIds } from './layoutWidgetSet';const storage = new Map<string, string>();@@ -18,7 +19,7 @@ const HINT_KEY = 'invokeai:v7:webv2:boot-widgets';describe('boot widget preload', () => {it('derives the default layout boot set: active instances plus every bottom item', () => {- const typeIds = getBootWidgetTypeIds(defaultLayoutPreset.snapshot);+ const typeIds = getLayoutWidgetTypeIds(defaultLayoutPreset.snapshot);for (const region of Object.values(defaultLayoutPreset.snapshot.widgetRegions)) {const activeTypeId = defaultLayoutPreset.snapshot.widgetInstances[region.activeInstanceId]?.typeId;@@ -36,9 +37,6 @@ describe('boot widget preload', () => {expect(typeIds).toContain(typeId);}}-- expect(typeIds).toEqual([...typeIds].sort());- expect(new Set(typeIds).size).toBe(typeIds.length);});it('round-trips the hint through storage', () => {diff --git a/invokeai/frontend/webv2/src/workbench/bootWidgetPreload.ts b/invokeai/frontend/webv2/src/workbench/bootWidgetPreload.tsindex 16167141b1..b591f0f436 100644--- a/invokeai/frontend/webv2/src/workbench/bootWidgetPreload.ts+++ b/invokeai/frontend/webv2/src/workbench/bootWidgetPreload.ts@@ -1,7 +1,8 @@import type { WidgetTypeId } from '@workbench/widgetContracts';import { defaultLayoutPreset } from './layoutPresets';-import { getWidgetHosts, preloadWidget } from './widgetRegistry';+import { getLayoutWidgetTypeIds } from './layoutWidgetSet';+import { getWidgetHosts, warmWidgets } from './widgetRegistry';/*** The widget implementations a boot will render are only knowable for certain@@ -21,38 +22,6 @@ const BOOT_WIDGET_HINT_STORAGE_KEY = 'invokeai:v7:webv2:boot-widgets';/** Sanity cap so a corrupt hint cannot fan out unbounded chunk fetches. */const BOOT_WIDGET_HINT_LIMIT = 32;-/**- * The subset of a project or preset snapshot the boot preloader reads. Both- * `Project` and `LayoutPresetSnapshot` satisfy it structurally.- */-interface BootWidgetLayout {- widgetInstances: Record<string, { typeId: WidgetTypeId }>;- widgetRegions: Record<string, { activeInstanceId: string; instanceIds: string[] }>;-}--/**- * The widget types a layout renders at boot: each region's active instance,- * plus every placed bottom instance — the status bar mounts all of its items- * as compact widgets, not just the active one.- */-export const getBootWidgetTypeIds = (layout: BootWidgetLayout): WidgetTypeId[] => {- const typeIds = new Set<WidgetTypeId>();-- for (const [region, state] of Object.entries(layout.widgetRegions)) {- const instanceIds = region === 'bottom' ? [state.activeInstanceId, ...state.instanceIds] : [state.activeInstanceId];-- for (const instanceId of instanceIds) {- const typeId = layout.widgetInstances[instanceId]?.typeId;-- if (typeId) {- typeIds.add(typeId);- }- }- }-- return [...typeIds].sort();-};-export const readBootWidgetHint = (): WidgetTypeId[] | null => {try {const raw = window.localStorage.getItem(BOOT_WIDGET_HINT_STORAGE_KEY);@@ -96,7 +65,5 @@ export const preloadBootWidgets = (): void => {widget.host?.preload();}- for (const typeId of readBootWidgetHint() ?? getBootWidgetTypeIds(defaultLayoutPreset.snapshot)) {- preloadWidget(typeId);- }+ warmWidgets(readBootWidgetHint() ?? getLayoutWidgetTypeIds(defaultLayoutPreset.snapshot));};diff --git a/invokeai/frontend/webv2/src/workbench/layoutPresetActivation.ts b/invokeai/frontend/webv2/src/workbench/layoutPresetActivation.tsindex 599e55c792..fc142f3e89 100644--- a/invokeai/frontend/webv2/src/workbench/layoutPresetActivation.ts+++ b/invokeai/frontend/webv2/src/workbench/layoutPresetActivation.ts@@ -1,6 +1,7 @@import type { LayoutPreset, LayoutPresetId } from './layoutContracts';-import { loadWidget, preloadWidget } from './widgetRegistry';+import { getLayoutWidgetTypeIds } from './layoutWidgetSet';+import { loadWidgets, warmWidgets } from './widgetRegistry';/*** How long an activation waits for widget implementations before applying the@@ -62,21 +63,8 @@ export const createLayoutPresetActivator = ({};export const preloadLayoutPresetWidgets = (preset: LayoutPreset): void => {- for (const region of Object.values(preset.snapshot.widgetRegions)) {- const typeId = preset.snapshot.widgetInstances[region.activeInstanceId]?.typeId;-- if (typeId) {- preloadWidget(typeId);- }- }+ warmWidgets(getLayoutWidgetTypeIds(preset.snapshot));};export const loadLayoutPresetWidgets = (preset: LayoutPreset): Promise<unknown> =>- Promise.allSettled(- Object.values(preset.snapshot.widgetRegions).flatMap((region) => {- const typeId = preset.snapshot.widgetInstances[region.activeInstanceId]?.typeId;- const pending = typeId ? loadWidget(typeId) : null;-- return pending ? [pending] : [];- })- );+ loadWidgets(getLayoutWidgetTypeIds(preset.snapshot));diff --git a/invokeai/frontend/webv2/src/workbench/layoutWidgetSet.test.ts b/invokeai/frontend/webv2/src/workbench/layoutWidgetSet.test.tsnew file mode 100644index 0000000000..8ccd7c5829--- /dev/null+++ b/invokeai/frontend/webv2/src/workbench/layoutWidgetSet.test.ts@@ -0,0 +1,48 @@+import { describe, expect, it } from 'vitest';++import { layoutPresets } from './layoutPresets';+import { getLayoutWidgetTypeIds } from './layoutWidgetSet';++describe('layout widget set', () => {+ it('includes every bottom instance, not just the active one', () => {+ for (const preset of layoutPresets) {+ const typeIds = getLayoutWidgetTypeIds(preset.snapshot);++ for (const instanceId of preset.snapshot.widgetRegions.bottom.instanceIds) {+ const typeId = preset.snapshot.widgetInstances[instanceId]?.typeId;++ if (typeId) {+ expect(typeIds).toContain(typeId);+ }+ }+ }+ });++ it('orders region actives ahead of the rest of the bottom strip', () => {+ const preset = layoutPresets.find(({ id }) => id === 'edit')!;+ const typeIds = getLayoutWidgetTypeIds(preset.snapshot);+ const centerTypeId = preset.snapshot.widgetInstances[preset.snapshot.widgetRegions.center.activeInstanceId]!.typeId;++ expect(typeIds[0]).toBe(centerTypeId);+ expect(typeIds.indexOf('server-status')).toBeGreaterThan(typeIds.indexOf(centerTypeId));+ });++ it('deduplicates a type placed in more than one region', () => {+ const preset = layoutPresets.find(({ id }) => id === 'compose')!;+ const typeIds = getLayoutWidgetTypeIds(preset.snapshot);++ expect(new Set(typeIds).size).toBe(typeIds.length);+ });++ it('skips instance ids that have no instance', () => {+ const typeIds = getLayoutWidgetTypeIds({+ widgetInstances: { real: { typeId: 'gallery' } },+ widgetRegions: {+ bottom: { activeInstanceId: 'missing', instanceIds: ['missing', 'real'] },+ center: { activeInstanceId: 'missing', instanceIds: ['missing'] },+ },+ });++ expect(typeIds).toEqual(['gallery']);+ });+});diff --git a/invokeai/frontend/webv2/src/workbench/layoutWidgetSet.ts b/invokeai/frontend/webv2/src/workbench/layoutWidgetSet.tsnew file mode 100644index 0000000000..3787ed13cf--- /dev/null+++ b/invokeai/frontend/webv2/src/workbench/layoutWidgetSet.ts@@ -0,0 +1,54 @@+import type { WidgetTypeId } from '@workbench/widgetContracts';++/**+ * The subset of a project or preset snapshot that determines which widgets a+ * layout renders. Both `Project` and `LayoutPresetSnapshot` satisfy it+ * structurally.+ */+export interface LayoutWidgetSource {+ widgetInstances: Record<string, { typeId: WidgetTypeId }>;+ widgetRegions: Record<string, { activeInstanceId: string; instanceIds: string[] }>;+}++/**+ * Regions in the order a boot reveals them, so callers that care about+ * priority get it for free and callers that do not are unaffected.+ */+const REGION_ORDER = ['center', 'left', 'right', 'bottom'];++/**+ * Every widget type a layout renders: each region's active instance, plus every+ * placed bottom instance — the status bar mounts all of its items as compact+ * widgets, not just the active one.+ *+ * Single definition on purpose. The boot preloader and the preset activation+ * gate previously disagreed: activation only awaited region actives, so a+ * switch never gated on four of the five bottom instances every built-in preset+ * places, and the atomic reveal the deadline buys was incomplete.+ */+export const getLayoutWidgetTypeIds = (layout: LayoutWidgetSource): WidgetTypeId[] => {+ const typeIds = new Set<WidgetTypeId>();+ const regions = Object.entries(layout.widgetRegions).sort(+ (left, right) => REGION_ORDER.indexOf(left[0]) - REGION_ORDER.indexOf(right[0])+ );++ const add = (instanceId: string): void => {+ const typeId = layout.widgetInstances[instanceId]?.typeId;++ if (typeId) {+ typeIds.add(typeId);+ }+ };++ for (const [, state] of regions) {+ add(state.activeInstanceId);+ }++ for (const [region, state] of regions) {+ if (region === 'bottom') {+ state.instanceIds.forEach(add);+ }+ }++ return [...typeIds];+};diff --git a/invokeai/frontend/webv2/src/workbench/widgetRegistry.ts b/invokeai/frontend/webv2/src/workbench/widgetRegistry.tsindex f7f27796cd..41b1a54ff9 100644--- a/invokeai/frontend/webv2/src/workbench/widgetRegistry.ts+++ b/invokeai/frontend/webv2/src/workbench/widgetRegistry.ts@@ -156,31 +156,42 @@ export const getWidgetHosts = (): RegisteredWidget[] =>export const getWidgetById = (widgetId: WidgetTypeId): RegisteredWidget | undefined =>registeredWidgets.find((widget) => widget.manifest.id === widgetId);-/** Starts a cached implementation load for an explicit user intent. */-export const preloadWidget = (widgetId: WidgetTypeId): void => {- const widget = getWidgetById(widgetId);+/** Starts cached implementation loads. Unknown or disabled ids are skipped. */+export const warmWidgets = (typeIds: readonly WidgetTypeId[]): void => {+ for (const typeId of typeIds) {+ const widget = getWidgetById(typeId);- if (widget?.status === 'enabled') {- widget.implementation.preload();+ if (widget?.status === 'enabled') {+ widget.implementation.preload();+ }}};/**- * Like {@link preloadWidget}, but resolves when the implementation is in hand.+ * Like {@link warmWidgets}, but resolves when the implementations are in hand.*- * Callers that are about to make a widget appear use this to get the module+ * Callers that are about to make widgets appear use this to get the modules* BEFORE committing the state change, so the render never suspends. Suspending* is not free even when the chunk is already downloaded: showing a fallback* makes React withhold the resolved content for `FALLBACK_THROTTLE_MS` (300ms)* to avoid a flash, which measured as the entire cost of a layout switch.- *- * Returns `null` for an unknown or disabled widget, so callers can skip it.*/-export const loadWidget = (widgetId: WidgetTypeId): Promise<unknown> | null => {- const widget = getWidgetById(widgetId);+export const loadWidgets = (typeIds: readonly WidgetTypeId[]): Promise<unknown> =>+ Promise.allSettled(+ typeIds.flatMap((typeId) => {+ const widget = getWidgetById(typeId);- return widget?.status === 'enabled' ? widget.implementation.load() : null;-};+ return widget?.status === 'enabled' ? [widget.implementation.load()] : [];+ })+ );++/** Whether every renderable widget in the set is already in memory. */+export const areWidgetsLoaded = (typeIds: readonly WidgetTypeId[]): boolean =>+ typeIds.every((typeId) => {+ const widget = getWidgetById(typeId);++ return widget?.status !== 'enabled' || widget.implementation.getStatus() === 'loaded';+ });export const widgetRegistrationFailures = registeredWidgets.flatMap((widget) =>widget.failure ? [widget.failure] : []diff --git a/invokeai/frontend/webv2/src/workbench/widgets/image-map/ImageMapWidgetView.tsx b/invokeai/frontend/webv2/src/workbench/widgets/image-map/ImageMapWidgetView.tsxindex b1fdb6e70e..2e802591bf 100644--- a/invokeai/frontend/webv2/src/workbench/widgets/image-map/ImageMapWidgetView.tsx+++ b/invokeai/frontend/webv2/src/workbench/widgets/image-map/ImageMapWidgetView.tsx@@ -69,7 +69,7 @@ export const ImageMapWidgetView = (_props: WidgetViewProps) => {// the whole widget, so suspending on the plotly chunk replaced the entire// panel — header and actions menu included — with a skeleton frame, and// then held the resolved content for React's fallback throttle on top. It- // also sits above `loadWidget`, which preloads only the implementation+ // also sits above `loadWidgets`, which preloads only the implementation// chunk and cannot reach this nested import, so a preset switch onto an// already-loaded map suspended anyway. Confining it here keeps the frame// mounted and the spinner where the plot will appear.