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(dnd): route gallery drags to canvas targets

joshistoast0f1c832ad25d
diff --git a/invokeai/frontend/webv2/src/workbench/widgetDnd.test.ts b/invokeai/frontend/webv2/src/workbench/widgetDnd.test.ts
index 6a1096162a..9fe8a15d2e 100644
--- a/invokeai/frontend/webv2/src/workbench/widgetDnd.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/widgetDnd.test.ts
@@ -1,3 +1,5 @@
+import type { CollisionDetection, DroppableContainer } from '@dnd-kit/core';
+
import { describe, expect, it } from 'vitest';
import type { Project, WidgetTypeId, WorkbenchState } from './types';
@@ -8,9 +10,53 @@ import {
getWidgetInstanceDragId,
getWidgetRegionDropData,
resolveWidgetDragEnd,
+ widgetCollisionDetection,
} from './widgetDnd';
+import { getCanvasImageDropData } from './widgets/canvas/canvasImageDnd';
+import { getGalleryImageDragData } from './widgets/gallery/galleryDnd';
import { createInitialWorkbenchState } from './workbenchState';
+type CollisionArgs = Parameters<CollisionDetection>[0];
+
+const getRect = (left: number, top: number, width: number, height: number) => ({
+ bottom: top + height,
+ height,
+ left,
+ right: left + width,
+ top,
+ width,
+});
+
+const createDroppable = (id: string, data: object, rect: ReturnType<typeof getRect>): DroppableContainer => ({
+ data: { current: data as Record<string, unknown> },
+ disabled: false,
+ id,
+ key: id,
+ node: { current: null },
+ rect: { current: rect },
+});
+
+const createCollisionArgs = (options: {
+ activeData: object;
+ collisionRect?: ReturnType<typeof getRect>;
+ droppables: Array<{ data: object; id: string; rect: ReturnType<typeof getRect> }>;
+ pointerCoordinates: { x: number; y: number } | null;
+}): CollisionArgs => {
+ const collisionRect = options.collisionRect ?? getRect(45, 45, 10, 10);
+
+ return {
+ active: {
+ data: { current: options.activeData as Record<string, unknown> },
+ id: 'active',
+ rect: { current: { initial: collisionRect, translated: collisionRect } },
+ },
+ collisionRect,
+ droppableContainers: options.droppables.map(({ data, id, rect }) => createDroppable(id, data, rect)),
+ droppableRects: new Map(options.droppables.map(({ id, rect }) => [id, rect])),
+ pointerCoordinates: options.pointerCoordinates,
+ };
+};
+
const getWidget = (typeId: WidgetTypeId) => {
const widgets: Partial<
Record<
@@ -138,3 +184,136 @@ describe('widget dnd helpers', () => {
});
});
});
+
+describe('widget collision detection', () => {
+ const outerRect = getRect(0, 0, 100, 100);
+ const innerRect = getRect(25, 25, 50, 50);
+ const pointerCoordinates = { x: 50, y: 50 };
+
+ it('routes a gallery image drag to an inner canvas target instead of its enclosing widget', () => {
+ const collisions = widgetCollisionDetection(
+ createCollisionArgs({
+ activeData: getGalleryImageDragData([{ boardId: 'none', imageName: 'image.png' }]),
+ droppables: [
+ {
+ data: getWidgetInstanceDragData('center', 'canvas', 'canvas'),
+ id: 'canvas-widget',
+ rect: outerRect,
+ },
+ { data: getWidgetRegionDropData('center'), id: 'center-region', rect: outerRect },
+ { data: getCanvasImageDropData('raster'), id: 'canvas-raster-target', rect: innerRect },
+ { data: { kind: 'future-target' }, id: 'future-target', rect: innerRect },
+ ],
+ pointerCoordinates,
+ })
+ );
+
+ expect(collisions.map(({ id }) => id)).toEqual(['canvas-raster-target', 'future-target']);
+ });
+
+ it('keeps widget instances ahead of overlapping non-widget targets for widget drags', () => {
+ const activeData = getWidgetInstanceDragData('center', 'preview', 'preview');
+ const collisions = widgetCollisionDetection(
+ createCollisionArgs({
+ activeData,
+ droppables: [
+ { data: getWidgetInstanceDragData('center', 'canvas', 'canvas'), id: 'canvas-widget', rect: outerRect },
+ { data: getCanvasImageDropData('raster'), id: 'canvas-raster-target', rect: innerRect },
+ ],
+ pointerCoordinates,
+ })
+ );
+
+ expect(collisions.map(({ id }) => id)).toEqual(['canvas-widget']);
+ });
+
+ it('keeps widget regions ahead of overlapping non-widget targets for widget drags', () => {
+ const activeData = getWidgetInstanceDragData('center', 'preview', 'preview');
+ const collisions = widgetCollisionDetection(
+ createCollisionArgs({
+ activeData,
+ droppables: [
+ { data: getWidgetRegionDropData('right'), id: 'right-region', rect: outerRect },
+ { data: getCanvasImageDropData('raster'), id: 'canvas-raster-target', rect: innerRect },
+ ],
+ pointerCoordinates,
+ })
+ );
+
+ expect(collisions.map(({ id }) => id)).toEqual(['right-region']);
+ });
+
+ it('treats malformed widget active data as a non-widget drag', () => {
+ const collisions = widgetCollisionDetection(
+ createCollisionArgs({
+ activeData: { instanceId: 'preview', kind: 'widget-instance', region: 'center' },
+ droppables: [
+ { data: getWidgetInstanceDragData('center', 'canvas', 'canvas'), id: 'canvas-widget', rect: outerRect },
+ { data: getCanvasImageDropData('raster'), id: 'canvas-raster-target', rect: innerRect },
+ ],
+ pointerCoordinates,
+ })
+ );
+
+ expect(collisions.map(({ id }) => id)).toEqual(['canvas-raster-target']);
+ });
+
+ it('excludes widget droppables from the no-pointer closest-center fallback for non-widget drags', () => {
+ const collisions = widgetCollisionDetection(
+ createCollisionArgs({
+ activeData: getGalleryImageDragData([{ boardId: 'none', imageName: 'image.png' }]),
+ collisionRect: getRect(0, 0, 20, 20),
+ droppables: [
+ {
+ data: getWidgetInstanceDragData('center', 'canvas', 'canvas'),
+ id: 'canvas-widget',
+ rect: getRect(0, 0, 20, 20),
+ },
+ {
+ data: getWidgetRegionDropData('center'),
+ id: 'center-region',
+ rect: getRect(20, 20, 20, 20),
+ },
+ {
+ data: getCanvasImageDropData('raster'),
+ id: 'canvas-raster-target',
+ rect: getRect(100, 100, 20, 20),
+ },
+ { data: { kind: 'future-target' }, id: 'future-target', rect: getRect(100, 100, 20, 20) },
+ ],
+ pointerCoordinates: null,
+ })
+ );
+
+ expect(collisions.map(({ id }) => id)).toEqual(['canvas-raster-target', 'future-target']);
+ });
+
+ it('keeps the existing no-pointer closest-center behavior for widget drags', () => {
+ const collisions = widgetCollisionDetection(
+ createCollisionArgs({
+ activeData: getWidgetInstanceDragData('center', 'preview', 'preview'),
+ collisionRect: getRect(0, 0, 20, 20),
+ droppables: [
+ {
+ data: getWidgetInstanceDragData('center', 'canvas', 'canvas'),
+ id: 'canvas-widget',
+ rect: getRect(0, 0, 20, 20),
+ },
+ {
+ data: getWidgetRegionDropData('right'),
+ id: 'right-region',
+ rect: getRect(20, 20, 20, 20),
+ },
+ {
+ data: getCanvasImageDropData('raster'),
+ id: 'canvas-raster-target',
+ rect: getRect(100, 100, 20, 20),
+ },
+ ],
+ pointerCoordinates: null,
+ })
+ );
+
+ expect(collisions.map(({ id }) => id)).toEqual(['canvas-widget', 'right-region', 'canvas-raster-target']);
+ });
+});
diff --git a/invokeai/frontend/webv2/src/workbench/widgetDnd.ts b/invokeai/frontend/webv2/src/workbench/widgetDnd.ts
index 73afda14f5..184b3f1789 100644
--- a/invokeai/frontend/webv2/src/workbench/widgetDnd.ts
+++ b/invokeai/frontend/webv2/src/workbench/widgetDnd.ts
@@ -80,7 +80,8 @@ export const isWidgetInstanceDragData = (data: unknown): data is WidgetInstanceD
isRecord(data) &&
data.kind === 'widget-instance' &&
typeof data.instanceId === 'string' &&
- isWidgetRegion(data.region);
+ isWidgetRegion(data.region) &&
+ typeof data.typeId === 'string';
export const isWidgetRegionDropData = (data: unknown): data is WidgetRegionDropData =>
isRecord(data) && data.kind === 'widget-region' && isWidgetRegion(data.region);
@@ -214,7 +215,13 @@ export const resolveWidgetDragEnd = (
export const widgetCollisionDetection: CollisionDetection = (args) => {
const activeData = args.active.data.current;
- const pointerCollisions = pointerWithin(args);
+ const collisionArgs = isWidgetInstanceDragData(activeData)
+ ? args
+ : {
+ ...args,
+ droppableContainers: args.droppableContainers.filter((container) => !isWidgetDndData(container.data.current)),
+ };
+ const pointerCollisions = pointerWithin(collisionArgs);
if (pointerCollisions.length > 0) {
const widgetItemCollisions = pointerCollisions.filter((collision) => {
@@ -258,7 +265,7 @@ export const widgetCollisionDetection: CollisionDetection = (args) => {
return pointerCollisions;
}
- return args.pointerCoordinates ? [] : closestCenter(args);
+ return args.pointerCoordinates ? [] : closestCenter(collisionArgs);
};
const getCollisionData = (args: Parameters<CollisionDetection>[0], id: unknown): unknown =>
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/canvas/canvasImageDnd.test.ts b/invokeai/frontend/webv2/src/workbench/widgets/canvas/canvasImageDnd.test.ts
new file mode 100644
index 0000000000..31daeee8b3
--- /dev/null
+++ b/invokeai/frontend/webv2/src/workbench/widgets/canvas/canvasImageDnd.test.ts
@@ -0,0 +1,29 @@
+import { describe, expect, it } from 'vitest';
+
+import { getCanvasImageDropData, isCanvasImageDropData } from './canvasImageDnd';
+
+describe('canvasImageDnd', () => {
+ it.each(['raster', 'control', 'regional-reference', 'inpaint-mask', 'control-resized'] as const)(
+ 'round-trips the %s destination through the factory and guard',
+ (destination) => {
+ const data = getCanvasImageDropData(destination);
+
+ expect(data).toEqual({ destination, kind: 'canvas-image-target' });
+ expect(isCanvasImageDropData(data)).toBe(true);
+ }
+ );
+
+ it.each([
+ null,
+ undefined,
+ 'canvas-image-target',
+ {},
+ { destination: 'raster' },
+ { kind: 'canvas-image-target' },
+ { destination: 'regional-guidance', kind: 'canvas-image-target' },
+ { destination: 'unknown', kind: 'canvas-image-target' },
+ { destination: null, kind: 'canvas-image-target' },
+ ])('rejects malformed canvas image drop data: %j', (value) => {
+ expect(isCanvasImageDropData(value)).toBe(false);
+ });
+});
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/canvas/canvasImageDnd.ts b/invokeai/frontend/webv2/src/workbench/widgets/canvas/canvasImageDnd.ts
new file mode 100644
index 0000000000..790416e4cf
--- /dev/null
+++ b/invokeai/frontend/webv2/src/workbench/widgets/canvas/canvasImageDnd.ts
@@ -0,0 +1,27 @@
+import type { GalleryCanvasImportDestination } from '@workbench/canvas-operations/api';
+
+export interface CanvasImageDropData {
+ kind: 'canvas-image-target';
+ destination: Exclude<GalleryCanvasImportDestination, 'regional-guidance'>;
+}
+
+export const isCanvasImageDropData = (value: unknown): value is CanvasImageDropData =>
+ isRecord(value) && value.kind === 'canvas-image-target' && isCanvasImageDropDestination(value.destination);
+
+export const getCanvasImageDropData = (destination: CanvasImageDropData['destination']): CanvasImageDropData => ({
+ destination,
+ kind: 'canvas-image-target',
+});
+
+const canvasImageDropDestinations = {
+ control: true,
+ 'control-resized': true,
+ 'inpaint-mask': true,
+ raster: true,
+ 'regional-reference': true,
+} satisfies Record<CanvasImageDropData['destination'], true>;
+
+const isCanvasImageDropDestination = (value: unknown): value is CanvasImageDropData['destination'] =>
+ typeof value === 'string' && Object.hasOwn(canvasImageDropDestinations, value);
+
+const isRecord = (value: unknown): value is Record<string, unknown> => typeof value === 'object' && value !== null;