Skip to main content

strata/runtime/
scrollbar.rs

1//! Visual scrollbar thumb rendering and drag interaction handling.
2
3use super::*;
4
5impl AppRuntime {
6    /// Renders sleek semi-transparent scrollbar thumbs for all overflowing scroll containers.
7    pub fn render_scrollbars(&mut self) {
8        let (mx, my) = self.last_mouse_pos;
9        let is_dragging = self.scrollbar_drag.is_some();
10        let is_kinetic = self.active_smooth_scroll.is_some();
11        let is_recent_scroll = self
12            .last_scroll_interaction
13            .is_some_and(|t| t.elapsed().as_millis() < 700);
14
15        for bar in &self.active_scrollbars {
16            if bar.max_scroll_y <= 1.0
17                || bar.clip_bounds.width <= 0.0
18                || bar.clip_bounds.height <= 0.0
19            {
20                continue;
21            }
22            let is_hovered = bar.clip_bounds.contains_point(mx, my)
23                && (bar.thumb_bounds.contains_point(mx, my)
24                    || bar.track_bounds.contains_point(mx, my)
25                    || (mx >= bar.track_bounds.x
26                        && mx <= bar.bounds.x + bar.bounds.width + 1.0
27                        && my >= bar.track_bounds.y
28                        && my <= bar.track_bounds.y + bar.track_bounds.height));
29            let is_active = is_dragging || is_hovered || is_kinetic || is_recent_scroll;
30            let alpha = if is_active { 150u8 } else { 60u8 };
31
32            let mut thumb_bounds = bar.thumb_bounds;
33            if is_active {
34                let expand_px = 2.0;
35                thumb_bounds.x = (thumb_bounds.x - expand_px).max(bar.bounds.x);
36                thumb_bounds.width =
37                    (thumb_bounds.width + expand_px).min((bar.bounds.x + bar.bounds.width) - thumb_bounds.x);
38            }
39            let min_y = bar.track_bounds.y;
40            let max_y = bar.track_bounds.y + bar.track_bounds.height;
41            let clamped_top = thumb_bounds.y.clamp(min_y, max_y);
42            let clamped_bot = (thumb_bounds.y + thumb_bounds.height).clamp(clamped_top, max_y);
43            thumb_bounds.y = clamped_top;
44            thumb_bounds.height = clamped_bot - clamped_top;
45            if thumb_bounds.width <= 0.0 || thumb_bounds.height <= 0.0 {
46                continue;
47            }
48
49            self.display_list
50                .push(strata_render::compositor::DrawCommand::PushClip {
51                    bounds: bar.clip_bounds,
52                    radius: bar.border_radius,
53                });
54            self.display_list
55                .push(strata_render::compositor::DrawCommand::DrawRoundedRect {
56                    bounds: thumb_bounds,
57                    color: strata_style::types::Color::rgba(255, 255, 255, alpha),
58                    border_radius: strata_style::types::BorderRadius::all(3.0),
59                    border_width: strata_style::types::EdgeInsets::all(0.0),
60                    border_color: None,
61                });
62            self.display_list
63                .push(strata_render::compositor::DrawCommand::PopClip);
64        }
65    }
66
67    /// Checks if a pointer press landed on any active scrollbar thumb or track and starts dragging.
68    pub fn check_scrollbar_thumb_press(&mut self, px: f32, py: f32) -> bool {
69        for bar in self.active_scrollbars.iter().rev() {
70            if bar.max_scroll_y <= 1.0
71                || bar.clip_bounds.width <= 0.0
72                || bar.clip_bounds.height <= 0.0
73                || !bar.clip_bounds.contains_point(px, py)
74            {
75                continue;
76            }
77
78            // Hit test check with edge-right pixel snapping tolerance (+1.0px for window edge)
79            let in_x_thumb =
80                px >= bar.thumb_bounds.x && px <= bar.bounds.x + bar.bounds.width + 1.0;
81            let in_y_thumb =
82                py >= bar.thumb_bounds.y && py <= bar.thumb_bounds.y + bar.thumb_bounds.height;
83            let on_thumb = in_x_thumb && in_y_thumb;
84
85            let in_x_track =
86                px >= bar.track_bounds.x && px <= bar.bounds.x + bar.bounds.width + 1.0;
87            let in_y_track =
88                py >= bar.track_bounds.y && py <= bar.track_bounds.y + bar.track_bounds.height;
89            let on_track = in_x_track && in_y_track;
90
91            if on_thumb {
92                self.active_smooth_scroll = None;
93                self.last_scroll_interaction = Some(std::time::Instant::now());
94                self.scrollbar_drag = Some(ScrollbarDrag {
95                    is_id: bar.is_id,
96                    id: bar.id.clone(),
97                    path: bar.path.clone(),
98                    start_pointer_y: py,
99                    start_scroll_y: bar.scroll_y,
100                    available_travel: bar.available_travel,
101                    max_scroll_y: bar.max_scroll_y,
102                });
103                return true;
104            } else if on_track {
105                // Click landed on track gutter (above or below thumb):
106                // Jump thumb center to clicked position and start dragging immediately.
107                self.active_smooth_scroll = None;
108                self.last_scroll_interaction = Some(std::time::Instant::now());
109                let click_travel = (py - bar.track_bounds.y - bar.thumb_bounds.height / 2.0)
110                    .clamp(0.0, bar.available_travel);
111                let target_scroll_y = if bar.available_travel > 0.0 {
112                    (click_travel / bar.available_travel) * bar.max_scroll_y
113                } else {
114                    0.0
115                };
116                let dy = target_scroll_y - bar.scroll_y;
117                let path = bar.path.clone();
118                let is_id = bar.is_id;
119                let id = bar.id.clone();
120                let max_scroll_y = bar.max_scroll_y;
121                let available_travel = bar.available_travel;
122                let bounds_y = bar.bounds.y;
123
124                self.apply_scroll_delta_to_container(
125                    &path,
126                    is_id,
127                    &id,
128                    target_scroll_y,
129                    dy,
130                    max_scroll_y,
131                    available_travel,
132                    bounds_y,
133                );
134
135                self.scrollbar_drag = Some(ScrollbarDrag {
136                    is_id,
137                    id,
138                    path,
139                    start_pointer_y: py,
140                    start_scroll_y: target_scroll_y,
141                    available_travel,
142                    max_scroll_y,
143                });
144                return true;
145            }
146        }
147        false
148    }
149
150    /// Updates active scrollbar thumb drag during pointer movement.
151    pub fn poll_scrollbar_drag(&mut self, _px: f32, py: f32) -> bool {
152        let Some(drag) = self.scrollbar_drag.clone() else {
153            return false;
154        };
155        if drag.available_travel <= 0.0 {
156            return false;
157        }
158
159        let dy_pointer = py - drag.start_pointer_y;
160        let delta_scroll = (dy_pointer / drag.available_travel) * drag.max_scroll_y;
161        let new_scroll_y = (drag.start_scroll_y + delta_scroll).clamp(0.0, drag.max_scroll_y);
162
163        if let Some(bar) = self.active_scrollbars.iter().find(|b| b.path == drag.path) {
164            let cur_y = bar.scroll_y;
165            let dy = new_scroll_y - cur_y;
166            if dy.abs() > 0.01 {
167                self.last_scroll_interaction = Some(std::time::Instant::now());
168                self.apply_scroll_delta_to_container(
169                    &drag.path,
170                    drag.is_id,
171                    &drag.id,
172                    new_scroll_y,
173                    dy,
174                    drag.max_scroll_y,
175                    drag.available_travel,
176                    bar.bounds.y,
177                );
178                return true;
179            }
180        }
181
182        false
183    }
184}