feat(diag): full input logging + read barrel from pointerFlags; focal-jump reject
All checks were successful
CI / Windows build (push) Successful in 12m44s

Buttons (likely fix + ground truth): device diag showed ptr=12577 pen=10056 —
WM_POINTER reaches the observer and GetPointerPenInfo succeeds, so the buttons
were just read from the wrong field. Native now resolves the barrel from BOTH
penFlags(PEN_FLAG_BARREL) AND pointerInfo.pointerFlags(POINTER_FLAG_SECONDBUTTON)
— many pens use the latter. It also emits the full raw set (pointerFlags,
penFlags, penMask, ButtonChangeType, tilt) plus OR-accumulated flags so a single
session reveals exactly which field each button sets.

Comprehensive logging (per user request "用好用的log库 / 我手动开启日志再记录"):
new DiagnosticLogger emits through dart:developer log(name 'badnote.input') —
capturable via `flutter run` / DevTools / `flutter logs` — AND mirrors to a file
(path shown in the overlay) for the packaged GUI build that has no console.
Manually enabled by the toolbar diagnostic toggle; off by default. PEN lines log
on raw-field change; ZOOM lines log every scale frame + rebaselines.

Zoom: scale-only glitch rejection didn't stop the jumping, so add focal/position
glitch rejection — drop a 2-finger frame whose focal jumps >250px (a touch
misread). The full per-frame trace (raw scale, pointerCount, applied change,
focal jump, drops) is now logged so the residual cause is unambiguous.

InputDiagnostics singleton accumulates the stats; the overlay shows summary +
last trace lines + log path + reset. Removed the ad-hoc inline zoom min/max.

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-23 01:10:17 +08:00
parent ae9e070b46
commit f9ec04fe86
6 changed files with 379 additions and 55 deletions

View File

@@ -0,0 +1,90 @@
// lib/editor/canvas/input_diagnostics.dart
//
// Live zoom/pan diagnostics for the pen canvas. PenInteractiveViewer records one
// entry per scale-update frame; the editor's diagnostic overlay displays the
// accumulated summary + a rolling trace so a SINGLE device session reveals the
// nature of any "跳变" (is it a raw-scale spike, a focal/position jump, or a
// pointer-count oscillation?). All numbers reset via [reset].
import 'package:flutter/foundation.dart';
import '../input/diagnostic_logger.dart';
class InputDiagnostics extends ChangeNotifier {
InputDiagnostics._();
static final InputDiagnostics instance = InputDiagnostics._();
int frames = 0;
int scaleDropped = 0; // frames rejected as a scale glitch
int focalDropped = 0; // frames rejected as a focal/position glitch
int rebaselines = 0; // pointer-count re-baselines
int pointerCountMax = 0;
double rawScaleMin = double.infinity, rawScaleMax = 0;
double scaleMin = double.infinity, scaleMax = 0;
double maxFocalJumpPx = 0; // largest single-frame local focal delta
double maxAppliedScaleJump = 1; // largest single-frame applied scale ratio
final List<String> _trace = <String>[];
List<String> get trace => List.unmodifiable(_trace);
void recordRebaseline() {
rebaselines++;
DiagnosticLogger.instance.log('ZOOM rebaseline');
notifyListeners();
}
void recordScaleFrame({
required double rawScale,
required int pointerCount,
required double currentScale,
required double appliedChange, // 1.0 when the frame was dropped
required double focalJumpPx,
required bool scaleDrop,
required bool focalDrop,
}) {
frames++;
if (scaleDrop) scaleDropped++;
if (focalDrop) focalDropped++;
if (rawScale < rawScaleMin) rawScaleMin = rawScale;
if (rawScale > rawScaleMax) rawScaleMax = rawScale;
final double resulting = currentScale * appliedChange;
if (resulting < scaleMin) scaleMin = resulting;
if (resulting > scaleMax) scaleMax = resulting;
if (pointerCount > pointerCountMax) pointerCountMax = pointerCount;
if (focalJumpPx > maxFocalJumpPx) maxFocalJumpPx = focalJumpPx;
final double jump = appliedChange >= 1 ? appliedChange : 1 / appliedChange;
if (jump > maxAppliedScaleJump) maxAppliedScaleJump = jump;
final String line = 'p$pointerCount raw=${rawScale.toStringAsFixed(3)} '
'ch=${appliedChange.toStringAsFixed(3)} '
'cur=${currentScale.toStringAsFixed(3)} '
'fj=${focalJumpPx.toStringAsFixed(0)}'
'${scaleDrop ? " SDROP" : ""}${focalDrop ? " FDROP" : ""}';
_trace.add(line);
if (_trace.length > 24) _trace.removeAt(0);
DiagnosticLogger.instance.log('ZOOM $line');
notifyListeners();
}
void reset() {
frames = scaleDropped = focalDropped = rebaselines = pointerCountMax = 0;
rawScaleMin = scaleMin = double.infinity;
rawScaleMax = scaleMax = 0;
maxFocalJumpPx = 0;
maxAppliedScaleJump = 1;
_trace.clear();
notifyListeners();
}
String _f(double v) => v.isFinite ? v.toStringAsFixed(2) : '-';
String summary() {
if (frames == 0) return 'zoom: (pinch to record)';
return 'zoom f=$frames sDrop=$scaleDropped fDrop=$focalDropped '
'rebase=$rebaselines pMax=$pointerCountMax\n'
' raw=${_f(rawScaleMin)}..${_f(rawScaleMax)} '
'scale=${_f(scaleMin)}..${_f(scaleMax)}\n'
' maxFocalJump=${maxFocalJumpPx.toStringAsFixed(0)}px '
'maxScaleJump=${_f(maxAppliedScaleJump)}';
}
}

