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(ui): improve preset dragging

joshistoastb74c21e1185b
diff --git a/invokeai/frontend/webv2/src/workbench/shell/topbar/LayoutPresetStrip.browser.test.tsx b/invokeai/frontend/webv2/src/workbench/shell/topbar/LayoutPresetStrip.browser.test.tsx
index 6b1ddb08c4..64e6c9227e 100644
--- a/invokeai/frontend/webv2/src/workbench/shell/topbar/LayoutPresetStrip.browser.test.tsx
+++ b/invokeai/frontend/webv2/src/workbench/shell/topbar/LayoutPresetStrip.browser.test.tsx
@@ -80,9 +80,24 @@ const presetTabIds = () =>
const presetTab = (id: string): HTMLButtonElement | null =>
document.querySelector<HTMLButtonElement>(`[role="tab"][data-layout-preset-id="${id}"]`);
-const pointer = (type: string, target: EventTarget, clientX: number, clientY: number): void => {
+const mouse = (type: string, target: EventTarget, clientX: number, clientY: number): void => {
target.dispatchEvent(
- new PointerEvent(type, { bubbles: true, button: 0, clientX, clientY, isPrimary: true, pointerId: 1 })
+ new MouseEvent(type, { bubbles: true, button: 0, buttons: type === 'mouseup' ? 0 : 1, clientX, clientY })
+ );
+};
+
+const touch = (type: string, target: EventTarget, touchTarget: EventTarget, clientX: number, clientY: number): void => {
+ const point = new Touch({ clientX, clientY, identifier: 1, target: touchTarget });
+ const isEnd = type === 'touchend' || type === 'touchcancel';
+
+ target.dispatchEvent(
+ new TouchEvent(type, {
+ bubbles: true,
+ cancelable: true,
+ changedTouches: [point],
+ targetTouches: isEnd ? [] : [point],
+ touches: isEnd ? [] : [point],
+ })
);
};
@@ -127,16 +142,48 @@ describe('LayoutPresetStrip', () => {
const startY = start.top + start.height / 2;
const endX = end.left + end.width / 2;
- await act(() => pointer('pointerdown', source!, startX, startY));
- await act(() => pointer('pointermove', source!.ownerDocument, startX + 8, startY));
- await act(() => pointer('pointermove', source!.ownerDocument, endX, startY));
- await act(() => pointer('pointerup', source!.ownerDocument, endX, startY));
+ await act(() => mouse('mousedown', source!, startX, startY));
+ await act(() => mouse('mousemove', source!.ownerDocument, startX + 8, startY));
+ await act(() => mouse('mousemove', source!.ownerDocument, endX, startY));
+ await act(() => mouse('mouseup', source!.ownerDocument, endX, startY));
expect(store.getSnapshot().account.layoutPresetOrder).toEqual(['custom-1', 'edit', 'automate', 'compose']);
expect(store.getSnapshot().activeProject.layout.presetId).toBe(activeBeforeDrag);
});
- it('does not auto-scroll the strip while dragging near its right edge', async () => {
+ it('keeps a non-overflowing strip stationary while dragging near its right edge', async () => {
+ await renderStrip();
+ const scrollContainer = document.querySelector<HTMLElement>('[data-layout-preset-scroll]');
+ const source = presetTab('compose');
+ expect(scrollContainer).not.toBeNull();
+ expect(source).not.toBeNull();
+ Object.defineProperty(scrollContainer!, 'scrollWidth', {
+ configurable: true,
+ value: scrollContainer!.clientWidth,
+ });
+ expect(scrollContainer!.scrollWidth).toBeLessThanOrEqual(scrollContainer!.clientWidth);
+ const sourceRect = source!.getBoundingClientRect();
+ const containerRect = scrollContainer!.getBoundingClientRect();
+ const startX = sourceRect.left + sourceRect.width / 2;
+ const startY = sourceRect.top + sourceRect.height / 2;
+ const edgeX = containerRect.right - 2;
+
+ await act(() => mouse('mousedown', source!, startX, startY));
+ await act(() => mouse('mousemove', source!.ownerDocument, startX + 8, startY));
+ await act(() => mouse('mousemove', source!.ownerDocument, edgeX, startY));
+ await act(
+ () =>
+ new Promise<void>((resolve) => {
+ globalThis.setTimeout(resolve, 150);
+ })
+ );
+
+ expect(scrollContainer!.scrollLeft).toBe(0);
+
+ await act(() => mouse('mouseup', source!.ownerDocument, edgeX, startY));
+ });
+
+ it('auto-scrolls only an overflowing strip and stops when the drag ends', async () => {
for (let index = 2; index <= 8; index += 1) {
store.commands.layout.createPreset(`custom-${index}`, `Custom ${index}`, 'star');
}
@@ -152,18 +199,63 @@ describe('LayoutPresetStrip', () => {
const startY = sourceRect.top + sourceRect.height / 2;
const edgeX = containerRect.right - 2;
- await act(() => pointer('pointerdown', source!, startX, startY));
- await act(() => pointer('pointermove', source!.ownerDocument, startX + 8, startY));
- await act(() => pointer('pointermove', source!.ownerDocument, edgeX, startY));
+ await act(() => mouse('mousedown', source!, startX, startY));
+ await act(() => mouse('mousemove', source!.ownerDocument, startX + 8, startY));
+ await act(() => mouse('mousemove', source!.ownerDocument, edgeX, startY));
await act(
() =>
new Promise<void>((resolve) => {
- globalThis.setTimeout(resolve, 150);
+ globalThis.setTimeout(resolve, 250);
})
);
- expect(scrollContainer!.scrollLeft).toBe(0);
+ expect(scrollContainer!.scrollLeft).toBeGreaterThan(0);
- await act(() => pointer('pointerup', source!.ownerDocument, edgeX, startY));
+ await act(() => mouse('mouseup', source!.ownerDocument, edgeX, startY));
+ const scrollLeftAfterDrop = scrollContainer!.scrollLeft;
+ await act(
+ () =>
+ new Promise<void>((resolve) => {
+ globalThis.setTimeout(resolve, 100);
+ })
+ );
+
+ expect(scrollContainer!.scrollLeft).toBe(scrollLeftAfterDrop);
+ });
+
+ it('preserves horizontal touch panning and reorders after a long press', async () => {
+ await renderStrip();
+ const source = presetTab('compose');
+ const target = presetTab('automate');
+ expect(source).not.toBeNull();
+ expect(target).not.toBeNull();
+ expect(getComputedStyle(source!).touchAction).toBe('pan-x');
+ const start = source!.getBoundingClientRect();
+ const end = target!.getBoundingClientRect();
+ const startX = start.left + start.width / 2;
+ const startY = start.top + start.height / 2;
+ const endX = end.left + end.width / 2;
+
+ await act(() => touch('touchstart', source!, source!, startX, startY));
+ expect(source!.style.opacity).not.toBe('0.5');
+ await act(
+ () =>
+ new Promise<void>((resolve) => {
+ globalThis.setTimeout(resolve, 100);
+ })
+ );
+ expect(source!.style.opacity).not.toBe('0.5');
+ await act(
+ () =>
+ new Promise<void>((resolve) => {
+ globalThis.setTimeout(resolve, 200);
+ })
+ );
+ expect(source!.style.opacity).toBe('0.5');
+ await act(() => touch('touchmove', source!, source!, endX, startY));
+ await act(() => touch('touchend', source!, source!, endX, startY));
+
+ expect(source!.style.opacity).not.toBe('0.5');
+ expect(store.getSnapshot().account.layoutPresetOrder).toEqual(['custom-1', 'edit', 'automate', 'compose']);
});
});
diff --git a/invokeai/frontend/webv2/src/workbench/shell/topbar/LayoutPresetStrip.tsx b/invokeai/frontend/webv2/src/workbench/shell/topbar/LayoutPresetStrip.tsx
index 9e92c5324f..6e627d60af 100644
--- a/invokeai/frontend/webv2/src/workbench/shell/topbar/LayoutPresetStrip.tsx
+++ b/invokeai/frontend/webv2/src/workbench/shell/topbar/LayoutPresetStrip.tsx
@@ -1,10 +1,10 @@
-import type { DragEndEvent } from '@dnd-kit/core';
+import type { AutoScrollOptions, DragEndEvent } from '@dnd-kit/core';
import type { LayoutPreset, LayoutPresetId } from '@workbench/layoutContracts';
import type { Project } from '@workbench/projectContracts';
import type { KeyboardEvent, MouseEvent } from 'react';
import { Box, HStack, Icon, Menu, Portal, Text, VisuallyHidden } from '@chakra-ui/react';
-import { closestCenter, DndContext, PointerSensor, useSensor, useSensors } from '@dnd-kit/core';
+import { closestCenter, DndContext, MouseSensor, TouchSensor, useSensor, useSensors } from '@dnd-kit/core';
import { restrictToHorizontalAxis, restrictToParentElement } from '@dnd-kit/modifiers';
import { horizontalListSortingStrategy, SortableContext, useSortable } from '@dnd-kit/sortable';
import { CSS } from '@dnd-kit/utilities';
@@ -26,7 +26,7 @@ import {
SettingsIcon,
Trash2Icon,
} from 'lucide-react';
-import { useCallback, useMemo, useState } from 'react';
+import { useCallback, useMemo, useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';
import type { LayoutPresetDialogValue } from './layoutPresetDialogModel';
@@ -41,7 +41,8 @@ import { useTopbarShortcut } from './useTopbarShortcut';
const PRESET_MENU_ATTRIBUTE = 'data-preset-menu';
const PRESET_SCROLL_CSS = { '&::-webkit-scrollbar': { display: 'none' }, scrollbarWidth: 'none' } as const;
const DND_MODIFIERS = [restrictToHorizontalAxis, restrictToParentElement];
-const POINTER_SENSOR_OPTIONS = { activationConstraint: { distance: 6 } } as const;
+const MOUSE_SENSOR_OPTIONS = { activationConstraint: { distance: 6 } } as const;
+const TOUCH_SENSOR_OPTIONS = { activationConstraint: { delay: 250, tolerance: 5 } } as const;
const createCustomPresetId = (): string =>
`custom-layout-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`;
@@ -56,6 +57,7 @@ export const LayoutPresetStrip = () => {
const { t } = useTranslation();
const { activePreset, hasDrifted } = useLayoutDrift();
const { layout } = useWorkbenchCommands();
+ const scrollContainerRef = useRef<HTMLDivElement>(null);
const [isSaveAsOpen, setIsSaveAsOpen] = useState(false);
const [menuTarget, setMenuTarget] = useState<{ anchor: DOMRect; preset: LayoutPreset } | null>(null);
@@ -64,7 +66,16 @@ export const LayoutPresetStrip = () => {
const sourceOptions = useActiveProjectSelector(selectPlacedGraphWidgetSources);
const presets = useMemo(() => getOrderedLayoutPresets(account), [account]);
const presetIds = useMemo(() => presets.map(({ id }) => id), [presets]);
- const sensors = useSensors(useSensor(PointerSensor, POINTER_SENSOR_OPTIONS));
+ const sensors = useSensors(
+ useSensor(MouseSensor, MOUSE_SENSOR_OPTIONS),
+ useSensor(TouchSensor, TOUCH_SENSOR_OPTIONS)
+ );
+ const autoScroll = useMemo<AutoScrollOptions>(
+ () => ({
+ canScroll: (element) => element === scrollContainerRef.current && element.scrollWidth > element.clientWidth,
+ }),
+ []
+ );
const saveAsDefaultRoute = useMemo(
() => ({ destination: invocation.destination, sourceId: invocation.sourceId }),
[invocation.destination, invocation.sourceId]
@@ -111,9 +122,16 @@ export const LayoutPresetStrip = () => {
return (
<>
<HStack gap="1" justify="center" maxW="min(36vw, 44rem)" minW="0">
- <Box css={PRESET_SCROLL_CSS} data-layout-preset-scroll="" maxW="full" minW="0" overflowX="auto">
+ <Box
+ ref={scrollContainerRef}
+ css={PRESET_SCROLL_CSS}
+ data-layout-preset-scroll=""
+ maxW="full"
+ minW="0"
+ overflowX="auto"
+ >
<DndContext
- autoScroll={false}
+ autoScroll={autoScroll}
collisionDetection={closestCenter}
modifiers={DND_MODIFIERS}
sensors={sensors}
@@ -269,6 +287,7 @@ const PresetTab = ({
data-layout-preset-id={preset.id}
gap="1.5"
style={dndStyle}
+ touchAction="pan-x"
value={preset.id}
onClick={handleClick}
onContextMenu={handleContextMenu}