Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly handle releasing mouse buttons when not hovering a window #121

Closed
Closed
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
34 changes: 32 additions & 2 deletions src/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,38 @@ pub fn process_input(
// When a user releases a mouse button, Safari emits both `CursorLeft` and `CursorEntered`
// events during the same frame. We don't want to reset mouse position in such a case, otherwise
// we won't be able to process the mouse button event.
if cursor_left_window.is_some() && cursor_left_window != cursor_entered_window {
egui_context.mouse_position = None;
if let Some(window_id) = &cursor_left_window {
if cursor_left_window != cursor_entered_window {
Comment on lines +131 to +132
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! Thanks for the PR.

Is the goal here to fix the behaviour for any platform? I believe this branch will be entered only for Safari, as this is the only browser that emits CursorLeft and CursorEntered on mouse button release.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems off since this change fixed unexpected behavior on desktop linux.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Furthermore, this branch needs to be possible to entered on all platforms - after all it's the only branch where mouse_position is reset.

Copy link
Owner

@mvlabat mvlabat Sep 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm.. I'm not sure it's working as intended. If I keep dragging the mouse pointer and it leaves the window, it'll fire the release button event immediately (as demonstrated in the video). Yet in the main version we keep reading mouse events when outside of a window, which allows us to continue dragging sliders for example (see #61).

Screen.Recording.2022-09-17.at.11.50.24.mov

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also not sure I understand what issue the PR is trying to address (or whether the issue indeed exists).

During this recording (the main branch), I dragged the cursor outside of the window, released it when being outside, moved the cursor back to the window, and we can see that bevy_egui didn't continue drawing to the canvas. If I understand correctly, your point is that we don't fire mouse release events when releasing the cursor outside of the window. But if that was the case, wouldn't it keep drawing when I moved the cursor back to the window?

Screen.Recording.2022-09-17.at.11.56.30.mov

Copy link
Contributor Author

@TheRawMeatball TheRawMeatball Sep 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And I just tested the main branch - the drawing continues when i release the button outside the window and move the cursor back in. Perhaps the issue doesn't happen on macs for one reason or another?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

egui-show-bug-.webm

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see, thanks for clarifying. I was indeed able to reproduce it on my KDE laptop.

Turns out, for some platforms, winit fires CursorLeft together with button input events when releasing a button outside of a window. I've just pushed #123 with the fix that also preserves the dragging behaviour. Could you please test it and let me know if it works for you?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to work :)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merged and released. Thanks again for flagging it!

egui_context.mouse_position = None;

if let Some(events) = input_resources
.egui_input
.get_mut(window_id)
.map(|egui_input| &mut egui_input.raw_input.events)
{
for button in [
egui::PointerButton::Primary,
egui::PointerButton::Secondary,
egui::PointerButton::Middle,
egui::PointerButton::Extra1,
egui::PointerButton::Extra2,
] {
if egui_context.ctx[window_id]
.input()
.pointer
.button_down(button)
{
events.push(egui::Event::PointerButton {
pressed: false,
button,
modifiers: Default::default(),
pos: Default::default(),
});
}
}
events.push(egui::Event::PointerGone);
}
}
}

if let Some(cursor_moved) = input_events.ev_cursor.iter().next_back() {
Expand Down