euv-core 0.53.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
use super::*;

/// Returns `true` when `node` forms a Tier 0 top-layer overlay barrier (`Portal` or `Position::Fixed`).
///
/// # Arguments
///
/// - `&SceneNode` - Candidate scene node.
///
/// # Returns
///
/// - `bool` - Whether scroll events must not pass behind `node`.
#[must_use]
pub(crate) fn is_overlay_scroll_barrier(node: &SceneNode) -> bool {
    matches!(node.get_kind(), NodeKind::Portal)
        || node.style_ref().layout_ref().get_position() == Position::Fixed
}

/// Computes normalized scroll bounds and current offsets for `node` across standard and virtualized containers.
///
/// # Arguments
///
/// - `&SceneNode` - Target scene node.
///
/// # Returns
///
/// - `ScrollAxisMetrics` - Scrollability extents and current offsets for both axes.
#[must_use]
pub(crate) fn node_scroll_metrics(node: &SceneNode) -> ScrollAxisMetrics {
    let is_scroll_box: bool = node.style_ref().layout_ref().get_overflow() == Overflow::Scroll;
    if !is_scroll_box {
        return ScrollAxisMetrics::default();
    }
    let layout_box: LayoutBox = node.get_layout_box();
    let view_w: f32 = layout_box.get_content_bounds().get_width().max(0.0);
    let view_h: f32 = layout_box.get_content_bounds().get_height().max(0.0);
    let max_x: f32 = (layout_box.get_content_size().get_width() - view_w).max(0.0);
    let cur_x: f32 = layout_box.get_scroll_offset().get_x().clamp(0.0, max_x);
    if let Some((vy, total_h)) = node.virtual_scroll_metrics() {
        let max_y: f32 = (total_h - view_h).max(0.0);
        let cur_y: f32 = vy.clamp(0.0, max_y);
        ScrollAxisMetrics::new(true, true, cur_x, max_x, cur_y, max_y, total_h)
    } else {
        let max_y: f32 = (layout_box.get_content_size().get_height() - view_h).max(0.0);
        let cur_y: f32 = layout_box.get_scroll_offset().get_y().clamp(0.0, max_y);
        ScrollAxisMetrics::new(true, false, cur_x, max_x, cur_y, max_y, 0.0)
    }
}

/// Returns `true` when an axis has scrollable overflow and remaining travel in the direction of `delta`.
///
/// # Arguments
///
/// - `f32` - Current scroll offset along the axis.
/// - `f32` - Maximum scrollable offset along the axis.
/// - `f32` - Incoming scroll delta along the axis (negative scrolls toward `0.0`, positive toward `max_scroll`).
///
/// # Returns
///
/// - `bool` - Whether this axis can consume at least part of `delta`.
#[must_use]
pub(crate) fn axis_can_scroll(cur: f32, max_scroll: f32, delta: f32) -> bool {
    if max_scroll <= SCROLL_OVERFLOW_EPS || delta.abs() <= SCROLL_DELTA_EPS {
        return false;
    }
    let clamped: f32 = cur.clamp(0.0, max_scroll);
    if delta < 0.0 {
        clamped > SCROLL_BOUNDARY_EPS
    } else {
        clamped < max_scroll - SCROLL_BOUNDARY_EPS
    }
}

/// Applies `delta` along one scroll axis, clamping cleanly at `[0.0, max_scroll]` and returning any unconsumed overscroll delta.
///
/// # Arguments
///
/// - `f32` - Current scroll offset along the axis.
/// - `f32` - Maximum scrollable offset along the axis.
/// - `f32` - Incoming scroll delta along the axis.
///
/// # Returns
///
/// - `(f32, f32, f32)` - Tuple `(next_offset, consumed_delta, remaining_delta)`.
#[must_use]
pub(crate) fn consume_scroll_axis(cur: f32, max_scroll: f32, delta: f32) -> (f32, f32, f32) {
    if max_scroll <= SCROLL_OVERFLOW_EPS || delta.abs() <= SCROLL_DELTA_EPS {
        return (cur.clamp(0.0, max_scroll), 0.0, delta);
    }
    let clamped_cur: f32 = cur.clamp(0.0, max_scroll);
    let next: f32 = (clamped_cur + delta).clamp(0.0, max_scroll);
    let consumed: f32 = next - clamped_cur;
    if consumed.abs() <= SCROLL_DELTA_EPS {
        (clamped_cur, 0.0, delta)
    } else {
        (next, consumed, delta - consumed)
    }
}

