1#![allow(missing_docs)]
2
3use crate::prelude::*;
4use crate::v_flex;
5use gpui::{
6 div, AnyElement, Element, IntoElement, ParentElement, RenderOnce, Styled, WindowContext,
7};
8use smallvec::SmallVec;
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 appears when you click your avatar.
18///
19/// Related elements:
20///
21/// [`ContextMenu`](crate::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_flex().elevation_2(cx).py_1().children(self.children))
49 .when_some(self.aside, |this, aside| {
50 this.child(
51 v_flex()
52 .elevation_2(cx)
53 .bg(cx.theme().colors().surface_background)
54 .px_1()
55 .child(aside),
56 )
57 })
58 }
59}
60
61impl Default for Popover {
62 fn default() -> Self {
63 Self::new()
64 }
65}
66
67impl Popover {
68 pub fn new() -> Self {
69 Self {
70 children: SmallVec::new(),
71 aside: None,
72 }
73 }
74
75 pub fn aside(mut self, aside: impl IntoElement) -> Self
76 where
77 Self: Sized,
78 {
79 self.aside = Some(aside.into_element().into_any());
80 self
81 }
82}
83
84impl ParentElement for Popover {
85 fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
86 self.children.extend(elements)
87 }
88}