1use gpui::{
2 div, AnyElement, Div, Element, ElementId, IntoElement, ParentElement, RenderOnce, Styled,
3 WindowContext,
4};
5use smallvec::SmallVec;
6
7use crate::prelude::*;
8use crate::v_stack;
9
10/// A popover is used to display a menu or show some options.
11///
12/// Clicking the element that launches the popover should not change the current view,
13/// and the popover should be statically positioned relative to that element (not the
14/// user's mouse.)
15///
16/// Example: A "new" menu with options like "new file", "new folder", etc,
17/// Linear's "Display" menu, a profile menu that appers when you click your avatar.
18///
19/// Related elements:
20///
21/// `ContextMenu`:
22///
23/// Used to display a popover menu that only contains a list of items. Context menus are always
24/// launched by secondary clicking on an element. The menu is positioned relative to the user's cursor.
25///
26/// Example: Right clicking a file in the file tree to get a list of actions, right clicking
27/// a tab to in the tab bar to get a list of actions.
28///
29/// `Dropdown`:
30///
31/// Used to display a list of options when the user clicks an element. The menu is
32/// positioned relative the element that was clicked, and clicking an item in the
33/// dropdown should change the value of the element that was clicked.
34///
35/// Example: A theme select control. Displays "One Dark", clicking it opens a list of themes.
36/// When one is selected, the theme select control displays the selected theme.
37#[derive(IntoElement)]
38pub struct Popover {
39 children: SmallVec<[AnyElement; 2]>,
40 aside: Option<AnyElement>,
41}
42
43impl RenderOnce for Popover {
44 type Rendered = Div;
45
46 fn render(self, cx: &mut WindowContext) -> Self::Rendered {
47 div()
48 .flex()
49 .gap_1()
50 .child(v_stack().elevation_2(cx).px_1().children(self.children))
51 .when_some(self.aside, |this, aside| {
52 this.child(
53 v_stack()
54 .elevation_2(cx)
55 .bg(cx.theme().colors().surface_background)
56 .px_1()
57 .child(aside),
58 )
59 })
60 }
61}
62
63impl Popover {
64 pub fn new() -> Self {
65 Self {
66 children: SmallVec::new(),
67 aside: None,
68 }
69 }
70
71 pub fn aside(mut self, aside: impl IntoElement) -> Self
72 where
73 Self: Sized,
74 {
75 self.aside = Some(aside.into_element().into_any());
76 self
77 }
78}
79
80impl ParentElement for Popover {
81 fn children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]> {
82 &mut self.children
83 }
84}