1use super::try_rect;
2use crate::{
3 geometry::vector::Vector2F, AfterLayoutContext, AppContext, Element, Event, EventContext,
4 LayoutContext, MutableAppContext, PaintContext, SizeConstraint,
5};
6use std::cell::RefCell;
7
8pub struct EventHandler {
9 child: Box<dyn Element>,
10 mouse_down: Option<RefCell<Box<dyn FnMut(&mut EventContext, &AppContext) -> bool>>>,
11 origin: Option<Vector2F>,
12}
13
14impl EventHandler {
15 pub fn new(child: Box<dyn Element>) -> Self {
16 Self {
17 child,
18 mouse_down: None,
19 origin: None,
20 }
21 }
22
23 pub fn on_mouse_down<F>(mut self, callback: F) -> Self
24 where
25 F: 'static + FnMut(&mut EventContext, &AppContext) -> bool,
26 {
27 self.mouse_down = Some(RefCell::new(Box::new(callback)));
28 self
29 }
30}
31
32impl Element for EventHandler {
33 fn layout(
34 &mut self,
35 constraint: SizeConstraint,
36 ctx: &mut LayoutContext,
37 app: &AppContext,
38 ) -> Vector2F {
39 self.child.layout(constraint, ctx, app)
40 }
41
42 fn after_layout(&mut self, ctx: &mut AfterLayoutContext, app: &mut MutableAppContext) {
43 self.child.after_layout(ctx, app);
44 }
45
46 fn paint(&mut self, origin: Vector2F, ctx: &mut PaintContext, app: &AppContext) {
47 self.origin = Some(origin);
48 self.child.paint(origin, ctx, app);
49 }
50
51 fn size(&self) -> Option<Vector2F> {
52 self.child.size()
53 }
54
55 fn dispatch_event(&self, event: &Event, ctx: &mut EventContext, app: &AppContext) -> bool {
56 match event {
57 Event::LeftMouseDown { position, .. } => {
58 if let Some(callback) = self.mouse_down.as_ref() {
59 let rect = try_rect(self.origin, self.size()).unwrap();
60 if rect.contains_point(*position) {
61 return callback.borrow_mut()(ctx, app);
62 }
63 }
64 false
65 }
66 _ => false,
67 }
68 }
69}