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(webv2): check droppable visibility at collision time so revealed boards stay droppable

joshistoaste54e0fe58f2b
diff --git a/invokeai/frontend/webv2/src/workbench/shell/WorkbenchShell.tsx b/invokeai/frontend/webv2/src/workbench/shell/WorkbenchShell.tsx
index 1947160230..f0d86c3968 100644
--- a/invokeai/frontend/webv2/src/workbench/shell/WorkbenchShell.tsx
+++ b/invokeai/frontend/webv2/src/workbench/shell/WorkbenchShell.tsx
@@ -20,7 +20,6 @@ import {
getRegionDropState,
isWidgetDndData,
isWidgetInstanceDragData,
- measureDroppableVisibleRect,
resolveWidgetDragEnd,
type ActiveWidgetDrag,
widgetCollisionDetection,
@@ -55,16 +54,11 @@ const DND_MODIFIERS = [restrictToWindowEdges];
* image is actually held to scroll the list. Speed is halved because at
* dnd-kit's default a list this small dumps its full range in under half a
* second, far too fast to pick a row. Phantom off-screen targets triggering
- * scrolls from afar are prevented by DND_MEASURING, not by shrinking zones.
+ * scrolls from afar are prevented by the visibility check inside
+ * widgetCollisionDetection, not by shrinking zones.
*/
const DND_AUTO_SCROLL = { acceleration: 5 };
-/**
- * Droppables are measured clipped to their scroll containers so targets
- * scrolled out of view cannot catch drags (see measureDroppableVisibleRect).
- */
-const DND_MEASURING = { droppable: { measure: measureDroppableVisibleRect } };
-
export const WorkbenchShell = () => {
const { notifications, widgets } = useWorkbenchCommands();
const { t } = useTranslation();
@@ -233,7 +227,6 @@ export const WorkbenchShell = () => {
<DndContext
autoScroll={DND_AUTO_SCROLL}
collisionDetection={widgetCollisionDetection}
- measuring={DND_MEASURING}
modifiers={DND_MODIFIERS}
sensors={sensors}
onDragCancel={handleDragCancel}
diff --git a/invokeai/frontend/webv2/src/workbench/widgetDnd.browser.test.ts b/invokeai/frontend/webv2/src/workbench/widgetDnd.browser.test.ts
index 1c4b4d0aae..54b55dfa14 100644
--- a/invokeai/frontend/webv2/src/workbench/widgetDnd.browser.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/widgetDnd.browser.test.ts
@@ -1,6 +1,7 @@
+import { getClientRect, type ClientRect, type CollisionDetection, type DroppableContainer } from '@dnd-kit/core';
import { afterEach, describe, expect, it } from 'vitest';
-import { measureDroppableVisibleRect } from './widgetDnd';
+import { measureDroppableVisibleRect, widgetCollisionDetection } from './widgetDnd';
let host: HTMLDivElement | null = null;
@@ -95,3 +96,105 @@ describe('measureDroppableVisibleRect', () => {
expect(measureDroppableVisibleRect(target).height).toBe(40);
});
});
+
+describe('widgetCollisionDetection pointer visibility', () => {
+ // dnd-kit measures droppable rects once at drag start and only shifts them
+ // by ancestor scroll deltas afterwards, so these tests hand the detection
+ // the stale drag-start rect while the live DOM says otherwise.
+ const createCollisionArgs = (options: {
+ droppables: Array<{ id: string; node: HTMLElement; rect: ClientRect }>;
+ pointerCoordinates: { x: number; y: number };
+ }): Parameters<CollisionDetection>[0] => {
+ const { x, y } = options.pointerCoordinates;
+ const collisionRect = { bottom: y + 5, height: 10, left: x - 5, right: x + 5, top: y - 5, width: 10 };
+ const droppableContainers: DroppableContainer[] = options.droppables.map(({ id, node, rect }) => ({
+ data: { current: { kind: 'test-target' } },
+ disabled: false,
+ id,
+ key: id,
+ node: { current: node },
+ rect: { current: rect },
+ }));
+
+ return {
+ active: {
+ data: { current: { kind: 'test-drag' } },
+ id: 'active',
+ rect: { current: { initial: collisionRect, translated: collisionRect } },
+ },
+ collisionRect,
+ droppableContainers,
+ droppableRects: new Map(options.droppables.map(({ id, rect }) => [id, rect])),
+ pointerCoordinates: options.pointerCoordinates,
+ };
+ };
+
+ const mountBoardListLikeScroller = (): { scroller: HTMLElement; target: HTMLElement } => {
+ const root = mount(`
+ <div id="scroller" style="height:100px;overflow-y:auto;width:200px;">
+ <div style="height:120px;"></div>
+ <div id="target" style="height:40px;"></div>
+ </div>
+ `);
+
+ return {
+ scroller: root.querySelector<HTMLElement>('#scroller')!,
+ target: root.querySelector<HTMLElement>('#target')!,
+ };
+ };
+
+ it('ignores a pointer over the hidden part of a droppable scrolled out of view', () => {
+ const { target } = mountBoardListLikeScroller();
+ // Drag-start rect: unclipped, extending below the 100px scroller.
+ const staleRect = getClientRect(target);
+ const collisions = widgetCollisionDetection(
+ createCollisionArgs({
+ droppables: [{ id: 'board-row', node: target, rect: staleRect }],
+ pointerCoordinates: { x: 100, y: (staleRect.top + staleRect.bottom) / 2 },
+ })
+ );
+
+ expect(collisions).toEqual([]);
+ });
+
+ it('hits a droppable revealed by scrolling mid-drag despite its stale drag-start rect', () => {
+ const { scroller, target } = mountBoardListLikeScroller();
+ const dragStartRect = getClientRect(target);
+
+ scroller.scrollTop = 60;
+
+ // What dnd-kit's Rect getters report after the scroll: edges shifted by
+ // the delta, width/height still frozen from drag start.
+ const adjustedRect = { ...dragStartRect, bottom: dragStartRect.bottom - 60, top: dragStartRect.top - 60 };
+ const collisions = widgetCollisionDetection(
+ createCollisionArgs({
+ droppables: [{ id: 'board-row', node: target, rect: adjustedRect }],
+ pointerCoordinates: { x: 100, y: (adjustedRect.top + adjustedRect.bottom) / 2 },
+ })
+ );
+
+ expect(collisions.map(({ id }) => id)).toEqual(['board-row']);
+ });
+
+ it('keeps only the visibly hit droppable when a hidden row overlays another target', () => {
+ const { target } = mountBoardListLikeScroller();
+ const staleRect = getClientRect(target);
+ const grid = document.createElement('div');
+
+ grid.style.cssText = 'height:200px;left:0;position:fixed;top:100px;width:200px;';
+ host!.append(grid);
+
+ const pointer = { x: 100, y: (staleRect.top + staleRect.bottom) / 2 };
+ const collisions = widgetCollisionDetection(
+ createCollisionArgs({
+ droppables: [
+ { id: 'board-row', node: target, rect: staleRect },
+ { id: 'image-grid', node: grid, rect: getClientRect(grid) },
+ ],
+ pointerCoordinates: pointer,
+ })
+ );
+
+ expect(collisions.map(({ id }) => id)).toEqual(['image-grid']);
+ });
+});
diff --git a/invokeai/frontend/webv2/src/workbench/widgetDnd.ts b/invokeai/frontend/webv2/src/workbench/widgetDnd.ts
index 420f7d495c..eb947fe6c8 100644
--- a/invokeai/frontend/webv2/src/workbench/widgetDnd.ts
+++ b/invokeai/frontend/webv2/src/workbench/widgetDnd.ts
@@ -1,21 +1,28 @@
import type { WidgetRegion, WidgetRegionState } from '@workbench/layoutContracts';
import type { WidgetIconComponent, WidgetInstanceId, WidgetTypeId } from '@workbench/widgetContracts';
-import { closestCenter, getClientRect, pointerWithin, type CollisionDetection, type ClientRect } from '@dnd-kit/core';
+import {
+ closestCenter,
+ getClientRect,
+ pointerWithin,
+ type Collision,
+ type CollisionDetection,
+ type ClientRect,
+} from '@dnd-kit/core';
import { arrayMove } from '@dnd-kit/sortable';
const clipsOverflow = (value: string): boolean => value !== 'visible';
/**
- * Measures a droppable clipped by its overflow ancestors.
+ * Measures a droppable's rect clipped by its overflow ancestors — the part
+ * of it a user can actually see. A fully hidden droppable collapses to a
+ * zero-size rect no pointer can be within.
*
- * dnd-kit's default measure is the raw client rect, which for a droppable
- * scrolled out of a scroll container extends invisibly across whatever is
- * rendered below — a gallery board row below the fold overlays the entire
- * image grid. `pointerWithin` then reports drags as "over" that hidden row,
- * and the auto-scroller races its scroll container to the bottom. Clipping
- * the rect to what is actually visible removes the phantom targets: a fully
- * clipped droppable collapses to a zero-size rect no pointer can be within.
+ * This must NOT be used as `measuring.droppable.measure`: dnd-kit measures
+ * droppables once at drag start and only shifts the cached rect by ancestor
+ * scroll deltas (width/height stay frozen), so a row hidden at drag start
+ * would keep a zero-size rect even after auto-scroll reveals it. It is
+ * evaluated live at collision time instead (see widgetCollisionDetection).
*/
export const measureDroppableVisibleRect = (element: HTMLElement): ClientRect => {
const rect = { ...getClientRect(element) };
@@ -256,6 +263,40 @@ export const resolveWidgetDragEnd = (
};
};
+/**
+ * Drops pointer collisions with droppables the pointer is not visibly over.
+ *
+ * The rects `pointerWithin` tested against are cached at drag start and never
+ * re-clipped, so a droppable scrolled out of its container extends invisibly
+ * across whatever is rendered below — a gallery board row below the fold
+ * overlays the entire image grid, catching drags and racing the auto-scroller.
+ * Re-checking the pointer against the live visible rect removes those phantom
+ * hits while still letting a row that auto-scroll reveals mid-drag be hit the
+ * moment it appears.
+ */
+const dropOccludedPointerCollisions = (
+ collisions: Collision[],
+ args: Parameters<CollisionDetection>[0]
+): Collision[] => {
+ const pointer = args.pointerCoordinates;
+
+ if (!pointer) {
+ return collisions;
+ }
+
+ return collisions.filter((collision) => {
+ const node = args.droppableContainers.find((container) => container.id === collision.id)?.node.current;
+
+ if (!node) {
+ return true;
+ }
+
+ const rect = measureDroppableVisibleRect(node);
+
+ return pointer.x >= rect.left && pointer.x <= rect.right && pointer.y >= rect.top && pointer.y <= rect.bottom;
+ });
+};
+
export const widgetCollisionDetection: CollisionDetection = (args) => {
const activeData = args.active.data.current;
const collisionArgs = isWidgetInstanceDragData(activeData)
@@ -264,7 +305,7 @@ export const widgetCollisionDetection: CollisionDetection = (args) => {
...args,
droppableContainers: args.droppableContainers.filter((container) => !isWidgetDndData(container.data.current)),
};
- const pointerCollisions = pointerWithin(collisionArgs);
+ const pointerCollisions = dropOccludedPointerCollisions(pointerWithin(collisionArgs), args);
if (pointerCollisions.length > 0) {
const widgetItemCollisions = pointerCollisions.filter((collision) => {