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

View File

@@ -130,8 +130,10 @@ class _PenCanvasState extends State<PenCanvas> {
bool _eraserActive = false;
/// Eraser preview cursor (normalized page coords), or null when not in eraser
/// mode / the pen is not near the page. Drives [EraserPreviewPainter].
PenPoint? _eraserCursor;
/// mode / the pen is not near the page. A ValueNotifier so the preview layer
/// repaints on cursor moves WITHOUT rebuilding the whole canvas every frame
/// (the old per-move setState was the eraser-lag source).
final ValueNotifier<PenPoint?> _eraserCursor = ValueNotifier<PenPoint?>(null);
/// True when the eraser would act (eraser tool selected, or a barrel/inverted
/// eraser signal is live).
@@ -139,23 +141,16 @@ class _PenCanvasState extends State<PenCanvas> {
widget.tool == CanvasTool.eraser || _eraserActive;
/// Eraser radius as a fraction of page width (shared by the live erase and the
/// preview overlay so they always agree).
double get _eraserRadius => widget.strokeWidth * 2;
/// preview overlay so they always agree). A decisive fixed size — the old
/// strokeWidth*2 was so small that a pass removed only a couple of points and
/// the stroke visibly survived ("选中了的笔画也不见得能删掉").
static const double _eraserRadius = 0.02;
/// Page aspect (height / width) so the eraser circle stays round on screen.
double get _pageAspect => widget.pageSize.width <= 0
? 1.0
: widget.pageSize.height / widget.pageSize.width;
/// Update (or clear) the eraser-preview cursor from a global pointer position.
void _updateEraserCursor(Offset globalPosition) {
if (_isEraserMode) {
setState(() => _eraserCursor = _toNormalized(globalPosition, null));
} else if (_eraserCursor != null) {
setState(() => _eraserCursor = null);
}
}
// The explicit user toggle wins: if finger-drawing is ON, a single finger
// draws even after a stylus has been seen. (Palm rejection when the toggle is
// OFF is automatic — fingers simply never draw — and a 2nd pointer always
@@ -295,12 +290,12 @@ class _PenCanvasState extends State<PenCanvas> {
if (p != null) _livePoints.add(p);
if (_eraserActive || widget.tool == CanvasTool.eraser) {
_eraserCursor.value = p;
_eraseAt(p);
// Keep the stroke pointer reserved so moves keep erasing, but don't paint.
setState(() {
_liveStroke = null;
_eraserCursor = p;
});
// No setState here: the preview repaints via the notifier, and any erased
// stroke repaints via the editor's onEraseStroke setState. (_liveStroke is
// already null in eraser mode.)
if (_liveStroke != null) setState(() => _liveStroke = null);
return;
}
_updateLiveStroke();
@@ -312,8 +307,8 @@ class _PenCanvasState extends State<PenCanvas> {
if (p == null) return;
if (_eraserActive || widget.tool == CanvasTool.eraser) {
_eraserCursor.value = p;
_eraseAt(p);
setState(() => _eraserCursor = p);
return;
}
_livePoints.add(p);
@@ -335,6 +330,7 @@ class _PenCanvasState extends State<PenCanvas> {
}
_drawPointer = null;
_livePoints.clear();
_eraserCursor.value = null; // hide the preview when the pen lifts
setState(() => _liveStroke = null);
}
@@ -342,6 +338,7 @@ class _PenCanvasState extends State<PenCanvas> {
void _cancelStroke() {
_drawPointer = null;
_livePoints.clear();
_eraserCursor.value = null;
setState(() => _liveStroke = null);
}
@@ -400,7 +397,8 @@ class _PenCanvasState extends State<PenCanvas> {
'/${event.pressureMax.toStringAsFixed(0)} '
'norm=${norm?.toStringAsFixed(3) ?? "null"} '
'peak=${_peakNorm.toStringAsFixed(3)} '
'btn=${event.buttons} tilt=${event.tilt.toStringAsFixed(2)}');
'btn=${event.buttons} tilt=${event.tilt.toStringAsFixed(2)}'
'\n${PenInputService.instance.debugSummary}');
}
void _onPointerHover(PointerHoverEvent event) {
@@ -411,8 +409,6 @@ class _PenCanvasState extends State<PenCanvas> {
_dispatchHwButtonActions();
// Detect eraser (barrel button / inverted) while hovering.
_eraserActive = _isEraserSignal(event);
// Live eraser-preview cursor follows the hovering pen.
_updateEraserCursor(event.position);
}
}
@@ -467,6 +463,12 @@ class _PenCanvasState extends State<PenCanvas> {
if (wasDrawer) _cancelStroke();
}
@override
void dispose() {
_eraserCursor.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
// The PEN never reaches PenInteractiveViewer's recognizer (it excludes
@@ -516,7 +518,7 @@ class _PenCanvasState extends State<PenCanvas> {
),
// Eraser preview: faint outline on strokes about to be deleted +
// the eraser circle. Mounted only in eraser mode with a cursor.
if (_isEraserMode && _eraserCursor != null)
if (_isEraserMode)
Positioned.fill(
child: RepaintBoundary(
child: CustomPaint(
@@ -526,7 +528,6 @@ class _PenCanvasState extends State<PenCanvas> {
radius: _eraserRadius,
aspect: _pageAspect,
pageSize: widget.pageSize,
thinning: widget.thinning,
),
),
),

View File

@@ -40,9 +40,11 @@ const Set<PointerDeviceKind> _kPanZoomDevices = <PointerDeviceKind>{
PointerDeviceKind.unknown,
};
/// Per-frame multiplicative scale-change clamp (flicker guard).
const double _kMaxScaleChangePerFrame = 1.35;
const double _kMinScaleChangePerFrame = 1 / _kMaxScaleChangePerFrame;
/// A real pinch changes scale only modestly per frame (≲1.15x at 60fps). A frame
/// demanding far more than this is a Windows multi-touch position glitch, not
/// intent — that frame is dropped so the zoom can't pop and snap back.
const double _kScaleGlitchHi = 1.4;
const double _kScaleGlitchLo = 1 / _kScaleGlitchHi;
const double _kDrag = 0.0000135;
@@ -177,14 +179,14 @@ class _PenInteractiveViewerState extends State<PenInteractiveViewer>
case _GestureType.scale:
assert(_scaleStart != null);
final double desiredScale = _scaleStart! * details.scale;
// Flicker guard: clamp the per-frame change so a single-frame touch
// jitter can't pop the zoom and snap back. Absolute tracking means a
// real pinch just resumes next frame.
final double scaleChange = clampDouble(
desiredScale / scale,
_kMinScaleChangePerFrame,
_kMaxScaleChangePerFrame,
);
final double scaleChange = desiredScale / scale;
// Glitch rejection: drop a frame that demands an implausible per-frame
// scale jump (a Windows multi-touch position glitch). The next good
// frame resumes from the true finger positions, so the spike never
// shows — unlike clamping, which still applied a visible partial jump.
if (scaleChange > _kScaleGlitchHi || scaleChange < _kScaleGlitchLo) {
return;
}
_transformer.value = _matrixScale(_transformer.value, scaleChange);
// Keep the focal point anchored under the fingers across the scale.

View File

@@ -94,6 +94,19 @@ class PenInputService {
bool get isActive => _active;
bool _active = false;
// Native-side diagnostics (see windows/runner/pen_channel.cpp): how many
// WM_POINTER / PT_PEN / legacy-mouse messages the observer has seen. Lets the
// on-device overlay tell us WHICH layer is failing for buttons/tilt.
int _diagPtr = 0;
int _diagPen = 0;
int _diagMouse = 0;
int _diagMsg = 0;
/// One-line native readout for the diagnostic overlay.
String get debugSummary => _active
? 'native ptr=$_diagPtr pen=$_diagPen mouse=$_diagMouse msg=0x${_diagMsg.toRadixString(16)}'
: 'native: channel silent (no events)';
/// Begins listening to the native channel. Idempotent; safe on any platform
/// (no-ops where the channel has no handler).
void start() {
@@ -123,6 +136,10 @@ class PenInputService {
tiltX: (event['tiltX'] as num?)?.toDouble() ?? 0.0,
tiltY: (event['tiltY'] as num?)?.toDouble() ?? 0.0,
);
_diagPtr = (event['diagPtr'] as num?)?.toInt() ?? _diagPtr;
_diagPen = (event['diagPen'] as num?)?.toInt() ?? _diagPen;
_diagMouse = (event['diagMouse'] as num?)?.toInt() ?? _diagMouse;
_diagMsg = (event['diagMsg'] as num?)?.toInt() ?? _diagMsg;
_active = true;
}