Skip to content

Commit 9fdb751

Browse files
authored
Added mouse capture/release to windows backend (#81)
* Added mouse capture/release to windows backend * Mouse capture now automatic only Removed manual ability to trigger mouse capture and release. * Added refcount for mouse button event Added refcount to prevent the mouse capture from releasing before all mouse buttons have been released. * Removed unnecessary function from window
1 parent c64b225 commit 9fdb751

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

src/win/window.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use winapi::um::winuser::{
1313
WM_SYSKEYDOWN, WM_KEYUP, WM_SYSKEYUP, WM_INPUTLANGCHANGE,
1414
GET_XBUTTON_WPARAM, WM_LBUTTONDOWN, WM_LBUTTONUP, WM_MBUTTONDOWN, WM_MBUTTONUP,
1515
WM_RBUTTONDOWN, WM_RBUTTONUP, WM_XBUTTONDOWN, WM_XBUTTONUP, XBUTTON1, XBUTTON2,
16+
SetCapture, GetCapture, ReleaseCapture, IsWindow
1617
};
1718

1819
use std::cell::RefCell;
@@ -68,6 +69,8 @@ unsafe extern "system" fn wnd_proc(
6869
let mut window = Window { hwnd };
6970
let mut window = crate::Window(&mut window);
7071

72+
let mut mouse_button_counter = window_state.borrow().mouse_button_counter;
73+
7174
match msg {
7275
WM_MOUSEMOVE => {
7376
let x = (lparam & 0xFFFF) as i32;
@@ -104,9 +107,18 @@ unsafe extern "system" fn wnd_proc(
104107
if let Some(button) = button {
105108
let event = match msg {
106109
WM_LBUTTONDOWN | WM_MBUTTONDOWN | WM_RBUTTONDOWN | WM_XBUTTONDOWN => {
110+
// Capture the mouse cursor on button down
111+
mouse_button_counter = mouse_button_counter.saturating_add(1);
112+
SetCapture(hwnd);
107113
MouseEvent::ButtonPressed(button)
108114
}
109115
WM_LBUTTONUP | WM_MBUTTONUP | WM_RBUTTONUP | WM_XBUTTONUP => {
116+
// Release the mouse cursor capture when all buttons are released
117+
mouse_button_counter = mouse_button_counter.saturating_sub(1);
118+
if mouse_button_counter == 0 {
119+
ReleaseCapture();
120+
}
121+
110122
MouseEvent::ButtonReleased(button)
111123
}
112124
_ => {
@@ -156,6 +168,8 @@ unsafe extern "system" fn wnd_proc(
156168
}
157169
_ => {}
158170
}
171+
172+
window_state.borrow_mut().mouse_button_counter = mouse_button_counter;
159173
}
160174

161175
return DefWindowProcW(hwnd, msg, wparam, lparam);
@@ -193,6 +207,7 @@ struct WindowState {
193207
window_class: ATOM,
194208
window_info: WindowInfo,
195209
keyboard_state: KeyboardState,
210+
mouse_button_counter: usize,
196211
handler: Box<dyn WindowHandler>,
197212
}
198213

@@ -301,6 +316,7 @@ impl Window {
301316
window_class,
302317
window_info,
303318
keyboard_state: KeyboardState::new(),
319+
mouse_button_counter: 0,
304320
handler,
305321
}));
306322

0 commit comments

Comments
 (0)