Skip to main content

tui_lipan/widgets/zstack/
mod.rs

1mod layout;
2mod node;
3mod reconcile;
4
5pub(crate) use self::layout::measure_zstack;
6pub use self::node::ZStackNode;
7pub(crate) use self::reconcile::reconcile_zstack;
8
9use crate::core::element::{Element, ElementKind};
10use crate::style::{LayoutConstraints, Length, Style};
11
12/// Overlay container.
13///
14/// Unlike `VStack`/`HStack`, `ZStack` does not split space: every child receives the full
15/// available rectangle.
16///
17/// Children are rendered in order (painter's algorithm). The last child is on top.
18#[derive(Clone, Default)]
19pub struct ZStack {
20    pub(crate) style: Style,
21    pub(crate) passthrough: bool,
22    pub(crate) children: Vec<Element>,
23}
24
25impl ZStack {
26    /// Create an empty ZStack.
27    pub fn new() -> Self {
28        Self::default()
29    }
30
31    /// Set base style.
32    pub fn style(mut self, style: Style) -> Self {
33        self.style = style;
34        self
35    }
36
37    /// Allow pointer events to pass through non-interactive layers.
38    pub fn passthrough(mut self, passthrough: bool) -> Self {
39        self.passthrough = passthrough;
40        self
41    }
42
43    /// Add a child.
44    pub fn child(mut self, child: impl Into<Element>) -> Self {
45        self.children.push(child.into());
46        self
47    }
48}
49
50impl From<ZStack> for Element {
51    fn from(value: ZStack) -> Self {
52        let (min_w, min_h) = measure_zstack(&value, None, None);
53        Element::new(ElementKind::ZStack(value)).with_layout(
54            LayoutConstraints::default()
55                .min_width(Length::Px(min_w))
56                .min_height(Length::Px(min_h)),
57        )
58    }
59}
60
61impl crate::layout::hash::LayoutHash for ZStack {
62    fn layout_hash(
63        &self,
64        hasher: &mut impl std::hash::Hasher,
65        recurse: &dyn Fn(&Element) -> Option<u64>,
66    ) -> Option<()> {
67        use std::hash::Hash;
68        self.passthrough.hash(hasher);
69        crate::layout::hash::hash_children(&self.children, hasher, recurse)
70    }
71}