/// Determines whether `node` intentionally terminates scroll chaining for the remaining `(rem_dx, rem_dy)` delta.
///
/// # Arguments
///
/// - `&SceneNode` - Current node along the ancestor chain.
/// - `ScrollAxisMetrics` - Precomputed scroll metrics for `node`.
/// - `f32` - Remaining horizontal scroll delta.
/// - `f32` - Remaining vertical scroll delta.
///
/// # Returns
///
/// - `bool` - `true` when scroll propagation must stop at `node`.
#[must_use]
pub(crate) fn node_blocks_scroll_chaining(
    node: &SceneNode,
    metrics: ScrollAxisMetrics,
    rem_dx: f32,
    rem_dy: f32,
) -> bool {
    if is_overlay_scroll_barrier(node) {
        return true;
    }
    if !node.style_ref().paint_ref().get_stop_scroll_propagation() {
        return false;
    }
    if !metrics.get_is_scroll_box() {
        return true;
    }
    let blocks_x: bool = axis_can_scroll(metrics.get_cur_x(), metrics.get_max_x(), rem_dx);
    let blocks_y: bool = axis_can_scroll(metrics.get_cur_y(), metrics.get_max_y(), rem_dy);
    blocks_x || blocks_y
}

/// Finds the innermost ancestor starting at `start` that can scroll in `delta`'s direction without passing an overlay or scroll-lock barrier.
///
/// # Arguments
///
/// - `&SceneTree` - Retained scene tree.
/// - `Option<NodeId>` - Starting hit node handle.
/// - `Point` - Desired scroll delta `(dx, dy)`.
///
/// # Returns
///
/// - `Option<NodeId>` - First scrollable ancestor with remaining extent in `delta`'s direction.
#[must_use]
pub(crate) fn find_scroll_chain_target(
    tree: &SceneTree,
    start: Option<NodeId>,
    delta: Point,
) -> Option<NodeId> {
    let dx: f32 = delta.get_x();
    let dy: f32 = delta.get_y();
    let mut cursor: Option<NodeId> = start;
    let mut first_scroll_box: Option<NodeId> = None;
    while let Some(aid) = cursor {
        let Some(node) = tree.get_node(aid) else {
            break;
        };
        let metrics: ScrollAxisMetrics = node_scroll_metrics(node);
        if metrics.get_is_scroll_box() {
            if first_scroll_box.is_none() {
                first_scroll_box = Some(aid);
            }
            let can_x: bool = axis_can_scroll(metrics.get_cur_x(), metrics.get_max_x(), dx);
            let can_y: bool = axis_can_scroll(metrics.get_cur_y(), metrics.get_max_y(), dy);
            if can_x || can_y {
                return Some(aid);
            }
        }
        if node_blocks_scroll_chaining(node, metrics, dx, dy) {
            break;
        }
        cursor = node.parent_id();
    }
    if let Some(container_id) = first_scroll_box.or(start) {
        let allow_descendant_fallback: bool = tree.child_count(container_id) <= 1
            || tree
                .get_node(container_id)
                .is_some_and(|scene_node: &SceneNode| {
                    matches!(
                        scene_node.style_ref().get_layout().get_flex_direction(),
                        FlexDirection::Row | FlexDirection::RowReverse
                    )
                });
        if !allow_descendant_fallback {
            return None;
        }
        let (allow_x, allow_y): (bool, bool) = match first_scroll_box
            .and_then(|id: NodeId| tree.get_node(id))
            .map(node_scroll_metrics)
        {
            Some(matrix_elems) => (
                dx.abs() > SCROLL_DELTA_EPS && matrix_elems.get_max_x() <= SCROLL_OVERFLOW_EPS,
                dy.abs() > SCROLL_DELTA_EPS && matrix_elems.get_max_y() <= SCROLL_OVERFLOW_EPS,
            ),
            None => (dx.abs() > SCROLL_DELTA_EPS, dy.abs() > SCROLL_DELTA_EPS),
        };
        if !allow_x && !allow_y {
            return None;
        }
        for child_id in tree.children_iter(container_id) {
            if !tree.is_node_visible(child_id) {
                continue;
            }
            if let Some(child) = tree.get_node(child_id) {
                if child.style_ref().paint_ref().get_pointer_events() == PointerEvents::None
                    || is_overlay_scroll_barrier(child)
                {
                    continue;
                }
                let cmetrics: ScrollAxisMetrics = node_scroll_metrics(child);
                if cmetrics.get_is_scroll_box()
                    && ((allow_x
                        && axis_can_scroll(cmetrics.get_cur_x(), cmetrics.get_max_x(), dx))
                        || (allow_y
                            && axis_can_scroll(cmetrics.get_cur_y(), cmetrics.get_max_y(), dy)))
                {
                    return Some(child_id);
                }
                for grand_id in tree.children_iter(child_id) {
                    if !tree.is_node_visible(grand_id) {
                        continue;
                    }
                    if let Some(grand) = tree.get_node(grand_id) {
                        if grand.style_ref().paint_ref().get_pointer_events() == PointerEvents::None
                            || is_overlay_scroll_barrier(grand)
                        {
                            continue;
                        }
                        let gmetrics: ScrollAxisMetrics = node_scroll_metrics(grand);
                        if gmetrics.get_is_scroll_box()
                            && ((allow_x
                                && axis_can_scroll(gmetrics.get_cur_x(), gmetrics.get_max_x(), dx))
                                || (allow_y
                                    && axis_can_scroll(
                                        gmetrics.get_cur_y(),
                                        gmetrics.get_max_y(),
                                        dy,
                                    )))
                        {
                            return Some(grand_id);
                        }
                    }
                }
            }
        }
    }
    None
}

