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
fix(ui): coordinate preset activation
joshistoast785f13e66c5e
diff --git a/invokeai/frontend/webv2/src/workbench/hotkeys/firstPartyCommands.test.ts b/invokeai/frontend/webv2/src/workbench/hotkeys/firstPartyCommands.test.tsindex 215ad7989d..69e2771047 100644--- a/invokeai/frontend/webv2/src/workbench/hotkeys/firstPartyCommands.test.ts+++ b/invokeai/frontend/webv2/src/workbench/hotkeys/firstPartyCommands.test.ts@@ -1,15 +1,31 @@import { describe, expect, it } from 'vitest';+import { createInitialWorkbenchState } from '../workbenchState';import { firstPartyHotkeyCatalog } from './catalog';import {FIRST_PARTY_APP_COMMAND_IDS,FIRST_PARTY_COMMAND_IDS,FIRST_PARTY_IMAGE_RECALL_COMMAND_IDS,+ getLayoutPresetCommands,} from './firstPartyCommands';const centrallyOwnedCommandIds = new Set(FIRST_PARTY_COMMAND_IDS);describe('first-party hotkey commands', () => {+ it('uses the account names for built-in layout commands', () => {+ const initial = createInitialWorkbenchState();+ const account = {+ ...initial.account,+ layoutPresetMetadataOverrides: { compose: { label: 'Writing' } },+ };++ expect(getLayoutPresetCommands(account).map(({ title }) => title)).toEqual([+ 'Writing layout',+ 'Edit layout',+ 'Automate layout',+ ]);+ });+it('registers handlers for implemented app and shared image recall commands', () => {const sharedImageRecallCommands = new Set(['gallery.remix',diff --git a/invokeai/frontend/webv2/src/workbench/hotkeys/firstPartyCommands.ts b/invokeai/frontend/webv2/src/workbench/hotkeys/firstPartyCommands.tsindex 1c9844acb7..7ba84319af 100644--- a/invokeai/frontend/webv2/src/workbench/hotkeys/firstPartyCommands.ts+++ b/invokeai/frontend/webv2/src/workbench/hotkeys/firstPartyCommands.ts@@ -1,5 +1,6 @@import type { ImageRecallKind } from '@workbench/image-actions/imageRecall';import type { ResultDestination } from '@workbench/invocationContracts';+import type { AccountState } from '@workbench/projectContracts';import { invalidateGallery } from '@features/gallery/queries';import {@@ -19,24 +20,39 @@ import {} from '@platform/state/accountLifecycle';import { useQueryClient } from '@tanstack/react-query';import { submitActiveInvocation } from '@workbench/activeInvocationSubmission';-import { createLayoutPresetActivator, loadLayoutPresetWidgets } from '@workbench/layoutPresetActivation';-import { builtInLayoutPresetDescriptors, getLayoutPreset } from '@workbench/layoutPresets';+import { builtInLayoutPresetDescriptors } from '@workbench/layoutPresets';+import { resolveSavedLayoutPreset } from '@workbench/layoutPresetSnapshots';import { toggleCommandPalette } from '@workbench/palette/paletteStore';import { getWorkbenchPreferences } from '@workbench/settings/store';import { openProjectSwitcher } from '@workbench/shell/topbar/projectSwitcherStore';import { openWidgetPlacement } from '@workbench/widgetPlacementCommands';import { getWidgetsForRegion } from '@workbench/widgetRegistry';import { getProjectWidgetValues } from '@workbench/widgetState';-import { useWorkbenchCommands, useWorkbenchExtensions, useWorkbenchQueries } from '@workbench/WorkbenchContext';-import { useEffect, useEffectEvent, useMemo } from 'react';+import {+ useWorkbenchCommands,+ useWorkbenchExtensions,+ useWorkbenchQueries,+ useWorkbenchSelector,+} from '@workbench/WorkbenchContext';+import { useEffect, useEffectEvent } from 'react';/** ⌥1 / ⌥2 / ⌥3 — the three shipped layout presets, in strip order. */const layoutPresetCommands = builtInLayoutPresetDescriptors.map(({ hotkeyId, preset }) => ({id: `app.${hotkeyId}`,presetId: preset.id,- title: `${preset.label} layout`,}));+export const getLayoutPresetCommands = (account: AccountState) =>+ layoutPresetCommands.map((command) => ({+ ...command,+ title: `${resolveSavedLayoutPreset(account, command.presetId).label} layout`,+ }));++const areLayoutPresetCommandsEqual = (+ left: ReturnType<typeof getLayoutPresetCommands>,+ right: ReturnType<typeof getLayoutPresetCommands>+): boolean => left.length === right.length && left.every((command, index) => command.title === right[index]?.title);+const imageRecallCommands: Record<string, ImageRecallKind> = {'gallery.remix': 'remix','viewer.recallAll': 'all',@@ -87,9 +103,9 @@ export const useRegisterFirstPartyCommands = () => {const queries = useWorkbenchQueries();const queryClient = useQueryClient();const { layout, notifications, queue, widgets } = commands;- const activateLayoutPreset = useMemo(- () => createLayoutPresetActivator({ apply: layout.applyPreset, load: loadLayoutPresetWidgets }),- [layout.applyPreset]+ const currentLayoutPresetCommands = useWorkbenchSelector(+ (snapshot) => getLayoutPresetCommands(snapshot.account),+ areLayoutPresetCommandsEqual);useInvocationTemplatesSelector((snapshot) => snapshot.status);@@ -187,8 +203,8 @@ export const useRegisterFirstPartyCommands = () => {id: 'app.saveLayoutPreset',title: 'Save changes to the active layout preset',}),- ...layoutPresetCommands.map(({ id, presetId, title }) =>- commandApi.register({ handler: () => void activateLayoutPreset(getLayoutPreset(presetId)), id, title })+ ...currentLayoutPresetCommands.map(({ id, presetId, title }) =>+ commandApi.register({ handler: () => void layout.activatePreset(presetId), id, title })),commandApi.register({handler: () => {@@ -360,5 +376,5 @@ export const useRegisterFirstPartyCommands = () => {return () => {disposers.forEach((dispose) => dispose());};- }, [activateLayoutPreset, commandApi, commands, layout, notifications, queries, queryClient, queue, widgets]);+ }, [commandApi, commands, currentLayoutPresetCommands, layout, notifications, queries, queryClient, queue, widgets]);};diff --git a/invokeai/frontend/webv2/src/workbench/shell/topbar/LayoutPresetStrip.tsx b/invokeai/frontend/webv2/src/workbench/shell/topbar/LayoutPresetStrip.tsxindex 26f2e4fea8..b1ede9f802 100644--- a/invokeai/frontend/webv2/src/workbench/shell/topbar/LayoutPresetStrip.tsx+++ b/invokeai/frontend/webv2/src/workbench/shell/topbar/LayoutPresetStrip.tsx@@ -13,11 +13,7 @@ import { MenuContent } from '@platform/ui/Menu';import { Tabs } from '@platform/ui/Tabs';import { Tooltip } from '@platform/ui/Tooltip';import { getPlacedWidgetTypeIds, graphWidgetSources } from '@workbench/graphWidgets';-import {- createLayoutPresetActivator,- loadLayoutPresetWidgets,- preloadLayoutPresetWidgets,-} from '@workbench/layoutPresetActivation';+import { preloadLayoutPresetWidgets } from '@workbench/layoutPresetActivation';import { getOrderedLayoutPresets } from '@workbench/layoutPresetCollection';import { useActiveProjectSelector, useWorkbenchCommands, useWorkbenchSelector } from '@workbench/WorkbenchContext';import {@@ -71,18 +67,13 @@ export const LayoutPresetStrip = () => {() => ({ destination: invocation.destination, sourceId: invocation.sourceId }),[invocation.destination, invocation.sourceId]);- const activatePreset = useMemo(- () => createLayoutPresetActivator({ apply: layout.applyPreset, load: loadLayoutPresetWidgets }),- [layout.applyPreset]- );-const applyPreset = useCallback((preset: LayoutPreset) => {if (preset.id !== activePreset.id) {- void activatePreset(preset);+ void layout.activatePreset(preset.id);}},- [activatePreset, activePreset.id]+ [activePreset.id, layout]);const handleValueChange = useCallback((event: { value: string }) => {diff --git a/invokeai/frontend/webv2/src/workbench/workbenchStore.test.ts b/invokeai/frontend/webv2/src/workbench/workbenchStore.test.tsindex 7f6b8703fc..537f2ca6f8 100644--- a/invokeai/frontend/webv2/src/workbench/workbenchStore.test.ts+++ b/invokeai/frontend/webv2/src/workbench/workbenchStore.test.ts@@ -1,6 +1,6 @@import { beforeEach, describe, expect, it, vi } from 'vitest';-import type { LayoutPresetRoute } from './layoutContracts';+import type { LayoutPreset, LayoutPresetRoute } from './layoutContracts';import type { WorkbenchInternalStore, WorkbenchSnapshot } from './workbenchStore';import { clearProjectDiagnostics, configureDiagnostics, getProjectDiagnostics } from './diagnostics/logger';@@ -57,6 +57,31 @@ describe('createWorkbenchStore', () => {expect(snapshot.projects).toHaveLength(1);});+ it('coordinates layout preset activation across every command caller', async () => {+ const deferred = new Map<string, { promise: Promise<void>; resolve: () => void }>();+ const loadLayoutPresetWidgets = (preset: LayoutPreset): Promise<void> => {+ let resolve!: () => void;+ const promise = new Promise<void>((next) => {+ resolve = next;+ });++ deferred.set(preset.id, { promise, resolve });+ return promise;+ };+ const store = createWorkbenchStore(createInitialWorkbenchState(), { loadLayoutPresetWidgets });++ const stripActivation = store.commands.layout.activatePreset('compose');+ const hotkeyActivation = store.commands.layout.activatePreset('edit');++ deferred.get('edit')?.resolve();+ await hotkeyActivation;+ deferred.get('compose')?.resolve();+ await stripActivation;++ expect(store.getSnapshot().activeProject.layout.presetId).toBe('edit');+ expect(store.getSnapshot().activeProject.invocation).toMatchObject({ destination: 'canvas', sourceId: 'canvas' });+ });+it('notifies subscribers once for reducer changes and not for no-op reducer results', () => {const store = createWorkbenchStore();const listener = vi.fn();diff --git a/invokeai/frontend/webv2/src/workbench/workbenchStore.ts b/invokeai/frontend/webv2/src/workbench/workbenchStore.tsindex 57467a0163..713d5c61f5 100644--- a/invokeai/frontend/webv2/src/workbench/workbenchStore.ts+++ b/invokeai/frontend/webv2/src/workbench/workbenchStore.ts@@ -1,4 +1,5 @@import type { ProjectGraphAction } from '@features/workflow/utility';+import type { LayoutPreset } from '@workbench/layoutContracts';import type { Project, WorkbenchState } from '@workbench/projectContracts';import {@@ -12,6 +13,8 @@ import type { CanvasEditIntent } from './autoRoutePolicy';import type { CanvasProjectMutation } from './canvasProjectMutations';import { recordDiagnosticEntry } from './diagnostics/logger';+import { createLayoutPresetActivator, loadLayoutPresetWidgets } from './layoutPresetActivation';+import { resolveSavedLayoutPreset } from './layoutPresetSnapshots';import { getWorkbenchPreferences } from './settings/store';import {createInitialWorkbenchState,@@ -55,8 +58,16 @@ export type ProjectCommandResult =| { ok: true }| { ok: false; reason: 'invalid-name' | 'last-project' | 'project-not-found' };-const createCommands = (dispatch: WorkbenchDispatch, getState: () => WorkbenchState) => {+const createCommands = (+ dispatch: WorkbenchDispatch,+ getState: () => WorkbenchState,+ loadPresetWidgets: (preset: LayoutPreset) => Promise<unknown>+) => {const command = createCommandFactory(dispatch);+ const activateLayoutPreset = createLayoutPresetActivator({+ apply: (presetId) => dispatch({ presetId, type: 'applyPreset' }),+ load: loadPresetWidgets,+ });return {account: {@@ -240,6 +251,8 @@ const createCommands = (dispatch: WorkbenchDispatch, getState: () => WorkbenchSttoggleSourceLock: command('toggleSourceLock'),},layout: {+ activatePreset: (presetId: ActionPayload<'applyPreset'>['presetId']) =>+ activateLayoutPreset(resolveSavedLayoutPreset(getState().account, presetId)),applyPreset: command('applyPreset', (presetId: ActionPayload<'applyPreset'>['presetId']) => ({ presetId })),createPreset: command('addLayoutPreset',@@ -502,6 +515,10 @@ export interface WorkbenchInternalStore {subscribe: (listener: () => void) => () => void;}+export interface WorkbenchStoreOptions {+ loadLayoutPresetWidgets?: (preset: LayoutPreset) => Promise<unknown>;+}+const getActiveProject = (state: WorkbenchState): Project =>state.projects.find((project) => project.id === state.activeProjectId) ?? state.projects[0];@@ -588,7 +605,10 @@ const recordDiagnosticForAction = (}};-export const createWorkbenchStore = (initialState = createInitialWorkbenchState()): WorkbenchInternalStore => {+export const createWorkbenchStore = (+ initialState = createInitialWorkbenchState(),+ options: WorkbenchStoreOptions = {}+): WorkbenchInternalStore => {let state = initialState;let hasHydrated = false;let persistedRevision = 0;@@ -621,7 +641,7 @@ export const createWorkbenchStore = (initialState = createInitialWorkbenchState(const getState = (): WorkbenchState => state;return {- commands: createCommands(dispatch, getState),+ commands: createCommands(dispatch, getState, options.loadLayoutPresetWidgets ?? loadLayoutPresetWidgets),getPersistedRevision: () => persistedRevision,getSnapshot: snapshotStore.getSnapshot,getState,