mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-05 21:42:59 -05:00
Merge pull request #10977 from dotCooCoo/fix/tablet-pen-stroke-held
Windows: a pen or touch stroke stays down until the pen lifts
This commit is contained in:
3 files changed
+180
-10
No files matched your search
@@ -91,7 +91,6 @@ use craft::ui::shell::embed;
|
||||
use craft::window::Prefix;
|
||||
use craft::window::policy::{TitleBarPref, decide_for};
|
||||
use craft::window::restore::Store;
|
||||
use craft::window::system::SystemWindows;
|
||||
use vectorcraft_engine::Session;
|
||||
use vectorcraft_engine::cmd::fileio;
|
||||
use vectorcraft_engine::file_access::{self, AutomationRoots};
|
||||
@@ -896,7 +895,8 @@ fn main() -> std::process::ExitCode {
|
||||
if let Some(window) = &winit_window {
|
||||
restore_main_window(window, &placement, settings.lock().get().ui.legacy_window, wayland);
|
||||
}
|
||||
let spec = main_window_spec(shell_info, Arc::new(SystemWindows), host_services::open_link)
|
||||
// A pen or a finger in contact counts as the primary button (`windows_stylus`).
|
||||
let spec = main_window_spec(shell_info, Arc::new(windows_stylus::ContactWindows), host_services::open_link)
|
||||
.placement(Arc::new(Mutex::new(placement)))
|
||||
.frame(decision);
|
||||
let spec = match start_pen(&winit_window) {
|
||||
|
||||
@@ -28,13 +28,20 @@
|
||||
//! converted to radians. `POINTER_PEN_INFO` has no field for an airbrush pen's finger wheel, which
|
||||
//! comes from the tablet driver's Wintab interface instead, when it has one
|
||||
//! ([`crate::windows_wintab`]).
|
||||
//!
|
||||
//! The window host reads the mouse buttons Windows reports down ([`ContactWindows`]) to release a
|
||||
//! press whose release the window never received. A pen or a finger in contact is not a mouse
|
||||
//! button there, so [`msg_hook`] also notes each pointer in contact, and [`ContactWindows`] reports
|
||||
//! the primary button down while one is. Otherwise the press of a pen stroke is released on its
|
||||
//! first move.
|
||||
#![cfg_attr(not(windows), allow(dead_code))]
|
||||
|
||||
use std::cell::Cell;
|
||||
use std::sync::OnceLock;
|
||||
use std::sync::{Mutex, OnceLock, PoisonError};
|
||||
|
||||
use craft::behavior::PointerSample;
|
||||
use craft::behavior::{Monitor, PointerSample};
|
||||
use craft::ui::runtime::pen::PenSource;
|
||||
use craft::window::system::{HeldButtons, SystemWindows, WindowSystem};
|
||||
|
||||
/// `POINTER_MESSAGE_FLAG_SECONDBUTTON` (`winuser.h`): in the high word of a pointer message's
|
||||
/// `wParam`, a pen in contact with its barrel button pressed.
|
||||
@@ -44,6 +51,8 @@ const POINTER_MESSAGE_FLAG_SECONDBUTTON: usize = 0x0020;
|
||||
const POINTER_MESSAGE_FLAG_INRANGE: usize = 0x0002;
|
||||
/// `POINTER_MESSAGE_FLAG_CANCELED` (`winuser.h`): the pointer's input was canceled.
|
||||
const POINTER_MESSAGE_FLAG_CANCELED: usize = 0x8000;
|
||||
/// `POINTER_MESSAGE_FLAG_INCONTACT` (`winuser.h`): a pen or a finger touching the surface.
|
||||
const POINTER_MESSAGE_FLAG_INCONTACT: usize = 0x0004;
|
||||
/// `PEN_FLAG_INVERTED` and `PEN_FLAG_ERASER` (`winuser.h`), in `POINTER_PEN_INFO::penFlags`.
|
||||
const PEN_FLAG_INVERTED: u32 = 0x0002;
|
||||
const PEN_FLAG_ERASER: u32 = 0x0004;
|
||||
@@ -67,6 +76,8 @@ const WM_POINTERDOWN: u32 = 0x0246;
|
||||
const WM_POINTERUP: u32 = 0x0247;
|
||||
const WM_POINTERENTER: u32 = 0x0249;
|
||||
const WM_POINTERLEAVE: u32 = 0x024A;
|
||||
/// `WM_POINTERCAPTURECHANGED` (`winuser.h`): another window took the pointer's capture.
|
||||
const WM_POINTERCAPTURECHANGED: u32 = 0x024C;
|
||||
|
||||
/// A pen's tilt along x and y and its barrel rotation, in radians.
|
||||
type Pose = ([f32; 2], f32);
|
||||
@@ -81,6 +92,84 @@ thread_local! {
|
||||
/// The pen source [`msg_hook`] writes, once [`start`] has made it.
|
||||
static FEED: OnceLock<PenSource> = OnceLock::new();
|
||||
|
||||
/// The ids of the pointers in contact (pens and fingers), from [`msg_hook`].
|
||||
static CONTACTS: Mutex<Vec<u32>> = Mutex::new(Vec::new());
|
||||
|
||||
/// The running platform's window services ([`SystemWindows`]), with the primary button reported
|
||||
/// down while a pen or a finger is in contact.
|
||||
pub struct ContactWindows;
|
||||
|
||||
impl WindowSystem for ContactWindows {
|
||||
fn desktop_pointer(&self) -> Option<[f32; 2]> {
|
||||
SystemWindows.desktop_pointer()
|
||||
}
|
||||
|
||||
fn buttons_held(&self) -> Option<HeldButtons> {
|
||||
let in_contact = !CONTACTS.lock().unwrap_or_else(PoisonError::into_inner).is_empty();
|
||||
with_contact(SystemWindows.buttons_held(), in_contact)
|
||||
}
|
||||
|
||||
fn discard_marked_text(&self) -> bool {
|
||||
SystemWindows.discard_marked_text()
|
||||
}
|
||||
|
||||
fn command_held(&self) -> Option<bool> {
|
||||
SystemWindows.command_held()
|
||||
}
|
||||
|
||||
fn warp_pointer(&self, to: [f32; 2]) -> bool {
|
||||
SystemWindows.warp_pointer(to)
|
||||
}
|
||||
|
||||
fn monitors(&self) -> Vec<Monitor> {
|
||||
SystemWindows.monitors()
|
||||
}
|
||||
}
|
||||
|
||||
/// The buttons `held` with the primary one added while a pointer is `in_contact`; `None` where the
|
||||
/// platform tells nothing.
|
||||
fn with_contact(held: Option<HeldButtons>, in_contact: bool) -> Option<HeldButtons> {
|
||||
held.map(|mut held| {
|
||||
if in_contact {
|
||||
held.insert(HeldButtons::PRIMARY);
|
||||
}
|
||||
held
|
||||
})
|
||||
}
|
||||
|
||||
/// The pointer a message with this `wParam` is about, and whether it is now in contact: a pointer
|
||||
/// that goes down or moves with `POINTER_MESSAGE_FLAG_INCONTACT` and no cancel is; one that hovers,
|
||||
/// is canceled, goes up, leaves or loses its capture to another window is not. `None` for any other
|
||||
/// message.
|
||||
fn contact_update(message: u32, wparam: usize) -> Option<(u32, bool)> {
|
||||
let id = (wparam & 0xffff) as u32;
|
||||
let flags = wparam >> 16;
|
||||
match message {
|
||||
WM_POINTERDOWN | WM_POINTERUPDATE => Some((id, flags & POINTER_MESSAGE_FLAG_INCONTACT != 0 && flags & POINTER_MESSAGE_FLAG_CANCELED == 0)),
|
||||
WM_POINTERUP | WM_POINTERLEAVE | WM_POINTERCAPTURECHANGED => Some((id, false)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Notes in `contacts` the pointer that a message with this `wParam` puts in or out of contact
|
||||
/// ([`contact_update`]). A mouse message that a mouse made (`from_mouse`) ends every contact, so a
|
||||
/// contact whose end went to another window does not hold the primary button for the mouse.
|
||||
/// A pointer goes into contact only when `watched` returns true: the thread's hook that sees its
|
||||
/// sent leave and capture change is installed. Otherwise a later message of the contact tries
|
||||
/// again. Returns whether the message put a pointer in contact.
|
||||
fn follow_contact(contacts: &mut Vec<u32>, message: u32, wparam: usize, from_mouse: impl FnOnce() -> bool, watched: impl FnOnce() -> bool) -> bool {
|
||||
match contact_update(message, wparam) {
|
||||
Some((id, true)) if !contacts.contains(&id) && watched() => {
|
||||
contacts.push(id);
|
||||
return true;
|
||||
}
|
||||
Some((id, false)) => contacts.retain(|c| *c != id),
|
||||
None if MOUSE_MESSAGES.contains(&message) && !contacts.is_empty() && from_mouse() => contacts.clear(),
|
||||
_ => {}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
/// The canvas's pen source, which [`msg_hook`] writes before winit handles each message.
|
||||
pub fn start() -> PenSource {
|
||||
FEED.get_or_init(PenSource::default).clone()
|
||||
@@ -99,6 +188,12 @@ pub fn msg_hook(msg: *const std::ffi::c_void) -> bool {
|
||||
let Some(msg) = (unsafe { msg.cast::<MSG>().as_ref() }) else {
|
||||
return false;
|
||||
};
|
||||
// SAFETY: `GetMessageExtraInfo` reads the extra information of the thread's last message.
|
||||
let from_mouse = || is_mouse(unsafe { GetMessageExtraInfo() }.0 as u32);
|
||||
// The contact's sent leave and capture change come through the thread's hook, so a contact is
|
||||
// noted only once the hook is installed.
|
||||
let watched = || crate::windows_wintab::hook_sent_messages(msg.time);
|
||||
follow_contact(&mut CONTACTS.lock().unwrap_or_else(PoisonError::into_inner), msg.message, msg.wParam.0, from_mouse, watched);
|
||||
let Some(pen) = FEED.get() else {
|
||||
return false;
|
||||
};
|
||||
@@ -119,8 +214,6 @@ pub fn msg_hook(msg: *const std::ffi::c_void) -> bool {
|
||||
let pose = pen_pose(info.penMask, info.tiltX, info.tiltY, info.rotation);
|
||||
Some(pen_sample(info.pressure, info.penMask, info.penFlags, in_contact, pose, wheel))
|
||||
};
|
||||
// SAFETY: `GetMessageExtraInfo` reads the extra information of the thread's last message.
|
||||
let from_mouse = || is_mouse(unsafe { GetMessageExtraInfo() }.0 as u32);
|
||||
if let Some(sample) = feed_update(msg.message, msg.wParam.0, read_pen, from_mouse) {
|
||||
// The barrel goes first: the contact's press reads it with the sample.
|
||||
pen.set_barrel(sample.is_some() && (msg.wParam.0 >> 16) & POINTER_MESSAGE_FLAG_SECONDBUTTON != 0);
|
||||
@@ -188,9 +281,13 @@ fn sent_forgets_wheel(message: u32, wparam: usize, pen: Option<u32>) -> bool {
|
||||
|
||||
/// A message Windows sent to a window procedure of the thread, from the Wintab reader's hook on
|
||||
/// them ([`crate::windows_wintab`]): a pen leaving the window starts the wheel readings afresh, as
|
||||
/// [`msg_hook`] does for the messages it sees.
|
||||
/// [`msg_hook`] does for the messages it sees, and a pointer that leaves it or loses its capture is
|
||||
/// no longer in contact.
|
||||
#[cfg(windows)]
|
||||
pub fn sent_message(message: u32, wparam: usize) {
|
||||
if matches!(message, WM_POINTERLEAVE | WM_POINTERCAPTURECHANGED) {
|
||||
follow_contact(&mut CONTACTS.lock().unwrap_or_else(PoisonError::into_inner), message, wparam, || false, || true);
|
||||
}
|
||||
if sent_forgets_wheel(message, wparam, WHEEL_PEN.get()) {
|
||||
crate::windows_wintab::forget();
|
||||
}
|
||||
@@ -334,6 +431,71 @@ mod tests {
|
||||
assert_eq!(pen_sample(2000, PEN_MASK_PRESSURE, 0, false, UPRIGHT, None).pressure, 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_pointer_in_contact_holds_the_primary_button() {
|
||||
assert_eq!(with_contact(Some(HeldButtons::NONE), true), Some(HeldButtons::PRIMARY), "a pen stroke while no mouse button is down");
|
||||
assert_eq!(with_contact(Some(HeldButtons::NONE), false), Some(HeldButtons::NONE));
|
||||
let held = with_contact(Some(HeldButtons::SECONDARY), true).unwrap_or_default();
|
||||
assert!(held.contains(HeldButtons::PRIMARY) && held.contains(HeldButtons::SECONDARY), "the mouse's own buttons stay");
|
||||
assert_eq!(with_contact(None, true), None, "a platform that tells nothing still tells nothing");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_pen_stroke_is_in_contact_from_its_down_to_its_up() {
|
||||
let flags = |f: usize| (f << 16) | 7;
|
||||
let touching = flags(POINTER_MESSAGE_FLAG_INRANGE | POINTER_MESSAGE_FLAG_INCONTACT);
|
||||
let no_mouse = || -> bool { panic!("a pointer message is not a mouse message") };
|
||||
let follow = |contacts: &mut Vec<u32>, message: u32, wparam: usize| follow_contact(contacts, message, wparam, no_mouse, || true);
|
||||
let mut contacts = vec![];
|
||||
follow(&mut contacts, WM_POINTERUPDATE, flags(POINTER_MESSAGE_FLAG_INRANGE));
|
||||
assert!(contacts.is_empty(), "a hovering pen is in no contact");
|
||||
assert!(follow(&mut contacts, WM_POINTERDOWN, touching), "the pen goes into contact");
|
||||
assert_eq!(contacts, [7], "the pen touches down");
|
||||
assert!(!follow(&mut contacts, WM_POINTERUPDATE, touching), "it is already in contact");
|
||||
assert_eq!(contacts, [7], "and stays in contact while it moves");
|
||||
follow(&mut contacts, WM_POINTERDOWN, (POINTER_MESSAGE_FLAG_INCONTACT << 16) | 9);
|
||||
follow(&mut contacts, WM_POINTERUP, flags(POINTER_MESSAGE_FLAG_INRANGE));
|
||||
assert_eq!(contacts, [9], "the pen goes up and a finger stays down");
|
||||
follow(&mut contacts, WM_POINTERUPDATE, ((POINTER_MESSAGE_FLAG_INCONTACT | POINTER_MESSAGE_FLAG_CANCELED) << 16) | 9);
|
||||
assert!(contacts.is_empty(), "a canceled contact ends");
|
||||
follow(&mut contacts, WM_POINTERDOWN, touching);
|
||||
follow(&mut contacts, WM_POINTERLEAVE, 7);
|
||||
assert!(contacts.is_empty(), "a pointer that leaves the window ends its contact");
|
||||
follow(&mut contacts, WM_POINTERDOWN, touching);
|
||||
follow(&mut contacts, WM_POINTERCAPTURECHANGED, 7);
|
||||
assert!(contacts.is_empty(), "a pointer whose capture another window took ends its contact");
|
||||
|
||||
// WM_LBUTTONDOWN.
|
||||
follow(&mut contacts, WM_POINTERDOWN, touching);
|
||||
follow_contact(&mut contacts, 0x0201, 0, || false, || true);
|
||||
assert_eq!(contacts, [7], "a mouse message Windows made from the pen keeps the contact");
|
||||
follow_contact(&mut contacts, 0x0201, 0, || true, || true);
|
||||
assert!(contacts.is_empty(), "a press of the mouse ends a contact whose end never arrived");
|
||||
// WM_KEYDOWN.
|
||||
follow(&mut contacts, WM_POINTERDOWN, touching);
|
||||
follow_contact(&mut contacts, 0x0100, 0, || -> bool { panic!("not a mouse message") }, || true);
|
||||
assert_eq!(contacts, [7]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_contact_is_noted_only_while_its_end_can_arrive() {
|
||||
let touching = ((POINTER_MESSAGE_FLAG_INRANGE | POINTER_MESSAGE_FLAG_INCONTACT) << 16) | 7;
|
||||
let no_mouse = || -> bool { panic!("a pointer message is not a mouse message") };
|
||||
let mut contacts = vec![];
|
||||
assert!(!follow_contact(&mut contacts, WM_POINTERDOWN, touching, no_mouse, || false), "the hook did not install");
|
||||
assert!(contacts.is_empty(), "a contact whose leave could not arrive is not noted");
|
||||
assert!(follow_contact(&mut contacts, WM_POINTERUPDATE, touching, no_mouse, || true), "the next move tries again");
|
||||
assert_eq!(contacts, [7]);
|
||||
let asked = std::cell::Cell::new(false);
|
||||
follow_contact(&mut contacts, WM_POINTERUPDATE, touching, no_mouse, || {
|
||||
asked.set(true);
|
||||
true
|
||||
});
|
||||
assert!(!asked.get(), "a pointer already in contact does not install the hook again");
|
||||
follow_contact(&mut contacts, WM_POINTERUP, 7, no_mouse, || -> bool { panic!("an up is not a contact") });
|
||||
assert!(contacts.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mouse_messages_from_a_pen_or_a_touch_are_told_apart() {
|
||||
assert!(is_mouse(0), "a mouse");
|
||||
|
||||
@@ -625,7 +625,7 @@ fn message(message: u32, wparam: usize, lparam: isize) -> Message {
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
pub use win::{forget, msg_hook, pen_wheel, set_wheel_used, wheel_pen};
|
||||
pub use win::{forget, hook_sent_messages, msg_hook, pen_wheel, set_wheel_used, wheel_pen};
|
||||
|
||||
#[cfg(windows)]
|
||||
#[allow(unsafe_code)]
|
||||
@@ -758,8 +758,9 @@ mod win {
|
||||
/// The thread's hook on the messages Windows sends to its windows' procedures, which winit's
|
||||
/// message hook does not see: another app becoming the active one (`WM_ACTIVATEAPP`, sent to a
|
||||
/// top-level window of the app, <https://learn.microsoft.com/en-us/windows/win32/winmsg/wm-activateapp>)
|
||||
/// closes the context ([`deactivate`]), and a pen's `WM_POINTERENTER` and `WM_POINTERLEAVE`
|
||||
/// (<https://learn.microsoft.com/en-us/windows/win32/inputmsg/wm-pointerleave>) go to
|
||||
/// closes the context ([`deactivate`]), and a pointer's `WM_POINTERENTER`, `WM_POINTERLEAVE`
|
||||
/// (<https://learn.microsoft.com/en-us/windows/win32/inputmsg/wm-pointerleave>) and
|
||||
/// `WM_POINTERCAPTURECHANGED` go to
|
||||
/// [`crate::windows_stylus::sent_message`]. The hook sees them whichever of the app's windows
|
||||
/// they are sent to (<https://learn.microsoft.com/en-us/windows/win32/winmsg/callwndproc>).
|
||||
unsafe extern "system" fn call_wnd_proc(code: i32, wparam: WPARAM, lparam: LPARAM) -> LRESULT {
|
||||
@@ -1089,6 +1090,13 @@ mod win {
|
||||
settle();
|
||||
}
|
||||
|
||||
/// Installs [`call_wnd_proc`] for a pointer that went into contact at the message time `time`,
|
||||
/// so the pointer's sent `WM_POINTERLEAVE` and `WM_POINTERCAPTURECHANGED` reach
|
||||
/// [`crate::windows_stylus::sent_message`] whether or not a context opens ([`hook_thread`]).
|
||||
pub fn hook_sent_messages(time: u32) -> bool {
|
||||
hook_thread(time)
|
||||
}
|
||||
|
||||
/// The pen whose leaving starts the readings afresh, for a message of the pen with the pointer
|
||||
/// id `pen` ([`Readings::owner_or`]).
|
||||
pub fn wheel_pen(pen: u32) -> u32 {
|
||||
|
||||
Reference in new issue
Block a user