/// Locates the first visible flow child intersecting the top of `scroll_id`'s viewport to anchor across dynamic re-layouts.
///
/// # Arguments
///
/// - `&SceneTree` - Retained scene tree prior to layout.
/// - `NodeId` - Active scrolled container node handle.
/// - `f32` - Current visible viewport top coordinate in layout space.
/// - `f32` - Current visible viewport bottom coordinate in layout space.
///
/// # Returns
///
/// - `Option<NodeId>` - Stable visual anchor descendant handle, if any.
#[must_use]
fn find_scroll_anchor_node(
    tree: &SceneTree,
    scroll_id: NodeId,
    view_top: f32,
    view_bottom: f32,
) -> Option<NodeId> {
    let mut container_id: NodeId = scroll_id;
    for _ in 0..SCROLL_ANCHOR_MAX_WRAPPER_DEPTH {
        let mut visible_flow_child: Option<NodeId> = None;
        let mut flow_count: usize = 0;
        for child_id in tree.children_iter(container_id) {
            let Some(child_node) = tree.get_node(child_id) else {
                continue;
            };
            let child_layout: &LayoutStyle = child_node.layout_ref();
            if child_layout.get_display() == Display::None
                || child_layout.get_position() != Position::Relative
            {
                continue;
            }
            flow_count += 1;
            visible_flow_child = Some(child_id);
            if flow_count > 1 {
                break;
            }
        }
        if flow_count == 1
            && let Some(only_child) = visible_flow_child
            && let Some(only_node) = tree.get_node(only_child)
            && only_node.layout_ref().get_overflow() != Overflow::Scroll
            && only_node.layout_ref().get_flex_direction() == FlexDirection::Column
            && tree.has_children(only_child)
        {
            container_id = only_child;
        } else {
            break;
        }
    }
    let container_node: &SceneNode = tree.get_node(container_id)?;
    if container_node.layout_ref().get_flex_direction() != FlexDirection::Column
        || container_node.layout_ref().get_justify_content() != JustifyContent::Start
    {
        return None;
    }
    let dirty_layout_base: u16 = DIRTY_LAYOUT | DIRTY_CHILDREN;
    let dirty_layout_mask: u16 = dirty_layout_base | DIRTY_SUBTREE_LAYOUT;
    let mut fallback_anchor: Option<NodeId> = None;
    for child_id in tree.children_iter(container_id) {
        let Some(child_node) = tree.get_node(child_id) else {
            continue;
        };
        let child_layout: &LayoutStyle = child_node.layout_ref();
        if child_layout.get_display() == Display::None
            || child_layout.get_position() != Position::Relative
        {
            continue;
        }
        let bounds: Rect = child_node.layout_box_ref().get_bounds();
        if bounds.get_height() <= 0.0 || bounds.bottom() <= view_top + SCROLL_ANCHOR_DELTA_EPS {
            continue;
        }
        if bounds.get_y() >= view_bottom {
            break;
        }
        if (child_node.get_dirty().get_bits() & dirty_layout_mask) == 0 {
            return Some(child_id);
        }
        if fallback_anchor.is_none() && bounds.get_y() >= view_top - SCROLL_ANCHOR_DELTA_EPS {
            fallback_anchor = Some(child_id);
        }
    }
    fallback_anchor
}

