gpui-pre 0.3.14

Zed's GPU-accelerated UI framework (gpui-pre snapshot of zed@f126950)
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
895
896
897
use crate::{Action, KeyBinding, KeyDownEvent, PlatformInput};
use crate::{
    AnyWindowHandle, App, Bounds, Element, ElementId, FocusHandle, FocusId, Focusable,
    GlobalElementId, Hitbox, HitboxBehavior, HitboxId, Hsla, InspectorElementId,
    InteractiveElement, IntoElement, LayoutId, PaintNativeView, ParentElement, Pixels, Point,
    Style, Styled, Window, div,
};
use anyhow::{Context as _, Result};
use collections::HashSet;
use std::{
    any::{Any, TypeId},
    cell::{Cell, RefCell},
    ffi::c_void,
    ptr::NonNull,
    rc::{Rc, Weak},
};

/// Identifies a [`NativeView`] within its window.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct NativeViewId(u64);

#[cfg(any(test, feature = "test-support"))]
impl NativeViewId {
    /// Makes an ID for a test that needs one without a window to attach a view to.
    pub fn for_test(id: u64) -> Self {
        Self(id)
    }
}

/// A view owned by the platform's UI toolkit, shown inside a GPUI window.
///
/// Only macOS shows native views so far, where the view is an `NSView`.
/// [`NativeView::new`] fails on every other platform.
///
/// The view is composited beneath the window's GPUI content and shows through
/// wherever this handle is used as an element, so elements painted after it
/// cover it like they would cover any other element. The view is treated as
/// opaque, and is not affected by element opacity or transformations.
///
/// Mouse input over the view goes to the view, and to nothing behind it,
/// wherever no element painted after the view takes mouse input. An element
/// that is only drawn over the view covers it without taking its mouse input. The window's own content is still told of a mouse press
/// there, as a press outside every element. An element behind the view hears
/// no mouse moves while the pointer is over it, so follow a drag that can cross
/// the view with `on_drag_move`, or with an element in front of the view.
///
/// The view takes keyboard input directly from the platform while its
/// [`FocusHandle`] is focused, which a click on the view does, as can an
/// assistive technology such as VoiceOver moving its cursor into the view.
/// A key the view doesn't use is then dispatched from the view's element like
/// any other key press, so key bindings apply to what the view leaves.
///
/// Two kinds of key reach key bindings before the view. Bindings for actions
/// passed to [`reserve_action`](Self::reserve_action) apply first, and their
/// keys never reach the view. A key held with Command or Control is offered to
/// the view as a key equivalent, which is how a web view takes it; a view that
/// only takes such keys as ordinary key presses, as AppKit's text views do,
/// gets one after key bindings have had it.
///
/// The view may take a moment to leave a key, so the keys of any other
/// multi-key binding can reach the view when they are typed quickly.
///
/// The view stays attached to the window it was created in, hidden when it
/// isn't painted, until the last clone of this handle is dropped. Painted more
/// than once in a frame, it shows only where it was painted first. It shows
/// nothing as an element of any other window, so showing the same platform
/// view in another window takes dropping this handle and creating one there.
#[derive(Clone)]
pub struct NativeView(Rc<NativeViewState>);

pub(crate) struct NativeViewState {
    id: NativeViewId,
    window: AnyWindowHandle,
    focus_handle: FocusHandle,
    background: Cell<Hsla>,
    /// The hitbox of the occurrence that shows the view, which is the one
    /// painted first in a frame that paints the view more than once.
    shown_hitbox: Cell<Option<HitboxId>>,
    reserved_actions: RefCell<HashSet<TypeId>>,
    _attachment: Box<dyn Any>,
}

