fission_core/ui/widgets/stack.rs
1use crate::lowering::{LoweringContext, NodeBuilder, wrap_zstack_child};
2use crate::ui::traits::Lower;
3use crate::ui::Node;
4use fission_ir::{LayoutOp, NodeId, Op};
5use serde::{Deserialize, Serialize};
6
7/// A z-axis stacking container that layers children on top of each other.
8///
9/// Children are painted in order: the first child is at the bottom, the last
10/// is on top. Use [`Positioned`](super::Positioned) children to place them
11/// at absolute offsets within the stack.
12///
13/// The stack's size is determined by its largest child.
14///
15/// # Example
16///
17/// ```rust,ignore
18/// ZStack {
19/// children: vec![
20/// Image { source: "bg.png".into(), ..Default::default() }.into_node().into(),
21/// Positioned {
22/// bottom: Some(16.0),
23/// right: Some(16.0),
24/// child: Some(Box::new(Text::new("Overlay").into_node())),
25/// ..Default::default()
26/// }.into_node().into(),
27/// ],
28/// ..Default::default()
29/// }
30/// ```
31#[derive(Debug, Default, Clone, Serialize, Deserialize)]
32pub struct ZStack {
33 /// Explicit node identity.
34 pub id: Option<NodeId>,
35 /// Children painted in order (first = bottom, last = top).
36 pub children: Vec<Node>,
37}
38
39impl ZStack {
40 pub fn children(mut self, children: Vec<Node>) -> Self {
41 self.children = children;
42 self
43 }
44
45 pub fn into_node(self) -> Node {
46 Node::ZStack(self)
47 }
48}
49
50impl Lower for ZStack {
51 fn lower(&self, cx: &mut LoweringContext) -> NodeId {
52 let id = self.id.unwrap_or_else(|| cx.next_node_id());
53
54 cx.push_scope(id);
55
56 let mut builder = NodeBuilder::new(id, Op::Layout(LayoutOp::ZStack));
57 for child in &self.children {
58 let child_id = child.lower(cx);
59 builder.add_child(wrap_zstack_child(cx, child_id));
60 }
61
62 cx.pop_scope();
63
64 builder.build(cx)
65 }
66}