| 1 | //! Touch selection: the grab handles and the edit menu a long press leaves |
| 2 | //! behind, drawn over an [`crate::input::Input`] or over the window text |
| 3 | //! selection that [`crate::text::TextView`] takes part in. |
| 4 | //! |
| 5 | //! Base owns the gesture and the drag; see [`gpui_base::TouchSelectionSnapshot`]. |
| 6 | //! This module draws what Base laid out: a handle at each end of the |
| 7 | //! selection, and a row of commands above it. |
| 8 | |
| 9 | mod edit_menu; |
| 10 | mod handle; |
| 11 | mod window_overlay; |
| 12 | |
| 13 | use std::rc::Rc; |
| 14 | |
| 15 | use gpui::{AnyElement, App, Bounds, ElementId, IntoElement, Pixels, Point, TouchPhase, Window}; |
| 16 | use gpui_base::{SelectionEdge, TouchHandle, TouchSelectionSnapshot}; |
| 17 | |
| 18 | pub(crate) use edit_menu::{EditMenu, EditMenuItem}; |
| 19 | pub(crate) use handle::{DragHandler, SelectionHandles, SnapshotSource, SurfaceHandler}; |
| 20 | pub(crate) use window_overlay::WindowTouchSelectionOverlay; |
| 21 | |
| 22 | /// Draws one touch selection: its handles and, when open, its edit menu. |
| 23 | /// |
| 24 | /// The handles read the selection's geometry as they paint, so they follow |
| 25 | /// text that scrolls in the same frame. The menu is placed from the snapshot |
| 26 | /// read here; it steps aside while the text scrolls, so a frame's lag in its |
| 27 | /// anchor never shows. |
| 28 | pub(crate) struct TouchSelectionOverlay { |
| 29 | id: ElementId, |
| 30 | source: SnapshotSource, |
| 31 | items: Vec<EditMenuItem>, |
| 32 | on_drag: Option<DragHandler>, |
| 33 | on_paint: Option<SurfaceHandler>, |
| 34 | } |
| 35 | |
| 36 | impl TouchSelectionOverlay { |
| 37 | pub(crate) fn new( |
| 38 | id: impl Into<ElementId>, |
| 39 | source: impl Fn(&Window, &App) -> Option<TouchSelectionSnapshot> + 'static, |
| 40 | ) -> Self { |
| 41 | Self { |
| 42 | id: id.into(), |
| 43 | source: Rc::new(source), |
| 44 | items: Vec::new(), |
| 45 | on_drag: None, |
| 46 | on_paint: None, |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /// Draws floating handles, dragged through `on_drag`. An owner whose |
| 51 | /// text paints its own handles in place leaves this unset. |
| 52 | pub(crate) fn handles( |
| 53 | mut self, |
| 54 | on_drag: impl Fn(SelectionEdge, TouchPhase, Point<Pixels>, &mut Window, &mut App) + 'static, |
| 55 | ) -> Self { |
| 56 | self.on_drag = Some(Rc::new(on_drag)); |
| 57 | self |
| 58 | } |
| 59 | |
| 60 | /// The commands the edit menu offers. With none, no menu is drawn. |
| 61 | pub(crate) fn items(mut self, items: impl IntoIterator<Item = EditMenuItem>) -> Self { |
| 62 | self.items.extend(items); |
| 63 | self |
| 64 | } |
| 65 | |
| 66 | /// Called with every surface's bounds as it paints, for an owner whose |
| 67 | /// press handling must leave those surfaces alone. |
| 68 | pub(crate) fn on_paint( |
| 69 | mut self, |
| 70 | on_paint: impl Fn(Bounds<Pixels>, &mut Window, &mut App) + 'static, |
| 71 | ) -> Self { |
| 72 | self.on_paint = Some(Rc::new(on_paint)); |
| 73 | self |
| 74 | } |
| 75 | |
| 76 | /// The elements to add to the owner: the handles, and the menu when it |
| 77 | /// is open. Each floats in window coordinates. |
| 78 | pub(crate) fn into_elements(self, window: &Window, cx: &App) -> Vec<AnyElement> { |
| 79 | let Some(snapshot) = (self.source)(window, cx) else { |
| 80 | return Vec::new(); |
| 81 | }; |
| 82 | let mut elements = Vec::with_capacity(2); |
| 83 | if let Some(on_drag) = self.on_drag { |
| 84 | let handles = SelectionHandles::new(self.source, on_drag); |
| 85 | let handles = match self.on_paint.clone() { |
| 86 | Some(on_paint) => handles.on_paint(on_paint), |
| 87 | None => handles, |
| 88 | }; |
| 89 | elements.push(handles.into_any_element()); |
| 90 | } |
| 91 | |
| 92 | if let Some(mut anchor) = snapshot |
| 93 | .bounds() |
| 94 | .filter(|_| snapshot.is_menu_open() && !self.items.is_empty()) |
| 95 | { |
| 96 | // Leave the knobs uncovered: the menu anchors to the selection |
| 97 | // plus the room its handles take above and below. |
| 98 | if !snapshot.is_empty() { |
| 99 | anchor.origin.y -= TouchHandle::EXTENT; |
| 100 | anchor.size.height += TouchHandle::EXTENT * 2.; |
| 101 | } |
| 102 | let menu = EditMenu::new(self.id, anchor).items(self.items); |
| 103 | let menu = match self.on_paint { |
| 104 | Some(on_paint) => menu.on_paint(on_paint), |
| 105 | None => menu, |
| 106 | }; |
| 107 | elements.push(menu.into_any_element()); |
| 108 | } |
| 109 | elements |
| 110 | } |
| 111 | } |