freya_core/style/
shadow.rs

1use std::fmt::Display;
2
3use torin::scaled::Scaled;
4
5use crate::prelude::Color;
6
7#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8#[derive(Default, Clone, Debug, PartialEq)]
9pub enum ShadowPosition {
10    #[default]
11    Normal,
12    Inset,
13}
14
15#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
16#[derive(Default, Clone, Debug, PartialEq)]
17pub struct Shadow {
18    pub(crate) position: ShadowPosition,
19    pub(crate) x: f32,
20    pub(crate) y: f32,
21    pub(crate) blur: f32,
22    pub(crate) spread: f32,
23    pub color: Color,
24}
25
26impl Display for Shadow {
27    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28        f.write_str(&format!(
29            "{:?} {} {} {} {}",
30            self.position, self.x, self.y, self.blur, self.spread
31        ))
32    }
33}
34
35impl<C: Into<Color>> From<(f32, f32, f32, f32, C)> for Shadow {
36    fn from((x, y, blur, spread, color): (f32, f32, f32, f32, C)) -> Self {
37        Self {
38            position: ShadowPosition::default(),
39            x,
40            y,
41            blur,
42            spread,
43            color: color.into(),
44        }
45    }
46}
47
48impl Shadow {
49    pub fn new() -> Self {
50        Self::default()
51    }
52
53    /// Shorthand for [Self::position].
54    pub fn inset(mut self) -> Self {
55        self.position = ShadowPosition::Inset;
56        self
57    }
58
59    /// Shorthand for [Self::position].
60    pub fn normal(mut self) -> Self {
61        self.position = ShadowPosition::Normal;
62        self
63    }
64
65    pub fn position(mut self, position: ShadowPosition) -> Self {
66        self.position = position;
67        self
68    }
69
70    pub fn x(mut self, x: f32) -> Self {
71        self.x = x;
72        self
73    }
74
75    pub fn y(mut self, y: f32) -> Self {
76        self.y = y;
77        self
78    }
79
80    pub fn blur(mut self, blur: f32) -> Self {
81        self.blur = blur;
82        self
83    }
84
85    pub fn spread(mut self, spread: f32) -> Self {
86        self.spread = spread;
87        self
88    }
89
90    pub fn color(mut self, color: impl Into<Color>) -> Self {
91        self.color = color.into();
92        self
93    }
94}
95
96impl Scaled for Shadow {
97    fn scale(&mut self, scale_factor: f32) {
98        self.x *= scale_factor;
99        self.y *= scale_factor;
100        self.spread *= scale_factor;
101        self.blur *= scale_factor;
102    }
103}