ailloli_ui_widgets/layout/
padding.rs

1//! Low-level inner-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/// Inner logical-pixel spacing around at most one child.
11///
12/// Constraints are deflated before child layout and final size is inflated.
13/// Insets are not normalized; callers should provide finite non-negative values.
14///
15/// # Examples
16///
17/// ```
18/// use ailloli_ui_core::EdgeInsets;
19/// use ailloli_ui_widgets::layout::Padding;
20/// let padding = Padding::new(EdgeInsets::all(6.0));
21/// assert_eq!(padding.padding.horizontal(), 12.0);
22/// ```
23pub struct Padding {
24    /// Inner logical-pixel insets in left/top/right/bottom order.
25    pub padding: EdgeInsets,
26}
27
28impl Padding {
29    /// Creates an inner-inset widget with the exact supplied edges.
30    ///
31    /// # Examples
32    ///
33    /// ```
34    /// use ailloli_ui_core::EdgeInsets;
35    /// use ailloli_ui_widgets::layout::Padding;
36    /// let padding = Padding::new(EdgeInsets::new(1.0, 2.0, 3.0, 4.0));
37    /// assert_eq!((padding.padding.left, padding.padding.top), (1.0, 2.0));
38    /// ```
39    pub fn new(padding: EdgeInsets) -> Self {
40        Self { padding }
41    }
42}
43
44/// Deflates constraints, offsets at most one child, and reinflates final size.
45impl<A: 'static> Widget<A> for Padding {
46    fn debug_name(&self) -> &'static str {
47        "Padding"
48    }
49
50    fn layout(
51        &self,
52        engine: &mut LayoutEngine<'_, A>,
53        ctx: &mut LayoutCtx<'_>,
54        children: &mut [LayoutChild],
55        constraints: Constraints,
56    ) -> LayoutResult {
57        let inner = constraints.deflate(self.padding);
58        let mut child_layouts = Vec::new();
59        let mut child_size = Size::default();
60
61        if let Some(child) = children.first_mut() {
62            let r = child.layout(engine, ctx, inner);
63            child_size = r.size;
64            child_layouts.push(ChildLayout {
65                offset: Offset::new(self.padding.left, self.padding.top),
66                size: r.size,
67                paint_bounds: Rect::new(0.0, 0.0, r.size.w, r.size.h),
68                visual_bounds: r.visual_bounds,
69            });
70        }
71
72        let size = constraints.constrain(Size::new(
73            child_size.w + self.padding.horizontal(),
74            child_size.h + self.padding.vertical(),
75        ));
76
77        LayoutResult {
78            size,
79            children: child_layouts,
80            paint_bounds: Rect::new(0.0, 0.0, size.w, size.h),
81            visual_bounds: Rect::new(0.0, 0.0, size.w, size.h),
82            overlay_hit_bounds: Vec::new(),
83            clip: None,
84            is_window_root_clip: false,
85            artifact: None,
86        }
87    }
88
89    fn paint(&self, _ctx: &mut PaintCtx<'_>, _bounds: Rect, _layout: &LayoutResult) {}
90}