bevy_pf 0.2.9

A XAML / WPF-like UI framework for Bevy: XAML in macros or files, styling with resources, and the common WPF control set.
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
516
//! TabControl, TreeView, Menu/ContextMenu, and DataGrid.

use bevy::asset::AssetPlugin;
use bevy::prelude::*;
use bevy::reflect::Reflect;
use bevy::ui::widget::Text;
use bevy_pf::prelude::*;
use bevy_pf::{XamlEnv, instantiate_document_env};

fn test_app() -> App {
    let mut app = App::new();
    app.add_plugins((MinimalPlugins, AssetPlugin::default()));
    app.add_plugins(PfUiPlugin);
    app
}

fn spawn(app: &mut App, xaml: &str) -> Entity {
    let doc = bevy_pf_xaml::parse(xaml).expect("parses");
    let world = app.world_mut();
    let root = world.spawn_empty().id();
    let result =
        instantiate_document_env(world, root, &doc, &XamlEnv::default()).expect("instantiates");
    assert!(
        result.warnings.is_empty(),
        "expected clean instantiation: {:?}",
        result.warnings
    );
    root
}

fn named(app: &App, root: Entity, name: &str) -> Entity {
    app.world()
        .get::<XamlNames>(root)
        .unwrap()
        .get(name)
        .unwrap()
}

fn children_of(app: &App, e: Entity) -> Vec<Entity> {
    app.world()
        .get::<Children>(e)
        .map(|c| c.iter().collect())
        .unwrap_or_default()
}

#[test]
fn tab_control_selection() {
    let mut app = test_app();
    let root = spawn(
        &mut app,
        r#"<TabControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                       x:Name="T" SelectedIndex="1">
             <TabItem Header="General"><TextBlock Text="general content"/></TabItem>
             <TabItem Header="Advanced"><TextBlock Text="advanced content"/></TabItem>
             <TabItem Header="About"><TextBlock Text="about content"/></TabItem>
           </TabControl>"#,
    );
    let tab = named(&app, root, "T");
    let state = app
        .world()
        .get::<bevy_pf::components::PfTabControl>(tab)
        .unwrap()
        .clone();
    assert_eq!(state.headers.len(), 3);
    assert_eq!(state.contents.len(), 3);
    assert_eq!(state.selected, 1);

    // Only the selected tab's content is visible.
    fn display(app: &App, e: Entity) -> Display {
        app.world().get::<Node>(e).unwrap().display
    }
    assert_eq!(display(&app, state.contents[0]), Display::None);
    assert_ne!(display(&app, state.contents[1]), Display::None);
    assert_eq!(display(&app, state.contents[2]), Display::None);

    // Selected header is highlighted white.
    assert_eq!(
        app.world()
            .get::<BackgroundColor>(state.headers[1])
            .unwrap()
            .0,
        Color::WHITE
    );

    bevy_pf::instantiate::select_tab(app.world_mut(), tab, 2);
    let state = app
        .world()
        .get::<bevy_pf::components::PfTabControl>(tab)
        .unwrap()
        .clone();
    assert_eq!(state.selected, 2);
    assert_eq!(display(&app, state.contents[1]), Display::None);
    assert_ne!(display(&app, state.contents[2]), Display::None);
}

