1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use std::any::Any;
use accesskit::Role;
use super::{View, BuildContext, LayoutContext, Id};


pub fn clickable<F, V>(_role: Role, on_click: F, view: V) -> EventHandler<F, V> {
    EventHandler::new(on_click, view)
}

pub struct EventHandler<F, V> {
    on_event: F,
    view: V,
}

impl<F, V> EventHandler<F, V> {
    pub fn new(on_event: F, view: V) -> Self {
        Self { on_event, view }
    }
}

impl<T, A, F, V> View<T, A> for EventHandler<F, V>
where
    V: View<T, A>,
    F: FnMut(&mut T),
{
    fn build(&mut self, cx: &mut BuildContext) -> Id {
        self.view.build(cx)
    }

    fn rebuild(&mut self, cx: &mut BuildContext, old: &mut Self) {
        self.view.rebuild(cx, &mut old.view)
    }

    fn layout(&mut self, cx: &mut LayoutContext, id: Id) {
        self.view.layout(cx, id)
    }

    fn paint(&mut self, taffy: &taffy::Taffy, canvas: &mut skia_safe::Canvas) {
        self.view.paint(taffy, canvas)
    }

    fn message(&mut self, state: &mut T, id_path: &[Id], message: &dyn Any) -> Option<A> {
        (self.on_event)(state);
        self.view.message(state, id_path, message)
    }
}