Skip to main content

glory_render/
presence.rs

1//! Bounded presence presentation. Logical availability follows `open` immediately.
2use crate::Error;
3
4pub const MAX_PRESENCE_MILLIS: u16 = 2000;
5/// Closed presentation; opacity/scale use thousandths, translations CSS pixels.
6#[derive(Clone, Copy, Debug, PartialEq, Eq)]
7pub struct PresenceStyle {
8    pub opacity: u16,
9    pub scale_x: u16,
10    pub scale_y: u16,
11    pub translate_x: i16,
12    pub translate_y: i16,
13    pub rotate: i16,
14    pub skew_x: i16,
15    pub skew_y: i16,
16    pub origin_x: u8,
17    pub origin_y: u8,
18}
19impl Default for PresenceStyle {
20    fn default() -> Self {
21        Self {
22            opacity: 0,
23            scale_x: 1000,
24            scale_y: 1000,
25            translate_x: 0,
26            translate_y: 0,
27            rotate: 0,
28            skew_x: 0,
29            skew_y: 0,
30            origin_x: 50,
31            origin_y: 50,
32        }
33    }
34}
35impl PresenceStyle {
36    pub fn validate(self) -> Result<(), Error> {
37        if self.opacity > 1000
38            || self.scale_x > 2000
39            || self.scale_y > 2000
40            || self.translate_x.unsigned_abs() > 200
41            || self.translate_y.unsigned_abs() > 200
42            || self.rotate.unsigned_abs() > 180
43            || self.skew_x.unsigned_abs() > 45
44            || self.skew_y.unsigned_abs() > 45
45            || self.origin_x > 100
46            || self.origin_y > 100
47        {
48            return Err(Error::InvalidProps(
49                "presence style exceeds opacity/scale/translation/angle/origin bounds",
50            ));
51        }
52        Ok(())
53    }
54    pub fn has_transform(self) -> bool {
55        self.scale_x != 1000
56            || self.scale_y != 1000
57            || self.translate_x != 0
58            || self.translate_y != 0
59            || self.rotate != 0
60            || self.skew_x != 0
61            || self.skew_y != 0
62    }
63}
64#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
65pub enum PresenceEffect {
66    None,
67    #[default]
68    Fade,
69    FadeUp,
70    FadeDown,
71    FadeLeft,
72    FadeRight,
73    Scale,
74    ScaleX,
75    ScaleY,
76    SlideUp,
77    SlideDown,
78    SlideLeft,
79    SlideRight,
80    SkewUp,
81    SkewDown,
82    RotateLeft,
83    RotateRight,
84    Pop,
85    PopBottomLeft,
86    PopBottomRight,
87    PopTopLeft,
88    PopTopRight,
89    Custom(PresenceStyle),
90}
91impl PresenceEffect {
92    pub fn as_str(self) -> &'static str {
93        match self {
94            Self::None => "none",
95            Self::Fade => "fade",
96            Self::FadeUp => "fade-up",
97            Self::FadeDown => "fade-down",
98            Self::FadeLeft => "fade-left",
99            Self::FadeRight => "fade-right",
100            Self::Scale => "scale",
101            Self::ScaleX => "scale-x",
102            Self::ScaleY => "scale-y",
103            Self::SlideUp => "slide-up",
104            Self::SlideDown => "slide-down",
105            Self::SlideLeft => "slide-left",
106            Self::SlideRight => "slide-right",
107            Self::SkewUp => "skew-up",
108            Self::SkewDown => "skew-down",
109            Self::RotateLeft => "rotate-left",
110            Self::RotateRight => "rotate-right",
111            Self::Pop => "pop",
112            Self::PopBottomLeft => "pop-bottom-left",
113            Self::PopBottomRight => "pop-bottom-right",
114            Self::PopTopLeft => "pop-top-left",
115            Self::PopTopRight => "pop-top-right",
116            Self::Custom(_) => "custom",
117        }
118    }
119}
120impl PresenceEffect {
121    pub fn style(self, distance: u16) -> PresenceStyle {
122        let mut style = PresenceStyle::default();
123        let distance = distance.min(200) as i16;
124        match self {
125            Self::Custom(value) => return value,
126            Self::None => style.opacity = 1000,
127            Self::Fade => (),
128            Self::FadeUp | Self::SlideUp => style.translate_y = distance,
129            Self::FadeDown | Self::SlideDown => style.translate_y = -distance,
130            Self::FadeLeft | Self::SlideLeft => style.translate_x = distance,
131            Self::FadeRight | Self::SlideRight => style.translate_x = -distance,
132            Self::Scale => {
133                style.scale_x = 850;
134                style.scale_y = 850;
135            }
136            Self::ScaleX => style.scale_x = 0,
137            Self::ScaleY => style.scale_y = 0,
138            Self::SkewUp => {
139                style.skew_x = -10;
140                style.translate_y = distance;
141            }
142            Self::SkewDown => {
143                style.skew_x = 10;
144                style.translate_y = -distance;
145            }
146            Self::RotateLeft => {
147                style.rotate = -12;
148                style.origin_x = 0;
149            }
150            Self::RotateRight => {
151                style.rotate = 12;
152                style.origin_x = 100;
153            }
154            Self::Pop
155            | Self::PopBottomLeft
156            | Self::PopBottomRight
157            | Self::PopTopLeft
158            | Self::PopTopRight => {
159                style.scale_x = 900;
160                style.scale_y = 900;
161                match self {
162                    Self::PopBottomLeft => {
163                        style.origin_x = 0;
164                        style.origin_y = 100;
165                    }
166                    Self::PopBottomRight => {
167                        style.origin_x = 100;
168                        style.origin_y = 100;
169                    }
170                    Self::PopTopLeft => {
171                        style.origin_x = 0;
172                        style.origin_y = 0;
173                    }
174                    Self::PopTopRight => {
175                        style.origin_x = 100;
176                        style.origin_y = 0;
177                    }
178                    _ => (),
179                }
180            }
181        }
182        if matches!(
183            self,
184            Self::SlideUp | Self::SlideDown | Self::SlideLeft | Self::SlideRight
185        ) {
186            style.opacity = 1000;
187        }
188        style
189    }
190}
191#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
192pub enum PresenceTiming {
193    Linear,
194    #[default]
195    Ease,
196    EaseIn,
197    EaseOut,
198    EaseInOut,
199}
200impl PresenceTiming {
201    pub fn as_str(self) -> &'static str {
202        match self {
203            Self::Linear => "linear",
204            Self::Ease => "ease",
205            Self::EaseIn => "ease-in",
206            Self::EaseOut => "ease-out",
207            Self::EaseInOut => "ease-in-out",
208        }
209    }
210}
211#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
212pub struct PresenceConfig {
213    pub open: bool,
214    /// Animate an initially open declaration. Otherwise its first frame settles.
215    pub appear: bool,
216    pub effect: PresenceEffect,
217    pub timing: PresenceTiming,
218    pub duration_millis: u16,
219    pub delay_millis: u16,
220    pub distance: u16,
221    pub revision: u64,
222}
223impl PresenceConfig {
224    pub fn validate(&self) -> Result<(), Error> {
225        self.effect.style(self.distance).validate()?;
226        if self.duration_millis > MAX_PRESENCE_MILLIS
227            || self.delay_millis > MAX_PRESENCE_MILLIS
228            || self.distance > 200
229        {
230            return Err(Error::InvalidProps(
231                "presence requires duration/delay <= 2000ms and distance <= 200px",
232            ));
233        }
234        Ok(())
235    }
236    pub(crate) fn validate_style(&self, style: &crate::style::Style) -> Result<(), Error> {
237        if style.opacity.is_some() || style.display.is_some() {
238            return Err(Error::InvalidProps(
239                "presence owns opacity and display; put custom presentation on its child",
240            ));
241        }
242        Ok(())
243    }
244}
245pub(crate) fn revision(
246    previous: Option<&PresenceConfig>,
247    next: Option<&PresenceConfig>,
248) -> Result<(), Error> {
249    if let Some(previous) = previous {
250        let next = next.ok_or(Error::InvalidProps(
251            "a live presence declaration cannot be removed",
252        ))?;
253        if next.revision < previous.revision
254            || next.revision == previous.revision && next != previous
255        {
256            return Err(Error::PresenceRevision {
257                current: previous.revision,
258                incoming: next.revision,
259            });
260        }
261    }
262    Ok(())
263}