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(webv2): add draggable conditioning rebalance bar editor
joshistoast4604a8527326
diff --git a/invokeai/frontend/webv2/src/features/generation/ui/shared/ConditioningRebalanceBars.browser.test.tsx b/invokeai/frontend/webv2/src/features/generation/ui/shared/ConditioningRebalanceBars.browser.test.tsxnew file mode 100644index 0000000000..926377eff1--- /dev/null+++ b/invokeai/frontend/webv2/src/features/generation/ui/shared/ConditioningRebalanceBars.browser.test.tsx@@ -0,0 +1,221 @@+import { ChakraProvider } from '@chakra-ui/react';+import { system } from '@theme/system';+import { act } from 'react';+import { createRoot, type Root } from 'react-dom/client';+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';++import { ConditioningRebalanceBars } from './ConditioningRebalanceBars';++let host: HTMLDivElement | null = null;+let root: Root | null = null;+(globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;++const onActiveIndexChange = vi.fn();+const onCommit = vi.fn();+const onPreview = vi.fn();++const TAP_COUNT = 12;+/** All weights at 1.0, so `getRebalanceBarScale` reports the nominal ceiling. */+const FLAT_WEIGHTS = Array.from({ length: TAP_COUNT }, () => 1);+const SCALE = 8;++const tapLabel = (tap: number, layer: number) => `Tap ${tap}, encoder layer ${layer}`;++const renderBars = async (weights: readonly number[] = FLAT_WEIGHTS) => {+ await act(() =>+ root?.render(+ <ChakraProvider value={system}>+ <ConditioningRebalanceBars+ activeIndex={null}+ tapLabel={tapLabel}+ weights={weights}+ onActiveIndexChange={onActiveIndexChange}+ onCommit={onCommit}+ onPreview={onPreview}+ />+ </ChakraProvider>+ )+ );++ const bars = [...document.querySelectorAll('[role="slider"]')];+ const track = bars[0]?.parentElement;++ if (bars.length !== TAP_COUNT || !track) {+ throw new Error('rebalance bars did not render');+ }++ return { bars, rect: track.getBoundingClientRect(), track };+};++const interact = async (run: () => void) => {+ await act(async () => {+ run();+ await Promise.resolve();+ });+};++/** Pointer coordinates that land on `index`'s column at `weight` on the track. */+const pointAt = (rect: DOMRect, index: number, weight: number) => ({+ clientX: rect.left + (index + 0.5) * (rect.width / TAP_COUNT),+ clientY: rect.top + (1 - weight / SCALE) * rect.height,+});++const lastPreview = (): number[] => (onPreview.mock.calls.at(-1)?.[0] as number[] | null) ?? [];++const lastCommit = (): number[] => (onCommit.mock.calls.at(-1)?.[0] as number[] | undefined) ?? [];++beforeEach(() => {+ host = document.createElement('div');+ document.body.append(host);+ root = createRoot(host);+ onActiveIndexChange.mockClear();+ onCommit.mockClear();+ onPreview.mockClear();+});++afterEach(async () => {+ await act(() => root?.unmount());+ host?.remove();+ host = null;+ root = null;+});++describe('ConditioningRebalanceBars', () => {+ it('names each bar by its tap and the encoder layer it weights', async () => {+ const { bars } = await renderBars();++ expect(bars).toHaveLength(TAP_COUNT);+ expect(bars[0]?.getAttribute('aria-label')).toBe('Tap 1, encoder layer 2');+ expect(bars[7]?.getAttribute('aria-label')).toBe('Tap 8, encoder layer 23');+ expect(bars[11]?.getAttribute('aria-label')).toBe('Tap 12, encoder layer 35');+ });++ it('exposes the weight range to assistive tech', async () => {+ const { bars } = await renderBars();+ const bar = bars[0];++ expect(bar?.getAttribute('aria-orientation')).toBe('vertical');+ expect(bar?.getAttribute('aria-valuemin')).toBe('0');+ expect(bar?.getAttribute('aria-valuemax')).toBe(String(SCALE));+ expect(bar?.getAttribute('aria-valuenow')).toBe('1');+ expect(bar?.getAttribute('tabindex')).toBe('0');+ });++ it('rescales rather than clipping when a weight overshoots the nominal ceiling', async () => {+ const { bars } = await renderBars([...FLAT_WEIGHTS.slice(0, 11), 12.5]);++ expect(bars[0]?.getAttribute('aria-valuemax')).toBe('12.5');+ });++ it('drags one bar to the pointed-at weight and commits once on release', async () => {+ const { rect, track } = await renderBars();+ const target = pointAt(rect, 3, 6);++ await interact(() => {+ track.dispatchEvent(new PointerEvent('pointerdown', { bubbles: true, ...target }));+ });++ expect(lastPreview()[3]).toBeCloseTo(6, 1);+ // Neighbours are untouched by a stationary press.+ expect(lastPreview()[2]).toBe(1);+ expect(lastPreview()[4]).toBe(1);+ expect(onCommit).not.toHaveBeenCalled();++ await interact(() => window.dispatchEvent(new PointerEvent('pointerup', target)));++ expect(onPreview).toHaveBeenLastCalledWith(null);+ expect(onCommit).toHaveBeenCalledOnce();+ expect(lastCommit()[3]).toBeCloseTo(6, 1);+ });++ it('paints across every column a sweep passes, including ones it skipped', async () => {+ const { rect, track } = await renderBars();++ await interact(() => {+ track.dispatchEvent(new PointerEvent('pointerdown', { bubbles: true, ...pointAt(rect, 0, 0) }));+ });+ // One large move from the first column to the last: the columns in between get+ // no pointer sample of their own and must still be filled in.+ await interact(() => window.dispatchEvent(new PointerEvent('pointermove', pointAt(rect, 11, 8))));++ const painted = lastPreview();++ expect(painted[0]).toBeCloseTo(0, 1);+ expect(painted[11]).toBeCloseTo(8, 1);++ for (let index = 1; index < TAP_COUNT; index += 1) {+ expect(painted[index] ?? 0).toBeGreaterThan(painted[index - 1] ?? 0);+ }+ });++ it('clamps a drag that leaves the track instead of reporting an impossible weight', async () => {+ const { rect, track } = await renderBars();+ // Stay in column 5's x band throughout, so this measures vertical clamping only.+ const { clientX } = pointAt(rect, 5, 4);++ await interact(() => {+ track.dispatchEvent(new PointerEvent('pointerdown', { bubbles: true, ...pointAt(rect, 5, 4) }));+ });++ await interact(() => window.dispatchEvent(new PointerEvent('pointermove', { clientX, clientY: -9000 })));+ expect(lastPreview()[5]).toBe(SCALE);++ await interact(() => window.dispatchEvent(new PointerEvent('pointermove', { clientX, clientY: 9000 })));+ expect(lastPreview()[5]).toBe(0);+ });++ it('adjusts a bar by keyboard on the step grid', async () => {+ const { bars } = await renderBars();+ const bar = bars[4];++ await interact(() => bar?.dispatchEvent(new KeyboardEvent('keydown', { bubbles: true, key: 'ArrowUp' })));+ expect(lastCommit()[4]).toBeCloseTo(1.1, 5);++ await interact(() =>+ bar?.dispatchEvent(new KeyboardEvent('keydown', { bubbles: true, key: 'ArrowDown', shiftKey: true }))+ );+ expect(lastCommit()[4]).toBe(0);++ await interact(() => bar?.dispatchEvent(new KeyboardEvent('keydown', { bubbles: true, key: 'End' })));+ expect(lastCommit()[4]).toBe(SCALE);++ await interact(() => bar?.dispatchEvent(new KeyboardEvent('keydown', { bubbles: true, key: 'Home' })));+ expect(lastCommit()[4]).toBe(0);+ });++ it('leaves the other taps alone when one is adjusted by keyboard', async () => {+ const { bars } = await renderBars();++ await interact(() => bars[4]?.dispatchEvent(new KeyboardEvent('keydown', { bubbles: true, key: 'ArrowUp' })));++ const committed = lastCommit();++ expect(committed.filter((weight) => weight !== 1)).toHaveLength(1);+ });++ it('moves focus between bars with the horizontal arrows', async () => {+ const { bars } = await renderBars();++ await act(() => {+ (bars[4] as HTMLElement).focus();+ });+ await interact(() => bars[4]?.dispatchEvent(new KeyboardEvent('keydown', { bubbles: true, key: 'ArrowRight' })));++ expect(document.activeElement).toBe(bars[5]);+ expect(onCommit).not.toHaveBeenCalled();++ await interact(() => bars[5]?.dispatchEvent(new KeyboardEvent('keydown', { bubbles: true, key: 'ArrowLeft' })));++ expect(document.activeElement).toBe(bars[4]);+ });++ it('reports the tap under the pointer so the parent can read it out', async () => {+ const { rect, track } = await renderBars();++ await interact(() => {+ track.dispatchEvent(new PointerEvent('pointerdown', { bubbles: true, ...pointAt(rect, 7, 3) }));+ });++ expect(onActiveIndexChange).toHaveBeenLastCalledWith(7);+ });+});diff --git a/invokeai/frontend/webv2/src/features/generation/ui/shared/ConditioningRebalanceBars.tsx b/invokeai/frontend/webv2/src/features/generation/ui/shared/ConditioningRebalanceBars.tsxnew file mode 100644index 0000000000..63cfb4f965--- /dev/null+++ b/invokeai/frontend/webv2/src/features/generation/ui/shared/ConditioningRebalanceBars.tsx@@ -0,0 +1,291 @@+/* oxlint-disable react-perf/jsx-no-new-object-as-prop, react-perf/jsx-no-new-function-as-prop */+import { Box, Flex } from '@chakra-ui/react';+import {+ adjustRebalanceWeight,+ barFillFraction,+ getRebalanceBarScale,+ KREA2_TAP_LAYERS,+ REBALANCE_NEUTRAL_WEIGHT,+ REBALANCE_WEIGHT_MIN,+ REBALANCE_WEIGHT_STEP,+ snapRebalanceWeight,+ weightFromTrackFraction,+} from '@features/generation/core/conditioningRebalance';+import { useCallback, useRef, type KeyboardEvent, type PointerEvent } from 'react';++export const REBALANCE_TRACK_HEIGHT_PX = 80;++/**+ * One continuous recessed track rather than twelve gapped wells.+ *+ * Twelve flexed columns divide a fluid width into fractional positions, so a 1px gap+ * between them lands mid-device-pixel and antialiases to a different apparent width in+ * every column — the separators visibly drift. Spacing the *fills* inside flush columns+ * puts every edge against the same background, so the bars read evenly at any width.+ */+const TRACK_PROPS = {+ bg: 'bg.inset',+ borderRadius: 'md',+ h: `${REBALANCE_TRACK_HEIGHT_PX}px`,+ overflow: 'hidden',+ position: 'relative',+ px: '1px',+ touchAction: 'none',+ w: 'full',+} as const;++/** Half the visual space between neighbouring bars; also insets the outermost pair. */+const BAR_INSET = '1px';++const COLUMN_HOVER_PROPS = { boxShadow: 'inset 0 0 0 1px {colors.border.emphasized}' } as const;+const COLUMN_FOCUS_PROPS = {+ boxShadow: 'inset 0 0 0 1px {colors.accent.solid}',+ outline: 'none',+} as const;++/** The "no change" rule sits behind the bars and must never eat a pointer event. */+const NEUTRAL_RULE_PROPS = {+ bg: 'fg.grid',+ h: '1px',+ left: 0,+ pointerEvents: 'none',+ position: 'absolute',+ right: 0,+ zIndex: 1,+} as const;++const getColumnIndex = (clientX: number, rect: DOMRect, count: number): number => {+ if (rect.width <= 0) {+ return 0;+ }++ return Math.min(count - 1, Math.max(0, Math.floor(((clientX - rect.left) / rect.width) * count)));+};++const getWeightAt = (clientY: number, rect: DOMRect, scale: number): number =>+ rect.height <= 0 ? scale : weightFromTrackFraction((clientY - rect.top) / rect.height, scale);++/**+ * Fills the columns a fast sweep skipped over, so the committed vector matches the+ * stroke the pointer actually drew rather than only its sample points.+ */+const strokeBetween = (+ weights: number[],+ fromIndex: number,+ fromWeight: number,+ toIndex: number,+ toWeight: number,+ scale: number+): void => {+ if (fromIndex === toIndex) {+ weights[toIndex] = toWeight;++ return;+ }++ const span = Math.abs(toIndex - fromIndex);+ const direction = toIndex > fromIndex ? 1 : -1;++ for (let offset = 1; offset <= span; offset += 1) {+ weights[fromIndex + offset * direction] = snapRebalanceWeight(+ fromWeight + (toWeight - fromWeight) * (offset / span),+ scale+ );+ }+};++interface ConditioningRebalanceBarsProps {+ /** The vector currently rendered, including any in-flight drag preview. */+ weights: readonly number[];+ /** Index whose readout the parent is showing; drives the emphasized column. */+ activeIndex: number | null;+ disabled?: boolean;+ /** Builds the per-bar accessible name from its 1-based tap and its encoder layer. */+ tapLabel: (tap: number, layer: number) => string;+ onActiveIndexChange: (index: number | null) => void;+ onPreview: (weights: number[] | null) => void;+ onCommit: (weights: number[]) => void;+}++/**+ * The twelve per-tap gains as draggable vertical bars.+ *+ * Pointer input is absolute — a bar jumps to wherever you point, and a horizontal sweep+ * paints across every column it passes, which is what makes drawing a curve feel direct.+ * The parent holds `onPreview`'s live vector so a drag renders without writing settings on+ * every move; `onCommit` fires once, on release.+ */+export const ConditioningRebalanceBars = ({+ activeIndex,+ disabled,+ onActiveIndexChange,+ onCommit,+ onPreview,+ tapLabel,+ weights,+}: ConditioningRebalanceBarsProps) => {+ const trackRef = useRef<HTMLDivElement | null>(null);+ const barRefs = useRef<(HTMLDivElement | null)[]>([]);+ const isDraggingRef = useRef(false);+ const scale = getRebalanceBarScale(weights);++ const handlePointerDown = useCallback(+ (event: PointerEvent<HTMLDivElement>) => {+ const rect = trackRef.current?.getBoundingClientRect();++ if (disabled || rect === undefined || (event.pointerType === 'mouse' && event.button !== 0)) {+ return;+ }++ event.preventDefault();++ const working = [...weights];+ const pointerSession = new AbortController();+ let lastIndex = getColumnIndex(event.clientX, rect, working.length);+ let lastWeight = getWeightAt(event.clientY, rect, scale);++ working[lastIndex] = lastWeight;+ isDraggingRef.current = true;+ onActiveIndexChange(lastIndex);+ onPreview([...working]);+ // preventDefault above suppressed the implicit focus; without this a click+ // would leave the bar unfocusable by the arrow keys that just edited it.+ barRefs.current[lastIndex]?.focus();++ const handlePointerMove = (moveEvent: globalThis.PointerEvent) => {+ const index = getColumnIndex(moveEvent.clientX, rect, working.length);+ const weight = getWeightAt(moveEvent.clientY, rect, scale);++ strokeBetween(working, lastIndex, lastWeight, index, weight, scale);+ lastIndex = index;+ lastWeight = weight;+ onActiveIndexChange(index);+ onPreview([...working]);+ };++ const handlePointerUp = () => {+ pointerSession.abort();+ isDraggingRef.current = false;+ onPreview(null);+ onCommit(working);+ };++ window.addEventListener('pointermove', handlePointerMove, { signal: pointerSession.signal });+ window.addEventListener('pointerup', handlePointerUp, { signal: pointerSession.signal });+ window.addEventListener('pointercancel', handlePointerUp, { signal: pointerSession.signal });+ },+ [disabled, scale, weights, onActiveIndexChange, onCommit, onPreview]+ );++ const handleKeyDown = useCallback(+ (event: KeyboardEvent<HTMLDivElement>, index: number) => {+ if (disabled) {+ return;+ }++ if (event.key === 'ArrowLeft' || event.key === 'ArrowRight') {+ const nextIndex = Math.min(weights.length - 1, Math.max(0, index + (event.key === 'ArrowRight' ? 1 : -1)));++ event.preventDefault();+ onActiveIndexChange(nextIndex);+ barRefs.current[nextIndex]?.focus();++ return;+ }++ const current = weights[index] ?? REBALANCE_NEUTRAL_WEIGHT;+ const step = event.shiftKey ? REBALANCE_WEIGHT_STEP * 10 : REBALANCE_WEIGHT_STEP;+ const nextWeights: Partial<Record<string, number>> = {+ ArrowDown: adjustRebalanceWeight(current, -step, scale),+ ArrowUp: adjustRebalanceWeight(current, step, scale),+ End: scale,+ Home: REBALANCE_WEIGHT_MIN,+ };+ const next = nextWeights[event.key];++ if (next === undefined || next === current) {+ return;+ }++ event.preventDefault();++ const committed = [...weights];++ committed[index] = next;+ onCommit(committed);+ },+ [disabled, scale, weights, onActiveIndexChange, onCommit]+ );++ const handleTrackPointerLeave = useCallback(() => {+ // A drag that wanders outside the track still owns the readout until release.+ if (!isDraggingRef.current) {+ onActiveIndexChange(null);+ }+ }, [onActiveIndexChange]);++ return (+ <Flex+ align="stretch"+ ref={trackRef}+ {...TRACK_PROPS}+ onPointerDown={handlePointerDown}+ onPointerLeave={handleTrackPointerLeave}+ >+ <Box {...NEUTRAL_RULE_PROPS} bottom={`${barFillFraction(REBALANCE_NEUTRAL_WEIGHT, scale) * 100}%`} />+ {weights.map((weight, index) => {+ const tap = index + 1;+ const layer = KREA2_TAP_LAYERS[index] ?? 0;++ return (+ <Flex+ align="stretch"+ aria-disabled={disabled || undefined}+ aria-label={tapLabel(tap, layer)}+ aria-orientation="vertical"+ aria-valuemax={scale}+ aria-valuemin={REBALANCE_WEIGHT_MIN}+ aria-valuenow={weight}+ aria-valuetext={weight.toFixed(2)}+ boxShadow={index === activeIndex ? COLUMN_HOVER_PROPS.boxShadow : undefined}+ cursor={disabled ? 'default' : 'ns-resize'}+ data-active={index === activeIndex ? '' : undefined}+ data-index={index}+ flex="1"+ justify="stretch"+ key={tap}+ minW="0"+ position="relative"+ px={BAR_INSET}+ ref={(element: HTMLDivElement | null) => {+ barRefs.current[index] = element;+ }}+ role="slider"+ tabIndex={disabled ? -1 : 0}+ _focusVisible={COLUMN_FOCUS_PROPS}+ _hover={disabled ? undefined : COLUMN_HOVER_PROPS}+ onBlur={() => onActiveIndexChange(null)}+ onFocus={() => onActiveIndexChange(index)}+ onKeyDown={(event: KeyboardEvent<HTMLDivElement>) => handleKeyDown(event, index)}+ onPointerEnter={() => onActiveIndexChange(index)}+ >+ <Box+ alignSelf="flex-end"+ // Bars at or below neutral are holding the prompt rather than pushing it;+ // the quieter fill is what makes that readable without a legend. `muted` is+ // too close to the well to see at these heights, so the step is to+ // `emphasized`.+ bg={weight > REBALANCE_NEUTRAL_WEIGHT ? 'accent.solid' : 'accent.emphasized'}+ borderRadius="1px"+ h={`${barFillFraction(weight, scale) * 100}%`}+ // A zero tap keeps a stub so every slot stays visible and hittable now that+ // the track no longer draws a well behind each one.+ minH="2px"+ w="full"+ />+ </Flex>+ );+ })}+ </Flex>+ );+};