Skip to main content

euv_core/style/effect/
impl.rs

1use super::*;
2
3/// Default empty rare-effect entry with identity transform and zero backdrop blur.
4impl Default for PaintEffectEntry {
5    /// Creates an empty `PaintEffectEntry` matching default unpainted effects.
6    ///
7    /// # Returns
8    ///
9    /// - `Self` - Empty paint effect entry.
10    #[inline(always)]
11    fn default() -> Self {
12        Self {
13            shadow: None,
14            gradient: None,
15            transform: Transform2D::default(),
16            focus_ring: None,
17            backdrop_blur: 0.0,
18        }
19    }
20}
21
22/// Inspection and extraction helpers for `PaintEffectEntry`.
23impl PaintEffectEntry {
24    /// Builds a `PaintEffectEntry` from a `PaintStyle` and backdrop blur radius.
25    ///
26    /// # Arguments
27    ///
28    /// - `&PaintStyle` - Source paint style reference.
29    /// - `f32` - Backdrop blur radius in logical pixels.
30    ///
31    /// # Returns
32    ///
33    /// - `Self` - Extracted rare-effect entry.
34    #[must_use]
35    #[inline(always)]
36    pub fn from_paint_and_blur(paint: &PaintStyle, backdrop_blur: f32) -> Self {
37        Self {
38            shadow: paint.box_shadow(),
39            gradient: paint.background_gradient(),
40            transform: paint.get_transform(),
41            focus_ring: paint.focus_ring_border(),
42            backdrop_blur: if backdrop_blur.is_finite() {
43                backdrop_blur.max(0.0)
44            } else {
45                0.0
46            },
47        }
48    }
49
50    /// Returns `true` when no shadow, gradient, non-identity transform, focus ring, or backdrop blur is active.
51    ///
52    /// # Arguments
53    ///
54    /// - `&Self` - Paint effect entry reference.
55    ///
56    /// # Returns
57    ///
58    /// - `bool` - `true` when the entry can be omitted from `EffectTable`.
59    #[must_use]
60    #[inline(always)]
61    pub fn is_empty(&self) -> bool {
62        self.box_shadow().is_none()
63            && self.background_gradient().is_none()
64            && self.get_transform().is_identity()
65            && self.focus_ring_border().is_none()
66            && self.get_backdrop_blur() <= 0.0
67    }
68
69    /// Returns the optional drop shadow stored in this effect entry.
70    ///
71    /// # Arguments
72    ///
73    /// - `&Self` - Paint effect entry reference.
74    ///
75    /// # Returns
76    ///
77    /// - `Option<Shadow>` - Drop shadow if set.
78    #[must_use]
79    #[inline(always)]
80    pub fn box_shadow(&self) -> Option<Shadow> {
81        let Self { shadow, .. } = *self;
82        shadow
83    }
84
85    /// Returns the optional linear gradient stored in this effect entry.
86    ///
87    /// # Arguments
88    ///
89    /// - `&Self` - Paint effect entry reference.
90    ///
91    /// # Returns
92    ///
93    /// - `Option<LinearGradient>` - Linear gradient if set.
94    #[must_use]
95    #[inline(always)]
96    pub fn background_gradient(&self) -> Option<LinearGradient> {
97        let Self { gradient, .. } = *self;
98        gradient
99    }
100
101    /// Returns the optional focus ring border stored in this effect entry.
102    ///
103    /// # Arguments
104    ///
105    /// - `&Self` - Paint effect entry reference.
106    ///
107    /// # Returns
108    ///
109    /// - `Option<Border>` - Focus ring border if set.
110    #[must_use]
111    #[inline(always)]
112    pub fn focus_ring_border(&self) -> Option<Border> {
113        let Self { focus_ring, .. } = *self;
114        focus_ring
115    }
116}
117
118/// Slot-recycled sparse effect storage operations for `EffectTable`.
119impl EffectTable {
120    /// Creates an empty `EffectTable` with zero initial heap allocations.
121    ///
122    /// # Returns
123    ///
124    /// - `Self` - Empty effect table.
125    #[must_use]
126    #[inline(always)]
127    pub fn new() -> Self {
128        Self::default()
129    }
130
131    /// Returns the number of live rare-effect entries currently stored.
132    ///
133    /// # Arguments
134    ///
135    /// - `&Self` - Effect table reference.
136    ///
137    /// # Returns
138    ///
139    /// - `usize` - Count of live effect entries.
140    #[must_use]
141    #[inline(always)]
142    pub fn len(&self) -> usize {
143        self.get_live_count()
144    }
145
146    /// Returns `true` when zero rare-effect entries are active.
147    ///
148    /// # Arguments
149    ///
150    /// - `&Self` - Effect table reference.
151    ///
152    /// # Returns
153    ///
154    /// - `bool` - Whether the table has no live entries.
155    #[must_use]
156    #[inline(always)]
157    pub fn is_empty(&self) -> bool {
158        self.get_live_count() == 0
159    }
160
161    /// Looks up a live `PaintEffectEntry` by its 1-based `effect_id` (`0` returns `None`).
162    ///
163    /// # Arguments
164    ///
165    /// - `&Self` - Effect table reference.
166    /// - `u32` - 1-based effect identifier.
167    ///
168    /// # Returns
169    ///
170    /// - `Option<&PaintEffectEntry>` - Borrowed effect entry if present.
171    #[must_use]
172    #[inline(always)]
173    pub fn get(&self, effect_id: u32) -> Option<&PaintEffectEntry> {
174        if effect_id == EFFECT_ID_NONE {
175            return None;
176        }
177        let idx: usize = (effect_id - 1) as usize;
178        self.get_entries().get(idx).and_then(Option::as_ref)
179    }
180
181    /// Inserts `entry` into the sparse table and returns its 1-based `effect_id` (`0` when `entry.is_empty()`).
182    ///
183    /// # Arguments
184    ///
185    /// - `&Self` - Mutable effect table.
186    /// - `PaintEffectEntry` - Rare effect payload to store.
187    ///
188    /// # Returns
189    ///
190    /// - `u32` - 1-based effect identifier or `EFFECT_ID_NONE` when empty.
191    pub fn insert(&mut self, entry: PaintEffectEntry) -> u32 {
192        if entry.is_empty() {
193            return EFFECT_ID_NONE;
194        }
195        let slot_idx: u32 = if let Some(recycled) = self.get_mut_free_slots().pop() {
196            self.get_mut_entries()[recycled as usize] = Some(entry);
197            recycled
198        } else {
199            let next_idx: u32 = self.get_entries().len() as u32;
200            self.get_mut_entries().push(Some(entry));
201            next_idx
202        };
203        self.set_live_count(self.get_live_count().saturating_add(1));
204        slot_idx + 1
205    }
206
207    /// Updates an existing `effect_id` in place, allocating on transition from none and freeing on transition to empty.
208    ///
209    /// # Arguments
210    ///
211    /// - `&Self` - Mutable effect table.
212    /// - `u32` - Previous 1-based effect identifier (`EFFECT_ID_NONE` if none).
213    /// - `PaintEffectEntry` - Replacement effect payload.
214    ///
215    /// # Returns
216    ///
217    /// - `u32` - Updated 1-based effect identifier or `EFFECT_ID_NONE`.
218    pub fn update(&mut self, current_id: u32, entry: PaintEffectEntry) -> u32 {
219        if entry.is_empty() {
220            self.remove(current_id);
221            return EFFECT_ID_NONE;
222        }
223        if current_id != EFFECT_ID_NONE {
224            let idx: usize = (current_id - 1) as usize;
225            if idx < self.get_entries().len() {
226                let was_none: bool = self.get_entries()[idx].is_none();
227                self.get_mut_entries()[idx] = Some(entry);
228                if was_none {
229                    let next_live: usize = self.get_live_count().saturating_add(1);
230                    let _: &mut Self = self.set_live_count(next_live);
231                }
232                return current_id;
233            }
234        }
235        self.insert(entry)
236    }
237
238    /// Releases the slot associated with `effect_id` back to the free list in O(1).
239    ///
240    /// # Arguments
241    ///
242    /// - `&Self` - Mutable effect table.
243    /// - `u32` - 1-based effect identifier to remove (`EFFECT_ID_NONE` is a no-op).
244    pub fn remove(&mut self, effect_id: u32) {
245        if effect_id == EFFECT_ID_NONE {
246            return;
247        }
248        let idx: usize = (effect_id - 1) as usize;
249        let removed: bool = if let Some(slot) = self.get_mut_entries().get_mut(idx) {
250            slot.take().is_some()
251        } else {
252            false
253        };
254        if removed {
255            self.get_mut_free_slots().push(effect_id - 1);
256            self.set_live_count(self.get_live_count().saturating_sub(1));
257        }
258    }
259}
260
261/// Default 32-byte compact paint style (`opacity = 1.0`, transparent background, no effects).
262impl Default for CompactPaintStyle {
263    /// Creates a default transparent `CompactPaintStyle`.
264    ///
265    /// # Returns
266    ///
267    /// - `Self` - Default compact paint style.
268    #[inline(always)]
269    fn default() -> Self {
270        Self {
271            background: Color::default(),
272            border_color: Color::default(),
273            border_width: 0.0,
274            radii_q4: [0; 4],
275            opacity: 1.0,
276            effect_id: EFFECT_ID_NONE,
277            flags: 0,
278        }
279    }
280}
281
282/// Encoding and decoding helpers for 32-byte `CompactPaintStyle`.
283impl CompactPaintStyle {
284    /// Quantizes a logical corner radius in pixels into quarter-pixel `u16` fixed-point storage.
285    ///
286    /// # Arguments
287    ///
288    /// - `f32` - Logical corner radius in pixels.
289    ///
290    /// # Returns
291    ///
292    /// - `u16` - Clamped quarter-pixel fixed-point radius.
293    #[must_use]
294    #[inline(always)]
295    fn quantize_radius(value: f32) -> u16 {
296        if !value.is_finite() || value <= 0.0 {
297            return 0;
298        }
299        let scaled: f32 = (value * COMPACT_RADIUS_SCALE).round();
300        if scaled >= u16::MAX as f32 {
301            u16::MAX
302        } else {
303            scaled as u16
304        }
305    }
306
307    /// Encodes a `CursorIcon` enum discriminant into a compact `u32` byte value.
308    ///
309    /// # Arguments
310    ///
311    /// - `CursorIcon` - Cursor icon variant.
312    ///
313    /// # Returns
314    ///
315    /// - `u32` - Packed discriminant value.
316    #[must_use]
317    #[inline(always)]
318    const fn encode_cursor(cursor: CursorIcon) -> u32 {
319        match cursor {
320            CursorIcon::Default => 0,
321            CursorIcon::Pointer => 1,
322            CursorIcon::Text => 2,
323            CursorIcon::Crosshair => 3,
324            CursorIcon::Move => 4,
325            CursorIcon::NotAllowed => 5,
326            CursorIcon::EwResize => 6,
327            CursorIcon::NsResize => 7,
328            CursorIcon::Grab => 8,
329            CursorIcon::Grabbing => 9,
330        }
331    }
332
333    /// Decodes a compact `u32` byte discriminant back into `CursorIcon`.
334    ///
335    /// # Arguments
336    ///
337    /// - `u32` - Packed discriminant value.
338    ///
339    /// # Returns
340    ///
341    /// - `CursorIcon` - Decoded cursor icon.
342    #[must_use]
343    #[inline(always)]
344    const fn decode_cursor(bits: u32) -> CursorIcon {
345        match bits {
346            1 => CursorIcon::Pointer,
347            2 => CursorIcon::Text,
348            3 => CursorIcon::Crosshair,
349            4 => CursorIcon::Move,
350            5 => CursorIcon::NotAllowed,
351            6 => CursorIcon::EwResize,
352            7 => CursorIcon::NsResize,
353            8 => CursorIcon::Grab,
354            9 => CursorIcon::Grabbing,
355            _ => CursorIcon::Default,
356        }
357    }
358
359    /// Encodes a `PointerEvents` enum discriminant into a compact `u32` value.
360    ///
361    /// # Arguments
362    ///
363    /// - `PointerEvents` - Pointer hit-test policy.
364    ///
365    /// # Returns
366    ///
367    /// - `u32` - Packed discriminant value.
368    #[must_use]
369    #[inline(always)]
370    const fn encode_pointer_events(policy: PointerEvents) -> u32 {
371        match policy {
372            PointerEvents::Auto => 0,
373            PointerEvents::None => 1,
374        }
375    }
376
377    /// Decodes a compact `u32` value back into `PointerEvents`.
378    ///
379    /// # Arguments
380    ///
381    /// - `u32` - Packed discriminant value.
382    ///
383    /// # Returns
384    ///
385    /// - `PointerEvents` - Decoded pointer policy.
386    #[must_use]
387    #[inline(always)]
388    const fn decode_pointer_events(bits: u32) -> PointerEvents {
389        match bits {
390            1 => PointerEvents::None,
391            _ => PointerEvents::Auto,
392        }
393    }
394
395    /// Packs a `PaintStyle` and its sparse `effect_id` into a 32-byte `CompactPaintStyle`.
396    ///
397    /// # Arguments
398    ///
399    /// - `&PaintStyle` - Source paint style reference.
400    /// - `u32` - 1-based `EffectTable` handle (`EFFECT_ID_NONE` when absent).
401    ///
402    /// # Returns
403    ///
404    /// - `Self` - Packed 32-byte compact paint style.
405    #[must_use]
406    #[inline(always)]
407    pub fn from_paint(paint: &PaintStyle, effect_id: u32) -> Self {
408        let bg_opt: Option<Color> = paint.background_color();
409        let border: Border = paint.get_border();
410        let radius: CornerRadius = paint.get_radius();
411        let mut flags: u32 = 0;
412        if bg_opt.is_some() {
413            flags |= COMPACT_PAINT_HAS_BACKGROUND;
414        }
415        if paint.get_stop_scroll_propagation() {
416            flags |= COMPACT_PAINT_STOP_SCROLL;
417        }
418        if paint.get_stop_event_propagation() {
419            flags |= COMPACT_PAINT_STOP_EVENT;
420        }
421        flags |= Self::encode_cursor(paint.get_cursor()) << COMPACT_PAINT_CURSOR_SHIFT;
422        flags |=
423            Self::encode_pointer_events(paint.get_pointer_events()) << COMPACT_PAINT_POINTER_SHIFT;
424        Self {
425            background: bg_opt.unwrap_or_default(),
426            border_color: border.get_color(),
427            border_width: border.get_width(),
428            radii_q4: [
429                Self::quantize_radius(radius.get_top_left()),
430                Self::quantize_radius(radius.get_top_right()),
431                Self::quantize_radius(radius.get_bottom_right()),
432                Self::quantize_radius(radius.get_bottom_left()),
433            ],
434            opacity: paint.get_opacity(),
435            effect_id,
436            flags,
437        }
438    }
439
440    /// Returns the solid background color if `COMPACT_PAINT_HAS_BACKGROUND` is set.
441    ///
442    /// # Arguments
443    ///
444    /// - `&Self` - Compact paint style reference.
445    ///
446    /// # Returns
447    ///
448    /// - `Option<Color>` - Background color when set.
449    #[must_use]
450    #[inline(always)]
451    pub fn background_color(&self) -> Option<Color> {
452        if (self.get_flags() & COMPACT_PAINT_HAS_BACKGROUND) != 0 {
453            Some(self.get_background())
454        } else {
455            None
456        }
457    }
458
459    /// Reconstructs the `Border` stroke from inline width and color fields.
460    ///
461    /// # Arguments
462    ///
463    /// - `&Self` - Compact paint style reference.
464    ///
465    /// # Returns
466    ///
467    /// - `Border` - Decoded border stroke.
468    #[must_use]
469    #[inline(always)]
470    pub fn border(&self) -> Border {
471        Border::new(self.get_border_width(), self.get_border_color())
472    }
473
474    /// Reconstructs per-corner radii from quarter-pixel fixed-point storage.
475    ///
476    /// # Arguments
477    ///
478    /// - `&Self` - Compact paint style reference.
479    ///
480    /// # Returns
481    ///
482    /// - `CornerRadius` - Decoded corner radii.
483    #[must_use]
484    #[inline(always)]
485    pub fn corner_radius(&self) -> CornerRadius {
486        let q: [u16; 4] = self.get_radii_q4();
487        CornerRadius::new(
488            q[0] as f32 * COMPACT_RADIUS_INV_SCALE,
489            q[1] as f32 * COMPACT_RADIUS_INV_SCALE,
490            q[2] as f32 * COMPACT_RADIUS_INV_SCALE,
491            q[3] as f32 * COMPACT_RADIUS_INV_SCALE,
492        )
493    }
494
495    /// Decodes the packed `CursorIcon` hint.
496    ///
497    /// # Arguments
498    ///
499    /// - `&Self` - Compact paint style reference.
500    ///
501    /// # Returns
502    ///
503    /// - `CursorIcon` - Cursor icon hint.
504    #[must_use]
505    #[inline(always)]
506    pub fn cursor(&self) -> CursorIcon {
507        let bits: u32 =
508            (self.get_flags() >> COMPACT_PAINT_CURSOR_SHIFT) & COMPACT_PAINT_CURSOR_MASK;
509        Self::decode_cursor(bits)
510    }
511
512    /// Decodes the packed `PointerEvents` policy.
513    ///
514    /// # Arguments
515    ///
516    /// - `&Self` - Compact paint style reference.
517    ///
518    /// # Returns
519    ///
520    /// - `PointerEvents` - Pointer hit-testing policy.
521    #[must_use]
522    #[inline(always)]
523    pub fn pointer_events(&self) -> PointerEvents {
524        let bits: u32 =
525            (self.get_flags() >> COMPACT_PAINT_POINTER_SHIFT) & COMPACT_PAINT_POINTER_MASK;
526        Self::decode_pointer_events(bits)
527    }
528
529    /// Returns whether scroll chaining stops at this node.
530    ///
531    /// # Arguments
532    ///
533    /// - `&Self` - Compact paint style reference.
534    ///
535    /// # Returns
536    ///
537    /// - `bool` - True when scroll propagation is blocked.
538    #[must_use]
539    #[inline(always)]
540    pub fn stop_scroll_propagation(&self) -> bool {
541        (self.get_flags() & COMPACT_PAINT_STOP_SCROLL) != 0
542    }
543
544    /// Returns whether pointer/click event bubbling stops at this node.
545    ///
546    /// # Arguments
547    ///
548    /// - `&Self` - Compact paint style reference.
549    ///
550    /// # Returns
551    ///
552    /// - `bool` - True when event bubbling is blocked.
553    #[must_use]
554    #[inline(always)]
555    pub fn stop_event_propagation(&self) -> bool {
556        (self.get_flags() & COMPACT_PAINT_STOP_EVENT) != 0
557    }
558}