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.

feat(generation): scrubber for reference weight and crosshair find badge

Josh Corbettc8d5e7d12fb1
diff --git a/invokeai/frontend/webv2/src/features/gallery/ui/FindInGalleryButton.tsx b/invokeai/frontend/webv2/src/features/gallery/ui/FindInGalleryButton.tsx
index f77ba82d34..221b6c41c0 100644
--- a/invokeai/frontend/webv2/src/features/gallery/ui/FindInGalleryButton.tsx
+++ b/invokeai/frontend/webv2/src/features/gallery/ui/FindInGalleryButton.tsx
@@ -2,13 +2,13 @@ import type { IconButtonProps } from '@platform/ui/Button';
import { IconButton } from '@platform/ui/Button';
import { Tooltip } from '@platform/ui/Tooltip';
-import { ImageIcon } from 'lucide-react';
+import { CrosshairIcon } from 'lucide-react';
import { useTranslation } from 'react-i18next';
/**
* "Find in gallery", for the panels that condition on media they did not pick
- * from the grid they are looking at. The mark is the Gallery widget's own rail
- * icon at control scale, so the badge names its destination.
+ * from the grid they are looking at. The mark is the same crosshair the
+ * workflow form's pinned fields use to locate their node: "take me to it".
*
* Presentation is the caller's: this owns the icon, the accessible name, and
* the tooltip, which are what must not drift between the four thumbnails that
@@ -37,7 +37,7 @@ export const FindInGalleryButton = ({ name, onFind, ...buttonProps }: FindInGall
return (
<Tooltip content={label}>
<IconButton aria-label={label} size="2xs" {...buttonProps} onClick={onFind}>
- <ImageIcon />
+ <CrosshairIcon />
</IconButton>
</Tooltip>
);
diff --git a/invokeai/frontend/webv2/src/features/generation/ui/reference-images/ReferenceImageControls.browser.test.tsx b/invokeai/frontend/webv2/src/features/generation/ui/reference-images/ReferenceImageControls.browser.test.tsx
new file mode 100644
index 0000000000..d722b6e5cf
--- /dev/null
+++ b/invokeai/frontend/webv2/src/features/generation/ui/reference-images/ReferenceImageControls.browser.test.tsx
@@ -0,0 +1,99 @@
+import type { GenerateReferenceImageConfig } from '@features/generation/core/types';
+
+import { ChakraProvider } from '@chakra-ui/react';
+import { system } from '@theme/system';
+import i18next from 'i18next';
+import { act, useCallback, useState } from 'react';
+import { createRoot, type Root } from 'react-dom/client';
+import { I18nextProvider, initReactI18next } from 'react-i18next';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+
+import { CLIP_VISION_MODELS } from './referenceImageConfig';
+import { IPAdapterControls } from './ReferenceImageControls';
+
+const i18n = i18next.createInstance();
+await i18n.use(initReactI18next).init({
+ fallbackLng: 'en',
+ lng: 'en',
+ resources: {
+ en: { translation: { widgets: { generate: { activeSteps: 'Active steps', mode: 'Mode', weight: 'Weight' } } } },
+ },
+});
+(globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
+
+type IPAdapterConfig = Extract<GenerateReferenceImageConfig, { type: 'ip_adapter' }>;
+
+const CONFIG: IPAdapterConfig = {
+ beginEndStepPct: [0, 1],
+ clipVisionModel: CLIP_VISION_MODELS[0]!,
+ image: null,
+ method: 'full',
+ model: null,
+ type: 'ip_adapter',
+ weight: 1,
+};
+
+/** Track inset the scrubber keeps from its frame edge (see ScrubberField). */
+const TRACK_INSET_PX = 10;
+
+let host: HTMLDivElement;
+let root: Root;
+const onChange = vi.fn();
+
+/** Mirrors the real card: each commit lands back on the rendered config. */
+const Harness = () => {
+ const [config, setConfig] = useState<IPAdapterConfig>(CONFIG);
+ const handleChange = useCallback((next: GenerateReferenceImageConfig) => {
+ onChange(next);
+ setConfig(next as IPAdapterConfig);
+ }, []);
+
+ return <IPAdapterControls config={config} disabled={false} onChange={handleChange} />;
+};
+
+beforeEach(() => {
+ onChange.mockClear();
+ host = document.createElement('div');
+ host.style.width = '320px';
+ document.body.append(host);
+ root = createRoot(host);
+});
+
+afterEach(async () => {
+ await act(() => root.unmount());
+ host.remove();
+});
+
+const pointer = (target: EventTarget, type: string, init: PointerEventInit) =>
+ act(() => target.dispatchEvent(new PointerEvent(type, { bubbles: true, button: 0, ...init })));
+
+describe('IPAdapterControls weight', () => {
+ it('commits every step of a drag, including the one back to where the drag started', async () => {
+ await act(() =>
+ root.render(
+ <I18nextProvider i18n={i18n}>
+ <ChakraProvider value={system}>
+ <Harness />
+ </ChakraProvider>
+ </I18nextProvider>
+ )
+ );
+
+ const frame = host.querySelector<HTMLElement>('[data-scope="scrubber"]')!;
+ const rect = frame.getBoundingClientRect();
+ const trackWidth = rect.width - TRACK_INSET_PX * 2;
+ // 10% of the 0..2 track is 0.2 of weight.
+ const x = (fraction: number) => rect.left + TRACK_INSET_PX + fraction * trackWidth;
+
+ await pointer(frame, 'pointerdown', { clientX: x(0.5) });
+ await pointer(window, 'pointermove', { clientX: x(0.6) });
+
+ expect(onChange).toHaveBeenLastCalledWith(expect.objectContaining({ weight: 1.2 }));
+
+ await pointer(window, 'pointermove', { clientX: x(0.5) });
+ await pointer(window, 'pointerup', { clientX: x(0.5) });
+
+ expect(onChange).toHaveBeenLastCalledWith(expect.objectContaining({ weight: 1 }));
+ expect(host.querySelector('[role="slider"]')?.getAttribute('aria-valuenow')).toBe('1');
+ });
+});
diff --git a/invokeai/frontend/webv2/src/features/generation/ui/reference-images/ReferenceImageControls.tsx b/invokeai/frontend/webv2/src/features/generation/ui/reference-images/ReferenceImageControls.tsx
index 988ae7343c..e184b7efe5 100644
--- a/invokeai/frontend/webv2/src/features/generation/ui/reference-images/ReferenceImageControls.tsx
+++ b/invokeai/frontend/webv2/src/features/generation/ui/reference-images/ReferenceImageControls.tsx
@@ -6,20 +6,12 @@ import type {
IPAdapterMethod,
} from '@features/generation/core/types';
import type { FeatureHintId } from '@platform/ui/hints';
-import type { KeyboardEvent, ReactNode } from 'react';
+import type { ReactNode } from 'react';
-import {
- Collapsible,
- createListCollection,
- Flex,
- HStack,
- NumberInput,
- SegmentGroup,
- Stack,
- Text,
-} from '@chakra-ui/react';
+import { Collapsible, createListCollection, HStack, SegmentGroup, Stack, Text } from '@chakra-ui/react';
import { Field, Select, Slider } from '@platform/ui';
import { FeatureHint } from '@platform/ui/hints';
+import { ScrubberField } from '@platform/ui/ScrubberField';
import { ChevronRightIcon } from 'lucide-react';
import { useCallback, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';
@@ -73,11 +65,9 @@ const renderStyleVariantItem = (option: StyleVariantOption) => (
</Stack>
);
-const WEIGHT_MARKS = [
- { label: '0', value: 0 },
- { label: '1', value: 1 },
- { label: '2', value: 2 },
-];
+/** The default weight is the one stop a drag can snap to; the track ends stand in for 0 and 2. */
+const WEIGHT_MARKS = [0, 1, 2];
+const DEFAULT_WEIGHT = 1;
const BEGIN_END_MARKS = [
{ label: '0%', value: 0 },
@@ -130,11 +120,8 @@ export const IPAdapterControls = ({
onChange: (config: GenerateReferenceImageConfig) => void;
}) => {
const { t } = useTranslation();
- const [draftWeight, setDraftWeight] = useState<number | null>(null);
const [draftBeginEndStepPct, setDraftBeginEndStepPct] = useState<[number, number] | null>(null);
const mode = getReferenceMode(config.method);
- const weight = draftWeight ?? config.weight;
- const sliderWeight = Math.max(0, weight);
const beginEndStepPct = draftBeginEndStepPct ?? config.beginEndStepPct;
const styleVariantCollection = useMemo(
() =>
@@ -152,16 +139,10 @@ export const IPAdapterControls = ({
[config.method]
);
- const commitWeight = useCallback(
- (nextWeight: number) => {
- setDraftWeight(null);
-
- if (nextWeight !== config.weight) {
- onChange({ ...config, weight: nextWeight });
- }
- },
- [config, onChange]
- );
+ // No "unchanged" guard: a drag keeps the handler it started with, so `config.weight`
+ // here is the press value and a drag back to it must still commit. The scrubber
+ // already dedupes per step.
+ const handleWeightChange = useCallback((weight: number) => onChange({ ...config, weight }), [config, onChange]);
const commitBeginEndStepPct = useCallback(
(nextBeginEndStepPct: [number, number]) => {
@@ -204,38 +185,6 @@ export const IPAdapterControls = ({
[config, onChange]
);
- const handleWeightInputChange = useCallback(({ valueAsNumber }: NumberInput.ValueChangeDetails) => {
- if (Number.isFinite(valueAsNumber)) {
- setDraftWeight(valueAsNumber);
- }
- }, []);
-
- const handleWeightInputBlur = useCallback(() => commitWeight(weight), [commitWeight, weight]);
-
- const handleWeightInputKeyDown = useCallback(
- (event: KeyboardEvent<HTMLInputElement>) => {
- if (event.key === 'Enter') {
- commitWeight(weight);
- }
- },
- [commitWeight, weight]
- );
-
- const handleWeightSliderChange = useCallback(({ value }: SliderValueChangeDetails) => {
- if (Number.isFinite(value[0])) {
- setDraftWeight(value[0] as number);
- }
- }, []);
-
- const handleWeightSliderChangeEnd = useCallback(
- ({ value }: SliderValueChangeDetails) => {
- if (Number.isFinite(value[0])) {
- commitWeight(value[0] as number);
- }
- },
- [commitWeight]
- );
-
const handleStepsChange = useCallback(({ value }: SliderValueChangeDetails) => {
const begin = value[0];
const end = value[1];
@@ -257,8 +206,6 @@ export const IPAdapterControls = ({
[commitBeginEndStepPct]
);
- const weightSliderValue = useMemo(() => [sliderWeight], [sliderWeight]);
- const weightAriaLabel = useMemo(() => [t('widgets.generate.weight')], [t]);
const stepsAriaLabel = useMemo(() => [t('widgets.generate.activeSteps'), t('widgets.generate.activeSteps')], [t]);
return (
@@ -276,50 +223,20 @@ export const IPAdapterControls = ({
</SegmentGroup.Root>
</Stack>
- <Field
- align="center"
+ <ScrubberField
+ defaultValue={DEFAULT_WEIGHT}
disabled={disabled}
- gap="3"
+ formatValue={formatWeight}
hint="referenceImageWeight"
+ inputMin={-1}
label={t('widgets.generate.weight')}
- orientation="horizontal"
- >
- <Flex align="center" gap="3">
- <Slider
- aria-label={weightAriaLabel}
- formatValue={formatWeight}
- marks={WEIGHT_MARKS}
- max={2}
- min={0}
- disabled={disabled}
- size="sm"
- step={0.05}
- value={weightSliderValue}
- onValueChange={handleWeightSliderChange}
- onValueChangeEnd={handleWeightSliderChangeEnd}
- w="full"
- />
-
- <NumberInput.Root
- disabled={disabled}
- max={2}
- min={-1}
- size="xs"
- step={0.05}
- value={String(weight)}
- w="20"
- onValueChange={handleWeightInputChange}
- >
- <NumberInput.Control />
- <NumberInput.Input
- aria-label={t('widgets.generate.weight')}
- fontSize="xs"
- onBlur={handleWeightInputBlur}
- onKeyDown={handleWeightInputKeyDown}
- />
- </NumberInput.Root>
- </Flex>
- </Field>
+ marks={WEIGHT_MARKS}
+ max={2}
+ min={0}
+ step={0.05}
+ value={config.weight}
+ onChange={handleWeightChange}
+ />
<Collapsible.Root>
<Collapsible.Trigger