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.

fix(settings): support launchpad hotkeys

joshistoast5d43591960f9
diff --git a/invokeai/frontend/webv2/src/workbench/WorkbenchContext.tsx b/invokeai/frontend/webv2/src/workbench/WorkbenchContext.tsx
index 697bf63eed..df4855eab3 100644
--- a/invokeai/frontend/webv2/src/workbench/WorkbenchContext.tsx
+++ b/invokeai/frontend/webv2/src/workbench/WorkbenchContext.tsx
@@ -217,6 +217,8 @@ export const useWorkbenchExtensions = (): ExtensionRegistry => {
return extensions;
};
+export const useOptionalWorkbenchExtensions = (): ExtensionRegistry | null => use(WorkbenchExtensionsContext);
+
export const useOptionalWorkbenchCommands = () => useOptionalWorkbenchStore()?.commands ?? null;
export const useOptionalWorkbenchQueries = () => useOptionalWorkbenchStore()?.queries ?? null;
diff --git a/invokeai/frontend/webv2/src/workbench/hotkeys/extensionHotkeys.browser.test.tsx b/invokeai/frontend/webv2/src/workbench/hotkeys/extensionHotkeys.browser.test.tsx
new file mode 100644
index 0000000000..6cf9c39a31
--- /dev/null
+++ b/invokeai/frontend/webv2/src/workbench/hotkeys/extensionHotkeys.browser.test.tsx
@@ -0,0 +1,34 @@
+import { act } from 'react';
+import { createRoot, type Root } from 'react-dom/client';
+import { afterEach, describe, expect, it } from 'vitest';
+
+import { useExtensionHotkeyDefinitions } from './extensionHotkeys';
+
+let host: HTMLDivElement | null = null;
+let root: Root | null = null;
+(globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
+
+const Harness = () => {
+ const hotkeys = useExtensionHotkeyDefinitions();
+
+ return <output>{hotkeys.length}</output>;
+};
+
+afterEach(async () => {
+ await act(() => root?.unmount());
+ host?.remove();
+ host = null;
+ root = null;
+});
+
+describe('useExtensionHotkeyDefinitions', () => {
+ it('returns no extension hotkeys outside a WorkbenchProvider', async () => {
+ host = document.createElement('div');
+ document.body.append(host);
+ root = createRoot(host);
+
+ await act(() => root?.render(<Harness />));
+
+ expect(host.querySelector('output')?.textContent).toBe('0');
+ });
+});
diff --git a/invokeai/frontend/webv2/src/workbench/hotkeys/extensionHotkeys.ts b/invokeai/frontend/webv2/src/workbench/hotkeys/extensionHotkeys.ts
index 6a23e57bd1..76983df6fa 100644
--- a/invokeai/frontend/webv2/src/workbench/hotkeys/extensionHotkeys.ts
+++ b/invokeai/frontend/webv2/src/workbench/hotkeys/extensionHotkeys.ts
@@ -1,12 +1,15 @@
import type { WidgetRegion } from '@workbench/layoutContracts';
import type { WidgetHotkeyContribution, WorkbenchRegion } from '@workbench/widgetContracts';
-import { useWorkbenchExtensions } from '@workbench/WorkbenchContext';
+import { useOptionalWorkbenchExtensions } from '@workbench/WorkbenchContext';
import { useMemo, useSyncExternalStore } from 'react';
import type { HotkeyDefinition } from './types';
const widgetRegions = new Set<WorkbenchRegion>(['bottom', 'center', 'left', 'right']);
+const NO_EXTENSION_HOTKEYS: WidgetHotkeyContribution[] = [];
+const subscribeToNothing = (): (() => void) => () => {};
+const getNoExtensionHotkeys = (): WidgetHotkeyContribution[] => NO_EXTENSION_HOTKEYS;
const isWidgetRegion = (region: WorkbenchRegion | undefined): region is WidgetRegion =>
region !== undefined && widgetRegions.has(region);
@@ -40,8 +43,12 @@ export const toExtensionHotkeyDefinition = (hotkey: WidgetHotkeyContribution): H
};
export const useExtensionHotkeyDefinitions = (): HotkeyDefinition[] => {
- const store = useWorkbenchExtensions().stores.hotkeys;
- const hotkeys = useSyncExternalStore(store.subscribe, store.list, store.list);
+ const store = useOptionalWorkbenchExtensions()?.stores.hotkeys;
+ const hotkeys = useSyncExternalStore(
+ store?.subscribe ?? subscribeToNothing,
+ store?.list ?? getNoExtensionHotkeys,
+ store?.list ?? getNoExtensionHotkeys
+ );
return useMemo(
() => hotkeys.map(toExtensionHotkeyDefinition).filter((hotkey): hotkey is HotkeyDefinition => hotkey !== null),