1use core::cell::Cell;
12use std::rc::Rc;
13
14use egui::{Id, Rect, Ui, UiBuilder, Vec2};
15
16use crate::navigation::Direction;
17use crate::page::Outlet;
18
19#[derive(Debug, Clone, Copy, PartialEq)]
22pub struct Layer {
23 pub offset: Vec2,
25 pub opacity: f32,
26}
27
28impl Layer {
29 pub const VISIBLE: Self = Self {
30 offset: Vec2::ZERO,
31 opacity: 1.0,
32 };
33}
34
35#[derive(Debug, Clone, Copy)]
37pub struct TransitionFrame {
38 pub t: f32,
40 pub direction: Direction,
41 pub size: Vec2,
43}
44
45type Effect = Rc<dyn Fn(&TransitionFrame) -> (Layer, Layer)>;
46
47#[derive(Clone)]
62pub struct ViewTransition {
63 duration: f32,
64 effect: Effect,
65 reduced_motion: Option<bool>,
66}
67
68impl ViewTransition {
69 pub fn fade() -> Self {
71 Self::custom(|frame| {
72 let t = ease_in_out(frame.t);
73 (
74 Layer {
75 opacity: 1.0 - t,
76 ..Layer::VISIBLE
77 },
78 Layer {
79 opacity: t,
80 ..Layer::VISIBLE
81 },
82 )
83 })
84 .duration(0.2)
85 }
86
87 pub fn slide() -> Self {
90 Self::slide_by(30.0)
91 }
92
93 pub fn slide_by(distance: f32) -> Self {
95 Self::custom(move |frame| {
96 let shift = distance * frame.direction.sign();
97 let t = ease_out(frame.t);
98 let old = Layer {
99 opacity: 1.0 - (frame.t / 0.3).min(1.0),
101 offset: Vec2::new(-shift * t, 0.0),
102 };
103 let new = Layer {
104 opacity: ((frame.t - 0.3) / 0.7).clamp(0.0, 1.0),
106 offset: Vec2::new(shift * (1.0 - t), 0.0),
107 };
108 (old, new)
109 })
110 .duration(0.3)
111 }
112
113 pub fn push() -> Self {
115 Self::custom(|frame| {
116 let shift = frame.size.x * frame.direction.sign();
117 let t = ease_in_out(frame.t);
118 (
119 Layer {
120 offset: Vec2::new(-shift * t, 0.0),
121 opacity: if shift == 0.0 { 1.0 - t } else { 1.0 },
122 },
123 Layer {
124 offset: Vec2::new(shift * (1.0 - t), 0.0),
125 opacity: if shift == 0.0 { t } else { 1.0 },
126 },
127 )
128 })
129 .duration(0.35)
130 }
131
132 pub fn custom(effect: impl Fn(&TransitionFrame) -> (Layer, Layer) + 'static) -> Self {
134 Self {
135 duration: 0.3,
136 effect: Rc::new(effect),
137 reduced_motion: None,
138 }
139 }
140
141 pub fn duration(mut self, seconds: f32) -> Self {
143 self.duration = seconds;
144 self
145 }
146
147 pub fn reduced_motion(mut self, reduced: bool) -> Self {
150 self.reduced_motion = Some(reduced);
151 self
152 }
153
154 pub fn show(&self, ui: &mut Ui, mut outlet: Outlet<'_>) {
157 outlet.uses_transitions.set(true);
158 let rect = ui.available_rect_before_wrap();
159 let outlet_id = ui.id().with("egui_kit_outlet");
160
161 let frame = outlet.transition.as_deref().and_then(|transition| {
162 let state = transition.state();
163 let now = ui.input(|i| i.time);
164 let started = state.started.get().unwrap_or_else(|| {
165 state.started.set(Some(now));
166 now
167 });
168 state.claimed.set(true);
169 let t = if self.duration > 0.0 {
170 ((now - started) as f32 / self.duration).clamp(0.0, 1.0)
171 } else {
172 1.0
173 };
174 if t >= 1.0 {
175 state.done.set(true);
176 return None;
177 }
178 Some(TransitionFrame {
179 t,
180 direction: transition.direction(),
181 size: rect.size(),
182 })
183 });
184
185 let Some(frame) = frame else {
186 let mut page = ui.new_child(UiBuilder::new().id(outlet_id).max_rect(rect));
188 outlet.show(&mut page);
189 ui.advance_cursor_after_rect(page.min_rect());
190 return;
191 };
192
193 ui.ctx().request_repaint();
194 let (mut old, mut new) = (self.effect)(&frame);
195 if self.reduced_motion.unwrap_or_else(prefers_reduced_motion) {
196 old.offset = Vec2::ZERO;
197 new.offset = Vec2::ZERO;
198 }
199 let clip = rect.intersect(ui.clip_rect());
200
201 let mut previous_ui = layer_ui(ui, outlet_id.with("previous"), rect, clip, old);
203 if let Some(transition) = outlet.transition.as_deref_mut() {
204 transition.render_previous(&mut previous_ui);
205 }
206
207 let mut page = layer_ui(ui, outlet_id, rect, clip, new);
208 outlet.show(&mut page);
209 ui.advance_cursor_after_rect(page.min_rect().translate(-new.offset));
210 }
211}
212
213fn layer_ui(ui: &mut Ui, id: Id, rect: Rect, clip: Rect, layer: Layer) -> Ui {
214 let mut child = ui.new_child(
215 UiBuilder::new()
216 .id(id)
217 .max_rect(rect.translate(layer.offset)),
218 );
219 child.set_clip_rect(clip);
220 child.multiply_opacity(layer.opacity.clamp(0.0, 1.0));
221 child
222}
223
224fn ease_out(t: f32) -> f32 {
225 1.0 - (1.0 - t).powi(3)
226}
227
228fn ease_in_out(t: f32) -> f32 {
229 if t < 0.5 {
230 4.0 * t * t * t
231 } else {
232 1.0 - (-2.0 * t + 2.0).powi(3) / 2.0
233 }
234}
235
236pub fn prefers_reduced_motion() -> bool {
239 #[cfg(target_arch = "wasm32")]
240 {
241 web_sys::window()
242 .and_then(|window| {
243 window
244 .match_media("(prefers-reduced-motion: reduce)")
245 .ok()
246 .flatten()
247 })
248 .is_some_and(|query| query.matches())
249 }
250 #[cfg(not(target_arch = "wasm32"))]
251 {
252 false
253 }
254}
255
256#[derive(Default)]
258pub(crate) struct TransitionState {
259 pub(crate) started: Cell<Option<f64>>,
260 pub(crate) claimed: Cell<bool>,
262 pub(crate) done: Cell<bool>,
263}
264
265pub(crate) struct OutletTransition<'a> {
267 pub(crate) previous: &'a mut dyn FnMut(&mut Ui),
268 pub(crate) state: &'a TransitionState,
269 pub(crate) direction: Direction,
270}
271
272pub(crate) trait TransitionHandle {
274 fn render_previous(&mut self, ui: &mut Ui);
275 fn state(&self) -> &TransitionState;
276 fn direction(&self) -> Direction;
277}
278
279impl TransitionHandle for OutletTransition<'_> {
280 fn render_previous(&mut self, ui: &mut Ui) {
281 (self.previous)(ui);
282 }
283
284 fn state(&self) -> &TransitionState {
285 self.state
286 }
287
288 fn direction(&self) -> Direction {
289 self.direction
290 }
291}