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.

test(canvas): verify curves drag completion

joshistoastb660ea725524
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/layers/AdjustmentsPopover.tsx b/invokeai/frontend/webv2/src/workbench/widgets/layers/AdjustmentsPopover.tsx
index 2a8e0326e3..7fb038bdcc 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/layers/AdjustmentsPopover.tsx
+++ b/invokeai/frontend/webv2/src/workbench/widgets/layers/AdjustmentsPopover.tsx
@@ -15,8 +15,8 @@ import {
CURVE_SIZE,
curvePointFromSvg,
curvePointToSvg,
+ finishCurveDragResult,
getCurveGridCoordinates,
- resolveCurveDragEnd,
} from './curveEditorMath';
import { applyStructural } from './layerOps';
@@ -31,6 +31,20 @@ const IDENTITY_CURVE: [number, number][] = [
[255, 255],
];
+const withCurve = (
+ base: CanvasAdjustmentsContract,
+ channel: CurveChannel,
+ points: [number, number][]
+): CanvasAdjustmentsContract => ({
+ ...base,
+ curves: {
+ b: base.curves?.b ?? IDENTITY_CURVE,
+ g: base.curves?.g ?? IDENTITY_CURVE,
+ r: base.curves?.r ?? IDENTITY_CURVE,
+ [channel]: points,
+ },
+});
+
const formatSigned = (value: number): string => `${value > 0 ? '+' : ''}${Math.round(value * 100)}`;
interface AdjustmentsPopoverProps {
@@ -108,29 +122,12 @@ const AdjustmentsControls = ({ adjustments, engine, layer }: AdjustmentsControls
commit(t('widgets.layers.adjustments.reset'), { ...DEFAULT_ADJUSTMENTS }, adjustments);
}, [adjustments, commit, t]);
- const withCurve = useCallback(
- (
- base: CanvasAdjustmentsContract,
- channel: CurveChannel,
- points: [number, number][]
- ): CanvasAdjustmentsContract => ({
- ...base,
- curves: {
- b: base.curves?.b ?? IDENTITY_CURVE,
- g: base.curves?.g ?? IDENTITY_CURVE,
- r: base.curves?.r ?? IDENTITY_CURVE,
- [channel]: points,
- },
- }),
- []
- );
-
// Live (render-only) during a curve-point drag: preview without pushing history.
const handleCurveLive = useCallback(
(channel: CurveChannel, points: [number, number][]) => {
patchLive(withCurve(adjustments, channel, points));
},
- [adjustments, patchLive, withCurve]
+ [adjustments, patchLive]
);
const handleCurveCancel = useCallback((before: CanvasAdjustmentsContract) => patchLive(before), [patchLive]);
@@ -140,10 +137,10 @@ const AdjustmentsControls = ({ adjustments, engine, layer }: AdjustmentsControls
// `adjustments` has already advanced via the live previews), so it undoes the
// WHOLE gesture rather than the last frame.
const handleCurveCommit = useCallback(
- (channel: CurveChannel, points: [number, number][], before: CanvasAdjustmentsContract) => {
- commit(t('widgets.layers.adjustments.curves'), withCurve(before, channel, points), before);
+ (current: CanvasAdjustmentsContract, before: CanvasAdjustmentsContract) => {
+ commit(t('widgets.layers.adjustments.curves'), current, before);
},
- [commit, t, withCurve]
+ [commit, t]
);
return (
@@ -249,7 +246,7 @@ interface CurvesEditorProps {
/** Restores the pre-drag snapshot when the browser cancels a gesture. */
onCancel: (before: CanvasAdjustmentsContract) => void;
/** Commits one history entry for a completed gesture, undoing to `before`. */
- onCommit: (channel: CurveChannel, points: [number, number][], before: CanvasAdjustmentsContract) => void;
+ onCommit: (current: CanvasAdjustmentsContract, before: CanvasAdjustmentsContract) => void;
}
/** A compact per-channel curves editor (SVG): drag points, click to add, double-click to remove. */
@@ -357,12 +354,13 @@ const CurvesEditor = ({ adjustments, onCancel, onCommit, onLive }: CurvesEditorP
beforeRef.current = null;
latestPointsRef.current = null;
if (wasDragging && before && finalPoints) {
- const resolution = resolveCurveDragEnd(cancelled, before, finalPoints);
- if (resolution.commit) {
- onCommit(channel, resolution.commit, before);
- } else if (resolution.restore) {
- onCancel(resolution.restore);
- }
+ finishCurveDragResult({
+ before,
+ cancelled,
+ current: withCurve(before, channel, finalPoints),
+ onCommit: (current) => onCommit(current, before),
+ onPreview: onCancel,
+ });
}
};
@@ -379,7 +377,7 @@ const CurvesEditor = ({ adjustments, onCancel, onCommit, onLive }: CurvesEditorP
return;
}
const next = [...points, [nx, ny] as [number, number]].sort((a, b) => a[0] - b[0]);
- onCommit(channel, next, adjustments);
+ onCommit(withCurve(adjustments, channel, next), adjustments);
};
const handleRemove = (index: number) => (event: ReactPointerEvent<SVGCircleElement>) => {
@@ -388,8 +386,11 @@ const CurvesEditor = ({ adjustments, onCancel, onCommit, onLive }: CurvesEditorP
return;
}
onCommit(
- channel,
- points.filter((_, i) => i !== index),
+ withCurve(
+ adjustments,
+ channel,
+ points.filter((_, i) => i !== index)
+ ),
adjustments
);
};
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/layers/curveEditorMath.test.ts b/invokeai/frontend/webv2/src/workbench/widgets/layers/curveEditorMath.test.ts
index 89e6a2ed27..075f4affc2 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/layers/curveEditorMath.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/widgets/layers/curveEditorMath.test.ts
@@ -1,6 +1,39 @@
-import { describe, expect, it } from 'vitest';
+import type { CanvasAdjustmentsContract } from '@workbench/types';
-import { curvePointFromSvg, curvePointToSvg, getCurveGridCoordinates, resolveCurveDragEnd } from './curveEditorMath';
+import { describe, expect, it, vi } from 'vitest';
+
+import { curvePointFromSvg, curvePointToSvg, finishCurveDragResult, getCurveGridCoordinates } from './curveEditorMath';
+
+const BEFORE: CanvasAdjustmentsContract = {
+ brightness: 0,
+ contrast: 0,
+ curves: {
+ b: [
+ [0, 0],
+ [255, 255],
+ ],
+ g: [
+ [0, 0],
+ [255, 255],
+ ],
+ r: [
+ [0, 0],
+ [255, 255],
+ ],
+ },
+ saturation: 0,
+};
+
+const CURRENT: CanvasAdjustmentsContract = {
+ ...BEFORE,
+ curves: {
+ ...BEFORE.curves!,
+ r: [
+ [0, 32],
+ [255, 255],
+ ],
+ },
+};
describe('curve editor coordinates', () => {
it('insets endpoints so handles are fully visible', () => {
@@ -18,29 +51,25 @@ describe('curve editor coordinates', () => {
expect(getCurveGridCoordinates()).toEqual([6, 48, 90, 132, 174]);
});
- it('restores pre-drag points without a commit when cancelled', () => {
- const before = [
- [0, 0],
- [255, 255],
- ];
- const current = [
- [0, 32],
- [255, 255],
- ];
+ it('restores the pre-drag snapshot without committing when cancelled after a live change', () => {
+ const onPreview = vi.fn();
+ const onCommit = vi.fn();
+
+ finishCurveDragResult({ before: BEFORE, cancelled: true, current: CURRENT, onCommit, onPreview });
- expect(resolveCurveDragEnd(true, before, current)).toEqual({ commit: null, restore: before });
+ expect(onPreview).toHaveBeenCalledTimes(1);
+ expect(onPreview).toHaveBeenCalledWith(BEFORE);
+ expect(onCommit).not.toHaveBeenCalled();
});
- it('commits changed points without restoring when completed', () => {
- const before = [
- [0, 0],
- [255, 255],
- ];
- const current = [
- [0, 32],
- [255, 255],
- ];
+ it('commits the current snapshot without restoring on pointer up', () => {
+ const onPreview = vi.fn();
+ const onCommit = vi.fn();
+
+ finishCurveDragResult({ before: BEFORE, cancelled: false, current: CURRENT, onCommit, onPreview });
- expect(resolveCurveDragEnd(false, before, current)).toEqual({ commit: current, restore: null });
+ expect(onCommit).toHaveBeenCalledTimes(1);
+ expect(onCommit).toHaveBeenCalledWith(CURRENT);
+ expect(onPreview).not.toHaveBeenCalled();
});
});
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/layers/curveEditorMath.ts b/invokeai/frontend/webv2/src/workbench/widgets/layers/curveEditorMath.ts
index 7783794923..75058957c6 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/layers/curveEditorMath.ts
+++ b/invokeai/frontend/webv2/src/workbench/widgets/layers/curveEditorMath.ts
@@ -1,3 +1,5 @@
+import type { CanvasAdjustmentsContract } from '@workbench/types';
+
export const CURVE_SIZE = 180;
export const CURVE_PADDING = 6;
@@ -7,12 +9,27 @@ const clamp255 = (value: number): number => Math.max(0, Math.min(255, value));
export const getCurveGridCoordinates = (): number[] =>
Array.from({ length: 5 }, (_, index) => CURVE_PADDING + (index / 4) * CURVE_DRAW_SIZE);
-export const resolveCurveDragEnd = <TRestore, TCommit>(
- cancelled: boolean,
- before: TRestore,
- current: TCommit
-): { commit: TCommit | null; restore: TRestore | null } =>
- cancelled ? { commit: null, restore: before } : { commit: current, restore: null };
+interface FinishCurveDragResultArgs {
+ cancelled: boolean;
+ before: CanvasAdjustmentsContract;
+ current: CanvasAdjustmentsContract;
+ onPreview: (adjustments: CanvasAdjustmentsContract) => void;
+ onCommit: (adjustments: CanvasAdjustmentsContract) => void;
+}
+
+export const finishCurveDragResult = ({
+ before,
+ cancelled,
+ current,
+ onCommit,
+ onPreview,
+}: FinishCurveDragResultArgs): void => {
+ if (cancelled) {
+ onPreview(before);
+ return;
+ }
+ onCommit(current);
+};
export const curvePointToSvg = (x: number, y: number): { cx: number; cy: number } => ({
cx: CURVE_PADDING + (clamp255(x) / 255) * CURVE_DRAW_SIZE,