impl NativeView {
    /// Attaches a platform view to the window.
    ///
    /// # Errors
    ///
    /// Fails on platforms that cannot composite native views, which is every
    /// platform but macOS for now.
    ///
    /// # Safety
    ///
    /// `view` must point to a live view of the kind the window's platform
    /// embeds, which is an `NSView` on macOS, and that view must not be in
    /// another view hierarchy.
    ///
    /// GPUI retains the view and puts it inside a view of its own, until the
    /// last clone of the returned handle is dropped. Until then GPUI decides the
    /// view's frame, whether it is hidden, and its order among the window's other
    /// views, so the caller must leave those alone and must not move the view to
    /// another superview.
    pub unsafe fn new(view: NonNull<c_void>, window: &mut Window, cx: &mut App) -> Result<Self> {
        let id = NativeViewId(window.next_native_view_id);
        // SAFETY: The caller upholds the platform's requirements for `view`.
        let attachment = unsafe { window.platform_window.attach_native_view(id, view) }
            .context("this platform cannot show native views")?;
        window.next_native_view_id += 1;
        let state = Rc::new(NativeViewState {
            id,
            window: window.handle,
            focus_handle: cx.focus_handle(),
            background: Cell::new(Hsla::transparent_black()),
            shown_hitbox: Cell::new(None),
            reserved_actions: RefCell::default(),
            _attachment: attachment,
        });
        window.native_views.push(Rc::downgrade(&state));
        Ok(Self(state))
    }
}

impl NativeView {
    /// Sets the color shown where the view has not caught up with its element.
    ///
    /// The platform moves the view through its own compositor, so for a frame
    /// or two after its element moves or resizes, part of the element can be
    /// uncovered. A color close to the view's own background hides that. A
    /// window showing several native views uses the color of the one painted
    /// last.
    pub fn set_background(&self, color: impl Into<Hsla>, cx: &mut App) {
        let color = color.into();
        if self.0.background.replace(color) == color {
            return;
        }
        // The color is part of what the element paints.
        let window = self.0.window;
        cx.defer(move |cx| {
            window.update(cx, |_, window, _| window.refresh()).ok();
        });
    }

    /// Makes key bindings for `A` apply before the view sees the key.
    ///
    /// The view otherwise sees every key first and can keep any of them, so
    /// this is for the few bindings that must work whatever the view shows.
    /// The view never sees a key that such a binding starts with, even when the
    /// rest of the binding's keys don't follow.
    pub fn reserve_action<A: Action>(&self) {
        self.0
            .reserved_actions
            .borrow_mut()
            .insert(TypeId::of::<A>());
    }
}

impl Focusable for NativeView {
    fn focus_handle(&self, _: &App) -> FocusHandle {
        self.0.focus_handle.clone()
    }
}

impl IntoElement for NativeView {
    type Element = crate::Div;

    fn into_element(self) -> Self::Element {
        div()
            .track_focus(&self.0.focus_handle)
            .size_full()
            .child(NativeViewElement(self))
    }
}

struct NativeViewElement(NativeView);

impl Element for NativeViewElement {
    type RequestLayoutState = ();
    /// `None` in a window other than the one the view is attached to.
    type PrepaintState = Option<Hitbox>;

    fn id(&self) -> Option<ElementId> {
        None
    }

    fn source_location(&self) -> Option<&'static core::panic::Location<'static>> {
        None
    }

    fn request_layout(
        &mut self,
        _id: Option<&GlobalElementId>,
        _inspector_id: Option<&InspectorElementId>,
        window: &mut Window,
        cx: &mut App,
    ) -> (LayoutId, Self::RequestLayoutState) {
        let mut style = Style::default();
        style.size.width = crate::relative(1.).into();
        style.size.height = crate::relative(1.).into();
        (window.request_layout(style, [], cx), ())
    }

    fn prepaint(
        &mut self,
        _id: Option<&GlobalElementId>,
        _inspector_id: Option<&InspectorElementId>,
        bounds: Bounds<Pixels>,
        _request_layout: &mut Self::RequestLayoutState,
        window: &mut Window,
        _cx: &mut App,
    ) -> Option<Hitbox> {
        let state = &self.0.0;
        if state.window != window.handle {
            return None;
        }
        // The view is only where it is painted first, so a later occurrence
        // must not keep mouse input from what is behind it. It still has a
        // hitbox, which becomes the view's if a frame reuses this occurrence
        // after the first one has gone. An occurrence that is clipped away
        // paints nothing, and doesn't count as the first.
        let is_visible = !bounds.intersect(&window.content_mask().bounds).is_empty();
        let is_shown_elsewhere = state.shown_hitbox.get().is_some_and(|shown| {
            window
                .next_frame
                .hitboxes
                .iter()
                .any(|hitbox| hitbox.id == shown)
        });
        let shows_view = is_visible && !is_shown_elsewhere;
        let behavior = if shows_view {
            HitboxBehavior::BlockMouse
        } else {
            HitboxBehavior::Normal
        };
        let hitbox = window.insert_hitbox(bounds, behavior);
        if shows_view {
            state.shown_hitbox.set(Some(hitbox.id));
        }
        Some(hitbox)
    }

