From 7b99baaf1178e6e274e7f3866e35c72cfa925e0a Mon Sep 17 00:00:00 2001 From: Philipp Mildenberger Date: Sat, 11 Jan 2025 18:18:54 +0100 Subject: [PATCH] Add comments and handle paint_insets with the bounding_rect --- masonry/src/contexts.rs | 6 +++++- masonry/src/passes/compose.rs | 2 +- masonry/src/passes/paint.rs | 1 + masonry/src/widget/widget_mut.rs | 3 +++ masonry/src/widget/widget_state.rs | 9 ++++++--- 5 files changed, 16 insertions(+), 5 deletions(-) diff --git a/masonry/src/contexts.rs b/masonry/src/contexts.rs index 30bea04e1..dae13d280 100644 --- a/masonry/src/contexts.rs +++ b/masonry/src/contexts.rs @@ -658,7 +658,7 @@ impl ComposeCtx<'_> { self.widget_state.needs_compose } - /// Set a translation for the child widget. + /// Set the scroll translation for the child widget. /// /// The translation is applied on top of the position from [`LayoutCtx::place_child`]. pub fn set_child_scroll_translation( @@ -949,6 +949,7 @@ impl_context_method!(MutateCtx<'_>, EventCtx<'_>, UpdateCtx<'_>, { self.request_layout(); } + /// Indicate that the transform of this widget has changed. pub fn transform_changed(&mut self) { trace!("transform_changed"); self.widget_state.transform_changed = true; @@ -986,6 +987,9 @@ impl_context_method!(MutateCtx<'_>, EventCtx<'_>, UpdateCtx<'_>, { self.widget_state.is_explicitly_disabled = disabled; } + /// Set the transform for this widget. + /// + /// It behaves similarly as CSS transforms pub fn set_transform(&mut self, transform: Affine) { self.widget_state.transform = transform; self.transform_changed(); diff --git a/masonry/src/passes/compose.rs b/masonry/src/passes/compose.rs index 88805cc41..b55e127ce 100644 --- a/masonry/src/passes/compose.rs +++ b/masonry/src/passes/compose.rs @@ -36,7 +36,7 @@ fn compose_widget( state.item.window_transform = parent_window_transform * state.item.transform.then_translate(local_translation); - let local_rect = state.item.size.to_rect(); + let local_rect = state.item.size.to_rect() + state.item.paint_insets; state.item.bounding_rect = state.item.window_transform.transform_rect_bbox(local_rect); let mut ctx = ComposeCtx { diff --git a/masonry/src/passes/paint.rs b/masonry/src/passes/paint.rs index aa16cecef..c70a592d5 100644 --- a/masonry/src/passes/paint.rs +++ b/masonry/src/passes/paint.rs @@ -92,6 +92,7 @@ fn paint_widget( }, ); + // draw the global axis aligned bounding rect of the widget if debug_paint { const BORDER_WIDTH: f64 = 1.0; let color = get_debug_color(id.to_raw()); diff --git a/masonry/src/widget/widget_mut.rs b/masonry/src/widget/widget_mut.rs index 7a80d945f..df577ea93 100644 --- a/masonry/src/widget/widget_mut.rs +++ b/masonry/src/widget/widget_mut.rs @@ -48,6 +48,9 @@ impl WidgetMut<'_, W> { } } + /// Set the local transform of this widget. + /// + /// It behaves similarly as CSS transforms. pub fn set_transform(&mut self, transform: Affine) { self.ctx.set_transform(transform); } diff --git a/masonry/src/widget/widget_state.rs b/masonry/src/widget/widget_state.rs index a884ab437..39738f7e6 100644 --- a/masonry/src/widget/widget_state.rs +++ b/masonry/src/widget/widget_state.rs @@ -58,7 +58,7 @@ pub(crate) struct WidgetState { /// The size of the widget; this is the value returned by the widget's layout /// method. pub(crate) size: Size, - /// The origin of the widget in the parent's coordinate space; together with + /// The origin of the widget in the `window_transform` coordinate space; together with /// `size` these constitute the widget's layout rect. pub(crate) origin: Point, /// The insets applied to the layout rect to generate the paint rect. @@ -68,7 +68,7 @@ pub(crate) struct WidgetState { // TODO - Document // The computed paint rect, in local coordinates. pub(crate) local_paint_rect: Rect, - /// An axis aligned bounding rect (AABB in 2D), containing itself and all its descendents in window coordinates. + /// An axis aligned bounding rect (AABB in 2D), containing itself and all its descendents in window coordinates. Includes `paint_insets`. pub(crate) bounding_rect: Rect, /// The offset of the baseline relative to the bottom of the widget. /// @@ -98,8 +98,11 @@ pub(crate) struct WidgetState { /// This is being computed out of all ancestor transforms and `translation` pub(crate) window_transform: Affine, + /// Local transform of this widget in the parent coordinate space. pub(crate) transform: Affine, + /// translation applied by scrolling, this is applied after applying `transform` to this widget. pub(crate) scroll_translation: Vec2, + /// The `transform` or `scroll_translation` has changed. pub(crate) transform_changed: bool, // --- PASSES --- @@ -274,7 +277,7 @@ impl WidgetState { Rect::from_origin_size(self.origin, self.size) } - /// The axis aligned bounding rect of this widget in window coordinates. + /// The axis aligned bounding rect of this widget in window coordinates. Includes `paint_insets`. pub fn bounding_rect(&self) -> Rect { self.bounding_rect }