ailloli_ui_widgets/layout/
clip_rect.rs1use ailloli_ui_core::geometry::{ClipShape, Constraints, Rect, Size};
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
10pub struct ClipRect<A = ()> {
23 child: Option<View<A>>,
25}
26
27impl<A: 'static> Default for ClipRect<A> {
29 fn default() -> Self {
30 Self::new()
31 }
32}
33
34impl<A: 'static> ClipRect<A> {
35 pub fn new() -> Self {
45 Self { child: None }
46 }
47
48 pub fn child(mut self, child: impl IntoView<A>) -> Self {
58 self.child = Some(child.into_view());
59 self
60 }
61}
62
63struct ClipRectWidget;
65
66impl<A: 'static> Widget<A> for ClipRectWidget {
68 fn debug_name(&self) -> &'static str {
69 "ClipRect"
70 }
71
72 fn layout(
73 &self,
74 engine: &mut LayoutEngine<'_, A>,
75 ctx: &mut LayoutCtx<'_>,
76 children: &mut [LayoutChild],
77 constraints: Constraints,
78 ) -> LayoutResult {
79 let mut child_layouts = Vec::new();
80 let mut size = constraints.constrain(Size::default());
81
82 if let Some(child) = children.first_mut() {
83 let r = child.layout(engine, ctx, constraints);
84 size = constraints.constrain(r.size);
85 child_layouts.push(ChildLayout {
86 offset: Offset::default(),
87 size: r.size,
88 paint_bounds: Rect::new(0.0, 0.0, r.size.w, r.size.h),
89 visual_bounds: r.visual_bounds,
90 });
91 }
92
93 let bounds = Rect::new(0.0, 0.0, size.w, size.h);
94 LayoutResult {
95 size,
96 children: child_layouts,
97 paint_bounds: bounds,
98 visual_bounds: bounds,
99 overlay_hit_bounds: Vec::new(),
100 clip: Some(ClipShape::Rect(bounds)),
101 is_window_root_clip: false,
102 artifact: None,
103 }
104 }
105
106 fn paint(&self, _ctx: &mut PaintCtx<'_>, _bounds: Rect, _layout: &LayoutResult) {}
107}
108
109impl<A: 'static> IntoView<A> for ClipRect<A> {
111 fn into_view(self) -> View<A> {
112 let mut children = Vec::new();
113 if let Some(child) = self.child {
114 children.push(child);
115 }
116
117 View::node(ClipRectWidget, children)
118 }
119}