strata-ui 0.1.7

Universal frontend framework in pure Rust with pixel-perfect CSS emulation and React/TSX ergonomics
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
//! Visual scrollbar thumb rendering and drag interaction handling.

use super::*;

impl AppRuntime {
    fn resolve_layout_node_at_path(&self, path: &[usize]) -> Option<&LayoutNode> {
        let mut cur_bp = &self.root_blueprint;
        let mut cur_layout = &self.layout_root;
        for &idx in path {
            let (_, next_bp, next_layout) = Self::paired_blueprint_children(cur_bp, cur_layout)
                .into_iter()
                .find(|(i, _, _)| *i == idx)?;
            cur_bp = next_bp;
            cur_layout = next_layout;
        }
        Some(cur_layout)
    }

    /// Returns `true` when `(px, py)` is owned by `bar` at the top of the CSS stacking order
    /// (i.e., not occluded by a higher z-index sibling/floating window, a Top-Layer `<Portal>`,
    /// or a positive z-index overlay mounted inside `bar`).
    pub(crate) fn scrollbar_owns_point(
        &self,
        bar: &ActiveScrollbar,
        px: f32,
        py: f32,
        cached_hit: &mut Option<Option<Vec<usize>>>,
    ) -> bool {
        // Clamp probe inside `bar.bounds` so Fitts's law +1px right-edge hits still test
        // against the true top-most stacking context at the right edge of the scroll container.
        let probe_x = px.clamp(
            bar.bounds.x,
            (bar.bounds.x + bar.bounds.width).max(bar.bounds.x),
        );
        let probe_y = py.clamp(
            bar.bounds.y,
            (bar.bounds.y + bar.bounds.height).max(bar.bounds.y),
        );
        let direct_probe;
        let hit_path_opt = if (probe_x - px).abs() < 1e-4 && (probe_y - py).abs() < 1e-4 {
            cached_hit
                .get_or_insert_with(|| {
                    Self::hit_test_node_path(&self.root_blueprint, &self.layout_root, px, py)
                })
                .as_ref()
        } else {
            direct_probe =
                Self::hit_test_node_path(&self.root_blueprint, &self.layout_root, probe_x, probe_y);
            direct_probe.as_ref()
        };

        let Some(hit_path) = hit_path_opt else {
            return false;
        };
        if !hit_path.starts_with(&bar.path) {
            return false;
        }

        // Verify no descendant strictly inside `bar` along `hit_path` established a Top-Layer
        // portal or positive z-index stacking context that paints above `bar`'s scrollbar.
        let mut cur_bp = &self.root_blueprint;
        let mut cur_layout = &self.layout_root;
        for (depth, &idx) in hit_path.iter().enumerate() {
            let parent_display = Some(cur_layout.style.display);
            let Some((_, next_bp, next_layout)) =
                Self::paired_blueprint_children(cur_bp, cur_layout)
                    .into_iter()
                    .find(|(i, _, _)| *i == idx)
            else {
                break;
            };
            cur_bp = next_bp;
            cur_layout = next_layout;
            if depth + 1 > bar.path.len() {
                if cur_layout.is_top_layer {
                    return false;
                }
                if cur_layout.establishes_stacking_context_in(parent_display, false)
                    && cur_layout.style.z_index.unwrap_or(0) > 0
                {
                    return false;
                }
            }
        }
        true
    }

    #[inline]
    fn scrollbar_geom_contains_hover(bar: &ActiveScrollbar, mx: f32, my: f32) -> bool {
        bar.clip_bounds.contains_point(mx, my)
            && (bar.thumb_bounds.contains_point(mx, my)
                || bar.track_bounds.contains_point(mx, my)
                || (mx >= bar.track_bounds.x
                    && mx <= bar.bounds.x + bar.bounds.width + 1.0
                    && my >= bar.track_bounds.y
                    && my <= bar.track_bounds.y + bar.track_bounds.height))
    }

