2021-04-07 05:50:13 +00:00
|
|
|
use pathfinder_geometry::rect::RectF;
|
|
|
|
use serde_json::json;
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
use crate::{
|
2021-08-20 22:40:45 +00:00
|
|
|
geometry::vector::Vector2F, DebugContext, Element, ElementBox, Event, EventContext,
|
|
|
|
LayoutContext, PaintContext, SizeConstraint,
|
2021-03-10 04:00:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
pub struct EventHandler {
|
2021-03-22 02:54:23 +00:00
|
|
|
child: ElementBox,
|
|
|
|
mouse_down: Option<Box<dyn FnMut(&mut EventContext) -> bool>>,
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl EventHandler {
|
2021-03-22 02:54:23 +00:00
|
|
|
pub fn new(child: ElementBox) -> Self {
|
2021-03-10 04:00:51 +00:00
|
|
|
Self {
|
|
|
|
child,
|
|
|
|
mouse_down: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn on_mouse_down<F>(mut self, callback: F) -> Self
|
|
|
|
where
|
2021-03-22 02:54:23 +00:00
|
|
|
F: 'static + FnMut(&mut EventContext) -> bool,
|
2021-03-10 04:00:51 +00:00
|
|
|
{
|
2021-03-22 02:54:23 +00:00
|
|
|
self.mouse_down = Some(Box::new(callback));
|
2021-03-10 04:00:51 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Element for EventHandler {
|
2021-03-22 02:54:23 +00:00
|
|
|
type LayoutState = ();
|
|
|
|
type PaintState = ();
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
fn layout(
|
|
|
|
&mut self,
|
|
|
|
constraint: SizeConstraint,
|
2021-05-28 22:25:15 +00:00
|
|
|
cx: &mut LayoutContext,
|
2021-03-22 02:54:23 +00:00
|
|
|
) -> (Vector2F, Self::LayoutState) {
|
2021-05-28 22:25:15 +00:00
|
|
|
let size = self.child.layout(constraint, cx);
|
2021-03-22 02:54:23 +00:00
|
|
|
(size, ())
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
2021-03-22 02:54:23 +00:00
|
|
|
fn paint(
|
|
|
|
&mut self,
|
2021-04-07 05:50:13 +00:00
|
|
|
bounds: RectF,
|
2021-09-02 09:42:23 +00:00
|
|
|
visible_bounds: RectF,
|
2021-03-22 02:54:23 +00:00
|
|
|
_: &mut Self::LayoutState,
|
2021-05-28 22:25:15 +00:00
|
|
|
cx: &mut PaintContext,
|
2021-03-22 02:54:23 +00:00
|
|
|
) -> Self::PaintState {
|
2021-09-02 09:42:23 +00:00
|
|
|
self.child.paint(bounds.origin(), visible_bounds, cx);
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
2021-03-22 02:54:23 +00:00
|
|
|
fn dispatch_event(
|
|
|
|
&mut self,
|
|
|
|
event: &Event,
|
2021-04-07 05:50:13 +00:00
|
|
|
bounds: RectF,
|
2021-03-22 02:54:23 +00:00
|
|
|
_: &mut Self::LayoutState,
|
|
|
|
_: &mut Self::PaintState,
|
2021-05-28 22:25:15 +00:00
|
|
|
cx: &mut EventContext,
|
2021-03-22 02:54:23 +00:00
|
|
|
) -> bool {
|
2021-05-28 22:25:15 +00:00
|
|
|
if self.child.dispatch_event(event, cx) {
|
2021-03-22 02:54:23 +00:00
|
|
|
true
|
|
|
|
} else {
|
|
|
|
match event {
|
|
|
|
Event::LeftMouseDown { position, .. } => {
|
|
|
|
if let Some(callback) = self.mouse_down.as_mut() {
|
|
|
|
if bounds.contains_point(*position) {
|
2021-05-28 22:25:15 +00:00
|
|
|
return callback(cx);
|
2021-03-22 02:54:23 +00:00
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
2021-03-22 02:54:23 +00:00
|
|
|
false
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
2021-03-22 02:54:23 +00:00
|
|
|
_ => false,
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-07 05:50:13 +00:00
|
|
|
|
|
|
|
fn debug(
|
|
|
|
&self,
|
|
|
|
_: RectF,
|
|
|
|
_: &Self::LayoutState,
|
|
|
|
_: &Self::PaintState,
|
2021-05-28 22:25:15 +00:00
|
|
|
cx: &DebugContext,
|
2021-04-07 05:50:13 +00:00
|
|
|
) -> serde_json::Value {
|
|
|
|
json!({
|
|
|
|
"type": "EventHandler",
|
2021-05-28 22:25:15 +00:00
|
|
|
"child": self.child.debug(cx),
|
2021-04-07 05:50:13 +00:00
|
|
|
})
|
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|