fix(pen): eraser lag/stuck-red/reliability; zoom glitch-reject; native input diag
All checks were successful
CI / Windows build (push) Successful in 17m58s

Eraser (regression from the preview I added):
- LAG: the preview did setState on every hover/erase-move (rebuilding the whole
  canvas) and recomputed perfect_freehand getStroke per overlapped stroke per
  frame. Now the cursor is a ValueNotifier driving the preview layer's repaint
  directly (no canvas rebuild), and the highlight is a plain polyline of the
  point-runs inside the radius (no getStroke).
- STUCK RED ("一直红着"): the cursor was never cleared. Preview is now
  active-erase-only and cleared on pen up/cancel.
- "选中了的笔画也不见得能删掉": radius was strokeWidth*2 (tiny) so a pass removed
  ~2 points and the stroke survived. Now a decisive fixed 0.02 (page-width
  fraction). The highlight traces exactly the point-run that splitStrokeByCircle
  removes, so what turns red is what gets deleted.

Zoom: replace the per-frame scale CLAMP with glitch REJECTION — drop a frame
demanding an implausible per-frame scale jump (>1.4x or <0.71x; a real pinch is
≲1.15x/frame). A dropped frame catches up the next frame (absolute tracking), so
no lag, but the Windows multi-touch spike never shows. Pairs with the existing
pointer-count re-baseline.

Native diagnostic: ObservePenMessage now counts WM_POINTER* / PT_PEN / legacy
mouse messages it sees and emits them on the channel; PenInputService exposes
`debugSummary` and the overlay shows `native ptr=… pen=… mouse=… msg=0x…`. This
will tell us on-device whether WM_POINTER ever reaches the observer (→ buttons
recoverable) or Flutter is on a non-pointer path (→ not).

Dart: analyze clean, 66/66 tests, linux build green. Native compiles on CI.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-22 22:02:17 +08:00
parent 45d89b7790
commit ae9e070b46
5 changed files with 147 additions and 89 deletions

View File

@@ -4,10 +4,10 @@
// coordinates; both painters receive the on-screen page [Size] and scale
// points into pixels at paint time. perfect_freehand produces the outline.
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:perfect_freehand/perfect_freehand.dart' as pf;
import '../engine/stroke_eraser.dart' show strokeHit;
import '../engine/stroke_geometry.dart' show kDefaultPenThinning;
import 'pen_stroke.dart';
@@ -116,13 +116,13 @@ class EraserPreviewPainter extends CustomPainter {
required this.radius,
required this.aspect,
required this.pageSize,
this.thinning = kDefaultPenThinning,
});
}) : super(repaint: cursor);
final List<PenStroke> strokes;
/// Eraser center in normalized page coords, or null when no preview.
final PenPoint? cursor;
/// Eraser center in normalized page coords (null = no preview). A listenable
/// so the painter repaints on cursor moves WITHOUT rebuilding the canvas.
final ValueListenable<PenPoint?> cursor;
/// Eraser radius as a fraction of page width (matches the live erase test).
final double radius;
@@ -131,25 +131,44 @@ class EraserPreviewPainter extends CustomPainter {
final double aspect;
final Size pageSize;
final double thinning;
@override
void paint(Canvas canvas, Size size) {
final c = cursor;
final c = cursor.value;
if (c == null) return;
// Faint outline on each stroke the eraser currently overlaps.
// CHEAP, ACCURATE highlight: trace ONLY the point-runs inside the eraser
// radius — i.e. exactly what splitStrokeByCircle will remove — as a plain
// polyline (no perfect_freehand getStroke; that was the eraser lag source).
// So what turns red is exactly what gets deleted.
final r2 = radius * radius;
final highlight = Paint()
..color = const Color(0xFFFF5252).withValues(alpha: 0.55)
..color = const Color(0xFFFF5252).withValues(alpha: 0.85)
..style = PaintingStyle.stroke
..strokeWidth = 1.5
..strokeWidth = 3.0
..strokeCap = StrokeCap.round
..strokeJoin = StrokeJoin.round
..isAntiAlias = true;
for (final stroke in strokes) {
if (!strokeHit(stroke, c.x, c.y, radius, aspect: aspect)) continue;
final path =
buildStrokePath(stroke, pageSize, isComplete: true, thinning: thinning);
if (path.getBounds().isEmpty) continue;
canvas.drawPath(path, highlight);
Path? run;
void flush() {
if (run != null) {
canvas.drawPath(run!, highlight);
run = null;
}
}
for (final pt in stroke.points) {
final dx = pt.x - c.x;
final dy = (pt.y - c.y) * aspect;
if (dx * dx + dy * dy < r2) {
final o = Offset(pt.x * pageSize.width, pt.y * pageSize.height);
(run ??= Path()..moveTo(o.dx, o.dy)).lineTo(o.dx, o.dy);
} else {
flush();
}
}
flush();
}
// The eraser circle itself (radius is a page-width fraction → px = r * w).
@@ -159,7 +178,7 @@ class EraserPreviewPainter extends CustomPainter {
center,
rPx,
Paint()
..color = const Color(0xFF9E9E9E).withValues(alpha: 0.5)
..color = const Color(0xFF757575).withValues(alpha: 0.7)
..style = PaintingStyle.stroke
..strokeWidth = 1.0
..isAntiAlias = true,
@@ -167,22 +186,18 @@ class EraserPreviewPainter extends CustomPainter {
canvas.drawCircle(
center,
rPx,
Paint()
..color = const Color(0x14000000)
..style = PaintingStyle.fill,
Paint()..color = const Color(0x14000000),
);
}
@override
bool shouldRepaint(EraserPreviewPainter old) =>
old.cursor?.x != cursor?.x ||
old.cursor?.y != cursor?.y ||
!identical(old.cursor, cursor) ||
old.radius != radius ||
old.aspect != aspect ||
!identical(old.strokes, strokes) ||
old.strokes.length != strokes.length ||
old.pageSize != pageSize ||
old.thinning != thinning;
old.pageSize != pageSize;
}
/// Paints just the in-progress stroke (the live layer), kept behind its own