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