euv-core 0.47.1

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
use super::*;

/// Default empty rare-effect entry with identity transform and zero backdrop blur.
impl Default for PaintEffectEntry {
    /// Creates an empty `PaintEffectEntry` matching default unpainted effects.
    ///
    /// # Returns
    ///
    /// - `Self` - Empty paint effect entry.
    #[inline(always)]
    fn default() -> Self {
        Self {
            shadow: None,
            gradient: None,
            transform: Transform2D::default(),
            focus_ring: None,
            backdrop_blur: 0.0,
        }
    }
}

/// Inspection and extraction helpers for `PaintEffectEntry`.
impl PaintEffectEntry {
    /// Builds a `PaintEffectEntry` from a `PaintStyle` and backdrop blur radius.
    ///
    /// # Arguments
    ///
    /// - `&PaintStyle` - Source paint style reference.
    /// - `f32` - Backdrop blur radius in logical pixels.
    ///
    /// # Returns
    ///
    /// - `Self` - Extracted rare-effect entry.
    #[must_use]
    #[inline(always)]
    pub fn from_paint_and_blur(paint: &PaintStyle, backdrop_blur: f32) -> Self {
        Self {
            shadow: paint.box_shadow(),
            gradient: paint.background_gradient(),
            transform: paint.get_transform(),
            focus_ring: paint.focus_ring_border(),
            backdrop_blur: if backdrop_blur.is_finite() {
                backdrop_blur.max(0.0)
            } else {
                0.0
            },
        }
    }

    /// Returns `true` when no shadow, gradient, non-identity transform, focus ring, or backdrop blur is active.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Paint effect entry reference.
    ///
    /// # Returns
    ///
    /// - `bool` - `true` when the entry can be omitted from `EffectTable`.
    #[must_use]
    #[inline(always)]
    pub fn is_empty(&self) -> bool {
        self.box_shadow().is_none()
            && self.background_gradient().is_none()
            && self.get_transform().is_identity()
            && self.focus_ring_border().is_none()
            && self.get_backdrop_blur() <= 0.0
    }

    /// Returns the optional drop shadow stored in this effect entry.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Paint effect entry reference.
    ///
    /// # Returns
    ///
    /// - `Option<Shadow>` - Drop shadow if set.
    #[must_use]
    #[inline(always)]
    pub fn box_shadow(&self) -> Option<Shadow> {
        let Self { shadow, .. } = *self;
        shadow
    }

    /// Returns the optional linear gradient stored in this effect entry.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Paint effect entry reference.
    ///
    /// # Returns
    ///
    /// - `Option<LinearGradient>` - Linear gradient if set.
    #[must_use]
    #[inline(always)]
    pub fn background_gradient(&self) -> Option<LinearGradient> {
        let Self { gradient, .. } = *self;
        gradient
    }

    /// Returns the optional focus ring border stored in this effect entry.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Paint effect entry reference.
    ///
    /// # Returns
    ///
    /// - `Option<Border>` - Focus ring border if set.
    #[must_use]
    #[inline(always)]
    pub fn focus_ring_border(&self) -> Option<Border> {
        let Self { focus_ring, .. } = *self;
        focus_ring
    }
}

/// Slot-recycled sparse effect storage operations for `EffectTable`.
impl EffectTable {
    /// Creates an empty `EffectTable` with zero initial heap allocations.
    ///
    /// # Returns
    ///
    /// - `Self` - Empty effect table.
    #[must_use]
    #[inline(always)]
    pub fn new() -> Self {
        Self::default()
    }

    /// Returns the number of live rare-effect entries currently stored.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Effect table reference.
    ///
    /// # Returns
    ///
    /// - `usize` - Count of live effect entries.
    #[must_use]
    #[inline(always)]
    pub fn len(&self) -> usize {
        self.get_live_count()
    }

    /// Returns `true` when zero rare-effect entries are active.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Effect table reference.
    ///
    /// # Returns
    ///
    /// - `bool` - Whether the table has no live entries.
    #[must_use]
    #[inline(always)]
    pub fn is_empty(&self) -> bool {
        self.get_live_count() == 0
    }

