ailloli_ui_widgets/layout/
margin.rs

1//! Low-level outer-inset layout widget.
2
3use ailloli_ui_core::geometry::{Constraints, Rect, Size};
4use ailloli_ui_core::{EdgeInsets, Offset};
5use ailloli_ui_runtime::component::Widget;
6use ailloli_ui_runtime::layout::LayoutEngine;
7use ailloli_ui_runtime::layout::{ChildLayout, LayoutChild, LayoutCtx, LayoutResult};
8use ailloli_ui_runtime::scene::PaintCtx;
9
10/// Outer spacing around a child (margin).
11///
12/// Mirrors [`Padding`](crate::layout::Padding) behavior: deflates child constraints, inflates final size,
13/// and positions the child at `(margin.left, margin.top)`.
14/// Insets are logical pixels and are not normalized; callers should use finite
15/// non-negative values because negative/non-finite values can produce unusual
16/// offsets even though inner minima and maxima are floored at zero.
17///
18/// # Examples
19///
20/// ```
21/// use ailloli_ui_core::EdgeInsets;
22/// use ailloli_ui_widgets::layout::Margin;
23/// let margin = Margin::new(EdgeInsets::all(8.0));
24/// assert_eq!(margin.margin.left, 8.0);
25/// ```
26pub struct Margin {
27    /// Outer logical-pixel insets in left/top/right/bottom order.
28    pub margin: EdgeInsets,
29}
30
31impl Margin {
32    /// Creates an outer-inset widget with the exact supplied edges.
33    ///
34    /// # Examples
35    ///
36    /// ```
37    /// use ailloli_ui_core::EdgeInsets;
38    /// use ailloli_ui_widgets::layout::Margin;
39    /// let margin = Margin::new(EdgeInsets::new(1.0, 2.0, 3.0, 4.0));
40    /// assert_eq!((margin.margin.left, margin.margin.top), (1.0, 2.0));
41    /// ```
42    pub fn new(margin: EdgeInsets) -> Self {
43        Self { margin }
44    }
45}
46
47/// Deflates constraints, offsets at most one child, and reinflates final size.
48impl<A: 'static> Widget<A> for Margin {
49    fn debug_name(&self) -> &'static str {
50        "Margin"
51    }
52
53    fn layout(
54        &self,
55        engine: &mut LayoutEngine<'_, A>,
56        ctx: &mut LayoutCtx<'_>,
57        children: &mut [LayoutChild],
58        constraints: Constraints,
59    ) -> LayoutResult {
60        let inner = Constraints {
61            min_w: (constraints.min_w - self.margin.left - self.margin.right).max(0.0),
62            max_w: (constraints.max_w - self.margin.left - self.margin.right).max(0.0),
63            min_h: (constraints.min_h - self.margin.top - self.margin.bottom).max(0.0),
64            max_h: (constraints.max_h - self.margin.top - self.margin.bottom).max(0.0),
65        };
66
67        let mut child_layouts = Vec::new();
68        let mut child_size = Size::default();
69
70        if let Some(child) = children.first_mut() {
71            let r = child.layout(engine, ctx, inner);
72            child_size = r.size;
73            child_layouts.push(ChildLayout {
74                offset: Offset::new(self.margin.left, self.margin.top),
75                size: r.size,
76                paint_bounds: Rect::new(0.0, 0.0, r.size.w, r.size.h),
77                visual_bounds: r.visual_bounds,
78            });
79        }
80
81        let size = constraints.constrain(Size::new(
82            child_size.w + self.margin.left + self.margin.right,
83            child_size.h + self.margin.top + self.margin.bottom,
84        ));
85
86        LayoutResult {
87            size,
88            children: child_layouts,
89            paint_bounds: Rect::new(0.0, 0.0, size.w, size.h),
90            visual_bounds: Rect::new(0.0, 0.0, size.w, size.h),
91            overlay_hit_bounds: Vec::new(),
92            clip: None,
93            is_window_root_clip: false,
94            artifact: None,
95        }
96    }
97
98    fn paint(&self, _ctx: &mut PaintCtx<'_>, _bounds: Rect, _layout: &LayoutResult) {}
99}