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(ui): keep one generate field context menu open at a time

Josh Corbett16c9708f9228
diff --git a/invokeai/frontend/webv2/src/features/generation/ui/shared/GenerateFieldContextMenu.browser.test.tsx b/invokeai/frontend/webv2/src/features/generation/ui/shared/GenerateFieldContextMenu.browser.test.tsx
new file mode 100644
index 0000000000..05d1dd4647
--- /dev/null
+++ b/invokeai/frontend/webv2/src/features/generation/ui/shared/GenerateFieldContextMenu.browser.test.tsx
@@ -0,0 +1,84 @@
+import { Box, ChakraProvider } from '@chakra-ui/react';
+import { system } from '@theme/system';
+import { act } from 'react';
+import { createRoot, type Root } from 'react-dom/client';
+import { afterEach, describe, expect, it, vi } from 'vitest';
+import { userEvent } from 'vitest/browser';
+
+import { GenerateFieldContextMenu } from './GenerateFieldContextMenu';
+
+let host: HTMLDivElement | null = null;
+let root: Root | null = null;
+(globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
+
+afterEach(async () => {
+ await act(() => root?.unmount());
+ host?.remove();
+ host = null;
+ root = null;
+});
+
+const FIELDS = ['Steps', 'CFG'].map((name) => ({ copyValue: () => name, name }));
+const onReset = vi.fn();
+
+/** Two stacked fields, like Steps over CFG; each menu names its field so the open one is identifiable. */
+const renderFields = async () => {
+ host = document.createElement('div');
+ document.body.append(host);
+ root = createRoot(host);
+ await act(() =>
+ root?.render(
+ <ChakraProvider value={system}>
+ {FIELDS.map(({ copyValue, name }) => (
+ <GenerateFieldContextMenu key={name} copyValue={copyValue} resetLabel={`Reset ${name}`} onReset={onReset}>
+ <Box data-field={name} h="7" w="40rem">
+ {name}
+ </Box>
+ </GenerateFieldContextMenu>
+ ))}
+ </ChakraProvider>
+ )
+ );
+};
+
+const field = (name: string) => host!.querySelector<HTMLElement>(`[data-field="${name}"]`)!;
+/** Right-clicks near a field's left edge, or past where the other field's menu (opened at its left edge) reaches. */
+const rightClick = (element: HTMLElement, x = 8) =>
+ act(() => userEvent.click(element, { button: 'right', position: { x, y: 8 } }));
+const openMenus = () => [...document.querySelectorAll<HTMLElement>('[role="menu"][data-state="open"]')];
+const settle = () =>
+ new Promise((resolve) => {
+ setTimeout(resolve, 100);
+ });
+
+describe('GenerateFieldContextMenu', () => {
+ it('moves the menu to a second field right-clicked while the first is open', async () => {
+ await renderFields();
+
+ await rightClick(field('Steps'));
+ await expect.poll(() => openMenus()[0]?.textContent).toContain('Reset Steps');
+ await rightClick(field('CFG'), 500);
+ await settle();
+
+ expect(openMenus()).toHaveLength(1);
+ expect(openMenus()[0]?.textContent).toContain('Reset CFG');
+ });
+
+ it('keeps a menu open when right-clicked itself, then still moves to the next field', async () => {
+ await renderFields();
+
+ await rightClick(field('Steps'));
+ await expect.poll(() => openMenus()).toHaveLength(1);
+ await settle();
+ const menu = openMenus()[0]!;
+
+ await rightClick(menu, 20);
+ await settle();
+ expect(openMenus()).toEqual([menu]);
+
+ await rightClick(field('CFG'), 500);
+ await settle();
+ expect(openMenus()).toHaveLength(1);
+ expect(openMenus()[0]?.textContent).toContain('Reset CFG');
+ });
+});
diff --git a/invokeai/frontend/webv2/src/features/generation/ui/shared/GenerateFieldContextMenu.tsx b/invokeai/frontend/webv2/src/features/generation/ui/shared/GenerateFieldContextMenu.tsx
index e233a726b8..dc7dad35d5 100644
--- a/invokeai/frontend/webv2/src/features/generation/ui/shared/GenerateFieldContextMenu.tsx
+++ b/invokeai/frontend/webv2/src/features/generation/ui/shared/GenerateFieldContextMenu.tsx
@@ -2,8 +2,9 @@
import type { ReactNode } from 'react';
import { Box, Menu, Portal } from '@chakra-ui/react';
+import { createExternalStore } from '@platform/state/externalStore';
import { MenuContent } from '@platform/ui/Menu';
-import { useState } from 'react';
+import { useId } from 'react';
import { useTranslation } from 'react-i18next';
interface GenerateFieldContextMenuProps {
@@ -17,6 +18,13 @@ interface GenerateFieldContextMenuProps {
resetLabel?: string;
}
+/**
+ * One field menu is open at a time. A right-click on a second field leaves the first menu open (right-button
+ * presses don't dismiss it), and the two menus' dismissal then raced so the next right-click closed both; switching
+ * owners closes the first menu instead.
+ */
+const openMenu = createExternalStore<{ owner: string | null; x: number; y: number }>({ owner: null, x: 0, y: 0 });
+
export const GenerateFieldContextMenu = ({
children,
copyValue,
@@ -25,14 +33,15 @@ export const GenerateFieldContextMenu = ({
resetLabel,
}: GenerateFieldContextMenuProps) => {
const { t } = useTranslation();
- const [point, setPoint] = useState<{ x: number; y: number } | null>(null);
+ const owner = useId();
+ const point = openMenu.useSelector((menu) => (menu.owner === owner ? { x: menu.x, y: menu.y } : null));
return (
<Box
w="full"
onContextMenu={(event) => {
event.preventDefault();
- setPoint({ x: event.clientX, y: event.clientY });
+ openMenu.setSnapshot({ owner, x: event.clientX, y: event.clientY });
}}
>
{children}
@@ -43,8 +52,8 @@ export const GenerateFieldContextMenu = ({
placement: 'bottom-start',
}}
onOpenChange={(event) => {
- if (!event.open) {
- setPoint(null);
+ if (!event.open && openMenu.getSnapshot().owner === owner) {
+ openMenu.patchSnapshot({ owner: null });
}
}}
>