zed/crates/ui2/src/components/popover.rs

92 lines
2.9 KiB
Rust
Raw Normal View History

2023-11-21 17:10:06 +00:00
use gpui::{
2023-11-27 18:48:10 +00:00
AnyElement, Div, Element, ElementId, IntoElement, ParentElement, RenderOnce, Styled,
WindowContext,
2023-11-21 17:10:06 +00:00
};
use smallvec::SmallVec;
2023-11-27 15:29:29 +00:00
use theme2::ActiveTheme;
2023-11-21 17:10:06 +00:00
2023-11-27 18:48:10 +00:00
use crate::{v_stack, StyledExt};
2023-11-21 17:10:06 +00:00
/// A popover is used to display a menu or show some options.
///
/// Clicking the element that launches the popover should not change the current view,
/// and the popover should be statically positioned relative to that element (not the
/// user's mouse.)
///
/// Example: A "new" menu with options like "new file", "new folder", etc,
/// Linear's "Display" menu, a profile menu that appers when you click your avatar.
///
/// Related elements:
///
/// `ContextMenu`:
///
/// Used to display a popover menu that only contains a list of items. Context menus are always
/// launched by secondary clicking on an element. The menu is positioned relative to the user's cursor.
///
/// Example: Right clicking a file in the file tree to get a list of actions, right clicking
/// a tab to in the tab bar to get a list of actions.
///
/// `Dropdown`:
///
/// Used to display a list of options when the user clicks an element. The menu is
/// positioned relative the element that was clicked, and clicking an item in the
/// dropdown should change the value of the element that was clicked.
///
/// Example: A theme select control. Displays "One Dark", clicking it opens a list of themes.
/// When one is selected, the theme select control displays the selected theme.
2023-11-22 18:19:43 +00:00
#[derive(IntoElement)]
2023-11-21 17:10:06 +00:00
pub struct Popover {
children: SmallVec<[AnyElement; 2]>,
aside: Option<AnyElement>,
2023-11-21 17:10:06 +00:00
}
2023-11-22 18:19:43 +00:00
impl RenderOnce for Popover {
2023-11-21 17:10:06 +00:00
type Rendered = Div;
fn render(self, cx: &mut WindowContext) -> Self::Rendered {
2023-11-27 16:51:42 +00:00
v_stack()
.relative()
.elevation_2(cx)
.p_1()
.children(self.children)
2023-11-21 17:10:06 +00:00
.when_some(self.aside, |this, aside| {
// TODO: This will statically position the aside to the top right of the popover.
// We should update this to use gpui2::overlay avoid collisions with the window edges.
2023-11-21 17:10:06 +00:00
this.child(
v_stack()
2023-11-27 16:51:42 +00:00
.top_0()
.left_full()
.ml_1()
.absolute()
2023-11-21 17:10:06 +00:00
.elevation_2(cx)
2023-11-27 15:29:29 +00:00
.bg(cx.theme().colors().surface_background)
2023-11-21 17:10:06 +00:00
.p_1()
.child(aside),
2023-11-21 17:10:06 +00:00
)
})
}
}
impl Popover {
pub fn new() -> Self {
Self {
children: SmallVec::new(),
aside: None,
}
}
2023-11-22 18:19:43 +00:00
pub fn aside(mut self, aside: impl IntoElement) -> Self
where
Self: Sized,
{
2023-11-22 18:19:43 +00:00
self.aside = Some(aside.into_element().into_any());
self
}
2023-11-21 17:10:06 +00:00
}
impl ParentElement for Popover {
fn children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]> {
&mut self.children
}
}