    fn paint(
        &mut self,
        _id: Option<&GlobalElementId>,
        _inspector_id: Option<&InspectorElementId>,
        _bounds: Bounds<Pixels>,
        _request_layout: &mut Self::RequestLayoutState,
        hitbox: &mut Option<Hitbox>,
        window: &mut Window,
        _cx: &mut App,
    ) {
        let Some(hitbox) = hitbox else {
            return;
        };
        let id = self.0.0.id;
        let bounds = window.snap_bounds(hitbox.bounds);
        let content_mask = window.snapped_content_mask();
        window.next_frame.scene.insert_primitive(PaintNativeView {
            order: 0,
            id,
            hitbox_id: hitbox.id,
            bounds,
            content_mask,
            background: self.0.0.background.get(),
        });
    }
}

impl Window {
    /// Tells the platform which native view, if any, the focused handle belongs to.
    pub(super) fn sync_native_view_focus(&mut self) {
        self.native_views.retain(|view| view.strong_count() > 0);
        if self.native_views.is_empty() {
            return;
        }
        let focused = self
            .focus
            .and_then(|focus| self.native_view_for_focus(focus));
        self.platform_window.set_focused_native_view(focused);
    }

    /// Ends an unfinished composition in the window's own content when keyboard
    /// input is about to move to a native view.
    ///
    /// The platform also asks for this as its own view loses keyboard input, but
    /// that request needs the app, which is already borrowed when focus is moved
    /// from inside an update. The entity being updated may also be the one that
    /// holds the composition, so this waits for the update to end.
    pub(super) fn end_composition_before_focusing_native_view(
        &mut self,
        focus: FocusId,
        cx: &mut App,
    ) {
        if self.native_view_for_focus(focus).is_none() {
            return;
        }
        self.defer(cx, |window, cx| {
            if let Some(mut input_handler) = window.platform_window.take_input_handler() {
                input_handler.unmark_text_during_update(window, cx);
                window.platform_window.set_input_handler(input_handler);
            }
        });
    }

    fn native_view_for_focus(&self, focus: FocusId) -> Option<NativeViewId> {
        self.native_views
            .iter()
            .filter_map(Weak::upgrade)
            .find(|view| view.focus_handle.id == focus)
            .map(|view| view.id)
    }

    /// The native view that a mouse event at this position reaches, if no
    /// GPUI hitbox is above it there.
    pub(super) fn hit_test_native_view(&self, position: Point<Pixels>) -> Option<NativeViewId> {
        let hit_test = self.rendered_frame.hit_test(position);
        let topmost = hit_test.ids.first()?;
        self.rendered_frame
            .scene
            .native_views
            .iter()
            .find(|native_view| native_view.hitbox_id == *topmost)
            .map(|native_view| native_view.id)
    }

    /// Dispatches a key press that the focused native view has not seen yet, if
    /// it continues a pending binding or is bound to an action the view reserved.
    pub(super) fn dispatch_key_down_before_native_view(
        &mut self,
        event: KeyDownEvent,
        cx: &mut App,
    ) -> bool {
        let Some(view) = self
            .native_views
            .iter()
            .filter_map(Weak::upgrade)
            .find(|view| Some(view.focus_handle.id) == self.focus)
        else {
            return false;
        };
        let node_id = self.focus_node_id_in_rendered_frame(self.focus);
        let dispatch_path = self.rendered_frame.dispatch_tree.dispatch_path(node_id);
        let dispatch_tree = &self.rendered_frame.dispatch_tree;
        let pending = self.pending_input_keystrokes();
        let is_pending = pending.is_some();
        let continues_pending = pending.is_some_and(|pending| {
            let mut input = pending.to_vec();
            input.push(event.keystroke.clone());
            let (bindings, is_pending, _) =
                dispatch_tree.bindings_for_input(&input, &dispatch_path);
            is_pending || !bindings.is_empty()
        });
        let is_reserved = !continues_pending && {
            let input = std::slice::from_ref(&event.keystroke);
            let (bindings, _, context_stack) =
                dispatch_tree.bindings_for_input(input, &dispatch_path);
            let reserved_actions = view.reserved_actions.borrow();
            let is_reserved = |binding: &KeyBinding| {
                reserved_actions.contains(&binding.action.as_any().type_id())
            };
            // The first key of a longer binding is reserved too, or the view
            // could keep that key and the binding would never begin.
            bindings.first().is_some_and(is_reserved)
                || dispatch_tree
                    .possible_next_bindings_for_input(input, &context_stack)
                    .iter()
                    .any(is_reserved)
        };
        if is_pending && !continues_pending {
            // The pending binding ends here, as it would in the window's own
            // content. Left pending, it would be completed by a later key even
            // after the view had used this one.
            self.flush_pending_input(cx);
        }
        if !continues_pending && !is_reserved {
            // The key is left for the view like any other. Dispatching it here
            // as well would dispatch it twice when the view then leaves it.
            return false;
        }
        // The key is GPUI's whether or not anything acts on it. Leaving it to
        // the view as well would have it dispatched again when the view left it.
        self.dispatch_event(PlatformInput::KeyDown(event), cx);
        true
    }

