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.

Deduplicate layer drag validation

Josh Corbettea6404f028e1
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/layers/LayersTree.browser.test.tsx b/invokeai/frontend/webv2/src/workbench/widgets/layers/LayersTree.browser.test.tsx
index 686907a3ee..87fa87150d 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/layers/LayersTree.browser.test.tsx
+++ b/invokeai/frontend/webv2/src/workbench/widgets/layers/LayersTree.browser.test.tsx
@@ -540,6 +540,31 @@ describe('LayersTree selection, surfaces and structure', () => {
expect(scroller.scrollTop).toBe(0);
});
+ it('validates a semantic drop command only once across equivalent adjacent hit regions', async () => {
+ await renderTree(trio());
+ const first = host!.querySelector<HTMLElement>('[data-layer-row-id="first"]')!;
+ const second = host!.querySelector<HTMLElement>('[data-layer-row-id="second"]')!;
+ const third = host!.querySelector<HTMLElement>('[data-layer-row-id="third"]')!;
+ const start = centre(first);
+ const secondRect = second.getBoundingClientRect();
+ const thirdRect = third.getBoundingClientRect();
+ await act(() => pointer('pointerdown', first, start.x, start.y));
+ await act(() => pointer('pointermove', document, start.x + 8, start.y));
+ await act(() => pointer('pointermove', document, start.x + 8, secondRect.bottom - 2));
+ await settle();
+ expect(refusalChecks).toHaveBeenCalled();
+ const belowSecond = JSON.stringify(refusalChecks.mock.calls.at(-1)?.[0]);
+
+ refusalChecks.mockClear();
+ await act(() => pointer('pointermove', document, start.x + 8, thirdRect.top + 2));
+ await settle();
+ expect(refusalChecks).not.toHaveBeenCalled();
+ await act(() => pointer('pointerup', document, start.x + 8, thirdRect.top + 2));
+
+ // Below the second row and above the third row describe the same insertion gap.
+ expect(belowSecond).toBe(JSON.stringify({ beforeId: 'third', ids: ['first'], parentId: null, type: 'reparent' }));
+ });
+
it('scrolls the list while a drag rests in the edge band and asks the model once per target', async () => {
await renderTree(manyLayers(200));
const scroller = host!.querySelector<HTMLElement>('[role="tree"]')!.closest<HTMLElement>('[data-part="viewport"]')!;
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/layers/LayersTree.tsx b/invokeai/frontend/webv2/src/workbench/widgets/layers/LayersTree.tsx
index eb531aa4d3..90a9affdb1 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/layers/LayersTree.tsx
+++ b/invokeai/frontend/webv2/src/workbench/widgets/layers/LayersTree.tsx
@@ -128,6 +128,15 @@ const edgeOf = (
const childEdgeOf = (rect: { top: number; height: number } | undefined, y: number): 'above' | 'below' =>
!rect || y >= rect.top + rect.height / 2 ? 'below' : 'above';
+type ReparentCommand = Extract<DocumentCommand, { type: 'reparent' }>;
+
+const reparentCommandForTarget = (target: LayerDropTarget): ReparentCommand => ({
+ beforeId: target.beforeId,
+ ids: target.ids,
+ parentId: target.parentId,
+ type: 'reparent',
+});
+
const isMenuKey = (event: KeyboardEvent<HTMLElement>): boolean =>
event.key === 'ContextMenu' || (event.key === 'F10' && event.shiftKey);
@@ -874,15 +883,17 @@ export const LayersTree = ({
rows: stacks[drag.stack].rows,
});
}, [drag, stacks]);
+ // Dnd-kit can describe one insertion gap as both "below row N" and "above row N+1". Key the
+ // model check by the semantic command so virtualized auto-scroll does not repeat the same
+ // validation. Finishing the drag clears the target, and the prepared commit revalidates against
+ // the current document before applying the landing.
+ const refusalCommandKey = target ? JSON.stringify(reparentCommandForTarget(target)) : null;
const refusal = useMemo(
() =>
- target && engine
- ? (engine.document
- .model()
- ?.refusalFor({ beforeId: target.beforeId, ids: target.ids, parentId: target.parentId, type: 'reparent' }) ??
- null)
+ refusalCommandKey && engine
+ ? (engine.document.model()?.refusalFor(JSON.parse(refusalCommandKey) as ReparentCommand) ?? null)
: null,
- [engine, target]
+ [engine, refusalCommandKey]
);
const childDropPlan = useMemo(() => {