gpui-base 0.6.2

Behavior, interaction, and infrastructure foundations for GPUI applications.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
//! Touch selection for the input engine: long press, grab handles, edit menu.
//!
//! A long press selects the word under the finger and keeps following the
//! finger while it stays down, like the double-click-and-drag it stands in
//! for. Releasing opens the edit menu; from then on the selection carries a
//! handle at each end which the styled layer draws and which drags through
//! [`InputBaseState::begin_edge_drag`].
//!
//! The touch selection remembers the range it made. As soon as the selection
//! is something else — the caret moved, text was typed, another cursor was
//! added — the handles and the menu are gone, without anyone having to hide
//! them. Only the touch gesture and the menu's own actions move the range and
//! carry the touch selection along.

use gpui::{Context, LongPressEvent, Pixels, Point, TouchPhase, Window, point};

use super::{InputBaseState, InputModeKind};
use crate::touch_selection::{
    EdgeDrag, SelectionEdge, TouchSelectionSnapshot, caret_in_view, caret_line_box,
};

/// The selection a touch gesture made, and what it is doing now.
#[derive(Debug, Default)]
pub(super) struct TouchSelection {
    /// The range the gesture left; `None` when no touch selection is live.
    range: Option<(usize, usize)>,
    menu_open: bool,
    drag: Option<EdgeDrag>,
}

impl<M: InputModeKind> InputBaseState<M> {
    /// The live touch selection, laid out for the handles and the edit menu.
    ///
    /// `None` when the last touch selection has since changed into something
    /// else, or when its ends are not laid out (scrolled out of view).
    pub fn touch_selection(&self) -> Option<TouchSelectionSnapshot> {
        let range = self.touch_selection.range?;
        let selection = self.active_selection();
        if (selection.start, selection.end) != range {
            return None;
        }

        let layout = self.last_layout.as_ref()?;
        let line_height = layout.line_height;
        let laid_out = layout.visible_range_offset.clone();
        let origin = self.last_bounds?.origin;
        let viewport = self.input_bounds;
        // An end scrolled out of the input gets no handle. Its line may not
        // even be laid out; it then stands just outside the viewport on its
        // side, which is all the other end's drag needs to know about it.
        let caret_box = |offset: usize, stand_in_y: Pixels| {
            let (_, _, position) = self.line_and_position_for_offset(offset);
            match position.filter(|_| laid_out.contains(&offset) || laid_out.end == offset) {
                Some(position) => caret_line_box(origin + position, line_height),
                None => caret_line_box(point(viewport.left(), stand_in_y), line_height),
            }
        };
        let start = caret_box(range.0, viewport.top() - line_height);
        let end = caret_box(range.1, viewport.bottom());
        Some(
            TouchSelectionSnapshot::new(start, end)
                .with_edge_visible(SelectionEdge::Start, caret_in_view(start, viewport))
                .with_edge_visible(SelectionEdge::End, caret_in_view(end, viewport))
                .with_menu_open(self.touch_selection.menu_open)
                .with_dragging(self.touch_selection.drag.map(|drag| drag.edge())),
        )
    }

    /// Records the current selection as the one the touch gesture made.
    fn retain_touch_selection(&mut self) {
        let selection = self.active_selection();
        self.touch_selection.range = Some((selection.start, selection.end));
    }

    /// Keeps the current selection as a touch selection, with the menu open
    /// over it: what a double tap's word selection asks for.
    pub(super) fn keep_touch_selection(&mut self, cx: &mut Context<Self>) {
        self.retain_touch_selection();
        self.touch_selection.menu_open = true;
        self.touch_selection.drag = None;
        cx.notify();
    }

    /// Drops the handles and the edit menu. Called where the selection is
    /// about to be moved by something other than the gesture.
    pub(super) fn dismiss_touch_selection(&mut self, cx: &mut Context<Self>) {
        if self.touch_selection.range.is_none() {
            return;
        }
        self.touch_selection = TouchSelection::default();
        cx.notify();
    }

    /// Closes the edit menu and keeps the selection with its handles.
    ///
    /// The menu's own actions end here: Copy has done its work, Select All
    /// reopens the menu over the new range through [`Self::select_all`].
    pub fn close_edit_menu(&mut self, cx: &mut Context<Self>) {
        if !self.touch_selection.menu_open {
            return;
        }
        self.touch_selection.menu_open = false;
        cx.notify();
    }

