gpui-ce 0.3.0

A community fork of Zed's GPU-accelerated UI framework
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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
use crate::{
    App, Bounds, ClipboardItem, Context, Entity, InputHandler, Pixels, TextInputConfiguration,
    UTF16Selection, Window,
};
use std::ops::Range;

/// Converts a UTF-8 byte offset into a UTF-16 code-unit offset.
///
/// Platform text input counts in UTF-16 code units, while Rust strings index
/// by UTF-8 bytes. Use this to translate an offset in a `String` into the offset
/// the platform expects. An offset past the end returns the UTF-16 length of
/// `text`. An offset inside a character returns the offset of that character's
/// end.
///
/// # Examples
///
/// ```
/// use gpui::utf8_to_utf16_offset;
///
/// // "A" is 1 byte and 1 code unit. The emoji is 4 bytes and 2 code units.
/// assert_eq!(utf8_to_utf16_offset("A😀B", 5), 3);
/// assert_eq!(utf8_to_utf16_offset("A😀B", 100), 4);
/// ```
pub fn utf8_to_utf16_offset(text: &str, utf8_offset: usize) -> usize {
    text.char_indices()
        .take_while(|(offset, _)| *offset < utf8_offset)
        .map(|(_, character)| character.len_utf16())
        .sum()
}

/// Converts a UTF-16 code-unit offset into a UTF-8 byte offset.
///
/// The reverse of [`utf8_to_utf16_offset`]. Use it on ranges that the platform
/// sends, before slicing a `String`. An offset past the end returns the UTF-8
/// length of `text`. An offset inside a surrogate pair returns the offset of the
/// end of that character.
///
/// # Examples
///
/// ```
/// use gpui::utf16_to_utf8_offset;
///
/// assert_eq!(utf16_to_utf8_offset("A😀B", 3), 5);
/// assert_eq!(utf16_to_utf8_offset("A😀B", 100), 6);
/// ```
pub fn utf16_to_utf8_offset(text: &str, utf16_offset: usize) -> usize {
    let mut consumed_utf16 = 0;
    for (utf8_offset, character) in text.char_indices() {
        if consumed_utf16 >= utf16_offset {
            return utf8_offset;
        }
        consumed_utf16 += character.len_utf16();
    }
    text.len()
}

