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 a floating window's stored position on the axis a resize leaves alone
Josh Corbett2d661a1c9bd8
diff --git a/invokeai/frontend/webv2/ARCHITECTURE.md b/invokeai/frontend/webv2/ARCHITECTURE.mdindex ad0ce1b640..fd8af11ecf 100644--- a/invokeai/frontend/webv2/ARCHITECTURE.md+++ b/invokeai/frontend/webv2/ARCHITECTURE.md@@ -132,9 +132,9 @@ Workbench focus is transient and never persisted. `WorkbenchFocusProvider` (`wor### Window interactions- **Geometry is stored once and adapted in CSS.** Every window shares one static rule set; the stored geometry and stacking rank arrive as inline custom properties (registered as non-inheriting, so a drag restyles the window and not the widget inside it), so a commit changes values rather than minting a class. The position keeps a 48px sliver reachable (`FLOATING_VIEWPORT_MARGIN_PX`, the same margin `clampWindowToViewport` applies to commits) and the displayed size is capped to the viewport. A viewport that shrinks therefore persists nothing; the stored geometry applies again when there is room.-- **Gestures start from the rendered rectangle**, not the stored geometry, so a window CSS is clamping or capping does not jump. A gesture commits only what it changed: a move keeps the stored size, and a resize keeps the stored values of any axis it did not change (`commitResizedAxes`), because what is on screen there may only be the viewport's clamp or cap. A press that never moves, or a drag against the minimum or the cap, commits nothing. Keyboard steps follow the same rules.+- **Gestures start from the rendered rectangle**, not the stored geometry, so a window CSS is clamping or capping does not jump. A gesture commits only what it changed: a move keeps the stored size, and a resize keeps the stored values of any axis it did not change (`commitResizedAxes`), because what is on screen there may only be the viewport's clamp or cap. The viewport clamp applies to the changed axes alone, so a vertical resize never rewrites a stored x, nor a horizontal one a stored y. A press that never moves, or a drag against the minimum or the cap, commits nothing. Keyboard steps follow the same rules.- **One commit per gesture.** Pointer moves are coalesced to animation frames and previewed as inline custom properties on the window element; the store is written once, when the gesture ends — on release, or when the pointer is taken away (`pointercancel`, a release the page never saw), which keeps what was dragged so far. Escape discards the preview. Each keyboard step is its own commit.-- **Resize handles.** Eight pointer-only handles (four edges, four corners) straddle the border with matching cursors and non-overlapping hit areas, above an isolated content frame so widget stacking cannot cover them. Inside the bottom-right corner sits the `ResizeCorner`, the single labelled, keyboard-operable resize control; the pointer-only handle there is clipped to the band outside the border. That control announces the size on screen — the stored size as the viewport caps it — and follows the viewport, so its value and its keyboard steps agree. `resizeFloatingGeometry` anchors the opposite side, holds the size between the minimum and the viewport, and stops the top edge at the viewport's top.+- **Resize handles.** Eight pointer-only handles (four edges, four corners) straddle the border with matching cursors and non-overlapping hit areas, above an isolated content frame so widget stacking cannot cover them. Inside the bottom-right corner sits the `ResizeCorner`, the single labelled, keyboard-operable resize control; the pointer-only handle there is clipped to the band outside the border. That control announces the size on screen — the stored size as the viewport caps it — and follows the viewport, so its value and its keyboard steps agree. `resizeFloatingGeometry` anchors the opposite side, holds the size between the minimum and the viewport (a viewport smaller than the minimum lowers it to the viewport, so the shown size is not mistaken for a resize), and stops the top edge at the viewport's top.- **One gesture path.** The title bar, the grip, and every handle start through the window's single pointer session, so a new gesture replaces the one before it and resize gestures all raise the platform's resize-drag signal. The preview names the mode the gesture began in and applies only while the window is still in that mode: a window that maximizes or collapses under a gesture shows its new frame at once, and the gesture ends itself — releasing the drag cursor and committing nothing — the next time it hears from the pointer. Docking, removing, or switching project unmounts the host (hosts are keyed by project and instance), which aborts the session.- **Title bar.** Double-click maximizes or restores, and expands a collapsed window; Restore returns to the windowed geometry, which maximizing never touches. Collapse and Expand are explicit buttons. The label truncates first, down to its icon; contributed widget actions then give way in a strip that scrolls, without a scrollbar, by focus, a sideways scroll, or touch, so a focused action is always in view with its ring; the window's own controls never shrink.- **Shaded bodies stay mounted.** A shaded body is hidden with React `Activity`, so its local state survives and no second copy mounts. Drafts are flushed before shading, docking, and removing, because hiding or unmounting an editor stops its flusher.diff --git a/invokeai/frontend/webv2/src/workbench/floatingWindows.test.ts b/invokeai/frontend/webv2/src/workbench/floatingWindows.test.tsindex 6b639ef98b..772a673204 100644--- a/invokeai/frontend/webv2/src/workbench/floatingWindows.test.ts+++ b/invokeai/frontend/webv2/src/workbench/floatingWindows.test.ts@@ -250,34 +250,55 @@ describe('commitResizedAxes', () => {// Stored for a larger display; shown clamped and capped by a 600x500 viewport.const stored = { heightPx: 900, widthPx: 900, x: 5000, y: 40 };const start = { heightPx: 500, widthPx: 600, x: 552, y: 40 };+ const viewport = { height: 500, width: 600 };it('takes the resized values only on the axes the resize changed', () => {- expect(commitResizedAxes(stored, start, { ...start, widthPx: 550 })).toEqual({ ...stored, widthPx: 550, x: 552 });- expect(commitResizedAxes(stored, start, { ...start, heightPx: 350, y: 60 })).toEqual({+ expect(commitResizedAxes(stored, start, { ...start, widthPx: 550 }, viewport)).toEqual({+ ...stored,+ widthPx: 550,+ x: 552,+ });+ expect(commitResizedAxes(stored, start, { ...start, heightPx: 350, y: 60 }, viewport)).toEqual({...stored,heightPx: 350,y: 60,});- expect(commitResizedAxes(stored, start, { heightPx: 350, widthPx: 550, x: 560, y: 60 })).toEqual({+ expect(commitResizedAxes(stored, start, { heightPx: 350, widthPx: 550, x: 540, y: 60 }, viewport)).toEqual({heightPx: 350,widthPx: 550,- x: 560,+ x: 540,y: 60,});});+ it('holds a changed axis in the viewport, and never moves an untouched one into it', () => {+ // Shrunk from the right while its left edge sat at the sliver: the narrower window keeps 48px on screen.+ const atSliver = { heightPx: 300, widthPx: 400, x: 48 - 400, y: 40 };++ expect(commitResizedAxes(atSliver, atSliver, { ...atSliver, widthPx: 300 }, viewport)).toEqual({+ ...atSliver,+ widthPx: 300,+ x: 48 - 300,+ });+ // Shown at the right edge's clamp while stored far past it: a height change leaves the stored x alone.+ expect(commitResizedAxes(stored, start, { ...start, heightPx: 350 }, { height: 500, width: 599 })).toEqual({+ ...stored,+ heightPx: 350,+ });+ });+it('commits nothing when the resize changed nothing, as a drag against the cap or the minimum does', () => {- expect(commitResizedAxes(stored, start, { ...start })).toBeNull();+ expect(commitResizedAxes(stored, start, { ...start }, viewport)).toBeNull();});it('does not read sub-pixel arithmetic on an axis as a resize of it', () => {// At a fractional zoom the rectangle on screen is not on whole pixels, and the untouched axis comes back a// fraction off: it must keep what was stored, not take the viewport's cap.- expect(commitResizedAxes(stored, start, { ...start, heightPx: 350, widthPx: 600.3 })).toEqual({+ expect(commitResizedAxes(stored, start, { ...start, heightPx: 350, widthPx: 600.3 }, viewport)).toEqual({...stored,heightPx: 350,});- expect(commitResizedAxes(stored, start, { ...start, widthPx: 599.7, x: 552.2 })).toBeNull();+ expect(commitResizedAxes(stored, start, { ...start, widthPx: 599.7, x: 552.2 }, viewport)).toBeNull();});});diff --git a/invokeai/frontend/webv2/src/workbench/floatingWindows.ts b/invokeai/frontend/webv2/src/workbench/floatingWindows.tsindex b8c6807c36..b0818e0d75 100644--- a/invokeai/frontend/webv2/src/workbench/floatingWindows.ts+++ b/invokeai/frontend/webv2/src/workbench/floatingWindows.ts@@ -66,27 +66,31 @@ export const clampWindowToViewport = (});/**- * What a resize commits: on each axis the resize actually changed, the result from the rectangle on screen; on an- * axis it left alone, what was stored. An untouched axis may be showing the viewport's clamp or cap, which is not- * the user's choice to persist. Null when the resize changed nothing — a drag against the minimum or the cap.+ * What a resize commits: on each axis the resize actually changed, the result from the rectangle on screen, held in+ * the viewport by {@link clampWindowToViewport}; on an axis it left alone, what was stored. An untouched axis may be+ * showing the viewport's clamp or cap, which is not the user's choice to persist. Null when the resize changed+ * nothing — a drag against the minimum or the cap.*/export const commitResizedAxes = (stored: FloatingGeometry,start: FloatingGeometry,- resized: FloatingGeometry+ resized: FloatingGeometry,+ viewport: { width: number; height: number }): FloatingGeometry | null => {// The rectangle on screen can sit on fractional pixels, and what is committed is whole ones: a difference under- // half a pixel is arithmetic, not the user.+ // half a pixel is arithmetic, not the user. Changes are read before the clamp, which works in whole viewport+ // pixels and could otherwise move an axis the resize never touched.const changed = (from: number, to: number) => Math.abs(to - from) >= 0.5;const horizontal = changed(start.widthPx, resized.widthPx) || changed(start.x, resized.x);const vertical = changed(start.heightPx, resized.heightPx) || changed(start.y, resized.y);+ const placed = clampWindowToViewport(resized, viewport);return horizontal || vertical? {- heightPx: vertical ? resized.heightPx : stored.heightPx,- widthPx: horizontal ? resized.widthPx : stored.widthPx,- x: horizontal ? resized.x : stored.x,- y: vertical ? resized.y : stored.y,+ heightPx: vertical ? placed.heightPx : stored.heightPx,+ widthPx: horizontal ? placed.widthPx : stored.widthPx,+ x: horizontal ? placed.x : stored.x,+ y: vertical ? placed.y : stored.y,}: null;};diff --git a/invokeai/frontend/webv2/src/workbench/widget-frame/FloatingWidgetWindow.browser.test.tsx b/invokeai/frontend/webv2/src/workbench/widget-frame/FloatingWidgetWindow.browser.test.tsxindex adf8308302..06d6e157d6 100644--- a/invokeai/frontend/webv2/src/workbench/widget-frame/FloatingWidgetWindow.browser.test.tsx+++ b/invokeai/frontend/webv2/src/workbench/widget-frame/FloatingWidgetWindow.browser.test.tsx@@ -1049,6 +1049,29 @@ describe('FloatingWidgetWindow gestures', () => {});});+ it('keeps the stored position of the axis a pointer resize leaves alone, wherever the viewport holds it', async () => {+ await page.viewport(500, 260);+ // Stored far off the bottom-right; shown at (452, 212) and capped to 260px tall.+ await renderWindow({ ...state, heightPx: 300, widthPx: 400, x: 1000, y: 800 });++ await drag(edge('e'), -50, 0);+ expect(windowMocks.setFloatingGeometry).toHaveBeenLastCalledWith('image-map-instance', {+ heightPx: 300,+ widthPx: 350,+ x: 452,+ y: 800,+ });++ await nextFrame();+ await drag(edge('s'), 0, -40);+ expect(windowMocks.setFloatingGeometry).toHaveBeenLastCalledWith('image-map-instance', {+ heightPx: 220,+ widthPx: 400,+ x: 1000,+ y: 212,+ });+ });+it('does not grow a window past the viewport, so an edge at the cap resizes nothing instead of sliding it', async () => {await page.viewport(600, 500);await renderWindow({ ...state, heightPx: 300, widthPx: 600, x: 0, y: 40 });diff --git a/invokeai/frontend/webv2/src/workbench/widget-frame/FloatingWidgetWindow.tsx b/invokeai/frontend/webv2/src/workbench/widget-frame/FloatingWidgetWindow.tsxindex 4ed60027c0..ced2af8e0e 100644--- a/invokeai/frontend/webv2/src/workbench/widget-frame/FloatingWidgetWindow.tsx+++ b/invokeai/frontend/webv2/src/workbench/widget-frame/FloatingWidgetWindow.tsx@@ -90,8 +90,24 @@ const WINDOW_SX: SystemStyleObject = {// The title bar has no room for a scrollbar; the strip scrolls by focus, a sideways scroll, and touch.const ACTIONS_SCROLL_SX: SystemStyleObject = { '&::-webkit-scrollbar': { display: 'none' }, scrollbarWidth: 'none' };-/** A window is never shown larger than the viewport, so a resize does not grow past it either. */-const getViewportSize = () => ({ heightPx: window.innerHeight, widthPx: window.innerWidth });+const getViewport = () => ({ height: window.innerHeight, width: window.innerWidth });++const keepInViewport = (geometry: FloatingGeometry): FloatingGeometry => clampWindowToViewport(geometry, getViewport());++/** What resizing the rectangle on screen commits, or null for nothing (see {@link commitResizedAxes}). */+const resizeOnScreen = (+ stored: FloatingGeometry,+ start: FloatingGeometry,+ edge: FloatingResizeEdge,+ deltaX: number,+ deltaY: number+): FloatingGeometry | null => {+ const viewport = getViewport();+ // A window is never shown larger than the viewport, so a resize does not grow past it either.+ const max = { heightPx: viewport.height, widthPx: viewport.width };++ return commitResizedAxes(stored, start, resizeFloatingGeometry(start, edge, deltaX, deltaY, max), viewport);+};// A drag rewrites the preview properties every frame. Custom properties inherit by default, which would restyle// the whole widget inside the window each time; registered as non-inheriting, only the window itself is restyled.@@ -337,17 +353,14 @@ export const FloatingWidgetWindow = ({const { heightPx, mode, widthPx, x, y } = state;const commitGeometry = useCallback(- (geometry: FloatingGeometry) => {- const clamped = clampWindowToViewport(geometry, { height: window.innerHeight, width: window.innerWidth });-- // The rendered rectangle a gesture starts from can sit on fractional pixels.+ // The rendered rectangle a gesture starts from can sit on fractional pixels.+ (geometry: FloatingGeometry) =>widgets.setFloatingGeometry(instanceId, {- heightPx: Math.round(clamped.heightPx),- widthPx: Math.round(clamped.widthPx),- x: Math.round(clamped.x),- y: Math.round(clamped.y),- });- },+ heightPx: Math.round(geometry.heightPx),+ widthPx: Math.round(geometry.widthPx),+ x: Math.round(geometry.x),+ y: Math.round(geometry.y),+ }),[instanceId, widgets]);@@ -453,10 +466,15 @@ export const FloatingWidgetWindow = ({// which is not the user's choice to persist.gesture.live =edge !== 'move'- ? commitResizedAxes(stored, start, resizeFloatingGeometry(start, edge, deltaX, deltaY, getViewportSize()))+ ? resizeOnScreen(stored, start, edge, deltaX, deltaY): deltaX === 0 && deltaY === 0? null- : { heightPx: stored.heightPx, widthPx: stored.widthPx, x: start.x + deltaX, y: start.y + deltaY };+ : keepInViewport({+ heightPx: stored.heightPx,+ widthPx: stored.widthPx,+ x: start.x + deltaX,+ y: start.y + deltaY,+ });writePreview(gesture.live ? { geometry: gesture.live, mode: gesture.mode } : null);},});@@ -517,7 +535,7 @@ export const FloatingWidgetWindow = ({event.preventDefault();// Like a pointer move: from where the window is on screen, keeping the stored size.- commitGeometry({ heightPx, widthPx, x: rendered.x + offset[0], y: rendered.y + offset[1] });+ commitGeometry(keepInViewport({ heightPx, widthPx, x: rendered.x + offset[0], y: rendered.y + offset[1] }));},[commitGeometry, heightPx, mode, readRenderedGeometry, widthPx]);@@ -546,11 +564,7 @@ export const FloatingWidgetWindow = ({event.preventDefault();- const resized = commitResizedAxes(- { heightPx, widthPx, x, y },- rendered,- resizeFloatingGeometry(rendered, 'se', offset[0], offset[1], getViewportSize())- );+ const resized = resizeOnScreen({ heightPx, widthPx, x, y }, rendered, 'se', offset[0], offset[1]);if (resized) {commitGeometry(resized);