Skip to main content

aura_anim_iced/behavior/
rule.rs

1use crate::{
2    AnimationTargetId, PropertyTransition, Timing,
3    behavior::TransitionValueKind,
4    property::{PropertySpec, PropertyValueKind},
5};
6
7/// Reusable animation behavior for value changes on one property.
8///
9/// A rule describes the property and timing independently from any concrete UI
10/// target. Bind it to a target to create a [`PropertyTransition`] tracker.
11#[derive(Debug, Clone, Copy, PartialEq)]
12pub struct BehaviorRule<K: PropertyValueKind> {
13    property: PropertySpec<K>,
14    timing: Timing,
15}
16
17impl<K: PropertyValueKind> BehaviorRule<K> {
18    /// Creates a reusable rule with default timing.
19    #[must_use]
20    pub fn new(property: PropertySpec<K>) -> Self {
21        Self {
22            property,
23            timing: Timing::default(),
24        }
25    }
26
27    /// Replaces the timing used by transitions created from this rule.
28    #[must_use]
29    pub const fn with_timing(mut self, timing: Timing) -> Self {
30        self.timing = timing;
31        self
32    }
33
34    /// Returns the property animated by this rule.
35    #[must_use]
36    pub const fn property(&self) -> PropertySpec<K> {
37        self.property
38    }
39
40    /// Returns the timing used by transitions created from this rule.
41    #[must_use]
42    pub const fn timing(&self) -> Timing {
43        self.timing
44    }
45
46    /// Creates a target-bound value change tracker from this rule.
47    #[must_use]
48    pub fn bind(self, target: AnimationTargetId) -> PropertyTransition<K>
49    where
50        K: TransitionValueKind,
51        K::Inner: Copy + PartialEq,
52    {
53        PropertyTransition::from_rule(target, &self)
54    }
55}