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): keep keyboard focus after removing a floating widget
Josh Corbett33412f0e2bad
diff --git a/invokeai/frontend/webv2/ARCHITECTURE.md b/invokeai/frontend/webv2/ARCHITECTURE.mdindex e651a14ecd..85d03275a4 100644--- a/invokeai/frontend/webv2/ARCHITECTURE.md+++ b/invokeai/frontend/webv2/ARCHITECTURE.md@@ -126,7 +126,7 @@ Workbench focus is transient and never persisted. `WorkbenchFocusProvider` (`wor- **Raising.** A press inside a window always asks the reducer to raise it, which is a no-op for the topmost window. Focus raises a window only when it makes that window the active one: focus moving around inside the already-active window, or returning from a closing dialog or popover, writes nothing. Ordinary focus leaves a shaded window shaded; the explicit reveal (marker, center "Show window", recall) expands it.- **Recall is the one reveal that moves no focus.** A recall raises and expands its window but leaves keyboard focus, the outline, and the hotkey target where the user had them; the next press or focus change brings them back into agreement. Because focus inside the active window does not re-raise, it cannot re-bury the window a recall just revealed.- **Keys follow the outline.** A press inside a window moves keyboard focus into it when focus was elsewhere, and a press on a docked region pulls focus out of a floating window. The floating layer renders windows in a fixed DOM order and stacks them by z-index alone: raising a window never moves its node, which would drop the focus that raised it.-- **Focus follows placement:** into the window after Float and after a marker or "Show window" reveal, into the return region after Dock, and after Remove into the center when it gets its view back, otherwise into the window's own rail. A focus move targets the region frame that is on screen (side regions keep earlier panels mounted but hidden), waits a bounded time for its widget to mount, and is abandoned if the user presses somewhere else first.+- **Focus follows placement:** into the window after Float and after a marker or "Show window" reveal, into the return region after Dock, and after Remove from a marker's menu into the center when it gets its view back, otherwise into the panel the window's own rail is showing — or the center when that rail shows none, emptied by the float or collapsed. Removing a window from a rail's enable menu moves no focus: that menu stays open and keeps it. A focus move targets the region frame that is on screen (side regions keep earlier panels mounted but hidden), waits a bounded time for its widget to mount, and is abandoned if the user presses somewhere else first.- **Hotkeys read the same target.** `HotkeyContext.focusedRegion` is `WidgetRegion | 'floating' | null`; the persisted `WidgetRegion` is unchanged. A key press is aimed at the widget under the event target first and, outside any widget's DOM, at the active floating instance or the focused region's active member (`resolveHotkeyTarget`). A region's active pointer counts only while it names a member, so a view that floated out of an emptied center answers as a window, not as the center. A floating command source carries `region: 'floating'` and is never used to index `widgetRegions`. Generic `focused-region` shortcuts match under a window, shortcuts tied to a named docked region do not, and a floating widget's own `focused-region` contribution follows its window at the same priority it has when docked.### Window interactionsdiff --git a/invokeai/frontend/webv2/scripts/run-accessibility-journeys.mjs b/invokeai/frontend/webv2/scripts/run-accessibility-journeys.mjsindex 68bb5e078b..bd1fe8b53a 100644--- a/invokeai/frontend/webv2/scripts/run-accessibility-journeys.mjs+++ b/invokeai/frontend/webv2/scripts/run-accessibility-journeys.mjs@@ -1048,6 +1048,63 @@ const runFloatingWindowJourney = async (browser) => {await waitForSettledDocument(page);await assertNoAxeViolations(page, `${id}:docked`);+ // Removing a window from its marker's menu takes the menu and the marker with it. Focus goes to the panel the+ // window's rail is showing, or to the center when that rail shows none: collapsed, or emptied by the float.+ const floatImageMapFrom = async (widgetRail, menuName) => {+ await widgetRail.getByRole('button', { exact: true, name: menuName }).click();+ await page.getByRole('menuitemcheckbox', { exact: true, name: 'Image Map' }).click();+ await page.keyboard.press('Escape');+ await page.getByRole('button', { exact: true, name: 'Float Window' }).click();+ await floatingWindow.waitFor();+ await waitForActiveWindow();+ };+ // From the keyboard, where losing focus costs the most: Remove is the menu's last item.+ const removeImageMapFrom = async (widgetRail, focusedRegionAfter) => {+ await widgetRail.getByRole('button', { exact: true, name: 'Image Map, floating window' }).focus();+ await page.keyboard.press('Shift+F10');+ await page.waitForFunction(() => Boolean(document.activeElement?.closest('[role="menu"]')));+ await page.keyboard.press('End');+ await page.keyboard.press('Enter');+ await floatingWindow.waitFor({ state: 'detached' });+ await waitForFocusedRegion(focusedRegionAfter);+ };++ await floatImageMapFrom(rail, 'Inspect widgets');+ await removeImageMapFrom(rail, 'right');++ await floatImageMapFrom(rail, 'Inspect widgets');+ await rail.getByRole('button', { pressed: true }).click();+ await page.getByRole('complementary', { exact: true, name: 'right widget panel' }).waitFor({ state: 'detached' });+ await removeImageMapFrom(rail, 'center');++ const leftRail = page.getByRole('navigation', { exact: true, name: 'Create widget visibility' });+ for (const name of ['Video', 'Upscale']) {+ await leftRail.getByRole('button', { exact: true, name }).click({ button: 'right' });+ await page.getByRole('menuitem', { exact: true, name: `Remove ${name}` }).click();+ }+ await floatImageMapFrom(leftRail, 'Create widgets');+ await page.getByRole('complementary', { exact: true, name: 'left widget panel' }).waitFor({ state: 'detached' });+ await removeImageMapFrom(leftRail, 'center');++ // The enable menu stays open across a toggle and keeps focus; removing a window from it moves none.+ await floatImageMapFrom(leftRail, 'Create widgets');+ await leftRail.getByRole('button', { exact: true, name: 'Create widgets' }).click();+ await page.getByRole('menuitemcheckbox', { exact: true, name: 'Image Map' }).click();+ await floatingWindow.waitFor({ state: 'detached' });+ // A focus move would have landed within a frame of the removal.+ await page.evaluate(+ () =>+ new Promise((resolve) => {+ requestAnimationFrame(() => requestAnimationFrame(resolve));+ })+ );+ assert.equal(+ await page.evaluate(() => Boolean(document.activeElement?.closest('[role="menu"]'))),+ true,+ 'Removing a window from the enable menu must leave focus in that menu.'+ );+ await page.keyboard.press('Escape');+if (pageErrors.length > 0) {throw new AggregateError(pageErrors, `${id} raised uncaught browser errors.`);}diff --git a/invokeai/frontend/webv2/src/workbench/focusRegions.browser.test.tsx b/invokeai/frontend/webv2/src/workbench/focusRegions.browser.test.tsxindex 92d4e94379..e38eeb8119 100644--- a/invokeai/frontend/webv2/src/workbench/focusRegions.browser.test.tsx+++ b/invokeai/frontend/webv2/src/workbench/focusRegions.browser.test.tsx@@ -131,6 +131,18 @@ describe('focusRegion', () => {expect(center.getAttribute('data-highlighted')).toBe('true');});+ it('moves focus into a region as it stands when no widget is named', async () => {+ // The editor never opens here, so a move waiting on a widget would leave focus on the opener.+ const { center, opener } = await renderHarness((_open, focusRegion) => focusRegion('center'));+ opener.focus();++ await act(() => opener.click());+ await frames(3);++ expect(document.activeElement).toBe(center);+ expect(center.getAttribute('data-highlighted')).toBe('true');+ });+it('keeps the move when a closing menu hands focus back to the control that opened it', async () => {const { center, opener } = await renderHarness();// A menu item's menu restores focus to its trigger as it closes, after the move has landed.diff --git a/invokeai/frontend/webv2/src/workbench/focusRegions.tsx b/invokeai/frontend/webv2/src/workbench/focusRegions.tsxindex 321b4dcec7..550ebe7ec8 100644--- a/invokeai/frontend/webv2/src/workbench/focusRegions.tsx+++ b/invokeai/frontend/webv2/src/workbench/focusRegions.tsx@@ -29,8 +29,11 @@ export interface WorkbenchFocusController {clear(): void;/** Move keyboard focus into a floating window once it shows; focus arriving there activates it. */focusFloating(instanceId: WidgetInstanceId): void;- /** Move keyboard focus into the region a control just opened a widget in, once that widget shows. */- focusRegion(region: WidgetRegion, typeId: string): void;+ /**+ * Move keyboard focus into the region a control just opened a widget in, once that widget shows. Without a+ * widget, focus moves into the region as it stands.+ */+ focusRegion(region: WidgetRegion, typeId?: string): void;/** The focus target, or null once it no longer describes something on screen in this project and account. */getTarget(): WorkbenchFocusTarget | null;subscribe(listener: () => void): () => void;@@ -173,7 +176,7 @@ export const createWorkbenchFocusController = ({for (const container of document.querySelectorAll<HTMLElement>(`[data-focus-region="${region}"]`)) {if (container.getClientRects().length > 0 &&- container.querySelector(`[data-hotkey-widget-type-id="${CSS.escape(typeId)}"]`)+ (typeId === undefined || container.querySelector(`[data-hotkey-widget-type-id="${CSS.escape(typeId)}"]`))) {return container;}diff --git a/invokeai/frontend/webv2/src/workbench/shell/WorkbenchShell.tsx b/invokeai/frontend/webv2/src/workbench/shell/WorkbenchShell.tsxindex 0ae71520bd..3fc798ce17 100644--- a/invokeai/frontend/webv2/src/workbench/shell/WorkbenchShell.tsx+++ b/invokeai/frontend/webv2/src/workbench/shell/WorkbenchShell.tsx@@ -210,31 +210,35 @@ export const WorkbenchShell = () => {},[focusRegion, placementProject, widgets]);- const closeFloating = useCallback(+ const handleRemoveFloating = useCallback((instanceId: string) => {- const { widgetInstances, widgetRegions } = placementProject;+ const typeId = placementProject.widgetInstances[instanceId]?.typeId;const removed = removeFloatingPlacement({ instanceId, project: placementProject, widgets });if (!removed) {return;}- // The menu that asked and the marker it hung from are both gone. Focus goes to the center when it gets its- // view back, otherwise to whatever the window's own rail is showing.- const region = removed.restoresCenter ? 'center' : removed.returnRegion;- const shownInstanceId = removed.restoresCenter ? instanceId : widgetRegions[region].activeInstanceId;- const typeId = widgetInstances[shownInstanceId]?.typeId;-- if (typeId) {- focusRegion(region, typeId);+ // The marker's menu that asked and the marker itself are both gone. Focus goes to the center when it gets+ // its view back, otherwise to the panel the window's own rail is showing. Removing leaves that rail as it+ // was, so a rail showing no panel — emptied by the float, or collapsed — hands focus to the center instead.+ if (removed.restoresCenter) {+ focusRegion('center', typeId);+ return;}++ const isRailPanelShown =+ removed.returnRegion === 'left' ? isLeftPanelShown : removed.returnRegion === 'right' && isRightPanelShown;++ focusRegion(isRailPanelShown ? removed.returnRegion : 'center');},- [focusRegion, placementProject, widgets]+ [focusRegion, isLeftPanelShown, isRightPanelShown, placementProject, widgets]);+ // The enable menu stays open and keeps focus, so removing a window from it moves none.const handleToggleLeft = useCallback((item: (typeof leftMenuItems)[number]) =>item.isEnabled && item.isFloating- ? closeFloating(item.id)+ ? removeFloatingPlacement({ instanceId: item.id, project: placementProject, widgets }): item.isEnabled? closeWidgetPlacement({widgets,@@ -249,12 +253,12 @@ export const WorkbenchShell = () => {options: { createNew: item.allowMultiple, preferredRegions: ['left'] },typeId: item.typeId,}),- [closeFloating, placementProject, widgets]+ [placementProject, widgets]);const handleToggleRight = useCallback((item: (typeof rightMenuItems)[number]) =>item.isEnabled && item.isFloating- ? closeFloating(item.id)+ ? removeFloatingPlacement({ instanceId: item.id, project: placementProject, widgets }): item.isEnabled? closeWidgetPlacement({widgets,@@ -269,7 +273,7 @@ export const WorkbenchShell = () => {options: { createNew: item.allowMultiple, preferredRegions: ['right'] },typeId: item.typeId,}),- [closeFloating, placementProject, widgets]+ [placementProject, widgets]);const leftRailGroups = useMemo(() => [@@ -331,6 +335,7 @@ export const WorkbenchShell = () => {menuItems={leftMenuItems}side="left"onDock={handleDock}+ onRemoveFloating={handleRemoveFloating}onSelect={handleSelect}onToggle={handleToggleLeft}/>@@ -343,6 +348,7 @@ export const WorkbenchShell = () => {menuItems={rightMenuItems}side="right"onDock={handleDock}+ onRemoveFloating={handleRemoveFloating}onSelect={handleSelect}onToggle={handleToggleRight}/>diff --git a/invokeai/frontend/webv2/src/workbench/widget-frame/WidgetBar.tsx b/invokeai/frontend/webv2/src/workbench/widget-frame/WidgetBar.tsxindex 9c955128c0..574a1503c8 100644--- a/invokeai/frontend/webv2/src/workbench/widget-frame/WidgetBar.tsx+++ b/invokeai/frontend/webv2/src/workbench/widget-frame/WidgetBar.tsx@@ -39,11 +39,22 @@ interface WidgetBarProps {menuItems: WidgetBarItem[];/** Dock the floating window a marker stands for. */onDock: (instanceId: WidgetInstanceId) => void;+ /** Remove the floating window a marker stands for, asked from that marker's own menu. */+ onRemoveFloating: (instanceId: WidgetInstanceId) => void;onSelect: (region: WidgetBarGroup['region'], instanceId: WidgetInstanceId) => void;onToggle: (item: WidgetBarItem) => void;}-export const WidgetBar = ({ edgeRegion, groups, menuItems, onDock, onSelect, onToggle, side }: WidgetBarProps) => {+export const WidgetBar = ({+ edgeRegion,+ groups,+ menuItems,+ onDock,+ onRemoveFloating,+ onSelect,+ onToggle,+ side,+}: WidgetBarProps) => {const { t } = useTranslation();const isEdgeOutlined = useHighlightedRegion() === edgeRegion;const region = side;@@ -77,6 +88,13 @@ export const WidgetBar = ({ edgeRegion, groups, menuItems, onDock, onSelect, onTconst handleMenuToggle = useCallback((item: WidgetEnableMenuItem) => onToggle(item as WidgetBarItem), [onToggle]);const handleInstanceClose = useCallback(() => setInstanceMenuTarget(null), []);const handleInstanceDock = useCallback((item: WidgetEnableMenuItem) => onDock(item.id), [onDock]);+ // A marker's menu closes and its marker goes with the window, unlike the enable menu, which stays open: the+ // caller has focus to place only for this one.+ const handleInstanceRemove = useCallback(+ (item: WidgetEnableMenuItem) =>+ (item as WidgetBarItem).isFloating ? onRemoveFloating(item.id) : onToggle(item as WidgetBarItem),+ [onRemoveFloating, onToggle]+ );// A marker only ever shows in the rail its window returns to.const isMarkerMenu = (instanceMenuTarget?.item as WidgetBarItem | undefined)?.isFloating === true;@@ -125,7 +143,7 @@ export const WidgetBar = ({ edgeRegion, groups, menuItems, onDock, onSelect, onTtarget={instanceMenuTarget}onClose={handleInstanceClose}onDock={handleInstanceDock}- onRemove={handleMenuToggle}+ onRemove={handleInstanceRemove}/></Flex>);diff --git a/invokeai/frontend/webv2/src/workbench/widget-frame/WidgetBarFloatingMarker.browser.test.tsx b/invokeai/frontend/webv2/src/workbench/widget-frame/WidgetBarFloatingMarker.browser.test.tsxindex da49451bdd..6982eb18d8 100644--- a/invokeai/frontend/webv2/src/workbench/widget-frame/WidgetBarFloatingMarker.browser.test.tsx+++ b/invokeai/frontend/webv2/src/workbench/widget-frame/WidgetBarFloatingMarker.browser.test.tsx@@ -79,6 +79,7 @@ await i18n.use(initReactI18next).init({railMarker: '{{label}}, floating window',railMarkerHint: '{{label}} is floating — click to show the window',},+ rail: { inspectWidgets: 'Inspect widgets' },removeWidget: 'Remove {{label}}',},},@@ -86,7 +87,7 @@ await i18n.use(initReactI18next).init({},});-const handlers = { onDock: vi.fn(), onSelect: vi.fn(), onToggle: vi.fn() };+const handlers = { onDock: vi.fn(), onRemoveFloating: vi.fn(), onSelect: vi.fn(), onToggle: vi.fn() };const GROUPS: WidgetBarGroup[] = [{activeId: 'gallery',@@ -218,10 +219,46 @@ describe('WidgetBar floating marker', () => {await Promise.resolve();});- expect(handlers.onToggle).toHaveBeenCalledWith(expect.objectContaining({ id: 'image-map', isFloating: true }));+ expect(handlers.onRemoveFloating).toHaveBeenCalledExactlyOnceWith('image-map');+ expect(handlers.onToggle).not.toHaveBeenCalled();expect(handlers.onDock).not.toHaveBeenCalled();});+ it('removes the window from the enable menu as a toggle, not as a marker removal', async () => {+ await renderBar();+ await act(async () => {+ slot('Inspect widgets').click();+ await Promise.resolve();+ });++ const row = [...document.body.querySelectorAll<HTMLElement>('[role="menuitemcheckbox"]')].find(+ (item) => item.textContent === 'Image Map'+ )!;++ await act(async () => {+ row.click();+ await Promise.resolve();+ });++ expect(handlers.onToggle).toHaveBeenCalledExactlyOnceWith(+ expect.objectContaining({ id: 'image-map', isFloating: true })+ );+ expect(handlers.onRemoveFloating).not.toHaveBeenCalled();+ });++ it('removes a docked tab from its menu as a toggle', async () => {+ await renderBar();+ await openMenu(slot('Queue'));++ await act(async () => {+ menuItem('remove-widget')?.click();+ await Promise.resolve();+ });++ expect(handlers.onToggle).toHaveBeenCalledExactlyOnceWith(expect.objectContaining({ id: 'queue' }));+ expect(handlers.onRemoveFloating).not.toHaveBeenCalled();+ });+it('offers no Dock item for a docked tab', async () => {await renderBar();await openMenu(slot('Queue'));