2023-11-21 17:10:06 +00:00
|
|
|
use gpui::{
|
2023-11-21 17:26:10 +00:00
|
|
|
AnyElement, Component, Div, Element, ElementId, ParentElement, RenderOnce, Styled,
|
|
|
|
WindowContext,
|
2023-11-21 17:10:06 +00:00
|
|
|
};
|
|
|
|
use smallvec::SmallVec;
|
|
|
|
|
|
|
|
use crate::{v_stack, StyledExt};
|
|
|
|
|
|
|
|
#[derive(RenderOnce)]
|
|
|
|
pub struct Popover {
|
|
|
|
children: SmallVec<[AnyElement; 2]>,
|
2023-11-21 17:26:10 +00:00
|
|
|
aside: Option<AnyElement>,
|
2023-11-21 17:10:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Component for Popover {
|
|
|
|
type Rendered = Div;
|
|
|
|
|
|
|
|
fn render(self, cx: &mut WindowContext) -> Self::Rendered {
|
|
|
|
v_stack()
|
|
|
|
.relative()
|
|
|
|
.elevation_2(cx)
|
|
|
|
.p_1()
|
|
|
|
.children(self.children)
|
|
|
|
.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 avoid collisions with the window edges.
|
|
|
|
this.child(
|
|
|
|
v_stack()
|
|
|
|
.top_0()
|
|
|
|
.neg_right_1()
|
|
|
|
.absolute()
|
|
|
|
.elevation_2(cx)
|
|
|
|
.p_1()
|
2023-11-21 17:26:10 +00:00
|
|
|
.child(aside),
|
2023-11-21 17:10:06 +00:00
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Popover {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
children: SmallVec::new(),
|
|
|
|
aside: None,
|
|
|
|
}
|
|
|
|
}
|
2023-11-21 17:26:10 +00:00
|
|
|
|
|
|
|
pub fn aside(mut self, aside: impl RenderOnce) -> Self
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
|
|
|
self.aside = Some(aside.render_once().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
|
|
|
|
}
|
|
|
|
}
|