BlueGit

ict rusty-teams

Files

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

touch_scroll.rs

touch_scroll.rs 6.9 KiB Download Raw
1use std::cell::Cell;
2use std::rc::Rc;
3use std::time::Duration;
4
5use crate::long_press::long_press_right_click;
6use gpui_kit::{
7 Context, InteractiveElement as _, IntoElement, ListAlignment, ListState, MouseButton,
8 ParentElement as _, Render, ScrollHandle, StatefulInteractiveElement as _, Styled as _,
9 TestAppContext, TouchEvent, TouchId, TouchPhase, VisualTestContext, Window, div, list, point,
10 px,
11};
12
13const ROW_HEIGHT: f32 = 40.;
14const ROW_COUNT: usize = 60;
15
16#[derive(Clone, Default)]
17struct Clicks {
18 left: Rc<Cell<usize>>,
19 right: Rc<Cell<usize>>,
20}
21
22struct ScrollContainer {
23 scroll_handle: ScrollHandle,
24 clicks: Clicks,
25}
26
27impl Render for ScrollContainer {
28 fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
29 let clicks = self.clicks.clone();
30 div().size_full().child(long_press_right_click()).child(
31 div()
32 .id("scroll-container")
33 .size_full()
34 .overflow_y_scroll()
35 .track_scroll(&self.scroll_handle)
36 .children((0..ROW_COUNT).map(|row_index| {
37 let left = clicks.left.clone();
38 let right = clicks.right.clone();
39 div()
40 .id(row_index)
41 .h(px(ROW_HEIGHT))
42 .flex_none()
43 .w_full()
44 .on_click(move |_, _, _| left.set(left.get() + 1))
45 .on_mouse_down(MouseButton::Right, move |_, _, _| {
46 right.set(right.get() + 1)
47 })
48 })),
49 )
50 }
51}
52
53struct VirtualList {
54 list_state: ListState,
55}
56
57impl Render for VirtualList {
58 fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
59 list(self.list_state.clone(), |row_index, _, _| {
60 div()
61 .id(row_index)
62 .h(px(ROW_HEIGHT))
63 .flex_none()
64 .w_full()
65 .into_any_element()
66 })
67 .size_full()
68 }
69}
70
71struct Finger<'a> {
72 window_cx: &'a mut VisualTestContext,
73 touch_id: TouchId,
74}
75
76impl Finger<'_> {
77 fn touch(&mut self, phase: TouchPhase, x: f32, y: f32) {
78 self.window_cx.simulate_event(TouchEvent {
79 id: self.touch_id,
80 phase,
81 position: point(px(x), px(y)),
82 predicted_position: None,
83 force: None,
84 });
85 self.window_cx
86 .executor()
87 .advance_clock(Duration::from_millis(8));
88 }
89
90 fn pan(&mut self, from_y: f32, to_y: f32, steps: usize) {
91 self.touch(TouchPhase::Started, 100., from_y);
92 for step in 1..=steps {
93 let progress = step as f32 / steps as f32;
94 self.touch(TouchPhase::Moved, 100., from_y + (to_y - from_y) * progress);
95 }
96 self.touch(TouchPhase::Ended, 100., to_y);
97 }
98
99 fn tap(&mut self, x: f32, y: f32) {
100 self.touch(TouchPhase::Started, x, y);
101 self.touch(TouchPhase::Ended, x, y);
102 self.window_cx
103 .executor()
104 .advance_clock(Duration::from_millis(500));
105 }
106}
107
108fn open_scroll_container(
109 cx: &mut TestAppContext,
110) -> (ScrollHandle, Clicks, &mut VisualTestContext) {
111 cx.update(gpui_kit::init);
112 let scroll_handle = ScrollHandle::new();
113 let clicks = Clicks::default();
114 let (_, window_cx) = cx.add_window_view({
115 let scroll_handle = scroll_handle.clone();
116 let clicks = clicks.clone();
117 move |_, _| ScrollContainer {
118 scroll_handle,
119 clicks,
120 }
121 });
122 window_cx.update(|window, cx| window.draw(cx).clear(cx));
123 (scroll_handle, clicks, window_cx)
124}
125
126fn run_frames(window_cx: &mut VisualTestContext, frame_count: usize) {
127 for _ in 0..frame_count {
128 window_cx
129 .executor()
130 .advance_clock(Duration::from_millis(16));
131 window_cx.update(|window, cx| {
132 window.simulate_next_frame(cx);
133 window.draw(cx).clear(cx);
134 });
135 }
136}
137
138#[gpui_kit::test]
139fn finger_pan_scrolls_the_container_under_the_finger_by_pixels(cx: &mut TestAppContext) {
140 let (scroll_handle, clicks, window_cx) = open_scroll_container(cx);
141 let mut finger = Finger {
142 window_cx,
143 touch_id: TouchId(1),
144 };
145 finger.pan(300., 180., 30);
146 let offset_y = f32::from(scroll_handle.offset().y);
147 assert!(
148 (-120.5..=-100.).contains(&offset_y),
149 "a 120 px slow pan scrolls about 120 px, got {offset_y}"
150 );
151 assert_eq!(clicks.left.get(), 0, "a pan never clicks");
152}
153
154#[gpui_kit::test]
155fn fling_keeps_scrolling_after_release(cx: &mut TestAppContext) {
156 let (scroll_handle, _, window_cx) = open_scroll_container(cx);
157 let mut finger = Finger {
158 window_cx,
159 touch_id: TouchId(1),
160 };
161 finger.pan(300., 100., 5);
162 let released_at = f32::from(scroll_handle.offset().y);
163 run_frames(finger.window_cx, 30);
164 let coasted_to = f32::from(scroll_handle.offset().y);
165 assert!(
166 coasted_to < released_at - 50.,
167 "momentum continues the scroll after release: {released_at} -> {coasted_to}"
168 );
169}
170
171#[gpui_kit::test]
172fn tap_clicks_the_row_under_the_finger(cx: &mut TestAppContext) {
173 let (_, clicks, window_cx) = open_scroll_container(cx);
174 let mut finger = Finger {
175 window_cx,
176 touch_id: TouchId(1),
177 };
178 finger.tap(100., 60.);
179 assert_eq!(clicks.left.get(), 1);
180}
181
182#[gpui_kit::test]
183fn finger_pan_scrolls_a_virtual_list(cx: &mut TestAppContext) {
184 cx.update(gpui_kit::init);
185 let list_state = ListState::new(ROW_COUNT, ListAlignment::Top, px(200.));
186 let (_, window_cx) = cx.add_window_view({
187 let list_state = list_state.clone();
188 move |_, _| VirtualList { list_state }
189 });
190 window_cx.update(|window, cx| window.draw(cx).clear(cx));
191 let mut finger = Finger {
192 window_cx,
193 touch_id: TouchId(1),
194 };
195 finger.pan(300., 100., 30);
196 let scroll_top = list_state.logical_scroll_top();
197 let scrolled = scroll_top.item_ix as f32 * ROW_HEIGHT + f32::from(scroll_top.offset_in_item);
198 assert!(
199 (180.0..=200.5).contains(&scrolled),
200 "a 200 px slow pan scrolls the list about 200 px, got {scrolled}"
201 );
202}
203
204#[gpui_kit::test]
205fn long_press_without_a_handler_right_clicks_instead_of_click_or_scroll(cx: &mut TestAppContext) {
206 let (scroll_handle, clicks, window_cx) = open_scroll_container(cx);
207 let mut finger = Finger {
208 window_cx,
209 touch_id: TouchId(1),
210 };
211 finger.touch(TouchPhase::Started, 100., 60.);
212 finger
213 .window_cx
214 .executor()
215 .advance_clock(Duration::from_millis(600));
216 finger.window_cx.run_until_parked();
217 finger.touch(TouchPhase::Moved, 100., 20.);
218 finger.touch(TouchPhase::Ended, 100., 20.);
219 finger.window_cx.run_until_parked();
220 assert_eq!(clicks.right.get(), 1);
221 assert_eq!(clicks.left.get(), 0);
222 assert_eq!(f32::from(scroll_handle.offset().y), 0.);
223}