    /// Looks up a live `PaintEffectEntry` by its 1-based `effect_id` (`0` returns `None`).
    ///
    /// # Arguments
    ///
    /// - `&Self` - Effect table reference.
    /// - `u32` - 1-based effect identifier.
    ///
    /// # Returns
    ///
    /// - `Option<&PaintEffectEntry>` - Borrowed effect entry if present.
    #[must_use]
    #[inline(always)]
    pub fn get(&self, effect_id: u32) -> Option<&PaintEffectEntry> {
        if effect_id == EFFECT_ID_NONE {
            return None;
        }
        let idx: usize = (effect_id - 1) as usize;
        self.get_entries().get(idx).and_then(Option::as_ref)
    }

    /// Inserts `entry` into the sparse table and returns its 1-based `effect_id` (`0` when `entry.is_empty()`).
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable effect table.
    /// - `PaintEffectEntry` - Rare effect payload to store.
    ///
    /// # Returns
    ///
    /// - `u32` - 1-based effect identifier or `EFFECT_ID_NONE` when empty.
    pub fn insert(&mut self, entry: PaintEffectEntry) -> u32 {
        if entry.is_empty() {
            return EFFECT_ID_NONE;
        }
        let slot_idx: u32 = if let Some(recycled) = self.get_mut_free_slots().pop() {
            self.get_mut_entries()[recycled as usize] = Some(entry);
            recycled
        } else {
            let next_idx: u32 = self.get_entries().len() as u32;
            self.get_mut_entries().push(Some(entry));
            next_idx
        };
        self.set_live_count(self.get_live_count().saturating_add(1));
        slot_idx + 1
    }

    /// Updates an existing `effect_id` in place, allocating on transition from none and freeing on transition to empty.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable effect table.
    /// - `u32` - Previous 1-based effect identifier (`EFFECT_ID_NONE` if none).
    /// - `PaintEffectEntry` - Replacement effect payload.
    ///
    /// # Returns
    ///
    /// - `u32` - Updated 1-based effect identifier or `EFFECT_ID_NONE`.
    pub fn update(&mut self, current_id: u32, entry: PaintEffectEntry) -> u32 {
        if entry.is_empty() {
            self.remove(current_id);
            return EFFECT_ID_NONE;
        }
        if current_id != EFFECT_ID_NONE {
            let idx: usize = (current_id - 1) as usize;
            if idx < self.get_entries().len() {
                let was_none: bool = self.get_entries()[idx].is_none();
                self.get_mut_entries()[idx] = Some(entry);
                if was_none {
                    let next_live: usize = self.get_live_count().saturating_add(1);
                    let _: &mut Self = self.set_live_count(next_live);
                }
                return current_id;
            }
        }
        self.insert(entry)
    }

    /// Releases the slot associated with `effect_id` back to the free list in O(1).
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable effect table.
    /// - `u32` - 1-based effect identifier to remove (`EFFECT_ID_NONE` is a no-op).
    pub fn remove(&mut self, effect_id: u32) {
        if effect_id == EFFECT_ID_NONE {
            return;
        }
        let idx: usize = (effect_id - 1) as usize;
        let removed: bool = if let Some(slot) = self.get_mut_entries().get_mut(idx) {
            slot.take().is_some()
        } else {
            false
        };
        if removed {
            self.get_mut_free_slots().push(effect_id - 1);
            self.set_live_count(self.get_live_count().saturating_sub(1));
        }
    }
}

/// Default 32-byte compact paint style (`opacity = 1.0`, transparent background, no effects).
impl Default for CompactPaintStyle {
    /// Creates a default transparent `CompactPaintStyle`.
    ///
    /// # Returns
    ///
    /// - `Self` - Default compact paint style.
    #[inline(always)]
    fn default() -> Self {
        Self {
            background: Color::default(),
            border_color: Color::default(),
            border_width: 0.0,
            radii_q4: [0; 4],
            opacity: 1.0,
            effect_id: EFFECT_ID_NONE,
            flags: 0,
        }
    }
}

