masonry 0.5.0

Traits and types of the Masonry toolkit.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
// Copyright 2025 the Xilem Authors
// SPDX-License-Identifier: Apache-2.0

use std::cell::Cell;
use std::rc::Rc;

use std::assert_matches;

use crate::core::{NewWidget, Widget, WidgetTag};
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};

#[test]
fn layout_simple() {
    const BOX_WIDTH: f64 = 50.;

    let tag_1 = WidgetTag::named("box1");
    let tag_2 = WidgetTag::named("box2");
    let box_side = Length::px(BOX_WIDTH);

    let widget = Flex::column()
        .with_fixed(
            Flex::row()
                .with_fixed(
                    NewWidget::new(SizedBox::empty().width(box_side).height(box_side))
                        .with_tag(tag_1),
                )
                .with_fixed(
                    NewWidget::new(SizedBox::empty().width(box_side).height(box_side))
                        .with_tag(tag_2),
                )
                .with_spacer(1.0)
                .prepare(),
        )
        .with_spacer(1.0)
        .prepare();

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

    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);
    assert_eq!(first_box_size.height, BOX_WIDTH);

    assert_eq!(first_box_paint_rect.x0, 0.0);
    assert_eq!(first_box_paint_rect.y0, 0.0);
    assert_eq!(first_box_paint_rect.x1, BOX_WIDTH);
    assert_eq!(first_box_paint_rect.y1, BOX_WIDTH);
}

#[test]
fn forget_to_recurse_layout() {
    let widget = ModularWidget::new_parent(Flex::row().prepare())
        .measure_fn(|_, _, _, _, _, _| Length::ZERO)
        .layout_fn(|_child, _ctx, _, _| {
            // We forget to call ctx.run_layout();
        })
        .prepare();

    assert_debug_panics!(
        TestHarness::create(test_property_set(), widget),
        "LayoutCtx::run_layout() was not called"
    );
}

#[test]
fn forget_to_call_place_child() {
    let widget = ModularWidget::new_parent(Flex::row().prepare())
        .layout_fn(|child, ctx, _, size| {
            // We call ctx.run_layout(), but forget place_child
            ctx.run_layout(child, size);
        })
        .prepare();

    assert_debug_panics!(
        TestHarness::create(test_property_set(), widget),
        "LayoutCtx::place_child() was not called"
    );
}

#[test]
fn call_place_child_before_layout() {
    let widget = ModularWidget::new_parent(Flex::row().prepare())
        .measure_fn(|_, _, _, _, _, _| Length::ZERO)
        .layout_fn(|child, ctx, _, _| {
            // We call ctx.place_child(), but forget run_layout
            ctx.place_child(child, Point::ORIGIN);
        })
        .prepare();

    assert_debug_panics!(
        TestHarness::create(test_property_set(), widget),
        "trying to call 'place_child'"
    );
}

#[test]
fn call_child_size_before_layout() {
    let parent_tag = WidgetTag::unique();
    let layout_count = Rc::new(Cell::new(0));
    let layout_count_for_fn = layout_count.clone();

    let child = NewWidget::new(SizedBox::empty().width(20.px()).height(20.px()));
    let parent = ModularWidget::new_parent(child).layout_fn(move |child, ctx, _, size| {
        if layout_count_for_fn.get() > 0 {
            let _ = ctx.child_size(child);
        }

        layout_count_for_fn.set(layout_count_for_fn.get() + 1);

        let child_size = ctx.compute_size(child, SizeDef::fit(size), size.into());
        ctx.run_layout(child, child_size);
        ctx.place_child(child, Point::ORIGIN);
    });
    let widget = NewWidget::new(parent).with_tag(parent_tag);

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

    assert_debug_panics!(
        harness.edit_widget(parent_tag, |mut parent| {
            parent.ctx.request_layout();
        }),
        "trying to call 'child_size'"
    );
}

