use core::cell::Cell;
use std::rc::Rc;
use egui::{Id, Rect, Ui, UiBuilder, Vec2};
use crate::navigation::{Direction, Navigation};
use crate::page::Outlet;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Layer {
pub offset: Vec2,
pub opacity: f32,
}
impl Layer {
pub const VISIBLE: Self = Self {
offset: Vec2::ZERO,
opacity: 1.0,
};
}
#[derive(Debug, Clone)]
pub struct TransitionFrame {
pub t: f32,
pub direction: Direction,
pub size: Vec2,
pub navigation: Navigation,
}
type Effect = Rc<dyn Fn(&TransitionFrame) -> (Layer, Layer)>;
#[derive(Clone)]
pub struct ViewTransition {
duration: f32,
effect: Effect,
reduced_motion: Option<bool>,
}
impl ViewTransition {
pub fn fade() -> Self {
Self::custom(|frame| {
let t = ease_in_out(frame.t);
(
Layer {
opacity: 1.0 - t,
..Layer::VISIBLE
},
Layer {
opacity: t,
..Layer::VISIBLE
},
)
})
.duration(0.2)
}
pub fn slide() -> Self {
Self::slide_by(30.0)
}
pub fn slide_by(distance: f32) -> Self {
Self::custom(move |frame| {
let shift = distance * frame.direction.sign();
let t = ease_out(frame.t);
let old = Layer {
opacity: 1.0 - (frame.t / 0.3).min(1.0),
offset: Vec2::new(-shift * t, 0.0),
};
let new = Layer {
opacity: ((frame.t - 0.3) / 0.7).clamp(0.0, 1.0),
offset: Vec2::new(shift * (1.0 - t), 0.0),
};
(old, new)
})
.duration(0.3)
}
pub fn push() -> Self {
Self::custom(|frame| {
let shift = frame.size.x * frame.direction.sign();
let t = ease_in_out(frame.t);
(
Layer {
offset: Vec2::new(-shift * t, 0.0),
opacity: if shift == 0.0 { 1.0 - t } else { 1.0 },
},
Layer {
offset: Vec2::new(shift * (1.0 - t), 0.0),
opacity: if shift == 0.0 { t } else { 1.0 },
},
)
})
.duration(0.35)
}
pub fn custom(effect: impl Fn(&TransitionFrame) -> (Layer, Layer) + 'static) -> Self {
Self {
duration: 0.3,
effect: Rc::new(effect),
reduced_motion: None,
}
}
pub fn duration(mut self, seconds: f32) -> Self {
self.duration = seconds;
self
}
pub fn reduced_motion(mut self, reduced: bool) -> Self {
self.reduced_motion = Some(reduced);
self
}
pub fn show(&self, ui: &mut Ui, mut outlet: Outlet<'_>) {
outlet.uses_transitions.set(true);
let rect = ui.available_rect_before_wrap();
let outlet_id = ui.id().with("egui_kit_outlet");
let frame = outlet.transition.as_deref().and_then(|transition| {
let state = transition.state();
let now = ui.input(|i| i.time);
let started = state.started.get().unwrap_or_else(|| {
state.started.set(Some(now));
now
});
state.claimed.set(true);
let t = if self.duration > 0.0 {
((now - started) as f32 / self.duration).clamp(0.0, 1.0)
} else {
1.0
};
if t >= 1.0 {
state.done.set(true);
return None;
}
Some(TransitionFrame {
t,
direction: transition.direction(),
size: rect.size(),
navigation: transition.navigation().clone(),
})
});
let Some(frame) = frame else {
let mut page = ui.new_child(UiBuilder::new().id(outlet_id).max_rect(rect));
outlet.show(&mut page);
ui.advance_cursor_after_rect(page.min_rect());
return;
};
ui.ctx().request_repaint();
let (mut old, mut new) = (self.effect)(&frame);
if self.reduced_motion.unwrap_or_else(prefers_reduced_motion) {
old.offset = Vec2::ZERO;
new.offset = Vec2::ZERO;
}
let clip = rect.intersect(ui.clip_rect());
let mut previous_ui = layer_ui(ui, outlet_id.with("previous"), rect, clip, old);
if let Some(transition) = outlet.transition.as_deref_mut() {
transition.render_previous(&mut previous_ui);
}
let mut page = layer_ui(ui, outlet_id, rect, clip, new);
outlet.show(&mut page);
ui.advance_cursor_after_rect(page.min_rect().translate(-new.offset));
}
}
fn layer_ui(ui: &mut Ui, id: Id, rect: Rect, clip: Rect, layer: Layer) -> Ui {
let mut child = ui.new_child(
UiBuilder::new()
.id(id)
.max_rect(rect.translate(layer.offset)),
);
child.set_clip_rect(clip);
child.multiply_opacity(layer.opacity.clamp(0.0, 1.0));
child
}
fn ease_out(t: f32) -> f32 {
1.0 - (1.0 - t).powi(3)
}
fn ease_in_out(t: f32) -> f32 {
if t < 0.5 {
4.0 * t * t * t
} else {
1.0 - (-2.0 * t + 2.0).powi(3) / 2.0
}
}
pub fn prefers_reduced_motion() -> bool {
#[cfg(target_arch = "wasm32")]
{
web_sys::window()
.and_then(|window| {
window
.match_media("(prefers-reduced-motion: reduce)")
.ok()
.flatten()
})
.is_some_and(|query| query.matches())
}
#[cfg(not(target_arch = "wasm32"))]
{
false
}
}
#[derive(Default)]
pub(crate) struct TransitionState {
pub(crate) started: Cell<Option<f64>>,
pub(crate) claimed: Cell<bool>,
pub(crate) done: Cell<bool>,
}
pub(crate) struct OutletTransition<'a> {
pub(crate) previous: &'a mut dyn FnMut(&mut Ui),
pub(crate) state: &'a TransitionState,
pub(crate) direction: Direction,
pub(crate) navigation: &'a Navigation,
}
pub(crate) trait TransitionHandle {
fn render_previous(&mut self, ui: &mut Ui);
fn state(&self) -> &TransitionState;
fn direction(&self) -> Direction;
fn navigation(&self) -> &Navigation;
}
impl TransitionHandle for OutletTransition<'_> {
fn render_previous(&mut self, ui: &mut Ui) {
(self.previous)(ui);
}
fn state(&self) -> &TransitionState {
self.state
}
fn direction(&self) -> Direction {
self.direction
}
fn navigation(&self) -> &Navigation {
self.navigation
}
}