#[test]
fn tree_view_expansion_and_selection() {
    let mut app = test_app();
    let root = spawn(
        &mut app,
        r#"<TreeView xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="Tree">
             <TreeViewItem Header="Root" IsExpanded="True" x:Name="RootItem">
               <TreeViewItem Header="Child A" x:Name="ChildA">
                 <TreeViewItem Header="Grandchild"/>
               </TreeViewItem>
               <TreeViewItem Header="Child B"/>
             </TreeViewItem>
           </TreeView>"#,
    );
    let tree = named(&app, root, "Tree");
    let root_item = named(&app, root, "RootItem");
    let child_a = named(&app, root, "ChildA");

    let root_state = app
        .world()
        .get::<bevy_pf::components::PfTreeItem>(root_item)
        .unwrap()
        .clone();
    assert!(root_state.expanded);
    assert!(root_state.has_children);
    assert_ne!(
        app.world()
            .get::<Node>(root_state.container)
            .unwrap()
            .display,
        Display::None
    );
    // Two children under the root item.
    assert_eq!(children_of(&app, root_state.container).len(), 2);

    // Child A starts collapsed.
    let a_state = app
        .world()
        .get::<bevy_pf::components::PfTreeItem>(child_a)
        .unwrap()
        .clone();
    assert!(!a_state.expanded);
    assert_eq!(
        app.world().get::<Node>(a_state.container).unwrap().display,
        Display::None
    );

    // Toggling expands + selects it.
    bevy_pf::instantiate::toggle_tree_item(app.world_mut(), tree, child_a);
    let a_state = app
        .world()
        .get::<bevy_pf::components::PfTreeItem>(child_a)
        .unwrap()
        .clone();
    assert!(a_state.expanded);
    assert_ne!(
        app.world().get::<Node>(a_state.container).unwrap().display,
        Display::None
    );
    assert_eq!(
        app.world()
            .get::<bevy_pf::components::PfTreeView>(tree)
            .unwrap()
            .selected,
        Some(child_a)
    );
    // Arrow glyph flipped.
    assert_eq!(app.world().get::<Text>(a_state.arrow).unwrap().0, "−");
}

#[test]
fn menu_submenus_open_and_close() {
    let mut app = test_app();
    let root = spawn(
        &mut app,
        r#"<Menu xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="M">
             <MenuItem Header="File" x:Name="File">
               <MenuItem Header="New" x:Name="New"/>
               <MenuItem Header="Open Recent" x:Name="Recent">
                 <MenuItem Header="a.txt"/>
               </MenuItem>
               <Separator/>
               <MenuItem Header="Exit" x:Name="Exit"/>
             </MenuItem>
             <MenuItem Header="Help" x:Name="Help">
               <MenuItem Header="About"/>
             </MenuItem>
           </Menu>"#,
    );
    let menu = named(&app, root, "M");
    let file = named(&app, root, "File");
    let help = named(&app, root, "Help");
    let exit = named(&app, root, "Exit");

    let file_state = app
        .world()
        .get::<bevy_pf::components::PfMenuItem>(file)
        .unwrap()
        .clone();
    let file_popup = file_state.submenu.expect("File has a submenu");
    // The submenu lives under the overlay with a logical link back.
    assert_eq!(
        app.world()
            .get::<bevy_pf::components::PfLogicalParent>(file_popup)
            .unwrap()
            .0,
        file
    );
    // 4 entries: New, Open Recent, separator, Exit.
    assert_eq!(children_of(&app, file_popup).len(), 4);

    // Activate "File" -> its popup opens.
    bevy_pf::instantiate::activate_menu_item(app.world_mut(), file);
    assert!(
        app.world()
            .get::<bevy_pf::PfPopup>(file_popup)
            .unwrap()
            .open
    );

    // Activating "Help" closes File's popup, opens Help's.
    bevy_pf::instantiate::activate_menu_item(app.world_mut(), help);
    assert!(
        !app.world()
            .get::<bevy_pf::PfPopup>(file_popup)
            .unwrap()
            .open
    );
    let help_popup = app
        .world()
        .get::<bevy_pf::components::PfMenuItem>(help)
        .unwrap()
        .submenu
        .unwrap();
    assert!(
        app.world()
            .get::<bevy_pf::PfPopup>(help_popup)
            .unwrap()
            .open
    );

    // A leaf item closes everything in this menu.
    bevy_pf::instantiate::activate_menu_item(app.world_mut(), file);
    bevy_pf::instantiate::activate_menu_item(app.world_mut(), exit);
    assert!(
        !app.world()
            .get::<bevy_pf::PfPopup>(file_popup)
            .unwrap()
            .open
    );
    assert!(
        !app.world()
            .get::<bevy_pf::PfPopup>(help_popup)
            .unwrap()
            .open
    );
    let _ = menu;
}

