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 {
125            alpha: alpha_val, ..
126        } = color;
127        if alpha_val == 0 {
128            return false;
129        }
130        if blur_radius > 0.0 {
131            return true;
132        }
133        if spread_radius > 0.0 {
134            return true;
135        }
136        offset_x != 0.0 || offset_y != 0.0
137    }
138}
139
140/// Inspection helpers for `Transform2D`.
141impl Transform2D {
142    /// Returns true when the transform is the exact identity transform.
143    ///
144    /// # Arguments
145    ///
146    /// - `&Self` - Transform to inspect.
147    ///
148    /// # Returns
149    ///
150    /// - `bool` - True when translation and rotation are zero and scale is one.
151    #[must_use]
152    #[inline(always)]
153    pub fn is_identity(&self) -> bool {
154        self.get_translate_x() == 0.0
155            && self.get_translate_y() == 0.0
156            && self.get_scale_x() == 1.0
157            && self.get_scale_y() == 1.0
158            && self.get_rotation_rad() == 0.0
159    }
160}
161
162/// Non-panicking optional field readers for `PaintStyle`.
163impl PaintStyle {
164    /// Returns the optional background color.
165    ///
166    /// # Arguments
167    ///
168    /// - `&Self` - Paint style.
169    ///
170    /// # Returns
171    ///
172    /// - `Option<Color>` - Background color if set.
173    #[must_use]
174    #[inline(always)]
175    pub fn background_color(&self) -> Option<Color> {
176        let Self { background, .. } = *self;
177        background
178    }
179
180    /// Returns the optional linear gradient fill.
181    ///
182    /// # Arguments
183    ///
184    /// - `&Self` - Paint style.
185    ///
186    /// # Returns
187    ///
188    /// - `Option<LinearGradient>` - Linear gradient if set.
189    #[must_use]
190    #[inline(always)]
191    pub fn background_gradient(&self) -> Option<LinearGradient> {
192        let Self { gradient, .. } = *self;
193        gradient
194    }
195
196    /// Returns the optional drop shadow.
197    ///
198    /// # Arguments
199    ///
200    /// - `&Self` - Paint style.
201    ///
202    /// # Returns
203    ///
204    /// - `Option<Shadow>` - Drop shadow if set.
205    #[must_use]
206    #[inline(always)]
207    pub fn box_shadow(&self) -> Option<Shadow> {
208        let Self { shadow, .. } = *self;
209        shadow
210    }
211
212    /// Returns the optional focus ring stroke.
213    ///
214    /// # Arguments
215    ///
216    /// - `&Self` - Paint style.
217    ///
218    /// # Returns
219    ///
220    /// - `Option<Border>` - Focus ring stroke if set.
221    #[must_use]
222    #[inline(always)]
223    pub fn focus_ring_border(&self) -> Option<Border> {
224        let Self { focus_ring, .. } = *self;
225        focus_ring
226    }
227}