feat(pen): pressure-responsive width, configurable thinning, native Windows pen (tilt/buttons)
All checks were successful
CI / Windows build (push) Successful in 11m34s

W1 — Custom pen width + pressure sensitivity (Saber-style):
- Root cause of "压感没用": perfect_freehand 1.0.4 IGNORES real stylus pressure
  (hardcodes radius=size/2 when simulatePressure=false) — width never tracked pen
  force. Upgraded perfect_freehand ^1.0.0 -> ^2.0.0 (honors real pressure); migrated
  all 5 getStroke call sites to the 2.x API (PointVector / StrokeOptions / Offset).
- De-hardcoded `thinning` into `kDefaultPenThinning` (0.85), single source shared by
  the on-screen painter and the PDF export path; exposed as PenConfig.pressureSensitivity
  with a Pressure Sensitivity slider; live-applies via a config listener.

W3 — Native Windows pen plugin (tilt + barrel/eraser buttons):
- windows/runner/pen_channel.{h,cpp}: observe WM_POINTER at the TOP of MessageHandler
  (before HandleTopLevelWindowProc, which Flutter uses to consume pen events), read
  GetPointerPenInfo penFlags + tilt, stream over EventChannel('badnote/pen'); non-consuming.
- PenInputService: single latched hardware state (no Win32-pointerId<->event.pointer
  correlation); graceful no-op off-Windows.
- pen_canvas maps barrel/inverted/eraser through PenConfig.sideButton/eraserEnd
  (eraser/undo/toggleTool/pan) and captures tilt into PenPoint.tilt -> EditorPoint.tilt.

W2 — Zoom flicker: page raster isolated in its own RepaintBoundary (safe interim);
definitive crisp-on-zoom fix gated on the on-device root-cause probe (plan M3).

Plans: ralplan-consensus plan at docs/plans/2026-06-22-badnote-pen-polish.md
(Architect APPROVE-WITH-MUST-FIX M1-M4 + Critic ITERATE->APPROVE).

Tests: 58/58 pass incl. shared-thinning invariant + thinning-affects-outline +
tilt-adapter round-trip. flutter analyze clean; linux debug build OK.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-22 02:10:05 +08:00
parent e4a94d00c0
commit 3295018ee3
22 changed files with 1280 additions and 93 deletions

View File

@@ -10,6 +10,7 @@ add_executable(${BINARY_NAME} WIN32
"flutter_window.cpp"
"main.cpp"
"ocr_channel.cpp"
"pen_channel.cpp"
"utils.cpp"
"win32_window.cpp"
"${FLUTTER_MANAGED_DIR}/generated_plugin_registrant.cc"

View File

@@ -4,6 +4,7 @@
#include "flutter/generated_plugin_registrant.h"
#include "ocr_channel.h"
#include "pen_channel.h"
FlutterWindow::FlutterWindow(const flutter::DartProject& project)
: project_(project) {}
@@ -27,6 +28,7 @@ bool FlutterWindow::OnCreate() {
}
RegisterPlugins(flutter_controller_->engine());
RegisterOcrChannel(flutter_controller_->engine());
RegisterPenChannel(flutter_controller_->engine());
SetChildContent(flutter_controller_->view()->GetNativeWindow());
flutter_controller_->engine()->SetNextFrameCallback([&]() {
@@ -53,6 +55,9 @@ LRESULT
FlutterWindow::MessageHandler(HWND hwnd, UINT const message,
WPARAM const wparam,
LPARAM const lparam) noexcept {
// Observe pen messages before Flutter consumes WM_POINTER events.
ObservePenMessage(message, wparam, lparam);
// Give Flutter, including plugins, an opportunity to handle window messages.
if (flutter_controller_) {
std::optional<LRESULT> result =

View File

@@ -0,0 +1,89 @@
#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;
} // 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 (message != WM_POINTERENTER && message != WM_POINTERDOWN &&
message != WM_POINTERUPDATE && message != WM_POINTERUP) {
return;
}
if (!g_pen_sink) {
return;
}
UINT32 pointerId = GET_POINTERID_WPARAM(wparam);
POINTER_INPUT_TYPE type = PT_POINTER;
if (!GetPointerType(pointerId, &type) || type != PT_PEN) {
return;
}
POINTER_PEN_INFO ppi{};
if (!GetPointerPenInfo(pointerId, &ppi)) {
return;
}
int flags = 0;
if (ppi.penFlags & PEN_FLAG_BARREL) flags |= 1;
if (ppi.penFlags & PEN_FLAG_INVERTED) flags |= 2;
if (ppi.penFlags & PEN_FLAG_ERASER) flags |= 4;
flutter::EncodableMap payload{
{flutter::EncodableValue("flags"), flutter::EncodableValue(flags)},
{flutter::EncodableValue("tiltX"), flutter::EncodableValue(static_cast<double>(ppi.tiltX))},
{flutter::EncodableValue("tiltY"), flutter::EncodableValue(static_cast<double>(ppi.tiltY))},
};
g_pen_sink->Success(flutter::EncodableValue(payload));
// On pointer up, send a cleared flags event to signal lift-off.
if (message == WM_POINTERUP) {
flutter::EncodableMap clear{
{flutter::EncodableValue("flags"), flutter::EncodableValue(0)},
{flutter::EncodableValue("tiltX"), flutter::EncodableValue(0.0)},
{flutter::EncodableValue("tiltY"), flutter::EncodableValue(0.0)},
};
g_pen_sink->Success(flutter::EncodableValue(clear));
}
}

View File

@@ -0,0 +1,13 @@
#ifndef RUNNER_PEN_CHANNEL_H_
#define RUNNER_PEN_CHANNEL_H_
#include <windows.h>
namespace flutter {
class FlutterEngine;
}
void RegisterPenChannel(flutter::FlutterEngine* engine);
void ObservePenMessage(UINT message, WPARAM wparam, LPARAM lparam);
#endif // RUNNER_PEN_CHANNEL_H_