1use super::*;
4
5impl AppRuntime {
6 fn resolve_layout_node_at_path(&self, path: &[usize]) -> Option<&LayoutNode> {
7 let mut cur_bp = &self.root_blueprint;
8 let mut cur_layout = &self.layout_root;
9 for &idx in path {
10 let (_, next_bp, next_layout) = Self::paired_blueprint_children(cur_bp, cur_layout)
11 .into_iter()
12 .find(|(i, _, _)| *i == idx)?;
13 cur_bp = next_bp;
14 cur_layout = next_layout;
15 }
16 Some(cur_layout)
17 }
18
19 pub(crate) fn scrollbar_owns_point(
23 &self,
24 bar: &ActiveScrollbar,
25 px: f32,
26 py: f32,
27 cached_hit: &mut Option<Option<Vec<usize>>>,
28 ) -> bool {
29 let probe_x = px.clamp(
32 bar.bounds.x,
33 (bar.bounds.x + bar.bounds.width).max(bar.bounds.x),
34 );
35 let probe_y = py.clamp(
36 bar.bounds.y,
37 (bar.bounds.y + bar.bounds.height).max(bar.bounds.y),
38 );
39 let direct_probe;
40 let hit_path_opt = if (probe_x - px).abs() < 1e-4 && (probe_y - py).abs() < 1e-4 {
41 cached_hit
42 .get_or_insert_with(|| {
43 Self::hit_test_node_path(&self.root_blueprint, &self.layout_root, px, py)
44 })
45 .as_ref()
46 } else {
47 direct_probe =
48 Self::hit_test_node_path(&self.root_blueprint, &self.layout_root, probe_x, probe_y);
49 direct_probe.as_ref()
50 };
51
52 let Some(hit_path) = hit_path_opt else {
53 return false;
54 };
55 if !hit_path.starts_with(&bar.path) {
56 return false;
57 }
58
59 let mut cur_bp = &self.root_blueprint;
62 let mut cur_layout = &self.layout_root;
63 for (depth, &idx) in hit_path.iter().enumerate() {
64 let parent_display = Some(cur_layout.style.display);
65 let Some((_, next_bp, next_layout)) =
66 Self::paired_blueprint_children(cur_bp, cur_layout)
67 .into_iter()
68 .find(|(i, _, _)| *i == idx)
69 else {
70 break;
71 };
72 cur_bp = next_bp;
73 cur_layout = next_layout;
74 if depth + 1 > bar.path.len() {
75 if cur_layout.is_top_layer {
76 return false;
77 }
78 if cur_layout.establishes_stacking_context_in(parent_display, false)
79 && cur_layout.style.z_index.unwrap_or(0) > 0
80 {
81 return false;
82 }
83 }
84 }
85 true
86 }
87
88 #[inline]
89 fn scrollbar_geom_contains_hover(bar: &ActiveScrollbar, mx: f32, my: f32) -> bool {
90 bar.clip_bounds.contains_point(mx, my)
91 && (bar.thumb_bounds.contains_point(mx, my)
92 || bar.track_bounds.contains_point(mx, my)
93 || (mx >= bar.track_bounds.x
94 && mx <= bar.bounds.x + bar.bounds.width + 1.0
95 && my >= bar.track_bounds.y
96 && my <= bar.track_bounds.y + bar.track_bounds.height))
97 }
98
99 pub(crate) fn build_scrollbar_overlays(
103 &self,
104 ) -> Vec<strata_render::compositor::ScrollbarOverlay> {
105 if self.active_scrollbars.is_empty() {
106 return Vec::new();
107 }
108 let (mx, my) = self.last_mouse_pos;
109 let is_recent_scroll = self
110 .last_scroll_interaction
111 .is_some_and(|t| self.elapsed_since(t).as_millis() < 700);
112 let mut cached_hit: Option<Option<Vec<usize>>> = None;
113 let mut overlays = Vec::with_capacity(self.active_scrollbars.len());
114
115 for bar in &self.active_scrollbars {
116 if bar.max_scroll_y <= 1.0
117 || bar.clip_bounds.width <= 0.0
118 || bar.clip_bounds.height <= 0.0
119 {
120 continue;
121 }
122 let Some(node) = self.resolve_layout_node_at_path(&bar.path) else {
123 continue;
124 };
125 let is_dragging = self
126 .scrollbar_drag
127 .as_ref()
128 .is_some_and(|d| d.path == bar.path);
129 let is_kinetic = self
130 .active_smooth_scroll
131 .as_ref()
132 .is_some_and(|s| s.container_path == bar.path);
133 let is_hovered = Self::scrollbar_geom_contains_hover(bar, mx, my)
134 && self.scrollbar_owns_point(bar, mx, my, &mut cached_hit);
135 let is_active = is_dragging || is_hovered || is_kinetic || is_recent_scroll;
136 let alpha = if is_active { 150u8 } else { 60u8 };
137
138 let local_border = node.geometry.border_box;
139 let local_padding = node.geometry.padding_box;
140 let untransformed = (local_border.x - bar.bounds.x).abs() < 0.5
141 && (local_border.y - bar.bounds.y).abs() < 0.5
142 && (local_border.width - bar.bounds.width).abs() < 0.5
143 && (local_border.height - bar.bounds.height).abs() < 0.5;
144
145 let (mut thumb_bounds, track_bounds, container_bounds, clip_bounds) = if untransformed {
146 (bar.thumb_bounds, bar.track_bounds, bar.bounds, bar.clip_bounds)
147 } else {
148 let track_width = 10.0;
149 let inner_box = if local_padding.width > 0.0 && local_padding.height > 0.0 {
150 local_padding
151 } else {
152 local_border
153 };
154 let track_x = (inner_box.x + inner_box.width - track_width).max(inner_box.x);
155 let track_y = inner_box.y;
156 let track_h = inner_box.height;
157 let local_track = strata_layout::Rect::new(track_x, track_y, track_width, track_h);
158 let min_thumb = 24.0_f32.min(track_h);
159 let viewport_h = node.geometry.content_box.height.max(1.0);
160 let thumb_h =
161 ((viewport_h / bar.content_h.max(1.0)) * track_h).clamp(min_thumb, track_h);
162 let avail = (track_h - thumb_h).max(0.0);
163 let thumb_y = if bar.max_scroll_y > 0.0 && avail > 0.0 {
164 track_y + (bar.scroll_y / bar.max_scroll_y) * avail
165 } else {
166 track_y
167 };
168 let local_thumb = strata_layout::Rect::new(track_x, thumb_y, track_width, thumb_h);
169 (local_thumb, local_track, local_border, local_border)
170 };
171
172 if is_active {
173 let expand_px = 2.0;
174 thumb_bounds.x = (thumb_bounds.x - expand_px).max(container_bounds.x);
175 thumb_bounds.width = (thumb_bounds.width + expand_px)
176 .min((container_bounds.x + container_bounds.width) - thumb_bounds.x);
177 }
178 let min_y = track_bounds.y;
179 let max_y = track_bounds.y + track_bounds.height;
180 let clamped_top = thumb_bounds.y.clamp(min_y, max_y);
181 let clamped_bot = (thumb_bounds.y + thumb_bounds.height).clamp(clamped_top, max_y);
182 thumb_bounds.y = clamped_top;
183 thumb_bounds.height = clamped_bot - clamped_top;
184 if thumb_bounds.width <= 0.0 || thumb_bounds.height <= 0.0 {
185 continue;
186 }
187
188 overlays.push(strata_render::compositor::ScrollbarOverlay {
189 node_ptr: node as *const LayoutNode as usize,
190 clip_bounds,
191 clip_radius: bar.border_radius,
192 thumb_bounds,
193 color: strata_style::types::Color::rgba(255, 255, 255, alpha),
194 thumb_radius: strata_style::types::BorderRadius::all(3.0),
195 });
196 }
197
198 overlays
199 }
200
201 pub fn render_scrollbars(&mut self) {
203 self.render_scrollbars_filtered(None);
204 }
205
206 pub fn render_scrollbars_filtered(&mut self, top_layer: Option<bool>) {
208 let (mx, my) = self.last_mouse_pos;
209 let is_recent_scroll = self
210 .last_scroll_interaction
211 .is_some_and(|t| self.elapsed_since(t).as_millis() < 700);
212 let mut cached_hit: Option<Option<Vec<usize>>> = None;
213
214 for bar in &self.active_scrollbars {
215 if let Some(tl) = top_layer {
216 if bar.is_top_layer != tl {
217 continue;
218 }
219 }
220 if bar.max_scroll_y <= 1.0
221 || bar.clip_bounds.width <= 0.0
222 || bar.clip_bounds.height <= 0.0
223 {
224 continue;
225 }
226 let is_dragging = self
227 .scrollbar_drag
228 .as_ref()
229 .is_some_and(|d| d.path == bar.path);
230 let is_kinetic = self
231 .active_smooth_scroll
232 .as_ref()
233 .is_some_and(|s| s.container_path == bar.path);
234 let is_hovered = Self::scrollbar_geom_contains_hover(bar, mx, my)
235 && self.scrollbar_owns_point(bar, mx, my, &mut cached_hit);
236 let is_active = is_dragging || is_hovered || is_kinetic || is_recent_scroll;
237 let alpha = if is_active { 150u8 } else { 60u8 };
238
239 let mut thumb_bounds = bar.thumb_bounds;
240 if is_active {
241 let expand_px = 2.0;
242 thumb_bounds.x = (thumb_bounds.x - expand_px).max(bar.bounds.x);
243 thumb_bounds.width = (thumb_bounds.width + expand_px)
244 .min((bar.bounds.x + bar.bounds.width) - thumb_bounds.x);
245 }
246 let min_y = bar.track_bounds.y;
247 let max_y = bar.track_bounds.y + bar.track_bounds.height;
248 let clamped_top = thumb_bounds.y.clamp(min_y, max_y);
249 let clamped_bot = (thumb_bounds.y + thumb_bounds.height).clamp(clamped_top, max_y);
250 thumb_bounds.y = clamped_top;
251 thumb_bounds.height = clamped_bot - clamped_top;
252 if thumb_bounds.width <= 0.0 || thumb_bounds.height <= 0.0 {
253 continue;
254 }
255
256 self.display_list
257 .push(strata_render::compositor::DrawCommand::PushClip {
258 bounds: bar.clip_bounds,
259 radius: bar.border_radius,
260 });
261 self.display_list
262 .push(strata_render::compositor::DrawCommand::DrawRoundedRect {
263 bounds: thumb_bounds,
264 color: strata_style::types::Color::rgba(255, 255, 255, alpha),
265 border_radius: strata_style::types::BorderRadius::all(3.0),
266 border_width: strata_style::types::EdgeInsets::all(0.0),
267 border_color: None,
268 });
269 self.display_list
270 .push(strata_render::compositor::DrawCommand::PopClip);
271 }
272 }
273
274 pub fn check_scrollbar_thumb_press(&mut self, px: f32, py: f32) -> bool {
276 let mut cached_hit: Option<Option<Vec<usize>>> = None;
277 let mut matched: Option<(ActiveScrollbar, bool)> = None;
278
279 for bar in self.active_scrollbars.iter().rev() {
280 if bar.max_scroll_y <= 1.0
281 || bar.clip_bounds.width <= 0.0
282 || bar.clip_bounds.height <= 0.0
283 || !bar.clip_bounds.contains_point(px, py)
284 {
285 continue;
286 }
287
288 let in_x_thumb =
290 px >= bar.thumb_bounds.x && px <= bar.bounds.x + bar.bounds.width + 1.0;
291 let in_y_thumb =
292 py >= bar.thumb_bounds.y && py <= bar.thumb_bounds.y + bar.thumb_bounds.height;
293 let on_thumb = in_x_thumb && in_y_thumb;
294
295 let in_x_track =
296 px >= bar.track_bounds.x && px <= bar.bounds.x + bar.bounds.width + 1.0;
297 let in_y_track =
298 py >= bar.track_bounds.y && py <= bar.track_bounds.y + bar.track_bounds.height;
299 let on_track = in_x_track && in_y_track;
300
301 if !(on_thumb || on_track) {
302 continue;
303 }
304 if !self.scrollbar_owns_point(bar, px, py, &mut cached_hit) {
305 continue;
306 }
307 matched = Some((bar.clone(), on_thumb));
308 break;
309 }
310
311 let Some((bar, on_thumb)) = matched else {
312 return false;
313 };
314
315 if on_thumb {
316 self.active_smooth_scroll = None;
317 self.last_scroll_interaction = Some(self.now());
318 self.scrollbar_drag = Some(ScrollbarDrag {
319 is_id: bar.is_id,
320 id: bar.id,
321 path: bar.path,
322 start_pointer_y: py,
323 start_scroll_y: bar.scroll_y,
324 available_travel: bar.available_travel,
325 max_scroll_y: bar.max_scroll_y,
326 });
327 true
328 } else {
329 self.active_smooth_scroll = None;
332 self.last_scroll_interaction = Some(self.now());
333 let click_travel = (py - bar.track_bounds.y - bar.thumb_bounds.height / 2.0)
334 .clamp(0.0, bar.available_travel);
335 let target_scroll_y = if bar.available_travel > 0.0 {
336 (click_travel / bar.available_travel) * bar.max_scroll_y
337 } else {
338 0.0
339 };
340 let dy = target_scroll_y - bar.scroll_y;
341 let path = bar.path;
342 let is_id = bar.is_id;
343 let id = bar.id;
344 let max_scroll_y = bar.max_scroll_y;
345 let available_travel = bar.available_travel;
346 let bounds_y = bar.bounds.y;
347
348 self.apply_scroll_delta_to_container(
349 &path,
350 is_id,
351 &id,
352 target_scroll_y,
353 dy,
354 max_scroll_y,
355 available_travel,
356 bounds_y,
357 );
358
359 self.scrollbar_drag = Some(ScrollbarDrag {
360 is_id,
361 id,
362 path,
363 start_pointer_y: py,
364 start_scroll_y: target_scroll_y,
365 available_travel,
366 max_scroll_y,
367 });
368 true
369 }
370 }
371
372 pub fn poll_scrollbar_drag(&mut self, _px: f32, py: f32) -> bool {
374 let Some(drag) = self.scrollbar_drag.clone() else {
375 return false;
376 };
377 if drag.available_travel <= 0.0 {
378 return false;
379 }
380
381 let dy_pointer = py - drag.start_pointer_y;
382 let delta_scroll = (dy_pointer / drag.available_travel) * drag.max_scroll_y;
383 let new_scroll_y = (drag.start_scroll_y + delta_scroll).clamp(0.0, drag.max_scroll_y);
384
385 if let Some(bar) = self.active_scrollbars.iter().find(|b| b.path == drag.path) {
386 let cur_y = bar.scroll_y;
387 let dy = new_scroll_y - cur_y;
388 if dy.abs() > 0.01 {
389 self.last_scroll_interaction = Some(self.now());
390 self.apply_scroll_delta_to_container(
391 &drag.path,
392 drag.is_id,
393 &drag.id,
394 new_scroll_y,
395 dy,
396 drag.max_scroll_y,
397 drag.available_travel,
398 bar.bounds.y,
399 );
400 return true;
401 }
402 }
403
404 false
405 }
406}