On macOS, fix middle/other mouse buttons reporting

All buttons except for the left/right/middle was always reported
as middle.
This commit is contained in:
Roman Akberov 2023-01-18 04:32:34 +01:00 committed by GitHub
parent b1a5fae1f5
commit 1886949efe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 7 deletions

View file

@ -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 - Added Orbital support for Redox OS
- On X11, added `drag_resize_window` method. - On X11, added `drag_resize_window` method.
- Added `Window::set_transparent` to provide a hint about transparency of the window on Wayland and macOS. - 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 # 0.27.5

View file

@ -688,42 +688,42 @@ declare_class!(
fn mouse_down(&mut self, event: &NSEvent) { fn mouse_down(&mut self, event: &NSEvent) {
trace_scope!("mouseDown:"); trace_scope!("mouseDown:");
self.mouse_motion(event); self.mouse_motion(event);
self.mouse_click(event, MouseButton::Left, ElementState::Pressed); self.mouse_click(event, ElementState::Pressed);
} }
#[sel(mouseUp:)] #[sel(mouseUp:)]
fn mouse_up(&mut self, event: &NSEvent) { fn mouse_up(&mut self, event: &NSEvent) {
trace_scope!("mouseUp:"); trace_scope!("mouseUp:");
self.mouse_motion(event); self.mouse_motion(event);
self.mouse_click(event, MouseButton::Left, ElementState::Released); self.mouse_click(event, ElementState::Released);
} }
#[sel(rightMouseDown:)] #[sel(rightMouseDown:)]
fn right_mouse_down(&mut self, event: &NSEvent) { fn right_mouse_down(&mut self, event: &NSEvent) {
trace_scope!("rightMouseDown:"); trace_scope!("rightMouseDown:");
self.mouse_motion(event); self.mouse_motion(event);
self.mouse_click(event, MouseButton::Right, ElementState::Pressed); self.mouse_click(event, ElementState::Pressed);
} }
#[sel(rightMouseUp:)] #[sel(rightMouseUp:)]
fn right_mouse_up(&mut self, event: &NSEvent) { fn right_mouse_up(&mut self, event: &NSEvent) {
trace_scope!("rightMouseUp:"); trace_scope!("rightMouseUp:");
self.mouse_motion(event); self.mouse_motion(event);
self.mouse_click(event, MouseButton::Right, ElementState::Released); self.mouse_click(event, ElementState::Released);
} }
#[sel(otherMouseDown:)] #[sel(otherMouseDown:)]
fn other_mouse_down(&mut self, event: &NSEvent) { fn other_mouse_down(&mut self, event: &NSEvent) {
trace_scope!("otherMouseDown:"); trace_scope!("otherMouseDown:");
self.mouse_motion(event); self.mouse_motion(event);
self.mouse_click(event, MouseButton::Middle, ElementState::Pressed); self.mouse_click(event, ElementState::Pressed);
} }
#[sel(otherMouseUp:)] #[sel(otherMouseUp:)]
fn other_mouse_up(&mut self, event: &NSEvent) { fn other_mouse_up(&mut self, event: &NSEvent) {
trace_scope!("otherMouseUp:"); trace_scope!("otherMouseUp:");
self.mouse_motion(event); 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 // No tracing on these because that would be overly verbose
@ -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); self.update_potentially_stale_modifiers(event);
let window_event = Event::WindowEvent { let window_event = Event::WindowEvent {
@ -1052,3 +1054,16 @@ impl WinitView {
AppState::queue_event(EventWrapper::StaticEvent(window_event)); 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),
}
}