#[test]
fn run_layout_on_stashed() {
    let parent_tag = WidgetTag::named("parent");
    let widget =
        ModularWidget::new_parent(Flex::row().prepare()).layout_fn(|child, ctx, _, size| {
            ctx.run_layout(child, size);
            ctx.place_child(child, Point::ZERO);
        });
    let widget = NewWidget::new(widget).with_tag(parent_tag);

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

    assert_debug_panics!(
        harness.edit_widget(parent_tag, |mut parent| {
            parent.ctx.set_stashed(&mut parent.widget.state, true);
            parent.ctx.request_layout();
        }),
        "trying to compute layout of stashed widget"
    );
}

#[test]
fn stash_then_run_layout() {
    let parent_tag = WidgetTag::named("parent");
    let widget =
        ModularWidget::new_parent(Flex::row().prepare()).layout_fn(|child, ctx, _, size| {
            // We check that stashing a widget is effective "immediately"
            // and triggers an error.
            ctx.set_stashed(child, true);
            ctx.run_layout(child, size);
            ctx.place_child(child, Point::ZERO);
        });
    let widget = NewWidget::new(widget).with_tag(parent_tag);

    assert_debug_panics!(
        TestHarness::create(test_property_set(), widget),
        "trying to compute layout of stashed widget"
    );
}

#[test]
fn unstash_then_run_layout() {
    let parent_tag = WidgetTag::named("parent");
    let widget =
        ModularWidget::new_parent(Flex::row().prepare()).layout_fn(|child, ctx, _, size| {
            // We check that unstashing a widget is effective "immediately"
            // and avoids an error.
            ctx.set_stashed(child, false);
            ctx.run_layout(child, size);
            ctx.place_child(child, Point::ZERO);
        });
    let widget = NewWidget::new(widget).with_tag(parent_tag);

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

    harness.edit_widget(parent_tag, |mut parent| {
        parent.ctx.set_stashed(&mut parent.widget.state, true);
        parent.ctx.request_layout();
    });
}

#[test]
fn skip_layout_when_cached() {
    let button_tag = WidgetTag::named("button");
    let sibling_tag = WidgetTag::named("sibling");

    let button = NewWidget::new(Button::with_text("Foobar").record()).with_tag(button_tag);
    let sibling =
        NewWidget::new(SizedBox::empty().width(20.px()).height(20.px())).with_tag(sibling_tag);

    // We choose a ZStack, because it should pass down the same constraints no matter what.
    let parent = NewWidget::new(
        ZStack::new()
            .with(button, ChildAlignment::ParentAligned)
            .with(sibling, ChildAlignment::ParentAligned),
    );

    let mut harness = TestHarness::create(test_property_set(), parent);

    harness.flush_records_of(button_tag);
    harness.edit_widget(sibling_tag, |mut sized_box| {
        SizedBox::set_width(&mut sized_box, 30.px());
        SizedBox::set_height(&mut sized_box, 30.px());
    });

    // The button did not request layout and its input constraints are the same:
    // Nothing should happen to it.
    let button_records_iter = harness.take_records_of(button_tag).into_iter();

    // Measurements will still happen with debug assertions enabled because we verify the cache.
    #[cfg(debug_assertions)]
    let button_records_iter =
        button_records_iter.filter(|r| !matches!(r, masonry_testing::Record::Measure(_)));

    let button_records: Vec<_> = button_records_iter.collect();
    assert_matches!(button_records[..], []);
}

#[test]
fn pixel_snapping() {
    let child_tag = WidgetTag::named("child");
    let child = NewWidget::new(SizedBox::empty().size(10.3.px(), 10.3.px())).with_tag(child_tag);
    let pos = Point::new(5.1, 5.3);
    let parent = ModularWidget::new_parent(child).layout_fn(move |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, pos);
        ctx.set_baselines(2.4, 2.6);
    });
    let parent_tag = WidgetTag::named("parent");
    let parent = NewWidget::new(parent).with_tag(parent_tag);

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

    let child = harness.get_widget(child_tag);
    let ctx = child.ctx();
    let border_box = ctx.border_box();
    let content_box = ctx.content_box();
    let child_pos = ctx.to_window(border_box.origin());
    let first_baseline = harness.get_widget(parent_tag).ctx().first_baseline();
    let last_baseline = harness.get_widget(parent_tag).ctx().last_baseline();

    assert_eq!(child_pos, Point::new(5.0, 5.0));
    assert_eq!(content_box.origin(), Point::ORIGIN);
    assert_eq!(content_box.size(), Size::new(10., 11.));
    assert_eq!(border_box.size(), Size::new(10., 11.));
    assert_eq!(first_baseline, 2.4);
    assert_eq!(last_baseline, 2.6);
}

