ailloli_ui_widgets/layout/
align.rs

1//! Single-child alignment within the maximum allocated box.
2
3use ailloli_ui_core::geometry::{Constraints, Rect};
4use ailloli_ui_core::Offset;
5use ailloli_ui_runtime::component::{IntoView, View, Widget};
6use ailloli_ui_runtime::layout::LayoutEngine;
7use ailloli_ui_runtime::layout::{ChildLayout, LayoutChild, LayoutCtx, LayoutResult};
8use ailloli_ui_runtime::scene::PaintCtx;
9
10/// Aligns one child within allocated space using Flutter-like factors.
11///
12/// `x` and `y` range from `-1` (start/top) through `0` (center) to `1`
13/// (end/bottom). Finite values are clamped to that interval; `NaN` remains
14/// `NaN` and can propagate into layout offsets. With no child, the widget still
15/// expands to its constrained maximum size.
16///
17/// # Examples
18///
19/// ```
20/// use ailloli_ui_widgets::{layout::Align, text::Text};
21/// let centered: Align<()> = Align::new(0.0, 0.0).child(Text::new("center"));
22/// let _ = centered;
23/// ```
24pub struct Align<A = ()> {
25    /// Horizontal interpolation factor, normally in `0.0..=1.0`.
26    x: f32,
27    /// Vertical interpolation factor, normally in `0.0..=1.0`.
28    y: f32,
29    /// Optional sole child positioned inside the available bounds.
30    child: Option<View<A>>,
31}
32
33impl<A: 'static> Align<A> {
34    /// Creates an empty alignment box and clamps finite factors to `[-1, 1]`.
35    ///
36    /// # Examples
37    ///
38    /// ```
39    /// use ailloli_ui_widgets::layout::Align;
40    /// let align: Align<()> = Align::new(-1.0, 1.0);
41    /// let _ = align;
42    /// ```
43    pub fn new(x: f32, y: f32) -> Self {
44        Self {
45            x: x.clamp(-1.0, 1.0),
46            y: y.clamp(-1.0, 1.0),
47            child: None,
48        }
49    }
50
51    /// Sets the single aligned child, replacing any previous child.
52    ///
53    /// # Examples
54    ///
55    /// ```
56    /// use ailloli_ui_widgets::{layout::Align, text::Text};
57    /// let align: Align<()> = Align::new(0.0, 0.0).child(Text::new("child"));
58    /// let _ = align;
59    /// ```
60    pub fn child(mut self, child: impl IntoView<A>) -> Self {
61        self.child = Some(child.into_view());
62        self
63    }
64}
65
66/// Frozen normalized factors used by retained layout.
67struct AlignWidget {
68    /// Horizontal interpolation factor used after child measurement.
69    x: f32,
70    /// Vertical interpolation factor used after child measurement.
71    y: f32,
72}
73
74/// Lays out at most one loose child and positions it in the free space.
75impl<A: 'static> Widget<A> for AlignWidget {
76    fn debug_name(&self) -> &'static str {
77        "Align"
78    }
79
80    fn layout(
81        &self,
82        engine: &mut LayoutEngine<'_, A>,
83        ctx: &mut LayoutCtx<'_>,
84        children: &mut [LayoutChild],
85        constraints: Constraints,
86    ) -> LayoutResult {
87        let max = constraints.max_size();
88        let size = constraints.constrain(max);
89        let mut child_layouts = Vec::new();
90
91        if let Some(child) = children.first_mut() {
92            let r = child.layout(engine, ctx, Constraints::loose(max.w, max.h));
93            let free_x = (max.w - r.size.w).max(0.0);
94            let free_y = (max.h - r.size.h).max(0.0);
95            let offset = Offset::new(free_x * (self.x + 1.0) / 2.0, free_y * (self.y + 1.0) / 2.0);
96            child_layouts.push(ChildLayout {
97                offset,
98                size: r.size,
99                paint_bounds: Rect::new(0.0, 0.0, r.size.w, r.size.h),
100                visual_bounds: r.visual_bounds,
101            });
102        }
103
104        LayoutResult {
105            size,
106            children: child_layouts,
107            paint_bounds: Rect::new(0.0, 0.0, size.w, size.h),
108            visual_bounds: Rect::new(0.0, 0.0, size.w, size.h),
109            overlay_hit_bounds: Vec::new(),
110            clip: None,
111            is_window_root_clip: false,
112            artifact: None,
113        }
114    }
115
116    fn paint(&self, _ctx: &mut PaintCtx<'_>, _bounds: Rect, _layout: &LayoutResult) {}
117}
118
119/// Converts the builder into a retained node containing zero or one child.
120impl<A: 'static> IntoView<A> for Align<A> {
121    fn into_view(self) -> View<A> {
122        let mut children = Vec::new();
123        if let Some(child) = self.child {
124            children.push(child);
125        }
126
127        View::node(
128            AlignWidget {
129                x: self.x,
130                y: self.y,
131            },
132            children,
133        )
134    }
135}