Skip to main content

fission_core/ui/widgets/
align.rs

1use crate::lowering::{LoweringContext, NodeBuilder};
2use crate::ui::traits::Lower;
3use crate::ui::Node;
4use fission_ir::{LayoutOp, NodeId, Op};
5use serde::{Deserialize, Serialize};
6
7/// Centers its child within the available parent space.
8///
9/// `Align` is a convenience wrapper that applies center alignment on both
10/// axes. It expands to fill the parent and places the child at the centre.
11///
12/// # Example
13///
14/// ```rust,ignore
15/// Align::new(Text::new("Centered!").into_node())
16/// ```
17#[derive(Debug, Clone, Serialize, Deserialize)]
18pub struct Align {
19    /// Explicit node identity.
20    pub id: Option<NodeId>,
21    /// The child widget to center.
22    pub child: Box<Node>,
23}
24
25impl Align {
26    pub fn new(child: Node) -> Self {
27        Self {
28            child: Box::new(child),
29            id: None,
30        }
31    }
32
33    pub fn into_node(self) -> Node {
34        Node::Align(self)
35    }
36}
37
38impl Lower for Align {
39    fn lower(&self, cx: &mut LoweringContext) -> NodeId {
40        let id = self.id.unwrap_or_else(|| cx.next_node_id());
41        cx.push_scope(id);
42        let child_id = self.child.lower(cx);
43        cx.pop_scope();
44
45        let mut builder = NodeBuilder::new(id, Op::Layout(LayoutOp::Align));
46        builder.add_child(child_id);
47        builder.build(cx)
48    }
49}