Add missing mouse button events and mouse history navigation
Co-Authored-By: Max Brunsfeld Co-Authored-By: Nathan Sobo
This commit is contained in:
parent
03aa906068
commit
fc36c706d3
6 changed files with 179 additions and 37 deletions
|
@ -10,6 +10,8 @@ pub struct EventHandler {
|
|||
child: ElementBox,
|
||||
capture: Option<Box<dyn FnMut(&Event, RectF, &mut EventContext) -> bool>>,
|
||||
mouse_down: Option<Box<dyn FnMut(&mut EventContext) -> bool>>,
|
||||
right_mouse_down: Option<Box<dyn FnMut(&mut EventContext) -> bool>>,
|
||||
other_mouse_down: Option<Box<dyn FnMut(u16, &mut EventContext) -> bool>>,
|
||||
}
|
||||
|
||||
impl EventHandler {
|
||||
|
@ -18,6 +20,8 @@ impl EventHandler {
|
|||
child,
|
||||
capture: None,
|
||||
mouse_down: None,
|
||||
right_mouse_down: None,
|
||||
other_mouse_down: None,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -29,6 +33,22 @@ impl EventHandler {
|
|||
self
|
||||
}
|
||||
|
||||
pub fn on_right_mouse_down<F>(mut self, callback: F) -> Self
|
||||
where
|
||||
F: 'static + FnMut(&mut EventContext) -> bool,
|
||||
{
|
||||
self.right_mouse_down = Some(Box::new(callback));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn on_other_mouse_down<F>(mut self, callback: F) -> Self
|
||||
where
|
||||
F: 'static + FnMut(u16, &mut EventContext) -> bool,
|
||||
{
|
||||
self.other_mouse_down = Some(Box::new(callback));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn capture<F>(mut self, callback: F) -> Self
|
||||
where
|
||||
F: 'static + FnMut(&Event, RectF, &mut EventContext) -> bool,
|
||||
|
@ -86,7 +106,23 @@ impl Element for EventHandler {
|
|||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
},
|
||||
Event::RightMouseDown { position, .. } => {
|
||||
if let Some(callback) = self.right_mouse_down.as_mut() {
|
||||
if bounds.contains_point(*position) {
|
||||
return callback(cx);
|
||||
}
|
||||
}
|
||||
false
|
||||
},
|
||||
Event::OtherMouseDown { position, button, .. } => {
|
||||
if let Some(callback) = self.other_mouse_down.as_mut() {
|
||||
if bounds.contains_point(*position) {
|
||||
return callback(*button, cx);
|
||||
}
|
||||
}
|
||||
false
|
||||
},
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,6 +26,30 @@ pub enum Event {
|
|||
LeftMouseDragged {
|
||||
position: Vector2F,
|
||||
},
|
||||
RightMouseDown {
|
||||
position: Vector2F,
|
||||
ctrl: bool,
|
||||
alt: bool,
|
||||
shift: bool,
|
||||
cmd: bool,
|
||||
click_count: usize,
|
||||
},
|
||||
RightMouseUp {
|
||||
position: Vector2F,
|
||||
},
|
||||
OtherMouseDown {
|
||||
position: Vector2F,
|
||||
button: u16,
|
||||
ctrl: bool,
|
||||
alt: bool,
|
||||
shift: bool,
|
||||
cmd: bool,
|
||||
click_count: usize,
|
||||
},
|
||||
OtherMouseUp {
|
||||
position: Vector2F,
|
||||
button: u16,
|
||||
},
|
||||
MouseMoved {
|
||||
position: Vector2F,
|
||||
left_mouse_down: bool,
|
||||
|
|
|
@ -125,6 +125,48 @@ impl Event {
|
|||
window_height - native_event.locationInWindow().y as f32,
|
||||
),
|
||||
}),
|
||||
NSEventType::NSRightMouseDown => {
|
||||
let modifiers = native_event.modifierFlags();
|
||||
window_height.map(|window_height| Self::RightMouseDown {
|
||||
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| Self::RightMouseUp {
|
||||
position: vec2f(
|
||||
native_event.locationInWindow().x as f32,
|
||||
window_height - native_event.locationInWindow().y as f32,
|
||||
),
|
||||
}),
|
||||
NSEventType::NSOtherMouseDown => {
|
||||
let modifiers = native_event.modifierFlags();
|
||||
window_height.map(|window_height| Self::OtherMouseDown {
|
||||
position: vec2f(
|
||||
native_event.locationInWindow().x as f32,
|
||||
window_height - native_event.locationInWindow().y as f32,
|
||||
),
|
||||
button: native_event.buttonNumber() as u16,
|
||||
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 => window_height.map(|window_height| Self::OtherMouseUp {
|
||||
position: vec2f(
|
||||
native_event.locationInWindow().x as f32,
|
||||
window_height - native_event.locationInWindow().y as f32,
|
||||
),
|
||||
button: native_event.buttonNumber() as u16,
|
||||
}),
|
||||
NSEventType::NSLeftMouseDragged => {
|
||||
window_height.map(|window_height| Self::LeftMouseDragged {
|
||||
position: vec2f(
|
||||
|
|
|
@ -95,6 +95,22 @@ unsafe fn build_classes() {
|
|||
sel!(mouseUp:),
|
||||
handle_view_event as extern "C" fn(&Object, Sel, id),
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(rightMouseDown:),
|
||||
handle_view_event as extern "C" fn(&Object, Sel, id),
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(rightMouseUp:),
|
||||
handle_view_event as extern "C" fn(&Object, Sel, id),
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(otherMouseDown:),
|
||||
handle_view_event as extern "C" fn(&Object, Sel, id),
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(otherMouseUp:),
|
||||
handle_view_event as extern "C" fn(&Object, Sel, id),
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(mouseMoved:),
|
||||
handle_view_event as extern "C" fn(&Object, Sel, id),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue