1use crate::component::ComponentId;
11use crate::geometry::PointF;
12use crate::prop::PropEnum;
13use crate::ui::Ui;
14
15#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
18pub enum Transition {
19 #[default]
20 None,
21 Fade,
22 SlideLeft,
23 SlideRight,
24 SlideUp,
25 SlideDown,
26}
27
28impl Transition {
29 pub const ALL: [Transition; 6] =
30 [Transition::None, Transition::Fade, Transition::SlideLeft, Transition::SlideRight, Transition::SlideUp, Transition::SlideDown];
31
32 pub fn reversed(self) -> Self {
34 match self {
35 Transition::SlideLeft => Transition::SlideRight,
36 Transition::SlideRight => Transition::SlideLeft,
37 Transition::SlideUp => Transition::SlideDown,
38 Transition::SlideDown => Transition::SlideUp,
39 other => other,
40 }
41 }
42}
43
44impl PropEnum for Transition {
45 const NAMES: &'static [&'static str] = &["none", "fade", "slide_left", "slide_right", "slide_up", "slide_down"];
46
47 fn from_index(index: usize) -> Option<Self> {
48 Transition::ALL.get(index).copied()
49 }
50
51 fn index(self) -> usize {
52 self as usize
53 }
54}
55
56pub const TRANSITION_MS: f32 = 250.0;
58
59#[derive(Clone, Copy, Debug)]
60pub(crate) struct Running {
61 from: Option<ComponentId>,
62 to: Option<ComponentId>,
63 kind: Transition,
64 start: f64,
65 duration: f64,
66 from_opacity: f32,
68 to_opacity: f32,
69 close_from: bool,
71}
72
73impl Ui {
74 pub fn transition(&mut self, from: Option<ComponentId>, to: Option<ComponentId>, kind: Transition, duration_ms: f32) {
77 self.start_transition(from, to, kind, duration_ms, false);
78 }
79
80 pub(crate) fn start_transition(&mut self, from: Option<ComponentId>, to: Option<ComponentId>, kind: Transition, duration_ms: f32, close_from: bool) {
81 let duration_ms = if self.reduced_motion() { 0.0 } else { duration_ms };
83 let pending: Vec<Running> = self
85 .transitions
86 .iter()
87 .filter(|r| [r.from, r.to].iter().flatten().any(|c| Some(*c) == from || Some(*c) == to))
88 .copied()
89 .collect();
90 for r in pending {
91 self.finish_transition(r);
92 }
93 self.transitions.retain(|r| ![r.from, r.to].iter().flatten().any(|c| Some(*c) == from || Some(*c) == to));
94 if let Some(t) = to {
95 if let Some(cb) = self.control_mut(t) {
96 cb.set_visible(true);
97 }
98 }
99 let opacity = |ui: &Ui, id: Option<ComponentId>| id.and_then(|c| ui.control(c)).map_or(1.0, |cb| cb.opacity());
100 let running = Running {
101 from,
102 to,
103 kind,
104 start: self.time,
105 duration: (duration_ms.max(0.0) / 1000.0) as f64,
106 from_opacity: opacity(self, from),
107 to_opacity: opacity(self, to),
108 close_from,
109 };
110 if kind == Transition::None || running.duration <= 0.0 {
111 self.finish_transition(running);
112 return;
113 }
114 self.apply_transition(&running, 0.0);
115 self.transitions.push(running);
116 self.invalidate();
117 self.request_repaint();
118 }
119
120 pub(crate) fn transitions_entering(&self, id: ComponentId) -> bool {
122 self.transitions.iter().any(|r| r.to == Some(id))
123 }
124
125 pub fn in_transition(&self, id: ComponentId) -> bool {
127 self.transitions.iter().any(|r| r.from == Some(id) || r.to == Some(id))
128 }
129
130 pub(crate) fn tick_transitions(&mut self) {
131 if self.transitions.is_empty() {
132 return;
133 }
134 let now = self.time;
135 let running = std::mem::take(&mut self.transitions);
136 let mut keep = Vec::new();
137 for r in running {
138 let t = ((now - r.start) / r.duration).clamp(0.0, 1.0) as f32;
139 if t >= 1.0 {
140 self.finish_transition(r);
141 } else {
142 self.apply_transition(&r, crate::anim::Easing::OutCubic.apply(t));
143 keep.push(r);
144 }
145 }
146 keep.append(&mut self.transitions);
148 self.transitions = keep;
149 }
153
154 fn slide_vector(&self, kind: Transition, id: ComponentId) -> PointF {
156 let size = self.control(id).map(|cb| cb.bounds().size()).unwrap_or_default();
157 match kind {
158 Transition::SlideLeft => PointF::new(size.width, 0.0),
159 Transition::SlideRight => PointF::new(-size.width, 0.0),
160 Transition::SlideUp => PointF::new(0.0, size.height),
161 Transition::SlideDown => PointF::new(0.0, -size.height),
162 _ => PointF::ZERO,
163 }
164 }
165
166 fn apply_transition(&mut self, r: &Running, p: f32) {
167 match r.kind {
168 Transition::Fade => {
169 if let Some(cb) = r.to.and_then(|c| self.cb_mut(c)) {
170 cb.set_opacity(r.to_opacity * p);
171 }
172 if let Some(cb) = r.from.and_then(|c| self.cb_mut(c)) {
173 cb.set_opacity(r.from_opacity * (1.0 - p));
174 }
175 }
176 Transition::None => return,
177 kind => {
178 if let Some(to) = r.to {
179 let v = self.slide_vector(kind, to);
180 if let Some(cb) = self.cb_mut(to) {
181 cb.set_translate(PointF::new(v.x * (1.0 - p), v.y * (1.0 - p)));
182 }
183 }
184 if let Some(from) = r.from {
185 let v = self.slide_vector(kind, from);
186 if let Some(cb) = self.cb_mut(from) {
187 cb.set_translate(PointF::new(-v.x * p, -v.y * p));
188 }
189 }
190 }
191 }
192 for id in [r.from, r.to].into_iter().flatten() {
196 self.damage_placement(id);
197 }
198 }
199
200 fn finish_transition(&mut self, r: Running) {
201 for id in [r.from, r.to].into_iter().flatten() {
205 if let Some(key) = self.entry(id).and_then(|e| e.visual.borrow_mut().layer_key.take()) {
206 self.dead_layers.get_mut().push(key);
207 }
208 }
209 if let Some(cb) = r.to.and_then(|c| self.control_mut(c)) {
210 cb.set_translate(PointF::ZERO);
211 cb.set_opacity(r.to_opacity);
212 cb.set_visible(true);
213 }
214 if let Some(from) = r.from {
215 if let Some(cb) = self.control_mut(from) {
216 cb.set_translate(PointF::ZERO);
217 cb.set_opacity(r.from_opacity);
218 }
219 if r.close_from {
220 self.closing_now = Some(from);
221 self.close(from);
222 self.closing_now = None;
223 } else if let Some(cb) = self.control_mut(from) {
224 cb.set_visible(false);
225 }
226 }
227 self.invalidate();
228 self.request_repaint();
229 }
230}