Fix hovering the mouse over the active window creating an endless stream of CursorMoved events (#1170)

* Fix hovering the mouse over the active window creating an endless stream of CursorMoved events

* Format
This commit is contained in:
Osspial 2019-09-17 11:34:48 -04:00 committed by GitHub
parent 95581ab92f
commit d35ee0d580
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 4 deletions

View file

@ -12,6 +12,7 @@
- On Windows, fix handling of surrogate pairs when dispatching `ReceivedCharacter`. - On Windows, fix handling of surrogate pairs when dispatching `ReceivedCharacter`.
- On macOS 10.15, fix freeze upon exiting exclusive fullscreen mode. - On macOS 10.15, fix freeze upon exiting exclusive fullscreen mode.
- On iOS, fix null window on initial `HiDpiFactorChanged` event. - On iOS, fix null window on initial `HiDpiFactorChanged` event.
- On Windows, fix hovering the mouse over the active window creating an endless stream of CursorMoved events.
# 0.20.0 Alpha 3 (2019-08-14) # 0.20.0 Alpha 3 (2019-08-14)

View file

@ -141,6 +141,16 @@ pub fn set_cursor_hidden(hidden: bool) {
} }
} }
pub fn get_cursor_clip() -> Result<RECT, io::Error> {
unsafe {
let mut rect: RECT = mem::zeroed();
win_to_err(|| winuser::GetClipCursor(&mut rect)).map(|_| rect)
}
}
/// Sets the cursor's clip rect.
///
/// Note that calling this will automatically dispatch a `WM_MOUSEMOVE` event.
pub fn set_cursor_clip(rect: Option<RECT>) -> Result<(), io::Error> { pub fn set_cursor_clip(rect: Option<RECT>) -> Result<(), io::Error> {
unsafe { unsafe {
let rect_ptr = rect let rect_ptr = rect
@ -151,6 +161,19 @@ pub fn set_cursor_clip(rect: Option<RECT>) -> Result<(), io::Error> {
} }
} }
pub fn get_desktop_rect() -> RECT {
unsafe {
let left = winuser::GetSystemMetrics(winuser::SM_XVIRTUALSCREEN);
let top = winuser::GetSystemMetrics(winuser::SM_YVIRTUALSCREEN);
RECT {
left,
top,
right: left + winuser::GetSystemMetrics(winuser::SM_CXVIRTUALSCREEN),
bottom: top + winuser::GetSystemMetrics(winuser::SM_CYVIRTUALSCREEN),
}
}
}
pub fn is_focused(window: HWND) -> bool { pub fn is_focused(window: HWND) -> bool {
window == unsafe { winuser::GetActiveWindow() } window == unsafe { winuser::GetActiveWindow() }
} }

View file

@ -307,10 +307,25 @@ impl CursorFlags {
let client_rect = util::get_client_rect(window)?; let client_rect = util::get_client_rect(window)?;
if util::is_focused(window) { if util::is_focused(window) {
if self.contains(CursorFlags::GRABBED) { let cursor_clip = match self.contains(CursorFlags::GRABBED) {
util::set_cursor_clip(Some(client_rect))?; true => Some(client_rect),
} else { false => None,
util::set_cursor_clip(None)?; };
let rect_to_tuple = |rect: RECT| (rect.left, rect.top, rect.right, rect.bottom);
let active_cursor_clip = rect_to_tuple(util::get_cursor_clip()?);
let desktop_rect = rect_to_tuple(util::get_desktop_rect());
let active_cursor_clip = match desktop_rect == active_cursor_clip {
true => None,
false => Some(active_cursor_clip),
};
// We do this check because calling `set_cursor_clip` incessantly will flood the event
// loop with `WM_MOUSEMOVE` events, and `refresh_os_cursor` is called by `set_cursor_flags`
// which at times gets called once every iteration of the eventloop.
if active_cursor_clip != cursor_clip.map(rect_to_tuple) {
util::set_cursor_clip(cursor_clip)?;
} }
} }