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.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
feat(canvas): scroll the staging bar horizontally with the mouse wheel
Josh Corbettbedf15f8934e
diff --git a/invokeai/frontend/webv2/src/platform/ui/wheelScrollsHorizontally.browser.test.tsx b/invokeai/frontend/webv2/src/platform/ui/wheelScrollsHorizontally.browser.test.tsxnew file mode 100644index 0000000000..2fefcd6890--- /dev/null+++ b/invokeai/frontend/webv2/src/platform/ui/wheelScrollsHorizontally.browser.test.tsx@@ -0,0 +1,92 @@+import { ChakraProvider, ScrollArea } from '@chakra-ui/react';+import { system } from '@theme/system';+import { act } from 'react';+import { createRoot, type Root } from 'react-dom/client';+import { afterEach, describe, expect, it } from 'vitest';++import { wheelScrollsHorizontally } from './wheelScrollsHorizontally';++(globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;++let host: HTMLDivElement | null = null;+let root: Root | null = null;++afterEach(async () => {+ await act(() => root?.unmount());+ host?.remove();+ host = null;+ root = null;+});++const render = async (contentWidth: number) => {+ host = document.createElement('div');+ document.body.append(host);+ root = createRoot(host);+ await act(() =>+ root!.render(+ <div ref={wheelScrollsHorizontally} data-testid="strip" style={{ overflowX: 'auto', width: 200 }}>+ <div style={{ height: 20, width: contentWidth }} />+ </div>+ )+ );+ return host.querySelector<HTMLDivElement>('[data-testid="strip"]')!;+};++const wheel = (element: HTMLElement, init: WheelEventInit): WheelEvent => {+ const event = new WheelEvent('wheel', { bubbles: true, cancelable: true, ...init });+ element.dispatchEvent(event);+ return event;+};++describe('wheelScrollsHorizontally', () => {+ it('turns a vertical wheel into sideways travel and claims the event', async () => {+ const strip = await render(1000);+ expect(wheel(strip, { deltaY: 40 }).defaultPrevented).toBe(true);+ expect(strip.scrollLeft).toBe(40);+ wheel(strip, { deltaMode: WheelEvent.DOM_DELTA_LINE, deltaY: -1 });+ expect(strip.scrollLeft).toBe(24);+ });++ it('leaves trackpad gestures with their own horizontal delta alone', async () => {+ const strip = await render(1000);+ expect(wheel(strip, { deltaX: 30, deltaY: 40 }).defaultPrevented).toBe(false);+ expect(strip.scrollLeft).toBe(0);+ });++ it('passes the wheel through when the content fits', async () => {+ const strip = await render(100);+ expect(wheel(strip, { deltaY: 40 }).defaultPrevented).toBe(false);+ });++ it('drives the scroll container of a Chakra scroll area through its viewport ref', async () => {+ host = document.createElement('div');+ document.body.append(host);+ root = createRoot(host);+ await act(() =>+ root!.render(+ <ChakraProvider value={system}>+ <ScrollArea.Root size="xs" w="200px">+ <ScrollArea.Viewport ref={wheelScrollsHorizontally} data-testid="viewport">+ <ScrollArea.Content>+ <div style={{ height: 20, width: 1000 }} />+ </ScrollArea.Content>+ </ScrollArea.Viewport>+ <ScrollArea.Scrollbar orientation="horizontal">+ <ScrollArea.Thumb />+ </ScrollArea.Scrollbar>+ </ScrollArea.Root>+ </ChakraProvider>+ )+ );+ const viewport = host.querySelector<HTMLElement>('[data-testid="viewport"]')!;+ expect(viewport.scrollWidth).toBeGreaterThan(viewport.clientWidth);+ wheel(viewport, { deltaY: 40 });+ expect(viewport.scrollLeft).toBe(40);+ });++ it('stops listening once the strip is gone', async () => {+ const strip = await render(1000);+ await act(() => root!.unmount());+ expect(wheel(strip, { deltaY: 40 }).defaultPrevented).toBe(false);+ });+});diff --git a/invokeai/frontend/webv2/src/platform/ui/wheelScrollsHorizontally.ts b/invokeai/frontend/webv2/src/platform/ui/wheelScrollsHorizontally.tsnew file mode 100644index 0000000000..2a3f685423--- /dev/null+++ b/invokeai/frontend/webv2/src/platform/ui/wheelScrollsHorizontally.ts@@ -0,0 +1,32 @@+import type { RefCallback } from 'react';++// One wheel notch in `deltaMode` lines, matching what browsers scroll for it.+const LINE_HEIGHT_PX = 16;++const handleWheel = (event: WheelEvent): void => {+ const element = event.currentTarget;+ if (!(element instanceof HTMLElement) || event.deltaX !== 0 || event.deltaY === 0) {+ return;+ }+ if (element.scrollWidth <= element.clientWidth) {+ return;+ }+ event.preventDefault();+ element.scrollLeft += event.deltaMode === WheelEvent.DOM_DELTA_LINE ? event.deltaY * LINE_HEIGHT_PX : event.deltaY;+};++/**+ * Ref for a strip that only scrolls sideways: a vertical wheel, the one most+ * mice have, moves it along its axis instead of doing nothing. Trackpad+ * gestures that already carry a horizontal delta pass through untouched, as+ * does a strip whose content fits.+ */+export const wheelScrollsHorizontally: RefCallback<HTMLElement> = (element) => {+ if (!element) {+ return;+ }+ element.addEventListener('wheel', handleWheel, { passive: false });+ return () => {+ element.removeEventListener('wheel', handleWheel);+ };+};diff --git a/invokeai/frontend/webv2/src/workbench/widgets/canvas/StagingBar.tsx b/invokeai/frontend/webv2/src/workbench/widgets/canvas/StagingBar.tsxindex d01f610b91..ff68978ddb 100644--- a/invokeai/frontend/webv2/src/workbench/widgets/canvas/StagingBar.tsx+++ b/invokeai/frontend/webv2/src/workbench/widgets/canvas/StagingBar.tsx@@ -20,6 +20,7 @@ import { useQueueItemProgress, useQueueItemProgressImage } from '@features/queueimport { Button, Group, IconButton, MenuContent, toaster, Tooltip } from '@platform/ui';import { StreamingImageFrame } from '@platform/ui/streaming-image/StreamingImageFrame';import { progressImageToStreamingSource } from '@platform/ui/streaming-image/streamingImageSource';+import { wheelScrollsHorizontally } from '@platform/ui/wheelScrollsHorizontally';import { getCancelableCanvasStagingQueueItemId } from '@workbench/canvasStagingView';import { CanvasOptionsBar } from '@workbench/widgets/canvas/tool-options/CanvasOptionsBar';import {@@ -145,7 +146,7 @@ export const StagingBar = ({variant="hover"w="full">- <ScrollArea.Viewport h="full" scrollPaddingInline="2" w="full">+ <ScrollArea.Viewport ref={wheelScrollsHorizontally} h="full" scrollPaddingInline="2" w="full">{/** Leave the content slot's width alone: its inline `min-width:* fit-content` grows it to hold every thumbnail, so `justify` only