Skip to main content

mln_style/property/
transition.rs

1use std::sync::Arc;
2use std::time::Duration;
3
4use crate::collections::VecSet;
5use crate::conversion::{Converter, Convertible, Error};
6use crate::expression::GlobalStateMap;
7use crate::feature::{Value, ValueObject};
8use crate::time::Instant;
9
10#[derive(Clone, Copy, Debug, PartialEq, mln_wire::Wire)]
11pub struct TransitionOptions {
12    pub duration: Option<Duration>,
13    pub delay: Option<Duration>,
14    pub enable_placement_transitions: bool,
15}
16
17impl Default for TransitionOptions {
18    fn default() -> Self {
19        TransitionOptions::new(None, None, true)
20    }
21}
22
23impl TransitionOptions {
24    #[must_use]
25    pub fn new(duration: Option<Duration>, delay: Option<Duration>, enable_placement_transitions: bool) -> Self {
26        TransitionOptions { duration, delay, enable_placement_transitions }
27    }
28
29    #[must_use]
30    pub(crate) fn with_duration(duration: Duration) -> Self {
31        TransitionOptions::new(Some(duration), None, true)
32    }
33
34    #[must_use]
35    pub(crate) fn reverse_merge(&self, defaults: &TransitionOptions) -> TransitionOptions {
36        TransitionOptions::new(
37            if self.duration.is_some() { self.duration } else { defaults.duration },
38            if self.delay.is_some() { self.delay } else { defaults.delay },
39            self.enable_placement_transitions,
40        )
41    }
42
43    #[must_use]
44    pub(crate) fn is_defined(&self) -> bool {
45        self.duration.is_some() || self.delay.is_some()
46    }
47
48    #[must_use]
49    pub(crate) fn serialize(&self) -> Value {
50        let mut result = ValueObject::new();
51        // Whole milliseconds, as integers.
52        let millis = |duration: Duration| Value::Int(i64::try_from(duration.as_millis()).unwrap_or(i64::MAX));
53        if let Some(duration) = self.duration {
54            result.insert("duration".to_owned(), millis(duration));
55        }
56        if let Some(delay) = self.delay {
57            result.insert("delay".to_owned(), millis(delay));
58        }
59        Value::Object(result)
60    }
61}
62
63/// A change of the style's global state, across which the values that read it
64/// transition from what they showed before.
65#[derive(Clone, Debug)]
66pub struct GlobalStateChange {
67    /// The state before the change, which transitions already running keep reading.
68    pub prior_state: Arc<GlobalStateMap>,
69    /// The keys whose values differ between the prior and the current state.
70    pub changed_keys: Arc<VecSet<String>>,
71    /// Whether the global state is all that changed, so only the values that
72    /// read a changed key start a new transition.
73    pub state_only: bool,
74}
75
76#[derive(Clone, Debug, Default)]
77pub struct TransitionParameters {
78    /// When the transitions start; `None` starts them ended.
79    pub now: Option<Instant>,
80    pub transition: TransitionOptions,
81    /// Set when the global state changed since the last transition.
82    pub global_state_change: Option<GlobalStateChange>,
83}
84
85impl TransitionParameters {
86    /// Parameters without a global-state change.
87    #[must_use]
88    pub fn new(now: Option<Instant>, transition: TransitionOptions) -> Self {
89        TransitionParameters { now, transition, global_state_change: None }
90    }
91}
92
93/// `number` milliseconds, truncated to whole milliseconds; a negative
94/// number, below the style spec's minimum of 0, gives zero, as does NaN.
95fn whole_milliseconds(number: f32) -> Duration {
96    Duration::from_millis(number as u64)
97}
98
99impl Converter for TransitionOptions {
100    fn convert(value: &Convertible<'_>, error: &mut Error) -> Option<TransitionOptions> {
101        if !value.is_object() {
102            error.message = "transition must be an object".to_owned();
103            return None;
104        }
105
106        let mut result = TransitionOptions::default();
107
108        if let Some(duration) = value.object_member("duration") {
109            let Some(number) = duration.to_number() else {
110                error.message = "duration must be a number".to_owned();
111                return None;
112            };
113            result.duration = Some(whole_milliseconds(number));
114        }
115
116        if let Some(delay) = value.object_member("delay") {
117            let Some(number) = delay.to_number() else {
118                error.message = "delay must be a number".to_owned();
119                return None;
120            };
121            result.delay = Some(whole_milliseconds(number));
122        }
123
124        Some(result)
125    }
126}