ori_core/views/
animate.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
use crate::{
    context::{BuildCx, DrawCx, EventCx, LayoutCx, RebuildCx},
    event::Event,
    layout::{Size, Space},
    transition::Transition,
    view::View,
};

/// Create a new [`Animate`].
pub fn animate<T, V, S>(
    animate: impl FnMut(&mut S, &mut EventCx, &mut T, &Event) -> Option<V> + 'static,
) -> Animate<T, V, S> {
    Animate::new(animate)
}

/// Animate a view when hovered changes.
pub fn transition_hovered<T, V>(
    transition: Transition,
    mut view: impl FnMut(&mut EventCx, &mut T, f32) -> V + 'static,
) -> Animate<T, V, f32> {
    let mut built = false;

    animate(move |t: &mut f32, cx, data: &mut T, event| {
        if cx.is_hovered() || cx.has_hovered_changed() {
            cx.animate();
        }

        if let Event::Animate(dt) = event {
            if transition.step(t, cx.is_hovered() || cx.has_hovered(), *dt) {
                cx.animate();
                return Some(view(cx, data, transition.get(*t)));
            }
        }

        if !built {
            built = true;
            Some(view(cx, data, transition.get(*t)))
        } else {
            None
        }
    })
}

/// Animate a view when active changes.
pub fn transition_active<T, V>(
    transition: Transition,
    mut view: impl FnMut(&mut EventCx, &mut T, f32) -> V + 'static,
) -> Animate<T, V, f32> {
    let mut built = false;

    animate(move |t: &mut f32, cx, data: &mut T, event| {
        if cx.active_changed() || cx.has_active_changed() {
            cx.animate();
        }

        if let Event::Animate(dt) = event {
            if transition.step(t, cx.is_active() || cx.has_active(), *dt) {
                cx.animate();
                return Some(view(cx, data, transition.get(*t)));
            }
        }

        if !built {
            built = true;
            Some(view(cx, data, transition.get(*t)))
        } else {
            None
        }
    })
}

/// Animate a view when focused changes.
pub fn transition_focused<T, V>(
    transition: Transition,
    mut view: impl FnMut(&mut EventCx, &mut T, f32) -> V + 'static,
) -> Animate<T, V, f32> {
    let mut built = false;

    animate(move |t: &mut f32, cx, data: &mut T, event| {
        if cx.focused_changed() || cx.has_focused_changed() {
            cx.animate();
        }

        if let Event::Animate(dt) = event {
            if transition.step(t, cx.is_focused() || cx.has_focused(), *dt) {
                cx.animate();
                return Some(view(cx, data, transition.get(*t)));
            }
        }

        if !built {
            built = true;
            Some(view(cx, data, transition.get(*t)))
        } else {
            None
        }
    })
}

/// Animate a transition.
pub fn transition<T, V>(
    transition: Transition,
    active: bool,
    mut view: impl FnMut(&mut EventCx, &mut T, f32) -> V + 'static,
) -> Animate<T, V, f32> {
    let mut built = false;

    animate(move |t: &mut f32, cx, data: &mut T, event| {
        if let Event::Animate(dt) = event {
            if transition.step(t, active, *dt) {
                cx.animate();
                return Some(view(cx, data, transition.get(*t)));
            }
        }

        if !built {
            built = true;
            Some(view(cx, data, transition.get(*t)))
        } else {
            None
        }
    })
}

/// A view that animates.
///
/// For an example, see [`animate`](https://github.com/ori-ui/ori/blob/main/examples/animate.rs).
pub struct Animate<T, V, S = ()> {
    /// The animation callback.
    #[allow(clippy::type_complexity)]
    pub animate: Box<dyn FnMut(&mut S, &mut EventCx, &mut T, &Event) -> Option<V>>,
}

impl<T, V, S> Animate<T, V, S> {
    /// Create a new [`Animate`].
    pub fn new(
        animate: impl FnMut(&mut S, &mut EventCx, &mut T, &Event) -> Option<V> + 'static,
    ) -> Self {
        Self {
            animate: Box::new(animate),
        }
    }
}

#[doc(hidden)]
pub struct AnimateState<V: View<T>, S, T> {
    animate_state: S,
    view: Option<(V::State, V)>,
}

impl<T, V: View<T>, S: Default> View<T> for Animate<T, V, S> {
    type State = AnimateState<V, S, T>;

    fn build(&mut self, cx: &mut BuildCx, _data: &mut T) -> Self::State {
        cx.animate();

        AnimateState {
            animate_state: S::default(),
            view: None,
        }
    }

    fn rebuild(
        &mut self,
        _state: &mut Self::State,
        cx: &mut RebuildCx,
        _data: &mut T,
        _old: &Self,
    ) {
        cx.animate();
    }

    fn event(
        &mut self,
        state: &mut Self::State,
        cx: &mut EventCx,
        data: &mut T,
        event: &Event,
    ) -> bool {
        let new_view = (self.animate)(&mut state.animate_state, cx, data, event);

        match (state.view.as_mut(), new_view) {
            (None, None) => false,
            (None, Some(mut new_view)) => {
                let mut new_state = new_view.build(&mut cx.as_build_cx(), data);

                let handled = new_view.event(&mut new_state, cx, data, event);
                state.view = Some((new_state, new_view));

                handled
            }
            (Some((state, view)), None) => view.event(state, cx, data, event),
            (Some((state, old_view)), Some(mut new_view)) => {
                new_view.rebuild(state, &mut cx.as_rebuild_cx(), data, old_view);

                let handled = new_view.event(state, cx, data, event);
                *old_view = new_view;

                handled
            }
        }
    }

    fn layout(
        &mut self,
        state: &mut Self::State,
        cx: &mut LayoutCx,
        data: &mut T,
        space: Space,
    ) -> Size {
        match state.view {
            Some((ref mut view_state, ref mut view)) => view.layout(view_state, cx, data, space),
            None => space.min,
        }
    }

    fn draw(&mut self, state: &mut Self::State, cx: &mut DrawCx, data: &mut T) {
        if let Some((ref mut view_state, ref mut view)) = state.view {
            view.draw(view_state, cx, data);
        }
    }
}