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): refuse to compare the previewed image with itself

joshistoast71a0443b5898
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewCompareDropZone.browser.test.tsx b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewCompareDropZone.browser.test.tsx
index 7242678b48..49ee19ed9f 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewCompareDropZone.browser.test.tsx
+++ b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewCompareDropZone.browser.test.tsx
@@ -59,26 +59,33 @@ const DraggableThumb = () => {
);
};
-const DropMonitor = ({ onDrop }: { onDrop: (resolution: { imageName: string } | null) => void }) => {
+const DropMonitor = ({
+ currentImageName,
+ onDrop,
+}: {
+ currentImageName: string | null;
+ onDrop: (resolution: { imageName: string } | null) => void;
+}) => {
useDndMonitor({
onDragEnd: (event: DragEndEvent) =>
- onDrop(resolvePreviewCompareDrop(event.active.data.current, event.over?.data.current ?? null)),
+ onDrop(resolvePreviewCompareDrop(event.active.data.current, event.over?.data.current ?? null, currentImageName)),
});
return null;
};
-const renderHarness = async () => {
+/** `currentImageName` is what the frame is showing; the thumb drags `dragged.png`. */
+const renderHarness = async (currentImageName: string | null = null) => {
const onDrop = vi.fn();
const Harness = () => {
const sensors = useSensors(useSensor(PointerSensor, { activationConstraint: { distance: 6 } }));
return (
<DndContext collisionDetection={widgetCollisionDetection} sensors={sensors}>
- <DropMonitor onDrop={onDrop} />
+ <DropMonitor currentImageName={currentImageName} onDrop={onDrop} />
<DraggableThumb />
<div data-testid="frame" style={{ height: 300, left: 200, position: 'fixed', top: 100, width: 300 }}>
- <PreviewCompareDropZone />
+ <PreviewCompareDropZone currentImageName={currentImageName} />
</div>
</DndContext>
);
@@ -144,4 +151,36 @@ describe('PreviewCompareDropZone', () => {
expect(onDrop).toHaveBeenCalledWith(null);
});
+
+ it('offers no ring, and arms nothing, when the drag is the previewed image', async () => {
+ // Comparing an image with itself does nothing visible but still pauses
+ // live-follow, so the affordance has to be absent rather than merely inert.
+ const { onDrop } = await renderHarness('dragged.png');
+ const thumb = document.querySelector<HTMLElement>('[data-testid="thumb"]')!;
+
+ await interact(() => pointer('pointerdown', thumb, 30, 30));
+ await interact(() => pointer('pointermove', thumb.ownerDocument, 60, 60));
+
+ expect(document.body.textContent).not.toContain('Drop to compare');
+
+ await interact(() => pointer('pointermove', thumb.ownerDocument, 350, 250));
+ await interact(() => pointer('pointerup', thumb.ownerDocument, 350, 250));
+
+ expect(onDrop).toHaveBeenCalledWith(null);
+ });
+
+ it('still offers the ring for an image other than the previewed one', async () => {
+ const { onDrop } = await renderHarness('shown.png');
+ const thumb = document.querySelector<HTMLElement>('[data-testid="thumb"]')!;
+
+ await interact(() => pointer('pointerdown', thumb, 30, 30));
+ await interact(() => pointer('pointermove', thumb.ownerDocument, 60, 60));
+
+ expect(document.body.textContent).toContain('Drop to compare');
+
+ await interact(() => pointer('pointermove', thumb.ownerDocument, 350, 250));
+ await interact(() => pointer('pointerup', thumb.ownerDocument, 350, 250));
+
+ expect(onDrop).toHaveBeenCalledWith({ imageName: 'dragged.png' });
+ });
});
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewCompareDropZone.tsx b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewCompareDropZone.tsx
index e7096f9e1e..d382d91ce9 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewCompareDropZone.tsx
+++ b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewCompareDropZone.tsx
@@ -10,13 +10,25 @@ import { PREVIEW_COMPARE_DROP_DATA, PREVIEW_COMPARE_DROP_ID } from './previewCom
* A quiet drop ring over the preview frame, visible only while an all-image gallery-item
* drag is in flight. Dropping arms the dragged image for comparison (resolved
* by the widget shell's dnd monitor).
+ *
+ * The image already on screen is refused wherever it was dragged from — the
+ * frame itself, the filmstrip, or the gallery grid — because comparing an image
+ * with itself does nothing but pause live-follow. Only `items[0]` is tested,
+ * since that is the item `resolvePreviewCompareDrop` would arm.
*/
-export const PreviewCompareDropZone = () => {
+export const PreviewCompareDropZone = ({ currentImageName }: { currentImageName: string | null }) => {
const { t } = useTranslation();
const { active } = useDndContext();
- const { isOver, setNodeRef } = useDroppable({ data: PREVIEW_COMPARE_DROP_DATA, id: PREVIEW_COMPARE_DROP_ID });
+ const activeData = active?.data.current;
+ const isCompatible = isGalleryImageDragData(activeData);
+ const isSelfCompare = isCompatible && activeData.items[0]?.name === currentImageName;
+ const { isOver, setNodeRef } = useDroppable({
+ data: PREVIEW_COMPARE_DROP_DATA,
+ disabled: !isCompatible || isSelfCompare,
+ id: PREVIEW_COMPARE_DROP_ID,
+ });
- if (!isGalleryImageDragData(active?.data.current)) {
+ if (!isCompatible || isSelfCompare) {
return null;
}
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewFrame.tsx b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewFrame.tsx
index cdb1f8f452..8b3d84de90 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewFrame.tsx
+++ b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewFrame.tsx
@@ -243,7 +243,7 @@ const PreviewImageFrame = ({
w="full"
{...loupe.stageProps}
>
- <PreviewCompareDropZone />
+ <PreviewCompareDropZone currentImageName={dragItem?.kind === 'image' ? dragItem.name : null} />
<Box
ref={setContentRef}
{...listeners}
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewFrameDrag.browser.test.tsx b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewFrameDrag.browser.test.tsx
index 5795c66b47..300209a6e4 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewFrameDrag.browser.test.tsx
+++ b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewFrameDrag.browser.test.tsx
@@ -152,4 +152,23 @@ describe('PreviewFrame image drag', () => {
kind: 'gallery-item',
});
});
+
+ it('does not offer its own compare target to the image it is showing', async () => {
+ // Dropping the previewed image back on the frame used to arm a comparison
+ // of the image with itself: invisible, because compare mode requires the
+ // two to differ, but it still paused live-follow and left a comparison
+ // primed to spring open on the next selection.
+ const onDrop = await renderHarness();
+ const image = host!.querySelector<HTMLImageElement>('img[alt="preview.png"]')!;
+
+ await interact(() => pointer('pointerdown', image, 140, 140));
+ await interact(() => pointer('pointermove', image.ownerDocument, 170, 140));
+
+ expect(document.body.textContent).not.toContain('Drop to compare');
+
+ await interact(() => pointer('pointermove', image.ownerDocument, 150, 150));
+ await interact(() => pointer('pointerup', image.ownerDocument, 150, 150));
+
+ expect(onDrop.mock.calls[0]?.[0]?.overId).toBeNull();
+ });
});
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewWidgetView.tsx b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewWidgetView.tsx
index 1caee89988..43ad98500e 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewWidgetView.tsx
+++ b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewWidgetView.tsx
@@ -699,7 +699,13 @@ export const PreviewWidgetView = ({ region, runtime }: WidgetViewProps) => {
return;
}
- const resolution = resolvePreviewCompareDrop(event.active.data.current, event.over?.data.current ?? null);
+ // The image on screen is refused, so this can never resolve to the
+ // selection itself.
+ const resolution = resolvePreviewCompareDrop(
+ event.active.data.current,
+ event.over?.data.current ?? null,
+ selectedItem.name
+ );
if (!resolution) {
return;
@@ -707,9 +713,7 @@ export const PreviewWidgetView = ({ region, runtime }: WidgetViewProps) => {
// Prefer images we already hold (board context includes fresh local
// generations that would 404 on a backend by-name fetch).
- const localImageItem =
- boardItems.find((item) => item.kind === 'image' && item.name === resolution.imageName) ??
- (selectedItem.name === resolution.imageName ? selectedItem : null);
+ const localImageItem = boardItems.find((item) => item.kind === 'image' && item.name === resolution.imageName);
if (localImageItem?.kind === 'image') {
gallery.setCompareItem(localImageItem);
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/preview/previewCompareDnd.test.ts b/invokeai/frontend/webv2/src/workbench/widgets/preview/previewCompareDnd.test.ts
index 5658227a0e..3e73cfc8bf 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/preview/previewCompareDnd.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/widgets/preview/previewCompareDnd.test.ts
@@ -3,6 +3,9 @@ import { describe, expect, it } from 'vitest';
import { PREVIEW_COMPARE_DROP_DATA, resolvePreviewCompareDrop } from './previewCompareDnd';
+/** Nothing on screen, so no drop can be a self-compare. */
+const NOTHING_PREVIEWED = null;
+
describe('resolvePreviewCompareDrop', () => {
it('resolves the first dragged gallery image', () => {
const activeData = getGalleryItemDragData([
@@ -10,22 +13,27 @@ describe('resolvePreviewCompareDrop', () => {
{ kind: 'image', name: 'second.png' },
]);
- expect(resolvePreviewCompareDrop(activeData, PREVIEW_COMPARE_DROP_DATA)).toEqual({ imageName: 'first.png' });
+ expect(resolvePreviewCompareDrop(activeData, PREVIEW_COMPARE_DROP_DATA, NOTHING_PREVIEWED)).toEqual({
+ imageName: 'first.png',
+ });
});
it('ignores drops that are not on the compare target', () => {
const activeData = getGalleryItemDragData([{ kind: 'image', name: 'first.png' }]);
- expect(resolvePreviewCompareDrop(activeData, { kind: 'gallery-board' })).toBeNull();
- expect(resolvePreviewCompareDrop(activeData, null)).toBeNull();
+ expect(resolvePreviewCompareDrop(activeData, { kind: 'gallery-board' }, NOTHING_PREVIEWED)).toBeNull();
+ expect(resolvePreviewCompareDrop(activeData, null, NOTHING_PREVIEWED)).toBeNull();
});
it('ignores non-image, video, mixed, and empty drags', () => {
- expect(resolvePreviewCompareDrop({ kind: 'widget-instance' }, PREVIEW_COMPARE_DROP_DATA)).toBeNull();
+ expect(
+ resolvePreviewCompareDrop({ kind: 'widget-instance' }, PREVIEW_COMPARE_DROP_DATA, NOTHING_PREVIEWED)
+ ).toBeNull();
expect(
resolvePreviewCompareDrop(
getGalleryItemDragData([{ kind: 'video', name: 'clip.mp4' }]),
- PREVIEW_COMPARE_DROP_DATA
+ PREVIEW_COMPARE_DROP_DATA,
+ NOTHING_PREVIEWED
)
).toBeNull();
expect(
@@ -34,9 +42,48 @@ describe('resolvePreviewCompareDrop', () => {
{ kind: 'image', name: 'still.png' },
{ kind: 'video', name: 'clip.mp4' },
]),
- PREVIEW_COMPARE_DROP_DATA
+ PREVIEW_COMPARE_DROP_DATA,
+ NOTHING_PREVIEWED
)
).toBeNull();
- expect(resolvePreviewCompareDrop({ items: [], kind: 'gallery-item' }, PREVIEW_COMPARE_DROP_DATA)).toBeNull();
+ expect(
+ resolvePreviewCompareDrop({ items: [], kind: 'gallery-item' }, PREVIEW_COMPARE_DROP_DATA, NOTHING_PREVIEWED)
+ ).toBeNull();
+ });
+
+ it('refuses to compare the previewed image with itself', () => {
+ // Not merely a no-op: arming a comparison pauses live-follow, so this used
+ // to switch off in-progress images and leave a comparison that sprang open
+ // on the next selection.
+ const activeData = getGalleryItemDragData([{ kind: 'image', name: 'shown.png' }]);
+
+ expect(resolvePreviewCompareDrop(activeData, PREVIEW_COMPARE_DROP_DATA, 'shown.png')).toBeNull();
+ });
+
+ it('still resolves any other image while one is previewed', () => {
+ const activeData = getGalleryItemDragData([{ kind: 'image', name: 'other.png' }]);
+
+ expect(resolvePreviewCompareDrop(activeData, PREVIEW_COMPARE_DROP_DATA, 'shown.png')).toEqual({
+ imageName: 'other.png',
+ });
+ });
+
+ it('judges a multi-image drag by the image it would arm', () => {
+ // Only `items[0]` is compared, because only `items[0]` is ever armed — a
+ // drag whose *first* image is the previewed one is refused even though the
+ // rest of the selection differs, and vice versa.
+ const leadingIsPreviewed = getGalleryItemDragData([
+ { kind: 'image', name: 'shown.png' },
+ { kind: 'image', name: 'other.png' },
+ ]);
+ const trailingIsPreviewed = getGalleryItemDragData([
+ { kind: 'image', name: 'other.png' },
+ { kind: 'image', name: 'shown.png' },
+ ]);
+
+ expect(resolvePreviewCompareDrop(leadingIsPreviewed, PREVIEW_COMPARE_DROP_DATA, 'shown.png')).toBeNull();
+ expect(resolvePreviewCompareDrop(trailingIsPreviewed, PREVIEW_COMPARE_DROP_DATA, 'shown.png')).toEqual({
+ imageName: 'other.png',
+ });
});
});
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/preview/previewCompareDnd.ts b/invokeai/frontend/webv2/src/workbench/widgets/preview/previewCompareDnd.ts
index 2c52674f84..0d8e516750 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/preview/previewCompareDnd.ts
+++ b/invokeai/frontend/webv2/src/workbench/widgets/preview/previewCompareDnd.ts
@@ -17,13 +17,26 @@ export const PREVIEW_COMPARE_DROP_DATA: PreviewCompareDropData = { kind: 'previe
export const isPreviewCompareDropData = (value: unknown): value is PreviewCompareDropData =>
typeof value === 'object' && value !== null && (value as PreviewCompareDropData).kind === 'preview-compare-target';
-/** The image to arm for comparison, or null when the drag/drop pair is not ours. */
-export const resolvePreviewCompareDrop = (activeData: unknown, overData: unknown): { imageName: string } | null => {
+/**
+ * The image to arm for comparison, or null when the drag/drop pair is not ours
+ * — or when the drop would compare the previewed image with itself. That is not
+ * merely a no-op: arming a comparison pauses live-follow, so a self-drop would
+ * quietly switch off in-progress images and leave a comparison that springs
+ * open on the next selection.
+ *
+ * `currentImageName` is required rather than optional so every caller has to
+ * say what is on screen; a caller with nothing selected passes null.
+ */
+export const resolvePreviewCompareDrop = (
+ activeData: unknown,
+ overData: unknown,
+ currentImageName: string | null
+): { imageName: string } | null => {
if (!isGalleryImageDragData(activeData) || !isPreviewCompareDropData(overData)) {
return null;
}
const imageName = activeData.items[0]?.name;
- return imageName ? { imageName } : null;
+ return imageName && imageName !== currentImageName ? { imageName } : null;
};