| 1 | use std::rc::Rc; |
| 2 | |
| 3 | use gpui::{ |
| 4 | App, Bounds, Hitbox, HitboxBehavior, IntoElement, MouseButton, MouseDownEvent, MouseMoveEvent, |
| 5 | MouseUpEvent, Pixels, Point, RenderOnce, Styled as _, TouchDragEvent, TouchPhase, Window, |
| 6 | canvas, deferred, |
| 7 | }; |
| 8 | use gpui_base::{SelectionEdge, TouchHandle, TouchSelectionSnapshot}; |
| 9 | |
| 10 | use crate::ActiveTheme as _; |
| 11 | |
| 12 | /// Reads the touch selection as laid out by the time the handles paint. |
| 13 | pub(crate) type SnapshotSource = Rc<dyn Fn(&Window, &App) -> Option<TouchSelectionSnapshot>>; |
| 14 | /// What a handle's owner gets told as the finger moves it. |
| 15 | pub(crate) type DragHandler = |
| 16 | Rc<dyn Fn(SelectionEdge, TouchPhase, Point<Pixels>, &mut Window, &mut App)>; |
| 17 | /// Where a surface was painted this frame, for owners that must protect it. |
| 18 | pub(crate) type SurfaceHandler = Rc<dyn Fn(Bounds<Pixels>, &mut Window, &mut App)>; |
| 19 | |
| 20 | /// The grab handles at the ends of an input's touch selection, floating |
| 21 | /// above the input: its knobs reach past the field's edge, where the input's |
| 22 | /// own clip would cut them off. (Text that takes part in the window selection |
| 23 | /// paints its handles itself, in place; see |
| 24 | /// [`gpui_base::TextSelectionHandle::paint_touch_handles`].) |
| 25 | /// |
| 26 | /// The handles read the selection's geometry as they paint, after the input |
| 27 | /// has painted this frame, so they sit on the text as it is now rather than |
| 28 | /// as it was a frame ago while it scrolls. |
| 29 | /// |
| 30 | /// A handle claims the touch drag that begins on it before the window can |
| 31 | /// take the drag for panning, and blocks the mouse so a stray tap on it does |
| 32 | /// not reach the text underneath. A mouse can drag it too, which is how it is |
| 33 | /// exercised without a touch screen. |
| 34 | #[derive(IntoElement)] |
| 35 | pub(crate) struct SelectionHandles { |
| 36 | source: SnapshotSource, |
| 37 | on_drag: DragHandler, |
| 38 | on_paint: Option<SurfaceHandler>, |
| 39 | } |
| 40 | |
| 41 | /// One handle laid out for this frame. |
| 42 | struct LaidOutHandle { |
| 43 | edge: SelectionEdge, |
| 44 | caret: Bounds<Pixels>, |
| 45 | hitbox: Hitbox, |
| 46 | } |
| 47 | |
| 48 | impl SelectionHandles { |
| 49 | pub(crate) fn new(source: SnapshotSource, on_drag: DragHandler) -> Self { |
| 50 | Self { |
| 51 | source, |
| 52 | on_drag, |
| 53 | on_paint: None, |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | pub(crate) fn on_paint(mut self, on_paint: SurfaceHandler) -> Self { |
| 58 | self.on_paint = Some(on_paint); |
| 59 | self |
| 60 | } |
| 61 | |
| 62 | /// Lays out a handle for each end of a non-empty selection that is in |
| 63 | /// view. |
| 64 | fn layout(source: &SnapshotSource, window: &mut Window, cx: &mut App) -> Vec<LaidOutHandle> { |
| 65 | let Some(snapshot) = source(window, cx) else { |
| 66 | return Vec::new(); |
| 67 | }; |
| 68 | let window_bounds = Bounds::new(Point::default(), window.viewport_size()); |
| 69 | let mut handles = Vec::with_capacity(2); |
| 70 | if !snapshot.is_empty() { |
| 71 | for edge in [SelectionEdge::Start, SelectionEdge::End] { |
| 72 | // No handle for an end scrolled out of its owner, nor for one |
| 73 | // outside the window. |
| 74 | let caret = snapshot.edge(edge); |
| 75 | if !snapshot.is_edge_visible(edge) || !window_bounds.contains(&caret.origin) { |
| 76 | continue; |
| 77 | } |
| 78 | let hitbox = window.insert_hitbox( |
| 79 | TouchHandle::hit_bounds(edge, caret), |
| 80 | HitboxBehavior::BlockMouse, |
| 81 | ); |
| 82 | handles.push(LaidOutHandle { |
| 83 | edge, |
| 84 | caret, |
| 85 | hitbox, |
| 86 | }); |
| 87 | } |
| 88 | } |
| 89 | handles |
| 90 | } |
| 91 | |
| 92 | fn paint_handle(handle: &LaidOutHandle, window: &mut Window, cx: &mut App) { |
| 93 | TouchHandle::paint( |
| 94 | handle.edge, |
| 95 | handle.caret, |
| 96 | cx.theme().selection.alpha(1.), |
| 97 | window, |
| 98 | ); |
| 99 | } |
| 100 | |
| 101 | /// The drag begins on whichever handle is under the finger or pointer; |
| 102 | /// its moves and its end are routed by which end is being dragged, so |
| 103 | /// they keep coming even if that handle is not laid out for a frame. |
| 104 | fn listen( |
| 105 | handles: &[LaidOutHandle], |
| 106 | source: &SnapshotSource, |
| 107 | on_drag: &DragHandler, |
| 108 | window: &mut Window, |
| 109 | ) { |
| 110 | for handle in handles { |
| 111 | // Touch: the drag is offered on the first touch, before it can |
| 112 | // become a tap, a long press or a pan. |
| 113 | window.on_mouse_event({ |
| 114 | let hitbox = handle.hitbox.clone(); |
| 115 | let edge = handle.edge; |
| 116 | let on_drag = on_drag.clone(); |
| 117 | move |event: &TouchDragEvent, phase, window, cx| { |
| 118 | if !phase.bubble() |
| 119 | || event.phase != TouchPhase::Started |
| 120 | || window.default_prevented() |
| 121 | || !hitbox.is_hovered(window) |
| 122 | { |
| 123 | return; |
| 124 | } |
| 125 | window.prevent_default(); |
| 126 | cx.stop_propagation(); |
| 127 | on_drag(edge, TouchPhase::Started, event.position, window, cx); |
| 128 | } |
| 129 | }); |
| 130 | // Mouse: the same drag for a pointer. |
| 131 | window.on_mouse_event({ |
| 132 | let hitbox = handle.hitbox.clone(); |
| 133 | let edge = handle.edge; |
| 134 | let on_drag = on_drag.clone(); |
| 135 | move |event: &MouseDownEvent, phase, window, cx| { |
| 136 | if !phase.bubble() |
| 137 | || event.button != MouseButton::Left |
| 138 | || !hitbox.is_hovered(window) |
| 139 | { |
| 140 | return; |
| 141 | } |
| 142 | cx.stop_propagation(); |
| 143 | on_drag(edge, TouchPhase::Started, event.position, window, cx); |
| 144 | } |
| 145 | }); |
| 146 | } |
| 147 | |
| 148 | // The drag in progress is read live: it may have begun after this |
| 149 | // frame painted, and its first moves must not be lost. |
| 150 | let dragging = { |
| 151 | let source = source.clone(); |
| 152 | move |window: &Window, cx: &App| source(window, cx).and_then(|s| s.dragging()) |
| 153 | }; |
| 154 | window.on_mouse_event({ |
| 155 | let on_drag = on_drag.clone(); |
| 156 | let dragging = dragging.clone(); |
| 157 | move |event: &TouchDragEvent, phase, window, cx| { |
| 158 | if !phase.bubble() || event.phase == TouchPhase::Started { |
| 159 | return; |
| 160 | } |
| 161 | let Some(edge) = dragging(window, cx) else { |
| 162 | return; |
| 163 | }; |
| 164 | cx.stop_propagation(); |
| 165 | on_drag(edge, event.phase, event.position, window, cx); |
| 166 | } |
| 167 | }); |
| 168 | window.on_mouse_event({ |
| 169 | let on_drag = on_drag.clone(); |
| 170 | let dragging = dragging.clone(); |
| 171 | move |event: &MouseMoveEvent, phase, window, cx| { |
| 172 | if !phase.bubble() || event.pressed_button != Some(MouseButton::Left) { |
| 173 | return; |
| 174 | } |
| 175 | let Some(edge) = dragging(window, cx) else { |
| 176 | return; |
| 177 | }; |
| 178 | on_drag(edge, TouchPhase::Moved, event.position, window, cx); |
| 179 | } |
| 180 | }); |
| 181 | window.on_mouse_event({ |
| 182 | let on_drag = on_drag.clone(); |
| 183 | move |event: &MouseUpEvent, phase, window, cx| { |
| 184 | if !phase.bubble() || event.button != MouseButton::Left { |
| 185 | return; |
| 186 | } |
| 187 | let Some(edge) = dragging(window, cx) else { |
| 188 | return; |
| 189 | }; |
| 190 | on_drag(edge, TouchPhase::Ended, event.position, window, cx); |
| 191 | } |
| 192 | }); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | impl RenderOnce for SelectionHandles { |
| 197 | fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement { |
| 198 | let source = self.source; |
| 199 | let on_drag = self.on_drag; |
| 200 | let on_paint = self.on_paint; |
| 201 | deferred( |
| 202 | canvas( |
| 203 | { |
| 204 | let source = source.clone(); |
| 205 | move |_, window, cx| Self::layout(&source, window, cx) |
| 206 | }, |
| 207 | move |_, handles, window, cx| { |
| 208 | for handle in &handles { |
| 209 | Self::paint_handle(handle, window, cx); |
| 210 | if let Some(on_paint) = on_paint.as_ref() { |
| 211 | on_paint(handle.hitbox.bounds, window, cx); |
| 212 | } |
| 213 | } |
| 214 | Self::listen(&handles, &source, &on_drag, window); |
| 215 | }, |
| 216 | ) |
| 217 | .absolute() |
| 218 | .size_0(), |
| 219 | ) |
| 220 | .with_priority(gpui_base::POPUP_PRIORITY) |
| 221 | } |
| 222 | } |