BlueGit

ict rusty-teams

Files

rusty-teams / vendor / gpui-base / src / text_selection.rs

text_selection.rs

text_selection.rs 166.4 KiB Download Raw
1use std::{
2 collections::HashMap,
3 ops::Range,
4 rc::Rc,
5 sync::atomic::{AtomicU64, Ordering},
6};
7
8use gpui::{
9 App, AppContext as _, Bounds, Context, Element, ElementId, Entity, EntityId, EventEmitter,
10 Global, GlobalElementId, Half, Hitbox, HitboxBehavior, Hsla, InputEvent as _,
11 InspectorElementId, IntoElement, LayoutId, LongPressEvent, MouseButton, MouseDownEvent,
12 MouseMoveEvent, MouseUpEvent, Pixels, Point, ScrollDelta, ScrollWheelEvent, SharedString,
13 Style, Subscription, TextLayout, TouchDragEvent, TouchPhase, WeakEntity, Window, point, px,
14};
15
16use crate::text_boundary::{line_range_at, word_range_at};
17use crate::touch_selection::{
18 EdgeDrag, SelectionEdge, TouchHandle, TouchSelectionSnapshot, caret_in_view,
19};
20use crate::{AutoScroll, GlobalState};
21
22/// An opaque selection layer identifier.
23#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
24pub struct TextSelectionScopeId(u64);
25
26impl TextSelectionScopeId {
27 /// Allocates a process-unique scope identifier.
28 ///
29 /// Keep the returned identifier for the semantic lifetime of the scope;
30 /// do not allocate a new identifier on every frame.
31 pub fn new() -> Self {
32 static NEXT_SCOPE_ID: AtomicU64 = AtomicU64::new(1);
33 let value = NEXT_SCOPE_ID
34 .try_update(Ordering::Relaxed, Ordering::Relaxed, |value| {
35 value.checked_add(1)
36 })
37 .expect("text selection scope identifiers exhausted");
38 Self(value)
39 }
40
41 #[cfg(test)]
42 const fn from_raw(value: u64) -> Self {
43 Self(value)
44 }
45}
46
47/// Stable participant-defined identity for virtualized participant content.
48#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
49pub struct TextSelectionContentKey(u64);
50
51impl TextSelectionContentKey {
52 /// Creates a key from a participant-defined stable content identity.
53 pub const fn new(value: u64) -> Self {
54 Self(value)
55 }
56
57 /// Returns the participant-defined value.
58 pub const fn value(self) -> u64 {
59 self.0
60 }
61}
62
63/// A selection endpoint anchored to a participant's content coordinates.
64#[derive(Clone, Copy, Debug, PartialEq)]
65pub struct TextSelectionEndpoint {
66 entity_id: Option<EntityId>,
67 point: Point<Pixels>,
68 content_key: Option<TextSelectionContentKey>,
69}
70
71impl TextSelectionEndpoint {
72 /// Creates an endpoint at a participant-relative content point.
73 pub(crate) const fn new(entity_id: Option<EntityId>, point: Point<Pixels>) -> Self {
74 Self {
75 entity_id,
76 point,
77 content_key: None,
78 }
79 }
80
81 /// Sets participant-defined endpoint metadata.
82 pub(crate) const fn with_content_key(mut self, content_key: TextSelectionContentKey) -> Self {
83 self.content_key = Some(content_key);
84 self
85 }
86
87 /// Returns the participant which owns this endpoint, when it hit one.
88 pub const fn entity_id(&self) -> Option<EntityId> {
89 self.entity_id
90 }
91
92 /// Returns the participant-relative content point.
93 pub const fn content_point(&self) -> Point<Pixels> {
94 self.point
95 }
96
97 /// Returns participant-defined endpoint metadata captured when it hit a participant.
98 pub const fn content_key(&self) -> Option<TextSelectionContentKey> {
99 self.content_key
100 }
101}
102
103/// Window-coordinate anchor and cursor points for painting a selection.
104#[derive(Clone, Copy, Debug, PartialEq)]
105pub struct TextSelectionWindowPoints {
106 anchor: Point<Pixels>,
107 cursor: Point<Pixels>,
108}
109
110impl TextSelectionWindowPoints {
111 /// Returns the stable anchor in window coordinates.
112 pub const fn anchor(&self) -> Point<Pixels> {
113 self.anchor
114 }
115
116 /// Returns the moving cursor in window coordinates.
117 pub const fn cursor(&self) -> Point<Pixels> {
118 self.cursor
119 }
120}
121
122/// Participant-relative selection endpoints with an optional rendering projection.
123#[derive(Clone, Copy, Debug, PartialEq)]
124pub struct TextSelectionSnapshot {
125 anchor: TextSelectionEndpoint,
126 cursor: TextSelectionEndpoint,
127 is_selecting: bool,
128 window_points: Option<TextSelectionWindowPoints>,
129 coverage: TextSelectionCoverage,
130}
131
132/// How much of one participant participates in a window selection.
133#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
134pub enum TextSelectionCoverage {
135 /// Only the interval between this participant's two endpoints is selected.
136 #[default]
137 Bounded,
138 /// The participant is selected from its beginning through its endpoint.
139 FromStart,
140 /// The participant is selected from its endpoint through its end.
141 ToEnd,
142 /// The entire participant lies between endpoints in other participants.
143 Full,
144}
145
146impl TextSelectionSnapshot {
147 /// Creates a snapshot from stable participant-relative endpoints.
148 pub(crate) const fn new(anchor: TextSelectionEndpoint, cursor: TextSelectionEndpoint) -> Self {
149 Self {
150 anchor,
151 cursor,
152 is_selecting: false,
153 window_points: None,
154 coverage: TextSelectionCoverage::Bounded,
155 }
156 }
157
158 /// Sets whether the pointer gesture is still active.
159 pub(crate) const fn with_selecting(mut self, is_selecting: bool) -> Self {
160 self.is_selecting = is_selecting;
161 self
162 }
163
164 /// Sets the current window-coordinate rendering projection.
165 pub(crate) const fn with_window_points(
166 mut self,
167 window_points: Option<TextSelectionWindowPoints>,
168 ) -> Self {
169 self.window_points = window_points;
170 self
171 }
172
173 /// Sets the portion of the receiving participant covered by this selection.
174 #[cfg(test)]
175 pub(crate) const fn with_coverage(mut self, coverage: TextSelectionCoverage) -> Self {
176 self.coverage = coverage;
177 self
178 }
179
180 /// Returns the stable anchor endpoint.
181 pub const fn anchor(&self) -> TextSelectionEndpoint {
182 self.anchor
183 }
184
185 /// Returns the moving cursor endpoint.
186 pub const fn cursor(&self) -> TextSelectionEndpoint {
187 self.cursor
188 }
189
190 /// Returns whether the pointer gesture is still active.
191 pub const fn is_selecting(&self) -> bool {
192 self.is_selecting
193 }
194
195 /// Returns the window-coordinate endpoints for participants that need them.
196 pub const fn window_points(&self) -> Option<TextSelectionWindowPoints> {
197 self.window_points
198 }
199
200 /// Returns the portion of the receiving participant covered by this selection.
201 pub const fn coverage(&self) -> TextSelectionCoverage {
202 self.coverage
203 }
204}
205
206/// Retained element state proving a participant is still in the window.
207///
208/// A participant reports its geometry from `paint`, which a cached view
209/// (`Entity::cached`) skips: GPUI replays the recorded frame instead. The
210/// element's retained state is replayed with it, so this marker outlives the
211/// frames whose paint never ran, and dies with the frame that drops the
212/// element for good.
213struct RenderedMarker;
214
215/// Per-frame geometry reported by a [`TextSelectionHandle`] participant.
216pub struct TextSelectionRegistration {
217 hitbox: Hitbox,
218 bounds: Bounds<Pixels>,
219 scroll_offset: Point<Pixels>,
220 scope: TextSelectionScopeId,
221 document_order: u64,
222 text_bounds: Vec<Bounds<Pixels>>,
223 self_scroll: bool,
224 selection_edges: Option<(Bounds<Pixels>, Bounds<Pixels>)>,
225 rendered: Option<WeakEntity<RenderedMarker>>,
226}
227
228impl TextSelectionRegistration {
229 /// Creates a registration with default scope, order, and scroll offset.
230 pub fn new(hitbox: Hitbox, bounds: Bounds<Pixels>) -> Self {
231 Self {
232 hitbox,
233 bounds,
234 scroll_offset: Point::default(),
235 scope: TextSelectionScopeId::default(),
236 document_order: 0,
237 text_bounds: Vec::new(),
238 self_scroll: false,
239 selection_edges: None,
240 rendered: None,
241 }
242 }
243
244 /// Ties this registration to the retained state of the element reporting
245 /// it, so that a frame replayed from that element's cached view keeps it.
246 ///
247 /// Call this from the reporting element's `prepaint` or `paint`. A
248 /// registration made outside a drawing element cannot be tied to one and
249 /// is swept the first frame it misses, as every registration used to be.
250 pub fn with_rendered_element(
251 mut self,
252 participant: &TextSelectionHandle,
253 window: &mut Window,
254 cx: &mut App,
255 ) -> Self {
256 self.rendered = Some(
257 window
258 .use_keyed_state(
259 ElementId::NamedInteger(
260 "text-selection-participant".into(),
261 participant.entity_id().as_u64(),
262 ),
263 cx,
264 |_, _| RenderedMarker,
265 )
266 .downgrade(),
267 );
268 self
269 }
270
271 /// Whether the element that reported this registration is still part of
272 /// the window, even if it did not paint the frame that just finished.
273 fn is_rendered(&self) -> bool {
274 self.rendered
275 .as_ref()
276 .is_some_and(|marker| marker.upgrade().is_some())
277 }
278
279 /// Marks a participant that scrolls its own content in response to
280 /// [`TextSelectionEvent::AutoScroll`]. Drag auto-scroll then drives it
281 /// directly, measured against its own bounds, instead of synthesizing a
282 /// wheel event for the nearest scrollable ancestor.
283 pub(crate) fn with_self_scroll(mut self, self_scroll: bool) -> Self {
284 self.self_scroll = self_scroll;
285 self
286 }
287
288 /// Sets the participant's content scroll offset.
289 pub fn with_scroll_offset(mut self, scroll_offset: Point<Pixels>) -> Self {
290 self.scroll_offset = scroll_offset;
291 self
292 }
293
294 /// Sets the opaque selection scope.
295 pub fn with_scope(mut self, scope: TextSelectionScopeId) -> Self {
296 self.scope = scope;
297 self
298 }
299
300 /// Sets the stable logical document order.
301 pub fn with_document_order(mut self, document_order: u64) -> Self {
302 self.document_order = document_order;
303 self
304 }
305
306 /// Sets the glyph-bearing bounds used to reject blank-only gestures.
307 pub fn with_text_bounds(mut self, text_bounds: Vec<Bounds<Pixels>>) -> Self {
308 self.text_bounds = text_bounds;
309 self
310 }
311
312 /// Sets where the participant painted the two ends of its selection: the
313 /// caret line box before its first selected character and the one after
314 /// its last, in window coordinates. The touch handles are drawn there.
315 pub fn with_selection_edges(mut self, start: Bounds<Pixels>, end: Bounds<Pixels>) -> Self {
316 self.selection_edges = Some((start, end));
317 self
318 }
319
320 /// Returns the participant hitbox.
321 pub fn hitbox(&self) -> &Hitbox {
322 &self.hitbox
323 }
324
325 /// Returns the participant's window-coordinate bounds.
326 pub const fn bounds(&self) -> Bounds<Pixels> {
327 self.bounds
328 }
329
330 /// Returns the participant's content scroll offset.
331 pub const fn scroll_offset(&self) -> Point<Pixels> {
332 self.scroll_offset
333 }
334
335 /// Returns the opaque selection scope.
336 pub const fn scope(&self) -> TextSelectionScopeId {
337 self.scope
338 }
339
340 /// Returns the stable logical document order.
341 pub const fn document_order(&self) -> u64 {
342 self.document_order
343 }
344
345 /// Returns the glyph-bearing bounds used to reject blank-only gestures.
346 pub fn text_bounds(&self) -> &[Bounds<Pixels>] {
347 &self.text_bounds
348 }
349
350 /// Returns the caret line boxes at the participant's selection ends.
351 pub const fn selection_edges(&self) -> Option<(Bounds<Pixels>, Bounds<Pixels>)> {
352 self.selection_edges
353 }
354}
355
356/// Laid-out text reported by a plain selection participant during paint.
357#[derive(Clone)]
358pub struct TextSelectionRun {
359 /// Logical order within the containing participant.
360 document_order: u64,
361 /// The exact text used to produce `layout`.
362 text: SharedString,
363 /// Laid-out glyph geometry in window coordinates.
364 layout: TextLayout,
365 /// The run's window-coordinate paint bounds.
366 bounds: Bounds<Pixels>,
367}
368
369impl TextSelectionRun {
370 /// Creates a laid-out text run.
371 pub fn new(text: impl Into<SharedString>, layout: TextLayout, bounds: Bounds<Pixels>) -> Self {
372 Self {
373 document_order: 0,
374 text: text.into(),
375 layout,
376 bounds,
377 }
378 }
379
380 /// Sets the run's logical order within the participant.
381 pub const fn with_document_order(mut self, document_order: u64) -> Self {
382 self.document_order = document_order;
383 self
384 }
385
386 /// Returns the run's logical order within its participant.
387 pub const fn document_order(&self) -> u64 {
388 self.document_order
389 }
390
391 /// Returns the exact text used to produce the layout.
392 pub fn text(&self) -> &SharedString {
393 &self.text
394 }
395
396 /// Returns the laid-out glyph geometry.
397 pub fn layout(&self) -> &TextLayout {
398 &self.layout
399 }
400
401 /// Returns the run's window-coordinate paint bounds.
402 pub const fn bounds(&self) -> Bounds<Pixels> {
403 self.bounds
404 }
405}
406
407/// Selection projected onto a participant's laid-out text runs.
408#[derive(Clone, Debug, Default, PartialEq, Eq)]
409pub struct TextSelectionProjection {
410 /// Selected UTF-8 byte ranges paired with the input runs.
411 ranges: Vec<Option<Range<usize>>>,
412 /// Whether the participant participates in the current selection.
413 is_active: bool,
414}
415
416impl TextSelectionProjection {
417 /// Returns selected UTF-8 byte ranges paired with the input runs.
418 pub fn ranges(&self) -> &[Option<Range<usize>>] {
419 &self.ranges
420 }
421
422 /// Returns whether the participant participates in the selection.
423 pub const fn is_active(&self) -> bool {
424 self.is_active
425 }
426}
427
428/// Projects a participant selection snapshot onto laid-out plain-text runs.
429///
430/// The returned states retain the input order so callers can pair every state
431/// with its run. The ranges are always character boundaries; `order` is used
432/// only when a participant caches selected text for copying.
433fn project_ranges(
434 snapshot: Option<TextSelectionSnapshot>,
435 runs: &[TextSelectionRun],
436) -> TextSelectionProjection {
437 let Some(snapshot) = snapshot else {
438 return TextSelectionProjection {
439 ranges: vec![None; runs.len()],
440 is_active: false,
441 };
442 };
443 let Some(window_points) = snapshot.window_points() else {
444 return TextSelectionProjection {
445 ranges: vec![None; runs.len()],
446 is_active: true,
447 };
448 };
449
450 TextSelectionProjection {
451 ranges: runs
452 .iter()
453 .map(|run| selection_range_for_run(run, window_points.anchor, window_points.cursor))
454 .collect(),
455 is_active: true,
456 }
457}
458
459fn selection_range_for_run(
460 run: &TextSelectionRun,
461 selection_start: Point<Pixels>,
462 selection_end: Point<Pixels>,
463) -> Option<Range<usize>> {
464 if run.text.len() != run.layout.len() {
465 return None;
466 }
467
468 if run.text.is_empty() {
469 return None;
470 }
471
472 let line_height = run.layout.line_height();
473 // Each character is tested with its row's top and height, so a run whose
474 // rows all miss the band, or all lie strictly inside it with no endpoint
475 // on any row, has the same answer for every character. Decide those
476 // without the walk below, which scans the layout twice per character.
477 let (rows_top, rows_bottom) = text_rows_extent(&run.layout, line_height);
478 let band_top = selection_start.y.min(selection_end.y);
479 let band_bottom = selection_start.y.max(selection_end.y);
480 if rows_bottom <= band_top || rows_top > band_bottom {
481 return None;
482 }
483 if band_top < rows_top && band_bottom >= rows_bottom {
484 return Some(0..run.text.len());
485 }
486
487 let mut range = None;
488 for (offset, character) in run.text.char_indices() {
489 let next_offset = offset + character.len_utf8();
490 let Some(position) = run.layout.position_for_index(offset) else {
491 continue;
492 };
493
494 let char_width = run
495 .layout
496 .position_for_index(next_offset)
497 .filter(|next| next.y == position.y)
498 .map_or_else(|| line_height.half(), |next| next.x - position.x);
499
500 if point_in_selection_band(
501 position,
502 char_width,
503 selection_start,
504 selection_end,
505 line_height,
506 ) {
507 range.get_or_insert(offset..offset).end = next_offset;
508 }
509 }
510 range
511}
512
513/// The top of the first laid-out row of `text_layout` and the bottom of its
514/// last one, each row `line_height` tall as a selection band test sees it.
515///
516/// Both ends are accumulated the way [`TextLayout::position_for_index`] places
517/// rows, so comparisons against them agree with a per-character walk to the
518/// bit. The last laid-out row may hold no character such a walk tests (a
519/// trailing empty line, or a row whose only character is placed at the end of
520/// the row before it); callers treat the extent as covering, never as exact.
521pub(crate) fn text_rows_extent(text_layout: &TextLayout, line_height: Pixels) -> (Pixels, Pixels) {
522 let top = text_layout.bounds().top();
523 let layout_line_height = text_layout.line_height();
524 let lines = text_layout.line_layouts();
525 let mut last_line_top = top;
526 for line in lines.iter().take(lines.len().saturating_sub(1)) {
527 last_line_top += line.size(layout_line_height).height;
528 }
529 let last_row_top = lines.last().map_or(top, |line| {
530 last_line_top + line.wrap_boundaries.len() as f32 * layout_line_height
531 });
532 (top, last_row_top + line_height)
533}
534
535fn points_for_multi_click(
536 runs: &[TextSelectionRun],
537 position: Point<Pixels>,
538 click_count: usize,
539) -> Option<(Point<Pixels>, Point<Pixels>)> {
540 let run = runs.iter().find(|run| run.bounds.contains(&position))?;
541 if run.text.len() != run.layout.len() {
542 return None;
543 }
544 let offset = run.layout.index_for_position(position).ok()?;
545 let range = match click_count {
546 2 => word_range_at(&run.text, offset)?,
547 3.. => line_range_at(&run.text, offset),
548 _ => return None,
549 };
550 if range.is_empty() {
551 return None;
552 }
553 Some((
554 run.layout.position_for_index(range.start)?,
555 run.layout.position_for_index(range.end)?,
556 ))
557}
558
559fn point_in_selection_band(
560 position: Point<Pixels>,
561 char_width: Pixels,
562 selection_start: Point<Pixels>,
563 selection_end: Point<Pixels>,
564 line_height: Pixels,
565) -> bool {
566 let point_in_line =
567 |point: Point<Pixels>| point.y >= position.y && point.y < position.y + line_height;
568 let top = selection_start.y.min(selection_end.y);
569 let bottom = selection_start.y.max(selection_end.y);
570 let x = position.x + char_width.half();
571
572 if position.y + line_height <= top || position.y > bottom {
573 return false;
574 }
575
576 if point_in_line(selection_start) && point_in_line(selection_end) {
577 let left = selection_start.x.min(selection_end.x);
578 let right = selection_start.x.max(selection_end.x);
579 return x >= left && x <= right;
580 }
581
582 let (top_point, bottom_point) = if selection_start.y < selection_end.y {
583 (selection_start, selection_end)
584 } else {
585 (selection_end, selection_start)
586 };
587 if point_in_line(top_point) {
588 x >= top_point.x
589 } else if point_in_line(bottom_point) {
590 x <= bottom_point.x
591 } else {
592 true
593 }
594}
595
596type FocusCallback = Rc<dyn Fn(&mut Window, &mut App)>;
597type ClearHandler = Rc<dyn Fn(&mut App)>;
598type CopyCallback = Rc<dyn Fn(&mut App) -> String>;
599type ContentKeyResolver = Rc<dyn Fn(Point<Pixels>, &App) -> Option<TextSelectionContentKey>>;
600
601/// Notifications emitted by a text-selection participant.
602#[derive(Clone, Copy, Debug, PartialEq)]
603pub enum TextSelectionEvent {
604 /// The participant's window-selection projection changed.
605 SelectionChanged(Option<TextSelectionSnapshot>),
606 /// The active drag requests vertical auto-scroll, or `None` to stop.
607 AutoScroll(Option<Pixels>),
608 /// Window selection cleared the participant's participant-local state.
609 Cleared,
610 /// The touch selection the participant takes part in changed: it
611 /// appeared, its ends moved, a handle drag began or ended, or it went
612 /// away. The participant paints the handles and lays their hitboxes out
613 /// from the ends it painted last frame, so it renders once more.
614 TouchSelectionChanged,
615}
616
617struct CopyItem {
618 document_order: u64,
619 callback: Option<CopyCallback>,
620 fallback: String,
621}
622
623fn resolve_copy_items(mut items: Vec<CopyItem>, cx: &mut App) -> String {
624 items.sort_by_key(|item| item.document_order);
625 items
626 .into_iter()
627 .map(|item| {
628 item.callback
629 .map(|callback| callback(cx))
630 .unwrap_or(item.fallback)
631 })
632 .filter(|text| !text.trim().is_empty())
633 .collect::<Vec<_>>()
634 .join("\n")
635}
636
637fn dispatch_clear_handlers(handlers: Vec<ClearHandler>, cx: &mut App) {
638 for handler in handlers {
639 handler(cx);
640 }
641}
642
643struct SelectableTextState {
644 fallback_copy_text: String,
645 projected_copy_text: Option<String>,
646 runs: Vec<TextSelectionRun>,
647 local_selection: bool,
648 snapshot: Option<TextSelectionSnapshot>,
649 on_focus: Option<FocusCallback>,
650 clear: Option<ClearHandler>,
651 copy: Option<CopyCallback>,
652 content_key_resolver: Option<ContentKeyResolver>,
653}
654
655impl EventEmitter<TextSelectionEvent> for SelectableTextState {}
656
657impl SelectableTextState {
658 fn new(fallback_copy_text: impl Into<String>) -> Self {
659 Self {
660 fallback_copy_text: fallback_copy_text.into(),
661 projected_copy_text: None,
662 runs: Vec::new(),
663 local_selection: false,
664 snapshot: None,
665 on_focus: None,
666 clear: None,
667 copy: None,
668 content_key_resolver: None,
669 }
670 }
671
672 /// The current geometry selection snapshot for this participant.
673 fn snapshot(&self) -> Option<TextSelectionSnapshot> {
674 self.snapshot
675 }
676
677 /// Sets the text copied by this participant when it participates in selection.
678 fn set_fallback_copy_text(&mut self, text: impl Into<String>) {
679 self.fallback_copy_text = text.into();
680 self.projected_copy_text = None;
681 }
682
683 /// Marks participant-local selection (for example select-all) as active.
684 fn set_local_selection(&mut self, active: bool) {
685 self.local_selection = active;
686 }
687
688 /// Projects this participant's current snapshot onto plain-text runs and caches
689 /// their selected substrings for the window selection query.
690 ///
691 /// Call this once per painted run. A snapshot change or
692 /// Clearing window selection invalidates the cache immediately, so copy
693 /// never returns text from a previous projection while waiting to repaint.
694 fn update_runs(&mut self, runs: &[TextSelectionRun]) -> TextSelectionProjection {
695 self.runs = runs.to_vec();
696 let states = project_ranges(self.snapshot, runs);
697 let mut selected_runs = runs
698 .iter()
699 .zip(states.ranges())
700 .enumerate()
701 .filter_map(|(index, (run, state))| {
702 state.as_ref().map(|range| {
703 debug_assert!(run.text.is_char_boundary(range.start));
704 debug_assert!(run.text.is_char_boundary(range.end));
705 (
706 run.document_order,
707 index,
708 run.text[range.clone()].to_string(),
709 )
710 })
711 })
712 .collect::<Vec<_>>();
713 selected_runs.sort_by_key(|(order, index, _)| (*order, *index));
714 self.projected_copy_text =
715 Some(selected_runs.into_iter().map(|(_, _, text)| text).collect());
716 states
717 }
718
719 /// Installs the callback which focuses the participant when a drag begins in it.
720 fn set_focus_handler(&mut self, callback: impl Fn(&mut Window, &mut App) + 'static) {
721 self.on_focus = Some(Rc::new(callback));
722 }
723
724 fn clear_with(&mut self, callback: impl Fn(&mut App) + 'static) {
725 self.clear = Some(Rc::new(callback));
726 }
727
728 /// Installs a participant-specific copy projection.
729 fn copy_with(&mut self, callback: impl Fn(&mut App) -> String + 'static) {
730 self.copy = Some(Rc::new(callback));
731 }
732
733 /// Installs a participant-specific lookup for stable virtualized content keys.
734 fn resolve_content_key_with(
735 &mut self,
736 callback: impl Fn(Point<Pixels>, &App) -> Option<TextSelectionContentKey> + 'static,
737 ) {
738 self.content_key_resolver = Some(Rc::new(callback));
739 }
740
741 fn set_snapshot(&mut self, snapshot: Option<TextSelectionSnapshot>, cx: &mut Context<Self>) {
742 if self.snapshot == snapshot {
743 return;
744 }
745 self.snapshot = snapshot;
746 self.projected_copy_text = None;
747 cx.emit(TextSelectionEvent::SelectionChanged(snapshot));
748 }
749
750 fn clear_state(&mut self, cx: &mut Context<Self>) -> Option<ClearHandler> {
751 self.snapshot = None;
752 self.projected_copy_text = None;
753 self.local_selection = false;
754 cx.emit(TextSelectionEvent::Cleared);
755 cx.emit(TextSelectionEvent::SelectionChanged(None));
756 self.clear.clone()
757 }
758
759 fn set_auto_scroll(&self, delta: Option<Pixels>, cx: &mut Context<Self>) {
760 cx.emit(TextSelectionEvent::AutoScroll(delta));
761 }
762
763 fn focus(&self, window: &mut Window, cx: &mut App) {
764 if let Some(callback) = self.on_focus.clone() {
765 window.defer(cx, move |window, cx| callback(window, cx));
766 }
767 }
768
769 fn copy_item(&self, document_order: u64) -> Option<CopyItem> {
770 (self.snapshot.is_some() || self.local_selection).then(|| CopyItem {
771 document_order,
772 callback: self.copy.clone(),
773 fallback: self
774 .projected_copy_text
775 .clone()
776 .unwrap_or_else(|| self.fallback_copy_text.clone()),
777 })
778 }
779}
780
781/// The touch handles a participant laid out for a frame, with their hitboxes.
782#[derive(Default)]
783pub struct TouchHandleLayout {
784 hitboxes: Vec<(SelectionEdge, Hitbox)>,
785}
786
787/// A stable, participant-neutral handle for text that participates in window selection.
788#[derive(Clone)]
789pub struct TextSelectionHandle(Entity<SelectableTextState>);
790
791impl TextSelectionHandle {
792 /// Creates a selection participant handle with fallback text for copying.
793 pub fn new(fallback_copy_text: impl Into<String>, cx: &mut App) -> Self {
794 Self(cx.new(|_| SelectableTextState::new(fallback_copy_text)))
795 }
796
797 /// Returns this participant's stable identity.
798 pub fn entity_id(&self) -> EntityId {
799 self.0.entity_id()
800 }
801
802 /// Returns the current geometry selection snapshot for this participant.
803 pub fn snapshot(&self, cx: &App) -> Option<TextSelectionSnapshot> {
804 self.0.read(cx).snapshot()
805 }
806
807 /// Sets the fallback text copied while this participant participates.
808 pub fn set_fallback_copy_text(&self, text: impl Into<String>, cx: &mut App) {
809 self.0
810 .update(cx, |state, _| state.set_fallback_copy_text(text));
811 }
812
813 /// Marks participant-local selection, such as select-all, as active.
814 pub fn set_local_selection(&self, active: bool, cx: &mut App) {
815 self.0
816 .update(cx, |state, _| state.set_local_selection(active));
817 }
818
819 /// Returns whether participant-local selection is active.
820 pub fn has_local_selection(&self, cx: &App) -> bool {
821 self.0.read(cx).local_selection
822 }
823
824 /// Registers this participant and its geometry for the current frame.
825 pub fn register(
826 &self,
827 mut registration: TextSelectionRegistration,
828 window: &mut Window,
829 cx: &mut App,
830 ) {
831 if let Some(scope) = current_text_selection_scope(window.window_handle().window_id(), cx) {
832 registration.scope = scope;
833 }
834 let Some(state) = WindowSelectionState::existing(window, cx) else {
835 return;
836 };
837 state.update(cx, |state, cx| {
838 state.register_participant(self.clone(), registration, cx)
839 });
840 }
841
842 /// Projects the current snapshot onto plain-text runs and caches their copy text.
843 pub fn update_runs(&self, runs: &[TextSelectionRun], cx: &mut App) -> TextSelectionProjection {
844 self.0.update(cx, |state, _| state.update_runs(runs))
845 }
846
847 // Rich text renders its own selection; retain geometry only for word hit testing.
848 pub(crate) fn set_hit_test_runs(&self, runs: &[TextSelectionRun], cx: &mut App) {
849 self.0.update(cx, |state, _| state.runs = runs.to_vec());
850 }
851
852 /// Subscribes to participant selection notifications.
853 pub fn subscribe(
854 &self,
855 mut callback: impl FnMut(&TextSelectionEvent, &mut App) + 'static,
856 cx: &mut App,
857 ) -> Subscription {
858 cx.subscribe(&self.0, move |_, event, cx| callback(event, cx))
859 }
860
861 /// Subscribes `window` to refresh whenever this participant's selection changes.
862 #[must_use = "retain the subscription or explicitly detach it"]
863 pub fn refresh_window_on_change(&self, window: &Window, cx: &mut App) -> Subscription {
864 let window = window.window_handle();
865 self.subscribe(
866 move |event, cx| {
867 if matches!(event, TextSelectionEvent::SelectionChanged(_)) {
868 _ = window.update(cx, |_, window, _| window.refresh());
869 }
870 },
871 cx,
872 )
873 }
874
875 /// Sets the callback which focuses the participant when a drag begins in it.
876 pub fn focus_with(&self, callback: impl Fn(&mut Window, &mut App) + 'static, cx: &mut App) {
877 self.0
878 .update(cx, |state, _| state.set_focus_handler(callback));
879 }
880
881 /// Sets the synchronous participant cleanup command used by window clear.
882 pub fn clear_with(&self, callback: impl Fn(&mut App) + 'static, cx: &mut App) {
883 self.0.update(cx, |state, _| state.clear_with(callback));
884 }
885
886 /// Sets a participant-specific copy projection.
887 pub fn copy_with(&self, callback: impl Fn(&mut App) -> String + 'static, cx: &mut App) {
888 self.0.update(cx, |state, _| state.copy_with(callback));
889 }
890
891 /// Lays out the touch handles at the ends of this participant's
892 /// selection, as they were painted last frame, and gives each a hitbox
893 /// where the finger takes it. Call during the participant's prepaint;
894 /// hand the result to [`Self::paint_touch_handles`].
895 pub fn prepaint_touch_handles(&self, window: &mut Window, cx: &App) -> TouchHandleLayout {
896 let Some(state) = WindowSelectionState::existing(window, cx) else {
897 return TouchHandleLayout::default();
898 };
899 let hitboxes = state
900 .read(cx)
901 .touch_handles_of(self.entity_id())
902 .into_iter()
903 .map(|(edge, caret)| {
904 let hitbox = window.insert_hitbox(
905 TouchHandle::hit_bounds(edge, caret),
906 HitboxBehavior::BlockMouse,
907 );
908 (edge, hitbox)
909 })
910 .collect();
911 TouchHandleLayout { hitboxes }
912 }
913
914 /// Paints the touch handles at the ends of this participant's selection,
915 /// in `color`, where the participant is in the paint order — so whatever
916 /// is drawn over the text is drawn over its handles too. Call at the end
917 /// of the participant's paint, after [`Self::register`] for the frame.
918 ///
919 /// The handles take the finger's drag from there; Base moves the
920 /// selection with it.
921 pub fn paint_touch_handles(
922 &self,
923 layout: &TouchHandleLayout,
924 color: Hsla,
925 window: &mut Window,
926 cx: &mut App,
927 ) {
928 let Some(state) = WindowSelectionState::existing(window, cx) else {
929 return;
930 };
931 for (edge, caret) in state.read(cx).touch_handles_of(self.entity_id()) {
932 TouchHandle::paint(edge, caret, color, window);
933 }
934 for (edge, hitbox) in &layout.hitboxes {
935 let edge = *edge;
936 state.update(cx, |state, _| state.register_touch_ui(hitbox.bounds));
937 // Touch: the drag is offered on the first touch, before it can
938 // become a tap, a long press or a pan.
939 window.on_mouse_event({
940 let hitbox = hitbox.clone();
941 let state = state.downgrade();
942 move |event: &TouchDragEvent, phase, window, cx| {
943 if !phase.bubble()
944 || event.phase != TouchPhase::Started
945 || window.default_prevented()
946 || !hitbox.is_hovered(window)
947 {
948 return;
949 }
950 let Some(state) = state.upgrade() else {
951 return;
952 };
953 window.prevent_default();
954 cx.stop_propagation();
955 state.update(cx, |state, cx| {
956 state.begin_edge_drag(edge, event.position, window, cx)
957 });
958 WindowSelectionState::resolve_content_keys(&state, cx);
959 }
960 });
961 // Mouse: the same drag for a pointer.
962 window.on_mouse_event({
963 let hitbox = hitbox.clone();
964 let state = state.downgrade();
965 move |event: &MouseDownEvent, phase, window, cx| {
966 if !phase.bubble()
967 || event.button != MouseButton::Left
968 || !hitbox.is_hovered(window)
969 {
970 return;
971 }
972 let Some(state) = state.upgrade() else {
973 return;
974 };
975 cx.stop_propagation();
976 state.update(cx, |state, cx| {
977 state.begin_edge_drag(edge, event.position, window, cx)
978 });
979 WindowSelectionState::resolve_content_keys(&state, cx);
980 }
981 });
982 }
983 }
984
985 /// Sets a participant-specific lookup for stable virtualized content keys.
986 pub fn resolve_content_key_with(
987 &self,
988 callback: impl Fn(Point<Pixels>, &App) -> Option<TextSelectionContentKey> + 'static,
989 cx: &mut App,
990 ) {
991 self.0
992 .update(cx, |state, _| state.resolve_content_key_with(callback));
993 }
994
995 fn downgrade(&self) -> WeakEntity<SelectableTextState> {
996 self.0.downgrade()
997 }
998}
999
1000#[derive(Clone)]
1001struct ParticipantRegistration {
1002 participant: WeakEntity<SelectableTextState>,
1003 registration: Rc<TextSelectionRegistration>,
1004 generation: u64,
1005}
1006
1007#[derive(Clone)]
1008struct SelectionEndpoint {
1009 participant: Option<WeakEntity<SelectableTextState>>,
1010 point: Point<Pixels>,
1011 inside: bool,
1012 inside_text: bool,
1013 content_key: Option<TextSelectionContentKey>,
1014 content_key_resolver: Option<(ContentKeyResolver, Point<Pixels>)>,
1015}
1016
1017impl SelectionEndpoint {
1018 fn snapshot(&self) -> TextSelectionEndpoint {
1019 let snapshot = TextSelectionEndpoint::new(self.entity_id(), self.point);
1020 if let Some(content_key) = self.content_key {
1021 snapshot.with_content_key(content_key)
1022 } else {
1023 snapshot
1024 }
1025 }
1026
1027 fn resolve(
1028 &self,
1029 participants: &HashMap<EntityId, ParticipantRegistration>,
1030 ) -> Option<Point<Pixels>> {
1031 let participant = self.participant.as_ref()?;
1032 let registration = participants.get(&participant.entity_id())?;
1033 participant.upgrade()?;
1034 Some(
1035 self.point
1036 + registration.registration.scroll_offset
1037 + registration.registration.bounds.origin,
1038 )
1039 }
1040
1041 fn entity_id(&self) -> Option<EntityId> {
1042 self.participant
1043 .as_ref()
1044 .map(|participant| participant.entity_id())
1045 }
1046}
1047
1048/// What a long press left behind: the handles and the edit menu.
1049///
1050/// The presentation layer draws both and reports their bounds each frame, so
1051/// that a press on a handle or a menu item is not taken for a press on the
1052/// text underneath, which would clear the very selection they belong to.
1053#[derive(Default)]
1054struct TouchSelection {
1055 /// The current selection was made by touch and carries handles.
1056 active: bool,
1057 menu_open: bool,
1058 drag: Option<EdgeDrag>,
1059 /// Where the handles and the menu were painted this frame, and the frame
1060 /// before: a press arrives between frames, and the surfaces may have
1061 /// moved in the one that has not painted yet.
1062 ui_bounds: Vec<Bounds<Pixels>>,
1063 previous_ui_bounds: Vec<Bounds<Pixels>>,
1064 /// The ends as last laid out: `(start, end, start_visible, end_visible)`.
1065 /// A frame in which no participant paints a selection — the cursor sits
1066 /// between two characters mid-drag — would otherwise lose the handle the
1067 /// finger holds, and with it the rest of the drag.
1068 last_edges: std::cell::Cell<Option<(Bounds<Pixels>, Bounds<Pixels>, bool, bool)>>,
1069}
1070
1071impl TouchSelection {
1072 fn covers(&self, position: Point<Pixels>) -> bool {
1073 self.active
1074 && self
1075 .ui_bounds
1076 .iter()
1077 .chain(&self.previous_ui_bounds)
1078 .any(|bounds| bounds.contains(&position))
1079 }
1080
1081 /// A new frame begins: what was painted last frame is kept one frame more.
1082 fn begin_frame(&mut self) {
1083 self.previous_ui_bounds = std::mem::take(&mut self.ui_bounds);
1084 }
1085
1086 /// Drops the handles and the menu; returns whether there were any.
1087 fn reset(&mut self) -> bool {
1088 let had = self.active;
1089 self.active = false;
1090 self.menu_open = false;
1091 self.drag = None;
1092 self.last_edges.set(None);
1093 had
1094 }
1095}
1096
1097/// Window-local generic text-selection state.
1098#[derive(Default)]
1099struct WindowSelectionState {
1100 participants: HashMap<EntityId, ParticipantRegistration>,
1101 active_scope: TextSelectionScopeId,
1102 anchor: Option<SelectionEndpoint>,
1103 cursor: Option<SelectionEndpoint>,
1104 pending_extension_anchor: Option<SelectionEndpoint>,
1105 is_selecting: bool,
1106 did_hit_text: bool,
1107 frame_generation: u64,
1108 finish_frame_scheduled: bool,
1109 refresh_held_cursor: bool,
1110 mouse_down_prepared: bool,
1111 auto_scroll: AutoScroll,
1112 /// Where the anchor's text was when the last synthetic wheel went out,
1113 /// and how many went out without moving it. A container at its end
1114 /// cannot scroll further; pushing it on would only make one that
1115 /// bounces stretch and snap back on every tick.
1116 auto_scroll_stall: (Option<(Bounds<Pixels>, Point<Pixels>)>, u8),
1117 touch: TouchSelection,
1118 /// This entity, so that touch changes can notify observers from paths that
1119 /// only hold an [`App`].
1120 entity_id: Option<EntityId>,
1121}
1122
1123impl WindowSelectionState {
1124 fn resolve_content_keys(state: &Entity<Self>, cx: &mut App) {
1125 let pending = state.update(cx, |state, _| {
1126 [
1127 state
1128 .anchor
1129 .as_ref()
1130 .and_then(|endpoint| endpoint.content_key_resolver.clone()),
1131 state
1132 .cursor
1133 .as_ref()
1134 .and_then(|endpoint| endpoint.content_key_resolver.clone()),
1135 ]
1136 });
1137 let resolved =
1138 pending.map(|pending| pending.and_then(|(callback, point)| callback(point, cx)));
1139 state.update(cx, |state, cx| {
1140 if let (Some(endpoint), Some(key)) = (state.anchor.as_mut(), resolved[0]) {
1141 endpoint.content_key = Some(key);
1142 endpoint.content_key_resolver = None;
1143 }
1144 if let (Some(endpoint), Some(key)) = (state.cursor.as_mut(), resolved[1]) {
1145 endpoint.content_key = Some(key);
1146 endpoint.content_key_resolver = None;
1147 }
1148 state.publish_snapshots(cx);
1149 });
1150 }
1151 fn acquire(window_id: gpui::WindowId, cx: &mut App) -> Entity<Self> {
1152 if !cx.has_global::<SelectionStateRegistry>() {
1153 cx.set_global(SelectionStateRegistry::default());
1154 }
1155 if let Some(state) = cx
1156 .global::<SelectionStateRegistry>()
1157 .0
1158 .get(&window_id)
1159 .and_then(WeakEntity::upgrade)
1160 {
1161 return state;
1162 }
1163
1164 let active_scope = if cx.has_global::<PendingTextSelectionScopes>() {
1165 cx.global_mut::<PendingTextSelectionScopes>()
1166 .0
1167 .remove(&window_id)
1168 .unwrap_or_default()
1169 } else {
1170 TextSelectionScopeId::default()
1171 };
1172
1173 let state = cx.new(move |cx| {
1174 let entity_id = cx.entity_id();
1175 cx.on_release(move |state: &mut WindowSelectionState, cx| {
1176 let handlers = state.clear_state(cx);
1177 if cx.has_global::<SelectionStateRegistry>() {
1178 let registry = &mut cx.global_mut::<SelectionStateRegistry>().0;
1179 if registry
1180 .get(&window_id)
1181 .is_some_and(|state| state.entity_id() == entity_id)
1182 {
1183 registry.remove(&window_id);
1184 }
1185 }
1186 if !handlers.is_empty() {
1187 cx.defer(move |cx| dispatch_clear_handlers(handlers, cx));
1188 }
1189 })
1190 .detach();
1191 Self {
1192 active_scope,
1193 entity_id: Some(entity_id),
1194 ..Self::default()
1195 }
1196 });
1197 cx.global_mut::<SelectionStateRegistry>()
1198 .0
1199 .insert(window_id, state.downgrade());
1200 state
1201 }
1202
1203 #[cfg(test)]
1204 fn ensure(window: &Window, cx: &mut App) -> Entity<Self> {
1205 Self::acquire(window.window_handle().window_id(), cx)
1206 }
1207
1208 fn existing(window: &Window, cx: &App) -> Option<Entity<Self>> {
1209 if !cx.has_global::<SelectionStateRegistry>() {
1210 return None;
1211 }
1212 cx.global::<SelectionStateRegistry>()
1213 .0
1214 .get(&window.window_handle().window_id())
1215 .and_then(WeakEntity::upgrade)
1216 }
1217
1218 /// Updates the active scope. Participants from other scopes cannot participate.
1219 #[cfg(test)]
1220 fn set_active_scope(&mut self, scope: TextSelectionScopeId, cx: &mut App) {
1221 let handlers = self.set_active_scope_state(scope, cx);
1222 dispatch_clear_handlers(handlers, cx);
1223 }
1224
1225 fn set_active_scope_state(
1226 &mut self,
1227 scope: TextSelectionScopeId,
1228 cx: &mut App,
1229 ) -> Vec<ClearHandler> {
1230 if self.active_scope == scope {
1231 return Vec::new();
1232 }
1233 let handlers = self.clear_state(cx);
1234 self.active_scope = scope;
1235 self.publish_snapshots(cx);
1236 handlers
1237 }
1238
1239 /// Sweeps participants after a rendered frame has completed.
1240 ///
1241 /// Registrations are stamped with the current generation while any sibling
1242 /// is painting. Sweeping only after paint makes registration independent of
1243 /// whether a participant or the lifecycle element paints first.
1244 ///
1245 /// A missed generation alone does not mean the participant left the window:
1246 /// a cached view replays its recorded frame, painting the same text at the
1247 /// same place without running any of its elements. Such a participant keeps
1248 /// the registration it last reported, which still describes what is on
1249 /// screen; only one whose element GPUI has dropped is swept.
1250 pub fn finish_frame(&mut self, cx: &mut App) -> Vec<ClearHandler> {
1251 self.finish_frame_scheduled = false;
1252 let stale = self
1253 .participants
1254 .iter()
1255 .filter_map(|(id, registration)| {
1256 (registration.generation != self.frame_generation
1257 && !registration.registration.is_rendered())
1258 .then(|| (*id, registration.participant.clone()))
1259 })
1260 .collect::<Vec<_>>();
1261 let mut handlers = Vec::new();
1262 for (id, participant) in stale {
1263 self.participants.remove(&id);
1264 if let Some(participant) = participant.upgrade() {
1265 if let Some(handler) = participant.update(cx, |state, cx| state.clear_state(cx)) {
1266 handlers.push(handler);
1267 }
1268 }
1269 }
1270 self.publish_snapshots(cx);
1271 self.frame_generation = self.frame_generation.wrapping_add(1);
1272 handlers
1273 }
1274
1275 fn schedule_finish_frame(&mut self) -> bool {
1276 if self.finish_frame_scheduled {
1277 return false;
1278 }
1279 self.finish_frame_scheduled = true;
1280 true
1281 }
1282
1283 /// Registers this frame's geometry for a participant.
1284 pub fn register_participant(
1285 &mut self,
1286 selection: TextSelectionHandle,
1287 registration: TextSelectionRegistration,
1288 cx: &mut App,
1289 ) {
1290 self.prune_dead_participants();
1291 if self.is_selecting
1292 && registration.self_scroll
1293 && self.anchor.as_ref().and_then(SelectionEndpoint::entity_id)
1294 == Some(selection.entity_id())
1295 && self
1296 .participants
1297 .get(&selection.entity_id())
1298 .is_some_and(|previous| {
1299 previous.registration.scroll_offset != registration.scroll_offset
1300 || previous.registration.bounds != registration.bounds
1301 })
1302 {
1303 self.refresh_held_cursor = true;
1304 }
1305 // The handles sit on the painted ends; when those moved — a scroll, a
1306 // reflow, a select-all — whoever draws the handles needs to know.
1307 let edges_moved = self.touch.active
1308 && self
1309 .participants
1310 .get(&selection.entity_id())
1311 .is_none_or(|previous| {
1312 previous.registration.selection_edges != registration.selection_edges
1313 });
1314 self.participants.insert(
1315 selection.entity_id(),
1316 ParticipantRegistration {
1317 participant: selection.downgrade(),
1318 registration: Rc::new(registration),
1319 generation: self.frame_generation,
1320 },
1321 );
1322 self.publish_snapshots(cx);
1323 if edges_moved {
1324 self.touch_changed(cx);
1325 }
1326 }
1327
1328 /// Starts a selection gesture using bounds hit testing (useful to adapters/tests).
1329 #[cfg(test)]
1330 fn begin(&mut self, position: Point<Pixels>, extend: bool, cx: &mut App) {
1331 self.begin_impl(position, extend, false, None, cx);
1332 }
1333
1334 /// Updates the current gesture using bounds hit testing.
1335 #[cfg(test)]
1336 fn update(&mut self, position: Point<Pixels>, cx: &mut App) {
1337 self.update_impl(position, None, cx);
1338 }
1339
1340 /// Ends the current gesture and keeps its selection visible.
1341 pub fn end(&mut self, cx: &mut App) {
1342 self.pending_extension_anchor = None;
1343 if !self.is_selecting {
1344 return;
1345 }
1346 self.is_selecting = false;
1347 if !self.did_hit_text {
1348 self.anchor = None;
1349 self.cursor = None;
1350 }
1351 self.stop_anchor_auto_scroll(cx);
1352 self.publish_snapshots(cx);
1353 }
1354
1355 /// Clears both window selection and every participant's local selection.
1356 pub fn clear(&mut self, cx: &mut App) {
1357 let handlers = self.clear_state(cx);
1358 dispatch_clear_handlers(handlers, cx);
1359 }
1360
1361 fn clear_state(&mut self, cx: &mut App) -> Vec<ClearHandler> {
1362 self.stop_anchor_auto_scroll(cx);
1363 self.anchor = None;
1364 self.cursor = None;
1365 self.pending_extension_anchor = None;
1366 self.is_selecting = false;
1367 self.did_hit_text = false;
1368 if self.touch.reset() {
1369 self.touch_changed(cx);
1370 }
1371 self.prune_dead_participants();
1372 self.participants
1373 .values()
1374 .filter_map(|registration| registration.participant.upgrade())
1375 .filter_map(|participant| participant.update(cx, |state, cx| state.clear_state(cx)))
1376 .collect()
1377 }
1378
1379 /// Tells whoever draws the handles and the menu that they changed.
1380 ///
1381 /// Deferred, because the change may come from a participant painting its
1382 /// selection ends: a notification raised inside a draw marks the view
1383 /// dirty but starts no frame, and the handles would sit where they were
1384 /// until something else redrew the window.
1385 ///
1386 /// The participants in the selection hear it too: they paint the
1387 /// handles, and lay the handles' hitboxes out from the ends they painted
1388 /// last frame, so they render once more. A participant under a cached
1389 /// view would otherwise not, and the next frame would replay this one,
1390 /// with no hitbox where a handle is.
1391 fn touch_changed(&self, cx: &mut App) {
1392 let participants = self
1393 .participants
1394 .values()
1395 .filter_map(|registration| registration.participant.upgrade())
1396 .filter(|participant| participant.read(cx).snapshot.is_some())
1397 .collect::<Vec<_>>();
1398 let entity_id = self.entity_id;
1399 cx.defer(move |cx| {
1400 for participant in participants {
1401 participant.update(cx, |_, cx| {
1402 cx.emit(TextSelectionEvent::TouchSelectionChanged)
1403 });
1404 }
1405 if let Some(entity_id) = entity_id {
1406 cx.notify(entity_id);
1407 }
1408 });
1409 }
1410
1411 fn copy_items(&self, cx: &App) -> Vec<CopyItem> {
1412 self.participants
1413 .values()
1414 .filter_map(|registration| {
1415 let participant = registration.participant.upgrade()?;
1416 participant
1417 .read(cx)
1418 .copy_item(registration.registration.document_order)
1419 })
1420 .collect()
1421 }
1422
1423 #[cfg(test)]
1424 fn selected_text(&self, cx: &mut App) -> String {
1425 resolve_copy_items(self.copy_items(cx), cx)
1426 }
1427
1428 /// Whether the current endpoints take in at least one character of some
1429 /// participant's painted text, as opposed to two points with nothing
1430 /// between them.
1431 fn selects_text(&self, cx: &App) -> bool {
1432 self.snapshot().is_some()
1433 && self.participants.values().any(|registration| {
1434 registration
1435 .participant
1436 .upgrade()
1437 .is_some_and(|participant| {
1438 let participant = participant.read(cx);
1439 project_ranges(participant.snapshot, &participant.runs)
1440 .ranges()
1441 .iter()
1442 .any(|range| range.as_ref().is_some_and(|range| !range.is_empty()))
1443 })
1444 })
1445 }
1446
1447 /// Returns whether a drag or a participant-local selection is active.
1448 pub fn has_selection(&self, cx: &App) -> bool {
1449 self.snapshot().is_some()
1450 || self.participants.values().any(|registration| {
1451 registration
1452 .participant
1453 .upgrade()
1454 .is_some_and(|participant| participant.read(cx).local_selection)
1455 })
1456 }
1457
1458 /// Returns the current resolved selection endpoints.
1459 pub fn snapshot(&self) -> Option<TextSelectionSnapshot> {
1460 if !self.did_hit_text {
1461 return None;
1462 }
1463 let anchor_endpoint = self.anchor.as_ref()?;
1464 let cursor_endpoint = self.cursor.as_ref()?;
1465 let anchor = anchor_endpoint.resolve(&self.participants)?;
1466 let cursor = cursor_endpoint.resolve(&self.participants)?;
1467 (anchor != cursor).then(|| {
1468 TextSelectionSnapshot::new(anchor_endpoint.snapshot(), cursor_endpoint.snapshot())
1469 .with_selecting(self.is_selecting)
1470 .with_window_points(Some(TextSelectionWindowPoints { anchor, cursor }))
1471 })
1472 }
1473
1474 /// Returns whether a drag is currently in progress.
1475 #[cfg(test)]
1476 fn is_selecting(&self) -> bool {
1477 self.is_selecting
1478 }
1479
1480 fn prepare_for_mouse_down(&mut self, extend: bool, cx: &mut App) -> Vec<ClearHandler> {
1481 let pending_extension_anchor = extend.then(|| self.anchor.clone()).flatten();
1482 self.stop_anchor_auto_scroll(cx);
1483 self.anchor = None;
1484 self.cursor = None;
1485 self.pending_extension_anchor = None;
1486 self.is_selecting = false;
1487 self.did_hit_text = false;
1488 if self.touch.reset() {
1489 self.touch_changed(cx);
1490 }
1491 self.prune_dead_participants();
1492 let handlers = self
1493 .participants
1494 .values()
1495 .filter_map(|registration| registration.participant.upgrade())
1496 .filter_map(|participant| participant.update(cx, |state, cx| state.clear_state(cx)))
1497 .collect();
1498 self.pending_extension_anchor = pending_extension_anchor;
1499 handlers
1500 }
1501
1502 /// The touch selection laid out for its handles and edit menu.
1503 ///
1504 /// The ends come from the participants: the first selected character of
1505 /// the earliest participant in document order and the last of the latest.
1506 /// A participant whose selection is entirely scrolled away paints no ends,
1507 /// so the handles disappear with the text they mark.
1508 fn touch_selection(&self) -> Option<TouchSelectionSnapshot> {
1509 if !self.touch.active {
1510 return None;
1511 }
1512 // Each end with its owner's viewport: an end scrolled out of its
1513 // participant gets no handle.
1514 let mut start: Option<(u64, Bounds<Pixels>, bool)> = None;
1515 let mut end: Option<(u64, Bounds<Pixels>, bool)> = None;
1516 for registration in self.participants.values() {
1517 let geometry = &registration.registration;
1518 if geometry.scope != self.active_scope || registration.participant.upgrade().is_none() {
1519 continue;
1520 }
1521 let Some((edge_start, edge_end)) = geometry.selection_edges else {
1522 continue;
1523 };
1524 let order = geometry.document_order;
1525 let viewport = geometry.hitbox.bounds;
1526 if start.is_none_or(|(best, ..)| order < best) {
1527 start = Some((order, edge_start, caret_in_view(edge_start, viewport)));
1528 }
1529 if end.is_none_or(|(best, ..)| order >= best) {
1530 end = Some((order, edge_end, caret_in_view(edge_end, viewport)));
1531 }
1532 }
1533 let edges = match (start, end) {
1534 (Some((_, start, start_visible)), Some((_, end, end_visible))) => {
1535 let edges = (start, end, start_visible, end_visible);
1536 self.touch.last_edges.set(Some(edges));
1537 edges
1538 }
1539 _ if self.touch.drag.is_some() => self.touch.last_edges.get()?,
1540 _ => return None,
1541 };
1542 let (start, end, start_visible, end_visible) = edges;
1543 Some(
1544 TouchSelectionSnapshot::new(start, end)
1545 .with_edge_visible(SelectionEdge::Start, start_visible)
1546 .with_edge_visible(SelectionEdge::End, end_visible)
1547 .with_menu_open(self.touch.menu_open)
1548 .with_dragging(self.touch.drag.map(|drag| drag.edge())),
1549 )
1550 }
1551
1552 /// The handles `participant` paints: the start when no participant
1553 /// earlier in document order has a selection end, the end when none
1554 /// later has. Participants paint in document order, so a later one that
1555 /// takes the end over has registered its ends by the time it asks.
1556 fn touch_handles_of(&self, participant: EntityId) -> Vec<(SelectionEdge, Bounds<Pixels>)> {
1557 if !self.touch.active {
1558 return Vec::new();
1559 }
1560 let Some(own) = self.participants.get(&participant) else {
1561 return Vec::new();
1562 };
1563 let Some((start, end)) = own.registration.selection_edges else {
1564 return Vec::new();
1565 };
1566 if start == end {
1567 return Vec::new();
1568 }
1569 let order = own.registration.document_order;
1570 let others = self.participants.iter().filter(|(id, registration)| {
1571 **id != participant
1572 && registration.registration.scope == self.active_scope
1573 && registration.registration.selection_edges.is_some()
1574 && registration.participant.upgrade().is_some()
1575 });
1576 let (mut earlier, mut later) = (false, false);
1577 for (_, registration) in others {
1578 let other = registration.registration.document_order;
1579 earlier |= other < order;
1580 later |= other > order;
1581 }
1582 let viewport = own.registration.hitbox.bounds;
1583 let mut handles = Vec::with_capacity(2);
1584 if !earlier && caret_in_view(start, viewport) {
1585 handles.push((SelectionEdge::Start, start));
1586 }
1587 if !later && caret_in_view(end, viewport) {
1588 handles.push((SelectionEdge::End, end));
1589 }
1590 handles
1591 }
1592
1593 /// Keeps the selection a long press made, and opens the edit menu over it.
1594 fn keep_touch_selection(&mut self, cx: &mut App) {
1595 self.touch.active = self.snapshot().is_some();
1596 self.touch.menu_open = self.touch.active;
1597 self.touch.drag = None;
1598 self.touch_changed(cx);
1599 }
1600
1601 /// Records where the handles and the menu are painted this frame.
1602 fn register_touch_ui(&mut self, bounds: Bounds<Pixels>) {
1603 self.touch.ui_bounds.push(bounds);
1604 }
1605
1606 fn close_edit_menu(&mut self, cx: &mut App) {
1607 if !self.touch.menu_open {
1608 return;
1609 }
1610 self.touch.menu_open = false;
1611 self.touch_changed(cx);
1612 }
1613
1614 /// The handles follow the text, but the menu would sit over whatever
1615 /// scrolls underneath it: it steps aside while a finger scrolls and comes
1616 /// back over the handles once the finger lifts.
1617 fn edit_menu_on_scroll(&mut self, phase: TouchPhase, cx: &mut App) {
1618 if !self.touch.active || self.touch.drag.is_some() {
1619 return;
1620 }
1621 match phase {
1622 TouchPhase::Ended | TouchPhase::Cancelled => {
1623 if !self.touch.menu_open {
1624 self.touch.menu_open = true;
1625 self.touch_changed(cx);
1626 }
1627 }
1628 _ => self.close_edit_menu(cx),
1629 }
1630 }
1631
1632 /// Selects all of the participant the touch selection started in.
1633 ///
1634 /// This stays a point selection — anchored on the participant's first
1635 /// line of text, ending on its last — rather than switching the
1636 /// participant to a local select-all: one selection, painted, reported
1637 /// and copied the one way, with handles that drag on from its ends.
1638 fn select_all_touched(&mut self, cx: &mut App) {
1639 if !self.touch.active {
1640 return;
1641 }
1642 let Some((participant, registration)) = self.anchor_registration() else {
1643 return;
1644 };
1645 // From the participant's text runs, not its painted line bounds:
1646 // those are clipped to the viewport, and a message taller than the
1647 // screen would only select what is on it.
1648 let (anchor, cursor) = {
1649 let runs = &participant.read(cx).runs;
1650 let (Some(first), Some(last)) = (
1651 runs.iter().min_by_key(|run| run.document_order),
1652 runs.iter().max_by_key(|run| run.document_order),
1653 ) else {
1654 return;
1655 };
1656 let (Some(start), Some(end)) = (
1657 first.layout.position_for_index(0),
1658 last.layout.position_for_index(last.text.len()),
1659 ) else {
1660 return;
1661 };
1662 // Just inside the first and the last glyph, mid-line, so both
1663 // land on text.
1664 let inset = px(1.);
1665 (
1666 point(start.x + inset, start.y + first.layout.line_height() / 2.),
1667 point(end.x - inset, end.y + last.layout.line_height() / 2.),
1668 )
1669 };
1670 let content_key_resolver = participant.read(cx).content_key_resolver.clone();
1671 let to_endpoint = |window_point: Point<Pixels>| {
1672 let content_point =
1673 window_point - registration.bounds.origin - registration.scroll_offset;
1674 SelectionEndpoint {
1675 participant: Some(participant.downgrade()),
1676 point: content_point,
1677 inside: true,
1678 inside_text: true,
1679 content_key: None,
1680 content_key_resolver: content_key_resolver
1681 .clone()
1682 .map(|resolver| (resolver, content_point)),
1683 }
1684 };
1685 self.anchor = Some(to_endpoint(anchor));
1686 self.cursor = Some(to_endpoint(cursor));
1687 self.did_hit_text = true;
1688 self.is_selecting = false;
1689 self.touch.menu_open = true;
1690 self.publish_snapshots(cx);
1691 self.touch_changed(cx);
1692 }
1693
1694 /// Starts dragging one end of the touch selection from `finger`.
1695 ///
1696 /// The selection is rebuilt from the painted ends, anchored at the end that
1697 /// stays: a select-all or a word selection becomes an ordinary point
1698 /// selection which the drag then extends.
1699 fn begin_edge_drag(
1700 &mut self,
1701 edge: SelectionEdge,
1702 finger: Point<Pixels>,
1703 window: &mut Window,
1704 cx: &mut App,
1705 ) {
1706 let Some(snapshot) = self.touch_selection() else {
1707 return;
1708 };
1709 let handlers = self.prepare_for_mouse_down(false, cx);
1710 dispatch_clear_handlers(handlers, cx);
1711 let held = snapshot.edge(edge.opposite());
1712 // Just inside the held end, so the anchor lands on the character it
1713 // marks rather than on the boundary between two participants.
1714 let nudge = px(1.);
1715 let anchor_point = match edge {
1716 SelectionEdge::Start => point(held.left() - nudge, held.center().y),
1717 SelectionEdge::End => point(held.left() + nudge, held.center().y),
1718 };
1719 let anchor = self.endpoint(anchor_point, None, cx);
1720 let drag = EdgeDrag::begin(edge, snapshot.edge(edge), finger);
1721 let cursor = self.endpoint(drag.text_position(finger), Some(window), cx);
1722 self.did_hit_text = anchor.inside_text || cursor.inside_text;
1723 self.anchor = Some(anchor);
1724 self.cursor = Some(cursor);
1725 self.is_selecting = true;
1726 self.touch.active = true;
1727 self.touch.menu_open = false;
1728 self.touch.drag = Some(drag);
1729 self.publish_snapshots(cx);
1730 self.touch_changed(cx);
1731 }
1732
1733 fn update_edge_drag(&mut self, finger: Point<Pixels>, window: &Window, cx: &mut Context<Self>) {
1734 let Some(drag) = self.touch.drag else {
1735 return;
1736 };
1737 let before = self.cursor.clone();
1738 self.update_in_window(drag.text_position(finger), window, cx);
1739 // A handle never collapses the selection: at the other end it stops,
1740 // and the finger has to pass that end, onto text, to swap the two.
1741 if !self.selects_text(cx) {
1742 self.cursor = before;
1743 self.publish_snapshots(cx);
1744 return;
1745 }
1746 // Dragging one end past the other swaps them: the cursor now lies
1747 // before the anchor, so the finger holds what became the start.
1748 if let Some(points) = self
1749 .snapshot()
1750 .and_then(|snapshot| snapshot.window_points())
1751 {
1752 let (anchor, cursor) = (points.anchor(), points.cursor());
1753 let cursor_first = cursor.y < anchor.y || (cursor.y == anchor.y && cursor.x < anchor.x);
1754 let edge = if cursor_first {
1755 SelectionEdge::Start
1756 } else {
1757 SelectionEdge::End
1758 };
1759 if let Some(drag) = self.touch.drag.as_mut() {
1760 drag.set_edge(edge);
1761 }
1762 }
1763 self.touch_changed(cx);
1764 }
1765
1766 fn end_edge_drag(&mut self, cx: &mut App) {
1767 if self.touch.drag.take().is_none() {
1768 return;
1769 }
1770 self.end(cx);
1771 self.keep_touch_selection(cx);
1772 }
1773
1774 fn begin_in_window(
1775 &mut self,
1776 position: Point<Pixels>,
1777 extend: bool,
1778 window: &mut Window,
1779 cx: &mut App,
1780 ) {
1781 self.begin_impl(position, extend, true, Some(window), cx);
1782 }
1783
1784 fn update_in_window(
1785 &mut self,
1786 position: Point<Pixels>,
1787 window: &Window,
1788 cx: &mut Context<Self>,
1789 ) {
1790 if !cx.has_active_drag() {
1791 self.update_impl(position, Some(window), cx);
1792 self.update_auto_scroll(position, window, cx);
1793 }
1794 }
1795
1796 fn select_at(
1797 &mut self,
1798 position: Point<Pixels>,
1799 click_count: usize,
1800 window: &mut Window,
1801 cx: &mut App,
1802 ) {
1803 GlobalState::init(cx);
1804 if GlobalState::is_text_selection_suppressed(cx) {
1805 return;
1806 }
1807 let hit = self.endpoint(position, Some(window), cx);
1808 if !hit.inside_text {
1809 return;
1810 }
1811 let Some(participant) = hit
1812 .participant
1813 .and_then(|participant| participant.upgrade())
1814 else {
1815 return;
1816 };
1817 let points = points_for_multi_click(&participant.read(cx).runs, position, click_count);
1818 let Some((anchor, cursor)) = points else {
1819 return;
1820 };
1821 let Some(registration) = self.participants.get(&participant.entity_id()) else {
1822 return;
1823 };
1824 let content_key_resolver = participant.read(cx).content_key_resolver.clone();
1825 let to_endpoint = |point: Point<Pixels>| {
1826 let content_point = point
1827 - registration.registration.bounds.origin
1828 - registration.registration.scroll_offset;
1829 SelectionEndpoint {
1830 participant: Some(participant.downgrade()),
1831 point: content_point,
1832 inside: true,
1833 inside_text: true,
1834 content_key: None,
1835 content_key_resolver: content_key_resolver
1836 .clone()
1837 .map(|resolver| (resolver, content_point)),
1838 }
1839 };
1840 self.anchor = Some(to_endpoint(anchor));
1841 self.cursor = Some(to_endpoint(cursor));
1842 self.did_hit_text = true;
1843 self.is_selecting = false;
1844 participant.update(cx, |state, cx| state.focus(window, cx));
1845 self.publish_snapshots(cx);
1846 }
1847
1848 #[cfg(test)]
1849 fn update_in_window_with_active_drag(
1850 &mut self,
1851 position: Point<Pixels>,
1852 active_drag: bool,
1853 window: &Window,
1854 cx: &mut App,
1855 ) {
1856 if !active_drag {
1857 self.update_impl(position, Some(window), cx);
1858 }
1859 }
1860
1861 fn begin_impl(
1862 &mut self,
1863 position: Point<Pixels>,
1864 extend: bool,
1865 already_prepared: bool,
1866 window: Option<&mut Window>,
1867 cx: &mut App,
1868 ) {
1869 GlobalState::init(cx);
1870 if GlobalState::is_text_selection_suppressed(cx) {
1871 self.pending_extension_anchor = None;
1872 return;
1873 }
1874 let previous_anchor = extend
1875 .then(|| {
1876 self.pending_extension_anchor
1877 .take()
1878 .or_else(|| self.anchor.clone())
1879 })
1880 .flatten()
1881 .filter(|anchor| anchor.resolve(&self.participants).is_some());
1882 if !extend && !already_prepared {
1883 self.clear(cx);
1884 }
1885 let endpoint = self.endpoint(position, window.as_deref(), cx);
1886 let focus_participant = endpoint
1887 .inside
1888 .then(|| endpoint.participant.clone())
1889 .flatten();
1890 let anchor = previous_anchor.unwrap_or_else(|| endpoint.clone());
1891 self.anchor = Some(anchor.clone());
1892 self.cursor = Some(endpoint.clone());
1893 self.did_hit_text = anchor.inside_text || endpoint.inside_text;
1894 self.is_selecting = true;
1895 if let Some(participant) = focus_participant.and_then(|participant| participant.upgrade()) {
1896 if let Some(window) = window {
1897 participant.update(cx, |state, cx| state.focus(window, cx));
1898 }
1899 }
1900 self.publish_snapshots(cx);
1901 }
1902
1903 fn update_impl(&mut self, position: Point<Pixels>, window: Option<&Window>, cx: &mut App) {
1904 if !self.is_selecting {
1905 return;
1906 }
1907 let endpoint = self.endpoint(position, window, cx);
1908 self.did_hit_text |= endpoint.inside_text;
1909 self.cursor = Some(endpoint);
1910 if window.is_none() {
1911 self.update_participant_auto_scroll(position, cx);
1912 }
1913 self.publish_snapshots(cx);
1914 }
1915
1916 fn endpoint(
1917 &mut self,
1918 position: Point<Pixels>,
1919 window: Option<&Window>,
1920 cx: &App,
1921 ) -> SelectionEndpoint {
1922 self.prune_dead_participants();
1923 let mut hit: Option<(
1924 WeakEntity<SelectableTextState>,
1925 Rc<TextSelectionRegistration>,
1926 f32,
1927 )> = None;
1928 let mut predecessor: Option<(
1929 WeakEntity<SelectableTextState>,
1930 Rc<TextSelectionRegistration>,
1931 )> = None;
1932 let mut first: Option<(
1933 WeakEntity<SelectableTextState>,
1934 Rc<TextSelectionRegistration>,
1935 )> = None;
1936
1937 for registration in self.participants.values() {
1938 if registration.registration.scope != self.active_scope
1939 || registration.participant.upgrade().is_none()
1940 {
1941 continue;
1942 }
1943 let participant_geometry = &registration.registration;
1944 let hovered = window.map_or_else(
1945 || participant_geometry.bounds.contains(&position),
1946 |window| participant_geometry.hitbox.is_hovered(window),
1947 );
1948 if hovered {
1949 let area = f32::from(participant_geometry.bounds.size.width)
1950 * f32::from(participant_geometry.bounds.size.height);
1951 if hit.as_ref().is_none_or(|(_, best, best_area)| {
1952 area < *best_area
1953 || (area == *best_area
1954 && participant_geometry.document_order < best.document_order)
1955 }) {
1956 hit = Some((
1957 registration.participant.clone(),
1958 participant_geometry.clone(),
1959 area,
1960 ));
1961 }
1962 }
1963 if participant_geometry.bounds.top() <= position.y
1964 && predecessor.as_ref().is_none_or(|(_, best)| {
1965 participant_geometry.bounds.top() > best.bounds.top()
1966 || (participant_geometry.bounds.top() == best.bounds.top()
1967 && participant_geometry.document_order < best.document_order)
1968 })
1969 {
1970 predecessor = Some((
1971 registration.participant.clone(),
1972 participant_geometry.clone(),
1973 ));
1974 }
1975 if first.as_ref().is_none_or(|(_, best)| {
1976 participant_geometry.bounds.top() < best.bounds.top()
1977 || (participant_geometry.bounds.top() == best.bounds.top()
1978 && participant_geometry.document_order < best.document_order)
1979 }) {
1980 first = Some((
1981 registration.participant.clone(),
1982 participant_geometry.clone(),
1983 ));
1984 }
1985 }
1986
1987 let selection = hit
1988 .map(|(participant, registration, _)| (participant, registration, true))
1989 .or_else(|| {
1990 predecessor
1991 .or(first)
1992 .map(|(participant, registration)| (participant, registration, false))
1993 });
1994 match selection {
1995 Some((participant, registration, inside)) => {
1996 let point = position - registration.bounds.origin - registration.scroll_offset;
1997 let content_key_resolver = participant.upgrade().and_then(|participant| {
1998 participant
1999 .read(cx)
2000 .content_key_resolver
2001 .clone()
2002 .map(|callback| (callback, point))
2003 });
2004 SelectionEndpoint {
2005 point,
2006 participant: Some(participant),
2007 inside,
2008 inside_text: inside
2009 && registration
2010 .text_bounds
2011 .iter()
2012 .any(|bounds| bounds.contains(&position)),
2013 content_key: None,
2014 content_key_resolver,
2015 }
2016 }
2017 None => SelectionEndpoint {
2018 participant: None,
2019 point: position,
2020 inside: false,
2021 inside_text: false,
2022 content_key: None,
2023 content_key_resolver: None,
2024 },
2025 }
2026 }
2027
2028 fn publish_snapshots(&mut self, cx: &mut App) {
2029 self.prune_dead_participants();
2030 let snapshot = self.snapshot();
2031 let single_participant = self.single_participant();
2032 for (id, registration) in &self.participants {
2033 let Some(participant) = registration.participant.upgrade() else {
2034 continue;
2035 };
2036 let participant_snapshot = (registration.registration.scope == self.active_scope
2037 && self.participates(*id, registration)
2038 && single_participant.is_none_or(|single| single == *id))
2039 .then_some(snapshot)
2040 .flatten()
2041 .map(|mut snapshot| {
2042 snapshot.coverage = self.coverage_for(*id);
2043 snapshot
2044 });
2045 participant.update(cx, |state, cx| state.set_snapshot(participant_snapshot, cx));
2046 }
2047 }
2048
2049 fn coverage_for(&self, id: EntityId) -> TextSelectionCoverage {
2050 let Some(anchor) = self.anchor.as_ref().and_then(SelectionEndpoint::entity_id) else {
2051 return TextSelectionCoverage::Bounded;
2052 };
2053 let Some(cursor) = self.cursor.as_ref().and_then(SelectionEndpoint::entity_id) else {
2054 return TextSelectionCoverage::Bounded;
2055 };
2056 if anchor == cursor {
2057 return TextSelectionCoverage::Bounded;
2058 }
2059 let anchor_order = self.participants[&anchor].registration.document_order;
2060 let cursor_order = self.participants[&cursor].registration.document_order;
2061 if id != anchor && id != cursor {
2062 TextSelectionCoverage::Full
2063 } else if (id == anchor) == (anchor_order < cursor_order) {
2064 TextSelectionCoverage::ToEnd
2065 } else {
2066 TextSelectionCoverage::FromStart
2067 }
2068 }
2069
2070 fn single_participant(&self) -> Option<EntityId> {
2071 let anchor = self.anchor.as_ref()?.entity_id()?;
2072 let cursor = self.cursor.as_ref()?.entity_id()?;
2073 (anchor == cursor).then_some(anchor)
2074 }
2075
2076 fn participates(&self, id: EntityId, registration: &ParticipantRegistration) -> bool {
2077 let Some(anchor) = self.anchor.as_ref().and_then(SelectionEndpoint::entity_id) else {
2078 return false;
2079 };
2080 let Some(cursor) = self.cursor.as_ref().and_then(SelectionEndpoint::entity_id) else {
2081 return false;
2082 };
2083 let Some(anchor_registration) = self.participants.get(&anchor) else {
2084 return false;
2085 };
2086 let Some(cursor_registration) = self.participants.get(&cursor) else {
2087 return false;
2088 };
2089 let start = anchor_registration
2090 .registration
2091 .document_order
2092 .min(cursor_registration.registration.document_order);
2093 let end = anchor_registration
2094 .registration
2095 .document_order
2096 .max(cursor_registration.registration.document_order);
2097 (start..=end).contains(&registration.registration.document_order)
2098 || id == anchor
2099 || id == cursor
2100 }
2101
2102 fn update_auto_scroll(
2103 &mut self,
2104 position: Point<Pixels>,
2105 window: &Window,
2106 cx: &mut Context<Self>,
2107 ) {
2108 // A finished gesture keeps its anchor for shift-click extension; only
2109 // a live drag may scroll.
2110 if !self.is_selecting {
2111 return;
2112 }
2113 let Some((_, registration)) = self.anchor_registration() else {
2114 return;
2115 };
2116 // Exactly one writer per drag: a participant that scrolls its own
2117 // content is notified directly; anything else gets a synthetic wheel.
2118 if registration.self_scroll {
2119 self.auto_scroll.stop();
2120 self.update_participant_auto_scroll(position, cx);
2121 return;
2122 }
2123 // The content mask is the nearest clipping viewport established by a
2124 // scrollable ancestor. It remains stable as the participant itself
2125 // moves, so selection keeps scrolling the same related region even
2126 // after the anchor text has moved out of view.
2127 let visible_bounds = registration.hitbox.content_mask.bounds;
2128 // Keeps the synthesized wheel event hit-testing inside the mask.
2129 const HIT_TEST_INSET: Pixels = px(1.);
2130 // A collapsed mask leaves an empty clamp range below — stop.
2131 if visible_bounds.size.width < HIT_TEST_INSET * 2.
2132 || visible_bounds.size.height < HIT_TEST_INSET * 2.
2133 {
2134 self.stop_anchor_auto_scroll(cx);
2135 return;
2136 }
2137 let delta = AutoScroll::compute_delta(position.y, visible_bounds);
2138 let event_position = point(
2139 position.x.clamp(
2140 visible_bounds.left() + HIT_TEST_INSET,
2141 visible_bounds.right() - HIT_TEST_INSET,
2142 ),
2143 position.y.clamp(
2144 visible_bounds.top() + HIT_TEST_INSET,
2145 visible_bounds.bottom() - HIT_TEST_INSET,
2146 ),
2147 );
2148 self.auto_scroll.last_drag_position = Some(event_position);
2149 self.auto_scroll_stall = (None, 0);
2150 let window = window.window_handle();
2151 self.auto_scroll.set(delta, cx, move |delta, state, cx| {
2152 let Some(position) = state.auto_scroll.last_drag_position else {
2153 return;
2154 };
2155 // Hold off once the container has shown, over a few ticks, that
2156 // it has nowhere left to go; the next move of the finger asks
2157 // again.
2158 const STALLED_TICKS: u8 = 3;
2159 let geometry = state
2160 .anchor_registration()
2161 .map(|(_, registration)| (registration.bounds, registration.scroll_offset));
2162 let (last, stalled) = &mut state.auto_scroll_stall;
2163 if *last == geometry {
2164 *stalled = stalled.saturating_add(1);
2165 if *stalled >= STALLED_TICKS {
2166 return;
2167 }
2168 } else {
2169 *last = geometry;
2170 *stalled = 0;
2171 }
2172 let window = window;
2173 cx.defer(move |cx| {
2174 _ = window.update(cx, |_, window, cx| {
2175 window.dispatch_event(
2176 ScrollWheelEvent {
2177 position,
2178 delta: ScrollDelta::Pixels(point(px(0.), -delta)),
2179 ..Default::default()
2180 }
2181 .to_platform_input(),
2182 cx,
2183 );
2184 });
2185 });
2186 });
2187 }
2188
2189 /// Drives the anchor participant's own scrolling, measured against the
2190 /// visible portion of the participant's element bounds.
2191 fn update_participant_auto_scroll(&self, position: Point<Pixels>, cx: &mut App) {
2192 let Some((participant, registration)) = self.anchor_registration() else {
2193 return;
2194 };
2195 let visible_bounds = registration
2196 .bounds
2197 .intersect(&registration.hitbox.content_mask.bounds);
2198 let delta = if visible_bounds.size.width > px(0.) && visible_bounds.size.height > px(0.) {
2199 AutoScroll::compute_delta(position.y, visible_bounds)
2200 } else {
2201 None
2202 };
2203 participant.update(cx, |state, cx| state.set_auto_scroll(delta, cx));
2204 }
2205
2206 fn stop_anchor_auto_scroll(&mut self, cx: &mut App) {
2207 self.auto_scroll.stop();
2208 let Some(participant) = self.anchor_participant() else {
2209 return;
2210 };
2211 participant.update(cx, |state, cx| state.set_auto_scroll(None, cx));
2212 }
2213
2214 /// The live participant owning the anchor of the current gesture.
2215 fn anchor_participant(&self) -> Option<Entity<SelectableTextState>> {
2216 self.anchor
2217 .as_ref()
2218 .filter(|anchor| anchor.inside)?
2219 .participant
2220 .as_ref()?
2221 .upgrade()
2222 }
2223
2224 /// The anchor participant together with its current frame registration.
2225 fn anchor_registration(
2226 &self,
2227 ) -> Option<(Entity<SelectableTextState>, Rc<TextSelectionRegistration>)> {
2228 let participant = self.anchor_participant()?;
2229 let registration = self.participants.get(&participant.entity_id())?;
2230 Some((participant, registration.registration.clone()))
2231 }
2232
2233 fn prune_dead_participants(&mut self) {
2234 self.participants
2235 .retain(|_, registration| registration.participant.upgrade().is_some());
2236 }
2237}
2238
2239#[derive(Default)]
2240/// Non-owning window locator; retained [`TextSelection`] element state owns
2241/// each live selection entity.
2242struct SelectionStateRegistry(HashMap<gpui::WindowId, WeakEntity<WindowSelectionState>>);
2243
2244impl Global for SelectionStateRegistry {}
2245
2246#[derive(Default)]
2247struct PendingTextSelectionScopes(HashMap<gpui::WindowId, TextSelectionScopeId>);
2248
2249impl Global for PendingTextSelectionScopes {}
2250
2251#[derive(Default)]
2252struct TextSelectionScopeStacks(HashMap<gpui::WindowId, Vec<TextSelectionScopeId>>);
2253
2254impl Global for TextSelectionScopeStacks {}
2255
2256fn push_text_selection_scope(window_id: gpui::WindowId, scope: TextSelectionScopeId, cx: &mut App) {
2257 if !cx.has_global::<TextSelectionScopeStacks>() {
2258 cx.set_global(TextSelectionScopeStacks::default());
2259 }
2260 cx.global_mut::<TextSelectionScopeStacks>()
2261 .0
2262 .entry(window_id)
2263 .or_default()
2264 .push(scope);
2265}
2266
2267fn pop_text_selection_scope(window_id: gpui::WindowId, cx: &mut App) {
2268 let stacks = &mut cx.global_mut::<TextSelectionScopeStacks>().0;
2269 let remove_stack = stacks.get_mut(&window_id).is_some_and(|stack| {
2270 stack.pop();
2271 stack.is_empty()
2272 });
2273 if remove_stack {
2274 stacks.remove(&window_id);
2275 }
2276}
2277
2278fn current_text_selection_scope(
2279 window_id: gpui::WindowId,
2280 cx: &App,
2281) -> Option<TextSelectionScopeId> {
2282 cx.has_global::<TextSelectionScopeStacks>()
2283 .then(|| {
2284 cx.global::<TextSelectionScopeStacks>()
2285 .0
2286 .get(&window_id)
2287 .and_then(|stack| stack.last().copied())
2288 })
2289 .flatten()
2290}
2291
2292fn with_text_selection_scope<T>(
2293 window_id: gpui::WindowId,
2294 scope: TextSelectionScopeId,
2295 cx: &mut App,
2296 callback: impl FnOnce(&mut App) -> T,
2297) -> T {
2298 push_text_selection_scope(window_id, scope, cx);
2299 let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| callback(cx)));
2300 pop_text_selection_scope(window_id, cx);
2301 match result {
2302 Ok(result) => result,
2303 Err(payload) => std::panic::resume_unwind(payload),
2304 }
2305}
2306
2307/// Window-level operations for text selection.
2308pub struct TextSelection;
2309
2310impl TextSelection {
2311 /// Returns the currently selected text in logical document order.
2312 pub fn selected_text(window: &mut Window, cx: &mut App) -> String {
2313 let Some(state) = live_text_selection_state(window, cx) else {
2314 return String::new();
2315 };
2316 let items = state.read(cx).copy_items(cx);
2317 resolve_copy_items(items, cx)
2318 }
2319
2320 /// Returns whether the window has a geometry selection or any participant
2321 /// has an active participant-local selection such as select-all.
2322 pub fn has_selection(window: &mut Window, cx: &mut App) -> bool {
2323 live_text_selection_state(window, cx).is_some_and(|state| state.read(cx).has_selection(cx))
2324 }
2325
2326 /// Clears window selection and every participant's local selection.
2327 pub fn clear(window: &mut Window, cx: &mut App) {
2328 if let Some(state) = live_text_selection_state(window, cx) {
2329 let handlers = state.update(cx, |state, cx| state.clear_state(cx));
2330 dispatch_clear_handlers(handlers, cx);
2331 }
2332 }
2333
2334 /// Clears selection for a known window identifier.
2335 ///
2336 /// Prefer [`Self::clear`] when a window reference is available. This
2337 /// narrow entry point supports hosts retiring deprecated window wrappers.
2338 pub fn clear_for_window(window_id: gpui::WindowId, cx: &mut App) {
2339 clear_window_text_selection(window_id, cx);
2340 }
2341
2342 /// Ends the current drag while leaving its selection visible.
2343 pub fn end(window: &mut Window, cx: &mut App) {
2344 if let Some(state) = live_text_selection_state(window, cx) {
2345 state.update(cx, |state, cx| state.end(cx));
2346 }
2347 }
2348
2349 /// Calls `callback` whenever the touch selection changes: it appears, its
2350 /// handles move, its menu opens or closes, or it goes away. Whoever draws
2351 /// the handles and the menu re-renders from here.
2352 pub fn observe_touch_selection(
2353 window: &Window,
2354 cx: &mut App,
2355 callback: impl Fn(&mut App) + 'static,
2356 ) -> Subscription {
2357 let state = WindowSelectionState::acquire(window.window_handle().window_id(), cx);
2358 cx.observe(&state, move |_, cx| callback(cx))
2359 }
2360
2361 /// Returns the selection a long press made, laid out for its handles and
2362 /// edit menu, or `None` when the selection was made with a pointer.
2363 pub fn touch_selection(window: &Window, cx: &App) -> Option<TouchSelectionSnapshot> {
2364 WindowSelectionState::existing(window, cx)?
2365 .read(cx)
2366 .touch_selection()
2367 }
2368
2369 /// Records where a touch handle or the edit menu is painted this frame, so
2370 /// that pressing it does not clear the selection it belongs to. Call from
2371 /// paint, every frame the surface is shown.
2372 pub fn register_touch_ui(bounds: Bounds<Pixels>, window: &Window, cx: &mut App) {
2373 if let Some(state) = WindowSelectionState::existing(window, cx) {
2374 state.update(cx, |state, _| state.register_touch_ui(bounds));
2375 }
2376 }
2377
2378 /// Closes the edit menu and keeps the selection with its handles.
2379 pub fn close_edit_menu(window: &mut Window, cx: &mut App) {
2380 if let Some(state) = live_text_selection_state(window, cx) {
2381 state.update(cx, |state, cx| state.close_edit_menu(cx));
2382 }
2383 }
2384
2385 /// Selects all of the text the touch selection started in, keeping the
2386 /// handles and the edit menu over the result.
2387 pub fn select_all(window: &mut Window, cx: &mut App) {
2388 if let Some(state) = live_text_selection_state(window, cx) {
2389 state.update(cx, |state, cx| state.select_all_touched(cx));
2390 }
2391 }
2392
2393 /// Starts dragging one end of the touch selection from `finger`. The other
2394 /// end stays; the menu closes until [`Self::end_edge_drag`].
2395 pub fn begin_edge_drag(
2396 edge: SelectionEdge,
2397 finger: Point<Pixels>,
2398 window: &mut Window,
2399 cx: &mut App,
2400 ) {
2401 if let Some(state) = live_text_selection_state(window, cx) {
2402 state.update(cx, |state, cx| {
2403 state.begin_edge_drag(edge, finger, window, cx)
2404 });
2405 WindowSelectionState::resolve_content_keys(&state, cx);
2406 }
2407 }
2408
2409 /// Moves the dragged end to the text under `finger`.
2410 pub fn update_edge_drag(finger: Point<Pixels>, window: &mut Window, cx: &mut App) {
2411 if let Some(state) = live_text_selection_state(window, cx) {
2412 state.update(cx, |state, cx| state.update_edge_drag(finger, window, cx));
2413 WindowSelectionState::resolve_content_keys(&state, cx);
2414 }
2415 }
2416
2417 /// Ends the handle drag and reopens the edit menu over the result.
2418 pub fn end_edge_drag(window: &mut Window, cx: &mut App) {
2419 if let Some(state) = live_text_selection_state(window, cx) {
2420 state.update(cx, |state, cx| state.end_edge_drag(cx));
2421 }
2422 }
2423
2424 /// Activates the opaque selection scope for this window.
2425 pub fn activate_scope(scope: TextSelectionScopeId, window: &mut Window, cx: &mut App) {
2426 let Some(state) = WindowSelectionState::existing(window, cx) else {
2427 if !cx.has_global::<PendingTextSelectionScopes>() {
2428 cx.set_global(PendingTextSelectionScopes::default());
2429 }
2430 cx.global_mut::<PendingTextSelectionScopes>()
2431 .0
2432 .insert(window.window_handle().window_id(), scope);
2433 return;
2434 };
2435 let handlers = state.update(cx, |state, cx| state.set_active_scope_state(scope, cx));
2436 dispatch_clear_handlers(handlers, cx);
2437 }
2438}
2439
2440/// A zero-sized root layer which enables text selection for a window.
2441///
2442/// Mount one as the root's first child. Its stable `"window-text-selection"`
2443/// element identity retains the window-local selection entity across frames.
2444pub struct TextSelectionLayer;
2445
2446pub(crate) fn text_selection_scope(
2447 scope: TextSelectionScopeId,
2448 element: impl IntoElement,
2449) -> impl IntoElement {
2450 TextSelectionScopeMarker {
2451 scope,
2452 element: element.into_element(),
2453 }
2454}
2455
2456struct TextSelectionScopeMarker<E> {
2457 scope: TextSelectionScopeId,
2458 element: E,
2459}
2460
2461impl<E: Element> IntoElement for TextSelectionScopeMarker<E> {
2462 type Element = Self;
2463
2464 fn into_element(self) -> Self::Element {
2465 self
2466 }
2467}
2468
2469impl<E: Element> Element for TextSelectionScopeMarker<E> {
2470 type RequestLayoutState = E::RequestLayoutState;
2471 type PrepaintState = E::PrepaintState;
2472
2473 fn id(&self) -> Option<ElementId> {
2474 self.element.id()
2475 }
2476
2477 fn source_location(&self) -> Option<&'static std::panic::Location<'static>> {
2478 self.element.source_location()
2479 }
2480
2481 fn request_layout(
2482 &mut self,
2483 id: Option<&GlobalElementId>,
2484 inspector_id: Option<&InspectorElementId>,
2485 window: &mut Window,
2486 cx: &mut App,
2487 ) -> (LayoutId, Self::RequestLayoutState) {
2488 let window_id = window.window_handle().window_id();
2489 with_text_selection_scope(window_id, self.scope, cx, |cx| {
2490 self.element.request_layout(id, inspector_id, window, cx)
2491 })
2492 }
2493
2494 fn prepaint(
2495 &mut self,
2496 id: Option<&GlobalElementId>,
2497 inspector_id: Option<&InspectorElementId>,
2498 bounds: Bounds<Pixels>,
2499 request_layout: &mut Self::RequestLayoutState,
2500 window: &mut Window,
2501 cx: &mut App,
2502 ) -> Self::PrepaintState {
2503 let window_id = window.window_handle().window_id();
2504 with_text_selection_scope(window_id, self.scope, cx, |cx| {
2505 self.element
2506 .prepaint(id, inspector_id, bounds, request_layout, window, cx)
2507 })
2508 }
2509
2510 fn paint(
2511 &mut self,
2512 id: Option<&GlobalElementId>,
2513 inspector_id: Option<&InspectorElementId>,
2514 bounds: Bounds<Pixels>,
2515 request_layout: &mut Self::RequestLayoutState,
2516 prepaint: &mut Self::PrepaintState,
2517 window: &mut Window,
2518 cx: &mut App,
2519 ) {
2520 let window_id = window.window_handle().window_id();
2521 with_text_selection_scope(window_id, self.scope, cx, |cx| {
2522 self.element.paint(
2523 id,
2524 inspector_id,
2525 bounds,
2526 request_layout,
2527 prepaint,
2528 window,
2529 cx,
2530 );
2531 });
2532 }
2533}
2534
2535#[doc(hidden)]
2536pub struct TextSelectionLayerPrepaintState(Entity<WindowSelectionState>);
2537
2538impl IntoElement for TextSelectionLayer {
2539 type Element = Self;
2540
2541 fn into_element(self) -> Self::Element {
2542 self
2543 }
2544}
2545
2546impl Element for TextSelectionLayer {
2547 type RequestLayoutState = ();
2548 type PrepaintState = TextSelectionLayerPrepaintState;
2549
2550 fn id(&self) -> Option<ElementId> {
2551 Some("window-text-selection".into())
2552 }
2553
2554 fn source_location(&self) -> Option<&'static std::panic::Location<'static>> {
2555 None
2556 }
2557
2558 fn request_layout(
2559 &mut self,
2560 _: Option<&GlobalElementId>,
2561 _: Option<&InspectorElementId>,
2562 window: &mut Window,
2563 cx: &mut App,
2564 ) -> (LayoutId, Self::RequestLayoutState) {
2565 (window.request_layout(Style::default(), [], cx), ())
2566 }
2567
2568 fn prepaint(
2569 &mut self,
2570 global_id: Option<&GlobalElementId>,
2571 _: Option<&InspectorElementId>,
2572 _: Bounds<Pixels>,
2573 _: &mut Self::RequestLayoutState,
2574 window: &mut Window,
2575 cx: &mut App,
2576 ) -> Self::PrepaintState {
2577 // Automatic participant order is paint order within this frame. Keep
2578 // this lifecycle in base so base-only applications do not need a
2579 // separate root component to reset it. Otherwise, registering the
2580 // first of two selected TextViews temporarily reverses their order
2581 // against the previous frame and alternates coverage forever.
2582 GlobalState::init(cx);
2583 GlobalState::global_mut(cx).begin_selection_frame();
2584 let state = retain_text_selection_state(global_id, window, cx);
2585 // The handles and the menu register again as they paint this frame.
2586 state.update(cx, |state, _| state.touch.begin_frame());
2587 TextSelectionLayerPrepaintState(state)
2588 }
2589
2590 fn paint(
2591 &mut self,
2592 _: Option<&GlobalElementId>,
2593 _: Option<&InspectorElementId>,
2594 _: Bounds<Pixels>,
2595 _: &mut Self::RequestLayoutState,
2596 state: &mut Self::PrepaintState,
2597 window: &mut Window,
2598 cx: &mut App,
2599 ) {
2600 paint_text_selection(&state.0, window, cx);
2601 }
2602}
2603
2604fn retain_text_selection_state(
2605 global_id: Option<&GlobalElementId>,
2606 window: &mut Window,
2607 cx: &mut App,
2608) -> Entity<WindowSelectionState> {
2609 let window_id = window.window_handle().window_id();
2610 let state = window.with_element_state::<Entity<WindowSelectionState>, _>(
2611 global_id.expect("TextSelection has a stable element id"),
2612 |retained, _| {
2613 let state = retained.unwrap_or_else(|| WindowSelectionState::acquire(window_id, cx));
2614 (state.clone(), state)
2615 },
2616 );
2617 if !cx.has_global::<SelectionStateRegistry>() {
2618 cx.set_global(SelectionStateRegistry::default());
2619 }
2620 cx.global_mut::<SelectionStateRegistry>()
2621 .0
2622 .insert(window_id, state.downgrade());
2623 state
2624}
2625
2626fn paint_text_selection(state: &Entity<WindowSelectionState>, window: &mut Window, cx: &mut App) {
2627 if state.update(cx, |state, _| state.schedule_finish_frame()) {
2628 let state = state.downgrade();
2629 window.defer(cx, move |window, cx| {
2630 let Some(state) = state.upgrade() else {
2631 return;
2632 };
2633 let handlers = state.update(cx, |state, cx| state.finish_frame(cx));
2634 dispatch_clear_handlers(handlers, cx);
2635 // Direct participant scrolling produces no wheel event. Refresh
2636 // the held cursor after paint registers the new scroll geometry.
2637 let refresh_cursor = state.update(cx, |state, cx| {
2638 if std::mem::take(&mut state.refresh_held_cursor) && state.is_selecting {
2639 // A handle drag holds the finger off the text; keep the
2640 // same offset, or the cursor would hop between the two.
2641 let position = window.mouse_position();
2642 let position = state
2643 .touch
2644 .drag
2645 .map_or(position, |drag| drag.text_position(position));
2646 state.update_in_window(position, window, cx);
2647 true
2648 } else {
2649 false
2650 }
2651 });
2652 if refresh_cursor {
2653 WindowSelectionState::resolve_content_keys(&state, cx);
2654 }
2655 });
2656 }
2657
2658 let mouse_down_state = state.downgrade();
2659 window.on_mouse_event(move |event: &MouseDownEvent, phase, window, cx| {
2660 if event.button != MouseButton::Left {
2661 return;
2662 }
2663 let Some(state) = mouse_down_state.upgrade() else {
2664 return;
2665 };
2666 // A press on a handle or on the edit menu acts on the selection; it
2667 // must not clear it.
2668 if state.read(cx).touch.covers(event.position) {
2669 return;
2670 }
2671 if phase.capture() {
2672 GlobalState::init(cx);
2673 GlobalState::reset_text_selection_suppression(cx);
2674 let handlers = state.update(cx, |state, cx| {
2675 if state.mouse_down_prepared {
2676 return Vec::new();
2677 }
2678 state.mouse_down_prepared = true;
2679 state.prepare_for_mouse_down(event.click_count == 1 && event.modifiers.shift, cx)
2680 });
2681 dispatch_clear_handlers(handlers, cx);
2682 } else if event.click_count == 1 {
2683 if GlobalState::is_text_selection_suppressed(cx) {
2684 state.update(cx, |state, _| state.pending_extension_anchor = None);
2685 return;
2686 }
2687 state.update(cx, |state, cx| {
2688 if !state.is_selecting {
2689 state.begin_in_window(event.position, event.modifiers.shift, window, cx)
2690 }
2691 });
2692 WindowSelectionState::resolve_content_keys(&state, cx);
2693 } else if event.click_count >= 2 {
2694 if GlobalState::is_text_selection_suppressed(cx) {
2695 return;
2696 }
2697 let touch = GlobalState::is_touch_press(cx);
2698 state.update(cx, |state, cx| {
2699 state.select_at(event.position, event.click_count, window, cx);
2700 // A double tap is touch's other way to select a word, and it
2701 // gets the handles and the menu like a long press does.
2702 if touch {
2703 state.keep_touch_selection(cx);
2704 }
2705 });
2706 WindowSelectionState::resolve_content_keys(&state, cx);
2707 }
2708 });
2709
2710 // Every touch is offered as a drag first; that is how a tap's mouse
2711 // events are later told apart from a mouse's.
2712 window.on_mouse_event(move |event: &TouchDragEvent, phase, _, cx| {
2713 if phase.capture() && event.phase == TouchPhase::Started {
2714 GlobalState::note_touch(cx);
2715 }
2716 });
2717
2718 // Touch panning remains scrolling until a long press actually hits text.
2719 // Claiming the gesture keeps subsequent moves out of the pan recognizer.
2720 let long_press_state = state.downgrade();
2721 window.on_mouse_event(move |event: &LongPressEvent, phase, window, cx| {
2722 if !phase.bubble() {
2723 return;
2724 }
2725 let Some(state) = long_press_state.upgrade() else {
2726 return;
2727 };
2728 if event.phase == TouchPhase::Started {
2729 if window.default_prevented()
2730 || state.read(cx).touch.covers(event.start_position)
2731 || !state.update(cx, |state, cx| {
2732 state
2733 .endpoint(event.start_position, Some(window), cx)
2734 .inside_text
2735 })
2736 {
2737 return;
2738 }
2739 GlobalState::init(cx);
2740 GlobalState::reset_text_selection_suppression(cx);
2741 let handlers = state.update(cx, |state, cx| state.prepare_for_mouse_down(false, cx));
2742 dispatch_clear_handlers(handlers, cx);
2743 let selected = state.update(cx, |state, cx| {
2744 state.select_at(event.start_position, 2, window, cx);
2745 state.anchor.is_some()
2746 });
2747 if !selected {
2748 return;
2749 }
2750 window.capture_long_press(&state);
2751 } else if !window.has_long_press_capture(&state) {
2752 return;
2753 } else {
2754 state.update(cx, |state, cx| match event.phase {
2755 TouchPhase::Moved => {
2756 state.is_selecting = true;
2757 state.update_in_window(event.position, window, cx);
2758 }
2759 TouchPhase::Ended | TouchPhase::Cancelled => {
2760 state.end(cx);
2761 // The finger is up; the selection it made gets its
2762 // handles and the edit menu.
2763 state.keep_touch_selection(cx);
2764 }
2765 _ => {}
2766 });
2767 }
2768 window.prevent_default();
2769 cx.stop_propagation();
2770 WindowSelectionState::resolve_content_keys(&state, cx);
2771 });
2772
2773 // A handle drag in progress follows the finger or the pointer wherever
2774 // it goes, whether or not the handle it took is laid out this frame.
2775 let drag_state = state.downgrade();
2776 window.on_mouse_event(move |event: &TouchDragEvent, phase, window, cx| {
2777 if !phase.bubble() || event.phase == TouchPhase::Started {
2778 return;
2779 }
2780 let Some(state) = drag_state.upgrade() else {
2781 return;
2782 };
2783 if state.read(cx).touch.drag.is_none() {
2784 return;
2785 }
2786 cx.stop_propagation();
2787 state.update(cx, |state, cx| match event.phase {
2788 TouchPhase::Moved => state.update_edge_drag(event.position, window, cx),
2789 _ => state.end_edge_drag(cx),
2790 });
2791 WindowSelectionState::resolve_content_keys(&state, cx);
2792 });
2793 let drag_state = state.downgrade();
2794 window.on_mouse_event(move |event: &MouseMoveEvent, phase, window, cx| {
2795 if phase.bubble()
2796 && event.pressed_button == Some(MouseButton::Left)
2797 && let Some(state) = drag_state.upgrade()
2798 && state.read(cx).touch.drag.is_some()
2799 {
2800 state.update(cx, |state, cx| {
2801 state.update_edge_drag(event.position, window, cx)
2802 });
2803 WindowSelectionState::resolve_content_keys(&state, cx);
2804 }
2805 });
2806 let drag_state = state.downgrade();
2807 window.on_mouse_event(move |event: &MouseUpEvent, phase, _, cx| {
2808 if phase.bubble()
2809 && event.button == MouseButton::Left
2810 && let Some(state) = drag_state.upgrade()
2811 {
2812 state.update(cx, |state, cx| state.end_edge_drag(cx));
2813 }
2814 });
2815
2816 let mouse_move_state = state.downgrade();
2817 window.on_mouse_event(move |event: &MouseMoveEvent, phase, window, cx| {
2818 if phase.bubble()
2819 && let Some(state) = mouse_move_state.upgrade()
2820 {
2821 state.update(cx, |state, cx| {
2822 // A handle drag maps the pointer through the handle's offset;
2823 // the raw pointer must not fight it.
2824 if state.touch.drag.is_some() {
2825 return;
2826 }
2827 if event.pressed_button != Some(MouseButton::Left) {
2828 state.mouse_down_prepared = false;
2829 state.end(cx);
2830 return;
2831 }
2832 state.update_in_window(event.position, window, cx)
2833 });
2834 WindowSelectionState::resolve_content_keys(&state, cx);
2835 }
2836 });
2837
2838 let mouse_up_state = state.downgrade();
2839 window.on_mouse_event(move |_: &MouseUpEvent, phase, _, cx| {
2840 if phase.bubble()
2841 && let Some(state) = mouse_up_state.upgrade()
2842 {
2843 state.update(cx, |state, cx| {
2844 state.mouse_down_prepared = false;
2845 state.end(cx)
2846 });
2847 }
2848 });
2849
2850 let scroll_state = state.downgrade();
2851 window.on_mouse_event(move |event: &ScrollWheelEvent, phase, window, cx| {
2852 let Some(state) = scroll_state.upgrade() else {
2853 return;
2854 };
2855 // On capture, before a scroll container can stop the event: one
2856 // stretched past its end swallows the whole stream, the lift
2857 // included, and the menu would never come back.
2858 if phase.capture() {
2859 state.update(cx, |state, cx| {
2860 state.edit_menu_on_scroll(event.touch_phase, cx)
2861 });
2862 return;
2863 }
2864 if phase.bubble() {
2865 let position = window.mouse_position();
2866 state.update(cx, |state, cx| {
2867 // A handle drag holds the finger off the text; keep its offset.
2868 let position = state
2869 .touch
2870 .drag
2871 .map_or(position, |drag| drag.text_position(position));
2872 state.update_in_window(position, window, cx)
2873 });
2874 WindowSelectionState::resolve_content_keys(&state, cx);
2875 }
2876 });
2877}
2878
2879fn live_text_selection_state(
2880 window: &Window,
2881 cx: &mut App,
2882) -> Option<Entity<WindowSelectionState>> {
2883 WindowSelectionState::existing(window, cx)
2884}
2885
2886pub(crate) fn clear_window_text_selection(window_id: gpui::WindowId, cx: &mut App) {
2887 if !cx.has_global::<SelectionStateRegistry>() {
2888 return;
2889 }
2890 let Some(state) = cx
2891 .global::<SelectionStateRegistry>()
2892 .0
2893 .get(&window_id)
2894 .and_then(WeakEntity::upgrade)
2895 else {
2896 return;
2897 };
2898 let handlers = state.update(cx, |state, cx| state.clear_state(cx));
2899 dispatch_clear_handlers(handlers, cx);
2900}
2901
2902#[cfg(test)]
2903mod tests {
2904 use super::*;
2905 use crate::ElementExt as _;
2906 use gpui::{
2907 Bounds, ContentMask, Context, Hitbox, HitboxBehavior, HitboxId, InteractiveElement as _,
2908 IntoElement, ParentElement as _, Render, SharedString, Styled as _, StyledText,
2909 TestAppContext, TextLayout, Window, div, point, prelude::FluentBuilder as _, px, size,
2910 };
2911 use std::{
2912 cell::{Cell, RefCell},
2913 rc::Rc,
2914 };
2915
2916 struct FakeParticipant {
2917 selection: TextSelectionHandle,
2918 }
2919
2920 struct WindowSelectionView {
2921 selection: TextSelectionHandle,
2922 }
2923
2924 struct SelectionElementOnlyView;
2925 struct ToggleSelectionElementView {
2926 enabled: bool,
2927 selection: TextSelectionHandle,
2928 }
2929
2930 struct DoubleSelectionElementView {
2931 selection: TextSelectionHandle,
2932 }
2933
2934 struct WindowOwnedSelectionView {
2935 selection: TextSelectionHandle,
2936 }
2937
2938 struct FirstFrameScopedSelectionView {
2939 selection: TextSelectionHandle,
2940 }
2941
2942 struct PlainRunLayoutView {
2943 texts: Vec<SharedString>,
2944 layouts: Vec<TextLayout>,
2945 }
2946
2947 impl Render for WindowSelectionView {
2948 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
2949 div()
2950 }
2951 }
2952
2953 impl Render for SelectionElementOnlyView {
2954 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
2955 div()
2956 .size_full()
2957 .child(TextSelectionLayer)
2958 .child(
2959 div()
2960 .size_full()
2961 .on_mouse_down(MouseButton::Left, |_, _, cx| {
2962 GlobalState::suppress_text_selection(cx);
2963 }),
2964 )
2965 }
2966 }
2967
2968 impl Render for ToggleSelectionElementView {
2969 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
2970 let selection = self.selection.clone();
2971 div().when(self.enabled, |this| {
2972 this.child(TextSelectionLayer)
2973 .child(div().size_full().on_prepaint(move |bounds, window, cx| {
2974 let hitbox = window.insert_hitbox(bounds, HitboxBehavior::Normal);
2975 selection.register(
2976 TextSelectionRegistration::new(hitbox, bounds)
2977 .with_text_bounds(vec![bounds]),
2978 window,
2979 cx,
2980 );
2981 }))
2982 })
2983 }
2984 }
2985
2986 impl Render for DoubleSelectionElementView {
2987 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
2988 let selection = self.selection.clone();
2989 div()
2990 .size_full()
2991 .child(TextSelectionLayer)
2992 .child(TextSelectionLayer)
2993 .on_prepaint(move |bounds, window, cx| {
2994 let hitbox = window.insert_hitbox(bounds, HitboxBehavior::Normal);
2995 selection.register(
2996 TextSelectionRegistration::new(hitbox, bounds)
2997 .with_text_bounds(vec![bounds]),
2998 window,
2999 cx,
3000 );
3001 })
3002 }
3003 }
3004
3005 impl Render for WindowOwnedSelectionView {
3006 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
3007 let selection = self.selection.clone();
3008 div()
3009 .size_full()
3010 .child(TextSelectionLayer)
3011 .child(div().size_full().on_prepaint(move |bounds, window, cx| {
3012 let hitbox = window.insert_hitbox(bounds, HitboxBehavior::Normal);
3013 selection.register(
3014 TextSelectionRegistration::new(hitbox, bounds)
3015 .with_text_bounds(vec![bounds]),
3016 window,
3017 cx,
3018 );
3019 }))
3020 }
3021 }
3022
3023 impl Render for FirstFrameScopedSelectionView {
3024 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
3025 let scope = TextSelectionScopeId::from_raw(23);
3026 TextSelection::activate_scope(scope, window, cx);
3027 let selection = self.selection.clone();
3028
3029 div().child(TextSelectionLayer).child(
3030 div()
3031 .size_full()
3032 .on_prepaint(move |bounds, window, cx| {
3033 let hitbox = window.insert_hitbox(bounds, HitboxBehavior::Normal);
3034 selection.register(
3035 TextSelectionRegistration::new(hitbox, bounds)
3036 .with_text_bounds(vec![bounds]),
3037 window,
3038 cx,
3039 );
3040 })
3041 .text_selection_scope(scope),
3042 )
3043 }
3044 }
3045
3046 impl Render for PlainRunLayoutView {
3047 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
3048 self.layouts.clear();
3049 let children = self
3050 .texts
3051 .iter()
3052 .enumerate()
3053 .map(|(index, text)| {
3054 let text = StyledText::new(text.clone());
3055 self.layouts.push(text.layout().clone());
3056 div().absolute().top(px(index as f32 * 40.)).child(text)
3057 })
3058 .collect::<Vec<_>>();
3059 div().size_full().children(children)
3060 }
3061 }
3062
3063 impl FakeParticipant {
3064 fn new(text: &str, cx: &mut gpui::App) -> Self {
3065 let selection = TextSelectionHandle::new(text, cx);
3066 Self { selection }
3067 }
3068
3069 fn register(
3070 &self,
3071 selection_state: &mut WindowSelectionState,
3072 y: f32,
3073 scope: TextSelectionScopeId,
3074 document_order: u64,
3075 cx: &mut gpui::App,
3076 ) {
3077 let bounds = Bounds::new(point(px(0.), px(y)), size(px(100.), px(10.)));
3078 selection_state.register_participant(
3079 self.selection.clone(),
3080 TextSelectionRegistration::new(
3081 Hitbox {
3082 id: HitboxId::placeholder(),
3083 bounds,
3084 content_mask: ContentMask { bounds },
3085 behavior: HitboxBehavior::Normal,
3086 },
3087 bounds,
3088 )
3089 .with_scope(scope)
3090 .with_document_order(document_order)
3091 .with_text_bounds(vec![bounds]),
3092 cx,
3093 );
3094 }
3095 }
3096
3097 fn laid_out_runs(texts: &[&str], cx: &mut TestAppContext) -> Vec<(SharedString, TextLayout)> {
3098 let texts = texts
3099 .iter()
3100 .map(|text| SharedString::from(*text))
3101 .collect::<Vec<_>>();
3102 let view = cx.add_window({
3103 let texts = texts.clone();
3104 move |_, _| PlainRunLayoutView {
3105 texts,
3106 layouts: Vec::new(),
3107 }
3108 });
3109 cx.update_window(*view, |_, window, cx| {
3110 let _ = window.draw(cx);
3111 })
3112 .unwrap();
3113 let layouts = cx.update(|cx| view.read(cx).unwrap().layouts.clone());
3114 texts.into_iter().zip(layouts).collect()
3115 }
3116
3117 fn plain_snapshot(anchor: Point<Pixels>, cursor: Point<Pixels>) -> TextSelectionSnapshot {
3118 TextSelectionSnapshot::new(
3119 TextSelectionEndpoint::new(None, anchor),
3120 TextSelectionEndpoint::new(None, cursor),
3121 )
3122 .with_window_points(Some(TextSelectionWindowPoints { anchor, cursor }))
3123 }
3124
3125 #[gpui::test]
3126 fn scope_stack_is_cleaned_after_panicking_subtree(cx: &mut TestAppContext) {
3127 let window_id = {
3128 let (_, window_cx) = cx.add_window_view(|_, _| SelectionElementOnlyView);
3129 window_cx.update(|window, _| window.window_handle().window_id())
3130 };
3131 let scope = TextSelectionScopeId::from_raw(41);
3132
3133 cx.update(|cx| {
3134 let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
3135 with_text_selection_scope(window_id, scope, cx, |_| panic!("subtree failed"));
3136 }));
3137
3138 assert!(result.is_err());
3139 assert_eq!(current_text_selection_scope(window_id, cx), None);
3140 });
3141 }
3142
3143 #[gpui::test]
3144 fn reentrant_scope_from_one_window_does_not_pollute_another(cx: &mut TestAppContext) {
3145 let first_window_id = {
3146 let (_, window_cx) = cx.add_window_view(|_, _| SelectionElementOnlyView);
3147 window_cx.update(|window, _| window.window_handle().window_id())
3148 };
3149 let second_window_id = {
3150 let (_, window_cx) = cx.add_window_view(|_, _| SelectionElementOnlyView);
3151 window_cx.update(|window, _| window.window_handle().window_id())
3152 };
3153 let scope = TextSelectionScopeId::from_raw(42);
3154
3155 cx.update(|cx| {
3156 with_text_selection_scope(first_window_id, scope, cx, |cx| {
3157 assert_eq!(current_text_selection_scope(second_window_id, cx), None);
3158 assert_eq!(
3159 current_text_selection_scope(first_window_id, cx),
3160 Some(scope)
3161 );
3162 });
3163 });
3164 }
3165
3166 #[gpui::test]
3167 fn selection_callback_can_reenter_its_selection_state(cx: &mut TestAppContext) {
3168 let called = Rc::new(Cell::new(false));
3169 let called_from_callback = called.clone();
3170 let (selection_state, participant) = cx.update(|cx| {
3171 let selection_state = cx.new(|_| WindowSelectionState::default());
3172 let selection_state_for_callback = selection_state.clone();
3173 let participant = FakeParticipant::new("participant", cx);
3174 participant
3175 .selection
3176 .subscribe(
3177 move |event, cx| {
3178 if matches!(event, TextSelectionEvent::SelectionChanged(Some(_))) {
3179 selection_state_for_callback
3180 .update(cx, |_, _| called_from_callback.set(true));
3181 }
3182 },
3183 cx,
3184 )
3185 .detach();
3186 (selection_state, participant)
3187 });
3188 cx.run_until_parked();
3189 cx.update(|cx| {
3190 selection_state.update(cx, |selection_state, cx| {
3191 participant.register(selection_state, 0., TextSelectionScopeId::default(), 0, cx);
3192 selection_state.begin(point(px(1.), px(1.)), false, cx);
3193 selection_state.update(point(px(20.), px(1.)), cx);
3194 });
3195 });
3196 cx.run_until_parked();
3197 assert!(called.get());
3198 }
3199
3200 #[gpui::test]
3201 fn selection_events_preserve_snapshot_then_clear_order(cx: &mut TestAppContext) {
3202 let observed = Rc::new(RefCell::new(Vec::new()));
3203 let observed_for_callback = observed.clone();
3204 let selection = cx.update(|cx| {
3205 let selection = TextSelectionHandle::new("selection", cx);
3206 selection
3207 .subscribe(
3208 move |event, _| {
3209 if let TextSelectionEvent::SelectionChanged(snapshot) = event {
3210 observed_for_callback.borrow_mut().push(snapshot.is_some());
3211 }
3212 },
3213 cx,
3214 )
3215 .detach();
3216 selection
3217 });
3218 cx.run_until_parked();
3219 cx.update(|cx| {
3220 selection.0.update(cx, |state, cx| {
3221 state.set_snapshot(
3222 Some(plain_snapshot(point(px(1.), px(1.)), point(px(8.), px(1.)))),
3223 cx,
3224 );
3225 state.clear_state(cx);
3226 });
3227 });
3228 cx.run_until_parked();
3229 assert_eq!(&*observed.borrow(), &[true, false]);
3230 }
3231
3232 fn text_run(order: u64, text: SharedString, layout: TextLayout) -> TextSelectionRun {
3233 let bounds = layout.bounds();
3234 TextSelectionRun::new(text, layout, bounds).with_document_order(order)
3235 }
3236
3237 #[gpui::test]
3238 fn public_selection_data_uses_builders_and_readers(cx: &mut TestAppContext) {
3239 let bounds = Bounds::new(point(px(1.), px(2.)), size(px(30.), px(10.)));
3240 let hitbox = Hitbox {
3241 id: HitboxId::placeholder(),
3242 bounds,
3243 content_mask: ContentMask { bounds },
3244 behavior: HitboxBehavior::Normal,
3245 };
3246 let scope = TextSelectionScopeId::from_raw(7);
3247 let endpoint = TextSelectionEndpoint::new(None, bounds.origin)
3248 .with_content_key(TextSelectionContentKey::new(11));
3249 let snapshot = TextSelectionSnapshot::new(endpoint, endpoint)
3250 .with_selecting(true)
3251 .with_window_points(Some(TextSelectionWindowPoints {
3252 anchor: bounds.origin,
3253 cursor: bounds.bottom_right(),
3254 }))
3255 .with_coverage(TextSelectionCoverage::Full);
3256 let registration = TextSelectionRegistration::new(hitbox, bounds)
3257 .with_scroll_offset(point(px(3.), px(4.)))
3258 .with_scope(scope)
3259 .with_document_order(9)
3260 .with_text_bounds(vec![bounds]);
3261
3262 assert_eq!(endpoint.entity_id(), None);
3263 assert_eq!(endpoint.content_point(), bounds.origin);
3264 assert_eq!(
3265 endpoint.content_key(),
3266 Some(TextSelectionContentKey::new(11))
3267 );
3268 assert_eq!(snapshot.anchor(), endpoint);
3269 assert_eq!(snapshot.cursor(), endpoint);
3270 assert!(snapshot.is_selecting());
3271 assert_eq!(snapshot.coverage(), TextSelectionCoverage::Full);
3272 assert_eq!(
3273 snapshot.window_points(),
3274 Some(TextSelectionWindowPoints {
3275 anchor: bounds.origin,
3276 cursor: bounds.bottom_right(),
3277 })
3278 );
3279 assert_eq!(registration.bounds(), bounds);
3280 assert_eq!(registration.scroll_offset(), point(px(3.), px(4.)));
3281 assert_eq!(registration.scope(), scope);
3282 assert_eq!(registration.document_order(), 9);
3283 assert_eq!(registration.text_bounds(), &[bounds]);
3284
3285 let (text, layout) = laid_out_runs(&["aé"], cx).pop().unwrap();
3286 let text_run = TextSelectionRun::new(text.clone(), layout.clone(), layout.bounds())
3287 .with_document_order(3);
3288 assert_eq!(text_run.document_order(), 3);
3289 assert_eq!(text_run.text(), &text);
3290 assert_eq!(text_run.layout().len(), layout.len());
3291 assert_eq!(text_run.bounds(), layout.bounds());
3292
3293 let projection = TextSelectionProjection {
3294 ranges: vec![Some(1..3)],
3295 is_active: true,
3296 };
3297 assert_eq!(projection.ranges(), &[Some(1..3)]);
3298 assert!(projection.is_active());
3299 }
3300
3301 #[gpui::test]
3302 fn selection_handle_is_the_public_adapter_seam(cx: &mut TestAppContext) {
3303 let selected = Rc::new(Cell::new(false));
3304 let selected_from_callback = selected.clone();
3305 cx.update(|cx| {
3306 let selection = TextSelectionHandle::new("initial", cx);
3307 let entity_id = selection.entity_id();
3308 selection.set_fallback_copy_text("updated", cx);
3309 selection.set_local_selection(true, cx);
3310 selection
3311 .subscribe(
3312 move |event, _| {
3313 if let TextSelectionEvent::SelectionChanged(snapshot) = event {
3314 selected_from_callback.set(snapshot.is_some());
3315 }
3316 },
3317 cx,
3318 )
3319 .detach();
3320 selection.focus_with(|_, _| {}, cx);
3321 selection.copy_with(|_| "copied".to_string(), cx);
3322 selection.resolve_content_key_with(|_, _| Some(TextSelectionContentKey::new(3)), cx);
3323
3324 assert_eq!(selection.entity_id(), entity_id);
3325 assert_eq!(selection.snapshot(cx), None);
3326 assert_eq!(
3327 selection.update_runs(&[], cx),
3328 TextSelectionProjection::default()
3329 );
3330 });
3331 assert!(!selected.get());
3332 }
3333
3334 #[gpui::test]
3335 fn selection_handle_can_subscribe_its_window_to_refresh(cx: &mut TestAppContext) {
3336 let (_, cx) = cx.add_window_view(|_, cx| WindowSelectionView {
3337 selection: TextSelectionHandle::new("refresh", cx),
3338 });
3339 cx.update(|window, cx| {
3340 let selection = TextSelectionHandle::new("refresh", cx);
3341 selection.refresh_window_on_change(window, cx).detach();
3342 });
3343 }
3344
3345 #[gpui::test]
3346 fn plain_projection_preserves_forward_reversed_and_unicode_ranges(cx: &mut TestAppContext) {
3347 let (text, layout) = laid_out_runs(&["aé🙂z"], cx).pop().unwrap();
3348 let run = text_run(0, text, layout.clone());
3349 let start = layout.position_for_index(1).unwrap();
3350 let end = layout.position_for_index(7).unwrap();
3351
3352 let forward = project_ranges(Some(plain_snapshot(start, end)), std::slice::from_ref(&run));
3353 let reversed = project_ranges(Some(plain_snapshot(end, start)), &[run]);
3354
3355 assert_eq!(forward.ranges(), &[Some(1..7)]);
3356 assert_eq!(reversed.ranges(), &[Some(1..7)]);
3357 assert!(forward.is_active());
3358 assert!(reversed.is_active());
3359 }
3360
3361 #[gpui::test]
3362 fn double_click_expands_a_plain_run_to_the_input_word_boundary(cx: &mut TestAppContext) {
3363 let (text, layout) = laid_out_runs(&["one café, three"], cx).pop().unwrap();
3364 let run = text_run(0, text, layout.clone());
3365 let click = layout.position_for_index(6).unwrap();
3366
3367 let (anchor, cursor) =
3368 points_for_multi_click(std::slice::from_ref(&run), click, 2).unwrap();
3369 let states = project_ranges(Some(plain_snapshot(anchor, cursor)), &[run]);
3370
3371 assert_eq!(states.ranges(), &[Some(4..9)]);
3372 }
3373
3374 #[gpui::test]
3375 fn multi_click_uses_text_layout_window_coordinates_at_a_nonzero_origin(
3376 cx: &mut TestAppContext,
3377 ) {
3378 let mut runs = laid_out_runs(&["above", "alpha beta"], cx);
3379 let (text, layout) = runs.pop().unwrap();
3380 assert!(layout.bounds().origin.y > px(0.));
3381 let run = text_run(0, text, layout.clone());
3382 let click = layout.position_for_index(7).unwrap();
3383
3384 let (anchor, cursor) =
3385 points_for_multi_click(std::slice::from_ref(&run), click, 2).unwrap();
3386 let projection = project_ranges(Some(plain_snapshot(anchor, cursor)), &[run]);
3387
3388 assert_eq!(projection.ranges(), &[Some(6..10)]);
3389 }
3390
3391 #[gpui::test]
3392 fn triple_click_expands_to_the_input_logical_line_not_the_visual_row(cx: &mut TestAppContext) {
3393 let (text, layout) = laid_out_runs(&["second line"], cx).pop().unwrap();
3394 let run = text_run(0, text, layout.clone());
3395 let click = layout.position_for_index(4).unwrap();
3396
3397 let (anchor, cursor) =
3398 points_for_multi_click(std::slice::from_ref(&run), click, 4).unwrap();
3399 let states = project_ranges(Some(plain_snapshot(anchor, cursor)), &[run]);
3400
3401 assert_eq!(states.ranges(), &[Some(0..11)]);
3402 assert_eq!(line_range_at("first line\nsecond line\nthird", 15), 11..22);
3403 }
3404
3405 #[gpui::test]
3406 fn plain_projection_spans_multiple_runs_and_leaves_empty_gutters_unselected(
3407 cx: &mut TestAppContext,
3408 ) {
3409 let mut runs = laid_out_runs(&["first", "", "second"], cx);
3410 let (first_text, first_layout) = runs.remove(0);
3411 let (gutter_text, gutter_layout) = runs.remove(0);
3412 let (second_text, second_layout) = runs.remove(0);
3413 let start = first_layout.position_for_index(2).unwrap();
3414 let end = second_layout.position_for_index(3).unwrap();
3415 let states = project_ranges(
3416 Some(plain_snapshot(start, end)),
3417 &[
3418 text_run(2, second_text, second_layout),
3419 text_run(1, gutter_text, gutter_layout),
3420 text_run(0, first_text, first_layout),
3421 ],
3422 );
3423
3424 assert_eq!(states.ranges(), &[Some(0..3), None, Some(2..5)]);
3425 assert!(states.is_active());
3426 }
3427
3428 #[gpui::test]
3429 fn plain_projection_caches_multiple_participant_copies_in_document_order(
3430 cx: &mut TestAppContext,
3431 ) {
3432 let mut runs = laid_out_runs(&["one", "two"], cx);
3433 let (first_text, first_layout) = runs.remove(0);
3434 let (second_text, second_layout) = runs.remove(0);
3435 let snapshot = plain_snapshot(
3436 first_layout.position_for_index(1).unwrap(),
3437 second_layout.position_for_index(2).unwrap(),
3438 );
3439 cx.update(|cx| {
3440 let mut selection_state = WindowSelectionState::default();
3441 let first = FakeParticipant::new("", cx);
3442 let second = FakeParticipant::new("", cx);
3443 first.register(
3444 &mut selection_state,
3445 0.,
3446 TextSelectionScopeId::default(),
3447 1,
3448 cx,
3449 );
3450 second.register(
3451 &mut selection_state,
3452 20.,
3453 TextSelectionScopeId::default(),
3454 0,
3455 cx,
3456 );
3457
3458 first
3459 .selection
3460 .0
3461 .update(cx, |state, cx| state.set_snapshot(Some(snapshot), cx));
3462 let projection = first
3463 .selection
3464 .update_runs(&[text_run(0, first_text, first_layout)], cx);
3465 assert_eq!(projection.ranges(), &[Some(1..3)]);
3466 assert!(projection.is_active());
3467 second
3468 .selection
3469 .0
3470 .update(cx, |state, cx| state.set_snapshot(Some(snapshot), cx));
3471 let projection = second
3472 .selection
3473 .update_runs(&[text_run(0, second_text, second_layout)], cx);
3474 assert_eq!(projection.ranges(), &[Some(0..2)]);
3475 assert!(projection.is_active());
3476
3477 assert_eq!(selection_state.selected_text(cx), "tw\nne");
3478 });
3479 }
3480
3481 #[gpui::test]
3482 fn plain_projection_invalidates_cached_copy_when_the_snapshot_changes(cx: &mut TestAppContext) {
3483 let (text, layout) = laid_out_runs(&["first"], cx).pop().unwrap();
3484 let first_snapshot = plain_snapshot(
3485 layout.position_for_index(1).unwrap(),
3486 layout.position_for_index(3).unwrap(),
3487 );
3488 let changed_snapshot = plain_snapshot(
3489 layout.position_for_index(3).unwrap(),
3490 layout.position_for_index(5).unwrap(),
3491 );
3492 let run = text_run(0, text, layout);
3493 cx.update(|cx| {
3494 let mut selection_state = WindowSelectionState::default();
3495 let participant = FakeParticipant::new("", cx);
3496 participant.register(
3497 &mut selection_state,
3498 0.,
3499 TextSelectionScopeId::default(),
3500 0,
3501 cx,
3502 );
3503 participant.selection.0.update(cx, |state, cx| {
3504 state.set_snapshot(Some(first_snapshot), cx);
3505 state.update_runs(std::slice::from_ref(&run));
3506 });
3507 assert_eq!(selection_state.selected_text(cx), "ir");
3508
3509 participant.selection.0.update(cx, |state, cx| {
3510 state.set_snapshot(Some(changed_snapshot), cx);
3511 });
3512 assert_eq!(selection_state.selected_text(cx), "");
3513
3514 participant.selection.update_runs(&[run], cx);
3515 assert_eq!(selection_state.selected_text(cx), "st");
3516 selection_state.clear(cx);
3517 participant.selection.set_local_selection(true, cx);
3518 assert_eq!(selection_state.selected_text(cx), "");
3519 });
3520 }
3521
3522 #[gpui::test]
3523 fn plain_projection_orders_cached_runs_by_frame_order_not_input_order(cx: &mut TestAppContext) {
3524 let mut runs = laid_out_runs(&["one", "two"], cx);
3525 let (first_text, first_layout) = runs.remove(0);
3526 let (second_text, second_layout) = runs.remove(0);
3527 let snapshot = plain_snapshot(
3528 first_layout.position_for_index(1).unwrap(),
3529 second_layout.position_for_index(2).unwrap(),
3530 );
3531 cx.update(|cx| {
3532 let mut selection_state = WindowSelectionState::default();
3533 let participant = FakeParticipant::new("", cx);
3534 participant.register(
3535 &mut selection_state,
3536 0.,
3537 TextSelectionScopeId::default(),
3538 0,
3539 cx,
3540 );
3541 participant.selection.0.update(cx, |state, cx| {
3542 state.set_snapshot(Some(snapshot), cx);
3543 state.update_runs(&[
3544 text_run(1, first_text, first_layout),
3545 text_run(0, second_text, second_layout),
3546 ]);
3547 });
3548
3549 assert_eq!(selection_state.selected_text(cx), "twne");
3550 });
3551 }
3552
3553 #[gpui::test]
3554 fn plain_projection_safely_rejects_a_text_layout_length_mismatch(cx: &mut TestAppContext) {
3555 let (_, layout) = laid_out_runs(&["short"], cx).pop().unwrap();
3556 let start = layout.position_for_index(0).unwrap();
3557 let end = layout.position_for_index(5).unwrap();
3558 let states = project_ranges(
3559 Some(plain_snapshot(start, end)),
3560 &[text_run(0, SharedString::from("longer"), layout)],
3561 );
3562
3563 assert_eq!(states.ranges(), &[None]);
3564 assert!(states.is_active());
3565 }
3566
3567 #[gpui::test]
3568 fn begin_update_and_end_publish_a_cross_participant_selection(cx: &mut TestAppContext) {
3569 cx.update(|cx| {
3570 let mut selection_state = WindowSelectionState::default();
3571 let first = FakeParticipant::new("first", cx);
3572 let second = FakeParticipant::new("second", cx);
3573 first.register(
3574 &mut selection_state,
3575 0.,
3576 TextSelectionScopeId::default(),
3577 0,
3578 cx,
3579 );
3580 second.register(
3581 &mut selection_state,
3582 20.,
3583 TextSelectionScopeId::default(),
3584 1,
3585 cx,
3586 );
3587
3588 selection_state.begin(point(px(1.), px(1.)), false, cx);
3589 selection_state.update(point(px(1.), px(25.)), cx);
3590 assert!(selection_state.has_selection(cx));
3591 assert_eq!(selection_state.selected_text(cx), "first\nsecond");
3592
3593 selection_state.end(cx);
3594 assert!(!selection_state.is_selecting());
3595 });
3596 }
3597
3598 #[gpui::test]
3599 fn shift_extension_keeps_its_original_anchor_when_reversed(cx: &mut TestAppContext) {
3600 cx.update(|cx| {
3601 let mut selection_state = WindowSelectionState::default();
3602 let participant = FakeParticipant::new("participant", cx);
3603 participant.register(
3604 &mut selection_state,
3605 0.,
3606 TextSelectionScopeId::default(),
3607 0,
3608 cx,
3609 );
3610
3611 selection_state.begin(point(px(2.), px(2.)), false, cx);
3612 selection_state.end(cx);
3613 selection_state.begin(point(px(8.), px(2.)), true, cx);
3614 selection_state.end(cx);
3615 let first_anchor = selection_state.snapshot().unwrap().anchor();
3616
3617 selection_state.begin(point(px(0.), px(2.)), true, cx);
3618 selection_state.end(cx);
3619 let reversed = selection_state.snapshot().unwrap();
3620 assert_eq!(reversed.anchor(), first_anchor);
3621 assert!(reversed.cursor().content_point().x < reversed.anchor().content_point().x);
3622 });
3623 }
3624
3625 #[gpui::test]
3626 fn content_key_resolver_runs_outside_the_window_state_lease(cx: &mut TestAppContext) {
3627 cx.update(|cx| {
3628 let state = cx.new(|_| WindowSelectionState::default());
3629 let participant = FakeParticipant::new("virtual", cx);
3630 let state_for_callback = state.clone();
3631 participant.selection.resolve_content_key_with(
3632 move |_, cx| {
3633 let _ = state_for_callback.read(cx).snapshot();
3634 Some(TextSelectionContentKey::new(7))
3635 },
3636 cx,
3637 );
3638 state.update(cx, |state, cx| {
3639 participant.register(state, 0., TextSelectionScopeId::default(), 0, cx);
3640 state.begin(point(px(1.), px(1.)), false, cx);
3641 state.update(point(px(8.), px(1.)), cx);
3642 });
3643
3644 WindowSelectionState::resolve_content_keys(&state, cx);
3645
3646 assert_eq!(
3647 state.read(cx).snapshot().unwrap().cursor().content_key(),
3648 Some(TextSelectionContentKey::new(7))
3649 );
3650 });
3651 }
3652
3653 #[gpui::test]
3654 fn active_dnd_does_not_move_a_text_selection_cursor(cx: &mut TestAppContext) {
3655 let window = cx.add_window(|_, cx| WindowSelectionView {
3656 selection: TextSelectionHandle::new("unused", cx),
3657 });
3658 window
3659 .update(cx, |_, window, cx| {
3660 let mut state = WindowSelectionState::default();
3661 let participant = FakeParticipant::new("participant", cx);
3662 participant.register(&mut state, 0., TextSelectionScopeId::default(), 0, cx);
3663 state.begin(point(px(1.), px(1.)), false, cx);
3664 let before = state.cursor.as_ref().unwrap().point;
3665 state.update_in_window_with_active_drag(point(px(80.), px(1.)), true, window, cx);
3666 assert_eq!(state.cursor.as_ref().unwrap().point, before);
3667 })
3668 .unwrap();
3669 }
3670
3671 #[gpui::test]
3672 fn shift_extension_falls_back_when_the_anchor_participant_was_swept(cx: &mut TestAppContext) {
3673 cx.update(|cx| {
3674 let mut selection_state = WindowSelectionState::default();
3675 let first = FakeParticipant::new("first", cx);
3676 let second = FakeParticipant::new("second", cx);
3677 first.register(
3678 &mut selection_state,
3679 0.,
3680 TextSelectionScopeId::default(),
3681 0,
3682 cx,
3683 );
3684 selection_state.begin(point(px(1.), px(1.)), false, cx);
3685 selection_state.update(point(px(8.), px(1.)), cx);
3686 selection_state.end(cx);
3687
3688 selection_state.finish_frame(cx);
3689 selection_state.finish_frame(cx);
3690 second.register(
3691 &mut selection_state,
3692 20.,
3693 TextSelectionScopeId::default(),
3694 1,
3695 cx,
3696 );
3697 selection_state.begin(point(px(1.), px(21.)), true, cx);
3698 selection_state.update(point(px(8.), px(21.)), cx);
3699 selection_state.end(cx);
3700
3701 assert_eq!(selection_state.selected_text(cx), "second");
3702 });
3703 }
3704
3705 #[gpui::test]
3706 fn scope_and_suppression_prevent_unrelated_participants_from_participating(
3707 cx: &mut TestAppContext,
3708 ) {
3709 cx.update(|cx| {
3710 let mut selection_state = WindowSelectionState::default();
3711 let base = FakeParticipant::new("base", cx);
3712 let modal = FakeParticipant::new("modal", cx);
3713 base.register(
3714 &mut selection_state,
3715 0.,
3716 TextSelectionScopeId::default(),
3717 0,
3718 cx,
3719 );
3720 modal.register(&mut selection_state, 20., TextSelectionScopeId(1), 1, cx);
3721
3722 selection_state.set_active_scope(TextSelectionScopeId(1), cx);
3723 selection_state.begin(point(px(1.), px(21.)), false, cx);
3724 selection_state.update(point(px(8.), px(21.)), cx);
3725 selection_state.end(cx);
3726 assert_eq!(selection_state.selected_text(cx), "modal");
3727
3728 selection_state.clear(cx);
3729 GlobalState::init(cx);
3730 GlobalState::suppress_text_selection(cx);
3731 selection_state.begin(point(px(1.), px(21.)), false, cx);
3732 selection_state.update(point(px(8.), px(21.)), cx);
3733 assert!(!selection_state.has_selection(cx));
3734 });
3735 }
3736
3737 #[gpui::test]
3738 fn dead_participants_are_pruned_and_empty_selection_falls_back_safely(cx: &mut TestAppContext) {
3739 let selection_state = cx.update(|cx| {
3740 let selection_state = cx.new(|_| WindowSelectionState::default());
3741 let participant = FakeParticipant::new("gone", cx);
3742 selection_state.update(cx, |selection_state, cx| {
3743 participant.register(selection_state, 0., TextSelectionScopeId::default(), 0, cx)
3744 });
3745 selection_state
3746 });
3747 cx.update(|cx| {
3748 selection_state.update(cx, |selection_state, cx| {
3749 selection_state.begin(point(px(1.), px(1.)), false, cx);
3750 selection_state.update(point(px(8.), px(1.)), cx);
3751 selection_state.end(cx);
3752
3753 assert_eq!(selection_state.selected_text(cx), "");
3754 assert!(!selection_state.has_selection(cx));
3755 });
3756 });
3757 }
3758
3759 #[gpui::test]
3760 fn text_selection_namespace_reports_copies_ends_and_clears_selection(cx: &mut TestAppContext) {
3761 let (view, cx) = cx.add_window_view(|_, cx| WindowSelectionView {
3762 selection: TextSelectionHandle::new("copied", cx),
3763 });
3764 cx.update(|window, cx| {
3765 let selection = view.read(cx).selection.clone();
3766 let selection_state = WindowSelectionState::ensure(window, cx);
3767 selection_state.update(cx, |selection_state, cx| {
3768 FakeParticipant { selection }.register(
3769 selection_state,
3770 0.,
3771 TextSelectionScopeId::default(),
3772 0,
3773 cx,
3774 );
3775 selection_state.begin(point(px(1.), px(1.)), false, cx);
3776 selection_state.update(point(px(8.), px(1.)), cx);
3777 });
3778
3779 assert!(TextSelection::has_selection(window, cx));
3780 assert_eq!(TextSelection::selected_text(window, cx), "copied");
3781 TextSelection::end(window, cx);
3782 assert!(TextSelection::has_selection(window, cx));
3783 TextSelection::clear(window, cx);
3784 assert!(!TextSelection::has_selection(window, cx));
3785 assert_eq!(TextSelection::selected_text(window, cx), "");
3786 });
3787 }
3788
3789 #[gpui::test]
3790 fn two_windows_isolate_selection_copy_clear_and_release_ownership(cx: &mut TestAppContext) {
3791 let first = cx.add_window(|_, cx| WindowOwnedSelectionView {
3792 selection: TextSelectionHandle::new("first", cx),
3793 });
3794 let second = cx.add_window(|_, cx| WindowOwnedSelectionView {
3795 selection: TextSelectionHandle::new("second", cx),
3796 });
3797 let first_selection = cx.update(|cx| first.read(cx).unwrap().selection.clone());
3798 let second_selection = cx.update(|cx| second.read(cx).unwrap().selection.clone());
3799
3800 let first_state = cx
3801 .update_window(*first, |_, window, cx| {
3802 let _ = window.draw(cx);
3803 first_selection.set_local_selection(true, cx);
3804 assert_eq!(TextSelection::selected_text(window, cx), "first");
3805 WindowSelectionState::existing(window, cx)
3806 .unwrap()
3807 .downgrade()
3808 })
3809 .unwrap();
3810 cx.update_window(*second, |_, window, cx| {
3811 let _ = window.draw(cx);
3812 second_selection.set_local_selection(true, cx);
3813 assert_eq!(TextSelection::selected_text(window, cx), "second");
3814 })
3815 .unwrap();
3816
3817 cx.update_window(*first, |_, window, cx| {
3818 TextSelection::clear(window, cx);
3819 assert_eq!(TextSelection::selected_text(window, cx), "");
3820 })
3821 .unwrap();
3822 cx.update_window(*second, |_, window, cx| {
3823 assert_eq!(TextSelection::selected_text(window, cx), "second");
3824 })
3825 .unwrap();
3826
3827 cx.update_window(*first, |_, window, _| window.remove_window())
3828 .unwrap();
3829 cx.run_until_parked();
3830
3831 assert!(first_state.upgrade().is_none());
3832 cx.update_window(*second, |_, window, cx| {
3833 assert_eq!(TextSelection::selected_text(window, cx), "second");
3834 })
3835 .unwrap();
3836 cx.update(|cx| {
3837 assert_eq!(cx.global::<SelectionStateRegistry>().0.len(), 1);
3838 });
3839 }
3840
3841 #[gpui::test]
3842 fn copy_callback_can_reenter_window_and_handle_selection(cx: &mut TestAppContext) {
3843 let (_, cx) = cx.add_window_view(|_, _| SelectionElementOnlyView);
3844 cx.update(|window, cx| {
3845 let _ = window.draw(cx);
3846 let state = WindowSelectionState::existing(window, cx).unwrap();
3847 let selection = TextSelectionHandle::new("fallback", cx);
3848 let state_for_copy = state.clone();
3849 let selection_for_copy = selection.clone();
3850 selection.copy_with(
3851 move |cx: &mut App| {
3852 state_for_copy.update(cx, |state, _| {
3853 assert!(state.snapshot().is_some());
3854 });
3855 assert!(selection_for_copy.snapshot(cx).is_some());
3856 selection_for_copy.set_fallback_copy_text("reentered", cx);
3857 "reentrant copy".to_string()
3858 },
3859 cx,
3860 );
3861 state.update(cx, |state, cx| {
3862 FakeParticipant {
3863 selection: selection.clone(),
3864 }
3865 .register(state, 0., TextSelectionScopeId::default(), 0, cx);
3866 state.begin(point(px(1.), px(1.)), false, cx);
3867 state.update(point(px(8.), px(1.)), cx);
3868 state.end(cx);
3869 });
3870
3871 assert_eq!(TextSelection::selected_text(window, cx), "reentrant copy");
3872 });
3873 }
3874
3875 #[gpui::test]
3876 fn cross_participant_selection_excludes_participants_outside_its_document_interval(
3877 cx: &mut TestAppContext,
3878 ) {
3879 cx.update(|cx| {
3880 let mut selection_state = WindowSelectionState::default();
3881 let first = FakeParticipant::new("first", cx);
3882 let second = FakeParticipant::new("second", cx);
3883 let third = FakeParticipant::new("third", cx);
3884 first.register(
3885 &mut selection_state,
3886 0.,
3887 TextSelectionScopeId::default(),
3888 0,
3889 cx,
3890 );
3891 second.register(
3892 &mut selection_state,
3893 20.,
3894 TextSelectionScopeId::default(),
3895 1,
3896 cx,
3897 );
3898 third.register(
3899 &mut selection_state,
3900 40.,
3901 TextSelectionScopeId::default(),
3902 2,
3903 cx,
3904 );
3905
3906 selection_state.begin(point(px(1.), px(1.)), false, cx);
3907 selection_state.update(point(px(1.), px(25.)), cx);
3908 selection_state.end(cx);
3909
3910 assert_eq!(selection_state.selected_text(cx), "first\nsecond");
3911 assert!(third.selection.snapshot(cx).is_none());
3912 });
3913 }
3914
3915 #[gpui::test]
3916 fn changing_scope_clears_the_previous_scope_selection(cx: &mut TestAppContext) {
3917 cx.update(|cx| {
3918 let mut selection_state = WindowSelectionState::default();
3919 let base = FakeParticipant::new("base", cx);
3920 let modal = FakeParticipant::new("modal", cx);
3921 base.register(
3922 &mut selection_state,
3923 0.,
3924 TextSelectionScopeId::default(),
3925 0,
3926 cx,
3927 );
3928 modal.register(
3929 &mut selection_state,
3930 20.,
3931 TextSelectionScopeId::from_raw(1),
3932 1,
3933 cx,
3934 );
3935
3936 selection_state.begin(point(px(1.), px(1.)), false, cx);
3937 selection_state.update(point(px(8.), px(1.)), cx);
3938 selection_state.end(cx);
3939 selection_state.set_active_scope(TextSelectionScopeId::from_raw(1), cx);
3940
3941 assert!(!selection_state.has_selection(cx));
3942 assert!(base.selection.snapshot(cx).is_none());
3943 });
3944 }
3945
3946 #[gpui::test]
3947 fn blank_only_drag_never_publishes_or_copies_selection(cx: &mut TestAppContext) {
3948 cx.update(|cx| {
3949 let mut selection_state = WindowSelectionState::default();
3950 let participant = FakeParticipant::new("participant", cx);
3951 participant.register(
3952 &mut selection_state,
3953 0.,
3954 TextSelectionScopeId::default(),
3955 0,
3956 cx,
3957 );
3958
3959 selection_state.begin(point(px(200.), px(1.)), false, cx);
3960 selection_state.update(point(px(200.), px(8.)), cx);
3961 selection_state.end(cx);
3962
3963 assert!(!selection_state.has_selection(cx));
3964 assert_eq!(selection_state.selected_text(cx), "");
3965 assert!(participant.selection.snapshot(cx).is_none());
3966 });
3967 }
3968
3969 #[gpui::test]
3970 fn stale_live_participants_are_removed_when_the_next_frame_begins(cx: &mut TestAppContext) {
3971 cx.update(|cx| {
3972 let mut selection_state = WindowSelectionState::default();
3973 let participant = FakeParticipant::new("stale", cx);
3974 participant.register(
3975 &mut selection_state,
3976 0.,
3977 TextSelectionScopeId::default(),
3978 0,
3979 cx,
3980 );
3981 selection_state.begin(point(px(1.), px(1.)), false, cx);
3982 selection_state.update(point(px(8.), px(1.)), cx);
3983 selection_state.end(cx);
3984
3985 selection_state.finish_frame(cx);
3986 selection_state.finish_frame(cx);
3987 assert_eq!(selection_state.selected_text(cx), "");
3988 assert!(participant.selection.snapshot(cx).is_none());
3989 });
3990 }
3991
3992 #[gpui::test]
3993 fn clear_stops_anchor_auto_scroll_before_discarding_the_anchor(cx: &mut TestAppContext) {
3994 let commands = Rc::new(RefCell::new(Vec::new()));
3995 let observed = commands.clone();
3996 let (mut selection_state, participant) = cx.update(|cx| {
3997 let selection_state = WindowSelectionState::default();
3998 let participant = FakeParticipant::new("scroll", cx);
3999 participant
4000 .selection
4001 .subscribe(
4002 move |event, _| {
4003 if let TextSelectionEvent::AutoScroll(delta) = event {
4004 observed.borrow_mut().push(*delta);
4005 }
4006 },
4007 cx,
4008 )
4009 .detach();
4010 (selection_state, participant)
4011 });
4012 cx.run_until_parked();
4013 cx.update(|cx| {
4014 participant.register(
4015 &mut selection_state,
4016 0.,
4017 TextSelectionScopeId::default(),
4018 0,
4019 cx,
4020 );
4021
4022 selection_state.begin(point(px(1.), px(1.)), false, cx);
4023 selection_state.update(point(px(1.), px(25.)), cx);
4024 selection_state.clear(cx);
4025 });
4026 cx.run_until_parked();
4027 assert!(commands.borrow().iter().any(Option::is_some));
4028 assert_eq!(commands.borrow().last(), Some(&None));
4029 }
4030
4031 #[gpui::test]
4032 fn drag_auto_scroll_stops_when_the_content_mask_collapses(cx: &mut TestAppContext) {
4033 let window = cx.add_window(|_, cx| WindowSelectionView {
4034 selection: TextSelectionHandle::new("unused", cx),
4035 });
4036 window
4037 .update(cx, |_, window, cx| {
4038 let state = cx.new(|_| WindowSelectionState::default());
4039 let participant = FakeParticipant::new("participant", cx);
4040 state.update(cx, |state, cx| {
4041 participant.register(state, 0., TextSelectionScopeId::default(), 0, cx);
4042 state.begin(point(px(1.), px(1.)), false, cx);
4043 });
4044 // The scrollable ancestor got clipped away mid-drag, so the
4045 // refreshed registration carries a collapsed content mask.
4046 let collapsed = Bounds::new(point(px(0.), px(0.)), size(px(100.), px(0.)));
4047 state.update(cx, |state, cx| {
4048 state.register_participant(
4049 participant.selection.clone(),
4050 TextSelectionRegistration::new(
4051 Hitbox {
4052 id: HitboxId::placeholder(),
4053 bounds: collapsed,
4054 content_mask: ContentMask { bounds: collapsed },
4055 behavior: HitboxBehavior::Normal,
4056 },
4057 collapsed,
4058 )
4059 .with_text_bounds(vec![collapsed]),
4060 cx,
4061 );
4062 state.update_in_window(point(px(1.), px(50.)), window, cx);
4063 assert!(!state.auto_scroll.is_active());
4064 assert!(state.auto_scroll.last_drag_position.is_none());
4065 });
4066 })
4067 .unwrap();
4068 }
4069
4070 #[gpui::test]
4071 fn pointer_moves_after_a_click_do_not_auto_scroll(cx: &mut TestAppContext) {
4072 let window = cx.add_window(|_, cx| WindowSelectionView {
4073 selection: TextSelectionHandle::new("unused", cx),
4074 });
4075 window
4076 .update(cx, |_, window, cx| {
4077 let state = cx.new(|_| WindowSelectionState::default());
4078 let participant = FakeParticipant::new("participant", cx);
4079 state.update(cx, |state, cx| {
4080 participant.register(state, 0., TextSelectionScopeId::default(), 0, cx);
4081 // A click on text keeps its anchor so shift-click can extend it.
4082 state.begin(point(px(1.), px(1.)), false, cx);
4083 state.end(cx);
4084 assert!(state.anchor.is_some());
4085
4086 state.update_in_window(point(px(1.), px(50.)), window, cx);
4087 assert!(!state.auto_scroll.is_active());
4088 });
4089 })
4090 .unwrap();
4091 }
4092
4093 #[gpui::test]
4094 fn proxy_endpoints_break_equal_position_ties_by_document_order(cx: &mut TestAppContext) {
4095 cx.update(|cx| {
4096 let mut selection_state = WindowSelectionState::default();
4097 let later = FakeParticipant::new("later", cx);
4098 let earlier = FakeParticipant::new("earlier", cx);
4099 later.register(
4100 &mut selection_state,
4101 0.,
4102 TextSelectionScopeId::default(),
4103 2,
4104 cx,
4105 );
4106 earlier.register(
4107 &mut selection_state,
4108 0.,
4109 TextSelectionScopeId::default(),
4110 1,
4111 cx,
4112 );
4113
4114 selection_state.begin(point(px(1.), px(1.)), false, cx);
4115 selection_state.update(point(px(200.), px(25.)), cx);
4116 let endpoint = selection_state.snapshot().unwrap().cursor();
4117
4118 assert_eq!(endpoint.entity_id(), Some(earlier.selection.entity_id()));
4119 });
4120 }
4121
4122 #[gpui::test]
4123 fn equal_area_hovered_participants_break_ties_by_document_order(cx: &mut TestAppContext) {
4124 cx.update(|cx| {
4125 for _ in 0..64 {
4126 let mut selection_state = WindowSelectionState::default();
4127 let later = FakeParticipant::new("later", cx);
4128 let earliest = FakeParticipant::new("earliest", cx);
4129 let middle = FakeParticipant::new("middle", cx);
4130 later.register(
4131 &mut selection_state,
4132 0.,
4133 TextSelectionScopeId::default(),
4134 30,
4135 cx,
4136 );
4137 earliest.register(
4138 &mut selection_state,
4139 0.,
4140 TextSelectionScopeId::default(),
4141 10,
4142 cx,
4143 );
4144 middle.register(
4145 &mut selection_state,
4146 0.,
4147 TextSelectionScopeId::default(),
4148 20,
4149 cx,
4150 );
4151
4152 selection_state.begin(point(px(1.), px(1.)), false, cx);
4153 selection_state.update(point(px(8.), px(1.)), cx);
4154
4155 assert_eq!(
4156 selection_state.snapshot().unwrap().anchor().entity_id(),
4157 Some(earliest.selection.entity_id())
4158 );
4159 }
4160 });
4161 }
4162
4163 #[gpui::test]
4164 fn text_selection_namespace_is_a_safe_no_op_until_the_element_is_rendered(
4165 cx: &mut TestAppContext,
4166 ) {
4167 let (_, cx) = cx.add_window_view(|_, cx| WindowSelectionView {
4168 selection: TextSelectionHandle::new("not enabled", cx),
4169 });
4170 cx.update(|window, cx| {
4171 assert!(!TextSelection::has_selection(window, cx));
4172 assert_eq!(TextSelection::selected_text(window, cx), "");
4173 TextSelection::clear(window, cx);
4174 TextSelection::end(window, cx);
4175 assert!(!TextSelection::has_selection(window, cx));
4176 });
4177 }
4178
4179 #[gpui::test]
4180 fn unit_selection_element_supports_scope_and_registration_on_the_first_frame(
4181 cx: &mut TestAppContext,
4182 ) {
4183 let (view, cx) = cx.add_window_view(|_, cx| FirstFrameScopedSelectionView {
4184 selection: TextSelectionHandle::new("first frame", cx),
4185 });
4186 let selection = cx.update(|_, cx| view.read(cx).selection.clone());
4187
4188 cx.update(|window, cx| {
4189 let _ = window.draw(cx);
4190 let state = WindowSelectionState::existing(window, cx).unwrap();
4191 assert_eq!(
4192 state.read(cx).active_scope,
4193 TextSelectionScopeId::from_raw(23)
4194 );
4195 assert!(
4196 state
4197 .read(cx)
4198 .participants
4199 .contains_key(&selection.entity_id())
4200 );
4201 });
4202 }
4203
4204 #[gpui::test]
4205 fn lazy_registration_does_not_enable_queries_without_the_element(cx: &mut TestAppContext) {
4206 let (_, cx) = cx.add_window_view(|_, cx| WindowSelectionView {
4207 selection: TextSelectionHandle::new("registered", cx),
4208 });
4209 cx.update(|window, cx| {
4210 let selection = TextSelectionHandle::new("registered", cx);
4211 selection.set_local_selection(true, cx);
4212 let bounds = Bounds::new(point(px(0.), px(0.)), size(px(100.), px(20.)));
4213 let hitbox = Hitbox {
4214 id: HitboxId::placeholder(),
4215 bounds,
4216 content_mask: ContentMask { bounds },
4217 behavior: HitboxBehavior::Normal,
4218 };
4219 selection.register(
4220 TextSelectionRegistration::new(hitbox, bounds).with_text_bounds(vec![bounds]),
4221 window,
4222 cx,
4223 );
4224 assert_eq!(TextSelection::selected_text(window, cx), "");
4225 assert!(!TextSelection::has_selection(window, cx));
4226 TextSelection::clear(window, cx);
4227 assert_eq!(TextSelection::selected_text(window, cx), "");
4228 });
4229 }
4230
4231 #[gpui::test]
4232 fn retained_selection_state_releases_and_does_not_resurrect_selection(cx: &mut TestAppContext) {
4233 let (view, cx) = cx.add_window_view(|_, cx| ToggleSelectionElementView {
4234 enabled: true,
4235 selection: TextSelectionHandle::new("local", cx),
4236 });
4237 let selection = cx.update(|_, cx| view.read(cx).selection.clone());
4238 cx.update(|window, cx| {
4239 let _ = window.draw(cx);
4240 selection.set_local_selection(true, cx);
4241 assert!(TextSelection::has_selection(window, cx));
4242
4243 window.simulate_next_frame(cx);
4244 assert!(TextSelection::has_selection(window, cx));
4245 let _ = window.draw(cx);
4246 assert!(TextSelection::has_selection(window, cx));
4247 });
4248 view.update(cx, |view, cx| {
4249 view.enabled = false;
4250 cx.notify();
4251 });
4252 cx.update(|window, cx| {
4253 let _ = window.draw(cx);
4254 });
4255 cx.update(|window, cx| {
4256 window.simulate_next_frame(cx);
4257 });
4258 cx.update(|window, cx| {
4259 window.simulate_next_frame(cx);
4260 });
4261 cx.run_until_parked();
4262 cx.update(|window, cx| {
4263 assert!(!TextSelection::has_selection(window, cx));
4264 assert_eq!(TextSelection::selected_text(window, cx), "");
4265 assert!(!selection.has_local_selection(cx));
4266 TextSelection::clear(window, cx);
4267 });
4268
4269 view.update(cx, |view, cx| {
4270 view.enabled = true;
4271 cx.notify();
4272 });
4273 cx.update(|window, cx| {
4274 let _ = window.draw(cx);
4275 assert!(!TextSelection::has_selection(window, cx));
4276 assert_eq!(TextSelection::selected_text(window, cx), "");
4277 });
4278 }
4279
4280 #[gpui::test]
4281 fn mounted_selection_element_does_not_keep_an_idle_frame_queue_alive(cx: &mut TestAppContext) {
4282 let (_, cx) = cx.add_window_view(|_, _| SelectionElementOnlyView);
4283 cx.update(|window, cx| {
4284 let _ = window.draw(cx);
4285 assert_eq!(window.simulate_next_frame(cx), 0);
4286 assert_eq!(window.simulate_next_frame(cx), 0);
4287 assert!(live_text_selection_state(window, cx).is_some());
4288 });
4289 }
4290
4291 #[gpui::test]
4292 fn selection_element_initializes_suppression_and_respects_bubble_suppression(
4293 cx: &mut TestAppContext,
4294 ) {
4295 let (_, cx) = cx.add_window_view(|_, _| SelectionElementOnlyView);
4296 cx.update(|window, cx| {
4297 let _ = window.draw(cx);
4298 });
4299 cx.simulate_mouse_down(
4300 point(px(1.), px(1.)),
4301 MouseButton::Left,
4302 gpui::Modifiers::default(),
4303 );
4304 cx.simulate_mouse_up(
4305 point(px(1.), px(1.)),
4306 MouseButton::Left,
4307 gpui::Modifiers::default(),
4308 );
4309 cx.update(|window, cx| {
4310 assert!(GlobalState::is_text_selection_suppressed(cx));
4311 assert!(!TextSelection::has_selection(window, cx));
4312 });
4313 }
4314
4315 #[gpui::test]
4316 fn frame_sweep_keeps_a_participant_registered_before_the_selection_element_paints(
4317 cx: &mut TestAppContext,
4318 ) {
4319 cx.update(|cx| {
4320 let mut selection_state = WindowSelectionState::default();
4321 let participant = FakeParticipant::new("painted first", cx);
4322 participant.register(
4323 &mut selection_state,
4324 0.,
4325 TextSelectionScopeId::default(),
4326 0,
4327 cx,
4328 );
4329 selection_state.begin(point(px(1.), px(1.)), false, cx);
4330 selection_state.update(point(px(8.), px(1.)), cx);
4331 selection_state.end(cx);
4332
4333 selection_state.finish_frame(cx);
4334
4335 assert_eq!(selection_state.selected_text(cx), "painted first");
4336 assert!(participant.selection.snapshot(cx).is_some());
4337 });
4338 }
4339
4340 #[gpui::test]
4341 fn two_selection_elements_schedule_only_one_post_frame_sweep(cx: &mut TestAppContext) {
4342 let (view, cx) = cx.add_window_view(|_, cx| DoubleSelectionElementView {
4343 selection: TextSelectionHandle::new("once", cx),
4344 });
4345 cx.update(|window, cx| {
4346 let selection_state = WindowSelectionState::ensure(window, cx);
4347 let selection = view.read(cx).selection.clone();
4348 selection_state.update(cx, |selection_state, cx| {
4349 FakeParticipant { selection }.register(
4350 selection_state,
4351 0.,
4352 TextSelectionScopeId::default(),
4353 0,
4354 cx,
4355 );
4356 selection_state.begin(point(px(1.), px(1.)), false, cx);
4357 selection_state.update(point(px(8.), px(1.)), cx);
4358 selection_state.end(cx);
4359 });
4360
4361 let _ = window.draw(cx);
4362 window.simulate_next_frame(cx);
4363
4364 let items = selection_state.read(cx).copy_items(cx);
4365 assert_eq!(resolve_copy_items(items, cx), "once");
4366 });
4367 }
4368
4369 #[gpui::test]
4370 fn duplicate_selection_elements_gate_real_pointer_gestures_and_reentrant_clear(
4371 cx: &mut TestAppContext,
4372 ) {
4373 let (view, cx) = cx.add_window_view(|_, cx| DoubleSelectionElementView {
4374 selection: TextSelectionHandle::new("once", cx),
4375 });
4376 let clear_count = Rc::new(Cell::new(0));
4377 cx.update(|window, cx| {
4378 let state = WindowSelectionState::ensure(window, cx);
4379 let state_for_clear = state.clone();
4380 let count = clear_count.clone();
4381 let selection = view.read(cx).selection.clone();
4382 selection
4383 .subscribe(
4384 move |event, cx| {
4385 if matches!(event, TextSelectionEvent::Cleared) {
4386 count.set(count.get() + 1);
4387 let _ = state_for_clear.read(cx).snapshot();
4388 }
4389 },
4390 cx,
4391 )
4392 .detach();
4393 let _ = window.draw(cx);
4394 });
4395
4396 cx.simulate_mouse_down(
4397 point(px(10.), px(10.)),
4398 MouseButton::Left,
4399 gpui::Modifiers::default(),
4400 );
4401 cx.simulate_mouse_up(
4402 point(px(10.), px(10.)),
4403 MouseButton::Left,
4404 gpui::Modifiers::default(),
4405 );
4406 cx.simulate_mouse_down(
4407 point(px(70.), px(10.)),
4408 MouseButton::Left,
4409 gpui::Modifiers {
4410 shift: true,
4411 ..Default::default()
4412 },
4413 );
4414 cx.simulate_mouse_up(
4415 point(px(70.), px(10.)),
4416 MouseButton::Left,
4417 gpui::Modifiers::default(),
4418 );
4419 cx.update(|window, cx| assert!(TextSelection::has_selection(window, cx)));
4420
4421 cx.simulate_mouse_down(
4422 point(px(15.), px(10.)),
4423 MouseButton::Left,
4424 gpui::Modifiers::default(),
4425 );
4426 cx.simulate_mouse_move(
4427 point(px(85.), px(10.)),
4428 Some(MouseButton::Left),
4429 gpui::Modifiers::default(),
4430 );
4431 cx.simulate_mouse_up(
4432 point(px(85.), px(10.)),
4433 MouseButton::Left,
4434 gpui::Modifiers::default(),
4435 );
4436 cx.update(|window, cx| assert!(TextSelection::has_selection(window, cx)));
4437 assert_eq!(clear_count.get(), 3);
4438 }
4439
4440 #[gpui::test]
4441 fn mouse_move_without_button_ends_a_gesture_whose_mouse_up_was_missed(cx: &mut TestAppContext) {
4442 let (view, cx) = cx.add_window_view(|_, cx| DoubleSelectionElementView {
4443 selection: TextSelectionHandle::new("once", cx),
4444 });
4445 cx.update(|window, cx| {
4446 let _ = window.draw(cx);
4447 });
4448
4449 cx.simulate_mouse_down(
4450 point(px(15.), px(10.)),
4451 MouseButton::Left,
4452 gpui::Modifiers::default(),
4453 );
4454 cx.simulate_mouse_move(
4455 point(px(40.), px(10.)),
4456 Some(MouseButton::Left),
4457 gpui::Modifiers::default(),
4458 );
4459 cx.update(|window, cx| {
4460 let state = WindowSelectionState::ensure(window, cx);
4461 assert!(state.read(cx).is_selecting());
4462 });
4463
4464 cx.simulate_mouse_move(point(px(60.), px(10.)), None, gpui::Modifiers::default());
4465 let selection_after_release = cx.update(|window, cx| {
4466 let state = WindowSelectionState::ensure(window, cx);
4467 assert!(!state.read(cx).is_selecting());
4468 TextSelection::selected_text(window, cx)
4469 });
4470
4471 cx.simulate_mouse_move(point(px(85.), px(10.)), None, gpui::Modifiers::default());
4472 cx.update(|window, cx| {
4473 assert_eq!(
4474 TextSelection::selected_text(window, cx),
4475 selection_after_release
4476 );
4477 });
4478 let _ = view;
4479 }
4480
4481 #[gpui::test]
4482 fn selection_layer_handles_real_double_and_triple_click_events(cx: &mut TestAppContext) {
4483 let (text, layout) = laid_out_runs(&["alpha beta"], cx).pop().unwrap();
4484 let (view, cx) = cx.add_window_view(|_, cx| DoubleSelectionElementView {
4485 selection: TextSelectionHandle::new("", cx),
4486 });
4487 cx.update(|window, cx| {
4488 let _ = window.draw(cx);
4489 let selection = view.read(cx).selection.clone();
4490 selection.resolve_content_key_with(|_, _| Some(TextSelectionContentKey::new(17)), cx);
4491 selection.update_runs(&[text_run(0, text.clone(), layout.clone())], cx);
4492 });
4493
4494 let position = layout.position_for_index(7).unwrap();
4495 cx.simulate_event(MouseDownEvent {
4496 position,
4497 modifiers: gpui::Modifiers::default(),
4498 button: MouseButton::Left,
4499 click_count: 2,
4500 first_mouse: false,
4501 });
4502 cx.simulate_event(MouseUpEvent {
4503 position,
4504 modifiers: gpui::Modifiers::default(),
4505 button: MouseButton::Left,
4506 click_count: 2,
4507 });
4508 cx.update(|window, cx| {
4509 let selection = view.read(cx).selection.clone();
4510 selection.update_runs(&[text_run(0, text.clone(), layout.clone())], cx);
4511 assert_eq!(TextSelection::selected_text(window, cx), "beta");
4512 let snapshot = selection.snapshot(cx).unwrap();
4513 assert_eq!(
4514 snapshot.anchor().content_key(),
4515 Some(TextSelectionContentKey::new(17))
4516 );
4517 assert_eq!(
4518 snapshot.cursor().content_key(),
4519 Some(TextSelectionContentKey::new(17))
4520 );
4521 });
4522
4523 cx.simulate_event(MouseDownEvent {
4524 position,
4525 modifiers: gpui::Modifiers::default(),
4526 button: MouseButton::Left,
4527 click_count: 3,
4528 first_mouse: false,
4529 });
4530 cx.simulate_event(MouseUpEvent {
4531 position,
4532 modifiers: gpui::Modifiers::default(),
4533 button: MouseButton::Left,
4534 click_count: 3,
4535 });
4536 cx.update(|window, cx| {
4537 let selection = view.read(cx).selection.clone();
4538 selection.update_runs(&[text_run(0, text, layout)], cx);
4539 assert_eq!(TextSelection::selected_text(window, cx), "alpha beta");
4540 });
4541 }
4542}