ori_core/view/
any.rs

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use std::any::Any;

use crate::{
    context::{BuildCx, DrawCx, EventCx, LayoutCx, RebuildCx},
    event::Event,
    layout::{Size, Space},
};

use super::View;

/// The state of a [`BoxedView`].
pub type AnyState = Box<dyn Any>;
/// A boxed dynamic view.
pub type BoxedView<T> = Box<dyn AnyView<T>>;

/// Create a new [`BoxedView`].
///
/// This is useful for when you need to create a view that needs
/// to change its type dynamically.
///
/// ```compile_fail
/// # use ori_core::{views::*, view::View};
/// // this will fail to compile because the type of each branch is different
/// fn ui(data: &mut bool) -> impl View<bool> {
///     if *data {
///         button(text("True"))
///     } else {
///         text("False")
///     }
/// }
/// ```
///
/// ```no_run
/// # use ori_core::{views::*, view::{View, any}};
/// // whereas this will compile using `any`
/// fn ui(data: &mut bool) -> impl View<bool> {
///     if *data {
///         any(button(text("True")))
///     } else {
///         any(text("False"))
///     }
/// }
/// ```
pub fn any<'a, T>(view: impl AnyView<T> + 'a) -> Box<dyn AnyView<T> + 'a> {
    Box::new(view)
}

/// A view that supports dynamic dispatch.
pub trait AnyView<T> {
    /// Get a reference to the underlying [`Any`] object.
    fn as_any(&self) -> &dyn Any;

    /// Build the view.
    fn dyn_build(&mut self, cx: &mut BuildCx, data: &mut T) -> Box<dyn Any>;

    /// Rebuild the view.
    fn dyn_rebuild(
        &mut self,
        state: &mut AnyState,
        cx: &mut RebuildCx,
        data: &mut T,
        old: &dyn AnyView<T>,
    );

    /// Handle an event.
    fn dyn_event(
        &mut self,
        state: &mut AnyState,
        cx: &mut EventCx,
        data: &mut T,
        event: &Event,
    ) -> bool;

    /// Calculate the layout.
    fn dyn_layout(
        &mut self,
        state: &mut AnyState,
        cx: &mut LayoutCx,
        data: &mut T,
        space: Space,
    ) -> Size;

    /// Draw the view.
    fn dyn_draw(&mut self, state: &mut AnyState, cx: &mut DrawCx, data: &mut T);
}

impl<T, V> AnyView<T> for V
where
    V: View<T> + Any,
    V::State: Any,
{
    fn as_any(&self) -> &dyn Any {
        self
    }

    fn dyn_build(&mut self, cx: &mut BuildCx, data: &mut T) -> Box<dyn Any> {
        Box::new(self.build(cx, data))
    }

    fn dyn_rebuild(
        &mut self,
        state: &mut AnyState,
        cx: &mut RebuildCx,
        data: &mut T,
        old: &dyn AnyView<T>,
    ) {
        if let Some(old) = old.as_any().downcast_ref::<V>() {
            match state.downcast_mut::<V::State>() {
                Some(state) => self.rebuild(state, cx, data, old),
                None => eprintln!("Failed to downcast state"),
            }
        } else {
            *cx.view_state = Default::default();
            *state = self.dyn_build(&mut cx.as_build_cx(), data);
        }
    }

    fn dyn_event(
        &mut self,
        state: &mut AnyState,
        cx: &mut EventCx,
        data: &mut T,
        event: &Event,
    ) -> bool {
        match state.downcast_mut::<V::State>() {
            Some(state) => self.event(state, cx, data, event),
            None => false,
        }
    }

    fn dyn_layout(
        &mut self,
        state: &mut AnyState,
        cx: &mut LayoutCx,
        data: &mut T,
        space: Space,
    ) -> Size {
        match state.downcast_mut::<V::State>() {
            Some(state) => self.layout(state, cx, data, space),
            None => space.min,
        }
    }

    fn dyn_draw(&mut self, state: &mut AnyState, cx: &mut DrawCx, data: &mut T) {
        match state.downcast_mut::<V::State>() {
            Some(state) => self.draw(state, cx, data),
            None => eprintln!("Failed to downcast state"),
        }
    }
}

impl<T> View<T> for BoxedView<T> {
    type State = AnyState;

    fn build(&mut self, cx: &mut BuildCx, data: &mut T) -> Self::State {
        self.as_mut().dyn_build(cx, data)
    }

    fn rebuild(&mut self, state: &mut Self::State, cx: &mut RebuildCx, data: &mut T, old: &Self) {
        self.as_mut().dyn_rebuild(state, cx, data, old.as_ref());
    }

    fn event(
        &mut self,
        state: &mut Self::State,
        cx: &mut EventCx,
        data: &mut T,
        event: &Event,
    ) -> bool {
        self.as_mut().dyn_event(state, cx, data, event)
    }

    fn layout(
        &mut self,
        state: &mut Self::State,
        cx: &mut LayoutCx,
        data: &mut T,
        space: Space,
    ) -> Size {
        self.as_mut().dyn_layout(state, cx, data, space)
    }

    fn draw(&mut self, state: &mut Self::State, cx: &mut DrawCx, data: &mut T) {
        self.as_mut().dyn_draw(state, cx, data);
    }
}