    /// Builds inline [`strata_render::compositor::ScrollbarOverlay`] descriptors bound to each
    /// overflowing scroll container node so the compositor paints every scrollbar thumb inside its
    /// owning stacking context and clip bounds.
    pub(crate) fn build_scrollbar_overlays(
        &self,
    ) -> Vec<strata_render::compositor::ScrollbarOverlay> {
        if self.active_scrollbars.is_empty() {
            return Vec::new();
        }
        let (mx, my) = self.last_mouse_pos;
        let is_recent_scroll = self
            .last_scroll_interaction
            .is_some_and(|t| self.elapsed_since(t).as_millis() < 700);
        let mut cached_hit: Option<Option<Vec<usize>>> = None;
        let mut overlays = Vec::with_capacity(self.active_scrollbars.len());

        for bar in &self.active_scrollbars {
            if bar.max_scroll_y <= 1.0
                || bar.clip_bounds.width <= 0.0
                || bar.clip_bounds.height <= 0.0
            {
                continue;
            }
            let Some(node) = self.resolve_layout_node_at_path(&bar.path) else {
                continue;
            };
            let is_dragging = self
                .scrollbar_drag
                .as_ref()
                .is_some_and(|d| d.path == bar.path);
            let is_kinetic = self
                .active_smooth_scroll
                .as_ref()
                .is_some_and(|s| s.container_path == bar.path);
            let is_hovered = Self::scrollbar_geom_contains_hover(bar, mx, my)
                && self.scrollbar_owns_point(bar, mx, my, &mut cached_hit);
            let is_active = is_dragging || is_hovered || is_kinetic || is_recent_scroll;
            let alpha = if is_active { 150u8 } else { 60u8 };

            let local_border = node.geometry.border_box;
            let local_padding = node.geometry.padding_box;
            let untransformed = (local_border.x - bar.bounds.x).abs() < 0.5
                && (local_border.y - bar.bounds.y).abs() < 0.5
                && (local_border.width - bar.bounds.width).abs() < 0.5
                && (local_border.height - bar.bounds.height).abs() < 0.5;

            let (mut thumb_bounds, track_bounds, container_bounds, clip_bounds) = if untransformed {
                (bar.thumb_bounds, bar.track_bounds, bar.bounds, bar.clip_bounds)
            } else {
                let track_width = 10.0;
                let inner_box = if local_padding.width > 0.0 && local_padding.height > 0.0 {
                    local_padding
                } else {
                    local_border
                };
                let track_x = (inner_box.x + inner_box.width - track_width).max(inner_box.x);
                let track_y = inner_box.y;
                let track_h = inner_box.height;
                let local_track = strata_layout::Rect::new(track_x, track_y, track_width, track_h);
                let min_thumb = 24.0_f32.min(track_h);
                let viewport_h = node.geometry.content_box.height.max(1.0);
                let thumb_h =
                    ((viewport_h / bar.content_h.max(1.0)) * track_h).clamp(min_thumb, track_h);
                let avail = (track_h - thumb_h).max(0.0);
                let thumb_y = if bar.max_scroll_y > 0.0 && avail > 0.0 {
                    track_y + (bar.scroll_y / bar.max_scroll_y) * avail
                } else {
                    track_y
                };
                let local_thumb = strata_layout::Rect::new(track_x, thumb_y, track_width, thumb_h);
                (local_thumb, local_track, local_border, local_border)
            };

            if is_active {
                let expand_px = 2.0;
                thumb_bounds.x = (thumb_bounds.x - expand_px).max(container_bounds.x);
                thumb_bounds.width = (thumb_bounds.width + expand_px)
                    .min((container_bounds.x + container_bounds.width) - thumb_bounds.x);
            }
            let min_y = track_bounds.y;
            let max_y = track_bounds.y + track_bounds.height;
            let clamped_top = thumb_bounds.y.clamp(min_y, max_y);
            let clamped_bot = (thumb_bounds.y + thumb_bounds.height).clamp(clamped_top, max_y);
            thumb_bounds.y = clamped_top;
            thumb_bounds.height = clamped_bot - clamped_top;
            if thumb_bounds.width <= 0.0 || thumb_bounds.height <= 0.0 {
                continue;
            }

            overlays.push(strata_render::compositor::ScrollbarOverlay {
                node_ptr: node as *const LayoutNode as usize,
                clip_bounds,
                clip_radius: bar.border_radius,
                thumb_bounds,
                color: strata_style::types::Color::rgba(255, 255, 255, alpha),
                thumb_radius: strata_style::types::BorderRadius::all(3.0),
            });
        }

        overlays
    }