/// Captures pre-layout scroll anchors for visible scrolled containers whose subtrees are about to re-layout.
///
/// # Arguments
///
/// - `&SceneTree` - Retained scene tree prior to layout.
/// - `&mut Vec<ScrollAnchorSnapshot>` - Reusable output buffer cleared and populated in place.
pub(crate) fn capture_scroll_anchors(tree: &SceneTree, out: &mut Vec<ScrollAnchorSnapshot>) {
    out.clear();
    let subtree_mask: u16 = DIRTY_SUBTREE_LAYOUT | DIRTY_CHILDREN;
    for &candidate in tree.scroll_nodes().iter() {
        let Some(node) = tree.get_node(candidate) else {
            continue;
        };
        if !node.get_alive()
            || !tree.is_node_visible(candidate)
            || node.layout_ref().get_overflow() != Overflow::Scroll
            || node.virtual_scroll_metrics().is_some()
            || (node.get_dirty().get_bits() & subtree_mask) == 0
        {
            continue;
        }
        let layout_box: &LayoutBox = node.layout_box_ref();
        let cur_scroll_y: f32 = layout_box.get_scroll_offset().get_y();
        if cur_scroll_y <= SCROLL_ANCHOR_MIN_OFFSET_DP {
            continue;
        }
        let view_rect: Rect = layout_box.get_content_bounds();
        let view_h: f32 = view_rect.get_height();
        let old_content_h: f32 = layout_box.get_content_size().get_height();
        if view_h <= 1.0 || old_content_h <= view_h + SCROLL_OVERFLOW_EPS {
            continue;
        }
        let view_top: f32 = view_rect.get_y() + cur_scroll_y;
        let view_bottom: f32 = view_top + view_h;
        if let Some(anchor_id) = find_scroll_anchor_node(tree, candidate, view_top, view_bottom)
            && let Some(anchor_node) = tree.get_node(anchor_id)
        {
            let old_anchor_rel_y: f32 =
                anchor_node.layout_box_ref().get_bounds().get_y() - view_rect.get_y();
            out.push(ScrollAnchorSnapshot::new(
                candidate,
                anchor_id,
                old_anchor_rel_y,
            ));
        }
    }
}

