Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion masonry/src/layers/selector_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ impl Layer for SelectorMenu {
PointerEvent::Down(PointerButtonEvent { state, .. }) => {
let local_pos = ctx.local_position(state.position);

!ctx.border_box_size().to_rect().contains(local_pos)
!ctx.border_box().contains(local_pos)
}
PointerEvent::Cancel(..) => true,
_ => false,
Expand Down
164 changes: 160 additions & 4 deletions masonry/src/tests/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ use std::rc::Rc;
use assert_matches::assert_matches;

use crate::core::{NewWidget, Widget, WidgetTag};
use crate::kurbo::{Insets, Point, Rect, Size};
use crate::kurbo::{Insets, Point, Rect, Size, Vec2};
use crate::layout::{AsUnit, Length, SizeDef};
use crate::properties::{BorderWidth, Dimensions, Padding};
use crate::testing::{ModularWidget, TestHarness, TestWidgetExt, assert_debug_panics};
use crate::tests::{assert_point_approx_eq, assert_rect_approx_eq, assert_vec2_approx_eq};
use crate::theme::test_property_set;
use crate::widgets::{Button, ChildAlignment, Flex, Portal, SizedBox, ZStack};

Expand Down Expand Up @@ -41,7 +42,7 @@ fn layout_simple() {

let harness = TestHarness::create(test_property_set(), widget);

let first_box_size = harness.get_widget(tag_1).ctx().border_box_size();
let first_box_size = harness.get_widget(tag_1).ctx().border_box().size();
let first_box_paint_rect = harness.get_widget(tag_1).ctx().paint_box();

assert_eq!(first_box_size.width, BOX_WIDTH);
Expand Down Expand Up @@ -327,9 +328,9 @@ fn content_box() {
let harness = TestHarness::create(test_property_set(), hero);

let border_box = harness.get_widget(tag).ctx().border_box();
let border_box_size = harness.get_widget(tag).ctx().border_box_size();
let border_box_size = border_box.size();
let content_box = harness.get_widget(tag).ctx().content_box();
let content_box_size = harness.get_widget(tag).ctx().content_box_size();
let content_box_size = content_box.size();
let border_box_translation = harness.get_widget(tag).ctx().border_box_translation();

let expected_border_box_size = Size::new(100., 100.);
Expand Down Expand Up @@ -357,3 +358,158 @@ fn content_box() {
assert_eq!(border_box, expected_border_box);
assert_eq!(content_box, expected_content_box);
}

#[test]
fn boxes_match_without_insets_or_snapping() {
let tag = WidgetTag::unique();
let child = NewWidget::new(SizedBox::empty().size(10.px(), 8.px())).with_tag(tag);
let root = ModularWidget::new_parent(child)
.layout_fn(|child, ctx, _, size| {
let child_size = ctx.compute_size(child, SizeDef::fit(size), size.into());
ctx.run_layout(child, child_size);
ctx.place_child(child, Point::new(2., 3.));
})
.prepare();

let harness = TestHarness::create(test_property_set(), root);
let child = harness.get_widget(tag);
let ctx = child.ctx();

// Everything besides the bounding box will be exactly the same
let local_box = Rect::new(0., 0., 10., 8.);
assert_rect_approx_eq("content_box", ctx.content_box(), local_box);
assert_rect_approx_eq("border_box", ctx.border_box(), local_box);
assert_rect_approx_eq("paint_box", ctx.paint_box(), local_box);
assert_rect_approx_eq(
"bounding_box",
ctx.bounding_box(),
Rect::new(2., 3., 12., 11.),
);
}

#[test]
fn boxes_use_content_box_coordinates() {
let tag = WidgetTag::unique();

let child = ModularWidget::new(())
.layout_fn(|_, ctx, _, _| {
ctx.set_paint_insets(Insets::new(5.9, 6.1, 7.4, 8.2));
})
.prepare()
.with_tag(tag)
.with_props((
Dimensions::fixed(23.4.px(), 17.6.px()),
BorderWidth::all(0.7.px()),
Padding {
left: 1.2.px(),
right: 2.3.px(),
top: 3.4.px(),
bottom: 4.5.px(),
},
));

let root = ModularWidget::new_parent(child)
.layout_fn(|child, ctx, _, size| {
let child_size = ctx.compute_size(child, SizeDef::fit(size), size.into());
ctx.run_layout(child, child_size);
ctx.place_child(child, Point::new(5.3, 7.6));
})
.prepare()
.with_props(Dimensions::fixed(80.px(), 80.px()));

let harness = TestHarness::create(test_property_set(), root);
let child = harness.get_widget(tag);
let ctx = child.ctx();

// Border 0.7 + padding (1.2,3.4) gives top-left content inset (1.9,4.1).
// The chosen child origin is (5.3,7.6), and snapping rounds its border-box to (5,8)-(29,25).
assert_vec2_approx_eq(
"border_box_translation",
ctx.border_box_translation(),
Vec2::new(1.9, 4.1),
);

assert_rect_approx_eq(
"content_box",
ctx.content_box(),
Rect::new(0., 0., 19.1, 7.7),
);
assert_rect_approx_eq(
"border_box",
ctx.border_box(),
Rect::new(-1.9, -4.1, 22.1, 12.9),
);
assert_rect_approx_eq(
"paint_box",
ctx.paint_box(),
Rect::new(-5.9, -6.1, 26.5, 15.9),
);
assert_rect_approx_eq(
"bounding_box",
ctx.bounding_box(),
Rect::new(1., 6., 33.4, 28.),
);

assert_point_approx_eq(
"to_window content box origin",
ctx.to_window(Point::ORIGIN),
Point::new(6.9, 12.1),
);
assert_point_approx_eq(
"to_window border box origin",
ctx.to_window(ctx.border_box().origin()),
Point::new(5., 8.),
);
assert_point_approx_eq(
"to_local content box origin",
ctx.to_local(Point::new(6.9, 12.1)),
Point::ORIGIN,
);
assert_point_approx_eq(
"window_transform",
ctx.window_transform() * Point::new(2., 1.),
Point::new(8.9, 13.1),
);
}

#[test]
fn content_box_clamps_when_insets_exceed_size() {
let tag = WidgetTag::unique();

let child = ModularWidget::new(()).prepare().with_tag(tag).with_props((
Dimensions::fixed(2.5.px(), 2.5.px()),
BorderWidth::all(0.5.px()),
Padding {
left: 0.7.px(),
right: 0.8.px(),
top: 0.6.px(),
bottom: 0.9.px(),
},
));

let root = ModularWidget::new_parent(child)
.layout_fn(|child, ctx, _, size| {
let child_size = ctx.compute_size(child, SizeDef::fit(size), size.into());
ctx.run_layout(child, child_size);
ctx.place_child(child, Point::new(0.6, 0.6));
})
.prepare()
.with_props(Dimensions::fixed(20.px(), 20.px()));

let harness = TestHarness::create(test_property_set(), root);
let child = harness.get_widget(tag);
let ctx = child.ctx();

// Border 0.5 + padding (0.7,0.6) gives top-left content inset (1.2,1.1).
assert_vec2_approx_eq(
"border_box_translation",
ctx.border_box_translation(),
Vec2::new(1.2, 1.1),
);
assert_rect_approx_eq(
"border_box",
ctx.border_box(),
Rect::new(-1.2, -1.1, 0.8, 0.9),
);
assert_rect_approx_eq("content_box", ctx.content_box(), Rect::ZERO);
}
14 changes: 13 additions & 1 deletion masonry/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! both to centralize tests in a single crate and to have access to the `masonry`
//! widget/property set in our tests if needed.

use crate::kurbo::Rect;
use crate::kurbo::{Point, Rect, Vec2};

mod accessibility;
mod action;
Expand All @@ -27,6 +27,18 @@ pub(crate) fn assert_approx_eq(name: &str, actual: f64, expected: f64) {
);
}

#[track_caller]
pub(crate) fn assert_point_approx_eq(name: &str, actual: Point, expected: Point) {
assert_approx_eq(&format!("{name}.x"), actual.x, expected.x);
assert_approx_eq(&format!("{name}.y"), actual.y, expected.y);
}

#[track_caller]
pub(crate) fn assert_vec2_approx_eq(name: &str, actual: Vec2, expected: Vec2) {
assert_approx_eq(&format!("{name}.x"), actual.x, expected.x);
assert_approx_eq(&format!("{name}.y"), actual.y, expected.y);
}

#[track_caller]
pub(crate) fn assert_rect_approx_eq(name: &str, actual: Rect, expected: Rect) {
assert_approx_eq(&format!("{name}.x0"), actual.x0, expected.x0);
Expand Down
5 changes: 3 additions & 2 deletions masonry/src/widgets/disclosure_button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,16 @@ impl Widget for DisclosureButton {
let cache = ctx.property_cache();
let button_color = props.get::<ContentColor>(cache);

let size = ctx.content_box_size();
let content_box = ctx.content_box();
let size = content_box.size();
let half_size = size * 0.5;

let mut arrow = BezPath::new();
arrow.move_to((0.0, -half_size.height));
arrow.line_to((half_size.width, 0.0));
arrow.line_to((0.0, half_size.height));

let mut affine = Affine::translate(half_size.to_vec2());
let mut affine = Affine::translate(content_box.center().to_vec2());

// Rotate if it's disclosed
if self.is_disclosed() {
Expand Down
14 changes: 7 additions & 7 deletions masonry/src/widgets/portal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ impl<W: Widget + FromDynWidget + ?Sized> Portal<W> {
///
/// A position of zero means no scrolling at all.
pub fn set_viewport_pos(this: &mut WidgetMut<'_, Self>, position: Point) -> bool {
let portal_size = this.ctx.content_box_size();
let portal_size = this.ctx.content_box().size();
let content_size = this.widget.content_size;

let pos_changed = this
Expand Down Expand Up @@ -387,7 +387,7 @@ impl<W: Widget + FromDynWidget + ?Sized> Portal<W> {
/// `target` is in the child's border-box coordinate space, meaning a target
/// of `(0, 0, 10, 10)` will scroll an item at the top-left of the child into view.
pub fn pan_viewport_to(this: &mut WidgetMut<'_, Self>, target: Rect) -> bool {
let portal_size = this.ctx.content_box_size();
let portal_size = this.ctx.content_box().size();
let viewport = Rect::from_origin_size(this.widget.viewport_pos, portal_size);

let new_pos_x = compute_pan_range(
Expand Down Expand Up @@ -420,7 +420,7 @@ impl<W: Widget + FromDynWidget + ?Sized> Widget for Portal<W> {
let cache = ctx.property_cache();
let auto_hide_scroll_bar = props.get::<AutoHideScrollBar>(cache).0;

let portal_size = ctx.content_box_size();
let portal_size = ctx.content_box().size();
let content_size = self.content_size;

match *event {
Expand Down Expand Up @@ -479,7 +479,7 @@ impl<W: Widget + FromDynWidget + ?Sized> Widget for Portal<W> {
_props: &mut PropertiesMut<'_>,
event: &TextEvent,
) {
let portal_size = ctx.content_box_size();
let portal_size = ctx.content_box().size();
let content_size = self.content_size;
let target = ctx.target();
let scrollbar_target =
Expand Down Expand Up @@ -583,7 +583,7 @@ impl<W: Widget + FromDynWidget + ?Sized> Widget for Portal<W> {
_props: &mut PropertiesMut<'_>,
event: &AccessEvent,
) {
let portal_size = ctx.content_box_size();
let portal_size = ctx.content_box().size();
let content_size = self.content_size;
let target = ctx.target();
let scrollbar_target =
Expand Down Expand Up @@ -682,7 +682,7 @@ impl<W: Widget + FromDynWidget + ?Sized> Widget for Portal<W> {
fn update(&mut self, ctx: &mut UpdateCtx<'_>, _props: &mut PropertiesMut<'_>, event: &Update) {
match event {
Update::RequestPanToChild(target) => {
let portal_size = ctx.content_box_size();
let portal_size = ctx.content_box().size();
let content_size = self.content_size;

self.pan_viewport_to_raw(portal_size, content_size, *target);
Expand Down Expand Up @@ -857,7 +857,7 @@ impl<W: Widget + FromDynWidget + ?Sized> Widget for Portal<W> {
) {
node.set_clips_children();

let portal_size = ctx.content_box_size();
let portal_size = ctx.content_box().size();
let content_size = self.content_size;
let scroll_range = (content_size - portal_size).max(Size::ZERO);

Expand Down
24 changes: 14 additions & 10 deletions masonry/src/widgets/resize_observer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::layout::{LenReq, Length};
/// It reports the child's length as its own in [`measure`], syncing its size with the child's.
///
/// The size of this widget can be accessed through [`MutateCtx`] methods like
/// [`border_box_size`] and [`content_box_size`].
/// [`border_box`] and [`content_box`].
///
/// Ensure that `ResizeObserver` has [`Dimensions`] set via props to [`Dimensions::MAX`].
/// Max preferred size of `ResizeObserver` means that the question of size
Expand Down Expand Up @@ -44,8 +44,8 @@ use crate::layout::{LenReq, Length};
/// [`Dimensions`]: crate::properties::Dimensions
/// [`Dimensions::MAX`]: crate::properties::Dimensions::MAX
/// [`MutateCtx`]: crate::core::MutateCtx
/// [`border_box_size`]: crate::core::MutateCtx::border_box_size
/// [`content_box_size`]: crate::core::MutateCtx::content_box_size
/// [`border_box`]: crate::core::MutateCtx::border_box
/// [`content_box`]: crate::core::MutateCtx::content_box
// TODO: It would be nice to at least catch these loops.
// We could see how many times layout is executed without us being painted, and setting a threshold.
// The response if that gets too high (100?) could be debug_panicking, then stopping
Expand Down Expand Up @@ -100,11 +100,11 @@ impl ResizeObserver {
/// Currently only used by [`ResizeObserver`].
/// Note that this event does not itself include the final size.
/// That should instead be accessed through [`MutateCtx`] methods like
/// [`border_box_size`] and [`content_box_size`].
/// [`border_box`] and [`content_box`].
///
/// [`MutateCtx`]: crate::core::MutateCtx
/// [`border_box_size`]: crate::core::MutateCtx::border_box_size
/// [`content_box_size`]: crate::core::MutateCtx::content_box_size
/// [`border_box`]: crate::core::MutateCtx::border_box
/// [`content_box`]: crate::core::MutateCtx::content_box
#[derive(Debug)]
pub struct LayoutChanged;

Expand Down Expand Up @@ -201,7 +201,8 @@ mod tests {
harness
.get_widget_with_id(observer_id)
.ctx()
.border_box_size(),
.border_box()
.size(),
Size {
width: 100.,
height: 100.,
Expand All @@ -218,7 +219,8 @@ mod tests {
harness
.get_widget_with_id(observer_id)
.ctx()
.border_box_size(),
.border_box()
.size(),
Size {
width: 100.,
height: 200.,
Expand Down Expand Up @@ -250,7 +252,8 @@ mod tests {
harness
.get_widget_with_id(observer_id)
.ctx()
.border_box_size(),
.border_box()
.size(),
Size {
width: 200.,
height: 200.,
Expand All @@ -267,7 +270,8 @@ mod tests {
harness
.get_widget_with_id(observer_id)
.ctx()
.border_box_size(),
.border_box()
.size(),
Size {
width: 100.,
height: 150.,
Expand Down
Loading
Loading