Files
BadNote/windows/runner/pen_channel.cpp
Akiba So ae9e070b46
All checks were successful
CI / Windows build (push) Successful in 17m58s
fix(pen): eraser lag/stuck-red/reliability; zoom glitch-reject; native input diag
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>
2026-06-22 22:02:17 +08:00

113 lines
3.9 KiB
C++

#include "pen_channel.h"
#include <flutter/encodable_value.h>
#include <flutter/event_channel.h>
#include <flutter/event_stream_handler_functions.h>
#include <flutter/flutter_engine.h>
#include <flutter/standard_method_codec.h>
#include <memory>
namespace {
std::unique_ptr<flutter::EventSink<flutter::EncodableValue>> g_pen_sink;
std::unique_ptr<flutter::EventChannel<flutter::EncodableValue>> g_pen_channel;
// Diagnostic counters so the Dart side can see WHAT the observer receives:
// - g_ptr_msgs: WM_POINTER* messages seen (is WM_POINTER reaching us at all?)
// - g_pen_msgs: of those, PT_PEN with a successful GetPointerPenInfo
// - g_mouse_msgs: legacy mouse/touch input messages (Flutter on the old path?)
int g_ptr_msgs = 0;
int g_pen_msgs = 0;
int g_mouse_msgs = 0;
int g_last_msg = 0;
} // namespace
void RegisterPenChannel(flutter::FlutterEngine* engine) {
g_pen_channel =
std::make_unique<flutter::EventChannel<flutter::EncodableValue>>(
engine->messenger(), "badnote/pen",
&flutter::StandardMethodCodec::GetInstance());
auto handler = std::make_unique<
flutter::StreamHandlerFunctions<flutter::EncodableValue>>(
[](const flutter::EncodableValue* arguments,
std::unique_ptr<flutter::EventSink<flutter::EncodableValue>>&&
events)
-> std::unique_ptr<
flutter::StreamHandlerError<flutter::EncodableValue>> {
g_pen_sink = std::move(events);
return nullptr;
},
[](const flutter::EncodableValue* arguments)
-> std::unique_ptr<
flutter::StreamHandlerError<flutter::EncodableValue>> {
g_pen_sink = nullptr;
return nullptr;
});
g_pen_channel->SetStreamHandler(std::move(handler));
}
void ObservePenMessage(UINT message, WPARAM wparam, LPARAM lparam) {
if (!g_pen_sink) {
return;
}
const bool is_pointer =
message == WM_POINTERENTER || message == WM_POINTERDOWN ||
message == WM_POINTERUPDATE || message == WM_POINTERUP;
// Legacy input path: if Flutter is feeding us mouse/touch instead of pointer
// messages, these tell us so (so we know WM_POINTER never arrives here).
const bool is_legacy_input =
message == WM_LBUTTONDOWN || message == WM_LBUTTONUP ||
message == WM_MOUSEMOVE || message == WM_TOUCH;
if (!is_pointer && !is_legacy_input) {
return;
}
g_last_msg = static_cast<int>(message);
if (is_legacy_input) {
++g_mouse_msgs;
}
int flags = 0;
double tilt_x = 0.0;
double tilt_y = 0.0;
if (is_pointer) {
++g_ptr_msgs;
UINT32 pointerId = GET_POINTERID_WPARAM(wparam);
POINTER_INPUT_TYPE type = PT_POINTER;
if (GetPointerType(pointerId, &type) && type == PT_PEN) {
POINTER_PEN_INFO ppi{};
if (GetPointerPenInfo(pointerId, &ppi)) {
++g_pen_msgs;
if (ppi.penFlags & PEN_FLAG_BARREL) flags |= 1;
if (ppi.penFlags & PEN_FLAG_INVERTED) flags |= 2;
if (ppi.penFlags & PEN_FLAG_ERASER) flags |= 4;
tilt_x = static_cast<double>(ppi.tiltX);
tilt_y = static_cast<double>(ppi.tiltY);
}
}
if (message == WM_POINTERUP) {
flags = 0; // lift-off clears held flags
}
}
// Always emit the diagnostic counters so the Dart overlay can show whether
// WM_POINTER / PT_PEN ever reach this observer.
flutter::EncodableMap payload{
{flutter::EncodableValue("flags"), flutter::EncodableValue(flags)},
{flutter::EncodableValue("tiltX"), flutter::EncodableValue(tilt_x)},
{flutter::EncodableValue("tiltY"), flutter::EncodableValue(tilt_y)},
{flutter::EncodableValue("diagPtr"), flutter::EncodableValue(g_ptr_msgs)},
{flutter::EncodableValue("diagPen"), flutter::EncodableValue(g_pen_msgs)},
{flutter::EncodableValue("diagMouse"), flutter::EncodableValue(g_mouse_msgs)},
{flutter::EncodableValue("diagMsg"), flutter::EncodableValue(g_last_msg)},
};
g_pen_sink->Success(flutter::EncodableValue(payload));
}