ailloli_ui_widgets/layout/
clip_rect.rs

1//! Single-child rectangular clipping wrapper.
2
3use 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
10/// Clips one child to this wrapper's resolved logical-pixel bounds.
11///
12/// The child uses the incoming constraints. With no child, the wrapper resolves
13/// constrained zero size. The clip is not a window-root clip.
14///
15/// # Examples
16///
17/// ```
18/// use ailloli_ui_widgets::{layout::ClipRect, text::Text};
19/// let clip: ClipRect<()> = ClipRect::new().child(Text::new("visible portion"));
20/// let _ = clip;
21/// ```
22pub struct ClipRect<A = ()> {
23    /// Optional sole child clipped to the widget's laid-out rectangle.
24    child: Option<View<A>>,
25}
26
27/// Creates the same empty clip as [`ClipRect::new`].
28impl<A: 'static> Default for ClipRect<A> {
29    fn default() -> Self {
30        Self::new()
31    }
32}
33
34impl<A: 'static> ClipRect<A> {
35    /// Creates an empty rectangular clip wrapper.
36    ///
37    /// # Examples
38    ///
39    /// ```
40    /// use ailloli_ui_widgets::layout::ClipRect;
41    /// let clip: ClipRect<()> = ClipRect::new();
42    /// let _ = clip;
43    /// ```
44    pub fn new() -> Self {
45        Self { child: None }
46    }
47
48    /// Sets the single clipped child, replacing any previous child.
49    ///
50    /// # Examples
51    ///
52    /// ```
53    /// use ailloli_ui_widgets::{layout::ClipRect, text::Text};
54    /// let clip: ClipRect<()> = ClipRect::new().child(Text::new("child"));
55    /// let _ = clip;
56    /// ```
57    pub fn child(mut self, child: impl IntoView<A>) -> Self {
58        self.child = Some(child.into_view());
59        self
60    }
61}
62
63/// Stateless retained rectangular clip implementation.
64struct ClipRectWidget;
65
66/// Resolves child size and publishes an exact rectangular clip shape.
67impl<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
109/// Converts the builder into a retained node containing zero or one child.
110impl<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}