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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use super::{Id, View};
use std::{any::Any, marker::PhantomData};

pub struct Adapt<T1, A1, T2, A2, V, F = fn(&mut T1, AdaptThunk<T2, A2, V>) -> Option<A1>> {
    f: F,
    child: V,
    _marker: PhantomData<fn() -> (T1, A1, T2, A2)>,
}

pub struct AdaptThunk<'a, T2, A2, V: View<T2, A2>> {
    child: &'a mut V,
    id_path: &'a [Id],
    message: &'a dyn Any,
    _marker: PhantomData<(T2, A2)>,
}

impl<T1, A1, T2, A2, V, F> Adapt<T1, A1, T2, A2, V, F>
where
    V: View<T2, A2>,
    F: Fn(&mut T1, AdaptThunk<T2, A2, V>) -> Option<A1>,
{
    pub fn new(f: F, child: V) -> Self {
        Adapt {
            f,
            child,
            _marker: Default::default(),
        }
    }
}

impl<'a, T2, A2, V: View<T2, A2>> AdaptThunk<'a, T2, A2, V> {
    pub fn call(self, state: &mut T2) -> Option<A2> {
        self.child.message(state, self.id_path, self.message)
    }
}

impl<T1, A1, T2, A2, V, F> View<T1, A1> for Adapt<T1, A1, T2, A2, V, F>
where
    V: View<T2, A2>,
    F: Fn(&mut T1, AdaptThunk<T2, A2, V>) -> Option<A1>,
{
    fn build(&mut self, cx: &mut super::BuildContext) -> Id {
        self.child.build(cx)
    }

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

    fn message(&mut self, state: &mut T1, id_path: &[Id], message: &dyn Any) -> Option<A1> {
        let thunk = AdaptThunk {
            child: &mut self.child,
            id_path,
            message,
            _marker: PhantomData,
        };
        (self.f)(state, thunk)
    }

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

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