combine branches of events in from_native

This commit is contained in:
Keith Simmons 2022-07-06 16:59:58 -07:00
parent bcb553f233
commit c139f1e6b6
2 changed files with 59 additions and 98 deletions

View file

@ -7,7 +7,7 @@ use crate::{
}; };
use cocoa::{ use cocoa::{
appkit::{NSEvent, NSEventModifierFlags, NSEventType}, appkit::{NSEvent, NSEventModifierFlags, NSEventType},
base::{id, nil, YES}, base::{id, YES},
foundation::NSString as _, foundation::NSString as _,
}; };
use std::{borrow::Cow, ffi::CStr, os::raw::c_char}; use std::{borrow::Cow, ffi::CStr, os::raw::c_char};
@ -111,11 +111,23 @@ impl Event {
input, input,
})) }))
} }
NSEventType::NSLeftMouseDown => { NSEventType::NSLeftMouseDown
| NSEventType::NSRightMouseDown
| NSEventType::NSOtherMouseDown => {
let button = match native_event.buttonNumber() {
0 => MouseButton::Left,
1 => MouseButton::Right,
2 => MouseButton::Middle,
3 => MouseButton::Navigate(NavigationDirection::Back),
4 => MouseButton::Navigate(NavigationDirection::Forward),
// Other mouse buttons aren't tracked currently
_ => return None,
};
let modifiers = native_event.modifierFlags(); let modifiers = native_event.modifierFlags();
window_height.map(|window_height| { window_height.map(|window_height| {
Self::MouseDown(MouseEvent { Self::MouseDown(MouseEvent {
button: MouseButton::Left, button,
position: vec2f( position: vec2f(
native_event.locationInWindow().x as f32, native_event.locationInWindow().x as f32,
window_height - native_event.locationInWindow().y as f32, window_height - native_event.locationInWindow().y as f32,
@ -128,89 +140,23 @@ impl Event {
}) })
}) })
} }
NSEventType::NSLeftMouseUp => window_height.map(|window_height| { NSEventType::NSLeftMouseUp
let modifiers = native_event.modifierFlags(); | NSEventType::NSRightMouseUp
Self::MouseUp(MouseEvent { | NSEventType::NSOtherMouseUp => {
button: MouseButton::Left, let button = match native_event.buttonNumber() {
position: vec2f( 0 => MouseButton::Left,
native_event.locationInWindow().x as f32, 1 => MouseButton::Right,
window_height - native_event.locationInWindow().y as f32, 2 => MouseButton::Middle,
), 3 => MouseButton::Navigate(NavigationDirection::Back),
ctrl: modifiers.contains(NSEventModifierFlags::NSControlKeyMask), 4 => MouseButton::Navigate(NavigationDirection::Forward),
alt: modifiers.contains(NSEventModifierFlags::NSAlternateKeyMask),
shift: modifiers.contains(NSEventModifierFlags::NSShiftKeyMask),
cmd: modifiers.contains(NSEventModifierFlags::NSCommandKeyMask),
click_count: native_event.clickCount() as usize,
})
}),
NSEventType::NSRightMouseDown => {
let modifiers = native_event.modifierFlags();
window_height.map(|window_height| {
Self::MouseDown(MouseEvent {
button: MouseButton::Right,
position: vec2f(
native_event.locationInWindow().x as f32,
window_height - native_event.locationInWindow().y as f32,
),
ctrl: modifiers.contains(NSEventModifierFlags::NSControlKeyMask),
alt: modifiers.contains(NSEventModifierFlags::NSAlternateKeyMask),
shift: modifiers.contains(NSEventModifierFlags::NSShiftKeyMask),
cmd: modifiers.contains(NSEventModifierFlags::NSCommandKeyMask),
click_count: native_event.clickCount() as usize,
})
})
}
NSEventType::NSRightMouseUp => window_height.map(|window_height| {
let modifiers = native_event.modifierFlags();
Self::MouseUp(MouseEvent {
button: MouseButton::Right,
position: vec2f(
native_event.locationInWindow().x as f32,
window_height - native_event.locationInWindow().y as f32,
),
ctrl: modifiers.contains(NSEventModifierFlags::NSControlKeyMask),
alt: modifiers.contains(NSEventModifierFlags::NSAlternateKeyMask),
shift: modifiers.contains(NSEventModifierFlags::NSShiftKeyMask),
cmd: modifiers.contains(NSEventModifierFlags::NSCommandKeyMask),
click_count: native_event.clickCount() as usize,
})
}),
NSEventType::NSOtherMouseDown => {
let direction = match native_event.buttonNumber() {
3 => NavigationDirection::Back,
4 => NavigationDirection::Forward,
// Other mouse buttons aren't tracked currently // Other mouse buttons aren't tracked currently
_ => return None, _ => return None,
}; };
let modifiers = native_event.modifierFlags();
window_height.map(|window_height| {
Self::MouseDown(MouseEvent {
button: MouseButton::Navigate(direction),
position: vec2f(
native_event.locationInWindow().x as f32,
window_height - native_event.locationInWindow().y as f32,
),
ctrl: modifiers.contains(NSEventModifierFlags::NSControlKeyMask),
alt: modifiers.contains(NSEventModifierFlags::NSAlternateKeyMask),
shift: modifiers.contains(NSEventModifierFlags::NSShiftKeyMask),
cmd: modifiers.contains(NSEventModifierFlags::NSCommandKeyMask),
click_count: native_event.clickCount() as usize,
})
})
}
NSEventType::NSOtherMouseUp => {
let direction = match native_event.buttonNumber() {
3 => NavigationDirection::Back,
4 => NavigationDirection::Forward,
// Other mouse buttons aren't tracked currently
_ => return None,
};
let modifiers = native_event.modifierFlags();
window_height.map(|window_height| { window_height.map(|window_height| {
let modifiers = native_event.modifierFlags();
Self::MouseUp(MouseEvent { Self::MouseUp(MouseEvent {
button: MouseButton::Navigate(direction), button,
position: vec2f( position: vec2f(
native_event.locationInWindow().x as f32, native_event.locationInWindow().x as f32,
window_height - native_event.locationInWindow().y as f32, window_height - native_event.locationInWindow().y as f32,
@ -236,31 +182,42 @@ impl Event {
precise: native_event.hasPreciseScrollingDeltas() == YES, precise: native_event.hasPreciseScrollingDeltas() == YES,
}) })
}), }),
NSEventType::NSMouseMoved => window_height.map(|window_height| { NSEventType::NSLeftMouseDragged
let modifiers = native_event.modifierFlags(); | NSEventType::NSRightMouseDragged
let pressed_button_flags = NSEvent::pressedMouseButtons(nil); | NSEventType::NSOtherMouseDragged => {
let pressed_button = match native_event.buttonNumber() {
// Pick the "strongest" button to report in mouse dragged events 0 => MouseButton::Left,
let pressed_button = if pressed_button_flags & (1 << 0) != 0 { 1 => MouseButton::Right,
Some(MouseButton::Left) 2 => MouseButton::Middle,
} else if pressed_button_flags & (1 << 1) != 0 { 3 => MouseButton::Navigate(NavigationDirection::Back),
Some(MouseButton::Right) 4 => MouseButton::Navigate(NavigationDirection::Forward),
} else if pressed_button_flags & (1 << 2) != 0 { // Other mouse buttons aren't tracked currently
Some(MouseButton::Middle) _ => return None,
} else if pressed_button_flags & (1 << 3) != 0 {
Some(MouseButton::Navigate(NavigationDirection::Back))
} else if pressed_button_flags & (1 << 4) != 0 {
Some(MouseButton::Navigate(NavigationDirection::Forward))
} else {
None
}; };
window_height.map(|window_height| {
let modifiers = native_event.modifierFlags();
Self::MouseMoved(MouseMovedEvent {
pressed_button: Some(pressed_button),
position: vec2f(
native_event.locationInWindow().x as f32,
window_height - native_event.locationInWindow().y as f32,
),
ctrl: modifiers.contains(NSEventModifierFlags::NSControlKeyMask),
alt: modifiers.contains(NSEventModifierFlags::NSAlternateKeyMask),
shift: modifiers.contains(NSEventModifierFlags::NSShiftKeyMask),
cmd: modifiers.contains(NSEventModifierFlags::NSCommandKeyMask),
})
})
}
NSEventType::NSMouseMoved => window_height.map(|window_height| {
let modifiers = native_event.modifierFlags();
Self::MouseMoved(MouseMovedEvent { Self::MouseMoved(MouseMovedEvent {
position: vec2f( position: vec2f(
native_event.locationInWindow().x as f32, native_event.locationInWindow().x as f32,
window_height - native_event.locationInWindow().y as f32, window_height - native_event.locationInWindow().y as f32,
), ),
pressed_button, pressed_button: None,
ctrl: modifiers.contains(NSEventModifierFlags::NSControlKeyMask), ctrl: modifiers.contains(NSEventModifierFlags::NSControlKeyMask),
alt: modifiers.contains(NSEventModifierFlags::NSAlternateKeyMask), alt: modifiers.contains(NSEventModifierFlags::NSAlternateKeyMask),
shift: modifiers.contains(NSEventModifierFlags::NSShiftKeyMask), shift: modifiers.contains(NSEventModifierFlags::NSShiftKeyMask),

View file

@ -127,6 +127,10 @@ unsafe fn build_classes() {
sel!(mouseMoved:), sel!(mouseMoved:),
handle_view_event as extern "C" fn(&Object, Sel, id), handle_view_event as extern "C" fn(&Object, Sel, id),
); );
decl.add_method(
sel!(mouseDragged:),
handle_view_event as extern "C" fn(&Object, Sel, id),
);
decl.add_method( decl.add_method(
sel!(scrollWheel:), sel!(scrollWheel:),
handle_view_event as extern "C" fn(&Object, Sel, id), handle_view_event as extern "C" fn(&Object, Sel, id),