Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 80 additions & 1 deletion src/platform/win/mouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ impl WmMouse for BaseviewWindow {

fn on_capture_changed(&self, window: HWnd, window_gaining_capture: Option<HWnd>) {
if window_gaining_capture != Some(window) {
// Don't release the capture here: Windows sends this message after the capture has
// already moved, so releasing it would take it away from the window that now has it.
let previous_counter = self.window_state.mouse_button_counter.replace(0);
HWnd::release_capture();

if previous_counter == 0 {
self.handle_event(Event::Mouse(MouseEvent::CursorLeft));
Expand Down Expand Up @@ -85,3 +86,81 @@ impl WmMouse for BaseviewWindow {
self.handle_event(Event::Mouse(event));
}
}

#[cfg(test)]
mod tests {
use std::sync::atomic::{AtomicIsize, Ordering};
use std::sync::Arc;

use raw_window_handle::{HasWindowHandle, RawWindowHandle};
use windows_sys::Win32::Foundation::{HWND, WPARAM};
use windows_sys::Win32::System::SystemServices::MK_LBUTTON;
use windows_sys::Win32::UI::Input::KeyboardAndMouse::{GetCapture, SetCapture};
use windows_sys::Win32::UI::WindowsAndMessaging::{SendMessageW, WM_LBUTTONDOWN};

use crate::{
Event, EventStatus, HandlerError, Window, WindowHandler, WindowSettings, WindowSize,
};

struct EmptyHandler;

impl WindowHandler for EmptyHandler {
fn draw(&self) -> Result<(), HandlerError> {
Ok(())
}

fn resized(&self, _new_size: WindowSize) -> Result<(), HandlerError> {
Ok(())
}

fn on_event(&self, _event: Event) -> EventStatus {
EventStatus::Ignored
}
}

/// Creates a window that is never shown, and returns it with its `HWND`.
fn hidden_window() -> (Window, HWND) {
let hwnd = Arc::new(AtomicIsize::new(0));
let builder_hwnd = Arc::clone(&hwnd);

let window = Window::create(WindowSettings::new(), move |context| {
if let Ok(RawWindowHandle::Win32(handle)) =
context.window_handle().map(|handle| handle.as_raw())
{
builder_hwnd.store(handle.hwnd.get(), Ordering::Relaxed);
}
Ok(EmptyHandler)
});
let window = match window {
Ok(window) => window,
Err(error) => panic!("could not create the window: {error}"),
};

let hwnd = hwnd.load(Ordering::Relaxed);
assert_ne!(hwnd, 0, "the handler builder didn't get a Win32 window handle");

(window, hwnd as HWND)
}

#[test]
fn capture_transfer_keeps_the_new_owner() {
let (_first_window, first) = hidden_window();
let (_second_window, second) = hidden_window();

// A button press makes the first window take the capture.
// SAFETY: `first` is a live window created on this thread.
unsafe { SendMessageW(first, WM_LBUTTONDOWN, MK_LBUTTON as WPARAM, 0) };
// SAFETY: GetCapture has no preconditions.
let owner = unsafe { GetCapture() };
assert_eq!(owner, first, "the button press should capture the mouse");

// The second window takes the capture while the button is still down. The first
// window gets WM_CAPTURECHANGED for this before SetCapture returns.
// SAFETY: `second` is a live window created on this thread.
let previous_owner = unsafe { SetCapture(second) };
assert_eq!(previous_owner, first);
// SAFETY: GetCapture has no preconditions.
let owner = unsafe { GetCapture() };
assert_eq!(owner, second, "the second window lost the capture it just took");
}
}