masonry 0.4.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
// Copyright 2025 the Xilem Authors
// SPDX-License-Identifier: Apache-2.0

use accesskit::ActionRequest;
use assert_matches::assert_matches;
use masonry_core::core::{AccessEvent, NewWidget, TextEvent, Widget, WidgetTag};
use masonry_testing::{
    ModularWidget, Record, TestHarness, TestWidgetExt, assert_any, assert_debug_panics,
};
use ui_events::keyboard::{Key, NamedKey};
use ui_events::pointer::{PointerButton, PointerEvent, PointerInfo, PointerType};
use vello::kurbo::Size;

use crate::properties::types::AsUnit;
use crate::theme::default_property_set;
use crate::widgets::{Button, Flex, SizedBox, TextArea};

// POINTER EVENTS

fn create_capture_target() -> ModularWidget<()> {
    ModularWidget::new(())
        .pointer_event_fn(|_, ctx, _, event| {
            if matches!(event, PointerEvent::Down { .. }) {
                ctx.capture_pointer();
            }
        })
        .layout_fn(|_, _, _, _| Size::new(10., 10.))
}

#[test]
fn pointer_event() {
    let button_tag = WidgetTag::new("button");

    let button = NewWidget::new_with_tag(Button::with_text("button").record(), button_tag);

    let mut harness = TestHarness::create(default_property_set(), button);
    let button_id = harness.get_widget(button_tag).id();

    harness.flush_records_of(button_tag);
    harness.mouse_move_to(button_id);

    let records = harness.take_records_of(button_tag);
    assert_any(records, |r| {
        matches!(r, Record::PointerEvent(PointerEvent::Move(_)))
    });
}

#[test]
fn pointer_event_bubbling() {
    let button_tag = WidgetTag::new("button");
    let parent_tag = WidgetTag::new("parent");
    let grandparent_tag = WidgetTag::new("grandparent");

    let button = NewWidget::new_with_tag(Button::with_text("button").record(), button_tag);
    let parent = NewWidget::new_with_tag(ModularWidget::new_parent(button).record(), parent_tag);
    let grandparent =
        NewWidget::new_with_tag(ModularWidget::new_parent(parent).record(), grandparent_tag);

    let mut harness = TestHarness::create(default_property_set(), grandparent);
    let button_id = harness.get_widget(button_tag).id();

    harness.flush_records_of(button_tag);
    harness.mouse_click_on(button_id);

    fn is_pointer_down(record: Record) -> bool {
        matches!(record, Record::PointerEvent(PointerEvent::Down { .. }))
    }

    assert_any(harness.take_records_of(button_tag), is_pointer_down);
    assert_any(harness.take_records_of(parent_tag), is_pointer_down);
    assert_any(harness.take_records_of(grandparent_tag), is_pointer_down);
}

#[test]
fn pointer_capture_and_cancel() {
    let target_tag = WidgetTag::new("target");

    let target = create_capture_target();
    let target = NewWidget::new_with_tag(target, target_tag);

    let mut harness = TestHarness::create(default_property_set(), target);

    let target_id = harness.get_widget(target_tag).id();

    harness.mouse_move_to(target_id);
    harness.mouse_button_press(PointerButton::Primary);
    assert_eq!(harness.pointer_capture_target_id(), Some(target_id));

    harness.process_pointer_event(PointerEvent::Cancel(PointerInfo {
        pointer_id: None,
        persistent_device_id: None,
        pointer_type: PointerType::default(),
    }));
    assert_eq!(harness.pointer_capture_target_id(), None);
}

#[test]
fn synthetic_cancel() {
    let target_tag = WidgetTag::new("target");

    let target = create_capture_target();
    let target = NewWidget::new_with_tag(target.record(), target_tag);

    let mut harness = TestHarness::create(default_property_set(), target);

    let target_id = harness.get_widget(target_tag).id();

    harness.mouse_move_to(target_id);
    harness.mouse_button_press(PointerButton::Primary);
    assert_eq!(harness.pointer_capture_target_id(), Some(target_id));

    // When we disable a widget with pointer capture, it gets a
    // synthetic PointerCancel event.
    harness.set_disabled(target_tag, true);

    let records = harness.take_records_of(target_tag);
    assert_any(records, |r| {
        matches!(r, Record::PointerEvent(PointerEvent::Cancel(_)))
    });
}

