//! The sheet's colour transitions: a node's background and border ease to //! a new colour over its `transition`'s duration, instead of jumping in one //! frame. The press itself does not ease: the sheet's `:active` rules set //! `transition-duration: 0ms`, and a transition takes the duration of the //! state it goes *to*, as in the browser. So a tap shows at once and its //! release fades back. //! //! The restyle (`tree.rs`) writes the target colours as before; this runs //! after it, notices the write through change detection, and puts the //! colour shown so far back for the frames the fade lasts. Its own writes //! bypass change detection, so the next change it sees is the restyle's. use bevy::prelude::*; use crate::tree::UiTree; /// A node's colours on their way from `from` to `to`. #[derive(Component)] pub(crate) struct Fade { from: Colours, to: Colours, start: f32, secs: f32, } #[derive(Clone, Copy, PartialEq)] struct Colours { bg: Color, border: [Color; 4], } impl Colours { fn of(bg: &BackgroundColor, border: Option<&BorderColor>) -> Self { let border = border.map_or([Color::NONE; 4], |b| [b.top, b.right, b.bottom, b.left]); Colours { bg: bg.0, border } } fn mix(&self, other: &Colours, t: f32) -> Colours { let m = |a: Color, b: Color| a.mix(&b, t); Colours { bg: m(self.bg, other.bg), border: [ m(self.border[0], other.border[0]), m(self.border[1], other.border[1]), m(self.border[2], other.border[2]), m(self.border[3], other.border[3]), ], } } } impl Fade { fn shown(&self, now: f32) -> Colours { if self.secs <= 0.0 { return self.to; } let t = ((now - self.start) / self.secs).clamp(0.0, 1.0); self.from.mix(&self.to, ease(t)) } fn running(&self, now: f32) -> bool { self.secs > 0.0 && now < self.start + self.secs } } /// Close to the sheet's `cubic-bezier(0.2, 0, 0, 1)`: fast out, slow in. fn ease(t: f32) -> f32 { 1.0 - (1.0 - t).powi(3) } /// The seconds a node's colour change takes, from its computed /// `transition-duration` or `transition`: the first time in it, or none. fn duration(tree: &UiTree, entity: Entity) -> f32 { let Some(c) = tree .node_of(entity) .and_then(|id| tree.nodes.get(&id)) .and_then(|n| n.computed.as_ref()) else { return 0.0; }; let value = c .other .get("transition-duration") .or_else(|| c.other.get("transition")); value.map_or(0.0, |v| { // Only a transition that names a colour (or `all`) fades one. if c.other.get("transition-duration").is_none() && !["background", "border", "all"] .iter() .any(|p| v.contains(p)) { return 0.0; } first_time(v) }) } /// The first `ms` or `s` in a value. fn first_time(v: &str) -> f32 { for word in v.split(|ch: char| ch.is_whitespace() || ch == ',') { if let Some(ms) = word.strip_suffix("ms").and_then(|n| n.parse::().ok()) { return ms / 1000.0; } if let Some(s) = word.strip_suffix('s').and_then(|n| n.parse::().ok()) { return s; } } // An unresolved `var(--m-duration)`: the library's own. if v.contains("var(--m-duration)") { return 0.14; } 0.0 } pub(crate) fn fade( mut commands: Commands, time: Res