    /// Reopens the edit menu when `position` is on the touch selection, the
    /// way a tap on selected text asks for it. Returns whether it did.
    pub(super) fn reopen_edit_menu_at(
        &mut self,
        position: Point<Pixels>,
        cx: &mut Context<Self>,
    ) -> bool {
        let Some(snapshot) = self.touch_selection() else {
            return false;
        };
        if snapshot.is_empty() {
            return false;
        }
        let (offset, _, _) = self.resolve_mouse_position(position);
        let selection = self.active_selection();
        if offset <= selection.start || offset >= selection.end {
            return false;
        }
        self.touch_selection.menu_open = true;
        cx.notify();
        true
    }

    /// Steps the menu aside while the content scrolls under a finger, and
    /// brings it back over the handles once the finger lifts.
    pub(super) fn edit_menu_on_scroll(&mut self, phase: TouchPhase, cx: &mut Context<Self>) {
        if self.touch_selection.range.is_none() {
            return;
        }
        match phase {
            TouchPhase::Ended | TouchPhase::Cancelled => {
                if !self.touch_selection.menu_open {
                    self.touch_selection.menu_open = true;
                    cx.notify();
                }
            }
            _ => self.close_edit_menu(cx),
        }
    }

    /// Selects everything and, when a touch selection is live, keeps the
    /// handles and the edit menu over the new range.
    pub fn select_all_from_edit_menu(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        let touch = self.touch_selection.range.is_some();
        self.select_all(window, cx);
        if touch {
            self.retain_touch_selection();
            self.touch_selection.menu_open = true;
            cx.notify();
        }
    }

