From 0f14e63b340cd21c470e827bb789362fe302adf5 Mon Sep 17 00:00:00 2001 From: stuart nelson Date: Sun, 26 Nov 2017 15:43:13 -0500 Subject: [PATCH] Send mouse position after focused/cursorenter events (#349) * Update mouse pos after cursor enter event * Update mouse position on windows focus * Send device_id * Update other device id * Fix windows import * Remove deque for vec * Just send event * Use correct push_back method * Push correct event --- src/platform/linux/x11/mod.rs | 15 +++++++++++++-- src/platform/macos/events_loop.rs | 27 ++++++++++++++++++++++++++- src/platform/windows/events_loop.rs | 10 +++++++++- 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/src/platform/linux/x11/mod.rs b/src/platform/linux/x11/mod.rs index 873c1508..1e13c6d6 100644 --- a/src/platform/linux/x11/mod.rs +++ b/src/platform/linux/x11/mod.rs @@ -460,8 +460,13 @@ impl EventsLoop { physical_device.reset_scroll_position(info); } } + callback(Event::WindowEvent { window_id: mkwid(xev.event), event: CursorEntered { device_id: mkdid(xev.deviceid) } }); - callback(Event::WindowEvent { window_id: mkwid(xev.event), event: CursorEntered { device_id: mkdid(xev.deviceid) } }) + let new_cursor_pos = (xev.event_x, xev.event_y); + callback(Event::WindowEvent { window_id: wid, event: CursorMoved { + device_id: mkdid(xev.deviceid), + position: new_cursor_pos + }}) } ffi::XI_Leave => { let xev: &ffi::XILeaveEvent = unsafe { &*(xev.data as *const _) }; @@ -474,7 +479,13 @@ impl EventsLoop { let window_data = windows.get_mut(&WindowId(xev.event)).unwrap(); (self.display.xlib.XSetICFocus)(window_data.ic); } - callback(Event::WindowEvent { window_id: mkwid(xev.event), event: Focused(true) }) + callback(Event::WindowEvent { window_id: mkwid(xev.event), event: Focused(true) }); + + let new_cursor_pos = (xev.event_x, xev.event_y); + callback(Event::WindowEvent { window_id: wid, event: CursorMoved { + device_id: mkdid(xev.deviceid), + position: new_cursor_pos + }}) } ffi::XI_FocusOut => { let xev: &ffi::XIFocusOutEvent = unsafe { &*(xev.data as *const _) }; diff --git a/src/platform/macos/events_loop.rs b/src/platform/macos/events_loop.rs index 8aecbf46..7be58c81 100644 --- a/src/platform/macos/events_loop.rs +++ b/src/platform/macos/events_loop.rs @@ -442,7 +442,32 @@ impl EventsLoop { appkit::NSOtherMouseDown => { Some(into_event(WindowEvent::MouseInput { device_id: DEVICE_ID, state: ElementState::Pressed, button: MouseButton::Middle })) }, appkit::NSOtherMouseUp => { Some(into_event(WindowEvent::MouseInput { device_id: DEVICE_ID, state: ElementState::Released, button: MouseButton::Middle })) }, - appkit::NSMouseEntered => { Some(into_event(WindowEvent::CursorEntered { device_id: DEVICE_ID })) }, + appkit::NSMouseEntered => { + let window = match maybe_window.or_else(maybe_key_window) { + Some(window) => window, + None => return None, + }; + + let window_point = ns_event.locationInWindow(); + let view_point = if ns_window == cocoa::base::nil { + let ns_size = foundation::NSSize::new(0.0, 0.0); + let ns_rect = foundation::NSRect::new(window_point, ns_size); + let window_rect = window.window.convertRectFromScreen_(ns_rect); + window.view.convertPoint_fromView_(window_rect.origin, cocoa::base::nil) + } else { + window.view.convertPoint_fromView_(window_point, cocoa::base::nil) + }; + let view_rect = NSView::frame(*window.view); + let scale_factor = window.hidpi_factor(); + + let x = (scale_factor * view_point.x as f32) as f64; + let y = (scale_factor * (view_rect.size.height - view_point.y) as f32) as f64; + let window_event = WindowEvent::CursorMoved { device_id: DEVICE_ID, position: (x, y) }; + let event = Event::WindowEvent { window_id: ::WindowId(window.id()), event: window_event }; + + self.shared.pending_events.lock().unwrap().push_back(event); + Some(into_event(WindowEvent::CursorEntered { device_id: DEVICE_ID })) + }, appkit::NSMouseExited => { Some(into_event(WindowEvent::CursorLeft { device_id: DEVICE_ID })) }, appkit::NSMouseMoved | diff --git a/src/platform/windows/events_loop.rs b/src/platform/windows/events_loop.rs index f362d9a4..f68fbe7e 100644 --- a/src/platform/windows/events_loop.rs +++ b/src/platform/windows/events_loop.rs @@ -673,11 +673,19 @@ pub unsafe extern "system" fn callback(window: winapi::HWND, msg: winapi::UINT, }, winapi::WM_SETFOCUS => { - use events::WindowEvent::Focused; + use events::WindowEvent::{Focused, CursorMoved}; send_event(Event::WindowEvent { window_id: SuperWindowId(WindowId(window)), event: Focused(true) }); + + let x = winapi::GET_X_LPARAM(lparam) as f64; + let y = winapi::GET_Y_LPARAM(lparam) as f64; + + send_event(Event::WindowEvent { + window_id: SuperWindowId(WindowId(window)), + event: CursorMoved { device_id: DEVICE_ID, position: (x, y) }, + }); 0 },