#[test]
fn layout_insets() {
    const BOX_WIDTH: f64 = 50.;

    let child_tag = WidgetTag::named("child");
    let parent_tag = WidgetTag::named("parent");

    let child_widget = ModularWidget::new(())
        .measure_fn(|_, _, _, _, _, _| BOX_WIDTH.px())
        .layout_fn(|_, ctx, _, _| {
            // this widget paints twenty points above and below its layout bounds
            ctx.set_paint_insets(Insets::uniform_xy(0., 20.));
        });

    let parent_widget = NewWidget::new(SizedBox::new(
        NewWidget::new(child_widget).with_tag(child_tag),
    ))
    .with_tag(parent_tag);

    let root_widget = Portal::new(parent_widget).prepare();

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

    let child_paint_rect = harness.get_widget(child_tag).ctx().paint_box();
    let parent_paint_rect = harness.get_widget(parent_tag).ctx().paint_box();
    let parent_bounding_rect = harness.get_widget(parent_tag).ctx().bounding_box();

    // The child's paint box is affected by its paint insets
    assert_eq!(child_paint_rect.x0, 0.0);
    assert_eq!(child_paint_rect.y0, -20.0);
    assert_eq!(child_paint_rect.x1, BOX_WIDTH);
    assert_eq!(child_paint_rect.y1, BOX_WIDTH + 20.0);

    // The parent's paint box is not affected by the child's paint insets
    assert_eq!(parent_paint_rect.x0, 0.0);
    assert_eq!(parent_paint_rect.y0, 0.0);
    assert_eq!(parent_paint_rect.x1, BOX_WIDTH);
    assert_eq!(parent_paint_rect.y1, BOX_WIDTH);

    // The parent's bounding box is affected by the child's paint insets
    assert_eq!(parent_bounding_rect.x0, 0.0);
    assert_eq!(parent_bounding_rect.y0, -20.0);
    assert_eq!(parent_bounding_rect.x1, BOX_WIDTH);
    assert_eq!(parent_bounding_rect.y1, BOX_WIDTH + 20.0);
}

#[test]
fn content_box() {
    let tag = WidgetTag::named("hero");

    let props = (
        Dimensions::fixed(100.px(), 100.px()),
        Padding {
            left: 1.px(),
            right: 2.px(),
            top: 3.px(),
            bottom: 4.px(),
        },
        BorderWidth::all(1.px()),
    );

    let hero = NewWidget::new(Button::with_text("Hero"))
        .with_tag(tag)
        .with_props(props);

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

    let border_box = harness.get_widget(tag).ctx().border_box();
    let border_box_size = border_box.size();
    let content_box = harness.get_widget(tag).ctx().content_box();
    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.);
    let expected_content_box_size = Size::new(95., 91.);

    assert_eq!(border_box_size, expected_border_box_size);
    assert_eq!(border_box.size(), expected_border_box_size);

    assert_eq!(content_box_size, expected_content_box_size);
    assert_eq!(content_box.size(), expected_content_box_size);

    let expected_border_box_origin = Point::new(-2., -4.);
    let expected_content_box_origin = Point::ORIGIN;

    assert_eq!(
        border_box_translation,
        -expected_border_box_origin.to_vec2()
    );

    let expected_border_box =
        Rect::from_origin_size(expected_border_box_origin, expected_border_box_size);
    let expected_content_box =
        Rect::from_origin_size(expected_content_box_origin, expected_content_box_size);

    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);
}