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): route floating-window keystrokes, and dock where the widget floated from

Lincoln Stein7bb81c5693e1
diff --git a/invokeai/frontend/webv2/src/workbench/floatingWidgets.test.ts b/invokeai/frontend/webv2/src/workbench/floatingWidgets.test.ts
index 1c3d18c83c..86c4f807d5 100644
--- a/invokeai/frontend/webv2/src/workbench/floatingWidgets.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/floatingWidgets.test.ts
@@ -115,6 +115,40 @@ describe('dockFloatingWidget', () => {
expect(getActiveProject(state).widgetRegions.right.instanceIds).not.toContain('gallery');
});
+ it('restores the tab to the position it floated from', () => {
+ // Docking appended, so float-then-dock — a gesture that reads as undoing
+ // the float — permanently reordered the rail and left the project drifted
+ // from its preset.
+ const initial = createInitialWorkbenchState();
+ const before = [...getActiveProject(initial).widgetRegions.right.instanceIds];
+
+ let state = workbenchReducer(initial, { instanceId: 'image-map', type: 'floatWidget' });
+ state = workbenchReducer(state, { instanceId: 'image-map', type: 'dockFloatingWidget' });
+
+ expect(getActiveProject(state).widgetRegions.right.instanceIds).toEqual(before);
+ });
+
+ it('docks to the end when the remembered index is gone or nonsensical', () => {
+ const floated = workbenchReducer(createInitialWorkbenchState(), {
+ instanceId: 'image-map',
+ type: 'floatWidget',
+ });
+ const withBadIndex: WorkbenchState = {
+ ...floated,
+ projects: floated.projects.map((project) => ({
+ ...project,
+ floatingWidgets: project.floatingWidgets && {
+ ...project.floatingWidgets,
+ 'image-map': { ...project.floatingWidgets['image-map']!, returnIndex: Number.NaN },
+ },
+ })),
+ };
+
+ const state = workbenchReducer(withBadIndex, { instanceId: 'image-map', type: 'dockFloatingWidget' });
+
+ expect(getActiveProject(state).widgetRegions.right.instanceIds.at(-1)).toBe('image-map');
+ });
+
it('is a no-op when nothing is floating', () => {
const initial = createInitialWorkbenchState();
diff --git a/invokeai/frontend/webv2/src/workbench/layoutContracts.ts b/invokeai/frontend/webv2/src/workbench/layoutContracts.ts
index 0c72b466fc..3eb72ee686 100644
--- a/invokeai/frontend/webv2/src/workbench/layoutContracts.ts
+++ b/invokeai/frontend/webv2/src/workbench/layoutContracts.ts
@@ -32,6 +32,12 @@ export interface FloatingWidgetState {
mode: FloatingWidgetMode;
/** The dockable region this window returns to when docked. */
returnRegion: WidgetRegion;
+ /**
+ * Where it sat in that region's tab strip when it floated, so docking puts it
+ * back rather than appending. Optional: entries written before this existed,
+ * and any whose index cannot be trusted, dock to the end.
+ */
+ returnIndex?: number;
/** Z-order within the floating layer; higher renders on top. */
stackOrder: number;
}
diff --git a/invokeai/frontend/webv2/src/workbench/widget-frame/FloatingWidgetWindow.tsx b/invokeai/frontend/webv2/src/workbench/widget-frame/FloatingWidgetWindow.tsx
index 4ee785a4a1..170af42153 100644
--- a/invokeai/frontend/webv2/src/workbench/widget-frame/FloatingWidgetWindow.tsx
+++ b/invokeai/frontend/webv2/src/workbench/widget-frame/FloatingWidgetWindow.tsx
@@ -274,6 +274,18 @@ export const FloatingWidgetWindow = ({
rounded={isMaximized ? 'none' : 'md'}
shadow="xl"
zIndex={FLOATING_BASE_Z_INDEX + stackRank}
+ // The docked frames carry these and the hotkey runtime reads them to tell
+ // which widget a keystroke is for. Floating content renders bare — this
+ // window is its chrome — so without them `getHotkeyTargetWidget` found
+ // nothing and the runtime fell back to the last focused REGION's active
+ // widget: Delete pressed over a floating window ran the docked Gallery's
+ // delete-selection on whatever that had selected. `floating` is a legal
+ // contribution-source region and is what the runtime registers this
+ // widget's own contributions under, so its hotkeys now resolve as well —
+ // previously they could not fire at all.
+ data-hotkey-widget-instance-id={instanceId}
+ data-hotkey-widget-region="floating"
+ data-hotkey-widget-type-id={instance.typeId}
onPointerDownCapture={handleFocus}
{...positionProps}
>
diff --git a/invokeai/frontend/webv2/src/workbench/workbenchState.ts b/invokeai/frontend/webv2/src/workbench/workbenchState.ts
index 4e131d9964..6889092cfc 100644
--- a/invokeai/frontend/webv2/src/workbench/workbenchState.ts
+++ b/invokeai/frontend/webv2/src/workbench/workbenchState.ts
@@ -1324,6 +1324,30 @@ const isWidgetRegionId = (value: unknown): value is WidgetRegion => WIDGET_REGIO
* window's fixed-position CSS, so anything malformed is dropped rather than
* carried: the widget then reappears docked instead of not at all.
*/
+/**
+ * Put a docking widget back where it was, not on the end.
+ *
+ * The rail is an ordered tab strip, so appending turned float-then-dock — a
+ * gesture that reads as undoing the float — into a permanent reordering, which
+ * then registered as drift from the preset. The rail may have changed while the
+ * window was open, so the remembered index is clamped rather than trusted.
+ */
+const insertAtReturnIndex = (
+ instanceIds: WidgetInstanceId[],
+ instanceId: WidgetInstanceId,
+ returnIndex: number | undefined
+): WidgetInstanceId[] => {
+ const next = [...instanceIds];
+
+ next.splice(
+ isFiniteNumber(returnIndex) && returnIndex >= 0 ? Math.min(Math.floor(returnIndex), next.length) : next.length,
+ 0,
+ instanceId
+ );
+
+ return next;
+};
+
const normalizeFloatingWidgets = (
value: unknown,
widgetInstances: Record<WidgetInstanceId, WidgetInstanceContract>
@@ -1356,6 +1380,12 @@ const normalizeFloatingWidgets = (
floatingWidgets[instanceId] = {
...clampSizeToMinimum({ heightPx: state.heightPx, widthPx: state.widthPx, x: state.x, y: state.y }),
mode: state.mode,
+ // Carried explicitly, like every other field: this rebuilds the entry
+ // rather than spreading it, so anything not named here is dropped. A
+ // nonsensical index is simply omitted — docking falls back to appending.
+ ...(isFiniteNumber(state.returnIndex) && state.returnIndex >= 0
+ ? { returnIndex: Math.floor(state.returnIndex) }
+ : {}),
returnRegion: state.returnRegion,
stackOrder: state.stackOrder,
};
@@ -3308,6 +3338,7 @@ export const __workbenchReducerInternal = (
const floating: FloatingWidgetState = {
...cascadeDefaultGeometry(Object.keys(project.floatingWidgets ?? {}).length),
mode: 'windowed',
+ returnIndex: hostRegion.instanceIds.indexOf(action.instanceId),
returnRegion: hostRegionId,
stackOrder: nextStackOrder(project.floatingWidgets),
};
@@ -3345,7 +3376,7 @@ export const __workbenchReducerInternal = (
const region = project.widgetRegions[floating.returnRegion];
const instanceIds = region.instanceIds.includes(action.instanceId)
? region.instanceIds
- : [...region.instanceIds, action.instanceId];
+ : insertAtReturnIndex(region.instanceIds, action.instanceId, floating.returnIndex);
return {
...project,