diff --git a/masonry/src/contexts.rs b/masonry/src/contexts.rs index 527065165..de26e7678 100644 --- a/masonry/src/contexts.rs +++ b/masonry/src/contexts.rs @@ -259,9 +259,9 @@ impl_context_method!( self.widget_state.window_origin() } - /// The axis aligned bounding box of this widget in window coordinates. - pub fn bbox(&self) -> Rect { - self.widget_state.bbox() + /// The axis aligned bounding rect of this widget in window coordinates. + pub fn bounding_rect(&self) -> Rect { + self.widget_state.bounding_rect() } pub fn paint_rect(&self) -> Rect { @@ -975,7 +975,7 @@ impl LayoutCtx<'_> { ) -> Insets { self.assert_layout_done(child, "compute_insets_from_child"); self.assert_placed(child, "compute_insets_from_child"); - let parent_bounds = Rect::ZERO.with_size(my_size); + let parent_bounds = my_size.to_rect(); let union_paint_rect = self .get_child_state(child) .paint_rect() diff --git a/masonry/src/passes/accessibility.rs b/masonry/src/passes/accessibility.rs index 157a84950..ebe4e17a4 100644 --- a/masonry/src/passes/accessibility.rs +++ b/masonry/src/passes/accessibility.rs @@ -89,7 +89,10 @@ fn build_accessibility_tree( // --- MARK: BUILD NODE --- fn build_access_node(widget: &mut dyn Widget, ctx: &mut AccessCtx) -> Node { let mut node = Node::new(widget.accessibility_role()); - node.set_bounds(to_accesskit_rect(ctx.widget_state.bbox(), ctx.scale_factor)); + node.set_bounds(to_accesskit_rect( + ctx.widget_state.bounding_rect(), + ctx.scale_factor, + )); node.set_children( widget diff --git a/masonry/src/passes/compose.rs b/masonry/src/passes/compose.rs index 4b6e79149..d2574e194 100644 --- a/masonry/src/passes/compose.rs +++ b/masonry/src/passes/compose.rs @@ -31,13 +31,13 @@ fn compose_widget( } let local_translation = state.item.translation + state.item.origin.to_vec2(); + state.item.window_transform = parent_window_transform * widget.item.transform().then_translate(local_translation); state.item.window_origin = state.item.window_transform.translation().to_point(); - state.item.bbox = state - .item - .window_transform - .transform_rect_bbox(state.item.size.to_rect()); + + let local_rect = state.item.size.to_rect(); + state.item.bounding_rect = state.item.window_transform.transform_rect_bbox(local_rect); let mut ctx = ComposeCtx { global_state, @@ -79,7 +79,7 @@ fn compose_widget( transformed, parent_transform, ); - parent_state.bbox = parent_state.bbox.union(state.item.bbox); + parent_state.bounding_rect = parent_state.bounding_rect.union(state.item.bounding_rect); parent_state.merge_up(state.item); }, ); diff --git a/masonry/src/passes/paint.rs b/masonry/src/passes/paint.rs index 143448adc..9b84e2017 100644 --- a/masonry/src/passes/paint.rs +++ b/masonry/src/passes/paint.rs @@ -63,7 +63,7 @@ fn paint_widget( complete_scene.append(scene, Some(transform)); let id = state.item.id; - let bbox = state.item.bbox; + let bounding_rect = state.item.bounding_rect; let parent_state = state.item; recurse_on_children( id, @@ -94,7 +94,7 @@ fn paint_widget( if debug_paint { const BORDER_WIDTH: f64 = 1.0; let color = get_debug_color(id.to_raw()); - let rect = bbox.inset(BORDER_WIDTH / -2.0); + let rect = bounding_rect.inset(BORDER_WIDTH / -2.0); stroke(complete_scene, &rect, color, BORDER_WIDTH); } diff --git a/masonry/src/testing/harness.rs b/masonry/src/testing/harness.rs index fbb54a5fa..212c020c0 100644 --- a/masonry/src/testing/harness.rs +++ b/masonry/src/testing/harness.rs @@ -418,7 +418,7 @@ impl TestHarness { /// /// Combines [`mouse_move`](Self::mouse_move), [`mouse_button_press`](Self::mouse_button_press), and [`mouse_button_release`](Self::mouse_button_release). pub fn mouse_click_on(&mut self, id: WidgetId) { - let widget_rect = self.get_widget(id).ctx().bbox(); + let widget_rect = self.get_widget(id).ctx().bounding_rect(); let widget_center = widget_rect.center(); self.mouse_move(widget_center); @@ -430,7 +430,7 @@ impl TestHarness { pub fn mouse_move_to(&mut self, id: WidgetId) { // FIXME - handle case where the widget isn't visible // FIXME - assert that the widget correctly receives the event otherwise? - let widget_rect = self.get_widget(id).ctx().bbox(); + let widget_rect = self.get_widget(id).ctx().bounding_rect(); let widget_center = widget_rect.center(); self.mouse_move(widget_center); diff --git a/masonry/src/widget/flex.rs b/masonry/src/widget/flex.rs index bff9e815d..b7940eb08 100644 --- a/masonry/src/widget/flex.rs +++ b/masonry/src/widget/flex.rs @@ -1161,7 +1161,7 @@ impl Widget for Flex { // or be clipped (e.g. if its parent is a Portal). let my_size: Size = self.direction.pack(major, minor_dim).into(); - let my_bounds = Rect::ZERO.with_size(my_size); + let my_bounds = my_size.to_rect(); let insets = child_paint_rect - my_bounds; ctx.set_paint_insets(insets); diff --git a/masonry/src/widget/tests/status_change.rs b/masonry/src/widget/tests/status_change.rs index 93f89bd88..689f94519 100644 --- a/masonry/src/widget/tests/status_change.rs +++ b/masonry/src/widget/tests/status_change.rs @@ -72,10 +72,10 @@ fn propagate_hovered() { harness.mouse_move_to(empty); - dbg!(harness.get_widget(button).ctx().bbox()); - dbg!(harness.get_widget(pad).ctx().bbox()); - dbg!(harness.get_widget(root).ctx().bbox()); - dbg!(harness.get_widget(empty).ctx().bbox()); + dbg!(harness.get_widget(button).ctx().bounding_rect()); + dbg!(harness.get_widget(pad).ctx().bounding_rect()); + dbg!(harness.get_widget(root).ctx().bounding_rect()); + dbg!(harness.get_widget(empty).ctx().bounding_rect()); eprintln!("root: {root:?}"); eprintln!("empty: {empty:?}"); diff --git a/masonry/src/widget/text_area.rs b/masonry/src/widget/text_area.rs index 0189c74db..ece27460c 100644 --- a/masonry/src/widget/text_area.rs +++ b/masonry/src/widget/text_area.rs @@ -447,7 +447,7 @@ impl Widget for TextArea { fn on_pointer_event(&mut self, ctx: &mut EventCtx, event: &PointerEvent) { let (fctx, lctx) = ctx.text_contexts(); let is_rtl = self.editor.layout(fctx, lctx).is_rtl(); - let padding = Point::new(self.padding.get_left(is_rtl), self.padding.top); + let padding = Vec2::new(self.padding.get_left(is_rtl), self.padding.top); match event { PointerEvent::PointerDown(button, _) => { if !ctx.is_disabled() && *button == PointerButton::Primary { diff --git a/masonry/src/widget/widget.rs b/masonry/src/widget/widget.rs index 05445ba87..845d5a6af 100644 --- a/masonry/src/widget/widget.rs +++ b/masonry/src/widget/widget.rs @@ -333,7 +333,7 @@ pub(crate) fn find_widget_at_pos<'c>( widget: &WidgetRef<'c, dyn Widget>, pos: Point, ) -> Option> { - if widget.ctx.widget_state.bbox.contains(pos) { + if widget.ctx.widget_state.bounding_rect.contains(pos) { let local_pos = widget.ctx().widget_state.window_transform.inverse() * pos; if Some(false) == widget.ctx.clip_path().map(|clip| clip.contains(local_pos)) { diff --git a/masonry/src/widget/widget_state.rs b/masonry/src/widget/widget_state.rs index 0acdce433..8788abd2b 100644 --- a/masonry/src/widget/widget_state.rs +++ b/masonry/src/widget/widget_state.rs @@ -53,8 +53,8 @@ pub(crate) struct WidgetState { // TODO - Document // The computed paint rect, in local coordinates. pub(crate) local_paint_rect: Rect, - /// An axis aligned bounding box (AABB), containing itself and all its descendents. - pub(crate) bbox: Rect, + /// An axis aligned bounding rect (AABB in 2D), containing itself and all its descendents. + pub(crate) bounding_rect: Rect, /// The offset of the baseline relative to the bottom of the widget. /// /// In general, this will be zero; the bottom of the widget will be considered @@ -81,9 +81,9 @@ pub(crate) struct WidgetState { // efficiently hold an arbitrary shape. pub(crate) clip_path: Option, - /// This is being computed out of the descendant transforms and `translation` + // TODO is it worth to compute/cache the inverse as well (or does it just take valuable memory)? + /// This is being computed out of all ancestor transforms and `translation` pub(crate) window_transform: Affine, - // TODO - Handle matrix transforms correctly pub(crate) translation: Vec2, pub(crate) transform_changed: bool, @@ -197,7 +197,7 @@ impl WidgetState { #[cfg(debug_assertions)] widget_name, window_transform: Affine::IDENTITY, - bbox: Rect::ZERO, + bounding_rect: Rect::ZERO, } } @@ -259,14 +259,14 @@ impl WidgetState { Rect::from_origin_size(self.origin, self.size) } - /// The axis aligned bounding box of this widget in window coordinates. - pub fn bbox(&self) -> Rect { - self.bbox + /// The axis aligned bounding rect of this widget in window coordinates. + pub fn bounding_rect(&self) -> Rect { + self.bounding_rect } /// Returns the area being edited by an IME, in global coordinates. /// - /// By default, returns the same as [`Self::bbox`]. + /// By default, returns the same as [`Self::bounding_rect`]. pub(crate) fn get_ime_area(&self) -> Rect { self.window_transform .transform_rect_bbox(self.ime_area.unwrap_or_else(|| self.size.to_rect()))