    /// Focuses the handle of a native view that the platform gave keyboard input to.
    pub(super) fn focus_native_view(&mut self, id: NativeViewId, cx: &mut App) {
        let view = self
            .native_views
            .iter()
            .filter_map(Weak::upgrade)
            .find(|view| view.id == id);
        if let Some(view) = view {
            self.focus(&view.focus_handle, cx);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        Context, Entity, FocusHandle, InputHandler, KeyBinding, Keystroke, Modifiers, MouseButton,
        Render, ScaledPixels, TestAppContext, UTF16Selection, VisualTestContext, WindowHandle,
        canvas, point, prelude::*, px, rgb,
    };
    use std::ops::Range;

    #[gpui::test]
    fn focusing_a_native_view_gives_it_keyboard_input(cx: &mut TestAppContext) {
        let window = open_window(cx);
        let platform_window = cx.test_window(window.into());

        let id = window
            .update(cx, |view, window, cx| {
                window.focus(&view.native_view.focus_handle(cx), cx);
                view.native_view.0.id
            })
            .unwrap();
        assert_eq!(platform_window.focused_native_view(), Some(id));

        window
            .update(cx, |view, window, cx| window.focus(&view.other_focus, cx))
            .unwrap();
        assert_eq!(platform_window.focused_native_view(), None);

        window
            .update(cx, |view, window, cx| {
                window.focus(&view.native_view.focus_handle(cx), cx);
                window.blur(cx);
            })
            .unwrap();
        assert_eq!(platform_window.focused_native_view(), None);
    }

    #[gpui::test]
    fn platform_focus_of_a_native_view_focuses_its_handle(cx: &mut TestAppContext) {
        let window = open_window(cx);
        let platform_window = cx.test_window(window.into());
        let id = window
            .update(cx, |view, window, cx| {
                window.focus(&view.other_focus, cx);
                view.native_view.0.id
            })
            .unwrap();

        platform_window.simulate_native_view_focused(id);

        window
            .update(cx, |view, window, cx| {
                assert!(view.native_view.focus_handle(cx).is_focused(window));
            })
            .unwrap();
        assert_eq!(platform_window.focused_native_view(), Some(id));
    }

    #[gpui::test]
    fn only_reserved_and_pending_bindings_apply_before_a_native_view(cx: &mut TestAppContext) {
        cx.update(|cx| {
            cx.bind_keys([
                KeyBinding::new("cmd-r", Reserved, None),
                KeyBinding::new("cmd-o", Ordinary, None),
                KeyBinding::new("cmd-k o", Ordinary, None),
                KeyBinding::new("cmd-j r", Reserved, None),
            ]);
        });
        let window = open_window(cx);
        let platform_window = cx.test_window(window.into());
        window
            .update(cx, |view, window, cx| {
                view.native_view.reserve_action::<Reserved>();
                window.focus(&view.native_view.focus_handle(cx), cx);
            })
            .unwrap();
        cx.run_until_parked();
        let press = |keystroke: &'static str| KeyDownEvent {
            keystroke: Keystroke::parse(keystroke).unwrap(),
            is_held: false,
            prefer_character_input: false,
        };
        let actions = |cx: &mut TestAppContext| {
            window
                .update(cx, |view, _, _| std::mem::take(&mut view.actions))
                .unwrap()
        };

        assert!(platform_window.simulate_key_down_before_native_view(press("cmd-r")));
        assert_eq!(actions(cx), ["reserved"]);

        // The view gets to see an ordinary binding's key first.
        assert!(!platform_window.simulate_key_down_before_native_view(press("cmd-o")));
        assert_eq!(actions(cx), [] as [&str; 0]);

        // Once the view has left the first key of a binding to GPUI, the rest
        // of that binding's keys are GPUI's.
        assert!(!platform_window.simulate_key_down_before_native_view(press("cmd-k")));
        cx.simulate_keystrokes(window.into(), "cmd-k");
        // A key that doesn't continue the binding ends it. The key is the
        // view's to see first, and isn't dispatched here, which would dispatch
        // it twice if the view then left it.
        assert!(!platform_window.simulate_key_down_before_native_view(press("x")));
        assert!(
            !window
                .update(cx, |_, window, _| window.has_pending_keystrokes())
                .unwrap()
        );
        let keys = |cx: &mut TestAppContext| {
            window
                .update(cx, |view, _, _| std::mem::take(&mut view.keys))
                .unwrap()
        };
        // The binding's first key is dispatched on its own, as it is when
        // the binding times out.
        assert_eq!(keys(cx), ["k"]);
        cx.simulate_keystrokes(window.into(), "x");
        assert_eq!(keys(cx), ["x"]);
        // Whether or not the view used that key, the rest of the binding's
        // keys no longer complete it, and are the view's again.
        assert!(!platform_window.simulate_key_down_before_native_view(press("o")));
        assert_eq!(actions(cx), [] as [&str; 0]);

        // The view never sees the first key of a reserved binding, which it
        // could otherwise keep.
        assert!(platform_window.simulate_key_down_before_native_view(press("cmd-j")));
        assert!(platform_window.simulate_key_down_before_native_view(press("r")));
        assert_eq!(actions(cx), ["reserved"]);

        // Nothing applies ahead of the window's own content.
        window
            .update(cx, |view, window, cx| window.focus(&view.other_focus, cx))
            .unwrap();
        assert!(!platform_window.simulate_key_down_before_native_view(press("cmd-r")));
    }