View File

@@ -12,12 +12,14 @@ import '../../services/database_service.dart';
import '../engine/stroke_geometry.dart' show kDefaultPenThinning;
import '../engine/stroke_model.dart';
import '../engine/undo_stack.dart';
import '../input/diagnostic_logger.dart';
import '../input/pen_config.dart';
import '../input/pen_input_service.dart';
import '../persistence/editor_repository.dart';
import '../persistence/save_scheduler.dart';
import '../ui/pen_settings_page.dart';
import '../ui/thumbnail_grid.dart';
import 'input_diagnostics.dart';
import 'pen_canvas.dart';
import 'pen_stroke.dart';
@@ -95,13 +97,6 @@ class _PenEditorScreenState extends State<PenEditorScreen> {
String _penDebug = '';
bool _showPenDebug = false;
/// Live zoom-scale diagnostic (only tracked while the overlay is on), so we
/// can capture the value the pinch flash jumps to. `_zoomMin/_zoomMax` record
/// the extremes seen since the overlay was last enabled.
double _zoomNow = 1.0;
double _zoomMin = double.infinity;
double _zoomMax = 0.0;
// Tool state.
CanvasTool _tool = CanvasTool.pen;
Color _color = Colors.black;
@@ -126,23 +121,11 @@ class _PenEditorScreenState extends State<PenEditorScreen> {
// Begin listening to the native Windows pen plugin (barrel/eraser/tilt).
// No-op on platforms without the plugin (W3).
PenInputService.instance.start();
_transform.addListener(_onTransformDebug);
_initPersistence();
_initPenConfig();
_open();
}
/// Track the live zoom scale for the diagnostic overlay (no-op when off).
void _onTransformDebug() {
if (!_showPenDebug) return;
final s = _transform.value.getMaxScaleOnAxis();
setState(() {
_zoomNow = s;
if (s < _zoomMin) _zoomMin = s;
if (s > _zoomMax) _zoomMax = s;
});
}
Future<void> _initPenConfig() async {
final controller = await PenConfigController.load();
if (!mounted) {
@@ -240,10 +223,10 @@ class _PenEditorScreenState extends State<PenEditorScreen> {
scheduler.dispose();
}
_document?.dispose();
_transform.removeListener(_onTransformDebug);
_transform.dispose();
_penConfig?.dispose();
PenInputService.instance.stop();
DiagnosticLogger.instance.stop();
super.dispose();
}
@@ -453,18 +436,59 @@ class _PenEditorScreenState extends State<PenEditorScreen> {
child: Material(
color: Theme.of(context).colorScheme.inverseSurface,
borderRadius: BorderRadius.circular(8),
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 10, vertical: 6),
child: Text(
'${_penDebug.isEmpty ? 'hover / draw with the pen…' : _penDebug}'
'\nzoom=${_zoomNow.toStringAsFixed(2)} '
'min=${_zoomMin.isFinite ? _zoomMin.toStringAsFixed(2) : '-'} '
'max=${_zoomMax.toStringAsFixed(2)}',
style: TextStyle(
fontFamily: 'monospace',
fontSize: 12,
color: Theme.of(context).colorScheme.onInverseSurface,
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 380),
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 10, vertical: 6),
child: ListenableBuilder(
listenable: InputDiagnostics.instance,
builder: (context, _) {
final cs = Theme.of(context).colorScheme;
final d = InputDiagnostics.instance;
final tail = d.trace.length > 6
? d.trace.sublist(d.trace.length - 6)
: d.trace;
final mono = TextStyle(
fontFamily: 'monospace',
fontSize: 11,
color: cs.onInverseSurface);
final monoFaint = mono.copyWith(
fontSize: 10,
color:
cs.onInverseSurface.withValues(alpha: 0.75));
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Text(
_penDebug.isEmpty
? 'hover / draw with the pen…'
: _penDebug,
style: mono),
const SizedBox(height: 4),
Text(d.summary(), style: mono),
if (tail.isNotEmpty) ...[
const SizedBox(height: 4),
Text(tail.join('\n'), style: monoFaint),
],
const SizedBox(height: 4),
Text(
'log: ${DiagnosticLogger.instance.path ?? "(developer.log only)"}',
style: monoFaint),
Align(
alignment: Alignment.centerRight,
child: TextButton(
onPressed: () =>
InputDiagnostics.instance.reset(),
child: Text('Reset stats',
style:
TextStyle(color: cs.inversePrimary)),
),
),
],
);
},
),
),
),
@@ -621,15 +645,17 @@ class _PenEditorScreenState extends State<PenEditorScreen> {
_ToolButton(
icon: Icons.bug_report_outlined,
selected: _showPenDebug,
tooltip: 'Pen + zoom diagnostic',
onPressed: () => setState(() {
_showPenDebug = !_showPenDebug;
if (_showPenDebug) {
_zoomMin = double.infinity;
_zoomMax = 0.0;
_zoomNow = _transform.value.getMaxScaleOnAxis();
tooltip: 'Input diagnostic (writes a log file)',
onPressed: () {
final on = !_showPenDebug;
setState(() => _showPenDebug = on);
if (on) {
InputDiagnostics.instance.reset();
DiagnosticLogger.instance.start();
} else {
DiagnosticLogger.instance.stop();
}
}),
},
),
],
),

