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

Support mouse scroll in X11. #961

Merged
merged 6 commits into from
May 20, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ This means that druid no longer requires cairo on macOS and uses Core Graphics i
- Routing `LifeCycle::FocusChanged` to descendant widgets. ([#925] by [@yrns])
- Built-in open and save menu items now show the correct label and submit the right commands. ([#930] by [@finnerale])
- Wheel events now properly update hot state. ([#951] by [@xStrom])
- X11: Support mouse scrolling. ([#961] by [@jneem])

### Visual

Expand Down Expand Up @@ -200,6 +201,7 @@ This means that druid no longer requires cairo on macOS and uses Core Graphics i
[#953]: https://github.com/xi-editor/druid/pull/953
[#954]: https://github.com/xi-editor/druid/pull/954
[#959]: https://github.com/xi-editor/druid/pull/959
[#961]: https://github.com/xi-editor/druid/pull/961

## [0.5.0] - 2020-04-01

Expand Down
17 changes: 14 additions & 3 deletions druid-shell/src/platform/x11/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ impl Application {
self.screen_num
}

// TODO(x11/events): handle mouse scroll events
#[allow(clippy::cognitive_complexity)]
pub fn run(self, _handler: Option<Box<dyn AppHandler>>) {
loop {
Expand Down Expand Up @@ -226,7 +225,16 @@ impl Application {
let window_id = button_press.event();
match self.window(window_id) {
Ok(w) => {
if let Err(err) = w.handle_button_press(button_press) {
// X doesn't have dedicated scroll events: it uses mouse buttons instead.
// Buttons 4/5 are vertical; 6/7 are horizontal.
if button_press.detail() >= 4 && button_press.detail() <= 7 {
if let Err(err) = w.handle_wheel(button_press) {
log::error!(
"BUTTON_PRESS - failed to handle wheel: {}",
err
);
}
} else if let Err(err) = w.handle_button_press(button_press) {
log::error!("BUTTON_PRESS - failed to handle: {}", err);
}
}
Expand All @@ -238,7 +246,10 @@ impl Application {
let window_id = button_release.event();
match self.window(window_id) {
Ok(w) => {
if let Err(err) = w.handle_button_release(button_release) {
if button_release.detail() >= 4 && button_release.detail() <= 7 {
// This is the release event corresponding to a mouse wheel.
// Ignore it: we already handled the press event.
} else if let Err(err) = w.handle_button_release(button_release) {
log::error!("BUTTON_RELEASE - failed to handle: {}", err);
}
}
Expand Down
36 changes: 36 additions & 0 deletions druid-shell/src/platform/x11/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,42 @@ impl Window {
}
}

pub fn handle_wheel(&self, event: &xcb::ButtonPressEvent) -> Result<(), Error> {
let button = event.detail();
// The scroll delta of 120.0 was stolen from the GTK code, which apparently came from the
// windows docs.
xStrom marked this conversation as resolved.
Show resolved Hide resolved
let delta = match button {
xStrom marked this conversation as resolved.
Show resolved Hide resolved
4 => (0.0, -120.0),
5 => (0.0, 120.0),
6 => (-120.0, 0.0),
7 => (120.0, 0.0),
_ => {
log::error!("unexpected mouse wheel button: {}", button);
Copy link
Member

Choose a reason for hiding this comment

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

This is a good spot to return an error instead. Perhaps something like:

_ => return Err(Error::Generic(format!("unexpected mouse wheel button: {}", button))),

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm not sure if this affects your preferred error handling technique, but hitting this case represents a bug in druid-shell. It's impossible for now because we check that the button is between 4 and 7 before calling handle_wheel.

Copy link
Member

@xStrom xStrom May 19, 2020

Choose a reason for hiding this comment

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

Yeah I think returning an error in case of invalid input parameters is just fine. Certainly better than generating a zero delta wheel event.

(0.0, 0.0)
}
};
let mouse_event = MouseEvent {
pos: Point::new(event.event_x() as f64, event.event_y() as f64),
buttons: mouse_buttons(event.state()),
mods: key_mods(event.state()),
count: 0,
focus: false,
button: MouseButton::None,
wheel_delta: delta.into(),
};

match self.handler.try_borrow_mut() {
Ok(mut handler) => {
handler.wheel(&mouse_event);
Ok(())
}
Err(err) => Err(Error::BorrowError(format!(
"Window::handle_wheel handle: {}",
err
))),
}
}

pub fn handle_motion_notify(
&self,
motion_notify: &xcb::MotionNotifyEvent,
Expand Down