/// Encoding and decoding helpers for 32-byte `CompactPaintStyle`.
impl CompactPaintStyle {
    /// Quantizes a logical corner radius in pixels into quarter-pixel `u16` fixed-point storage.
    ///
    /// # Arguments
    ///
    /// - `f32` - Logical corner radius in pixels.
    ///
    /// # Returns
    ///
    /// - `u16` - Clamped quarter-pixel fixed-point radius.
    #[must_use]
    #[inline(always)]
    fn quantize_radius(value: f32) -> u16 {
        if !value.is_finite() || value <= 0.0 {
            return 0;
        }
        let scaled: f32 = (value * COMPACT_RADIUS_SCALE).round();
        if scaled >= u16::MAX as f32 {
            u16::MAX
        } else {
            scaled as u16
        }
    }

    /// Encodes a `CursorIcon` enum discriminant into a compact `u32` byte value.
    ///
    /// # Arguments
    ///
    /// - `CursorIcon` - Cursor icon variant.
    ///
    /// # Returns
    ///
    /// - `u32` - Packed discriminant value.
    #[must_use]
    #[inline(always)]
    const fn encode_cursor(cursor: CursorIcon) -> u32 {
        match cursor {
            CursorIcon::Default => 0,
            CursorIcon::Pointer => 1,
            CursorIcon::Text => 2,
            CursorIcon::Crosshair => 3,
            CursorIcon::Move => 4,
            CursorIcon::NotAllowed => 5,
            CursorIcon::EwResize => 6,
            CursorIcon::NsResize => 7,
            CursorIcon::Grab => 8,
            CursorIcon::Grabbing => 9,
        }
    }

    /// Decodes a compact `u32` byte discriminant back into `CursorIcon`.
    ///
    /// # Arguments
    ///
    /// - `u32` - Packed discriminant value.
    ///
    /// # Returns
    ///
    /// - `CursorIcon` - Decoded cursor icon.
    #[must_use]
    #[inline(always)]
    const fn decode_cursor(bits: u32) -> CursorIcon {
        match bits {
            1 => CursorIcon::Pointer,
            2 => CursorIcon::Text,
            3 => CursorIcon::Crosshair,
            4 => CursorIcon::Move,
            5 => CursorIcon::NotAllowed,
            6 => CursorIcon::EwResize,
            7 => CursorIcon::NsResize,
            8 => CursorIcon::Grab,
            9 => CursorIcon::Grabbing,
            _ => CursorIcon::Default,
        }
    }

    /// Encodes a `PointerEvents` enum discriminant into a compact `u32` value.
    ///
    /// # Arguments
    ///
    /// - `PointerEvents` - Pointer hit-test policy.
    ///
    /// # Returns
    ///
    /// - `u32` - Packed discriminant value.
    #[must_use]
    #[inline(always)]
    const fn encode_pointer_events(policy: PointerEvents) -> u32 {
        match policy {
            PointerEvents::Auto => 0,
            PointerEvents::None => 1,
        }
    }

    /// Decodes a compact `u32` value back into `PointerEvents`.
    ///
    /// # Arguments
    ///
    /// - `u32` - Packed discriminant value.
    ///
    /// # Returns
    ///
    /// - `PointerEvents` - Decoded pointer policy.
    #[must_use]
    #[inline(always)]
    const fn decode_pointer_events(bits: u32) -> PointerEvents {
        match bits {
            1 => PointerEvents::None,
            _ => PointerEvents::Auto,
        }
    }

    /// Packs a `PaintStyle` and its sparse `effect_id` into a 32-byte `CompactPaintStyle`.
    ///
    /// # Arguments
    ///
    /// - `&PaintStyle` - Source paint style reference.
    /// - `u32` - 1-based `EffectTable` handle (`EFFECT_ID_NONE` when absent).
    ///
    /// # Returns
    ///
    /// - `Self` - Packed 32-byte compact paint style.
    #[must_use]
    #[inline(always)]
    pub fn from_paint(paint: &PaintStyle, effect_id: u32) -> Self {
        let bg_opt: Option<Color> = paint.background_color();
        let border: Border = paint.get_border();
        let radius: CornerRadius = paint.get_radius();
        let mut flags: u32 = 0;
        if bg_opt.is_some() {
            flags |= COMPACT_PAINT_HAS_BACKGROUND;
        }
        if paint.get_stop_scroll_propagation() {
            flags |= COMPACT_PAINT_STOP_SCROLL;
        }
        if paint.get_stop_event_propagation() {
            flags |= COMPACT_PAINT_STOP_EVENT;
        }
        flags |= Self::encode_cursor(paint.get_cursor()) << COMPACT_PAINT_CURSOR_SHIFT;
        flags |=
            Self::encode_pointer_events(paint.get_pointer_events()) << COMPACT_PAINT_POINTER_SHIFT;
        Self {
            background: bg_opt.unwrap_or_default(),
            border_color: border.get_color(),
            border_width: border.get_width(),
            radii_q4: [
                Self::quantize_radius(radius.get_top_left()),
                Self::quantize_radius(radius.get_top_right()),
                Self::quantize_radius(radius.get_bottom_right()),
                Self::quantize_radius(radius.get_bottom_left()),
            ],
            opacity: paint.get_opacity(),
            effect_id,
            flags,
        }
    }