/// Text input for a view: the methods the platform calls for typed text, IME
/// composition, paste and selection.
///
/// Implement this on a view that edits text, such as a text field. Then wrap the
/// view in an [`ElementInputHandler`] and install it during paint with
/// [`Window::handle_input`](crate::Window::handle_input). The platform's input method sends its requests
/// through that wrapper, and each method runs with the view updated.
///
/// Ranges are in UTF-16 code units, the unit the platform uses, and are
/// relative to the whole document. Convert with [`utf8_to_utf16_offset`] and
/// [`utf16_to_utf8_offset`] when your text is a UTF-8 `String`. Every method has
/// the same meaning as the matching method on [`InputHandler`], which documents
/// the platform side.
///
/// On touch platforms, dismissing the software keyboard can leave the view focused. Call
/// [`Window::request_virtual_keyboard`] from an editing gesture, such as a tap on the field,
/// to show it again. After edits or selection changes that your app makes itself, call
/// [`Window::notify_text_input_changed`] or [`Window::notify_text_selection_changed`] so the
/// platform's text services stay in sync. UIKit also needs
/// [`text_length_utf16`](Self::text_length_utf16), or a bounded
/// [`text_input_editable_range`](Self::text_input_editable_range), and
/// [`set_selected_text_range`](Self::set_selected_text_range). For native selection
/// rectangles, implement [`selection_bounds_for_range`](Self::selection_bounds_for_range),
/// which returns all of a range's line fragments at once.
///
/// The text guide has a complete editable field: [text input](crate::_documentation::guides::text_input).
///
/// # Examples
///
/// ```
/// use gpui::{
///     Bounds, Context, ElementInputHandler, EntityInputHandler, FocusHandle, IntoElement,
///     Pixels, Point, Render, TestApp, UTF16Selection, Window, canvas, div, prelude::*,
/// };
/// use std::ops::Range;
///
/// struct Field {
///     focus_handle: FocusHandle,
///     text: String,
/// }
///
/// impl EntityInputHandler for Field {
///     fn text_for_range(
///         &mut self,
///         range: Range<usize>,
///         adjusted_range: &mut Option<Range<usize>>,
///         _window: &mut Window,
///         _cx: &mut Context<Self>,
///     ) -> Option<String> {
///         *adjusted_range = Some(range.clone());
///         let start = gpui::utf16_to_utf8_offset(&self.text, range.start);
///         let end = gpui::utf16_to_utf8_offset(&self.text, range.end);
///         Some(self.text[start..end].to_string())
///     }
///
///     fn selected_text_range(
///         &mut self,
///         _ignore_disabled_input: bool,
///         _window: &mut Window,
///         _cx: &mut Context<Self>,
///     ) -> Option<UTF16Selection> {
///         let end = gpui::utf8_to_utf16_offset(&self.text, self.text.len());
///         Some(UTF16Selection { range: end..end, reversed: false })
///     }
///
///     fn marked_text_range(&self, _window: &mut Window, _cx: &mut Context<Self>) -> Option<Range<usize>> {
///         None
///     }
///
///     fn unmark_text(&mut self, _window: &mut Window, _cx: &mut Context<Self>) {}
///
///     fn replace_text_in_range(
///         &mut self,
///         range: Option<Range<usize>>,
///         text: &str,
///         _window: &mut Window,
///         cx: &mut Context<Self>,
///     ) {
///         let range = range.unwrap_or_else(|| {
///             let end = gpui::utf8_to_utf16_offset(&self.text, self.text.len());
///             end..end
///         });
///         let start = gpui::utf16_to_utf8_offset(&self.text, range.start);
///         let end = gpui::utf16_to_utf8_offset(&self.text, range.end);
///         self.text.replace_range(start..end, text);
///         cx.notify();
///     }
///
///     fn replace_and_mark_text_in_range(
///         &mut self,
///         range: Option<Range<usize>>,
///         new_text: &str,
///         _new_selected_range: Option<Range<usize>>,
///         window: &mut Window,
///         cx: &mut Context<Self>,
///     ) {
///         self.replace_text_in_range(range, new_text, window, cx);
///     }
///
///     fn bounds_for_range(
///         &mut self,
///         _range: Range<usize>,
///         element_bounds: Bounds<Pixels>,
///         _window: &mut Window,
///         _cx: &mut Context<Self>,
///     ) -> Option<Bounds<Pixels>> {
///         Some(element_bounds)
///     }
///
///     fn character_index_for_point(
///         &mut self,
///         _point: Point<Pixels>,
///         _window: &mut Window,
///         _cx: &mut Context<Self>,
///     ) -> Option<usize> {
///         Some(0)
///     }
/// }
///
/// impl Render for Field {
///     fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
///         let view = cx.entity();
///         let focus_handle = self.focus_handle.clone();
///         div().size_full().track_focus(&self.focus_handle).child(
///             canvas(
///                 |_, _, _| {},
///                 move |bounds, _, window, cx| {
///                     window.handle_input(&focus_handle, ElementInputHandler::new(bounds, view), cx);
///                 },
///             )
///             .size_full(),
///         )
///     }
/// }
///
/// let mut app = TestApp::new();
/// let mut window = app.open_window(|window, cx| {
///     let focus_handle = cx.focus_handle();
///     window.focus(&focus_handle, cx);
///     Field { focus_handle, text: String::new() }
/// });
///
/// window.simulate_input("Ada");
/// assert_eq!(window.read(|field, _| field.text.clone()), "Ada");
/// ```
pub trait EntityInputHandler: 'static + Sized {
    /// Returns the text in a UTF-16 range, for the platform to read back.
    ///
    /// The platform uses this to read text around the cursor, for example for
    /// autocorrect or the character picker. If you return text for a different
    /// range than was asked for, such as one snapped to a character boundary,
    /// write the range you returned into `adjusted_range`.
    fn text_for_range(
        &mut self,
        range: Range<usize>,
        adjusted_range: &mut Option<Range<usize>>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Option<String>;

    /// Returns the current selection as a UTF-16 range and direction.
    ///
    /// Returns `None` if there is no selection to report. `ignore_disabled_input`
    /// is passed through from the platform, and each implementation decides what it
    /// means. The bundled text elements ignore it.
    fn selected_text_range(
        &mut self,
        ignore_disabled_input: bool,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Option<UTF16Selection>;

    /// Returns the UTF-16 range of the text in the current IME composition, or
    /// `None` if no composition is active.
    ///
    /// Composition is the text an input method is still building, such as the
    /// underlined kana before a Japanese conversion is confirmed.
    fn marked_text_range(
        &self,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Option<Range<usize>>;

    /// Ends the IME composition and keeps its text as ordinary text.
    ///
    /// The platform calls this when it finishes composing. After this call,
    /// [`EntityInputHandler::marked_text_range`] returns `None`.
    fn unmark_text(&mut self, window: &mut Window, cx: &mut Context<Self>);

    /// Inserts clipboard contents at the selection, for platforms that deliver
    /// paste as an input event, such as the web.
    ///
    /// The default inserts only the plain-text part of `item`, through
    /// [`EntityInputHandler::replace_text_in_range`] with a `None` range. Override
    /// it to handle rich content.
    fn paste(&mut self, item: ClipboardItem, window: &mut Window, cx: &mut Context<Self>) {
        if let Some(text) = item.text() {
            self.replace_text_in_range(None, &text, window, cx);
        }
    }

    /// Replaces a UTF-16 range with typed text, or inserts at a cursor.
    ///
    /// This is how typed characters reach your field. A `None` range means the
    /// platform gave no range. The bundled text elements then replace the marked
    /// text if there is any, and otherwise the selection.
    fn replace_text_in_range(
        &mut self,
        range: Option<Range<usize>>,
        text: &str,
        window: &mut Window,
        cx: &mut Context<Self>,
    );

    /// Replaces a range with IME text and marks that text as composing.
    ///
    /// The platform calls this for each step of an input method's composition.
    /// The marked text is underlined and stays editable until
    /// [`EntityInputHandler::unmark_text`] is called. `new_selected_range` is the
    /// selection within `new_text`, in UTF-16 units, counted from the start of
    /// `new_text`. When it is `None`, the cursor goes to the end of the new text.
    fn replace_and_mark_text_in_range(
        &mut self,
        range: Option<Range<usize>>,
        new_text: &str,
        new_selected_range: Option<Range<usize>>,
        window: &mut Window,
        cx: &mut Context<Self>,
    );

    /// Returns where a UTF-16 range is drawn, so the platform can place its
    /// candidate window and other overlays next to the text.
    ///
    /// `element_bounds` is the bounds of the element that installed this handler.
    /// Return `None` when the text cannot be measured. Returned bounds are in window
    /// coordinates, in logical pixels.
    fn bounds_for_range(
        &mut self,
        range_utf16: Range<usize>,
        element_bounds: Bounds<Pixels>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Option<Bounds<Pixels>>;

    /// Returns the UTF-16 index of the character at `point`, which the platform uses
    /// to map a screen position to text.
    ///
    /// `point` is in window coordinates, in logical pixels.
    /// See [`InputHandler::selection_bounds_for_range`] for details.
    fn selection_bounds_for_range(
        &mut self,
        _range_utf16: Range<usize>,
        _element_bounds: Bounds<Pixels>,
        _window: &mut Window,
        _cx: &mut Context<Self>,
    ) -> Vec<Bounds<Pixels>> {
        Vec::new()
    }

    /// See [`InputHandler::character_index_for_point`] for details
    fn character_index_for_point(
        &mut self,
        point: crate::Point<Pixels>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Option<usize>;

    /// Sets the selection to a UTF-16 range, when the platform moves it.
    ///
    /// Platforms call this for system-driven selection changes, such as a dragged
    /// selection handle or Select All from system UI. It is the reverse of
    /// [`EntityInputHandler::selected_text_range`]. The default does nothing.
    fn set_selected_text_range(
        &mut self,
        _range_utf16: Range<usize>,
        _window: &mut Window,
        _cx: &mut Context<Self>,
    ) {
    }

    /// Returns the length of the document in UTF-16 code units, or `None` if it is
    /// not known. The default is `None`.
    fn text_length_utf16(
        &mut self,
        _window: &mut Window,
        _cx: &mut Context<Self>,
    ) -> Option<usize> {
        None
    }

    /// Returns whether the field accepts typed text right now.
    ///
    /// Return `false` for a read-only or disabled field. The default is `true`.
    /// For a keystroke with [`KeyDownEvent::prefer_character_input`](crate::KeyDownEvent::prefer_character_input) set, the window
    /// sends the keystroke to text input instead of key bindings only when this
    /// returns `true`.
    fn accepts_text_input(&self, _window: &mut Window, _cx: &mut Context<Self>) -> bool {
        true
    }

    /// Returns platform text assistance settings for this field, such as
    /// autocorrect and the action key on the software keyboard.
    ///
    /// The window asks on every frame and forwards the result only when it
    /// changes, so return a cheap value. You can vary it with application state,
    /// such as the cursor position. The default disables text assistance.
    fn text_input_configuration(
        &mut self,
        _window: &mut Window,
        _cx: &mut Context<Self>,
    ) -> TextInputConfiguration {
        TextInputConfiguration::default()
    }

    /// Returns the UTF-16 range that the platform may read and edit around the
    /// selection, or `None` for no limit.
    ///
    /// Platforms that mirror text into an IME-editable buffer use this range to
    /// keep multi-step edits, such as word deletion and autocorrect, inside the
    /// region you choose. The range should contain the selection. The default is
    /// `None`.
    fn text_input_editable_range(
        &mut self,
        _window: &mut Window,
        _cx: &mut Context<Self>,
    ) -> Option<Range<usize>> {
        None
    }
}

/// Connects an [`EntityInputHandler`] view to the platform's text input.
///
/// This is the usual way to install an [`EntityInputHandler`]. It implements
/// [`InputHandler`] by forwarding each call to the view, with the element's
/// bounds filled in for the methods that need them. Create one during paint and
/// pass it to [`Window::handle_input`](crate::Window::handle_input). The handler is active for the next
/// frame only, so install it again on every paint.
pub struct ElementInputHandler<V> {
    view: Entity<V>,
    element_bounds: Bounds<Pixels>,
}

impl<V: 'static> ElementInputHandler<V> {
    /// Creates a handler that forwards platform text input to `view`.
    ///
    /// Pass the bounds of the element that is painting. They are returned from
    /// [`InputHandler::element_bounds`] and passed to
    /// [`EntityInputHandler::bounds_for_range`]. Call this in
    /// [`Element::paint`][element_paint], then pass the handler to
    /// [`Window::handle_input`](crate::Window::handle_input). The window installs the handler only while the
    /// given focus handle is focused.
    ///
    /// [element_paint]: crate::Element::paint
    pub fn new(element_bounds: Bounds<Pixels>, view: Entity<V>) -> Self {
        ElementInputHandler {
            view,
            element_bounds,
        }
    }
}

impl<V: EntityInputHandler> InputHandler for ElementInputHandler<V> {
    fn selected_text_range(
        &mut self,
        ignore_disabled_input: bool,
        window: &mut Window,
        cx: &mut App,
    ) -> Option<UTF16Selection> {
        self.view.update(cx, |view, cx| {
            view.selected_text_range(ignore_disabled_input, window, cx)
        })
    }

    fn marked_text_range(&mut self, window: &mut Window, cx: &mut App) -> Option<Range<usize>> {
        self.view
            .update(cx, |view, cx| view.marked_text_range(window, cx))
    }

    fn text_for_range(
        &mut self,
        range_utf16: Range<usize>,
        adjusted_range: &mut Option<Range<usize>>,
        window: &mut Window,
        cx: &mut App,
    ) -> Option<String> {
        self.view.update(cx, |view, cx| {
            view.text_for_range(range_utf16, adjusted_range, window, cx)
        })
    }

    fn replace_text_in_range(
        &mut self,
        replacement_range: Option<Range<usize>>,
        text: &str,
        window: &mut Window,
        cx: &mut App,
    ) {
        self.view.update(cx, |view, cx| {
            view.replace_text_in_range(replacement_range, text, window, cx)
        });
    }

    fn replace_and_mark_text_in_range(
        &mut self,
        range_utf16: Option<Range<usize>>,
        new_text: &str,
        new_selected_range: Option<Range<usize>>,
        window: &mut Window,
        cx: &mut App,
    ) {
        self.view.update(cx, |view, cx| {
            view.replace_and_mark_text_in_range(
                range_utf16,
                new_text,
                new_selected_range,
                window,
                cx,
            )
        });
    }

    fn unmark_text(&mut self, window: &mut Window, cx: &mut App) {
        self.view
            .update(cx, |view, cx| view.unmark_text(window, cx));
    }

    fn paste(&mut self, item: ClipboardItem, window: &mut Window, cx: &mut App) {
        self.view
            .update(cx, |view, cx| view.paste(item, window, cx));
    }

    fn bounds_for_range(
        &mut self,
        range_utf16: Range<usize>,
        window: &mut Window,
        cx: &mut App,
    ) -> Option<Bounds<Pixels>> {
        self.view.update(cx, |view, cx| {
            view.bounds_for_range(range_utf16, self.element_bounds, window, cx)
        })
    }

    fn selection_bounds_for_range(
        &mut self,
        range_utf16: Range<usize>,
        window: &mut Window,
        cx: &mut App,
    ) -> Vec<Bounds<Pixels>> {
        self.view.update(cx, |view, cx| {
            view.selection_bounds_for_range(range_utf16, self.element_bounds, window, cx)
        })
    }

    fn character_index_for_point(
        &mut self,
        point: crate::Point<Pixels>,
        window: &mut Window,
        cx: &mut App,
    ) -> Option<usize> {
        self.view.update(cx, |view, cx| {
            view.character_index_for_point(point, window, cx)
        })
    }

    fn set_selected_text_range(
        &mut self,
        range_utf16: Range<usize>,
        window: &mut Window,
        cx: &mut App,
    ) {
        self.view.update(cx, |view, cx| {
            view.set_selected_text_range(range_utf16, window, cx)
        })
    }

    fn element_bounds(&mut self, _window: &mut Window, _cx: &mut App) -> Option<Bounds<Pixels>> {
        Some(self.element_bounds)
    }

    fn text_length_utf16(&mut self, window: &mut Window, cx: &mut App) -> Option<usize> {
        self.view
            .update(cx, |view, cx| view.text_length_utf16(window, cx))
    }

    fn accepts_text_input(&mut self, window: &mut Window, cx: &mut App) -> bool {
        self.view
            .update(cx, |view, cx| view.accepts_text_input(window, cx))
    }

    fn prefers_ime_for_printable_keys(&mut self, window: &mut Window, cx: &mut App) -> bool {
        self.view
            .update(cx, |view, cx| view.accepts_text_input(window, cx))
    }

    fn text_input_configuration(
        &mut self,
        window: &mut Window,
        cx: &mut App,
    ) -> TextInputConfiguration {
        self.view
            .update(cx, |view, cx| view.text_input_configuration(window, cx))
    }

    fn text_input_editable_range(
        &mut self,
        window: &mut Window,
        cx: &mut App,
    ) -> Option<Range<usize>> {
        self.view
            .update(cx, |view, cx| view.text_input_editable_range(window, cx))
    }
}

#[cfg(test)]
mod configuration_tests {
    use super::*;
    use crate::{
        AnyWindowHandle, AppContext as _, FocusHandle, InteractiveElement as _, IntoElement,
        ParentElement as _, Render, Styled as _, TestAppContext, TextInputAction,
        TextInputStateChange, canvas, div,
    };

    #[gpui::test]
    fn text_input_configuration_and_focus_state_are_forwarded_on_change(cx: &mut TestAppContext) {
        let custom = TextInputConfiguration {
            autocorrect: true,
            input_action: TextInputAction::Send,
            ..Default::default()
        };
        let window = cx.add_window({
            let custom = custom.clone();
            move |_, cx| ConfigurationTestView {
                focus_handle: cx.focus_handle(),
                configuration: custom,
                accepts_text_input: true,
            }
        });
        let view = window.root(cx).unwrap();
        let test_window = cx.test_window(window.into());
        let window = AnyWindowHandle::from(window);
        let draw = |cx: &mut TestAppContext| {
            cx.update_window(window, |_, window, cx| window.draw(cx).clear(cx))
                .unwrap();
        };

        // Nothing is focused, so the platform learns the default configuration.
        draw(cx);
        assert_eq!(
            test_window.text_input_configurations(),
            vec![TextInputConfiguration::default()]
        );
        assert!(test_window.text_input_state_changes().is_empty());

        // Focusing the view routes its configuration to the platform.
        cx.update_window(window, |_, window, cx| {
            let focus_handle = view.read(cx).focus_handle.clone();
            window.focus(&focus_handle, cx);
        })
        .unwrap();
        draw(cx);
        assert_eq!(
            test_window.text_input_configurations(),
            vec![TextInputConfiguration::default(), custom.clone()]
        );
        assert_eq!(
            test_window.text_input_state_changes(),
            vec![TextInputStateChange::FocusGained]
        );

        // Redrawing without a change forwards nothing.
        draw(cx);
        assert_eq!(test_window.text_input_configurations().len(), 2);
        assert_eq!(test_window.text_input_state_changes().len(), 1);

        // Changing the configuration forwards the new value.
        let updated = TextInputConfiguration {
            suggestions: true,
            ..custom
        };
        view.update(cx, {
            let updated = updated.clone();
            |view, cx| {
                view.configuration = updated;
                cx.notify();
            }
        });
        draw(cx);
        assert_eq!(
            test_window.text_input_configurations().last(),
            Some(&updated)
        );
        assert_eq!(test_window.text_input_configurations().len(), 3);
        assert_eq!(test_window.text_input_state_changes().len(), 1);

        // Losing focus reverts the platform to the default configuration.
        cx.update_window(window, |_, window, cx| window.blur(cx))
            .unwrap();
        draw(cx);
        assert_eq!(
            test_window.text_input_configurations().last(),
            Some(&TextInputConfiguration::default())
        );
        assert_eq!(test_window.text_input_configurations().len(), 4);
        assert_eq!(
            test_window.text_input_state_changes(),
            vec![
                TextInputStateChange::FocusGained,
                TextInputStateChange::FocusLost
            ]
        );
    }

    #[gpui::test]
    fn text_notifications_require_a_focused_input_and_track_field_changes(cx: &mut TestAppContext) {
        let window = cx.add_window(|_, cx| ConfigurationTestView {
            focus_handle: cx.focus_handle(),
            configuration: TextInputConfiguration::default(),
            accepts_text_input: true,
        });
        let view = window.root(cx).unwrap();
        let mut test_window = cx.test_window(window.into());
        let window = AnyWindowHandle::from(window);
        let first_focus = view.read_with(cx, |view, _| view.focus_handle.clone());
        cx.update_window(window, |_, window, cx| {
            window.focus(&first_focus, cx);
            window.draw(cx).clear(cx);
            window.notify_text_input_changed(&first_focus);
            window.notify_text_selection_changed(&first_focus);
        })
        .unwrap();

        use crate::PlatformWindow as _;
        let mut installed_handler = test_window.take_input_handler().unwrap();
        assert!(installed_handler.query_accepts_focused_text_input());
        assert_eq!(installed_handler.focus_id(), Some(first_focus.id));
        let second_focus = cx
            .update_window(window, |_, window, cx| {
                let second_focus = view.update(cx, |view, cx| {
                    view.focus_handle = cx.focus_handle();
                    cx.notify();
                    view.focus_handle.clone()
                });
                window.focus(&second_focus, cx);
                second_focus
            })
            .unwrap();
        assert!(!installed_handler.query_accepts_focused_text_input());
        test_window.set_input_handler(installed_handler);
        cx.update_window(window, |_, window, cx| {
            window.draw(cx).clear(cx);
            window.notify_text_input_changed(&first_focus);
        })
        .unwrap();
        assert_eq!(
            test_window.text_input_state_changes(),
            [
                TextInputStateChange::FocusGained,
                TextInputStateChange::ContentChanged,
                TextInputStateChange::SelectionChanged,
            ]
        );

        let installed_handler = test_window.take_input_handler().unwrap();
        assert_eq!(installed_handler.focus_id(), Some(second_focus.id));
        test_window.set_input_handler(installed_handler);
        cx.update_window(window, |_, window, cx| {
            window.draw(cx).clear(cx);
            window.notify_text_selection_changed(&second_focus);
        })
        .unwrap();

        view.update(cx, |view, cx| {
            view.accepts_text_input = false;
            cx.notify();
        });
        cx.update_window(window, |_, window, cx| {
            window.draw(cx).clear(cx);
            window.notify_text_input_changed(&second_focus);
            window.notify_text_selection_changed(&second_focus);
            let ordinary_focus = cx.focus_handle();
            window.focus(&ordinary_focus, cx);
            window.draw(cx).clear(cx);
            window.notify_text_input_changed(&ordinary_focus);
        })
        .unwrap();
        assert_eq!(
            test_window.text_input_state_changes(),
            [
                TextInputStateChange::FocusGained,
                TextInputStateChange::ContentChanged,
                TextInputStateChange::SelectionChanged,
                TextInputStateChange::SelectionChanged,
                TextInputStateChange::FocusLost,
            ]
        );
    }

    struct ConfigurationTestView {
        focus_handle: FocusHandle,
        configuration: TextInputConfiguration,
        accepts_text_input: bool,
    }

    impl Render for ConfigurationTestView {
        fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
            let view = cx.entity();
            let focus_handle = self.focus_handle.clone();
            div().size_full().track_focus(&self.focus_handle).child(
                canvas(
                    |_, _, _| {},
                    move |bounds, _, window, cx| {
                        window.handle_input(
                            &focus_handle,
                            ElementInputHandler::new(bounds, view),
                            cx,
                        );
                    },
                )
                .size_full(),
            )
        }
    }

    impl EntityInputHandler for ConfigurationTestView {
        fn accepts_text_input(&self, _window: &mut Window, _cx: &mut Context<Self>) -> bool {
            self.accepts_text_input
        }

        fn text_for_range(
            &mut self,
            _range: std::ops::Range<usize>,
            _adjusted_range: &mut Option<std::ops::Range<usize>>,
            _window: &mut Window,
            _cx: &mut Context<Self>,
        ) -> Option<String> {
            None
        }

        fn selected_text_range(
            &mut self,
            _ignore_disabled_input: bool,
            _window: &mut Window,
            _cx: &mut Context<Self>,
        ) -> Option<UTF16Selection> {
            None
        }

        fn marked_text_range(
            &self,
            _window: &mut Window,
            _cx: &mut Context<Self>,
        ) -> Option<std::ops::Range<usize>> {
            None
        }

        fn unmark_text(&mut self, _window: &mut Window, _cx: &mut Context<Self>) {}

        fn replace_text_in_range(
            &mut self,
            _range: Option<std::ops::Range<usize>>,
            _text: &str,
            _window: &mut Window,
            _cx: &mut Context<Self>,
        ) {
        }

        fn replace_and_mark_text_in_range(
            &mut self,
            _range: Option<std::ops::Range<usize>>,
            _new_text: &str,
            _new_selected_range: Option<std::ops::Range<usize>>,
            _window: &mut Window,
            _cx: &mut Context<Self>,
        ) {
        }

        fn bounds_for_range(
            &mut self,
            _range_utf16: std::ops::Range<usize>,
            _element_bounds: Bounds<Pixels>,
            _window: &mut Window,
            _cx: &mut Context<Self>,
        ) -> Option<Bounds<Pixels>> {
            None
        }

        fn character_index_for_point(
            &mut self,
            _point: crate::Point<Pixels>,
            _window: &mut Window,
            _cx: &mut Context<Self>,
        ) -> Option<usize> {
            None
        }

        fn text_input_configuration(
            &mut self,
            _window: &mut Window,
            _cx: &mut Context<Self>,
        ) -> TextInputConfiguration {
            self.configuration.clone()
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn text_offsets_convert_across_unicode_encodings() {
        let cases = [
            ("", vec![(0, 0)]),
            ("plain", vec![(0, 0), (1, 1), (5, 5)]),
            (
                "A😀日本e\u{301}",
                vec![(0, 0), (1, 1), (5, 3), (8, 4), (11, 5), (12, 6), (14, 7)],
            ),
        ];

        for (text, boundaries) in cases {
            for (utf8, utf16) in boundaries {
                assert_eq!(utf8_to_utf16_offset(text, utf8), utf16, "{text:?}");
                assert_eq!(utf16_to_utf8_offset(text, utf16), utf8, "{text:?}");
            }

            assert_eq!(
                utf8_to_utf16_offset(text, usize::MAX),
                text.encode_utf16().count()
            );
            assert_eq!(utf16_to_utf8_offset(text, usize::MAX), text.len());
        }

        let text = "A😀B";
        assert_eq!(utf16_to_utf8_offset(text, 2), 5);
        assert_eq!(utf8_to_utf16_offset(text, 2), 3);
    }
}