Skip to main content

rustmotion_core/traits/
shadowed.rs

1use schemars::JsonSchema;
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
5pub struct Shadow {
6    pub color: String,
7    #[serde(default)]
8    pub offset_x: f32,
9    #[serde(default)]
10    pub offset_y: f32,
11    #[serde(default)]
12    pub blur: f32,
13}
14
15/// Trait for components that support a drop shadow.
16pub trait Shadowed {
17    fn shadow(&self) -> Option<&Shadow>;
18}
19
20/// Mutable access to shadow — needed by builder traits.
21pub trait ShadowedMut: Shadowed {
22    fn set_shadow(&mut self, shadow: Option<Shadow>);
23}
24
25/// Builder API for shadow.
26pub trait ShadowedExt: ShadowedMut + Sized {
27    fn shadow_sm(mut self) -> Self {
28        self.set_shadow(Some(Shadow {
29            color: "rgba(0,0,0,0.1)".into(),
30            offset_x: 0.0,
31            offset_y: 1.0,
32            blur: 2.0,
33        }));
34        self
35    }
36
37    fn shadow_md(mut self) -> Self {
38        self.set_shadow(Some(Shadow {
39            color: "rgba(0,0,0,0.15)".into(),
40            offset_x: 0.0,
41            offset_y: 4.0,
42            blur: 6.0,
43        }));
44        self
45    }
46
47    fn shadow_lg(mut self) -> Self {
48        self.set_shadow(Some(Shadow {
49            color: "rgba(0,0,0,0.2)".into(),
50            offset_x: 0.0,
51            offset_y: 8.0,
52            blur: 16.0,
53        }));
54        self
55    }
56
57    fn shadow_xl(mut self) -> Self {
58        self.set_shadow(Some(Shadow {
59            color: "rgba(0,0,0,0.25)".into(),
60            offset_x: 0.0,
61            offset_y: 16.0,
62            blur: 32.0,
63        }));
64        self
65    }
66
67    fn shadow_none(mut self) -> Self {
68        self.set_shadow(None);
69        self
70    }
71
72    fn shadow_custom(
73        mut self,
74        color: impl Into<String>,
75        offset_x: f32,
76        offset_y: f32,
77        blur: f32,
78    ) -> Self {
79        self.set_shadow(Some(Shadow {
80            color: color.into(),
81            offset_x,
82            offset_y,
83            blur,
84        }));
85        self
86    }
87}
88
89impl<T: ShadowedMut + Sized> ShadowedExt for T {}