Skip to main content

cranpose_liquid/
appearance.rs

1//! Application-wide Liquid Glass appearance preferences.
2
3/// A tint percentage from clear (0) to fully tinted (100).
4#[derive(Clone, Copy, Debug, PartialEq)]
5pub struct GlassTintAmount(f32);
6
7/// A tint percentage was non-finite or outside the supported range.
8#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
9#[error("glass tint amount must be finite and between 0 and 100 percent")]
10pub struct InvalidGlassTintAmount;
11
12impl GlassTintAmount {
13    /// Validates a percentage from 0 through 100, inclusive.
14    pub fn from_percent(percent: f32) -> Result<Self, InvalidGlassTintAmount> {
15        if percent.is_finite() && (0.0..=100.0).contains(&percent) {
16            Ok(Self(percent))
17        } else {
18            Err(InvalidGlassTintAmount)
19        }
20    }
21
22    /// Returns the configured percentage.
23    pub fn percent(self) -> f32 {
24        self.0
25    }
26}
27
28impl Default for GlassTintAmount {
29    fn default() -> Self {
30        Self(25.0)
31    }
32}
33
34#[cfg(test)]
35#[path = "tests/appearance_tests.rs"]
36mod tests;