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
15pub trait Shadowed {
16    fn shadow(&self) -> Option<&Shadow>;
17}
18
19pub trait ShadowedMut: Shadowed {
20    fn set_shadow(&mut self, shadow: Option<Shadow>);
21}
22
23pub trait ShadowedExt: ShadowedMut + Sized {
24    fn shadow_sm(mut self) -> Self {
25        self.set_shadow(Some(Shadow {
26            color: "rgba(0,0,0,0.1)".into(),
27            offset_x: 0.0,
28            offset_y: 1.0,
29            blur: 2.0,
30        }));
31        self
32    }
33
34    fn shadow_md(mut self) -> Self {
35        self.set_shadow(Some(Shadow {
36            color: "rgba(0,0,0,0.15)".into(),
37            offset_x: 0.0,
38            offset_y: 4.0,
39            blur: 6.0,
40        }));
41        self
42    }
43
44    fn shadow_lg(mut self) -> Self {
45        self.set_shadow(Some(Shadow {
46            color: "rgba(0,0,0,0.2)".into(),
47            offset_x: 0.0,
48            offset_y: 8.0,
49            blur: 16.0,
50        }));
51        self
52    }
53
54    fn shadow_xl(mut self) -> Self {
55        self.set_shadow(Some(Shadow {
56            color: "rgba(0,0,0,0.25)".into(),
57            offset_x: 0.0,
58            offset_y: 16.0,
59            blur: 32.0,
60        }));
61        self
62    }
63
64    fn shadow_none(mut self) -> Self {
65        self.set_shadow(None);
66        self
67    }
68
69    fn shadow_custom(
70        mut self,
71        color: impl Into<String>,
72        offset_x: f32,
73        offset_y: f32,
74        blur: f32,
75    ) -> Self {
76        self.set_shadow(Some(Shadow {
77            color: color.into(),
78            offset_x,
79            offset_y,
80            blur,
81        }));
82        self
83    }
84}
85
86impl<T: ShadowedMut + Sized> ShadowedExt for T {}