#[test]
fn context_menu_attaches_and_opens() {
    let mut app = test_app();
    let root = spawn(
        &mut app,
        r#"<Border xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                   x:Name="B" Width="100" Height="50">
             <Border.ContextMenu>
               <ContextMenu>
                 <MenuItem Header="Copy" x:Name="Copy"/>
                 <MenuItem Header="Paste"/>
               </ContextMenu>
             </Border.ContextMenu>
           </Border>"#,
    );
    let owner = named(&app, root, "B");
    // The context popup is a menu popup rooted at the owner.
    let mut found = None;
    let mut query = app
        .world_mut()
        .query::<(Entity, &bevy_pf::components::PfMenuPopup)>();
    for (e, m) in query.iter(app.world()) {
        if m.menu_root == owner {
            found = Some(e);
        }
    }
    let popup = found.expect("context menu popup exists");
    assert_eq!(children_of(&app, popup).len(), 2);
    assert!(!app.world().get::<bevy_pf::PfPopup>(popup).unwrap().open);

    // Leaf activation closes it after a manual open.
    app.world_mut()
        .get_mut::<bevy_pf::PfPopup>(popup)
        .unwrap()
        .open = true;
    let copy = named(&app, root, "Copy");
    bevy_pf::instantiate::activate_menu_item(app.world_mut(), copy);
    assert!(!app.world().get::<bevy_pf::PfPopup>(popup).unwrap().open);
}

#[derive(Reflect, Default)]
struct Row {
    name: String,
    score: u32,
}

#[derive(Reflect, Default)]
struct GridVm {
    rows: Vec<Row>,
}

#[test]
fn data_grid_columns_and_rows() {
    let mut app = test_app();
    let vm = Bindable::new(GridVm {
        rows: vec![
            Row {
                name: "Ada".into(),
                score: 10,
            },
            Row {
                name: "Bob".into(),
                score: 20,
            },
        ],
    });
    let root = spawn(
        &mut app,
        r#"<DataGrid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                     x:Name="G" ItemsSource="{Binding rows}">
             <DataGrid.Columns>
               <DataGridTextColumn Header="Name" Binding="{Binding name}" Width="2*"/>
               <DataGridTextColumn Header="Score" Binding="{Binding score}" Width="*"/>
             </DataGrid.Columns>
           </DataGrid>"#,
    );
    app.world_mut()
        .entity_mut(root)
        .insert(DataContext(vm.clone()));
    app.update();

    let grid = named(&app, root, "G");
    let state = app
        .world()
        .get::<bevy_pf::components::PfDataGrid>(grid)
        .unwrap()
        .clone();
    assert_eq!(state.columns.len(), 2);
    assert_eq!(state.columns[0].header, "Name");
    assert_eq!(state.columns[1].path, "score");

    // Header row + rows host under the grid.
    let kids = children_of(&app, grid);
    assert_eq!(kids.len(), 2);
    let header_cells = children_of(&app, kids[0]);
    assert_eq!(app.world().get::<Text>(header_cells[0]).unwrap().0, "Name");

    // Generated rows with per-column cells.
    let rows = children_of(&app, state.rows_host);
    assert_eq!(rows.len(), 2);
    let row0_cells = children_of(&app, rows[0]);
    assert_eq!(app.world().get::<Text>(row0_cells[0]).unwrap().0, "Ada");
    assert_eq!(app.world().get::<Text>(row0_cells[1]).unwrap().0, "10");

    // Rows are selectable via the ListBox mechanism on the rows host.
    assert!(
        app.world()
            .get::<bevy_pf::components::PfListBox>(state.rows_host)
            .is_some()
    );
    assert!(
        app.world()
            .get::<bevy_pf::components::PfListBoxItem>(rows[0])
            .is_some()
    );

    // Model change rebuilds rows.
    vm.update(|m: &mut GridVm| {
        m.rows.push(Row {
            name: "Cleo".into(),
            score: 30,
        })
    });
    app.update();
    let rows = children_of(&app, state.rows_host);
    assert_eq!(rows.len(), 3);
}

