| 1 | //! The pointer and wheel event surface used by [`super::CarouselContent`]. |
| 2 | //! |
| 3 | //! The mask is deliberately a sibling of the scrolled content. A mask nested |
| 4 | //! inside the content would receive the content's scroll offset and would stop |
| 5 | //! covering the viewport after the first scroll. |
| 6 | |
| 7 | use std::cell::RefCell; |
| 8 | use std::panic::Location; |
| 9 | use std::rc::Rc; |
| 10 | |
| 11 | use gpui::{ |
| 12 | App, Axis, Bounds, ContentMask, Element, ElementId, GlobalElementId, Hitbox, IntoElement, |
| 13 | IsZero as _, LayoutId, MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, |
| 14 | OngoingScroll, Position, ScrollWheelEvent, Style, TouchPhase, Window, relative, |
| 15 | }; |
| 16 | |
| 17 | use super::state::CarouselState; |
| 18 | use crate::{AxisExt as _, OngoingScrollExt as _, global_state::GlobalState}; |
| 19 | |
| 20 | /// A viewport-sized, invisible event surface for a Carousel. |
| 21 | /// |
| 22 | /// This surface owns pointer, wheel, and trackpad input so that a Carousel |
| 23 | /// nested in a list can lock its own axis while allowing cross-axis gestures |
| 24 | /// to continue to an ancestor. |
| 25 | pub(super) struct CarouselScrollMask { |
| 26 | axis: Axis, |
| 27 | id: ElementId, |
| 28 | state: gpui::Entity<CarouselState>, |
| 29 | } |
| 30 | |
| 31 | impl CarouselScrollMask { |
| 32 | #[track_caller] |
| 33 | pub(super) fn new(axis: Axis, state: &gpui::Entity<CarouselState>) -> Self { |
| 34 | Self { |
| 35 | axis, |
| 36 | id: caller_id(), |
| 37 | state: state.clone(), |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | pub(super) fn id(mut self, id: impl Into<ElementId>) -> Self { |
| 42 | self.id = id.into(); |
| 43 | self |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | impl IntoElement for CarouselScrollMask { |
| 48 | type Element = Self; |
| 49 | |
| 50 | fn into_element(self) -> Self::Element { |
| 51 | self |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | impl Element for CarouselScrollMask { |
| 56 | type RequestLayoutState = (); |
| 57 | type PrepaintState = Hitbox; |
| 58 | |
| 59 | fn id(&self) -> Option<ElementId> { |
| 60 | let axis = if self.axis.is_horizontal() { |
| 61 | "horizontal" |
| 62 | } else { |
| 63 | "vertical" |
| 64 | }; |
| 65 | Some((self.id.clone(), axis).into()) |
| 66 | } |
| 67 | |
| 68 | fn source_location(&self) -> Option<&'static std::panic::Location<'static>> { |
| 69 | None |
| 70 | } |
| 71 | |
| 72 | fn request_layout( |
| 73 | &mut self, |
| 74 | _: Option<&GlobalElementId>, |
| 75 | _: Option<&gpui::InspectorElementId>, |
| 76 | window: &mut Window, |
| 77 | cx: &mut App, |
| 78 | ) -> (LayoutId, Self::RequestLayoutState) { |
| 79 | let mut style = Style::default(); |
| 80 | style.position = Position::Absolute; |
| 81 | style.inset.top = gpui::px(0.).into(); |
| 82 | style.inset.left = gpui::px(0.).into(); |
| 83 | style.size.width = relative(1.).into(); |
| 84 | style.size.height = relative(1.).into(); |
| 85 | (window.request_layout(style, None, cx), ()) |
| 86 | } |
| 87 | |
| 88 | fn prepaint( |
| 89 | &mut self, |
| 90 | _: Option<&GlobalElementId>, |
| 91 | _: Option<&gpui::InspectorElementId>, |
| 92 | bounds: Bounds<gpui::Pixels>, |
| 93 | _: &mut Self::RequestLayoutState, |
| 94 | window: &mut Window, |
| 95 | _: &mut App, |
| 96 | ) -> Self::PrepaintState { |
| 97 | window.insert_hitbox(bounds, gpui::HitboxBehavior::Normal) |
| 98 | } |
| 99 | |
| 100 | fn paint( |
| 101 | &mut self, |
| 102 | global_id: Option<&GlobalElementId>, |
| 103 | _: Option<&gpui::InspectorElementId>, |
| 104 | _: Bounds<gpui::Pixels>, |
| 105 | _: &mut Self::RequestLayoutState, |
| 106 | hitbox: &mut Self::PrepaintState, |
| 107 | window: &mut Window, |
| 108 | _: &mut App, |
| 109 | ) { |
| 110 | let axis = self.axis; |
| 111 | let hitbox_id = hitbox.id; |
| 112 | let bounds = hitbox.bounds; |
| 113 | let state = self.state.clone(); |
| 114 | let ongoing_scroll = global_id |
| 115 | .map(|global_id| { |
| 116 | window.with_element_state::<Rc<RefCell<OngoingScroll>>, _>(global_id, |value, _| { |
| 117 | let value = value.unwrap_or_default(); |
| 118 | (value.clone(), value) |
| 119 | }) |
| 120 | }) |
| 121 | .unwrap_or_default(); |
| 122 | |
| 123 | window.with_content_mask(Some(ContentMask { bounds }), |window| { |
| 124 | let pointer_state = state.clone(); |
| 125 | window.on_mouse_event(move |event: &MouseDownEvent, phase, window, cx| { |
| 126 | if phase.capture() |
| 127 | && event.button == MouseButton::Left |
| 128 | && hitbox_id.should_handle_scroll(window) |
| 129 | { |
| 130 | let started = |
| 131 | pointer_state.update(cx, |state, cx| state.begin_drag(event.position, cx)); |
| 132 | if started { |
| 133 | GlobalState::suppress_text_selection(cx); |
| 134 | } |
| 135 | } |
| 136 | }); |
| 137 | |
| 138 | let pointer_state = state.clone(); |
| 139 | window.on_mouse_event(move |event: &MouseMoveEvent, phase, _, cx| { |
| 140 | if !phase.capture() || event.pressed_button != Some(MouseButton::Left) { |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | let handled = |
| 145 | pointer_state.update(cx, |state, cx| state.update_drag(event.position, cx)); |
| 146 | if handled { |
| 147 | cx.stop_propagation(); |
| 148 | } |
| 149 | }); |
| 150 | |
| 151 | let pointer_state = state.clone(); |
| 152 | let mut suppress_release = false; |
| 153 | window.on_mouse_event(move |event: &MouseUpEvent, phase, window, cx| { |
| 154 | if event.button != MouseButton::Left { |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | if phase.capture() { |
| 159 | let snapshot = pointer_state.read(cx); |
| 160 | let handled = snapshot.is_pointer_drag_locked(); |
| 161 | suppress_release = handled || snapshot.should_suppress_pointer_click(); |
| 162 | pointer_state.update(cx, |state, cx| { |
| 163 | state.finish_drag(cx); |
| 164 | }); |
| 165 | if suppress_release { |
| 166 | // The release may be stopped before TextSelectionLayer's |
| 167 | // bubble listener. Clear its observable selection state |
| 168 | // here so a Carousel drag cannot leave a stale drag or |
| 169 | // participant-local selection behind. |
| 170 | gpui_base::TextSelection::clear(window, cx); |
| 171 | } |
| 172 | } else if suppress_release { |
| 173 | // All capture handlers have now cleared their pressed |
| 174 | // state. Stop before descendant click handlers run. |
| 175 | suppress_release = false; |
| 176 | cx.stop_propagation(); |
| 177 | } |
| 178 | }); |
| 179 | |
| 180 | window.on_mouse_event(move |event: &ScrollWheelEvent, phase, window, cx| { |
| 181 | if !phase.capture() || !hitbox_id.should_handle_scroll(window) { |
| 182 | return; |
| 183 | } |
| 184 | |
| 185 | let mut delta = event.delta.pixel_delta(window.line_height()); |
| 186 | if event.delta.precise() { |
| 187 | ongoing_scroll |
| 188 | .borrow_mut() |
| 189 | .lock_axis(&mut delta, event.touch_phase); |
| 190 | } |
| 191 | |
| 192 | if !delta.x.is_zero() && !delta.y.is_zero() { |
| 193 | if delta.x.abs() > delta.y.abs() { |
| 194 | delta.y = gpui::Pixels::ZERO; |
| 195 | } else { |
| 196 | delta.x = gpui::Pixels::ZERO; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | // Ignore the secondary axis. The lock above keeps this stable |
| 201 | // throughout a precise trackpad gesture. |
| 202 | if axis.is_horizontal() { |
| 203 | delta.y = gpui::Pixels::ZERO; |
| 204 | } else { |
| 205 | delta.x = gpui::Pixels::ZERO; |
| 206 | } |
| 207 | |
| 208 | let precise = event.delta.precise(); |
| 209 | let primary_delta = if axis.is_horizontal() { |
| 210 | delta.x |
| 211 | } else { |
| 212 | delta.y |
| 213 | }; |
| 214 | let consumed = if primary_delta.is_zero() { |
| 215 | false |
| 216 | } else if !precise { |
| 217 | state.update(cx, |state, cx| { |
| 218 | state.handle_wheel_step(axis, primary_delta, cx) |
| 219 | }) |
| 220 | } else if matches!(event.touch_phase, TouchPhase::Ended | TouchPhase::Cancelled) { |
| 221 | false |
| 222 | } else { |
| 223 | // A gesture this carousel already owns stays here even at |
| 224 | // an edge, so its leftover never scrolls an ancestor. One |
| 225 | // that begins at an edge belongs to the ancestor until it |
| 226 | // ends. |
| 227 | let owned = state.read(cx).has_scroll_gesture(); |
| 228 | let moved = state.update(cx, |state, cx| { |
| 229 | state.handle_scroll_delta(axis, primary_delta, event.touch_phase, cx) |
| 230 | }); |
| 231 | if !moved && !owned { |
| 232 | state.update(cx, |state, cx| state.defer_scroll_to_ancestor(cx)); |
| 233 | } |
| 234 | moved || owned |
| 235 | }; |
| 236 | |
| 237 | if precise { |
| 238 | match event.touch_phase { |
| 239 | TouchPhase::Ended => { |
| 240 | state.update(cx, |state, cx| state.finish_scroll(false, cx)); |
| 241 | } |
| 242 | TouchPhase::Cancelled => { |
| 243 | state.update(cx, |state, cx| state.finish_scroll(true, cx)); |
| 244 | } |
| 245 | TouchPhase::Started | TouchPhase::Moved => {} |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | // Horizontal carousels retain every gesture at their edge. A |
| 250 | // vertical carousel hands a gesture that begins at an edge to |
| 251 | // an ancestor so the surrounding document keeps scrolling. |
| 252 | if consumed || (axis.is_horizontal() && !primary_delta.is_zero()) { |
| 253 | cx.stop_propagation(); |
| 254 | } |
| 255 | }); |
| 256 | }); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | #[track_caller] |
| 261 | fn caller_id() -> ElementId { |
| 262 | ElementId::CodeLocation(*Location::caller()) |
| 263 | } |
| 264 | |
| 265 | #[cfg(test)] |
| 266 | mod tests { |
| 267 | use std::{cell::Cell, rc::Rc}; |
| 268 | |
| 269 | use gpui::{ |
| 270 | AppContext as _, Axis, Context, Entity, InteractiveElement as _, IntoElement, Modifiers, |
| 271 | MouseButton, ParentElement as _, Render, ScrollDelta, ScrollHandle, ScrollWheelEvent, |
| 272 | StatefulInteractiveElement as _, Styled as _, TestAppContext, TouchPhase, |
| 273 | VisualTestContext, Window, div, point, px, |
| 274 | }; |
| 275 | |
| 276 | use crate::{ |
| 277 | button::Button, |
| 278 | carousel::{ |
| 279 | Carousel, CarouselContent, CarouselItem, CarouselState, state::SCROLL_EVENT_SEPARATION, |
| 280 | }, |
| 281 | }; |
| 282 | |
| 283 | struct ButtonDragHarness { |
| 284 | state: Entity<CarouselState>, |
| 285 | clicks: Rc<Cell<usize>>, |
| 286 | } |
| 287 | |
| 288 | impl Render for ButtonDragHarness { |
| 289 | fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement { |
| 290 | let clicks = self.clicks.clone(); |
| 291 | Carousel::new("button-drag-carousel", &self.state) |
| 292 | .w(px(100.)) |
| 293 | .h(px(40.)) |
| 294 | .child( |
| 295 | CarouselContent::new(&self.state) |
| 296 | .h(px(40.)) |
| 297 | .child( |
| 298 | CarouselItem::new("button-slide", 0, &self.state) |
| 299 | .h(px(40.)) |
| 300 | .child( |
| 301 | Button::new("slide-button") |
| 302 | .w_full() |
| 303 | .h(px(40.)) |
| 304 | .on_click(move |_, _, _| clicks.set(clicks.get() + 1)), |
| 305 | ), |
| 306 | ) |
| 307 | .child( |
| 308 | CarouselItem::new("plain-slide", 1, &self.state) |
| 309 | .h(px(40.)) |
| 310 | .child("Second"), |
| 311 | ), |
| 312 | ) |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | #[gpui::test] |
| 317 | fn dragging_over_child_button_suppresses_click_without_leaving_it_pending( |
| 318 | cx: &mut TestAppContext, |
| 319 | ) { |
| 320 | cx.update(crate::init); |
| 321 | let state = cx.update(|cx| cx.new(|_| CarouselState::new(2))); |
| 322 | let clicks = Rc::new(Cell::new(0)); |
| 323 | let (_, cx) = cx.add_window_view({ |
| 324 | let state = state.clone(); |
| 325 | let clicks = clicks.clone(); |
| 326 | move |_, _| ButtonDragHarness { state, clicks } |
| 327 | }); |
| 328 | let cx: &mut VisualTestContext = cx; |
| 329 | cx.update(|window, cx| window.draw(cx).clear(cx)); |
| 330 | |
| 331 | cx.simulate_click(point(px(10.), px(10.)), Modifiers::default()); |
| 332 | assert_eq!(clicks.get(), 1); |
| 333 | clicks.set(0); |
| 334 | |
| 335 | cx.simulate_mouse_down( |
| 336 | point(px(30.), px(10.)), |
| 337 | MouseButton::Left, |
| 338 | Modifiers::default(), |
| 339 | ); |
| 340 | assert!(state.read_with(cx, |state, _| state.is_interacting())); |
| 341 | cx.simulate_mouse_move( |
| 342 | point(px(10.), px(10.)), |
| 343 | Some(MouseButton::Left), |
| 344 | Modifiers::default(), |
| 345 | ); |
| 346 | assert!(state.read_with(cx, |state, _| state.is_pointer_drag_locked())); |
| 347 | cx.simulate_mouse_up( |
| 348 | point(px(10.), px(10.)), |
| 349 | MouseButton::Left, |
| 350 | Modifiers::default(), |
| 351 | ); |
| 352 | assert_eq!(clicks.get(), 0); |
| 353 | |
| 354 | cx.update(|window, cx| window.draw(cx).clear(cx)); |
| 355 | cx.simulate_click(point(px(10.), px(10.)), Modifiers::default()); |
| 356 | assert_eq!(clicks.get(), 1); |
| 357 | } |
| 358 | |
| 359 | struct NestedVerticalCarouselHarness { |
| 360 | state: Entity<CarouselState>, |
| 361 | outer_handle: ScrollHandle, |
| 362 | } |
| 363 | |
| 364 | impl Render for NestedVerticalCarouselHarness { |
| 365 | fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement { |
| 366 | div() |
| 367 | .id("outer-scroll") |
| 368 | .w(px(100.)) |
| 369 | .h(px(100.)) |
| 370 | .overflow_y_scroll() |
| 371 | .track_scroll(&self.outer_handle) |
| 372 | .child( |
| 373 | Carousel::new("vertical-carousel", &self.state) |
| 374 | .w(px(100.)) |
| 375 | .h(px(60.)) |
| 376 | .child( |
| 377 | CarouselContent::new(&self.state) |
| 378 | .h_full() |
| 379 | .children((0..2).map(|index| { |
| 380 | CarouselItem::new(("vertical-slide", index), index, &self.state) |
| 381 | .child(index.to_string()) |
| 382 | })), |
| 383 | ), |
| 384 | ) |
| 385 | .child(div().w_full().h(px(400.))) |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | fn nested_vertical_carousel<'a>( |
| 390 | cx: &'a mut TestAppContext, |
| 391 | state: &Entity<CarouselState>, |
| 392 | ) -> (ScrollHandle, &'a mut VisualTestContext) { |
| 393 | let outer_handle = ScrollHandle::new(); |
| 394 | let (_, cx) = cx.add_window_view({ |
| 395 | let state = state.clone(); |
| 396 | let outer_handle = outer_handle.clone(); |
| 397 | move |_, _| NestedVerticalCarouselHarness { |
| 398 | state, |
| 399 | outer_handle, |
| 400 | } |
| 401 | }); |
| 402 | cx.update(|window, cx| window.draw(cx).clear(cx)); |
| 403 | (outer_handle, cx) |
| 404 | } |
| 405 | |
| 406 | fn wheel_line(cx: &mut VisualTestContext, lines: f32) { |
| 407 | cx.simulate_event(ScrollWheelEvent { |
| 408 | position: point(px(20.), px(20.)), |
| 409 | delta: ScrollDelta::Lines(point(0., lines)), |
| 410 | ..Default::default() |
| 411 | }); |
| 412 | } |
| 413 | |
| 414 | fn swipe(cx: &mut VisualTestContext, delta: f32, touch_phase: TouchPhase) { |
| 415 | cx.simulate_event(ScrollWheelEvent { |
| 416 | position: point(px(20.), px(20.)), |
| 417 | delta: ScrollDelta::Pixels(point(px(0.), px(delta))), |
| 418 | touch_phase, |
| 419 | ..Default::default() |
| 420 | }); |
| 421 | } |
| 422 | |
| 423 | #[gpui::test] |
| 424 | fn vertical_carousel_hands_scroll_to_parent_at_edge(cx: &mut TestAppContext) { |
| 425 | cx.update(crate::init); |
| 426 | let state = cx.update(|cx| { |
| 427 | cx.new(|_| { |
| 428 | CarouselState::new(2) |
| 429 | .with_axis(Axis::Vertical) |
| 430 | .with_selected_index(1) |
| 431 | }) |
| 432 | }); |
| 433 | let (outer_handle, cx) = nested_vertical_carousel(cx, &state); |
| 434 | |
| 435 | wheel_line(cx, -1.); |
| 436 | |
| 437 | assert_eq!( |
| 438 | state.read_with(cx, |state, _| state.selected_index()), |
| 439 | Some(1) |
| 440 | ); |
| 441 | assert!(outer_handle.offset().y < px(0.)); |
| 442 | } |
| 443 | |
| 444 | #[gpui::test] |
| 445 | fn vertical_carousel_keeps_a_wheel_burst_away_from_the_parent(cx: &mut TestAppContext) { |
| 446 | cx.update(crate::init); |
| 447 | let state = cx.update(|cx| cx.new(|_| CarouselState::new(2).with_axis(Axis::Vertical))); |
| 448 | let (outer_handle, cx) = nested_vertical_carousel(cx, &state); |
| 449 | |
| 450 | // One notch steps once; the rest of its burst stays here. |
| 451 | wheel_line(cx, -1.); |
| 452 | wheel_line(cx, -1.); |
| 453 | assert_eq!( |
| 454 | state.read_with(cx, |state, _| state.selected_index()), |
| 455 | Some(1) |
| 456 | ); |
| 457 | assert_eq!(outer_handle.offset().y, px(0.)); |
| 458 | |
| 459 | // A new notch at the edge scrolls the surrounding document instead. |
| 460 | cx.executor().advance_clock(SCROLL_EVENT_SEPARATION); |
| 461 | cx.run_until_parked(); |
| 462 | wheel_line(cx, -1.); |
| 463 | assert_eq!( |
| 464 | state.read_with(cx, |state, _| state.selected_index()), |
| 465 | Some(1) |
| 466 | ); |
| 467 | assert!(outer_handle.offset().y < px(0.)); |
| 468 | } |
| 469 | |
| 470 | #[gpui::test] |
| 471 | fn vertical_carousel_owns_a_trackpad_gesture_until_it_ends(cx: &mut TestAppContext) { |
| 472 | cx.update(crate::init); |
| 473 | let state = cx.update(|cx| cx.new(|_| CarouselState::new(2).with_axis(Axis::Vertical))); |
| 474 | let (outer_handle, cx) = nested_vertical_carousel(cx, &state); |
| 475 | |
| 476 | // The swipe overshoots the last item; the leftover stays here. |
| 477 | swipe(cx, -50., TouchPhase::Started); |
| 478 | swipe(cx, -50., TouchPhase::Moved); |
| 479 | swipe(cx, -50., TouchPhase::Moved); |
| 480 | swipe(cx, 0., TouchPhase::Ended); |
| 481 | assert_eq!(outer_handle.offset().y, px(0.)); |
| 482 | assert_eq!( |
| 483 | state.read_with(cx, |state, _| state.selected_index()), |
| 484 | Some(1) |
| 485 | ); |
| 486 | |
| 487 | // A gesture that begins at the edge scrolls the document instead. |
| 488 | swipe(cx, -50., TouchPhase::Started); |
| 489 | assert!(outer_handle.offset().y < px(0.)); |
| 490 | } |
| 491 | } |