Merge branch 'main' into select-on-rename

This commit is contained in:
Nathan Sobo 2022-03-11 15:30:07 -07:00
commit 951fd1ab36
28 changed files with 1063 additions and 257 deletions

View file

@ -3,13 +3,15 @@ use serde_json::json;
use crate::{
geometry::vector::Vector2F, DebugContext, Element, ElementBox, Event, EventContext,
LayoutContext, PaintContext, SizeConstraint,
LayoutContext, NavigationDirection, PaintContext, SizeConstraint,
};
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>>,
navigate_mouse_down: Option<Box<dyn FnMut(NavigationDirection, &mut EventContext) -> bool>>,
}
impl EventHandler {
@ -18,6 +20,8 @@ impl EventHandler {
child,
capture: None,
mouse_down: None,
right_mouse_down: None,
navigate_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_navigate_mouse_down<F>(mut self, callback: F) -> Self
where
F: 'static + FnMut(NavigationDirection, &mut EventContext) -> bool,
{
self.navigate_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,
@ -87,6 +107,26 @@ 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::NavigateMouseDown {
position,
direction,
..
} => {
if let Some(callback) = self.navigate_mouse_down.as_mut() {
if bounds.contains_point(*position) {
return callback(*direction, cx);
}
}
false
}
_ => false,
}
}