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        h_flex()
44            .items_start()
45            .gap_1()
46            .child(
47                div()
48                    .flex()
49                    .child(v_flex().elevation_2(cx).px_1().children(self.children)),
50            )
51            .when_some(self.aside, |this, aside| {
52                this.child(
53                    v_flex()
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 extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
82        self.children.extend(elements)
83    }
84}