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