#[test]
fn pointer_capture_suppresses_neighbors() {
    let target_tag = WidgetTag::new("target");
    let other_tag = WidgetTag::new("other");

    let target = create_capture_target();
    let target = NewWidget::new_with_tag(target, target_tag);

    let other = Button::with_text("");
    let other = NewWidget::new_with_tag(other.record(), other_tag);

    let parent = Flex::column()
        .with_child(target)
        .with_child(other)
        .with_auto_id();

    let mut harness = TestHarness::create(default_property_set(), parent);
    harness.flush_records_of(other_tag);

    let target_id = harness.get_widget(target_tag).id();
    let other_id = harness.get_widget(other_tag).id();

    harness.mouse_move_to(target_id);
    harness.mouse_button_press(PointerButton::Primary);

    assert_eq!(harness.pointer_capture_target_id(), Some(target_id));

    // As long as 'target' is captured, 'other' doesn't get pointer events, even when the cursor is on it.
    harness.mouse_move_to(other_id);
    assert_matches!(harness.take_records_of(other_tag)[..], []);

    // 'other' is not considered hovered either.
    assert!(!harness.get_widget(other_tag).ctx().is_hovered());

    // We end pointer capture.
    harness.mouse_button_release(PointerButton::Primary);
    assert_eq!(harness.pointer_capture_target_id(), None);

    // Once the capture is released, 'other' should immediately register as hovered.
    assert!(harness.get_widget(other_tag).ctx().is_hovered());
}

#[test]
fn try_capture_pointer_on_pointer_move() {
    let widget = ModularWidget::new(())
        .pointer_event_fn(|_, ctx, _, _event| {
            ctx.capture_pointer();
        })
        .with_auto_id();

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

    assert_debug_panics!(
        harness.mouse_move((10.0, 10.0)),
        "event does not allow pointer capture"
    );
}

#[test]
fn try_capture_pointer_on_text_event() {
    let widget = ModularWidget::new(())
        .accepts_focus(true)
        .text_event_fn(|_, ctx, _, _event| {
            ctx.capture_pointer();
        })
        .with_auto_id();

    let mut harness = TestHarness::create(default_property_set(), widget);
    let id = harness.root_id();
    harness.focus_on(Some(id));

    assert_debug_panics!(
        harness.keyboard_type_chars("a"),
        "event does not allow pointer capture"
    );
}

#[test]
fn pointer_cancel_on_window_blur() {
    let target_tag = WidgetTag::new("target");

    let target = create_capture_target();
    let target = NewWidget::new_with_tag(target.record(), target_tag);

    let mut harness = TestHarness::create(default_property_set(), target);

    let target_id = harness.get_widget(target_tag).id();

    harness.mouse_move_to(target_id);
    harness.mouse_button_press(PointerButton::Primary);
    assert_eq!(harness.pointer_capture_target_id(), Some(target_id));
    harness.flush_records_of(target_tag);

    harness.process_text_event(TextEvent::WindowFocusChange(false));

    let records = harness.take_records_of(target_tag);
    assert_any(records, |r| {
        matches!(r, Record::PointerEvent(PointerEvent::Cancel(..)))
    });
}

#[test]
fn click_anchors_focus() {
    let child_3 = WidgetTag::new("child_3");
    let child_4 = WidgetTag::new("child_4");
    let other = WidgetTag::new("other");

    let parent = Flex::column()
        .with_child(NewWidget::new_with_tag(
            SizedBox::empty().size(5.px(), 5.px()),
            other,
        ))
        .with_child(NewWidget::new(Button::with_text("")))
        .with_child(NewWidget::new(Button::with_text("")))
        .with_child(NewWidget::new_with_tag(Button::with_text(""), child_3))
        .with_child(NewWidget::new_with_tag(Button::with_text(""), child_4))
        .with_child(NewWidget::new(Button::with_text("")))
        .with_auto_id();

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

    let child_3_id = harness.get_widget(child_3).id();
    let child_4_id = harness.get_widget(child_4).id();
    let other_id = harness.get_widget(other).id();

    // Clicking a button doesn't focus it.
    harness.mouse_click_on(child_3_id);
    assert_eq!(harness.focused_widget_id(), None);

    // But the next tab event focuses its neighbor.
    harness.process_text_event(TextEvent::key_down(Key::Named(NamedKey::Tab)));
    assert_eq!(harness.focused_widget_id(), Some(child_4_id));

    // Clicking another non-focusable widget clears focus.
    harness.mouse_move_to_unchecked(other_id);
    harness.mouse_button_press(PointerButton::Primary);
    harness.mouse_button_release(PointerButton::Primary);
    assert_eq!(harness.focused_widget_id(), None);
}