/// Applies captured pre-layout scroll anchors after layout so height changes above the viewport keep scrolled content visually pinned.
///
/// # Arguments
///
/// - `&mut SceneTree` - Mutable retained scene tree immediately after layout.
/// - `&[ScrollAnchorSnapshot]` - Anchor snapshots captured immediately before layout.
///
/// # Returns
///
/// - `bool` - `true` when at least one scroll container's offset was adjusted.
pub(crate) fn apply_scroll_anchors(tree: &mut SceneTree, anchors: &[ScrollAnchorSnapshot]) -> bool {
    let mut adjusted_any: bool = false;
    for snap in anchors {
        let scroll_id: NodeId = snap.get_scroll_id();
        let anchor_id: NodeId = snap.get_anchor_id();
        if !tree.is_node_visible(scroll_id) || !tree.is_node_visible(anchor_id) {
            continue;
        }
        let next_offset_opt: Option<Point> =
            match (tree.get_node(scroll_id), tree.get_node(anchor_id)) {
                (Some(scroll_node), Some(anchor_node)) => {
                    let layout_box: &LayoutBox = scroll_node.layout_box_ref();
                    let view_rect: Rect = layout_box.get_content_bounds();
                    let view_h: f32 = view_rect.get_height();
                    let new_content_h: f32 = layout_box.get_content_size().get_height();
                    if view_h <= 1.0 {
                        None
                    } else {
                        let new_anchor_rel_y: f32 =
                            anchor_node.layout_box_ref().get_bounds().get_y() - view_rect.get_y();
                        let delta_y: f32 = new_anchor_rel_y - snap.get_old_anchor_rel_y();
                        if delta_y.abs() <= SCROLL_ANCHOR_DELTA_EPS {
                            None
                        } else {
                            let max_y: f32 = (new_content_h - view_h).max(0.0);
                            let cur: Point = layout_box.get_scroll_offset();
                            let next_y: f32 = (cur.get_y() + delta_y).clamp(0.0, max_y);
                            if (next_y - cur.get_y()).abs() > SCROLL_DELTA_EPS {
                                Some(Point::new(cur.get_x(), next_y))
                            } else {
                                None
                            }
                        }
                    }
                }
                _ => None,
            };
        if let Some(next_offset) = next_offset_opt {
            tree.set_scroll_offset(scroll_id, next_offset);
            adjusted_any = true;
        }
    }
    adjusted_any
}

/// Re-clamps all live visible non-virtual scroll container offsets into their latest laid-out `[0, max_scroll]` extents without resetting unlaid-out containers.
///
/// # Arguments
///
/// - `&mut SceneTree` - Mutable retained scene tree after layout.
///
/// # Returns
///
/// - `bool` - `true` when at least one scroll container's offset was clamped.
pub(crate) fn clamp_tree_scroll_offsets(tree: &mut SceneTree) -> bool {
    let mut clamped_any: bool = false;
    let scroll_count: usize = tree.scroll_nodes().len();
    for idx in 0..scroll_count {
        let candidate: NodeId = tree.scroll_nodes()[idx];
        if !tree.is_node_visible(candidate) {
            continue;
        }
        let has_children: bool = tree.has_children(candidate);
        let clamp_target: Option<Point> = tree.get_node(candidate).and_then(|node: &SceneNode| {
            if !node.get_alive()
                || node.layout_ref().get_overflow() != Overflow::Scroll
                || node.virtual_scroll_metrics().is_some()
            {
                return None;
            }
            let layout_box: &LayoutBox = node.layout_box_ref();
            let view_w: f32 = layout_box.get_content_bounds().get_width();
            let view_h: f32 = layout_box.get_content_bounds().get_height();
            let content_w: f32 = layout_box.get_content_size().get_width();
            let content_h: f32 = layout_box.get_content_size().get_height();
            if (view_w <= 0.0 && view_h <= 0.0)
                || (has_children && content_w <= 0.0 && content_h <= 0.0)
            {
                return None;
            }
            let max_x: f32 = (content_w - view_w).max(0.0);
            let max_y: f32 = (content_h - view_h).max(0.0);
            let cur: Point = layout_box.get_scroll_offset();
            let clamped: Point =
                Point::new(cur.get_x().clamp(0.0, max_x), cur.get_y().clamp(0.0, max_y));
            if (clamped.get_x() - cur.get_x()).abs() > SCROLL_DELTA_EPS
                || (clamped.get_y() - cur.get_y()).abs() > SCROLL_DELTA_EPS
            {
                Some(clamped)
            } else {
                None
            }
        });
        if let Some(clamped) = clamp_target {
            tree.set_scroll_offset(candidate, clamped);
            clamped_any = true;
        }
    }
    clamped_any
}