fission_core/ui/widgets/stack.rs
1use crate::internal::InternalLower;
2use crate::lowering::{wrap_zstack_child, InternalIrBuilder, InternalLoweringCx};
3use crate::ui::Widget;
4use fission_ir::{LayoutOp, Op, WidgetId};
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::asset("bg.png").into(),
21/// Positioned {
22/// bottom: Some(16.0),
23/// right: Some(16.0),
24/// child: Some(Text::new("Overlay").into()),
25/// ..Default::default()
26/// }.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<WidgetId>,
35 /// Children painted in order (first = bottom, last = top).
36 pub children: Vec<Widget>,
37}
38
39impl ZStack {
40 pub fn children(mut self, children: Vec<Widget>) -> Self {
41 self.children = children;
42 self
43 }
44}
45
46impl InternalLower for ZStack {
47 fn lower(&self, cx: &mut InternalLoweringCx) -> WidgetId {
48 let id = self.id.map(Into::into).unwrap_or_else(|| cx.next_node_id());
49
50 cx.push_scope(id);
51
52 let mut builder = InternalIrBuilder::new(id, Op::Layout(LayoutOp::ZStack));
53 for child in &self.children {
54 let child_id = child.lower(cx);
55 builder.add_child(wrap_zstack_child(cx, child_id));
56 }
57
58 cx.pop_scope();
59
60 builder.build(cx)
61 }
62}