    /// Renders sleek semi-transparent scrollbar thumbs for all overflowing scroll containers.
    pub fn render_scrollbars(&mut self) {
        self.render_scrollbars_filtered(None);
    }

    /// Renders overflowing scrollbar thumbs optionally filtered to document (`Some(false)`) or Top-Layer (`Some(true)`).
    pub fn render_scrollbars_filtered(&mut self, top_layer: Option<bool>) {
        let (mx, my) = self.last_mouse_pos;
        let is_recent_scroll = self
            .last_scroll_interaction
            .is_some_and(|t| self.elapsed_since(t).as_millis() < 700);
        let mut cached_hit: Option<Option<Vec<usize>>> = None;

        for bar in &self.active_scrollbars {
            if let Some(tl) = top_layer {
                if bar.is_top_layer != tl {
                    continue;
                }
            }
            if bar.max_scroll_y <= 1.0
                || bar.clip_bounds.width <= 0.0
                || bar.clip_bounds.height <= 0.0
            {
                continue;
            }
            let is_dragging = self
                .scrollbar_drag
                .as_ref()
                .is_some_and(|d| d.path == bar.path);
            let is_kinetic = self
                .active_smooth_scroll
                .as_ref()
                .is_some_and(|s| s.container_path == bar.path);
            let is_hovered = Self::scrollbar_geom_contains_hover(bar, mx, my)
                && self.scrollbar_owns_point(bar, mx, my, &mut cached_hit);
            let is_active = is_dragging || is_hovered || is_kinetic || is_recent_scroll;
            let alpha = if is_active { 150u8 } else { 60u8 };

            let mut thumb_bounds = bar.thumb_bounds;
            if is_active {
                let expand_px = 2.0;
                thumb_bounds.x = (thumb_bounds.x - expand_px).max(bar.bounds.x);
                thumb_bounds.width = (thumb_bounds.width + expand_px)
                    .min((bar.bounds.x + bar.bounds.width) - thumb_bounds.x);
            }
            let min_y = bar.track_bounds.y;
            let max_y = bar.track_bounds.y + bar.track_bounds.height;
            let clamped_top = thumb_bounds.y.clamp(min_y, max_y);
            let clamped_bot = (thumb_bounds.y + thumb_bounds.height).clamp(clamped_top, max_y);
            thumb_bounds.y = clamped_top;
            thumb_bounds.height = clamped_bot - clamped_top;
            if thumb_bounds.width <= 0.0 || thumb_bounds.height <= 0.0 {
                continue;
            }

            self.display_list
                .push(strata_render::compositor::DrawCommand::PushClip {
                    bounds: bar.clip_bounds,
                    radius: bar.border_radius,
                });
            self.display_list
                .push(strata_render::compositor::DrawCommand::DrawRoundedRect {
                    bounds: thumb_bounds,
                    color: strata_style::types::Color::rgba(255, 255, 255, alpha),
                    border_radius: strata_style::types::BorderRadius::all(3.0),
                    border_width: strata_style::types::EdgeInsets::all(0.0),
                    border_color: None,
                });
            self.display_list
                .push(strata_render::compositor::DrawCommand::PopClip);
        }
    }

