use crate::core::context::MountContext;
use crate::core::widget::Widget;
use crate::core::ElementId;
use crate::ecs::components;
use crate::style::styled::{StyleRefinement, Styled};
use crate::widgets::layout::apply_style;
pub struct ZStack {
children: Vec<Box<dyn Widget>>,
style: StyleRefinement,
}
impl ZStack {
pub fn new() -> Self {
Self {
children: Vec::new(),
style: StyleRefinement::default(),
}
}
pub fn push(mut self, child: impl Widget + 'static) -> Self {
self.children.push(Box::new(child));
self
}
}
impl Styled for ZStack {
fn style_refinement(&mut self) -> &mut StyleRefinement {
&mut self.style
}
}
impl Widget for ZStack {
fn component_mask(&self) -> u64 {
components::STYLE | components::LAYOUT | components::TEXT | components::TRANSFORM
}
fn mount_box(self: Box<Self>, ctx: &mut MountContext<'_>) -> ElementId {
let id = ctx.arena.allocate();
ctx.preallocate(id, self.component_mask());
{
let Some(element) = ctx.arena.get_mut(id) else {
return id;
};
element.set_accessible_role(accesskit::Role::Group);
element.set_flex_grow(1.0);
apply_style(&self.style, element);
}
for child in self.children {
let mut child_ctx = ctx.child_with_events(id);
let child_id = child.mount_box(&mut child_ctx);
{
let Some(el) = ctx.arena.get_mut(child_id) else {
return id;
};
let existing_z = el.z_index.max(1);
el.set_z_index(existing_z);
el.set_preferred_width(Some(0.0));
el.set_preferred_height(0.0);
el.set_width_dim(Some(crate::style::Dimension::Percent(100.0)));
el.set_height_dim(crate::style::Dimension::Percent(100.0));
}
ctx.arena.add_child(id, child_id);
}
id
}
}
impl Default for ZStack {
fn default() -> Self {
Self::new()
}
}
impl std::fmt::Debug for ZStack {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ZStack")
.field("children", &self.children.len())
.finish_non_exhaustive()
}
}