    #[gpui::test]
    fn mouse_reaches_a_native_view_only_where_nothing_covers_it(cx: &mut TestAppContext) {
        let window = open_window(cx);
        let platform_window = cx.test_window(window.into());
        let id = window
            .update(cx, |view, _, _| view.native_view.0.id)
            .unwrap();
        cx.run_until_parked();

        let uncovered = point(px(75.), px(75.));
        let covered = point(px(25.), px(25.));
        let outside = point(px(150.), px(150.));
        assert_eq!(platform_window.hit_test_native_view(uncovered), Some(id));
        assert_eq!(platform_window.hit_test_native_view(covered), None);
        assert_eq!(platform_window.hit_test_native_view(outside), None);
    }

    #[gpui::test]
    fn mouse_press_on_a_native_view_reaches_nothing_behind_it(cx: &mut TestAppContext) {
        let window = open_window(cx);
        cx.run_until_parked();
        let mut cx = VisualTestContext::from_window(window.into(), cx);

        // The platform passes on a press that it gives to the view.
        cx.simulate_mouse_down(
            point(px(75.), px(75.)),
            MouseButton::Left,
            Modifiers::default(),
        );

        let presses = window
            .update(&mut cx, |view, _, _| std::mem::take(&mut view.presses))
            .unwrap();
        assert_eq!(presses, ["outside cover"]);
    }

    #[gpui::test]
    fn changing_the_background_repaints_the_native_view(cx: &mut TestAppContext) {
        let window = open_window(cx);
        cx.run_until_parked();

        window
            .update(cx, |view, _, cx| {
                view.native_view.set_background(rgb(0x00ff00), cx);
            })
            .unwrap();
        cx.run_until_parked();

        window
            .update(cx, |_, window, _| {
                let backgrounds = window
                    .rendered_frame
                    .scene
                    .native_views
                    .iter()
                    .map(|native_view| native_view.background)
                    .collect::<Vec<_>>();
                assert_eq!(backgrounds, [Hsla::from(rgb(0x00ff00))]);
            })
            .unwrap();
    }

