Skip to content

Commit

Permalink
On macOS, fix middle/other mouse buttons reporting
Browse files Browse the repository at this point in the history
All buttons except for the left/right/middle was always reported
as middle.
  • Loading branch information
Roman Akberov authored Jan 18, 2023
1 parent b1a5fae commit 1886949
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ And please only add new entries to the top of this list, right below the `# Unre
- Added Orbital support for Redox OS
- On X11, added `drag_resize_window` method.
- Added `Window::set_transparent` to provide a hint about transparency of the window on Wayland and macOS.
- On macOS, fix the mouse buttons other than left/right/middle being reported as middle.

# 0.27.5

Expand Down
29 changes: 22 additions & 7 deletions src/platform_impl/macos/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,42 +688,42 @@ declare_class!(
fn mouse_down(&mut self, event: &NSEvent) {
trace_scope!("mouseDown:");
self.mouse_motion(event);
self.mouse_click(event, MouseButton::Left, ElementState::Pressed);
self.mouse_click(event, ElementState::Pressed);
}

#[sel(mouseUp:)]
fn mouse_up(&mut self, event: &NSEvent) {
trace_scope!("mouseUp:");
self.mouse_motion(event);
self.mouse_click(event, MouseButton::Left, ElementState::Released);
self.mouse_click(event, ElementState::Released);
}

#[sel(rightMouseDown:)]
fn right_mouse_down(&mut self, event: &NSEvent) {
trace_scope!("rightMouseDown:");
self.mouse_motion(event);
self.mouse_click(event, MouseButton::Right, ElementState::Pressed);
self.mouse_click(event, ElementState::Pressed);
}

#[sel(rightMouseUp:)]
fn right_mouse_up(&mut self, event: &NSEvent) {
trace_scope!("rightMouseUp:");
self.mouse_motion(event);
self.mouse_click(event, MouseButton::Right, ElementState::Released);
self.mouse_click(event, ElementState::Released);
}

#[sel(otherMouseDown:)]
fn other_mouse_down(&mut self, event: &NSEvent) {
trace_scope!("otherMouseDown:");
self.mouse_motion(event);
self.mouse_click(event, MouseButton::Middle, ElementState::Pressed);
self.mouse_click(event, ElementState::Pressed);
}

#[sel(otherMouseUp:)]
fn other_mouse_up(&mut self, event: &NSEvent) {
trace_scope!("otherMouseUp:");
self.mouse_motion(event);
self.mouse_click(event, MouseButton::Middle, ElementState::Released);
self.mouse_click(event, ElementState::Released);
}

// No tracing on these because that would be overly verbose
Expand Down Expand Up @@ -1001,7 +1001,9 @@ impl WinitView {
}
}

fn mouse_click(&mut self, event: &NSEvent, button: MouseButton, button_state: ElementState) {
fn mouse_click(&mut self, event: &NSEvent, button_state: ElementState) {
let button = mouse_button(event);

self.update_potentially_stale_modifiers(event);

let window_event = Event::WindowEvent {
Expand Down Expand Up @@ -1052,3 +1054,16 @@ impl WinitView {
AppState::queue_event(EventWrapper::StaticEvent(window_event));
}
}

/// Get the mouse button from the NSEvent.
fn mouse_button(event: &NSEvent) -> MouseButton {
// The buttonNumber property only makes sense for the mouse events:
// NSLeftMouse.../NSRightMouse.../NSOtherMouse...
// For the other events, it's always set to 0.
match event.buttonNumber() {
0 => MouseButton::Left,
1 => MouseButton::Right,
2 => MouseButton::Middle,
n => MouseButton::Other(n as u16),
}
}

0 comments on commit 1886949

Please sign in to comment.