feat(pen): eraser delete-preview + pre-disable IV pan on stylus hover
All checks were successful
CI / Windows build (push) Successful in 14m2s

Eraser preview (user request, "加一个淡一点的描边"): new EraserPreviewPainter
shows the eraser circle and a faint red outline over the committed strokes the
eraser currently overlaps, so you can see what is about to be deleted. Mounted
only in eraser mode (tool or barrel/inverted signal) with a live cursor that
follows the hovering/erasing pen; shares _eraserRadius/_pageAspect with the live
erase so preview and action always agree. Kept in its own RepaintBoundary.

Pen-feel fix (the "写字识别成单击" pan-steal): panEnabled now also requires the
last pointer to not be a stylus. Windows fires stylus HOVER before contact, so
_lastStylus is already true when the pen touches down -> the InteractiveViewer's
pan is disabled BEFORE the stroke's first move, instead of one frame late. A
2+ pointer pinch still always pans (focal translation); a finger/mouse down
flips _lastStylus back so finger-pan keeps working.

Grounded in Rnote + Saber research: Saber uses the same button detection we have
(buttons==kSecondaryButton || invertedStylus); the deeper zoom-flash / draw-vs-
pan robustness wants a Saber-style forked InteractiveViewer (single recognizer,
decide-at-start) -- scoped as the next step, not done here. 66/66 tests, linux
build green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-22 20:03:50 +08:00
parent c045fdd3ca
commit 25400c8b82
2 changed files with 159 additions and 9 deletions

View File

@@ -128,6 +128,45 @@ class _PenCanvasState extends State<PenCanvas> {
/// inverted stylus), detected on hover/down.
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;
/// True when the most recent pointer was a stylus. Set on stylus HOVER, which
/// precedes contact on Windows, so the InteractiveViewer's pan is already
/// disabled BEFORE the stroke starts — killing the 1-frame pan-steal that
/// corrupts fast strokes (the "写字识别成单击" feel bug). A finger/mouse down
/// flips it back so finger-pan still works.
bool _lastStylus = false;
/// True when the eraser would act (eraser tool selected, or a barrel/inverted
/// eraser signal is live).
bool get _isEraserMode =>
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;
/// 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;
void _setLastStylus(bool v) {
if (_lastStylus == v) return;
setState(() => _lastStylus = v);
}
/// 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
@@ -269,7 +308,10 @@ class _PenCanvasState extends State<PenCanvas> {
if (_eraserActive || widget.tool == CanvasTool.eraser) {
_eraseAt(p);
// Keep the stroke pointer reserved so moves keep erasing, but don't paint.
setState(() => _liveStroke = null);
setState(() {
_liveStroke = null;
_eraserCursor = p;
});
return;
}
_updateLiveStroke();
@@ -282,6 +324,7 @@ class _PenCanvasState extends State<PenCanvas> {
if (_eraserActive || widget.tool == CanvasTool.eraser) {
_eraseAt(p);
setState(() => _eraserCursor = p);
return;
}
_livePoints.add(p);
@@ -339,10 +382,8 @@ class _PenCanvasState extends State<PenCanvas> {
/// stays round on screen (the page rect is not square).
void _eraseAt(PenPoint? p) {
if (p == null) return;
final radius = widget.strokeWidth * 2; // normalized (page-width fraction)
final aspect = widget.pageSize.width <= 0
? 1.0
: widget.pageSize.height / widget.pageSize.width;
final radius = _eraserRadius; // normalized (page-width fraction)
final aspect = _pageAspect;
for (var i = widget.strokes.length - 1; i >= 0; i--) {
final stroke = widget.strokes[i];
if (!strokeHit(stroke, p.x, p.y, radius, aspect: aspect)) continue;
@@ -375,17 +416,21 @@ class _PenCanvasState extends State<PenCanvas> {
void _onPointerHover(PointerHoverEvent event) {
if (_isStylus(event.kind)) {
_setLastStylus(true);
_emitPenDebug(event);
// Fire edge-triggered button actions (undo / toggleTool) on hover so a
// mapped barrel press works without first touching down.
_dispatchHwButtonActions();
// Detect eraser (barrel button / inverted) while hovering.
_eraserActive = _isEraserSignal(event);
// Live eraser-preview cursor follows the hovering pen.
_updateEraserCursor(event.position);
}
}
void _onPointerDown(PointerDownEvent event) {
if (event.kind == PointerDeviceKind.trackpad) return;
_setLastStylus(_isStylus(event.kind));
if (_isStylus(event.kind)) {
_emitPenDebug(event);
// Fire edge-triggered button actions for a direct pen-down (no prior
@@ -437,10 +482,17 @@ class _PenCanvasState extends State<PenCanvas> {
@override
Widget build(BuildContext context) {
// Pan only when NOT mid-stroke; while drawing we suppress IV pan so it
// can't fight the stroke. (A 2nd finger cancels the stroke first, so pinch
// re-enables pan/zoom immediately.)
final panEnabled = _drawPointer == null;
// Pan rules (pen-first):
// - A 2+ pointer pinch ALWAYS pans (the focal-point translation is part of
// zooming), regardless of pen state.
// - Otherwise pan only when NOT mid-stroke AND the last pointer was not a
// stylus. Because Windows fires stylus HOVER before contact, _lastStylus
// is already true when the pen touches down, so the InteractiveViewer's
// pan is disabled BEFORE the stroke's first move — no 1-frame pan-steal
// that would corrupt a fast flick into a tap.
final panEnabled = _activePointers.length >= 2
? true
: (_drawPointer == null && !_lastStylus);
return Listener(
onPointerHover: _onPointerHover,
@@ -482,6 +534,23 @@ 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)
Positioned.fill(
child: RepaintBoundary(
child: CustomPaint(
painter: EraserPreviewPainter(
strokes: widget.strokes,
cursor: _eraserCursor,
radius: _eraserRadius,
aspect: _pageAspect,
pageSize: widget.pageSize,
thinning: widget.thinning,
),
),
),
),
// Live ink (current stroke only, isolated repaint).
Positioned.fill(
child: RepaintBoundary(