    #[gpui::test]
    fn native_view_painted_twice_shows_where_it_was_painted_first(cx: &mut TestAppContext) {
        let window = open_window(cx);
        window
            .update(cx, |view, _, cx| {
                view.paints_native_view_twice = true;
                cx.notify();
            })
            .unwrap();
        cx.run_until_parked();

        window
            .update(cx, |_, window, _| {
                let bounds = window
                    .rendered_frame
                    .scene
                    .native_views
                    .iter()
                    .map(|native_view| native_view.bounds.origin.x)
                    .collect::<Vec<_>>();
                assert_eq!(bounds, [ScaledPixels::from(0.)]);
            })
            .unwrap();

        // Where the view was painted again, it isn't shown and takes no mouse
        // input, so what is behind it there gets the press.
        let id = window
            .update(cx, |view, _, _| view.native_view.0.id)
            .unwrap();
        let platform_window = cx.test_window(window.into());
        let painted_first = point(px(75.), px(75.));
        let painted_again = point(px(250.), px(50.));
        assert_eq!(
            platform_window.hit_test_native_view(painted_first),
            Some(id)
        );
        assert_eq!(platform_window.hit_test_native_view(painted_again), None);
        let mut cx = VisualTestContext::from_window(window.into(), cx);
        cx.simulate_mouse_down(painted_again, MouseButton::Left, Modifiers::default());
        let presses = window
            .update(&mut cx, |view, _, _| std::mem::take(&mut view.presses))
            .unwrap();
        assert_eq!(presses, ["outside cover", "behind second occurrence"]);
    }

    #[gpui::test]
    fn native_view_is_drawn_in_paint_order(cx: &mut TestAppContext) {
        let window = open_window(cx);
        cx.run_until_parked();

        // The view shows through its container's background, and the element
        // painted after it covers it.
        window
            .update(cx, |_, window, _| {
                let labels = window
                    .rendered_frame
                    .scene
                    .batches()
                    .map(|batch| batch.label())
                    .collect::<Vec<_>>();
                assert_eq!(labels, ["quads (1)", "native views (1)", "quads (1)"]);
            })
            .unwrap();
    }

    #[gpui::test]
    fn native_view_stays_attached_until_its_last_handle_is_dropped(cx: &mut TestAppContext) {
        let window = open_window(cx);
        let platform_window = cx.test_window(window.into());
        let other_handle = window
            .update(cx, |view, _, _| view.native_view.clone())
            .unwrap();
        assert_eq!(platform_window.attached_native_view_count(), 1);

        window
            .update(cx, |view, window, cx| {
                view.native_view = new_native_view(window, cx);
            })
            .unwrap();
        assert_eq!(platform_window.attached_native_view_count(), 2);

        drop(other_handle);
        assert_eq!(platform_window.attached_native_view_count(), 1);
    }

    #[gpui::test]
    fn native_view_shows_nothing_in_another_window(cx: &mut TestAppContext) {
        let window = open_window(cx);
        let native_view = window
            .update(cx, |view, _, _| view.native_view.clone())
            .unwrap();
        let other_window = cx.add_window(|_, cx| TestView {
            native_view,
            other_focus: cx.focus_handle(),
            actions: Vec::new(),
            keys: Vec::new(),
            presses: Vec::new(),
            paints_native_view_twice: false,
            unmark_count: 0,
        });
        cx.run_until_parked();

        other_window
            .update(cx, |_, window, _| {
                assert!(window.rendered_frame.scene.native_views.is_empty());
            })
            .unwrap();
        window
            .update(cx, |_, window, _| {
                assert_eq!(window.rendered_frame.scene.native_views.len(), 1);
            })
            .unwrap();
    }

    #[gpui::test]
    fn focusing_a_native_view_ends_a_composition(cx: &mut TestAppContext) {
        let window = open_window(cx);
        window
            .update(cx, |view, window, cx| window.focus(&view.other_focus, cx))
            .unwrap();
        cx.run_until_parked();

        // The view that holds the composition moves focus from inside its own
        // update, so the composition can only be ended once that is over.
        window
            .update(cx, |view, window, cx| {
                window.focus(&view.native_view.focus_handle(cx), cx);
            })
            .unwrap();
        let unmark_count = window.update(cx, |view, _, _| view.unmark_count).unwrap();
        assert_eq!(unmark_count, 1);
    }

    actions!(native_view_tests, [Reserved, Ordinary]);