    /// Handles one phase of a long press inside the input.
    ///
    /// Returns whether the started phase was claimed; the caller then captures
    /// the gesture so the moves keep coming even after the finger leaves the
    /// input.
    pub(super) fn on_long_press(
        &mut self,
        event: &LongPressEvent,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> bool {
        match event.phase {
            TouchPhase::Started => {
                if self.disabled {
                    return false;
                }
                if !self.focus_handle.is_focused(window) {
                    window.focus(&self.focus_handle, cx);
                }
                // The input selects on its own; keep the window text selection
                // from starting a drag under it.
                crate::GlobalState::suppress_text_selection(cx);
                self.undo_manager.break_transaction_coalescing();
                M::clear_inline_completion(self, cx);
                self.touch_selection = TouchSelection::default();

                let (offset, line_end_affinity, _) =
                    self.resolve_mouse_position(event.start_position);
                self.selections.remove_all_but_active();
                self.set_cursor_to(offset);
                self.select_word(offset, window, cx);
                let pressed_word = !self.active_selection().is_empty()
                    && !self.selected_text().chars().all(char::is_whitespace);
                if !pressed_word {
                    // Whitespace or an empty field: the press places the caret,
                    // and the menu offers Paste and Select All.
                    self.move_to_with_affinity(offset, None, line_end_affinity, cx);
                    self.selected_word_range = None;
                }
                self.selecting = true;
                self.retain_touch_selection();
                cx.notify();
                true
            }
            TouchPhase::Moved => {
                let (offset, line_end_affinity, _) = self.resolve_mouse_position(event.position);
                if self.selected_word_range.is_some() {
                    // The press took a word; the sweep grows it a word at a time.
                    self.select_to_with_affinity(offset, line_end_affinity, cx);
                } else {
                    // The press placed the caret; the sweep carries it.
                    self.move_to_with_affinity(offset, None, line_end_affinity, cx);
                }
                self.retain_touch_selection();
                true
            }
            TouchPhase::Ended | TouchPhase::Cancelled => {
                self.selecting = false;
                self.selected_word_range = None;
                if self.touch_selection.range.is_some() {
                    self.touch_selection.menu_open = true;
                }
                cx.notify();
                true
            }
        }
    }

    /// Starts dragging one end of the touch selection from `finger`.
    ///
    /// The other end stays where it is. The menu closes for the duration of the
    /// drag and reopens on [`Self::end_edge_drag`].
    pub fn begin_edge_drag(
        &mut self,
        edge: SelectionEdge,
        finger: Point<Pixels>,
        cx: &mut Context<Self>,
    ) {
        let Some(snapshot) = self.touch_selection() else {
            return;
        };
        self.undo_manager.break_transaction_coalescing();
        self.selected_word_range = None;
        self.active_selection_mut().reversed = edge == SelectionEdge::Start;
        self.touch_selection.drag = Some(EdgeDrag::begin(edge, snapshot.edge(edge), finger));
        self.touch_selection.menu_open = false;
        cx.notify();
    }

    /// Moves the dragged end to the text under `finger`.
    ///
    /// Past the top or bottom of a multi-line input the content scrolls under
    /// the finger, so the end can reach text that was out of view.
    pub fn update_edge_drag(&mut self, finger: Point<Pixels>, cx: &mut Context<Self>) {
        let Some(drag) = self.touch_selection.drag else {
            return;
        };
        let position = drag.text_position(finger);
        self.extend_edge_drag_to(position, cx);

        if self.is_single_line() {
            return;
        }
        self.auto_scroll.last_drag_position = Some(position);
        let delta = crate::AutoScroll::compute_delta(position.y, self.input_bounds);
        // Input's ScrollHandle uses negative-y-is-down; negate the positive-towards-bottom delta.
        let scroll_delta = delta.map(|delta| -delta);
        self.auto_scroll.set(scroll_delta, cx, |delta, state, cx| {
            let current = state.scroll_handle.offset();
            state.update_scroll_offset(Some(point(current.x, current.y + delta)), cx);
            if let Some(position) = state.auto_scroll.last_drag_position {
                state.extend_edge_drag_to(position, cx);
            }
        });
    }

    fn extend_edge_drag_to(&mut self, position: Point<Pixels>, cx: &mut Context<Self>) {
        if self.touch_selection.drag.is_none() {
            return;
        }
        let (offset, line_end_affinity, _) = self.resolve_mouse_position(position);
        let before = *self.active_selection();
        self.select_to_with_affinity(offset, line_end_affinity, cx);
        // A handle never collapses the selection: at the other end it stops,
        // and the finger has to pass that end to swap the two.
        if self.active_selection().is_empty() {
            *self.active_selection_mut() = before;
            return;
        }
        // Dragging one end past the other swaps them: the selection now runs
        // the other way and the finger holds what became the other handle.
        let edge = if self.active_selection().reversed {
            SelectionEdge::Start
        } else {
            SelectionEdge::End
        };
        if let Some(drag) = self.touch_selection.drag.as_mut() {
            drag.set_edge(edge);
        }
        self.retain_touch_selection();
        cx.notify();
    }

    /// Ends the handle drag and reopens the edit menu.
    pub fn end_edge_drag(&mut self, cx: &mut Context<Self>) {
        if self.touch_selection.drag.take().is_none() {
            return;
        }
        self.auto_scroll.stop();
        if self.active_selection().is_empty() {
            self.active_selection_mut().reversed = false;
        }
        self.touch_selection.menu_open = true;
        cx.notify();
    }

    #[cfg(test)]
    pub(super) fn is_edit_menu_open(&self) -> bool {
        self.touch_selection.menu_open
    }

    #[cfg(test)]
    pub(super) fn touch_selection_range(&self) -> Option<std::ops::Range<usize>> {
        self.touch_selection.range.map(|(start, end)| start..end)
    }
}

#[cfg(test)]
mod tests {
    use gpui::{
        AppContext as _, Context, Entity, IntoElement, LongPressEvent, MouseButton,
        ParentElement as _, Render, Styled as _, TestAppContext, TouchPhase, VisualTestContext,
        Window, div, point, px,
    };

    use crate::input::{InputState, TextareaState};
    use crate::touch_selection::SelectionEdge;

    struct TouchRoot(Entity<InputState>);

    impl Render for TouchRoot {
        fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
            div().size_full().child(self.0.clone())
        }
    }

    struct TextareaRoot(Entity<TextareaState>);

