Files
BadNote/lib/editor/canvas/pen_canvas.dart
Akiba So 7e405453e0
All checks were successful
CI / Windows build (push) Successful in 18m35s
feat(pen): forked InteractiveViewer (stylus-exclusive draw) + zoom diagnostic
Replace stock InteractiveViewer with PenInteractiveViewer, a focused fork of
Flutter 3.44's InteractiveViewer for our config (constrained=false, infinite
boundary, no rotation — that machinery dropped as a no-op here). Two deliberate
changes, grounded in the Rnote/Saber research:

1. The pan/zoom ScaleGestureRecognizer excludes stylus/invertedStylus via
   `supportedDevices`. The pen never reaches it, so a stylus stroke can no longer
   be stolen as a pan on its first frame (the "写字识别成单击" feel bug, caused by
   stock IV's panEnabled updating a frame after the stroke began). Drawing is
   owned solely by the canvas Listener; no arena fight, no panEnabled lag. The
   prior _lastStylus hover hack is removed (superseded).

2. Per-frame scale change is clamped (×0.74..×1.35). Stock IV already damps focal
   jitter and guards the pan branch, but a single-frame multi-touch glitch could
   still spike details.scale, popping the zoom bigger/smaller and snapping back
   (the reported pinch flicker). Clamping swallows the spike; a real (gradual)
   pinch is unaffected since scale tracks absolutely from gesture start. Cap is
   far above any real pinch (~1.1-1.2x/frame), so no felt lag.

Everything else (scale-about-focal, pan, fling inertia, mouse-wheel zoom) is
Flutter's proven logic verbatim.

Also add an on-device input diagnostic (bug-report toggle): the existing pen
readout already prints kind/pressure/buttons; now it also shows live
zoom=now/min/max so the next device test captures (a) whether the side/eraser
button arrives as buttons/invertedStylus, and (b) the value any residual pinch
flash jumps to. 66/66 tests, analyze clean, linux build green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-22 20:27:56 +08:00

553 lines
21 KiB
Dart

// lib/editor/canvas/pen_canvas.dart
//
// Pen-first canvas: ONE shared transform (an InteractiveViewer driven by a
// TransformationController we own) zooms/pans BOTH the PDF page bitmap and the
// ink layer together. A Listener wrapped around the InteractiveViewer reads raw
// pointer kind + pressure and tracks the active pointer COUNT to arbitrate
// draw vs pan/zoom — we own the gesture pipeline, pdfrx never sees gestures.
//
// Gesture arbitration (reimplemented clean-room from Saber's documented model):
// - A draw gesture is exactly ONE active pointer that is a stylus / inverted
// stylus, OR (when the user's finger-drawing toggle is on) a single finger.
// - >= 2 active pointers ALWAYS means pan/zoom (pinch); never draw. If a 2nd
// pointer lands while a stroke is in progress, that stroke is discarded
// (accidental palm/finger).
// - Palm rejection: once any stylus event is seen in a session, finger-drawing
// is forced OFF so a resting palm/finger pans instead of marking.
// - While a stroke is active, the InteractiveViewer's pan is disabled so it
// can't fight the stroke; pinch-zoom still works because a 2nd pointer
// cancels the stroke first, re-enabling pan/zoom.
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import '../engine/stroke_eraser.dart';
import '../engine/stroke_geometry.dart' show kDefaultPenThinning;
import '../input/pen_config.dart';
import '../input/pen_input_service.dart';
import 'ink_painters.dart';
import 'pen_interactive_viewer.dart';
import 'pen_stroke.dart';
/// The active tool on the pen canvas.
enum CanvasTool { pen, highlighter, eraser }
class PenCanvas extends StatefulWidget {
const PenCanvas({
super.key,
required this.pageWidget,
required this.pageSize,
required this.strokes,
required this.transformationController,
required this.tool,
required this.color,
required this.strokeWidth,
required this.onStrokeComplete,
required this.onEraseStroke,
this.allowFingerDrawing = false,
this.minScale = 0.5,
this.maxScale = 8.0,
this.onPenDebug,
this.thinning = kDefaultPenThinning,
this.sideButtonAction = PenButtonAction.eraser,
this.eraserEndAction = PenButtonAction.eraser,
this.onPenButtonAction,
});
/// Debug hook: called with a readout of the latest pen event
/// (kind / pressure / pressureMin / pressureMax) so we can see what Windows
/// actually delivers. Null in release UI.
final void Function(String readout)? onPenDebug;
/// The rendered PDF page bitmap, already sized to [pageSize].
final Widget pageWidget;
/// On-screen size (at scale 1.0) of the page rectangle in logical pixels.
/// Ink normalized coords map onto this rectangle.
final Size pageSize;
/// Committed strokes for the CURRENT page (normalized coords).
final List<PenStroke> strokes;
/// Shared transform driving both page and ink.
final TransformationController transformationController;
final CanvasTool tool;
final Color color;
/// Pen width as a fraction of page width (so it zooms with the page).
final double strokeWidth;
/// Called with a finished stroke (normalized coords) to commit it.
final void Function(PenStroke stroke) onStrokeComplete;
/// Called to replace committed stroke [strokeIndex] with its surviving pieces
/// after a partial (segment) erase. An empty [replacements] list removes the
/// stroke entirely (whole-stroke erase).
final void Function(int strokeIndex, List<PenStroke> replacements)
onEraseStroke;
/// User toggle: allow a single finger to draw. Forced off once a stylus is
/// seen (palm rejection).
final bool allowFingerDrawing;
final double minScale;
final double maxScale;
/// perfect_freehand pressure→width response, from `PenConfig.pressureSensitivity`.
final double thinning;
/// Configured action for the pen's side barrel button (W3 — resolved against
/// the native pen plugin's flags on Windows).
final PenButtonAction sideButtonAction;
/// Configured action for the pen's eraser/inverted end (W3).
final PenButtonAction eraserEndAction;
/// Fired (edge-triggered) when a hardware pen button mapped to a non-eraser
/// action (undo / toggleTool) is pressed.
final void Function(PenButtonAction action)? onPenButtonAction;
@override
State<PenCanvas> createState() => _PenCanvasState();
}
class _PenCanvasState extends State<PenCanvas> {
/// Active (down) pointers by id → their device kind. Size == pointer count.
final Map<int, PointerDeviceKind> _activePointers = {};
/// The pointer id currently driving a stroke, or null.
int? _drawPointer;
/// In-progress stroke points (normalized).
final List<PenPoint> _livePoints = [];
/// Live stroke snapshot handed to the LiveInkPainter; null when idle.
PenStroke? _liveStroke;
/// True when the active stylus reports the eraser signal (barrel button or
/// 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 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;
/// 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
// cancels an in-progress stroke regardless.)
bool get _fingerDrawingEnabled => widget.allowFingerDrawing;
bool _isStylus(PointerDeviceKind kind) =>
kind == PointerDeviceKind.stylus ||
kind == PointerDeviceKind.invertedStylus;
/// Normalize stylus pressure to [0,1], or null when the device reports no
/// usable pressure range (then perfect_freehand simulates pressure).
double? _normalizedPressure(PointerEvent event) {
if (!_isStylus(event.kind)) return null;
final range = event.pressureMax - event.pressureMin;
if (range > 0.0001) {
return ((event.pressure - event.pressureMin) / range).clamp(0.0, 1.0);
}
// No advertised range (some Windows pen stacks): use the raw normalized
// pressure directly if it's a usable non-degenerate value, so we still get
// real force instead of falling back to velocity simulation.
if (event.pressure > 0.0 && event.pressure < 1.0) {
return event.pressure;
}
return null;
}
/// The eraser signal. Two sources, ORed:
/// 1. Flutter-native: secondary button held or an inverted stylus (works on
/// desktop / platforms that surface these).
/// 2. Windows native pen plugin: barrel / inverted / eraser flags that
/// Flutter 3.44 drops, mapped through the configured side-button /
/// eraser-end actions (W3). Level-triggered, so holding the button keeps
/// erasing — correct for an eraser.
bool _isEraserSignal(PointerEvent event) {
// BITMASK test, not equality: Flutter defines kPrimaryStylusButton == 0x02
// == kSecondaryButton, and kStylusContact == 0x01. While the pen TIP is
// down with the barrel pressed, event.buttons == 0x03, so `== kSecondaryButton`
// (0x02) is false — the side button registered only on hover, never while
// drawing. `& kSecondaryButton != 0` catches both.
if ((event.buttons & kSecondaryButton) != 0 ||
event.kind == PointerDeviceKind.invertedStylus) {
return true;
}
final hw = PenInputService.instance;
if (hw.isActive) {
final s = hw.current;
if ((s.inverted || s.eraser) &&
widget.eraserEndAction == PenButtonAction.eraser) {
return true;
}
if (s.barrel && widget.sideButtonAction == PenButtonAction.eraser) {
return true;
}
}
return false;
}
/// Resolve the currently-active configured action from the native pen flags
/// (eraser-end takes precedence over the side button when both are set).
PenButtonAction _activeHwAction() {
final hw = PenInputService.instance;
if (!hw.isActive) return PenButtonAction.none;
final s = hw.current;
if (s.inverted || s.eraser) return widget.eraserEndAction;
if (s.barrel) return widget.sideButtonAction;
return PenButtonAction.none;
}
/// Last hardware action seen, for rising-edge detection of undo/toggleTool.
PenButtonAction _lastHwAction = PenButtonAction.none;
/// Edge-triggered dispatch of non-eraser button actions (undo / toggleTool).
/// Eraser is handled level-triggered by [_isEraserSignal]; pan suppresses
/// drawing via [_shouldDraw].
void _dispatchHwButtonActions() {
final action = _activeHwAction();
if (action == _lastHwAction) return;
_lastHwAction = action;
if (action == PenButtonAction.undo ||
action == PenButtonAction.toggleTool) {
widget.onPenButtonAction?.call(action);
}
}
/// True while a hardware button mapped to `pan` is held (suppresses drawing
/// so the InteractiveViewer pans instead).
bool get _hwPanActive => _activeHwAction() == PenButtonAction.pan;
/// Pen tilt magnitude (degrees) for a stylus event, or null when unavailable.
double? _tiltFor(PointerEvent event) {
if (!_isStylus(event.kind)) return null;
final hw = PenInputService.instance;
if (!hw.isActive) return null;
final t = hw.current.tiltMagnitude;
return t == 0 ? null : t;
}
/// Decide whether the gesture currently forming should DRAW.
/// True iff exactly one active pointer AND (stylus OR finger-drawing on).
bool _shouldDraw(PointerDeviceKind kind) {
if (_activePointers.length != 1) return false;
// A hardware pen button mapped to `pan` suppresses drawing so the
// InteractiveViewer pans instead.
if (_hwPanActive) return false;
if (_isStylus(kind)) return true;
if (kind == PointerDeviceKind.mouse) return true;
if (kind == PointerDeviceKind.touch) return _fingerDrawingEnabled;
return false;
}
// --- Coordinate mapping ---------------------------------------------------
/// Map a global pointer position into normalized page coords using the
/// shared transform (inverse) and this widget's geometry.
PenPoint? _toNormalized(Offset globalPosition, double? pressure,
{double? tilt}) {
final box = context.findRenderObject() as RenderBox?;
if (box == null) return null;
final local = box.globalToLocal(globalPosition);
// Undo the InteractiveViewer transform to get scene (untransformed) coords.
final scene = widget.transformationController.toScene(local);
final nx = scene.dx / widget.pageSize.width;
final ny = scene.dy / widget.pageSize.height;
return PenPoint(nx, ny, pressure, tilt: tilt);
}
// --- Stroke lifecycle -----------------------------------------------------
void _startStroke(PointerDownEvent event) {
_drawPointer = event.pointer;
_livePoints.clear();
final p = _toNormalized(event.position, _normalizedPressure(event),
tilt: _tiltFor(event));
if (p != null) _livePoints.add(p);
if (_eraserActive || widget.tool == CanvasTool.eraser) {
_eraseAt(p);
// Keep the stroke pointer reserved so moves keep erasing, but don't paint.
setState(() {
_liveStroke = null;
_eraserCursor = p;
});
return;
}
_updateLiveStroke();
}
void _extendStroke(PointerMoveEvent event) {
final p = _toNormalized(event.position, _normalizedPressure(event),
tilt: _tiltFor(event));
if (p == null) return;
if (_eraserActive || widget.tool == CanvasTool.eraser) {
_eraseAt(p);
setState(() => _eraserCursor = p);
return;
}
_livePoints.add(p);
_updateLiveStroke();
}
void _endStroke() {
if (_drawPointer == null) return;
final wasEraser = _eraserActive || widget.tool == CanvasTool.eraser;
if (!wasEraser && _livePoints.isNotEmpty) {
widget.onStrokeComplete(
PenStroke(
points: List.of(_livePoints),
color: _currentColor().toARGB32(),
width: widget.strokeWidth,
kind: _currentKind(),
),
);
}
_drawPointer = null;
_livePoints.clear();
setState(() => _liveStroke = null);
}
/// Discard the in-progress stroke without committing (palm/2nd-finger).
void _cancelStroke() {
_drawPointer = null;
_livePoints.clear();
setState(() => _liveStroke = null);
}
void _updateLiveStroke() {
setState(() {
_liveStroke = PenStroke(
points: List.of(_livePoints),
color: _currentColor().toARGB32(),
width: widget.strokeWidth,
kind: _currentKind(),
);
});
}
PenStrokeKind _currentKind() =>
widget.tool == CanvasTool.highlighter
? PenStrokeKind.highlighter
: PenStrokeKind.pen;
Color _currentColor() => widget.tool == CanvasTool.highlighter
? widget.color.withAlpha(0x80)
: widget.color;
/// Partial (segment) erase: find the first committed stroke the eraser circle
/// touches and replace it with its surviving pieces. The eraser radius is in
/// normalized page-width fractions; [aspect] corrects the y axis so the circle
/// stays round on screen (the page rect is not square).
void _eraseAt(PenPoint? p) {
if (p == null) return;
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;
final pieces =
splitStrokeByCircle(stroke, p.x, p.y, radius, aspect: aspect);
// Defensive no-op guard (strokeHit already passed, so a hit is expected).
if (pieces.length == 1 && identical(pieces.first, stroke)) return;
widget.onEraseStroke(i, pieces);
return;
}
}
// --- Listener callbacks ---------------------------------------------------
/// Highest NORMALIZED pressure seen since the diagnostic was last reset —
/// makes "does pressure actually vary?" unambiguous on the readout.
double _peakNorm = 0;
void _emitPenDebug(PointerEvent event) {
final cb = widget.onPenDebug;
if (cb == null) return;
final norm = _normalizedPressure(event);
if (norm != null && norm > _peakNorm) _peakNorm = norm;
cb('${event.kind.name} raw=${event.pressure.toStringAsFixed(1)}'
'/${event.pressureMax.toStringAsFixed(0)} '
'norm=${norm?.toStringAsFixed(3) ?? "null"} '
'peak=${_peakNorm.toStringAsFixed(3)} '
'btn=${event.buttons} tilt=${event.tilt.toStringAsFixed(2)}');
}
void _onPointerHover(PointerHoverEvent event) {
if (_isStylus(event.kind)) {
_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;
if (_isStylus(event.kind)) {
_emitPenDebug(event);
// Fire edge-triggered button actions for a direct pen-down (no prior
// hover); the native observer latched this contact's flags before Flutter
// synthesized this event (plan M1/M2).
_dispatchHwButtonActions();
}
_activePointers[event.pointer] = event.kind;
// A 2nd pointer arriving during a stroke = pinch/palm → cancel the stroke
// and let the InteractiveViewer take over pan/zoom.
if (_activePointers.length >= 2) {
if (_drawPointer != null) _cancelStroke();
return;
}
// Single pointer: decide draw vs pan. Eraser is on if this stylus down
// signals it (barrel button / inverted), or hover already flagged it.
if (_isStylus(event.kind)) {
_eraserActive = _eraserActive || _isEraserSignal(event);
} else {
_eraserActive = false;
}
if (_shouldDraw(event.kind)) {
_startStroke(event);
}
}
void _onPointerMove(PointerMoveEvent event) {
if (_isStylus(event.kind)) _emitPenDebug(event);
if (event.pointer != _drawPointer) return;
if (_activePointers.length >= 2) return; // pinch owns it
_extendStroke(event);
}
void _onPointerUp(PointerUpEvent event) {
final wasDrawer = event.pointer == _drawPointer;
_activePointers.remove(event.pointer);
if (wasDrawer) _endStroke();
}
void _onPointerCancel(PointerCancelEvent event) {
final wasDrawer = event.pointer == _drawPointer;
_activePointers.remove(event.pointer);
if (wasDrawer) _cancelStroke();
}
@override
Widget build(BuildContext context) {
// The PEN never reaches PenInteractiveViewer's recognizer (it excludes
// stylus), so a stylus stroke can never be stolen as a pan. panEnabled only
// governs touch/mouse: suppress pan while a single-finger / mouse stroke is
// in progress (finger-drawing mode); a 2nd pointer cancels the stroke first
// so a pinch re-enables pan/zoom immediately.
final panEnabled = _drawPointer == null;
return Listener(
onPointerHover: _onPointerHover,
onPointerDown: _onPointerDown,
onPointerMove: _onPointerMove,
onPointerUp: _onPointerUp,
onPointerCancel: _onPointerCancel,
child: PenInteractiveViewer(
transformationController: widget.transformationController,
minScale: widget.minScale,
maxScale: widget.maxScale,
panEnabled: panEnabled,
scaleEnabled: true,
child: SizedBox(
width: widget.pageSize.width,
height: widget.pageSize.height,
child: Stack(
children: [
// PDF page bitmap. Wrapped in its own RepaintBoundary (W2) so the
// per-move live-ink repaints and the static-ink repaints never
// mark the page's raster layer dirty — isolating it from
// ink-driven repaints. (The definitive crisp-on-zoom / no-flash
// fix is the P0.5 page_tile DPI-on-settle double-buffer; this
// boundary is the safe, non-regressive interim per plan M3.)
Positioned.fill(
child: RepaintBoundary(child: widget.pageWidget),
),
// Committed ink (static layer, isolated repaint).
Positioned.fill(
child: RepaintBoundary(
child: CustomPaint(
painter: StaticInkPainter(
strokes: widget.strokes,
pageSize: widget.pageSize,
thinning: widget.thinning,
),
),
),
),
// 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(
child: CustomPaint(
painter: LiveInkPainter(
stroke: _liveStroke,
pageSize: widget.pageSize,
thinning: widget.thinning,
),
),
),
),
],
),
),
),
);
}
}