// TEXT EVENTS

#[test]
fn text_event() {
    let target_tag = WidgetTag::new("target");

    let target = NewWidget::new_with_tag(TextArea::new_editable("").record(), target_tag);

    let mut harness = TestHarness::create(default_property_set(), target);
    let target_id = harness.get_widget(target_tag).id();
    harness.flush_records_of(target_tag);

    // The widget isn't focused, it doesn't get text events.
    harness.keyboard_type_chars("A");
    assert_matches!(harness.take_records_of(target_tag)[..], []);

    // We focus on the widget, now it gets text events.
    harness.focus_on(Some(target_id));
    harness.keyboard_type_chars("A");
    let records = harness.take_records_of(target_tag);
    assert_any(records, |r| matches!(r, Record::TextEvent(_)));
}

#[test]
fn text_event_bubbling() {
    let target_tag = WidgetTag::new("target");
    let parent_tag = WidgetTag::new("parent");
    let grandparent_tag = WidgetTag::new("grandparent");

    let target = NewWidget::new_with_tag(
        ModularWidget::new(()).accepts_focus(true).record(),
        target_tag,
    );
    let parent = NewWidget::new_with_tag(ModularWidget::new_parent(target).record(), parent_tag);
    let grandparent =
        NewWidget::new_with_tag(ModularWidget::new_parent(parent).record(), grandparent_tag);

    let mut harness = TestHarness::create(default_property_set(), grandparent);
    let target_id = harness.get_widget(target_tag).id();

    harness.focus_on(Some(target_id));
    harness.process_text_event(TextEvent::key_down(Key::Character("A".into())));

    fn is_keyboard_event(record: Record) -> bool {
        matches!(record, Record::TextEvent(TextEvent::Keyboard(_)))
    }

    assert_any(harness.take_records_of(target_tag), is_keyboard_event);
    assert_any(harness.take_records_of(parent_tag), is_keyboard_event);
    assert_any(harness.take_records_of(grandparent_tag), is_keyboard_event);
}

#[test]
fn text_event_fallback() {
    let target_tag = WidgetTag::new("target");
    let other_tag = WidgetTag::new("other");

    let target = NewWidget::new_with_tag(TextArea::new_editable("").record(), target_tag);
    let other = NewWidget::new_with_tag(TextArea::new_editable(""), other_tag);
    let parent = Flex::row()
        .with_child(target)
        .with_child(other)
        .with_auto_id();

    let mut harness = TestHarness::create(default_property_set(), parent);
    let target_id = harness.get_widget(target_tag).id();
    let other_id = harness.get_widget(other_tag).id();
    harness.flush_records_of(target_tag);
    harness.set_focus_fallback(Some(target_id));

    harness.focus_on(Some(other_id));
    assert_matches!(harness.take_records_of(target_tag)[..], []);

    // If a widget is set as focus fallback, that widget gets text events when no widget is focused.
    harness.focus_on(None);
    harness.keyboard_type_chars("A");
    let records = harness.take_records_of(target_tag);
    assert_any(records, |r| matches!(r, Record::TextEvent(_)));

    // Unless it's disabled.
    harness.set_disabled(target_tag, true);
    harness.flush_records_of(target_tag);
    harness.keyboard_type_chars("A");
    assert_matches!(harness.take_records_of(target_tag)[..], []);
}

