Skip to content
Merged
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
16 changes: 16 additions & 0 deletions src/win/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use winapi::um::winuser::{
WM_SYSKEYDOWN, WM_KEYUP, WM_SYSKEYUP, WM_INPUTLANGCHANGE,
GET_XBUTTON_WPARAM, WM_LBUTTONDOWN, WM_LBUTTONUP, WM_MBUTTONDOWN, WM_MBUTTONUP,
WM_RBUTTONDOWN, WM_RBUTTONUP, WM_XBUTTONDOWN, WM_XBUTTONUP, XBUTTON1, XBUTTON2,
SetCapture, GetCapture, ReleaseCapture, IsWindow
};

use std::cell::RefCell;
Expand Down Expand Up @@ -67,6 +68,8 @@ unsafe extern "system" fn wnd_proc(
let mut window = Window { hwnd };
let mut window = crate::Window(&mut window);

let mut mouse_button_counter = window_state.borrow().mouse_button_counter;

match msg {
WM_MOUSEMOVE => {
let x = (lparam & 0xFFFF) as i32;
Expand Down Expand Up @@ -103,9 +106,18 @@ unsafe extern "system" fn wnd_proc(
if let Some(button) = button {
let event = match msg {
WM_LBUTTONDOWN | WM_MBUTTONDOWN | WM_RBUTTONDOWN | WM_XBUTTONDOWN => {
// Capture the mouse cursor on button down
mouse_button_counter = mouse_button_counter.saturating_add(1);
SetCapture(hwnd);
MouseEvent::ButtonPressed(button)
}
WM_LBUTTONUP | WM_MBUTTONUP | WM_RBUTTONUP | WM_XBUTTONUP => {
// Release the mouse cursor capture when all buttons are released
mouse_button_counter = mouse_button_counter.saturating_sub(1);
if mouse_button_counter == 0 {
ReleaseCapture();
}

MouseEvent::ButtonReleased(button)
}
_ => {
Expand Down Expand Up @@ -155,6 +167,8 @@ unsafe extern "system" fn wnd_proc(
}
_ => {}
}

window_state.borrow_mut().mouse_button_counter = mouse_button_counter;
}

return DefWindowProcA(hwnd, msg, wparam, lparam);
Expand Down Expand Up @@ -188,6 +202,7 @@ struct WindowState {
window_class: ATOM,
window_info: WindowInfo,
keyboard_state: KeyboardState,
mouse_button_counter: usize,
handler: Box<dyn WindowHandler>,
}

Expand Down Expand Up @@ -293,6 +308,7 @@ impl Window {
window_class,
window_info,
keyboard_state: KeyboardState::new(),
mouse_button_counter: 0,
handler,
}));

Expand Down