ailloli_ui_widgets/layout/
margin.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 Margin {
27 pub margin: EdgeInsets,
29}
30
31impl Margin {
32 pub fn new(margin: EdgeInsets) -> Self {
43 Self { margin }
44 }
45}
46
47impl<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}