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): cover curves pointer cancellation

joshistoast99fea91e7ce2
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/layers/AdjustmentsPopover.tsx b/invokeai/frontend/webv2/src/workbench/widgets/layers/AdjustmentsPopover.tsx
index 19e58dcd68..2a8e0326e3 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/layers/AdjustmentsPopover.tsx
+++ b/invokeai/frontend/webv2/src/workbench/widgets/layers/AdjustmentsPopover.tsx
@@ -16,6 +16,7 @@ import {
curvePointFromSvg,
curvePointToSvg,
getCurveGridCoordinates,
+ resolveCurveDragEnd,
} from './curveEditorMath';
import { applyStructural } from './layerOps';
@@ -341,7 +342,7 @@ const CurvesEditor = ({ adjustments, onCancel, onCommit, onLive }: CurvesEditorP
onLive(channel, next);
};
- const finishDrag = (event: ReactPointerEvent<SVGSVGElement>, commitDrag: boolean) => {
+ const finishDrag = (event: ReactPointerEvent<SVGSVGElement>, cancelled: boolean) => {
const wasDragging = dragIndexRef.current !== null;
const dragTarget = dragTargetRef.current;
if (dragTarget?.hasPointerCapture(event.pointerId)) {
@@ -355,15 +356,18 @@ const CurvesEditor = ({ adjustments, onCancel, onCommit, onLive }: CurvesEditorP
const finalPoints = latestPointsRef.current;
beforeRef.current = null;
latestPointsRef.current = null;
- if (wasDragging && before && finalPoints && commitDrag) {
- onCommit(channel, finalPoints, before);
- } else if (wasDragging && before && finalPoints) {
- onCancel(before);
+ 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);
+ }
}
};
- const handleUp = (event: ReactPointerEvent<SVGSVGElement>) => finishDrag(event, true);
- const handleCancel = (event: ReactPointerEvent<SVGSVGElement>) => finishDrag(event, false);
+ const handleUp = (event: ReactPointerEvent<SVGSVGElement>) => finishDrag(event, false);
+ const handleCancel = (event: ReactPointerEvent<SVGSVGElement>) => finishDrag(event, true);
const handleAdd = (event: ReactPointerEvent<SVGSVGElement>) => {
if (dragIndexRef.current !== null) {
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 c3f540f678..89e6a2ed27 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,6 @@
import { describe, expect, it } from 'vitest';
-import { curvePointFromSvg, curvePointToSvg, getCurveGridCoordinates } from './curveEditorMath';
+import { curvePointFromSvg, curvePointToSvg, getCurveGridCoordinates, resolveCurveDragEnd } from './curveEditorMath';
describe('curve editor coordinates', () => {
it('insets endpoints so handles are fully visible', () => {
@@ -17,4 +17,30 @@ describe('curve editor coordinates', () => {
it('places the grid at quarter intervals within the inset plot', () => {
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],
+ ];
+
+ expect(resolveCurveDragEnd(true, before, current)).toEqual({ commit: null, restore: before });
+ });
+
+ it('commits changed points without restoring when completed', () => {
+ const before = [
+ [0, 0],
+ [255, 255],
+ ];
+ const current = [
+ [0, 32],
+ [255, 255],
+ ];
+
+ expect(resolveCurveDragEnd(false, before, current)).toEqual({ commit: current, restore: null });
+ });
});
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/layers/curveEditorMath.ts b/invokeai/frontend/webv2/src/workbench/widgets/layers/curveEditorMath.ts
index b4bd8f015b..7783794923 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/layers/curveEditorMath.ts
+++ b/invokeai/frontend/webv2/src/workbench/widgets/layers/curveEditorMath.ts
@@ -7,6 +7,13 @@ 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 };
+
export const curvePointToSvg = (x: number, y: number): { cx: number; cy: number } => ({
cx: CURVE_PADDING + (clamp255(x) / 255) * CURVE_DRAW_SIZE,
cy: CURVE_SIZE - CURVE_PADDING - (clamp255(y) / 255) * CURVE_DRAW_SIZE,