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