View File

@@ -31,6 +31,8 @@ import 'package:flutter/gestures.dart';
import 'package:flutter/physics.dart';
import 'package:flutter/widgets.dart';
import 'input_diagnostics.dart';
/// Devices allowed to pan/zoom. Stylus + invertedStylus are excluded so the pen
/// is owned exclusively by the drawing `Listener`.
const Set<PointerDeviceKind> _kPanZoomDevices = <PointerDeviceKind>{
@@ -46,6 +48,11 @@ const Set<PointerDeviceKind> _kPanZoomDevices = <PointerDeviceKind>{
const double _kScaleGlitchHi = 1.4;
const double _kScaleGlitchLo = 1 / _kScaleGlitchHi;
/// During a 2-finger gesture the focal point (finger midpoint) should move
/// smoothly. A single-frame local jump beyond this is a Windows touch misread,
/// and the frame is dropped (position-jump guard).
const double _kFocalGlitchPx = 250.0;
const double _kDrag = 0.0000135;
enum _GestureType { pan, scale }
@@ -162,9 +169,11 @@ class _PenInteractiveViewerState extends State<PenInteractiveViewer>
_lastPointerCount = details.pointerCount;
_scaleStart = _transformer.value.getMaxScaleOnAxis();
_referenceFocalPoint = _transformer.toScene(details.localFocalPoint);
InputDiagnostics.instance.recordRebaseline();
return;
}
final double focalJumpPx = details.focalPointDelta.distance;
final Offset focalPointScene = _transformer.toScene(details.localFocalPoint);
if (_gestureType == _GestureType.pan) {
@@ -175,16 +184,36 @@ class _PenInteractiveViewerState extends State<PenInteractiveViewer>
}
if (!_gestureIsSupported(_gestureType)) return;
// Position-jump guard: during a pinch the focal midpoint should move
// smoothly; a big single-frame jump is a touch misread → drop the frame.
final bool focalDrop =
details.pointerCount >= 2 && focalJumpPx > _kFocalGlitchPx;
void record(double appliedChange, bool scaleDrop, bool focalDropped) {
InputDiagnostics.instance.recordScaleFrame(
rawScale: details.scale,
pointerCount: details.pointerCount,
currentScale: scale,
appliedChange: appliedChange,
focalJumpPx: focalJumpPx,
scaleDrop: scaleDrop,
focalDrop: focalDropped,
);
}
switch (_gestureType!) {
case _GestureType.scale:
assert(_scaleStart != null);
final double desiredScale = _scaleStart! * details.scale;
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) {
// Drop a frame demanding an implausible per-frame scale jump (Windows
// multi-touch glitch) OR an implausible focal jump. Absolute tracking
// means the next good frame resumes from the true finger positions, so
// the spike never shows.
final bool scaleDrop =
scaleChange > _kScaleGlitchHi || scaleChange < _kScaleGlitchLo;
if (scaleDrop || focalDrop) {
record(1.0, scaleDrop, focalDrop);
return;
}
_transformer.value = _matrixScale(_transformer.value, scaleChange);
@@ -203,16 +232,23 @@ class _PenInteractiveViewerState extends State<PenInteractiveViewer>
if (_round(_referenceFocalPoint!) != _round(focalPointSceneCheck)) {
_referenceFocalPoint = focalPointSceneCheck;
}
record(scaleChange, false, false);
case _GestureType.pan:
assert(_referenceFocalPoint != null);
// Throw away near-scale frames so a stale reference can't jump the pan.
if (details.scale != 1.0) return;
if (focalDrop) {
_referenceFocalPoint = _transformer.toScene(details.localFocalPoint);
record(1.0, false, true);
return;
}
final Offset translationChange =
focalPointScene - _referenceFocalPoint!;
_transformer.value =
_matrixTranslate(_transformer.value, translationChange);
_referenceFocalPoint = _transformer.toScene(details.localFocalPoint);
record(1.0, false, false);
}
}