#[test]
fn checkable_menu_item_toggles_on_activation() {
    let mut app = test_app();
    let root = spawn(
        &mut app,
        r#"<Menu xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
             <MenuItem Header="View">
               <MenuItem x:Name="Standard" IsCheckable="true" IsChecked="True" Header="Standard"/>
               <MenuItem x:Name="Plain" Header="Plain"/>
             </MenuItem>
           </Menu>"#,
    );
    let standard = named(&app, root, "Standard");
    let plain = named(&app, root, "Plain");

    // Initial IsChecked="True": Checked present, glyph visible.
    assert!(app.world().get::<bevy::ui::Checked>(standard).is_some());
    let glyph = app
        .world()
        .get::<bevy_pf::components::PfCheckableMenuItem>(standard)
        .unwrap()
        .glyph;
    assert_eq!(
        app.world().get::<Node>(glyph).unwrap().display,
        Display::Flex
    );

    // Leaf activation toggles off, then back on.
    bevy_pf::instantiate::activate_menu_item(app.world_mut(), standard);
    assert!(app.world().get::<bevy::ui::Checked>(standard).is_none());
    assert_eq!(
        app.world().get::<Node>(glyph).unwrap().display,
        Display::None
    );
    bevy_pf::instantiate::activate_menu_item(app.world_mut(), standard);
    assert!(app.world().get::<bevy::ui::Checked>(standard).is_some());

    // Non-checkable items are unaffected by activation.
    bevy_pf::instantiate::activate_menu_item(app.world_mut(), plain);
    assert!(app.world().get::<bevy::ui::Checked>(plain).is_none());
    assert!(
        app.world()
            .get::<bevy_pf::components::PfCheckableMenuItem>(plain)
            .is_none()
    );
}

#[test]
fn scroll_bar_structure_nudge_and_binding() {
    let mut app = test_app();
    let vm = Bindable::new(ScrollVm { pos: 20.0 });
    let root = spawn(
        &mut app,
        r##"<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
             <ScrollBar x:Name="S" Minimum="0" Maximum="100" ViewportSize="25"
                        SmallChange="5" Value="{Binding pos}"/>
           </StackPanel>"##,
    );
    app.world_mut()
        .entity_mut(root)
        .insert(DataContext(vm.clone()));
    app.update();

    let names = app.world().get::<XamlNames>(root).unwrap();
    let bar = names.get("S").unwrap();
    let sb = app
        .world()
        .get::<bevy_pf::components::PfScrollBar>(bar)
        .unwrap()
        .clone();

    // Value binding landed on the SliderValue carrier.
    assert_eq!(
        app.world()
            .get::<bevy::ui_widgets::SliderValue>(bar)
            .unwrap()
            .0,
        20.0
    );

    // Thumb is proportional: viewport 25 over range 100 -> 20% of track.
    let thumb_node = app.world().get::<Node>(sb.thumb).unwrap();
    match thumb_node.height {
        Val::Percent(p) => assert!((p - 20.0).abs() < 1.0, "thumb 20%, got {p}"),
        other => panic!("expected percent height, got {other:?}"),
    }

    // Line-button nudge: SmallChange steps and clamps.
    let children: Vec<Entity> = app.world().get::<Children>(bar).unwrap().iter().collect();
    assert_eq!(children.len(), 3, "dec, track, inc");
    for _ in 0..20 {
        // 20 x 5 = 100: clamps at Maximum.
        bevy_pf::instantiate::scroll_bar_nudge(app.world_mut(), bar, 1.0);
    }
    assert_eq!(
        app.world()
            .get::<bevy::ui_widgets::SliderValue>(bar)
            .unwrap()
            .0,
        100.0,
        "nudge clamps at Maximum"
    );

    // TwoWay write-back into the view-model.
    app.update();
    assert_eq!(
        vm.read_path("pos").and_then(|v| v.as_f64()),
        Some(100.0),
        "scrollbar value wrote back"
    );

    // Source change repositions.
    vm.update(|m: &mut ScrollVm| m.pos = 0.0);
    app.update();
    assert_eq!(
        app.world()
            .get::<bevy::ui_widgets::SliderValue>(bar)
            .unwrap()
            .0,
        0.0
    );
}

#[derive(bevy::reflect::Reflect, Default)]
struct ScrollVm {
    pos: f64,
}