    impl Render for TextareaRoot {
        fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
            // Short enough that forty lines scroll.
            div().w(px(300.)).h(px(80.)).child(self.0.clone())
        }
    }

    fn open_input<'a>(
        cx: &'a mut TestAppContext,
        value: &str,
    ) -> (Entity<InputState>, &'a mut VisualTestContext) {
        cx.update(crate::init);
        let value = value.to_string();
        let (root, cx) = cx.add_window_view(move |window, cx| {
            let input = cx.new(|cx| {
                let mut state = InputState::new(window, cx);
                state.set_value(value, window, cx);
                state
            });
            TouchRoot(input)
        });
        let input = root.read_with(cx, |root, _| root.0.clone());
        cx.run_until_parked();
        cx.update(|window, cx| {
            let _ = window.draw(cx);
        });
        (input, cx)
    }

    fn long_press(
        cx: &mut VisualTestContext,
        phase: TouchPhase,
        start: (f32, f32),
        at: (f32, f32),
    ) {
        cx.simulate_event(LongPressEvent {
            phase,
            start_position: point(px(start.0), px(start.1)),
            position: point(px(at.0), px(at.1)),
        });
        cx.update(|window, cx| {
            let _ = window.draw(cx);
        });
    }

    /// The window position of the caret before `offset`, in the input's line.
    fn caret_at(input: &Entity<InputState>, cx: &VisualTestContext, offset: usize) -> (f32, f32) {
        input.read_with(cx, |state, _| {
            let (_, _, position) = state.line_and_position_for_offset(offset);
            let position = state.last_bounds.unwrap().origin + position.unwrap();
            let line_height = state.last_layout.as_ref().unwrap().line_height;
            (position.x.into(), (position.y + line_height * 0.5).into())
        })
    }

    #[gpui::test]
    fn long_press_selects_word_then_release_opens_menu(cx: &mut TestAppContext) {
        let (input, cx) = open_input(cx, "quick select value");
        let start = caret_at(&input, cx, 8);
        long_press(cx, TouchPhase::Started, start, start);
        input.read_with(cx, |state, _| {
            assert_eq!(state.selected_text().to_string(), "select");
            assert!(!state.is_edit_menu_open());
            let snapshot = state.touch_selection().expect("touch selection is live");
            assert!(!snapshot.is_empty());
            assert!(!snapshot.is_menu_open());
        });

        let end = caret_at(&input, cx, 18);
        long_press(cx, TouchPhase::Moved, start, end);
        input.read_with(cx, |state, _| {
            assert_eq!(state.selected_text().to_string(), "select value");
        });

        long_press(cx, TouchPhase::Ended, start, end);
        input.read_with(cx, |state, _| {
            assert_eq!(state.selected_text().to_string(), "select value");
            assert!(state.is_edit_menu_open());
            let snapshot = state.touch_selection().expect("touch selection is live");
            assert!(snapshot.is_menu_open());
            assert!(snapshot.start().left() < snapshot.end().left());
        });
    }

    #[gpui::test]
    fn double_tap_selects_word_with_handles_and_menu(cx: &mut TestAppContext) {
        let (input, cx) = open_input(cx, "quick select value");
        let at = caret_at(&input, cx, 8);
        let position = point(px(at.0), px(at.1));
        // A tap arrives as mouse events; the touch that began it is what
        // tells the input they came from a finger.
        cx.update(|_, cx| crate::GlobalState::note_touch(cx));
        for click_count in [1, 2] {
            cx.simulate_event(gpui::MouseDownEvent {
                position,
                button: MouseButton::Left,
                modifiers: Default::default(),
                click_count,
                first_mouse: false,
            });
            cx.simulate_event(gpui::MouseUpEvent {
                position,
                button: MouseButton::Left,
                modifiers: Default::default(),
                click_count,
            });
        }
        cx.update(|window, cx| {
            let _ = window.draw(cx);
        });
        input.read_with(cx, |state, _| {
            assert_eq!(state.selected_text().to_string(), "select");
            let snapshot = state
                .touch_selection()
                .expect("a double tap is a touch selection");
            assert!(snapshot.is_menu_open());
        });
    }

    #[gpui::test]
    fn long_press_on_empty_input_places_caret_with_menu(cx: &mut TestAppContext) {
        let (input, cx) = open_input(cx, "");
        long_press(cx, TouchPhase::Started, (20., 10.), (20., 10.));
        long_press(cx, TouchPhase::Ended, (20., 10.), (20., 10.));
        input.read_with(cx, |state, _| {
            assert_eq!(state.selected_range(), 0..0);
            let snapshot = state.touch_selection().expect("caret still gets a menu");
            assert!(snapshot.is_empty());
            assert!(snapshot.is_menu_open());
        });
    }

    #[gpui::test]
    fn dragging_a_handle_moves_that_end_only(cx: &mut TestAppContext) {
        let (input, cx) = open_input(cx, "quick select value");
        let start = caret_at(&input, cx, 8);
        long_press(cx, TouchPhase::Started, start, start);
        long_press(cx, TouchPhase::Ended, start, start);

        // The finger holds the end knob, which hangs below the line.
        let end_caret = caret_at(&input, cx, 12);
        let finger = (end_caret.0, end_caret.1 + 20.);
        cx.update(|_, cx| {
            input.update(cx, |state, cx| {
                state.begin_edge_drag(SelectionEdge::End, point(px(finger.0), px(finger.1)), cx);
            });
        });
        input.read_with(cx, |state, _| {
            let snapshot = state.touch_selection().unwrap();
            assert_eq!(snapshot.dragging(), Some(SelectionEdge::End));
            assert!(!snapshot.is_menu_open());
        });

        let target = caret_at(&input, cx, 18);
        cx.update(|_, cx| {
            input.update(cx, |state, cx| {
                state.update_edge_drag(point(px(target.0), px(target.1 + 20.)), cx);
            });
        });
        input.read_with(cx, |state, _| {
            assert_eq!(state.selected_text().to_string(), "select value");
        });

        // Pull the start handle past the end: the ends swap, the finger keeps
        // its handle.
        cx.update(|_, cx| {
            input.update(cx, |state, cx| {
                state.end_edge_drag(cx);
                assert!(state.is_edit_menu_open());
                let start_caret = state.touch_selection().unwrap().start();
                state.begin_edge_drag(SelectionEdge::Start, start_caret.origin, cx);
            });
        });
        let target = caret_at(&input, cx, 0);
        cx.update(|_, cx| {
            input.update(cx, |state, cx| {
                state.update_edge_drag(point(px(target.0), px(target.1)), cx);
            });
        });
        input.read_with(cx, |state, _| {
            assert_eq!(state.selected_text().to_string(), "quick select value");
            assert_eq!(state.touch_selection_range(), Some(0..18));
        });
    }

    #[gpui::test]
    fn dragging_one_handle_past_the_other_swaps_them(cx: &mut TestAppContext) {
        let (input, cx) = open_input(cx, "quick select value");
        let start = caret_at(&input, cx, 8);
        long_press(cx, TouchPhase::Started, start, start);
        long_press(cx, TouchPhase::Ended, start, start);
        input.read_with(cx, |state, _| {
            assert_eq!(state.selected_text().to_string(), "select");
        });

        // Take the start handle and pull it past the end of "select" to the
        // end of the text: the finger now holds the end handle, and the
        // selection runs from the old end forward.
        let start_caret = caret_at(&input, cx, 6);
        cx.update(|_, cx| {
            input.update(cx, |state, cx| {
                state.begin_edge_drag(
                    SelectionEdge::Start,
                    point(px(start_caret.0), px(start_caret.1)),
                    cx,
                );
            });
        });
        let target = caret_at(&input, cx, 18);
        cx.update(|_, cx| {
            input.update(cx, |state, cx| {
                state.update_edge_drag(point(px(target.0), px(target.1)), cx);
            });
        });
        input.read_with(cx, |state, _| {
            assert_eq!(state.selected_text().to_string(), " value");
            assert_eq!(
                state.touch_selection().unwrap().dragging(),
                Some(SelectionEdge::End)
            );
        });

        // And back across again: it is the start handle once more.
        let target = caret_at(&input, cx, 0);
        cx.update(|_, cx| {
            input.update(cx, |state, cx| {
                state.update_edge_drag(point(px(target.0), px(target.1)), cx);
                state.end_edge_drag(cx);
            });
        });
        input.read_with(cx, |state, _| {
            assert_eq!(state.selected_text().to_string(), "quick select");
            assert!(state.is_edit_menu_open());
        });
    }

    #[gpui::test]
    fn touch_selection_goes_away_when_something_else_moves_the_selection(cx: &mut TestAppContext) {
        let (input, cx) = open_input(cx, "quick select value");
        let start = caret_at(&input, cx, 2);
        long_press(cx, TouchPhase::Started, start, start);
        long_press(cx, TouchPhase::Ended, start, start);
        input.read_with(cx, |state, _| assert!(state.touch_selection().is_some()));

        // Select All from the menu keeps it, over the new range.
        cx.update(|window, cx| {
            input.update(cx, |state, cx| state.select_all_from_edit_menu(window, cx));
        });
        input.read_with(cx, |state, _| {
            assert_eq!(state.selected_range(), 0..18);
            assert!(state.touch_selection().is_some());
            assert!(state.is_edit_menu_open());
        });

        // Copy closes the menu but keeps the handles.
        cx.update(|_, cx| input.update(cx, |state, cx| state.close_edit_menu(cx)));
        input.read_with(cx, |state, _| {
            let snapshot = state.touch_selection().unwrap();
            assert!(!snapshot.is_menu_open());
        });

        // Typing replaces the selection; nothing is left to hold a handle.
        cx.update(|window, cx| {
            input.update(cx, |state, cx| {
                state.replace_text_in_range_silent(None, "x", window, cx);
            });
        });
        input.read_with(cx, |state, _| assert!(state.touch_selection().is_none()));

        // A press elsewhere drops it too.
        long_press(cx, TouchPhase::Started, start, start);
        long_press(cx, TouchPhase::Ended, start, start);
        input.read_with(cx, |state, _| assert!(state.touch_selection().is_some()));
        let elsewhere = caret_at(&input, cx, 0);
        cx.simulate_mouse_down(
            point(px(elsewhere.0), px(elsewhere.1)),
            MouseButton::Left,
            Default::default(),
        );
        input.read_with(cx, |state, _| assert!(state.touch_selection().is_none()));
    }

    #[gpui::test]
    fn scrolling_closes_the_menu_and_keeps_the_handles(cx: &mut TestAppContext) {
        cx.update(crate::init);
        let (root, cx) = cx.add_window_view(|window, cx| {
            let textarea = cx.new(|cx| {
                let mut state = TextareaState::new(window, cx);
                let text = (0..40).map(|ix| format!("line {ix}")).collect::<Vec<_>>();
                state.set_value(text.join("\n"), window, cx);
                state
            });
            TextareaRoot(textarea)
        });
        let textarea = root.read_with(cx, |root, _| root.0.clone());
        cx.run_until_parked();
        cx.update(|window, cx| {
            let _ = window.draw(cx);
        });
        let start = textarea.read_with(cx, |state, _| {
            let (_, _, position) = state.line_and_position_for_offset(2);
            let position = state.last_bounds.unwrap().origin + position.unwrap();
            (f32::from(position.x), f32::from(position.y) + 4.)
        });
        long_press(cx, TouchPhase::Started, start, start);
        long_press(cx, TouchPhase::Ended, start, start);
        textarea.read_with(cx, |state, _| {
            assert_eq!(state.selected_text().to_string(), "line");
            assert!(state.touch_selection().unwrap().is_menu_open());
        });

        cx.update(|_, cx| {
            textarea.update(cx, |state, cx| {
                state.set_scroll_offset(point(px(0.), px(-8.)), cx);
                state.close_edit_menu(cx);
            });
        });
        cx.update(|window, cx| {
            let _ = window.draw(cx);
        });
        textarea.read_with(cx, |state, _| {
            let snapshot = state.touch_selection().expect("handles follow the text");
            assert!(!snapshot.is_menu_open());
            assert_eq!(state.selected_text().to_string(), "line");
        });

        // Scrolled far enough, the selection leaves the viewport: no handle
        // for it, and no menu anchored to nothing.
        cx.update(|_, cx| {
            textarea.update(cx, |state, cx| {
                state.set_scroll_offset(point(px(0.), px(-600.)), cx);
            });
        });
        cx.update(|window, cx| {
            let _ = window.draw(cx);
        });
        textarea.read_with(cx, |state, _| {
            let snapshot = state
                .touch_selection()
                .expect("the selection is still the touch one");
            assert!(!snapshot.is_edge_visible(SelectionEdge::Start));
            assert!(!snapshot.is_edge_visible(SelectionEdge::End));
            assert_eq!(snapshot.bounds(), None);
        });
    }
}