From 268c9a641a88fb7a1c974c356848def1e8509f88 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 5 Dec 2026 10:58:25 -0800 Subject: [PATCH 1/2] Windows: a pen or touch stroke stays down until the pen lifts With Windows Ink, winit reports a pen in contact as a touch, and egui turns it into the primary press. Since the craft-libs pin took d3a4e1592, the window host reads the mouse buttons with GetAsyncKeyState to find a release the window never received. A pen or a finger in contact is not a mouse button there, so the host released the press on the stroke's first move: a Pencil stroke drawn with a tablet pen left a mark a few pixels long. The message hook now notes each pointer in contact, from its WM_POINTERDOWN to its up, cancel, leave or capture change, and the app's window services report the primary button down while one is. A mouse message from a mouse ends every contact, so a contact whose end went to another window does not keep the primary button held. The thread's sent-message hook is installed when a pointer goes into contact, so the leave and capture change reach it without a Wintab context. craft-libs#5596 fixes the same check in the window host. When the pin takes it, ContactWindows can go back to SystemWindows. --- apps/vectorcraft/src/main.rs | 4 +- apps/vectorcraft/src/windows_stylus.rs | 151 ++++++++++++++++++++++++- apps/vectorcraft/src/windows_wintab.rs | 14 ++- 3 files changed, 159 insertions(+), 10 deletions(-) diff --git a/apps/vectorcraft/src/main.rs b/apps/vectorcraft/src/main.rs index 63135d00e5..cdbf2a0173 100644 --- a/apps/vectorcraft/src/main.rs +++ b/apps/vectorcraft/src/main.rs @@ -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}; @@ -885,7 +884,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 = InstallSpec::new(shell_info, Arc::new(SystemWindows)) + // A pen or a finger in contact counts as the primary button (`windows_stylus`). + let spec = InstallSpec::new(shell_info, Arc::new(windows_stylus::ContactWindows)) .app_id(vectorcraft_ui_egui::os_window::app_id()) .placement(Arc::new(Mutex::new(placement))) .frame(decision); diff --git a/apps/vectorcraft/src/windows_stylus.rs b/apps/vectorcraft/src/windows_stylus.rs index b8e6b88ee1..cbeca212ae 100644 --- a/apps/vectorcraft/src/windows_stylus.rs +++ b/apps/vectorcraft/src/windows_stylus.rs @@ -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,82 @@ thread_local! { /// The pen source [`msg_hook`] writes, once [`start`] has made it. static FEED: OnceLock = OnceLock::new(); +/// The ids of the pointers in contact (pens and fingers), from [`msg_hook`]. +static CONTACTS: Mutex> = 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 { + 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 { + SystemWindows.command_held() + } + + fn warp_pointer(&self, to: [f32; 2]) -> bool { + SystemWindows.warp_pointer(to) + } + + fn monitors(&self) -> Vec { + 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, in_contact: bool) -> Option { + 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. +/// Returns whether the message put a pointer in contact. +fn follow_contact(contacts: &mut Vec, message: u32, wparam: usize, from_mouse: impl FnOnce() -> bool) -> bool { + match contact_update(message, wparam) { + Some((id, true)) if !contacts.contains(&id) => { + 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 +186,13 @@ pub fn msg_hook(msg: *const std::ffi::c_void) -> bool { let Some(msg) = (unsafe { msg.cast::().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); + let touched = follow_contact(&mut CONTACTS.lock().unwrap_or_else(PoisonError::into_inner), msg.message, msg.wParam.0, from_mouse); + // The contact's sent leave and capture change come through the thread's hook. + if touched { + crate::windows_wintab::hook_sent_messages(msg.time); + } let Some(pen) = FEED.get() else { return false; }; @@ -119,8 +213,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); @@ -187,9 +279,13 @@ fn sent_forgets_wheel(message: u32, wparam: usize, pen: Option) -> 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); + } if sent_forgets_wheel(message, wparam, WHEEL_PEN.get()) { crate::windows_wintab::forget(); } @@ -328,6 +424,51 @@ 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 mut contacts = vec![]; + follow_contact(&mut contacts, WM_POINTERUPDATE, flags(POINTER_MESSAGE_FLAG_INRANGE), no_mouse); + assert!(contacts.is_empty(), "a hovering pen is in no contact"); + assert!(follow_contact(&mut contacts, WM_POINTERDOWN, touching, no_mouse), "the pen goes into contact"); + assert_eq!(contacts, [7], "the pen touches down"); + assert!(!follow_contact(&mut contacts, WM_POINTERUPDATE, touching, no_mouse), "it is already in contact"); + assert_eq!(contacts, [7], "and stays in contact while it moves"); + follow_contact(&mut contacts, WM_POINTERDOWN, (POINTER_MESSAGE_FLAG_INCONTACT << 16) | 9, no_mouse); + follow_contact(&mut contacts, WM_POINTERUP, flags(POINTER_MESSAGE_FLAG_INRANGE), no_mouse); + assert_eq!(contacts, [9], "the pen goes up and a finger stays down"); + follow_contact(&mut contacts, WM_POINTERUPDATE, ((POINTER_MESSAGE_FLAG_INCONTACT | POINTER_MESSAGE_FLAG_CANCELED) << 16) | 9, no_mouse); + assert!(contacts.is_empty(), "a canceled contact ends"); + follow_contact(&mut contacts, WM_POINTERDOWN, touching, no_mouse); + follow_contact(&mut contacts, WM_POINTERLEAVE, 7, no_mouse); + assert!(contacts.is_empty(), "a pointer that leaves the window ends its contact"); + follow_contact(&mut contacts, WM_POINTERDOWN, touching, no_mouse); + follow_contact(&mut contacts, WM_POINTERCAPTURECHANGED, 7, no_mouse); + assert!(contacts.is_empty(), "a pointer whose capture another window took ends its contact"); + + // WM_LBUTTONDOWN. + follow_contact(&mut contacts, WM_POINTERDOWN, touching, no_mouse); + follow_contact(&mut contacts, 0x0201, 0, || false); + assert_eq!(contacts, [7], "a mouse message Windows made from the pen keeps the contact"); + follow_contact(&mut contacts, 0x0201, 0, || true); + assert!(contacts.is_empty(), "a press of the mouse ends a contact whose end never arrived"); + // WM_KEYDOWN. + follow_contact(&mut contacts, WM_POINTERDOWN, touching, no_mouse); + follow_contact(&mut contacts, 0x0100, 0, || -> bool { panic!("not a mouse message") }); + assert_eq!(contacts, [7]); + } + #[test] fn mouse_messages_from_a_pen_or_a_touch_are_told_apart() { assert!(is_mouse(0), "a mouse"); diff --git a/apps/vectorcraft/src/windows_wintab.rs b/apps/vectorcraft/src/windows_wintab.rs index 31da32a1ff..50ccff2af8 100644 --- a/apps/vectorcraft/src/windows_wintab.rs +++ b/apps/vectorcraft/src/windows_wintab.rs @@ -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, ) - /// closes the context ([`deactivate`]), and a pen's `WM_POINTERENTER` and `WM_POINTERLEAVE` - /// () go to + /// closes the context ([`deactivate`]), and a pointer's `WM_POINTERENTER`, `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 (). 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 { From 3552f83b32879a945b8bff2206a956c2fd6cb87d Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 5 Dec 2026 12:02:44 -0800 Subject: [PATCH 2/2] Windows: note a pen contact only once its leave can arrive When the thread's sent-message hook failed to install, the message hook still noted the pointer in contact. Its WM_POINTERLEAVE and WM_POINTERCAPTURECHANGED are sent, so they never reached the contact list, and ContactWindows kept the primary button held until a mouse message from a mouse cleared it. follow_contact now adds a pointer only when the hook is installed. When the install fails, the contact is not noted, and the pointer's next in-contact message tries the install again. --- apps/vectorcraft/src/windows_stylus.rs | 69 +++++++++++++++++--------- 1 file changed, 45 insertions(+), 24 deletions(-) diff --git a/apps/vectorcraft/src/windows_stylus.rs b/apps/vectorcraft/src/windows_stylus.rs index cbeca212ae..8b0a0a7e2a 100644 --- a/apps/vectorcraft/src/windows_stylus.rs +++ b/apps/vectorcraft/src/windows_stylus.rs @@ -154,10 +154,12 @@ fn contact_update(message: u32, wparam: usize) -> Option<(u32, bool)> { /// 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. -/// Returns whether the message put a pointer in contact. -fn follow_contact(contacts: &mut Vec, message: u32, wparam: usize, from_mouse: impl FnOnce() -> bool) -> bool { +/// 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, 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) => { + Some((id, true)) if !contacts.contains(&id) && watched() => { contacts.push(id); return true; } @@ -188,11 +190,10 @@ pub fn msg_hook(msg: *const std::ffi::c_void) -> bool { }; // SAFETY: `GetMessageExtraInfo` reads the extra information of the thread's last message. let from_mouse = || is_mouse(unsafe { GetMessageExtraInfo() }.0 as u32); - let touched = follow_contact(&mut CONTACTS.lock().unwrap_or_else(PoisonError::into_inner), msg.message, msg.wParam.0, from_mouse); - // The contact's sent leave and capture change come through the thread's hook. - if touched { - crate::windows_wintab::hook_sent_messages(msg.time); - } + // 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; }; @@ -284,7 +285,7 @@ fn sent_forgets_wheel(message: u32, wparam: usize, pen: Option) -> bool { #[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); + 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(); @@ -438,37 +439,57 @@ mod tests { 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, message: u32, wparam: usize| follow_contact(contacts, message, wparam, no_mouse, || true); let mut contacts = vec![]; - follow_contact(&mut contacts, WM_POINTERUPDATE, flags(POINTER_MESSAGE_FLAG_INRANGE), no_mouse); + follow(&mut contacts, WM_POINTERUPDATE, flags(POINTER_MESSAGE_FLAG_INRANGE)); assert!(contacts.is_empty(), "a hovering pen is in no contact"); - assert!(follow_contact(&mut contacts, WM_POINTERDOWN, touching, no_mouse), "the pen goes into contact"); + assert!(follow(&mut contacts, WM_POINTERDOWN, touching), "the pen goes into contact"); assert_eq!(contacts, [7], "the pen touches down"); - assert!(!follow_contact(&mut contacts, WM_POINTERUPDATE, touching, no_mouse), "it is already in contact"); + assert!(!follow(&mut contacts, WM_POINTERUPDATE, touching), "it is already in contact"); assert_eq!(contacts, [7], "and stays in contact while it moves"); - follow_contact(&mut contacts, WM_POINTERDOWN, (POINTER_MESSAGE_FLAG_INCONTACT << 16) | 9, no_mouse); - follow_contact(&mut contacts, WM_POINTERUP, flags(POINTER_MESSAGE_FLAG_INRANGE), no_mouse); + 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_contact(&mut contacts, WM_POINTERUPDATE, ((POINTER_MESSAGE_FLAG_INCONTACT | POINTER_MESSAGE_FLAG_CANCELED) << 16) | 9, no_mouse); + follow(&mut contacts, WM_POINTERUPDATE, ((POINTER_MESSAGE_FLAG_INCONTACT | POINTER_MESSAGE_FLAG_CANCELED) << 16) | 9); assert!(contacts.is_empty(), "a canceled contact ends"); - follow_contact(&mut contacts, WM_POINTERDOWN, touching, no_mouse); - follow_contact(&mut contacts, WM_POINTERLEAVE, 7, no_mouse); + 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_contact(&mut contacts, WM_POINTERDOWN, touching, no_mouse); - follow_contact(&mut contacts, WM_POINTERCAPTURECHANGED, 7, no_mouse); + 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_contact(&mut contacts, WM_POINTERDOWN, touching, no_mouse); - follow_contact(&mut contacts, 0x0201, 0, || false); + 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); + 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_contact(&mut contacts, WM_POINTERDOWN, touching, no_mouse); - follow_contact(&mut contacts, 0x0100, 0, || -> bool { panic!("not a mouse message") }); + 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");