Skip to main content

euv_core/style/paint_prop/
impl.rs

1use super::*;
2
3/// Identity default for `Transform2D`.
4impl Default for Transform2D {
5    /// Creates an identity 2D transform (`scale = 1.0`, `translate = 0.0`, `rotation = 0.0`).
6    ///
7    /// # Returns
8    ///
9    /// - `Self` - Identity `Transform2D`.
10    #[inline(always)]
11    fn default() -> Self {
12        Self::new(0.0, 0.0, 1.0, 1.0, 0.0)
13    }
14}
15
16/// Default paint properties (`opacity = 1.0`, transparent background).
17impl Default for PaintStyle {
18    /// Creates default transparent paint properties.
19    ///
20    /// # Returns
21    ///
22    /// - `Self` - Default `PaintStyle`.
23    #[inline(always)]
24    fn default() -> Self {
25        Self {
26            background: None,
27            gradient: None,
28            border: Border::default(),
29            radius: CornerRadius::default(),
30            shadow: None,
31            focus_ring: None,
32            opacity: 1.0,
33            transform: Transform2D::default(),
34            cursor: CursorIcon::Default,
35            pointer_events: PointerEvents::Auto,
36            stop_scroll_propagation: false,
37            stop_event_propagation: false,
38        }
39    }
40}
41
42/// Exact fixed-point alpha scaling and visibility helpers for `Shadow`.
43impl Shadow {
44    /// Returns a copy of this shadow with its tint alpha scaled by `opacity` in `[0.0, 1.0]`.
45    ///
46    /// # Arguments
47    ///
48    /// - `&Self` - Source drop shadow.
49    /// - `f32` - Opacity multiplier clamped to `[0.0, 1.0]`.
50    ///
51    /// # Returns
52    ///
53    /// - `Self` - Shadow with exact fixed-point scaled tint alpha.
54    #[must_use]
55    #[inline(always)]
56    pub fn with_opacity(&self, opacity: f32) -> Self {
57        if opacity >= 1.0 {
58            return *self;
59        }
60        let Self {
61            offset_x,
62            offset_y,
63            blur_radius,
64            spread_radius,
65            color,
66        } = *self;
67        Self {
68            offset_x,
69            offset_y,
70            blur_radius,
71            spread_radius,
72            color: color.multiply_alpha(opacity),
73        }
74    }
75
76    /// Returns a copy of this shadow with its tint alpha scaled by an 8-bit integer factor `0..=255`.
77    ///
78    /// # Arguments
79    ///
80    /// - `&Self` - Source drop shadow.
81    /// - `u8` - 8-bit integer alpha multiplier (`0` = transparent, `255` = unchanged).
82    ///
83    /// # Returns
84    ///
85    /// - `Self` - Shadow with exact fixed-point scaled tint alpha.
86    #[must_use]
87    #[inline(always)]
88    pub const fn with_alpha_u8(&self, alpha_scale: u8) -> Self {
89        let Self {
90            offset_x,
91            offset_y,
92            blur_radius,
93            spread_radius,
94            color,
95        } = *self;
96        Self {
97            offset_x,
98            offset_y,
99            blur_radius,
100            spread_radius,
101            color: color.multiply_alpha_u8(alpha_scale),
102        }
103    }
104
105    /// Returns `true` when this shadow has non-zero alpha and non-zero visual extent.
106    ///
107    /// # Arguments
108    ///
109    /// - `&Self` - Drop shadow to inspect.
110    ///
111    /// # Returns
112    ///
113    /// - `bool` - `true` when the shadow contributes visible pixels.
114    #[must_use]
115    #[inline(always)]
116    pub const fn is_visible(&self) -> bool {
117        let Self {
118            offset_x,
119            offset_y,
120            blur_radius,
121            spread_radius,
122            color,
123        } = *self;
124        let Color { a, .. } = color;
125        if a == 0 {
126            return false;
127        }
128        if blur_radius > 0.0 {
129            return true;
130        }
131        if spread_radius > 0.0 {
132            return true;
133        }
134        offset_x != 0.0 || offset_y != 0.0
135    }
136}
137
138/// Inspection helpers for `Transform2D`.
139impl Transform2D {
140    /// Returns true when the transform is the exact identity transform.
141    ///
142    /// # Arguments
143    ///
144    /// - `&Self` - Transform to inspect.
145    ///
146    /// # Returns
147    ///
148    /// - `bool` - True when translation and rotation are zero and scale is one.
149    #[must_use]
150    #[inline(always)]
151    pub fn is_identity(&self) -> bool {
152        self.get_translate_x() == 0.0
153            && self.get_translate_y() == 0.0
154            && self.get_scale_x() == 1.0
155            && self.get_scale_y() == 1.0
156            && self.get_rotation_rad() == 0.0
157    }
158}
159
160/// Non-panicking optional field readers for `PaintStyle`.
161impl PaintStyle {
162    /// Returns the optional background color.
163    ///
164    /// # Arguments
165    ///
166    /// - `&Self` - Paint style.
167    ///
168    /// # Returns
169    ///
170    /// - `Option<Color>` - Background color if set.
171    #[must_use]
172    #[inline(always)]
173    pub fn background_color(&self) -> Option<Color> {
174        let Self { background, .. } = *self;
175        background
176    }
177
178    /// Returns the optional linear gradient fill.
179    ///
180    /// # Arguments
181    ///
182    /// - `&Self` - Paint style.
183    ///
184    /// # Returns
185    ///
186    /// - `Option<LinearGradient>` - Linear gradient if set.
187    #[must_use]
188    #[inline(always)]
189    pub fn background_gradient(&self) -> Option<LinearGradient> {
190        let Self { gradient, .. } = *self;
191        gradient
192    }
193
194    /// Returns the optional drop shadow.
195    ///
196    /// # Arguments
197    ///
198    /// - `&Self` - Paint style.
199    ///
200    /// # Returns
201    ///
202    /// - `Option<Shadow>` - Drop shadow if set.
203    #[must_use]
204    #[inline(always)]
205    pub fn box_shadow(&self) -> Option<Shadow> {
206        let Self { shadow, .. } = *self;
207        shadow
208    }
209
210    /// Returns the optional focus ring stroke.
211    ///
212    /// # Arguments
213    ///
214    /// - `&Self` - Paint style.
215    ///
216    /// # Returns
217    ///
218    /// - `Option<Border>` - Focus ring stroke if set.
219    #[must_use]
220    #[inline(always)]
221    pub fn focus_ring_border(&self) -> Option<Border> {
222        let Self { focus_ring, .. } = *self;
223        focus_ring
224    }
225}