diff options
author | Ben Bridle <ben@derelict.engineering> | 2024-12-16 14:22:33 +1300 |
---|---|---|
committer | Ben Bridle <ben@derelict.engineering> | 2024-12-16 14:22:33 +1300 |
commit | 393d4f309ba238f81d100aae409a784f82d88c15 (patch) | |
tree | a6ce747bde68b5ac93efaf308cb63442e5e87604 /src/phosphor.rs | |
parent | 598e503f9898580b40c7a63b343eb2b1d168c2c3 (diff) | |
download | phosphor-393d4f309ba238f81d100aae409a784f82d88c15.zip |
Only send Event::CursorExit when all mouse buttons are released
This is to allow a user to drag an element inside the program window
and for the mouse cursor to move in and out of the window during the
drag operation. This is the intuitive and expected behaviour.
Diffstat (limited to 'src/phosphor.rs')
-rw-r--r-- | src/phosphor.rs | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/src/phosphor.rs b/src/phosphor.rs index ab13cb0..08c71da 100644 --- a/src/phosphor.rs +++ b/src/phosphor.rs @@ -134,10 +134,16 @@ impl PhosphorApplication { } } WindowEvent::CursorEntered { .. } => { + window.pointer_state = PointerState::In; handle!(Event::CursorEnter); } WindowEvent::CursorLeft { .. } => { - handle!(Event::CursorExit); + if window.mouse_buttons.iter().all(|b| !b) { + window.pointer_state = PointerState::Out; + handle!(Event::CursorExit); + } else { + window.pointer_state = PointerState::PendingOut; + } } WindowEvent::MouseWheel { delta, .. } => match delta { MouseScrollDelta::LineDelta(x, y) => { @@ -159,6 +165,20 @@ impl PhosphorApplication { WinitMouseButton::Forward => handle!(Event::MouseButton { button: MouseButton::Forward, action }), _ => (), } + // Keep track of mouse button hold states, and mark cursor as out when all are released. + let button_index = match button { + WinitMouseButton::Left => 0, + WinitMouseButton::Middle => 1, + WinitMouseButton::Right => 2, + WinitMouseButton::Back => 3, + WinitMouseButton::Forward => 4, + _ => return, + }; + window.mouse_buttons[button_index] = action.is_pressed(); + if window.pointer_state == PointerState::PendingOut && window.mouse_buttons.iter().all(|b| !b) { + window.pointer_state = PointerState::Out; + handle!(Event::CursorExit); + } } WindowEvent::RedrawRequested => { window.redraw(); |