popover.rs

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