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