1use gpui::{
2 Anchor, Animation, AnimationExt as _, AnyElement, App, Bounds, Context, Div, ElementId,
3 FocusHandle, InteractiveElement as _, IntoElement, MouseButton, ParentElement, PathBuilder,
4 Pixels, Point, RenderOnce, Stateful, StyleRefinement, Styled, Window, canvas, point,
5 prelude::FluentBuilder as _, px,
6};
7use std::{cell::Cell, rc::Rc, time::Duration};
8
9use crate::{ActiveTheme as _, ThemeStyled as _};
10use crate::{
11 Selectable,
12 animation::ease_out_cubic,
13 styled::{popover_ring, popover_shadow},
14 v_flex,
15};
16use gpui_base::Placement;
17use gpui_base::Popover as BasePopover;
18pub use gpui_base::PopoverState;
19
20pub(crate) fn init(_: &mut App) {}
21
22const DROPDOWN_ENTER_DURATION: Duration = Duration::from_millis(150);
27
28const DROPDOWN_ENTER_OFFSET: Pixels = px(-8.);
34
35fn dropdown_positioner(bounds: Bounds<Pixels>) -> gpui_base::Positioner {
36 gpui_base::Positioner::side(bounds)
37 .placement(gpui_base::Placement::Bottom)
38 .align(gpui_base::Align::Start)
39 .offset(px(6.))
40 .margin(px(8.))
41}
42
43pub(crate) fn dropdown_popup(
85 id: impl Into<ElementId>,
86 bounds: Bounds<Pixels>,
87 surface: impl IntoElement + Styled + 'static,
88 cx: &App,
89) -> gpui_base::Positioner {
90 let travel: f32 = DROPDOWN_ENTER_OFFSET.into();
91 let ring = popover_ring(cx);
93
94 dropdown_positioner(bounds).child(surface.with_animation(
95 id,
96 Animation::new(DROPDOWN_ENTER_DURATION).with_easing(ease_out_cubic),
97 move |surface, delta| {
98 surface
99 .top(px(travel * (1. - delta)))
100 .opacity(delta)
101 .shadow(popover_shadow(ring, delta * delta * delta))
102 },
103 ))
104}
105
106#[derive(IntoElement)]
108pub struct Popover {
109 id: ElementId,
110 style: StyleRefinement,
111 anchor: Anchor,
112 offset: Option<Pixels>,
113 arrow: bool,
114 default_open: bool,
115 open: Option<bool>,
116 tracked_focus_handle: Option<FocusHandle>,
117 trigger: Option<Box<dyn FnOnce(bool, &Window, &App) -> AnyElement + 'static>>,
118 content: Option<
119 Rc<
120 dyn Fn(&mut PopoverState, &mut Window, &mut Context<PopoverState>) -> AnyElement
121 + 'static,
122 >,
123 >,
124 children: Vec<AnyElement>,
125 trigger_style: Option<StyleRefinement>,
128 mouse_button: MouseButton,
129 appearance: bool,
130 overlay_closable: bool,
131 on_open_change: Option<Rc<dyn Fn(&bool, &mut Window, &mut App)>>,
132}
133
134impl Popover {
135 pub fn new(id: impl Into<ElementId>) -> Self {
137 Self {
138 id: id.into(),
139 style: StyleRefinement::default(),
140 anchor: Anchor::TopLeft,
141 offset: None,
142 arrow: false,
143 trigger: None,
144 trigger_style: None,
145 content: None,
146 tracked_focus_handle: None,
147 children: vec![],
148 mouse_button: MouseButton::Left,
149 appearance: true,
150 overlay_closable: true,
151 default_open: false,
152 open: None,
153 on_open_change: None,
154 }
155 }
156
157 pub fn anchor(mut self, anchor: impl Into<Anchor>) -> Self {
163 self.anchor = anchor.into();
164 self
165 }
166
167 pub fn offset(mut self, offset: impl Into<Pixels>) -> Self {
170 self.offset = Some(offset.into());
171 self
172 }
173
174 pub fn arrow(mut self, arrow: bool) -> Self {
178 self.arrow = arrow;
179 self
180 }
181
182 pub fn mouse_button(mut self, mouse_button: MouseButton) -> Self {
184 self.mouse_button = mouse_button;
185 self
186 }
187
188 pub fn trigger<T>(mut self, trigger: T) -> Self
190 where
191 T: Selectable + IntoElement + 'static,
192 {
193 self.trigger = Some(Box::new(|is_open, _, _| {
194 let open = trigger.is_open();
195 trigger.open(open || is_open).into_any_element()
196 }));
197 self
198 }
199
200 pub fn default_open(mut self, open: bool) -> Self {
206 self.default_open = open;
207 self
208 }
209
210 pub fn open(mut self, open: bool) -> Self {
216 self.open = Some(open);
217 self
218 }
219
220 pub fn on_open_change<F>(mut self, callback: F) -> Self
226 where
227 F: Fn(&bool, &mut Window, &mut App) + 'static,
228 {
229 self.on_open_change = Some(Rc::new(callback));
230 self
231 }
232
233 pub fn trigger_style(mut self, style: StyleRefinement) -> Self {
239 self.trigger_style = Some(style);
240 self
241 }
242
243 pub fn overlay_closable(mut self, closable: bool) -> Self {
245 self.overlay_closable = closable;
246 self
247 }
248
249 pub fn content<F, E>(mut self, content: F) -> Self
254 where
255 E: IntoElement,
256 F: Fn(&mut PopoverState, &mut Window, &mut Context<PopoverState>) -> E + 'static,
257 {
258 self.content = Some(Rc::new(move |state, window, cx| {
259 content(state, window, cx).into_any_element()
260 }));
261 self
262 }
263
264 pub fn appearance(mut self, appearance: bool) -> Self {
271 self.appearance = appearance;
272 self
273 }
274
275 pub fn track_focus(mut self, handle: &FocusHandle) -> Self {
280 self.tracked_focus_handle = Some(handle.clone());
281 self
282 }
283}
284
285impl ParentElement for Popover {
286 fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
287 self.children.extend(elements);
288 }
289}
290
291impl Styled for Popover {
292 fn style(&mut self) -> &mut StyleRefinement {
293 &mut self.style
294 }
295}
296
297impl Popover {
298 pub(crate) fn render_popover_content(
299 anchor: Anchor,
300 appearance: bool,
301 _: &mut Window,
302 cx: &mut App,
303 ) -> Stateful<Div> {
304 v_flex()
305 .id("content")
306 .occlude()
307 .tab_group()
308 .when(appearance, |this| this.popover_style(cx).p_3())
309 .map(|this| match anchor {
310 Anchor::TopLeft | Anchor::TopCenter | Anchor::TopRight => this.top_1(),
311 Anchor::BottomLeft | Anchor::BottomCenter | Anchor::BottomRight => this.bottom_1(),
312 Anchor::LeftCenter | Anchor::RightCenter => this.top_1(), })
314 }
315}
316
317impl RenderOnce for Popover {
318 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
319 let anchor = self.anchor;
320 let arrow_size = if self.arrow {
321 window.rem_size() * 0.375
322 } else {
323 px(0.)
324 };
325 let offset = self.offset.unwrap_or(window.rem_size() * 0.25) + arrow_size;
326 let resolved = Rc::new(Cell::new(None));
327 let arrow_position = resolved.clone();
328 let arrow = self.arrow;
329 let background = self
330 .style
331 .background
332 .as_ref()
333 .and_then(gpui::Fill::color)
334 .unwrap_or_else(|| cx.theme().popover.into());
335 let ring = popover_ring(cx);
336 let radius = cx.theme().radius;
337 let appearance = self.appearance;
338 let style = self.style;
339 let children = self.children;
340 let content = self.content;
341 let trigger_style = self.trigger_style;
342
343 BasePopover::new(self.id)
344 .anchor(self.anchor)
345 .offset(offset)
346 .on_position(move |position, trigger| resolved.set(Some((position, trigger))))
347 .mouse_button(self.mouse_button)
348 .default_open(self.default_open)
349 .overlay_closable(self.overlay_closable)
350 .content(move |state, window, cx| {
351 v_flex()
352 .id("content")
353 .occlude()
354 .tab_group()
355 .when(appearance, |this| this.popover_style(cx).p_3())
356 .when_some(content, |this, content| {
357 this.child((content)(state, window, cx))
358 })
359 .children(children)
360 .refine_style(&style)
361 .when(arrow, |this| {
362 this.child(
363 canvas(
364 |_, _, _| {},
365 move |bounds, _, window, _| {
366 let Some((_, trigger)) = arrow_position.get() else {
367 return;
368 };
369 let (side, target) = arrow_anchor(anchor, trigger);
370 let trigger = Bounds::new(target, gpui::size(px(0.), px(0.)));
371 let points =
372 arrow_points(bounds, trigger, side, arrow_size, radius);
373 let mut fill = PathBuilder::fill();
374 fill.move_to(points[0]);
375 fill.line_to(points[1]);
376 fill.line_to(points[2]);
377 fill.close();
378 if let Ok(path) = fill.build() {
379 window.paint_path(path, background);
380 }
381 window.paint_quad(
385 gpui::PaintQuad::new(arrow_join_bounds(
386 points,
387 side,
388 px(1.),
389 ))
390 .background(background),
391 );
392 if appearance {
393 let mut outline = PathBuilder::stroke(px(1.));
394 outline.move_to(points[0]);
395 outline.line_to(points[1]);
396 outline.line_to(points[2]);
397 if let Ok(path) = outline.build() {
398 window.paint_path(path, ring);
399 }
400 }
401 },
402 )
403 .absolute()
404 .inset_0()
405 .size_full(),
406 )
407 })
408 })
409 .when_some(self.trigger, |this, trigger| this.trigger_with(trigger))
410 .when_some(trigger_style, |this, style| this.refine_style(&style))
411 .when_some(self.open, |this, open| this.open(open))
412 .when_some(self.tracked_focus_handle, |this, handle| {
413 this.track_focus(&handle)
414 })
415 .when_some(self.on_open_change, |this, callback| {
416 this.on_open_change(move |open, window, cx| callback(open, window, cx))
417 })
418 .into_any_element()
419 }
420}
421
422fn arrow_anchor(anchor: Anchor, trigger: Bounds<Pixels>) -> (Placement, Point<Pixels>) {
424 match anchor {
425 Anchor::TopLeft => (Placement::Bottom, trigger.bottom_left()),
426 Anchor::TopCenter => (Placement::Bottom, trigger.bottom_center()),
427 Anchor::TopRight => (Placement::Bottom, trigger.bottom_right()),
428 Anchor::BottomLeft => (Placement::Top, trigger.origin),
429 Anchor::BottomCenter => (Placement::Top, trigger.top_center()),
430 Anchor::BottomRight => (Placement::Top, trigger.top_right()),
431 Anchor::LeftCenter => (Placement::Right, trigger.right_center()),
432 Anchor::RightCenter => (Placement::Left, trigger.left_center()),
433 }
434}
435
436fn arrow_points(
438 surface: Bounds<Pixels>,
439 trigger: Bounds<Pixels>,
440 side: Placement,
441 depth: Pixels,
442 radius: Pixels,
443) -> [Point<Pixels>; 3] {
444 let horizontal = side.is_horizontal();
445 let (start, end, target) = if horizontal {
446 (surface.top(), surface.bottom(), trigger.center().y)
447 } else {
448 (surface.left(), surface.right(), trigger.center().x)
449 };
450 let half = depth.min((end - start) * 0.5);
451 let inset = (radius + half).min((end - start) * 0.5);
452 let center = target.clamp(start + inset, end - inset);
453 match side {
454 Placement::Bottom => [
455 point(center - half, surface.top()),
456 point(center, surface.top() - depth),
457 point(center + half, surface.top()),
458 ],
459 Placement::Top => [
460 point(center - half, surface.bottom()),
461 point(center, surface.bottom() + depth),
462 point(center + half, surface.bottom()),
463 ],
464 Placement::Right => [
465 point(surface.left(), center - half),
466 point(surface.left() - depth, center),
467 point(surface.left(), center + half),
468 ],
469 Placement::Left => [
470 point(surface.right(), center - half),
471 point(surface.right() + depth, center),
472 point(surface.right(), center + half),
473 ],
474 }
475}
476
477fn arrow_join_bounds(
478 points: [Point<Pixels>; 3],
479 side: Placement,
480 stroke: Pixels,
481) -> Bounds<Pixels> {
482 if side.is_horizontal() {
485 let inset = stroke.min((points[2].y - points[0].y) * 0.5);
486 Bounds::from_corners(
487 point(points[0].x - stroke, points[0].y + inset),
488 point(points[2].x + stroke, points[2].y - inset),
489 )
490 } else {
491 let inset = stroke.min((points[2].x - points[0].x) * 0.5);
492 Bounds::from_corners(
493 point(points[0].x + inset, points[0].y - stroke),
494 point(points[2].x - inset, points[2].y + stroke),
495 )
496 }
497}
498
499#[cfg(test)]
500mod tests {
501 use super::*;
502 use crate::{button::Button, h_flex, theme::Theme};
503 use gpui::{Bounds, Context, MouseButton, Point, Render, div, point, px, size};
504 use gpui_base::Popup as BasePopup;
505 use std::{cell::RefCell, rc::Rc};
506
507 #[test]
508 fn test_popover_builder_chaining() {
509 let popover = Popover::new("test")
510 .anchor(Anchor::BottomCenter)
511 .mouse_button(MouseButton::Right)
512 .default_open(true)
513 .appearance(false)
514 .overlay_closable(false);
515
516 assert_eq!(popover.anchor, Anchor::BottomCenter);
517 assert_eq!(popover.mouse_button, MouseButton::Right);
518 assert!(popover.default_open);
519 assert!(!popover.appearance);
520 assert!(!popover.overlay_closable);
521 }
522
523 #[test]
524 fn test_resolved_corner_top_positions() {
525 use gpui::px;
526
527 let bounds = Bounds {
528 origin: Point {
529 x: px(100.),
530 y: px(100.),
531 },
532 size: gpui::Size {
533 width: px(200.),
534 height: px(50.),
535 },
536 };
537
538 let pos = BasePopup::resolved_corner(Anchor::TopLeft, bounds);
539 assert_eq!(pos.x, px(100.));
540 assert_eq!(pos.y, px(100.));
541
542 let pos = BasePopup::resolved_corner(Anchor::TopCenter, bounds);
543 assert_eq!(pos.x, px(200.));
544 assert_eq!(pos.y, px(100.));
545
546 let pos = BasePopup::resolved_corner(Anchor::TopRight, bounds);
547 assert_eq!(pos.x, px(300.));
548 assert_eq!(pos.y, px(100.));
549
550 let pos = BasePopup::resolved_corner(Anchor::BottomLeft, bounds);
551 assert_eq!(pos.x, px(100.));
552 assert_eq!(pos.y, px(50.));
553
554 let pos = BasePopup::resolved_corner(Anchor::BottomCenter, bounds);
555 assert_eq!(pos.x, px(200.));
556 assert_eq!(pos.y, px(50.));
557
558 let pos = BasePopup::resolved_corner(Anchor::BottomRight, bounds);
559 assert_eq!(pos.x, px(300.));
560 assert_eq!(pos.y, px(50.));
561 }
562
563 struct PopoverHarness {
564 changes: Rc<RefCell<Vec<bool>>>,
565 }
566
567 struct AnchorHarness {
568 anchor: Anchor,
569 offset: Option<Pixels>,
570 origin: Point<Pixels>,
571 arrow: bool,
572 }
573
574 impl Render for AnchorHarness {
575 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
576 div().size_full().child(
577 div()
578 .absolute()
579 .left(self.origin.x)
580 .top(self.origin.y)
581 .child(
582 Popover::new("positioned-popover")
583 .default_open(true)
584 .appearance(false)
585 .arrow(self.arrow)
586 .anchor(self.anchor)
587 .when_some(self.offset, |this, gap| this.offset(gap))
588 .trigger(Button::new("positioned-trigger").size(px(40.)))
589 .child(
590 div()
591 .debug_selector(|| "positioned-content".into())
592 .size(px(60.)),
593 ),
594 ),
595 )
596 }
597 }
598
599 #[gpui::test]
600 fn anchor_and_offset_position_the_surface_on_each_trigger_edge(cx: &mut gpui::TestAppContext) {
601 cx.update(crate::init);
602 let (view, window) = cx.add_window_view(|_, _| AnchorHarness {
603 anchor: Anchor::TopLeft,
604 offset: None,
605 origin: point(px(200.), px(200.)),
606 arrow: false,
607 });
608 window.update(|window, cx| window.draw(cx).clear(cx));
609 window.update(|window, cx| window.draw(cx).clear(cx));
610 let legacy = window.debug_bounds("positioned-content").unwrap();
612 assert_eq!(legacy.left(), px(200.));
613 assert_eq!(legacy.top(), px(244.));
614
615 for (side, x, y) in [
616 (Anchor::BottomLeft, 200., 128.),
617 (Anchor::BottomCenter, 190., 128.),
618 (Anchor::BottomRight, 180., 128.),
619 (Anchor::TopLeft, 200., 252.),
620 (Anchor::TopCenter, 190., 252.),
621 (Anchor::TopRight, 180., 252.),
622 (Anchor::RightCenter, 128., 190.),
623 (Anchor::LeftCenter, 252., 190.),
624 ] {
625 window.update(|window, cx| {
626 view.update(cx, |view, cx| {
627 view.anchor = side;
628 view.offset = Some(px(12.));
629 cx.notify();
630 });
631 window.draw(cx).clear(cx);
632 });
633 assert_eq!(
634 window.debug_bounds("positioned-content").unwrap().origin,
635 point(px(x), px(y)),
636 "{side:?}"
637 );
638 }
639 window.update(|window, cx| {
641 view.update(cx, |view, cx| {
642 view.origin = point(px(260.), px(240.));
643 cx.notify();
644 });
645 window.draw(cx).clear(cx);
646 });
647 assert_eq!(
648 window.debug_bounds("positioned-content").unwrap().origin,
649 point(px(312.), px(230.))
650 );
651 }
652
653 struct TriggerStyleHarness {
654 styled: bool,
655 }
656
657 impl Render for TriggerStyleHarness {
658 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
659 div().size_full().child(
662 h_flex()
663 .absolute()
664 .left(px(100.))
665 .top(px(100.))
666 .w(px(200.))
667 .child(
668 Popover::new("trigger-style-popover")
669 .default_open(true)
670 .appearance(false)
671 .offset(px(0.))
672 .anchor(Anchor::TopRight)
673 .when(self.styled, |this| {
674 this.trigger_style(StyleRefinement::default().w_full())
675 })
676 .trigger(Button::new("styled-trigger").size(px(40.)))
677 .child(
678 div()
679 .debug_selector(|| "styled-content".into())
680 .size(px(20.)),
681 ),
682 ),
683 )
684 }
685 }
686
687 #[gpui::test]
691 fn trigger_style_is_applied_to_the_trigger_container(cx: &mut gpui::TestAppContext) {
692 cx.update(crate::init);
693 let (view, window) = cx.add_window_view(|_, _| TriggerStyleHarness { styled: false });
694 window.update(|window, cx| window.draw(cx).clear(cx));
695 window.update(|window, cx| window.draw(cx).clear(cx));
696 assert_eq!(
699 window.debug_bounds("styled-content").unwrap().origin,
700 point(px(120.), px(140.))
701 );
702
703 window.update(|window, cx| {
704 view.update(cx, |view, cx| {
705 view.styled = true;
706 cx.notify();
707 });
708 window.draw(cx).clear(cx);
709 });
710 window.update(|window, cx| window.draw(cx).clear(cx));
711 assert_eq!(
714 window.debug_bounds("styled-content").unwrap().origin,
715 point(px(280.), px(140.))
716 );
717 }
718
719 #[gpui::test]
720 fn arrow_reserves_space_without_changing_anchor_alignment(cx: &mut gpui::TestAppContext) {
721 cx.update(crate::init);
722 let (view, window) = cx.add_window_view(|_, _| AnchorHarness {
723 anchor: Anchor::TopLeft,
724 offset: Some(px(12.)),
725 origin: point(px(200.), px(8.)),
726 arrow: true,
727 });
728 window.update(|window, cx| window.draw(cx).clear(cx));
729 window.update(|window, cx| window.draw(cx).clear(cx));
730 assert_eq!(
732 window.debug_bounds("positioned-content").unwrap().origin,
733 point(px(200.), px(66.))
734 );
735 window.update(|window, cx| {
736 view.update(cx, |view, cx| {
737 view.anchor = Anchor::TopRight;
738 view.arrow = false;
739 cx.notify();
740 });
741 window.draw(cx).clear(cx);
742 });
743 assert_eq!(
744 window.debug_bounds("positioned-content").unwrap().origin,
745 point(px(180.), px(60.))
746 );
747 }
748
749 #[gpui::test]
750 fn anchor_does_not_flip_when_offset_or_arrow_is_enabled(cx: &mut gpui::TestAppContext) {
751 cx.update(crate::init);
752 let (_, window) = cx.add_window_view(|_, _| AnchorHarness {
753 anchor: Anchor::BottomCenter,
754 offset: Some(px(12.)),
755 origin: point(px(200.), px(8.)),
756 arrow: true,
757 });
758 window.update(|window, cx| window.draw(cx).clear(cx));
759 window.update(|window, cx| window.draw(cx).clear(cx));
760 assert_eq!(
762 window.debug_bounds("positioned-content").unwrap().top(),
763 px(8.)
764 );
765 }
766
767 #[test]
768 fn arrow_alignment_uses_the_anchor_instead_of_trigger_center() {
769 let trigger = Bounds::new(point(px(120.), px(120.)), size(px(40.), px(20.)));
770 for (anchor, side, target) in [
771 (
772 Anchor::TopLeft,
773 Placement::Bottom,
774 point(px(120.), px(140.)),
775 ),
776 (
777 Anchor::TopCenter,
778 Placement::Bottom,
779 point(px(140.), px(140.)),
780 ),
781 (
782 Anchor::TopRight,
783 Placement::Bottom,
784 point(px(160.), px(140.)),
785 ),
786 (
787 Anchor::BottomLeft,
788 Placement::Top,
789 point(px(120.), px(120.)),
790 ),
791 (
792 Anchor::BottomCenter,
793 Placement::Top,
794 point(px(140.), px(120.)),
795 ),
796 (
797 Anchor::BottomRight,
798 Placement::Top,
799 point(px(160.), px(120.)),
800 ),
801 (
802 Anchor::LeftCenter,
803 Placement::Right,
804 point(px(160.), px(130.)),
805 ),
806 (
807 Anchor::RightCenter,
808 Placement::Left,
809 point(px(120.), px(130.)),
810 ),
811 ] {
812 assert_eq!(arrow_anchor(anchor, trigger), (side, target));
813 }
814 }
815
816 #[test]
817 fn arrows_point_toward_the_trigger_on_every_resolved_side() {
818 let surface = Bounds::new(point(px(100.), px(100.)), size(px(80.), px(60.)));
819 for (side, trigger, tip) in [
820 (
821 Placement::Bottom,
822 Bounds::new(point(px(120.), px(50.)), size(px(40.), px(20.))),
823 point(px(140.), px(94.)),
824 ),
825 (
826 Placement::Top,
827 Bounds::new(point(px(120.), px(180.)), size(px(40.), px(20.))),
828 point(px(140.), px(166.)),
829 ),
830 (
831 Placement::Right,
832 Bounds::new(point(px(40.), px(120.)), size(px(40.), px(20.))),
833 point(px(94.), px(130.)),
834 ),
835 (
836 Placement::Left,
837 Bounds::new(point(px(200.), px(120.)), size(px(40.), px(20.))),
838 point(px(186.), px(130.)),
839 ),
840 ] {
841 assert_eq!(arrow_points(surface, trigger, side, px(6.), px(4.))[1], tip);
842 }
843 let clamped = arrow_points(
844 surface,
845 Bounds::new(point(px(0.), px(50.)), size(px(20.), px(20.))),
846 Placement::Bottom,
847 px(6.),
848 px(4.),
849 );
850 assert_eq!(clamped[0], point(px(104.), px(100.)));
851 assert_eq!(clamped[1], point(px(110.), px(94.)));
852 }
853
854 #[test]
855 fn arrow_join_covers_both_sides_of_the_surface_edge() {
856 let surface = Bounds::new(point(px(100.), px(100.)), size(px(80.), px(60.)));
857 let trigger = Bounds::new(point(px(120.), px(120.)), size(px(40.), px(20.)));
858 for (side, expected) in [
859 (
860 Placement::Bottom,
861 Bounds::new(point(px(135.), px(99.)), size(px(10.), px(2.))),
862 ),
863 (
864 Placement::Top,
865 Bounds::new(point(px(135.), px(159.)), size(px(10.), px(2.))),
866 ),
867 (
868 Placement::Right,
869 Bounds::new(point(px(99.), px(125.)), size(px(2.), px(10.))),
870 ),
871 (
872 Placement::Left,
873 Bounds::new(point(px(179.), px(125.)), size(px(2.), px(10.))),
874 ),
875 ] {
876 let points = arrow_points(surface, trigger, side, px(6.), px(4.));
877 assert_eq!(arrow_join_bounds(points, side, px(1.)), expected);
878 }
879 }
880
881 impl Render for PopoverHarness {
882 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
883 let changes = self.changes.clone();
884 Popover::new("runtime-popover")
885 .trigger(Button::new("runtime-trigger").label("Open").size(px(100.)))
886 .content(|_, _, _| {
887 div()
888 .debug_selector(|| "runtime-popover-content".into())
889 .size(px(40.))
890 })
891 .on_open_change(move |open, _, _| changes.borrow_mut().push(*open))
892 }
893 }
894
895 #[gpui::test]
896 fn pointer_open_and_outside_dismiss_use_the_base_popup_host(cx: &mut gpui::TestAppContext) {
897 cx.update(|cx| {
898 gpui_base::GlobalState::init(cx);
899 cx.set_global(Theme::default());
900 init(cx);
901 });
902
903 let changes = Rc::new(RefCell::new(Vec::new()));
904 let (_, cx) = cx.add_window_view({
905 let changes = changes.clone();
906 move |_, _| PopoverHarness { changes }
907 });
908 cx.update(|window, cx| window.draw(cx).clear(cx));
909
910 cx.simulate_click(point(px(20.), px(20.)), Default::default());
911 cx.update(|window, cx| window.draw(cx).clear(cx));
912 assert!(cx.debug_bounds("runtime-popover-content").is_some());
913
914 cx.simulate_click(point(px(300.), px(300.)), Default::default());
915 cx.update(|window, cx| window.draw(cx).clear(cx));
916 assert!(cx.debug_bounds("runtime-popover-content").is_none());
917 assert_eq!(&*changes.borrow(), &[true, false]);
920 }
921
922 struct DefaultOpenHarness;
923
924 impl Render for DefaultOpenHarness {
925 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
926 Popover::new("default-open-popover")
927 .default_open(true)
928 .trigger(Button::new("default-open-trigger").label("Open"))
929 .child(
930 div()
931 .debug_selector(|| "default-open-content".into())
932 .size(px(40.)),
933 )
934 }
935 }
936
937 #[gpui::test]
938 fn default_open_is_forwarded_to_the_base_popover(cx: &mut gpui::TestAppContext) {
939 cx.update(|cx| {
940 gpui_base::GlobalState::init(cx);
941 cx.set_global(Theme::default());
942 init(cx);
943 });
944 let (_, cx) = cx.add_window_view(|_, _| DefaultOpenHarness);
945 cx.update(|window, cx| window.draw(cx).clear(cx));
946 cx.update(|window, cx| window.draw(cx).clear(cx));
947 assert!(cx.debug_bounds("default-open-content").is_some());
948 }
949
950 struct Harness {
951 open: bool,
952 }
953
954 impl Render for Harness {
955 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
956 div().size_full().when(self.open, |this| {
957 this.child(dropdown_popup(
958 "dropdown",
959 Bounds::new(point(px(0.), px(100.)), size(px(120.), px(30.))),
960 div().debug_selector(|| "surface".into()).size(px(50.)),
961 cx,
962 ))
963 })
964 }
965 }
966
967 #[gpui::test]
972 fn the_enter_motion_starts_over_every_time_the_dropdown_opens(cx: &mut gpui::TestAppContext) {
973 cx.update(crate::init);
974 let executor = cx.executor();
975 let (view, window) = cx.add_window_view(|_, _| Harness { open: true });
976
977 window.update(|window, cx| window.draw(cx).clear(cx));
978 let opening = window.debug_bounds("surface").unwrap().origin;
979
980 executor.advance_clock(DROPDOWN_ENTER_DURATION * 4);
982 window.update(|window, cx| window.draw(cx).clear(cx));
983 let settled = window.debug_bounds("surface").unwrap().origin;
984
985 assert!(
986 opening.y < settled.y,
987 "the surface should slide down into place, from {opening:?} to {settled:?}",
988 );
989
990 for open in [false, true] {
991 window.update(|window, cx| {
992 view.update(cx, |this, cx| {
993 this.open = open;
994 cx.notify();
995 });
996 window.draw(cx).clear(cx);
997 });
998 }
999
1000 let reopening = window.debug_bounds("surface").unwrap().origin;
1001 assert!(
1002 reopening.y < settled.y,
1003 "reopening should start the motion over at {opening:?} rather than showing a \
1004 settled surface, but the first frame was already at {reopening:?}",
1005 );
1006 }
1007}