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
fix(webv2): keep ScrubberField edits from stealing focus, pointers, and hotkeys
Josh Corbettc2d9991ea643
diff --git a/invokeai/frontend/webv2/src/platform/ui/ScrubberField.browser.test.tsx b/invokeai/frontend/webv2/src/platform/ui/ScrubberField.browser.test.tsxindex 9328c86b79..21240db003 100644--- a/invokeai/frontend/webv2/src/platform/ui/ScrubberField.browser.test.tsx+++ b/invokeai/frontend/webv2/src/platform/ui/ScrubberField.browser.test.tsx@@ -249,6 +249,89 @@ describe('ScrubberField', () => {expect(editor()).toBeNull();});+ it('leaves focus where a blur sent it and only returns to the slider on Enter or Escape', async () => {+ const { onChange, slider } = await mount({ inputMax: 500 });+ const next = document.createElement('button');++ host?.append(next);+ await act(() => {+ valueButton()?.click();+ });+ await act(() => userEvent.keyboard('40'));+ await act(() => {+ next.focus();+ });++ expect(onChange).toHaveBeenLastCalledWith(40);+ expect(editor()).toBeNull();+ expect(document.activeElement).toBe(next);++ await act(() => {+ slider.focus();+ });+ await act(() => userEvent.keyboard('{Enter}{Escape}'));++ expect(document.activeElement).toBe(slider);++ // A blur with no destination (window deactivated, click on nothing focusable) keeps the tab stop.+ await act(() => userEvent.keyboard('{Enter}'));+ await act(() => {+ editor()?.blur();+ });++ expect(editor()).toBeNull();+ expect(document.activeElement).toBe(slider);+ });++ it('ignores pointers other than the one that started the drag', async () => {+ const { frame, onChange } = await mount();++ await pointer(frame, 'pointerdown', { clientX: trackX(frame, 0.5), pointerId: 1 });++ expect(onChange).toHaveBeenLastCalledWith(50);++ await pointer(window, 'pointermove', { clientX: trackX(frame, 0.8), pointerId: 2 });+ await pointer(window, 'pointerup', { clientX: trackX(frame, 0.8), pointerId: 2 });++ expect(onChange).toHaveBeenLastCalledWith(50);+ expect(frame.hasAttribute('data-dragging')).toBe(true);++ await pointer(window, 'pointermove', { clientX: trackX(frame, 0.6), pointerId: 1 });+ await pointer(window, 'pointerup', { clientX: trackX(frame, 0.6), pointerId: 1 });++ expect(onChange).toHaveBeenLastCalledWith(60);+ expect(frame.hasAttribute('data-dragging')).toBe(false);+ });++ it('consumes the keys it handles before they reach window-level shortcuts', async () => {+ // No default: Backspace/Delete restore nothing but are still the slider's keys.+ const { onChange, slider } = await mount();+ const seen: string[] = [];+ const listener = (event: KeyboardEvent) => {+ seen.push(event.key);+ };++ window.addEventListener('keydown', listener);++ try {+ await act(async () => {+ slider.focus();+ await userEvent.keyboard('{ArrowRight}{Home}{Delete}2');+ });++ expect(editor()?.value).toBe('2');+ expect(seen).toEqual([]);+ expect(onChange).toHaveBeenLastCalledWith(0);++ await act(() => userEvent.keyboard('{Escape}'));+ await act(() => userEvent.keyboard('q'));++ expect(seen).toEqual(['Escape', 'q']);+ } finally {+ window.removeEventListener('keydown', listener);+ }+ });+it('restores the default on double-click or Backspace, and only when one is given', async () => {const { frame, onChange, slider } = await mount({ defaultValue: 20 });diff --git a/invokeai/frontend/webv2/src/platform/ui/ScrubberField.tsx b/invokeai/frontend/webv2/src/platform/ui/ScrubberField.tsxindex 7b04acbd8c..bd61663afe 100644--- a/invokeai/frontend/webv2/src/platform/ui/ScrubberField.tsx+++ b/invokeai/frontend/webv2/src/platform/ui/ScrubberField.tsx@@ -2,6 +2,7 @@ import type { StackProps } from '@chakra-ui/react';import type { FeatureHintId } from '@platform/ui/hints';import type {ChangeEvent,+ FocusEvent,KeyboardEvent,PointerEvent as ReactPointerEvent,MouseEvent as ReactMouseEvent,@@ -313,8 +314,10 @@ export const ScrubberField = ({setEdit({ draft, selectAll });}, []);+ // Enter and Escape hand focus back to the slider; a blur means focus already+ // went where the user sent it, and pulling it back would break Tab and clicks.const finishEditing = useCallback(- (commit: boolean) => {+ (commit: boolean, restoreFocus: boolean) => {const session = editSessionRef.current;if (!session || session.finished) {@@ -323,7 +326,10 @@ export const ScrubberField = ({session.finished = true;setEdit(null);- sliderRef.current?.focus({ preventScroll: true });++ if (restoreFocus) {+ sliderRef.current?.focus({ preventScroll: true });+ }if (commit && edit) {const parsed = Number(edit.draft.trim());@@ -372,7 +378,8 @@ export const ScrubberField = ({// No capture available — fall through to the window listeners.}- const resolve = (pointer: { altKey: boolean; clientX: number; shiftKey: boolean }): number => {+ type PointerSample = { altKey: boolean; clientX: number; pointerId: number; shiftKey: boolean };+ const resolve = (pointer: PointerSample): number => {let raw: number;if (pointer.shiftKey || anchor) {@@ -389,7 +396,13 @@ export const ScrubberField = ({return pointer.altKey && markValues?.length ? nearestMark(raw, markValues) : snapToStep(raw);};- const apply = (pointer: { altKey: boolean; clientX: number; shiftKey: boolean }) => {+ // The gesture belongs to the pointer that started it; a second finger or+ // pen contact neither moves the value nor ends the drag.+ const apply = (pointer: PointerSample) => {+ if (pointer.pointerId !== event.pointerId) {+ return;+ }+if (isPendingTouch) {if (Math.abs(pointer.clientX - event.clientX) < TOUCH_INTENT_PX) {return;@@ -405,7 +418,11 @@ export const ScrubberField = ({onChange(next);}};- const end = () => {+ const end = (pointer: PointerEvent) => {+ if (pointer.pointerId !== event.pointerId) {+ return;+ }+session.abort();pointerSessionRef.current = null;setIsDragging(false);@@ -432,6 +449,9 @@ export const ScrubberField = ({[defaultValue, disabled, emit]);+ // Every key the slider acts on is consumed outright: the window-level hotkey+ // runtime only exempts editable elements, so a digit that opens the editor+ // must not also fire the shortcut bound to that digit.const handleSliderKeyDown = useCallback((event: KeyboardEvent<HTMLDivElement>) => {if (disabled || event.ctrlKey || event.metaKey) {@@ -466,12 +486,16 @@ export const ScrubberField = ({case 'Enter':case 'F2':event.preventDefault();+ event.stopPropagation();startEditing(String(value), true);return;case 'Backspace':case 'Delete':+ // Owned even without a default: unconsumed, they reach delete hotkeys.+ event.preventDefault();+ event.stopPropagation();+if (defaultValue !== undefined) {- event.preventDefault();emit(defaultValue);}return;@@ -479,12 +503,14 @@ export const ScrubberField = ({// A digit (or sign/point) starts typing straight away, like a spreadsheet cell.if (!event.altKey && /^[\d.-]$/.test(event.key)) {event.preventDefault();+ event.stopPropagation();startEditing(event.key, false);}return;}event.preventDefault();+ event.stopPropagation();emit(clamp(roundTo(next, decimals), typedMin, typedMax));},[decimals, defaultValue, disabled, emit, max, min, startEditing, step, typedMax, typedMin, value]@@ -517,15 +543,20 @@ export const ScrubberField = ({},[observeText, selectAll]);- const handleInputBlur = useCallback(() => finishEditing(true), [finishEditing]);+ // No `relatedTarget` means nothing else took focus (the window deactivated,+ // or a click landed on nothing focusable), so the slider keeps the tab stop.+ const handleInputBlur = useCallback(+ (event: FocusEvent<HTMLInputElement>) => finishEditing(true, event.relatedTarget === null),+ [finishEditing]+ );const handleInputKeyDown = useCallback((event: KeyboardEvent<HTMLInputElement>) => {if (event.key === 'Enter') {event.preventDefault();- finishEditing(true);+ finishEditing(true, true);} else if (event.key === 'Escape') {event.preventDefault();- finishEditing(false);+ finishEditing(false, true);}},[finishEditing]