Skip to content

Commit

Permalink
Rename bbox to bounding_rect as it's more appropriate in 2D space
Browse files Browse the repository at this point in the history
  • Loading branch information
Philipp-M committed Nov 25, 2024
1 parent 25005f1 commit 548156c
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 30 deletions.
8 changes: 4 additions & 4 deletions masonry/src/contexts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down
5 changes: 4 additions & 1 deletion masonry/src/passes/accessibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions masonry/src/passes/compose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
},
);
Expand Down
4 changes: 2 additions & 2 deletions masonry/src/passes/paint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions masonry/src/testing/harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion masonry/src/widget/flex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
8 changes: 4 additions & 4 deletions masonry/src/widget/tests/status_change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:?}");
Expand Down
2 changes: 1 addition & 1 deletion masonry/src/widget/text_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ impl<const EDITABLE: bool> Widget for TextArea<EDITABLE> {
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 {
Expand Down
2 changes: 1 addition & 1 deletion masonry/src/widget/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ pub(crate) fn find_widget_at_pos<'c>(
widget: &WidgetRef<'c, dyn Widget>,
pos: Point,
) -> Option<WidgetRef<'c, dyn Widget>> {
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)) {
Expand Down
18 changes: 9 additions & 9 deletions masonry/src/widget/widget_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -81,9 +81,9 @@ pub(crate) struct WidgetState {
// efficiently hold an arbitrary shape.
pub(crate) clip_path: Option<Rect>,

/// 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,

Expand Down Expand Up @@ -197,7 +197,7 @@ impl WidgetState {
#[cfg(debug_assertions)]
widget_name,
window_transform: Affine::IDENTITY,
bbox: Rect::ZERO,
bounding_rect: Rect::ZERO,
}
}

Expand Down Expand Up @@ -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()))
Expand Down

0 comments on commit 548156c

Please sign in to comment.