strata/runtime/
scrollbar.rs1use super::*;
4
5impl AppRuntime {
6 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 continue;
18 }
19 let is_hovered = bar.thumb_bounds.contains_point(mx, my)
20 || bar.track_bounds.contains_point(mx, my)
21 || (mx >= bar.track_bounds.x
22 && mx <= bar.bounds.x + bar.bounds.width + 1.0
23 && my >= bar.track_bounds.y
24 && my <= bar.track_bounds.y + bar.track_bounds.height);
25 let is_active = is_dragging || is_hovered || is_kinetic || is_recent_scroll;
26 let alpha = if is_active { 150u8 } else { 60u8 };
27
28 let mut thumb_bounds = bar.thumb_bounds;
29 if is_active {
30 let expand_px = 2.0;
31 thumb_bounds.x = (thumb_bounds.x - expand_px).max(bar.bounds.x);
32 thumb_bounds.width += expand_px;
33 }
34
35 self.display_list
36 .push(strata_render::compositor::DrawCommand::DrawRoundedRect {
37 bounds: thumb_bounds,
38 color: strata_style::types::Color::rgba(255, 255, 255, alpha),
39 border_radius: strata_style::types::BorderRadius::all(3.0),
40 border_width: strata_style::types::EdgeInsets::all(0.0),
41 border_color: None,
42 });
43 }
44 }
45
46 pub fn check_scrollbar_thumb_press(&mut self, px: f32, py: f32) -> bool {
48 for bar in &self.active_scrollbars {
49 if bar.max_scroll_y <= 1.0 {
50 continue;
51 }
52
53 let in_x_thumb =
55 px >= bar.thumb_bounds.x && px <= bar.bounds.x + bar.bounds.width + 1.0;
56 let in_y_thumb =
57 py >= bar.thumb_bounds.y && py <= bar.thumb_bounds.y + bar.thumb_bounds.height;
58 let on_thumb = in_x_thumb && in_y_thumb;
59
60 let in_x_track =
61 px >= bar.track_bounds.x && px <= bar.bounds.x + bar.bounds.width + 1.0;
62 let in_y_track =
63 py >= bar.track_bounds.y && py <= bar.track_bounds.y + bar.track_bounds.height;
64 let on_track = in_x_track && in_y_track;
65
66 if on_thumb {
67 self.active_smooth_scroll = None;
68 self.last_scroll_interaction = Some(std::time::Instant::now());
69 self.scrollbar_drag = Some(ScrollbarDrag {
70 is_id: bar.is_id,
71 id: bar.id.clone(),
72 path: bar.path.clone(),
73 start_pointer_y: py,
74 start_scroll_y: bar.scroll_y,
75 available_travel: bar.available_travel,
76 max_scroll_y: bar.max_scroll_y,
77 });
78 return true;
79 } else if on_track {
80 self.active_smooth_scroll = None;
83 self.last_scroll_interaction = Some(std::time::Instant::now());
84 let click_travel = (py - bar.track_bounds.y - bar.thumb_bounds.height / 2.0)
85 .clamp(0.0, bar.available_travel);
86 let target_scroll_y = if bar.available_travel > 0.0 {
87 (click_travel / bar.available_travel) * bar.max_scroll_y
88 } else {
89 0.0
90 };
91 let dy = target_scroll_y - bar.scroll_y;
92 let path = bar.path.clone();
93 let is_id = bar.is_id;
94 let id = bar.id.clone();
95 let max_scroll_y = bar.max_scroll_y;
96 let available_travel = bar.available_travel;
97 let bounds_y = bar.bounds.y;
98
99 self.apply_scroll_delta_to_container(
100 &path,
101 is_id,
102 &id,
103 target_scroll_y,
104 dy,
105 max_scroll_y,
106 available_travel,
107 bounds_y,
108 );
109
110 self.scrollbar_drag = Some(ScrollbarDrag {
111 is_id,
112 id,
113 path,
114 start_pointer_y: py,
115 start_scroll_y: target_scroll_y,
116 available_travel,
117 max_scroll_y,
118 });
119 return true;
120 }
121 }
122 false
123 }
124
125 pub fn poll_scrollbar_drag(&mut self, _px: f32, py: f32) -> bool {
127 let Some(drag) = self.scrollbar_drag.clone() else {
128 return false;
129 };
130 if drag.available_travel <= 0.0 {
131 return false;
132 }
133
134 let dy_pointer = py - drag.start_pointer_y;
135 let delta_scroll = (dy_pointer / drag.available_travel) * drag.max_scroll_y;
136 let new_scroll_y = (drag.start_scroll_y + delta_scroll).clamp(0.0, drag.max_scroll_y);
137
138 if let Some(bar) = self.active_scrollbars.iter().find(|b| b.path == drag.path) {
139 let cur_y = bar.scroll_y;
140 let dy = new_scroll_y - cur_y;
141 if dy.abs() > 0.01 {
142 self.last_scroll_interaction = Some(std::time::Instant::now());
143 self.apply_scroll_delta_to_container(
144 &drag.path,
145 drag.is_id,
146 &drag.id,
147 new_scroll_y,
148 dy,
149 drag.max_scroll_y,
150 drag.available_travel,
151 bar.bounds.y,
152 );
153 return true;
154 }
155 }
156
157 false
158 }
159}