Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(desktop): pointercancel 丢弃很短的误触半笔,较长笔迹照常保留

Dashf736a99b49b2
diff --git a/apps/desktop/src/renderer/components/chat/ImageLightbox.tsx b/apps/desktop/src/renderer/components/chat/ImageLightbox.tsx
index 8af6a3c725..86c9db777f 100644
--- a/apps/desktop/src/renderer/components/chat/ImageLightbox.tsx
+++ b/apps/desktop/src/renderer/components/chat/ImageLightbox.tsx
@@ -83,6 +83,10 @@ import {
type AnnotationStroke,
type DraftSvgPathCache,
} from './lightboxAnnotations';
+import {
+ INTERRUPTED_STROKE_DISCARD_SCREEN_PX,
+ annotationStrokeScreenLength,
+} from '@cindy/maker-shared/image-annotation';
import { AnnotationStrokesSvg, type AnnotationDraftPathRefs } from './AnnotationStrokesSvg';
import { useOptionalConfirmDialog } from '@/components/ui/confirm-dialog-provider';
import { getDraft, saveDraft } from '@/lib/composerDraftStore';
@@ -987,7 +991,7 @@ export function ImageLightbox({
scheduleDraftPaint();
}
- /** 收笔(pointerup / pointercancel / 失去 capture 均走这里,幂等)。 */
+ /** 收笔(pointerup / 失去 capture 走这里,较长的 pointercancel 也经此提交;幂等)。 */
function finishAnnotateStroke(e: React.PointerEvent<HTMLDivElement>) {
if (e.pointerId !== drawingPointerIdRef.current) return;
const draft = draftStrokeRef.current;
@@ -1006,6 +1010,27 @@ export function ImageLightbox({
}
}
+ /**
+ * pointercancel(系统 / 浏览器接管了这次触摸或笔输入,并非用户抬笔):很短的
+ * 半笔视为误触丢弃,较长的笔迹照常保留,不丢用户真正画的内容。阈值与手机端
+ * 多指打断共用。
+ */
+ function cancelAnnotateStroke(e: React.PointerEvent<HTMLDivElement>) {
+ if (e.pointerId !== drawingPointerIdRef.current) return;
+ const draft = draftStrokeRef.current;
+ const rect = imgRef.current?.getBoundingClientRect();
+ if (
+ draft &&
+ rect &&
+ annotationStrokeScreenLength(draft.points, rect.width, rect.height) <
+ INTERRUPTED_STROKE_DISCARD_SCREEN_PX
+ ) {
+ resetDraftStroke();
+ return;
+ }
+ finishAnnotateStroke(e);
+ }
+
/** 即时烧录当前所见(复制/另存为用),实现在共享模块 annotationBurnIn。 */
async function materializeAnnotatedImage(
outMimeOverride?: 'image/png' | 'image/jpeg',
@@ -1496,7 +1521,7 @@ export function ImageLightbox({
onPointerDown={isAnnotating ? handleAnnotatePointerDown : undefined}
onPointerMove={isAnnotating ? handleAnnotatePointerMove : undefined}
onPointerUp={isAnnotating ? finishAnnotateStroke : undefined}
- onPointerCancel={isAnnotating ? finishAnnotateStroke : undefined}
+ onPointerCancel={isAnnotating ? cancelAnnotateStroke : undefined}
onLostPointerCapture={isAnnotating ? finishAnnotateStroke : undefined}
onDoubleClick={isAnnotating ? undefined : handleImageDoubleClick}
>
diff --git a/apps/desktop/src/renderer/components/chat/__tests__/ImageLightboxAnnotate.test.tsx b/apps/desktop/src/renderer/components/chat/__tests__/ImageLightboxAnnotate.test.tsx
index 795c927985..f8fb329afa 100644
--- a/apps/desktop/src/renderer/components/chat/__tests__/ImageLightboxAnnotate.test.tsx
+++ b/apps/desktop/src/renderer/components/chat/__tests__/ImageLightboxAnnotate.test.tsx
@@ -171,6 +171,23 @@ describe('ImageLightbox annotate mode', () => {
expect(committedPaths('stroke')).toHaveLength(2);
});
+ it('pointercancel drops a tiny interrupted stroke but keeps a real one', () => {
+ render(<ImageLightbox src={SRC} onClose={vi.fn()} annotationEdit={{ onSave: vi.fn() }} />);
+ loadImage(lightboxImage());
+ openAnnotate();
+ const el = surface();
+ // 起笔后几乎没动就被系统接管:视为误触,不留红点。
+ fireEvent.pointerDown(el, { pointerId: 7, button: 0, clientX: 20, clientY: 20 });
+ fireEvent.pointerMove(el, { pointerId: 7, clientX: 24, clientY: 22 });
+ fireEvent.pointerCancel(el, { pointerId: 7, clientX: 24, clientY: 22 });
+ expect(committedPaths('stroke')).toHaveLength(0);
+ // 已经画了一段的笔迹被打断:照常保留。
+ fireEvent.pointerDown(el, { pointerId: 8, button: 0, clientX: 20, clientY: 20 });
+ fireEvent.pointerMove(el, { pointerId: 8, clientX: 120, clientY: 60 });
+ fireEvent.pointerCancel(el, { pointerId: 8, clientX: 120, clientY: 60 });
+ expect(committedPaths('stroke')).toHaveLength(1);
+ });
+
it('greys out undo / redo when there is nothing to undo or redo', () => {
render(<ImageLightbox src={SRC} onClose={vi.fn()} annotationEdit={{ onSave: vi.fn() }} />);
loadImage(lightboxImage());
diff --git a/apps/mobile/src/session/imageAnnotationModel.ts b/apps/mobile/src/session/imageAnnotationModel.ts
index 929ffbf8ca..9b92876083 100644
--- a/apps/mobile/src/session/imageAnnotationModel.ts
+++ b/apps/mobile/src/session/imageAnnotationModel.ts
@@ -18,6 +18,7 @@
import {
ANNOTATION_CANVAS_SCRIPT,
+ INTERRUPTED_STROKE_DISCARD_SCREEN_PX,
annotationStrokeToSvgPath,
annotationStrokeWidth,
annotationOutlineWidth,
@@ -141,7 +142,7 @@ export function annotationMinPointDistanceForRect(
* 短于它的半笔视为捏合起手的误触(点 / 小短线)丢弃;单指点按(手势正常结束)
* 与真正的笔画不受影响。
*/
-export const ANNOTATION_MULTI_TOUCH_DISCARD_SCREEN_PX = 12;
+export const ANNOTATION_MULTI_TOUCH_DISCARD_SCREEN_PX = INTERRUPTED_STROKE_DISCARD_SCREEN_PX;
/**
* 画笔手势结束时是否丢弃进行中的一笔:仅当手势非正常结束(第二根手指落下使
diff --git a/packages/maker-shared/src/__tests__/imageAnnotation.test.ts b/packages/maker-shared/src/__tests__/imageAnnotation.test.ts
index f3a6d756b5..10ee2bf3fa 100644
--- a/packages/maker-shared/src/__tests__/imageAnnotation.test.ts
+++ b/packages/maker-shared/src/__tests__/imageAnnotation.test.ts
@@ -4,7 +4,9 @@ import {
ANNOTATION_OUTLINE_COLOR,
ANNOTATION_STROKE_COLOR,
MAX_ANNOTATION_REGIONS,
+ INTERRUPTED_STROKE_DISCARD_SCREEN_PX,
annotationOutlineWidth,
+ annotationStrokeScreenLength,
annotationStrokeToSvgPath,
annotationStrokeWidth,
drawAnnotationStrokes,
@@ -231,3 +233,14 @@ describe('formatAnnotationRegion', () => {
);
});
});
+
+describe('annotationStrokeScreenLength', () => {
+ it('sums segment lengths in display pixels', () => {
+ expect(annotationStrokeScreenLength([{ x: 0, y: 0 }], 100, 100)).toBe(0);
+ expect(annotationStrokeScreenLength([{ x: 0, y: 0 }, { x: 0.03, y: 0.04 }], 100, 100)).toBeCloseTo(5);
+ expect(
+ annotationStrokeScreenLength([{ x: 0, y: 0 }, { x: 0.1, y: 0 }, { x: 0.1, y: 0.1 }], 200, 100),
+ ).toBeCloseTo(30);
+ expect(INTERRUPTED_STROKE_DISCARD_SCREEN_PX).toBe(12);
+ });
+});
diff --git a/packages/maker-shared/src/imageAnnotation.ts b/packages/maker-shared/src/imageAnnotation.ts
index a08c551f0b..58afadcb5f 100644
--- a/packages/maker-shared/src/imageAnnotation.ts
+++ b/packages/maker-shared/src/imageAnnotation.ts
@@ -106,6 +106,26 @@ export function annotationStrokeWidth(naturalWidth: number, naturalHeight: numbe
return Math.round(Math.max(primary, floor));
}
+/**
+ * 被系统 / 第二根手指打断(非正常抬笔)的笔迹,屏幕路径短于该值时视为误触丢弃;
+ * 更长的笔迹照常保留,避免丢掉用户真正画了的内容。桌面 pointercancel 与手机
+ * 多指打断共用同一阈值。
+ */
+export const INTERRUPTED_STROKE_DISCARD_SCREEN_PX = 12;
+
+/** 笔迹在 width×height 显示尺寸下的屏幕路径长度(像素)。 */
+export function annotationStrokeScreenLength(
+ points: readonly AnnotationPoint[],
+ width: number,
+ height: number,
+): number {
+ let length = 0;
+ for (let i = 1; i < points.length; i++) {
+ length += Math.hypot((points[i].x - points[i - 1].x) * width, (points[i].y - points[i - 1].y) * height);
+ }
+ return length;
+}
+
/** 白描边线宽(与 {@link annotationStrokeWidth} 配套)。 */
export function annotationOutlineWidth(strokeWidth: number): number {
return Math.round(strokeWidth * ANNOTATION_OUTLINE_WIDTH_RATIO);