ailloli_ui_widgets/layout/
padding.rs1use 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
10pub struct Padding {
24 pub padding: EdgeInsets,
26}
27
28impl Padding {
29 pub fn new(padding: EdgeInsets) -> Self {
40 Self { padding }
41 }
42}
43
44impl<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}