BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-ui-kit / src / scroll_edge.rs

scroll_edge.rs

scroll_edge.rs 10.1 KiB Download Raw
1use gpui_kit::prelude::FluentBuilder as _;
2use gpui_kit::*;
3
4use app_base::theme;
5
6pub const EDGE_THICKNESS: f32 = 1.;
7const SCROLLED_THRESHOLD: f32 = 0.5;
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub enum EdgeRule {
11 Always,
12 WhenScrolled,
13}
14
15pub fn content_hidden_above(offset_y: Pixels) -> bool {
16 f32::from(offset_y) < -SCROLLED_THRESHOLD
17}
18
19pub fn edge_visible(rule: EdgeRule, offset_y: Pixels) -> bool {
20 match rule {
21 EdgeRule::Always => true,
22 EdgeRule::WhenScrolled => content_hidden_above(offset_y),
23 }
24}
25
26pub fn edge_divider(tag: &'static str) -> Div {
27 div()
28 .debug_selector(move || tag.into())
29 .w_full()
30 .h(px(EDGE_THICKNESS))
31 .flex_none()
32 .bg(theme::border())
33}
34
35pub fn settled_offset(offset: Point<Pixels>, max_offset: Point<Pixels>) -> Point<Pixels> {
36 let settle = |value: Pixels, max: Pixels| value.max(-max.max(px(0.))).min(px(0.));
37 point(
38 settle(offset.x, max_offset.x),
39 settle(offset.y, max_offset.y),
40 )
41}
42
43pub fn scroll_offset(handle: &ScrollHandle) -> Point<Pixels> {
44 settled_offset(handle.offset(), handle.max_offset())
45}
46
47pub fn pushes_past_edge(offset_y: Pixels, max_offset_y: Pixels, delta: Point<Pixels>) -> bool {
48 let vertical = if delta.y.is_zero() { delta.x } else { delta.y };
49 if vertical > px(0.) {
50 !content_hidden_above(offset_y)
51 } else if vertical < px(0.) {
52 f32::from(offset_y + max_offset_y) <= SCROLLED_THRESHOLD
53 } else {
54 false
55 }
56}
57
58pub fn edge_scroll_guard(handle: &ScrollHandle) -> impl IntoElement {
59 let handle = handle.clone();
60 canvas(
61 |bounds, window, _| window.insert_hitbox(bounds, HitboxBehavior::Normal),
62 move |_, hitbox, window, _| {
63 window.on_mouse_event(move |event: &ScrollWheelEvent, phase, window, cx| {
64 if phase == DispatchPhase::Capture
65 && hitbox.should_handle_scroll(window)
66 && pushes_past_edge(
67 handle.offset().y,
68 handle.max_offset().y,
69 event.delta.pixel_delta(window.line_height()),
70 )
71 {
72 cx.stop_propagation();
73 }
74 });
75 },
76 )
77 .absolute()
78 .top_0()
79 .left_0()
80 .size_full()
81}
82
83pub fn with_top_edge(
84 scroll_area: impl IntoElement,
85 handle: &ScrollHandle,
86 tag: &'static str,
87) -> Div {
88 let visible = edge_visible(EdgeRule::WhenScrolled, scroll_offset(handle).y);
89 div()
90 .relative()
91 .flex()
92 .flex_col()
93 .flex_1()
94 .min_h_0()
95 .w_full()
96 .child(edge_scroll_guard(handle))
97 .child(scroll_area)
98 .when(visible, |area| {
99 area.child(edge_divider(tag).absolute().top_0().left_0().right_0())
100 })
101}
102
103#[cfg(test)]
104mod tests {
105 use std::cell::{Cell, RefCell};
106 use std::rc::Rc;
107
108 use gpui_kit::{
109 Context, Entity, InteractiveElement as _, IntoElement, ParentElement as _, Render,
110 ScrollDelta, ScrollHandle, ScrollWheelEvent, StatefulInteractiveElement as _, Styled as _,
111 TestAppContext, TouchPhase, VisualTestContext, Window, div, point, px,
112 };
113
114 use super::{
115 EdgeRule, content_hidden_above, edge_visible, pushes_past_edge, scroll_offset,
116 settled_offset, with_top_edge,
117 };
118
119 const EDGE_TAG: &str = "test-edge-line";
120 const ROW_HEIGHT: f32 = 40.;
121 const ROW_COUNT: usize = 60;
122
123 struct EdgeList {
124 handle: ScrollHandle,
125 rendered_offsets: Rc<RefCell<Vec<f32>>>,
126 }
127
128 impl Render for EdgeList {
129 fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
130 self.rendered_offsets
131 .borrow_mut()
132 .push(f32::from(scroll_offset(&self.handle).y));
133 let rows =
134 (0..ROW_COUNT).map(|row_index| div().id(row_index).h(px(ROW_HEIGHT)).flex_none());
135 let scroll_area = div()
136 .id("edge-list")
137 .flex_1()
138 .min_h_0()
139 .overflow_y_scroll()
140 .track_scroll(&self.handle)
141 .children(rows);
142 div().size_full().flex().flex_col().child(with_top_edge(
143 scroll_area,
144 &self.handle,
145 EDGE_TAG,
146 ))
147 }
148 }
149
150 struct Harness<'a> {
151 window_cx: &'a mut VisualTestContext,
152 handle: ScrollHandle,
153 notifications: Rc<Cell<usize>>,
154 rendered_offsets: Rc<RefCell<Vec<f32>>>,
155 }
156
157 impl Harness<'_> {
158 fn draw(&mut self) {
159 self.window_cx
160 .update(|window, cx| window.draw(cx).clear(cx));
161 }
162
163 fn wheel(&mut self, delta_y: f32, touch_phase: TouchPhase) {
164 self.window_cx.simulate_event(ScrollWheelEvent {
165 position: point(px(100.), px(200.)),
166 delta: ScrollDelta::Pixels(point(px(0.), px(delta_y))),
167 touch_phase,
168 ..Default::default()
169 });
170 self.draw();
171 }
172
173 fn touchpad_swipe(&mut self, delta_y: f32, steps: usize) {
174 self.wheel(delta_y, TouchPhase::Started);
175 for _ in 0..steps {
176 self.wheel(delta_y, TouchPhase::Moved);
177 }
178 self.wheel(0., TouchPhase::Ended);
179 }
180
181 fn offset_y(&self) -> f32 {
182 f32::from(self.handle.offset().y)
183 }
184
185 fn shows_line(&mut self) -> bool {
186 self.window_cx.debug_bounds(EDGE_TAG).is_some()
187 }
188 }
189
190 fn open(cx: &mut TestAppContext) -> Harness<'_> {
191 cx.update(gpui_kit::init);
192 let handle = ScrollHandle::new();
193 let rendered_offsets = Rc::new(RefCell::new(Vec::new()));
194 let (view, window_cx): (Entity<EdgeList>, _) = cx.add_window_view({
195 let handle = handle.clone();
196 let rendered_offsets = rendered_offsets.clone();
197 move |_, _| EdgeList {
198 handle,
199 rendered_offsets,
200 }
201 });
202 let notifications = Rc::new(Cell::new(0));
203 window_cx.update({
204 let notifications = notifications.clone();
205 move |_, cx| {
206 cx.observe(&view, move |_, _| {
207 notifications.set(notifications.get() + 1)
208 })
209 .detach()
210 }
211 });
212 let mut harness = Harness {
213 window_cx,
214 handle,
215 notifications,
216 rendered_offsets,
217 };
218 harness.draw();
219 harness
220 }
221
222 #[gpui_kit::test]
223 fn touchpad_overscroll_at_the_top_changes_nothing(cx: &mut TestAppContext) {
224 let mut harness = open(cx);
225 harness.notifications.set(0);
226 harness.touchpad_swipe(35., 12);
227 assert_eq!(harness.offset_y(), 0.);
228 assert_eq!(harness.notifications.get(), 0, "no re-render at the edge");
229 assert!(!harness.shows_line());
230 }
231
232 #[gpui_kit::test]
233 fn touchpad_overscroll_at_the_bottom_changes_nothing(cx: &mut TestAppContext) {
234 let mut harness = open(cx);
235 harness.wheel(-100_000., TouchPhase::Moved);
236 let bottom = harness.offset_y();
237 assert!(bottom < -1000.);
238 harness.notifications.set(0);
239 harness.touchpad_swipe(-35., 12);
240 assert_eq!(harness.offset_y(), bottom);
241 assert_eq!(harness.notifications.get(), 0);
242 assert!(harness.shows_line());
243 }
244
245 #[gpui_kit::test]
246 fn overshoot_past_the_top_is_never_rendered(cx: &mut TestAppContext) {
247 let mut harness = open(cx);
248 harness.wheel(-10., TouchPhase::Moved);
249 assert_eq!(harness.offset_y(), -10.);
250 harness.rendered_offsets.borrow_mut().clear();
251 harness.touchpad_swipe(400., 2);
252 assert_eq!(harness.offset_y(), 0.);
253 let rendered = harness.rendered_offsets.borrow().clone();
254 assert!(
255 rendered.iter().all(|offset| *offset <= 0.),
256 "render saw an offset past the top: {rendered:?}"
257 );
258 assert!(!harness.shows_line());
259 }
260
261 #[gpui_kit::test]
262 fn scrolling_down_from_the_top_still_scrolls(cx: &mut TestAppContext) {
263 let mut harness = open(cx);
264 harness.touchpad_swipe(-30., 4);
265 assert_eq!(harness.offset_y(), -150.);
266 assert!(harness.shows_line());
267 harness.touchpad_swipe(30., 4);
268 assert_eq!(harness.offset_y(), 0.);
269 assert!(!harness.shows_line());
270 }
271
272 #[test]
273 fn settled_offset_stays_inside_the_scroll_range() {
274 let max = point(px(0.), px(500.));
275 assert_eq!(settled_offset(point(px(0.), px(120.)), max).y, px(0.));
276 assert_eq!(settled_offset(point(px(0.), px(-900.)), max).y, px(-500.));
277 assert_eq!(settled_offset(point(px(0.), px(-200.)), max).y, px(-200.));
278 assert_eq!(
279 settled_offset(point(px(0.), px(-40.)), point(px(0.), px(0.))).y,
280 px(0.)
281 );
282 }
283
284 #[test]
285 fn only_deltas_beyond_an_edge_are_held_back() {
286 let max = px(500.);
287 assert!(pushes_past_edge(px(0.), max, point(px(0.), px(30.))));
288 assert!(pushes_past_edge(px(0.), max, point(px(12.), px(0.))));
289 assert!(!pushes_past_edge(px(0.), max, point(px(0.), px(-30.))));
290 assert!(!pushes_past_edge(px(-10.), max, point(px(0.), px(30.))));
291 assert!(pushes_past_edge(px(-500.), max, point(px(0.), px(-30.))));
292 assert!(!pushes_past_edge(px(-500.), max, point(px(0.), px(30.))));
293 assert!(!pushes_past_edge(px(0.), max, point(px(0.), px(0.))));
294 }
295
296 #[test]
297 fn the_list_at_its_top_shows_no_line() {
298 assert!(!content_hidden_above(px(0.)));
299 assert!(!edge_visible(EdgeRule::WhenScrolled, px(0.)));
300 }
301
302 #[test]
303 fn subpixel_jitter_at_the_top_shows_no_line() {
304 assert!(!edge_visible(EdgeRule::WhenScrolled, px(-0.3)));
305 }
306
307 #[test]
308 fn a_scrolled_list_shows_the_line() {
309 assert!(edge_visible(EdgeRule::WhenScrolled, px(-1.)));
310 assert!(edge_visible(EdgeRule::WhenScrolled, px(-480.)));
311 }
312
313 #[test]
314 fn a_fixed_header_always_shows_the_line() {
315 assert!(edge_visible(EdgeRule::Always, px(0.)));
316 assert!(edge_visible(EdgeRule::Always, px(-20.)));
317 }
318}