| 1 | use gpui::{ |
| 2 | App, ClickEvent, InteractiveElement, OngoingScroll, Pixels, Point, Stateful, |
| 3 | StatefulInteractiveElement, TouchPhase, Window, |
| 4 | }; |
| 5 | |
| 6 | /// gpui delimits scroll gestures with `std::time::Instant`, which is |
| 7 | /// unimplemented on wasm32: the first wheel event over an axis-locked scroll |
| 8 | /// area panics with "time not implemented on this platform" and takes the whole |
| 9 | /// application down, leaving the canvas unresponsive. Losing the axis lock in |
| 10 | /// the browser is by far the lesser cost, so the locks below are no-ops there. |
| 11 | pub trait OngoingScrollExt { |
| 12 | /// Locks a wheel delta to the axis its gesture started on, where the |
| 13 | /// platform supports it. |
| 14 | fn lock_axis(&mut self, delta: &mut Point<Pixels>, touch_phase: TouchPhase); |
| 15 | } |
| 16 | |
| 17 | impl OngoingScrollExt for OngoingScroll { |
| 18 | fn lock_axis(&mut self, delta: &mut Point<Pixels>, touch_phase: TouchPhase) { |
| 19 | #[cfg(target_family = "wasm")] |
| 20 | let _ = (delta, touch_phase); |
| 21 | #[cfg(not(target_family = "wasm"))] |
| 22 | self.filter(delta, touch_phase); |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | pub trait InteractiveElementExt: InteractiveElement { |
| 27 | /// Locks scrolling to the gesture's dominant axis, where the platform |
| 28 | /// supports it. See [`OngoingScrollExt`] for why this is a no-op on wasm32. |
| 29 | fn lock_scroll_axis(self) -> Self |
| 30 | where |
| 31 | Self: Sized + StatefulInteractiveElement, |
| 32 | { |
| 33 | #[cfg(target_family = "wasm")] |
| 34 | { |
| 35 | self |
| 36 | } |
| 37 | #[cfg(not(target_family = "wasm"))] |
| 38 | { |
| 39 | self.restrict_scroll_to_axis() |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | /// Set the listener for a double click event. |
| 44 | fn on_double_click( |
| 45 | mut self, |
| 46 | listener: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static, |
| 47 | ) -> Self |
| 48 | where |
| 49 | Self: Sized, |
| 50 | { |
| 51 | self.interactivity().on_click(move |event, window, cx| { |
| 52 | if event.click_count() == 2 { |
| 53 | listener(event, window, cx); |
| 54 | } |
| 55 | }); |
| 56 | self |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | impl<E: InteractiveElement> InteractiveElementExt for Stateful<E> {} |