use gpui::{div, prelude::*, px, AnyElement, App, Pixels, StyleRefinement, Window};
use super::Alignment;
use crate::components::direction::DirectionalStyled;
use crate::{
styles::ApplyStyleOverrides,
sx::{Sx, SxStyled},
};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum StackFit {
#[default]
Loose,
Expand,
}
#[derive(IntoElement)]
pub struct Stack {
alignment: Alignment,
fit: StackFit,
children: Vec<AnyElement>,
positioned: Vec<AnyElement>,
sx: Sx,
style_overrides: StyleRefinement,
}
crate::implement_style_overrides!(Stack);
impl Stack {
#[must_use]
pub fn new() -> Self {
Self {
alignment: Alignment::TopStart,
fit: StackFit::Loose,
children: Vec::new(),
positioned: Vec::new(),
sx: Sx::new(),
style_overrides: StyleRefinement::default(),
}
}
#[must_use]
pub fn alignment(mut self, alignment: Alignment) -> Self {
self.alignment = alignment;
self
}
#[must_use]
pub fn fit(mut self, fit: StackFit) -> Self {
self.fit = fit;
self
}
#[must_use]
pub fn positioned(mut self, positioned: Positioned) -> Self {
self.positioned.push(positioned.into_any_element());
self
}
}
impl Default for Stack {
fn default() -> Self {
Self::new()
}
}
impl ParentElement for Stack {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for Stack {
fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
let alignment = self.alignment;
let layers = self.children.into_iter().map(move |child| {
alignment
.apply(div().col_start(1).row_start(1))
.child(child)
});
div()
.relative()
.grid()
.grid_cols(1)
.grid_rows(1)
.when(self.fit == StackFit::Expand, gpui::Styled::size_full)
.children(layers)
.children(self.positioned)
.sx(&self.sx)
.apply_style_overrides(&self.style_overrides)
}
}
#[derive(IntoElement)]
pub struct Positioned {
top: Option<Pixels>,
end: Option<Pixels>,
bottom: Option<Pixels>,
start: Option<Pixels>,
width: Option<Pixels>,
height: Option<Pixels>,
children: Vec<AnyElement>,
sx: Sx,
style_overrides: StyleRefinement,
}
crate::implement_style_overrides!(Positioned);
impl Positioned {
#[must_use]
pub fn new() -> Self {
Self {
top: None,
end: None,
bottom: None,
start: None,
width: None,
height: None,
children: Vec::new(),
sx: Sx::new(),
style_overrides: StyleRefinement::default(),
}
}
#[must_use]
pub fn fill() -> Self {
Self::new()
.top(px(0.))
.end(px(0.))
.bottom(px(0.))
.start(px(0.))
}
#[must_use]
pub fn top(mut self, top: impl Into<Pixels>) -> Self {
self.top = Some(top.into());
self
}
#[must_use]
pub fn end(mut self, end: impl Into<Pixels>) -> Self {
self.end = Some(end.into());
self
}
#[must_use]
pub fn bottom(mut self, bottom: impl Into<Pixels>) -> Self {
self.bottom = Some(bottom.into());
self
}
#[must_use]
pub fn start(mut self, start: impl Into<Pixels>) -> Self {
self.start = Some(start.into());
self
}
#[must_use]
pub fn width(mut self, width: impl Into<Pixels>) -> Self {
self.width = Some(width.into());
self
}
#[must_use]
pub fn height(mut self, height: impl Into<Pixels>) -> Self {
self.height = Some(height.into());
self
}
}
impl Default for Positioned {
fn default() -> Self {
Self::new()
}
}
impl ParentElement for Positioned {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for Positioned {
fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
div()
.absolute()
.flex()
.flex_col()
.when_some(self.top, gpui::Styled::top)
.when_some(self.bottom, gpui::Styled::bottom)
.when_some(self.start, |positioned, start| {
positioned.inset_start(start)
})
.when_some(
self.end,
crate::components::direction::DirectionalStyled::inset_end,
)
.when_some(self.width, gpui::Styled::w)
.when_some(self.height, gpui::Styled::h)
.children(self.children)
.sx(&self.sx)
.apply_style_overrides(&self.style_overrides)
}
}