#[test]
fn tab_focus() {
    let child_1 = WidgetTag::new("child_1");
    let child_2 = WidgetTag::new("child_2");
    let child_3 = WidgetTag::new("child_3");
    let child_4 = WidgetTag::new("child_4");
    let child_5 = WidgetTag::new("child_5");

    let parent = Flex::column()
        .with_child(NewWidget::new_with_tag(Button::with_text(""), child_1))
        .with_child(NewWidget::new_with_tag(Button::with_text(""), child_2))
        .with_child(NewWidget::new_with_tag(Button::with_text(""), child_3))
        .with_child(NewWidget::new_with_tag(Button::with_text(""), child_4))
        .with_child(NewWidget::new_with_tag(Button::with_text(""), child_5))
        .with_auto_id();

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

    let child_1_id = harness.get_widget(child_1).id();
    let child_2_id = harness.get_widget(child_2).id();
    let child_3_id = harness.get_widget(child_3).id();
    let child_4_id = harness.get_widget(child_4).id();
    let child_5_id = harness.get_widget(child_5).id();

    assert_eq!(harness.focused_widget_id(), None);

    // Tab moves focus to the next focusable widget in the tree.
    harness.focus_on(Some(child_2_id));
    harness.press_tab_key(false);
    assert_eq!(harness.focused_widget_id(), Some(child_3_id));

    // Shift+Tab moves focus to the previous focusable widget in the tree.
    harness.focus_on(Some(child_4_id));
    harness.press_tab_key(true);
    assert_eq!(harness.focused_widget_id(), Some(child_3_id));

    // When nothing is focused, Tab focuses the first focusable widget in the tree.
    harness.focus_on(None);
    harness.press_tab_key(false);
    assert_eq!(harness.focused_widget_id(), Some(child_1_id));

    // When nothing is focused, Shift+Tab focuses the last focusable widget in the tree.
    harness.focus_on(None);
    harness.press_tab_key(true);
    assert_eq!(harness.focused_widget_id(), Some(child_5_id));
}

// ACCESS EVENTS

#[test]
fn access_event_bubbling() {
    let target_tag = WidgetTag::new("target");
    let parent_tag = WidgetTag::new("parent");
    let grandparent_tag = WidgetTag::new("grandparent");

    let target = NewWidget::new_with_tag(ModularWidget::new(()).record(), target_tag);
    let parent = NewWidget::new_with_tag(ModularWidget::new_parent(target).record(), parent_tag);
    let grandparent =
        NewWidget::new_with_tag(ModularWidget::new_parent(parent).record(), grandparent_tag);

    let mut harness = TestHarness::create(default_property_set(), grandparent);
    let target_id = harness.get_widget(target_tag).id();

    // Send random event
    harness.process_access_event(ActionRequest {
        action: accesskit::Action::Click,
        target: target_id.into(),
        data: None,
    });

    fn is_access_click(record: Record) -> bool {
        matches!(
            record,
            Record::AccessEvent(AccessEvent {
                action: accesskit::Action::Click,
                data: None
            })
        )
    }

    assert_any(harness.take_records_of(target_tag), is_access_click);
    assert_any(harness.take_records_of(parent_tag), is_access_click);
    assert_any(harness.take_records_of(grandparent_tag), is_access_click);
}

#[test]
fn accessibility_focus() {
    let child_2 = WidgetTag::new("child_2");
    let child_3 = WidgetTag::new("child_3");

    let parent = Flex::column()
        .with_child(NewWidget::new(Button::with_text("")))
        .with_child(NewWidget::new_with_tag(Button::with_text(""), child_2))
        .with_child(NewWidget::new_with_tag(Button::with_text(""), child_3))
        .with_child(NewWidget::new(Button::with_text("")))
        .with_auto_id();

    let mut harness = TestHarness::create(default_property_set(), parent);
    let child_2_id = harness.get_widget(child_2).id();
    let child_3_id = harness.get_widget(child_3).id();

    // Send focus event
    harness.process_access_event(ActionRequest {
        action: accesskit::Action::Focus,
        target: child_3_id.into(),
        data: None,
    });
    assert_eq!(harness.focused_widget_id(), Some(child_3_id));

    // Send blur event with incorrect id
    harness.process_access_event(ActionRequest {
        action: accesskit::Action::Blur,
        target: child_2_id.into(),
        data: None,
    });
    assert_eq!(harness.focused_widget_id(), Some(child_3_id));

    // Send blur event with correct id
    harness.process_access_event(ActionRequest {
        action: accesskit::Action::Blur,
        target: child_3_id.into(),
        data: None,
    });
    assert_eq!(harness.focused_widget_id(), None);
}