popover.rs

 1use gpui::{
 2    div, AnyElement, 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    fn render(self, cx: &mut WindowContext) -> impl IntoElement {
45        div()
46            .flex()
47            .gap_1()
48            .child(v_stack().elevation_2(cx).px_1().children(self.children))
49            .when_some(self.aside, |this, aside| {
50                this.child(
51                    v_stack()
52                        .elevation_2(cx)
53                        .bg(cx.theme().colors().surface_background)
54                        .px_1()
55                        .child(aside),
56                )
57            })
58    }
59}
60
61impl Popover {
62    pub fn new() -> Self {
63        Self {
64            children: SmallVec::new(),
65            aside: None,
66        }
67    }
68
69    pub fn aside(mut self, aside: impl IntoElement) -> Self
70    where
71        Self: Sized,
72    {
73        self.aside = Some(aside.into_element().into_any());
74        self
75    }
76}
77
78impl ParentElement for Popover {
79    fn children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]> {
80        &mut self.children
81    }
82}