    struct TestView {
        native_view: NativeView,
        other_focus: FocusHandle,
        actions: Vec<&'static str>,
        /// The key presses dispatched to the window's content.
        keys: Vec<String>,
        presses: Vec<&'static str>,
        paints_native_view_twice: bool,
        /// How many times the input handler of the "cover" element was told to
        /// end its composition.
        unmark_count: usize,
    }

    impl Render for TestView {
        fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
            div()
                .size_full()
                .on_action(cx.listener(|this, _: &Reserved, _, _| this.actions.push("reserved")))
                .on_action(cx.listener(|this, _: &Ordinary, _, _| this.actions.push("ordinary")))
                .on_key_down(cx.listener(|this, event: &KeyDownEvent, _, _| {
                    this.keys.push(event.keystroke.key.clone())
                }))
                .child(
                    div()
                        .absolute()
                        .top_0()
                        .left_0()
                        .size(px(100.))
                        .bg(rgb(0xff0000))
                        .on_mouse_down(
                            MouseButton::Left,
                            cx.listener(|this, _, _, _| this.presses.push("behind view")),
                        )
                        .child(self.native_view.clone()),
                )
                .when(self.paints_native_view_twice, |this| {
                    this.child(
                        div()
                            .absolute()
                            .top_0()
                            .left(px(200.))
                            .size(px(100.))
                            .on_mouse_down(
                                MouseButton::Left,
                                cx.listener(|this, _, _, _| {
                                    this.presses.push("behind second occurrence")
                                }),
                            )
                            .child(self.native_view.clone()),
                    )
                })
                .child(
                    div()
                        .id("cover")
                        .track_focus(&self.other_focus)
                        .absolute()
                        .top_0()
                        .left_0()
                        .size(px(50.))
                        .occlude()
                        .bg(rgb(0x0000ff))
                        .on_mouse_down_out(
                            cx.listener(|this, _, _, _| this.presses.push("outside cover")),
                        )
                        .child({
                            let focus = self.other_focus.clone();
                            let view = cx.entity();
                            canvas(
                                |_, _, _| {},
                                move |_, _, window, cx| {
                                    window.handle_input(&focus, TestInput { view }, cx);
                                },
                            )
                            .size_full()
                        }),
                )
        }
    }

    /// Reaches its view as an element's input handler does, by updating it.
    struct TestInput {
        view: Entity<TestView>,
    }

    impl InputHandler for TestInput {
        fn selected_text_range(
            &mut self,
            _: bool,
            _: &mut Window,
            _: &mut App,
        ) -> Option<UTF16Selection> {
            None
        }

        fn marked_text_range(&mut self, _: &mut Window, _: &mut App) -> Option<Range<usize>> {
            None
        }

        fn text_for_range(
            &mut self,
            _: Range<usize>,
            _: &mut Option<Range<usize>>,
            _: &mut Window,
            _: &mut App,
        ) -> Option<String> {
            None
        }

        fn replace_text_in_range(
            &mut self,
            _: Option<Range<usize>>,
            _: &str,
            _: &mut Window,
            _: &mut App,
        ) {
        }

        fn replace_and_mark_text_in_range(
            &mut self,
            _: Option<Range<usize>>,
            _: &str,
            _: Option<Range<usize>>,
            _: &mut Window,
            _: &mut App,
        ) {
        }

        fn unmark_text(&mut self, _: &mut Window, cx: &mut App) {
            self.view.update(cx, |view, _| view.unmark_count += 1);
        }

        fn bounds_for_range(
            &mut self,
            _: Range<usize>,
            _: &mut Window,
            _: &mut App,
        ) -> Option<Bounds<Pixels>> {
            None
        }

        fn character_index_for_point(
            &mut self,
            _: Point<Pixels>,
            _: &mut Window,
            _: &mut App,
        ) -> Option<usize> {
            None
        }
    }

    fn open_window(cx: &mut TestAppContext) -> WindowHandle<TestView> {
        cx.add_window(|window, cx| TestView {
            native_view: new_native_view(window, cx),
            other_focus: cx.focus_handle(),
            actions: Vec::new(),
            keys: Vec::new(),
            presses: Vec::new(),
            paints_native_view_twice: false,
            unmark_count: 0,
        })
    }

    fn new_native_view(window: &mut Window, cx: &mut App) -> NativeView {
        // SAFETY: The test platform never reads the view it is given.
        unsafe { NativeView::new(NonNull::dangling(), window, cx) }
            .expect("the test platform attaches native views")
    }
}