    /// Returns the solid background color if `COMPACT_PAINT_HAS_BACKGROUND` is set.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Compact paint style reference.
    ///
    /// # Returns
    ///
    /// - `Option<Color>` - Background color when set.
    #[must_use]
    #[inline(always)]
    pub fn background_color(&self) -> Option<Color> {
        if (self.get_flags() & COMPACT_PAINT_HAS_BACKGROUND) != 0 {
            Some(self.get_background())
        } else {
            None
        }
    }

    /// Reconstructs the `Border` stroke from inline width and color fields.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Compact paint style reference.
    ///
    /// # Returns
    ///
    /// - `Border` - Decoded border stroke.
    #[must_use]
    #[inline(always)]
    pub fn border(&self) -> Border {
        Border::new(self.get_border_width(), self.get_border_color())
    }

    /// Reconstructs per-corner radii from quarter-pixel fixed-point storage.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Compact paint style reference.
    ///
    /// # Returns
    ///
    /// - `CornerRadius` - Decoded corner radii.
    #[must_use]
    #[inline(always)]
    pub fn corner_radius(&self) -> CornerRadius {
        let q: [u16; 4] = self.get_radii_q4();
        CornerRadius::new(
            q[0] as f32 * COMPACT_RADIUS_INV_SCALE,
            q[1] as f32 * COMPACT_RADIUS_INV_SCALE,
            q[2] as f32 * COMPACT_RADIUS_INV_SCALE,
            q[3] as f32 * COMPACT_RADIUS_INV_SCALE,
        )
    }

    /// Decodes the packed `CursorIcon` hint.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Compact paint style reference.
    ///
    /// # Returns
    ///
    /// - `CursorIcon` - Cursor icon hint.
    #[must_use]
    #[inline(always)]
    pub fn cursor(&self) -> CursorIcon {
        let bits: u32 =
            (self.get_flags() >> COMPACT_PAINT_CURSOR_SHIFT) & COMPACT_PAINT_CURSOR_MASK;
        Self::decode_cursor(bits)
    }

    /// Decodes the packed `PointerEvents` policy.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Compact paint style reference.
    ///
    /// # Returns
    ///
    /// - `PointerEvents` - Pointer hit-testing policy.
    #[must_use]
    #[inline(always)]
    pub fn pointer_events(&self) -> PointerEvents {
        let bits: u32 =
            (self.get_flags() >> COMPACT_PAINT_POINTER_SHIFT) & COMPACT_PAINT_POINTER_MASK;
        Self::decode_pointer_events(bits)
    }

    /// Returns whether scroll chaining stops at this node.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Compact paint style reference.
    ///
    /// # Returns
    ///
    /// - `bool` - True when scroll propagation is blocked.
    #[must_use]
    #[inline(always)]
    pub fn stop_scroll_propagation(&self) -> bool {
        (self.get_flags() & COMPACT_PAINT_STOP_SCROLL) != 0
    }

    /// Returns whether pointer/click event bubbling stops at this node.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Compact paint style reference.
    ///
    /// # Returns
    ///
    /// - `bool` - True when event bubbling is blocked.
    #[must_use]
    #[inline(always)]
    pub fn stop_event_propagation(&self) -> bool {
        (self.get_flags() & COMPACT_PAINT_STOP_EVENT) != 0
    }
}