    /// Checks if a pointer press landed on any unoccluded active scrollbar thumb or track and starts dragging.
    pub fn check_scrollbar_thumb_press(&mut self, px: f32, py: f32) -> bool {
        let mut cached_hit: Option<Option<Vec<usize>>> = None;
        let mut matched: Option<(ActiveScrollbar, bool)> = None;

        for bar in self.active_scrollbars.iter().rev() {
            if bar.max_scroll_y <= 1.0
                || bar.clip_bounds.width <= 0.0
                || bar.clip_bounds.height <= 0.0
                || !bar.clip_bounds.contains_point(px, py)
            {
                continue;
            }

            // Hit test check with edge-right pixel snapping tolerance (+1.0px for window edge)
            let in_x_thumb =
                px >= bar.thumb_bounds.x && px <= bar.bounds.x + bar.bounds.width + 1.0;
            let in_y_thumb =
                py >= bar.thumb_bounds.y && py <= bar.thumb_bounds.y + bar.thumb_bounds.height;
            let on_thumb = in_x_thumb && in_y_thumb;

            let in_x_track =
                px >= bar.track_bounds.x && px <= bar.bounds.x + bar.bounds.width + 1.0;
            let in_y_track =
                py >= bar.track_bounds.y && py <= bar.track_bounds.y + bar.track_bounds.height;
            let on_track = in_x_track && in_y_track;

            if !(on_thumb || on_track) {
                continue;
            }
            if !self.scrollbar_owns_point(bar, px, py, &mut cached_hit) {
                continue;
            }
            matched = Some((bar.clone(), on_thumb));
            break;
        }

        let Some((bar, on_thumb)) = matched else {
            return false;
        };

        if on_thumb {
            self.active_smooth_scroll = None;
            self.last_scroll_interaction = Some(self.now());
            self.scrollbar_drag = Some(ScrollbarDrag {
                is_id: bar.is_id,
                id: bar.id,
                path: bar.path,
                start_pointer_y: py,
                start_scroll_y: bar.scroll_y,
                available_travel: bar.available_travel,
                max_scroll_y: bar.max_scroll_y,
            });
            true
        } else {
            // Click landed on track gutter (above or below thumb):
            // Jump thumb center to clicked position and start dragging immediately.
            self.active_smooth_scroll = None;
            self.last_scroll_interaction = Some(self.now());
            let click_travel = (py - bar.track_bounds.y - bar.thumb_bounds.height / 2.0)
                .clamp(0.0, bar.available_travel);
            let target_scroll_y = if bar.available_travel > 0.0 {
                (click_travel / bar.available_travel) * bar.max_scroll_y
            } else {
                0.0
            };
            let dy = target_scroll_y - bar.scroll_y;
            let path = bar.path;
            let is_id = bar.is_id;
            let id = bar.id;
            let max_scroll_y = bar.max_scroll_y;
            let available_travel = bar.available_travel;
            let bounds_y = bar.bounds.y;

            self.apply_scroll_delta_to_container(
                &path,
                is_id,
                &id,
                target_scroll_y,
                dy,
                max_scroll_y,
                available_travel,
                bounds_y,
            );

            self.scrollbar_drag = Some(ScrollbarDrag {
                is_id,
                id,
                path,
                start_pointer_y: py,
                start_scroll_y: target_scroll_y,
                available_travel,
                max_scroll_y,
            });
            true
        }
    }

    /// Updates active scrollbar thumb drag during pointer movement.
    pub fn poll_scrollbar_drag(&mut self, _px: f32, py: f32) -> bool {
        let Some(drag) = self.scrollbar_drag.clone() else {
            return false;
        };
        if drag.available_travel <= 0.0 {
            return false;
        }

        let dy_pointer = py - drag.start_pointer_y;
        let delta_scroll = (dy_pointer / drag.available_travel) * drag.max_scroll_y;
        let new_scroll_y = (drag.start_scroll_y + delta_scroll).clamp(0.0, drag.max_scroll_y);

        if let Some(bar) = self.active_scrollbars.iter().find(|b| b.path == drag.path) {
            let cur_y = bar.scroll_y;
            let dy = new_scroll_y - cur_y;
            if dy.abs() > 0.01 {
                self.last_scroll_interaction = Some(self.now());
                self.apply_scroll_delta_to_container(
                    &drag.path,
                    drag.is_id,
                    &drag.id,
                    new_scroll_y,
                    dy,
                    drag.max_scroll_y,
                    drag.available_travel,
                    bar.bounds.y,
                );
                return true;
            }
        }

        false
    }
}