Skip to main content

xen_animation/
transition.rs

1// SPDX-License-Identifier: Apache-2.0
2use std::time::Duration;
3use super::Easing;
4
5/// Describes how a single animated value should move from its current
6/// value to a new target: how long it takes, how long to wait before
7/// starting, and the easing curve applied over that duration.
8#[derive(Clone, Copy, Debug, PartialEq)]
9pub struct Transition {
10    /// How long the transition takes once it starts, excluding `delay`.
11    pub duration: Duration,
12    /// How long to wait after retargeting before the transition begins.
13    pub delay: Duration,
14    /// The timing curve applied to elapsed progress within `duration`.
15    pub easing: Easing,
16}
17
18impl Transition {
19    /// Creates a transition with the given duration, no delay, and linear easing.
20    pub const fn new(duration: Duration) -> Self {
21        Self { duration, delay: Duration::ZERO, easing: Easing::Linear }
22    }
23
24    /// Returns a copy of this transition with `delay` set.
25    pub const fn delay(mut self, delay: Duration) -> Self {
26        self.delay = delay;
27        self
28    }
29
30    /// Returns a copy of this transition with `easing` set.
31    pub const fn easing(mut self, easing: Easing) -> Self {
32        self.easing = easing;
33        self
34    }
35}
36
37impl Default for Transition {
38    /// 200ms, no delay, linear easing.
39    fn default() -> Self {
40        Self::new(Duration::from_millis(200))
41    }
42}
43
44/// Per-group transition overrides layered on top of a base `Transition`.
45/// Each field takes priority over the general transition for its own
46/// property group, so e.g. colors and transforms can animate with
47/// different durations/easings while sharing one base transition.
48#[derive(Clone, Copy, Debug, Default, PartialEq)]
49pub struct TransitionOverrides {
50    /// Override applied to color-valued properties.
51    pub colors: Option<Transition>,
52    /// Override applied to opacity.
53    pub opacity: Option<Transition>,
54    /// Override applied to box-shadow (reserved for future use).
55    pub shadow: Option<Transition>,
56    /// Override applied to transform-like properties (scale, etc).
57    pub transform: Option<Transition>,
58    /// Override applied to box-model properties (size, padding, margin, etc).
59    pub box_model: Option<Transition>,
60}
61
62impl TransitionOverrides {
63    /// Merges `patch` on top of `self`, field by field - each field in
64    /// `patch` takes precedence when set, otherwise `self`'s value is kept.
65    pub fn overlay(&self, patch: &Self) -> Self {
66        Self {
67            colors: patch.colors.or(self.colors),
68            opacity: patch.opacity.or(self.opacity),
69            shadow: patch.shadow.or(self.shadow),
70            transform: patch.transform.or(self.transform),
71            box_model: patch.box_model.or(self.box_model),
72        }
73    }
74}