bevy_pf 0.2.5

A XAML / WPF-like UI framework for Bevy: XAML in macros or files, styling with resources, and the common WPF control set.
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
//! `ItemsSource` + `DataTemplate`: data-driven item generation for
//! `ListBox`, `ItemsControl`, and `ComboBox`.
//!
//! Templates are stored as *unexpanded* XAML subtrees and instantiated per
//! item through the normal engine, with each item's `DataContext` scoped to
//! `<items-path>[i]` on the shared view-model — so `{Binding name}` inside a
//! template reads `items[i].name`, and TwoWay bindings write back into the
//! list element.

use std::sync::Arc;

use bevy::prelude::*;

use crate::binding::{DataContext, find_context};
use crate::components::{PfListBox, PfListBoxItem};

/// Which control hosts the generated items (decides wrapper chrome).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ItemsHostKind {
    ListBox,
    ItemsControl,
    ComboBox,
    DataGrid,
}

/// An `ItemsSource="{Binding path}"` on an items control.
#[derive(Component, Debug, Clone)]
pub struct PfItemsSource {
    pub path: String,
    pub template: Option<Arc<bevy_pf_xaml::XamlNode>>,
    /// Lexical resource scope where the template was declared, so
    /// `{StaticResource}` inside a `DataTemplate` resolves page resources.
    pub scopes: Option<Arc<crate::resources::ResourceScopes>>,
    pub display_member: Option<String>,
    /// `ItemContainerStyle`: applied to each generated item container.
    pub container_style: Option<Arc<crate::resources::PfStyle>>,
    pub kind: ItemsHostKind,
    pub seen_version: u64,
}

/// `Content="{Binding vm}"` on a ContentControl: content regenerated from
/// a template (explicit `ContentTemplate`, else DataType-implicit from the
/// bound value's type ident), falling back to display text for scalars.
#[derive(Component, Debug, Clone)]
pub struct PfContentSource {
    pub path: String,
    /// Explicit `ContentTemplate=` (wins over DataType selection).
    pub template: Option<Arc<bevy_pf_xaml::XamlNode>>,
    pub scopes: Option<Arc<crate::resources::ResourceScopes>>,
    pub seen_version: u64,
}

/// Rebuild ContentControl content when the bound model changes.
pub(crate) fn sync_content_sources(world: &mut World) {
    let mut query = world.query::<(Entity, &PfContentSource)>();
    let hosts: Vec<(Entity, PfContentSource)> =
        query.iter(world).map(|(e, s)| (e, s.clone())).collect();

    for (entity, source) in hosts {
        let Some(ctx) = find_context(world, entity) else {
            continue;
        };
        let version = ctx.version();
        if version == source.seen_version {
            continue;
        }
        if let Some(mut s) = world.get_mut::<PfContentSource>(entity) {
            s.seen_version = version;
        }

        // Explicit ContentTemplate, else implicit by the value's type ident.
        let template = source.template.clone().or_else(|| {
            let ident = ctx.type_ident(&source.path)?;
            match source
                .scopes
                .as_deref()?
                .lookup(&crate::resources::ResourceKey::Type(ident))
            {
                Some(crate::resources::PfValue::Template(t)) => Some(t.clone()),
                _ => None,
            }
        });

        world.entity_mut(entity).despawn_children();
        match template {
            Some(template) => {
                let wrapper = world
                    .spawn((Node::default(), DataContext(ctx.at(&source.path))))
                    .id();
                world.entity_mut(entity).add_children(&[wrapper]);
                if let Err(e) = crate::instantiate::instantiate_template(
                    world,
                    wrapper,
                    &template,
                    source.scopes.as_deref(),
                ) {
                    warn!("bevy_pf: content template failed: {e}");
                }
            }
            None => {
                let text = ctx
                    .read_path(&source.path)
                    .map(|v| v.to_display())
                    .unwrap_or_default();
                let label = spawn_runtime_text(world, &text);
                world.entity_mut(entity).add_children(&[label]);
            }
        }
    }
}

/// The entity that actually receives generated item containers: the
/// `ItemsPanel` panel when one was declared, else `host` itself.
pub fn items_container(world: &World, host: Entity) -> Entity {
    world
        .get::<crate::components::PfItemsPanel>(host)
        .map(|p| p.panel)
        .unwrap_or(host)
}

/// Rebuild generated items when the source list's model changes.
///
/// v1 rebuilds all items on any model version bump; granular diffing arrives
/// with `ObservableList` change events (roadmap 2.2).
pub(crate) fn sync_items_sources(world: &mut World) {
    let mut query = world.query::<(Entity, &PfItemsSource)>();
    let hosts: Vec<(Entity, PfItemsSource)> =
        query.iter(world).map(|(e, s)| (e, s.clone())).collect();

    for (entity, source) in hosts {
        let Some(ctx) = find_context(world, entity) else {
            continue;
        };
        let version = ctx.version();
        if version == source.seen_version {
            continue;
        }
        // A version bump alone is not a reason to regenerate: a busy model
        // (per-frame scalar setters on the same context) would churn item
        // entities every frame — visible as flicker, wasted layout, and
        // clicks that never land because the pressed entity is gone by
        // release. Only a change overlapping the items path rebuilds.
        if !ctx.changed_since(source.seen_version, std::iter::once(source.path.as_str())) {
            if let Some(mut s) = world.get_mut::<PfItemsSource>(entity) {
                s.seen_version = version;
            }
            continue;
        }
        if let Some(mut s) = world.get_mut::<PfItemsSource>(entity) {
            s.seen_version = version;
        }

        let Some(len) = ctx.list_len(&source.path) else {
            warn!(
                "bevy_pf: ItemsSource path `{}` is not a list on the data context",
                source.path
            );
            continue;
        };

        // The container whose children are the generated items.
        let container = match source.kind {
            ItemsHostKind::ComboBox => match world.get::<crate::components::PfComboBox>(entity) {
                Some(combo) => combo.popup,
                None => continue,
            },
            ItemsHostKind::DataGrid => match world.get::<crate::components::PfDataGrid>(entity) {
                Some(grid) => grid.rows_host,
                None => continue,
            },
            // ListBox/ItemsControl: an ItemsPanel redirects generation.
            _ => items_container(world, entity),
        };
        // The rebuild despawns every row, so a ListBox's `selected` entity is
        // about to dangle. Remember WHERE the selection was; a SelectedItem
        // binding will re-locate the item by value on the next apply, and
        // this keeps the position meanwhile so the row it prefers is the one
        // the user actually had.
        let previous_selection = world
            .get::<crate::components::PfListBox>(entity)
            .and_then(|l| l.selected)
            .and_then(|sel| {
                world
                    .get::<Children>(container)
                    .and_then(|c| c.iter().position(|child| child == sel))
            });
        world.entity_mut(container).despawn_children();

        let mut items = Vec::with_capacity(len);
        for index in 0..len {
            let item_ctx = ctx.at(format!("{}[{index}]", source.path));

            // DataGrid rows are fully generated here (template columns).
            if source.kind == ItemsHostKind::DataGrid {
                let Some(grid) = world.get::<crate::components::PfDataGrid>(entity).cloned() else {
                    continue;
                };
                let template: Vec<bevy::ui::RepeatedGridTrack> = grid
                    .columns
                    .iter()
                    .map(|c| match c.width {
                        bevy_pf_xaml::value::GridLength::Auto => bevy::ui::GridTrack::fr(1.0),
                        other => crate::convert::grid_track(other),
                    })
                    .collect();
                let row = world
                    .spawn((
                        Node {
                            display: Display::Grid,
                            grid_template_columns: template,
                            grid_template_rows: vec![bevy::ui::GridTrack::auto()],
                            ..Default::default()
                        },
                        PfListBoxItem,
                        Interaction::default(),
                        BackgroundColor(Color::NONE),
                    ))
                    .id();
                for (col_index, column) in grid.columns.iter().enumerate() {
                    let cell = if let Some(template) = &column.template {
                        // GridViewColumn.CellTemplate: expand with the row's
                        // scoped DataContext.
                        let wrapper = world
                            .spawn((Node::default(), DataContext(item_ctx.clone())))
                            .id();
                        if let Err(e) = crate::instantiate::instantiate_template(
                            world,
                            wrapper,
                            template,
                            source.scopes.as_deref(),
                        ) {
                            warn!("bevy_pf: cell template failed: {e}");
                        }
                        wrapper
                    } else {
                        let text = item_ctx
                            .read_path(&column.path)
                            .map(|v| v.to_display())
                            .unwrap_or_default();
                        spawn_runtime_text(world, &text)
                    };
                    if let Some(mut n) = world.get_mut::<Node>(cell) {
                        n.grid_column =
                            bevy::ui::GridPlacement::start_span(col_index as i16 + 1, 1);
                        n.grid_row = bevy::ui::GridPlacement::start_span(1, 1);
                        n.padding = UiRect::axes(Val::Px(8.0), Val::Px(3.0));
                    }
                    world.entity_mut(row).add_children(&[cell]);
                }
                let rows_host = container;
                world.entity_mut(row).observe(
                    move |click: On<Pointer<Click>>, mut lists: Query<&mut PfListBox>| {
                        let this = click.entity;
                        if let Ok(mut state) = lists.get_mut(rows_host) {
                            state.selected = Some(this);
                        }
                    },
                );
                items.push(row);
                continue;
            }

            // Wrapper per host kind.
            //
            // Item containers stack their content in a COLUMN, which is what
            // makes a template root fill the container's width: bevy_ui's
            // default cross-axis alignment stretches, and WPF's
            // `ContentPresenter` fills its container horizontally the same way.
            // A row-direction container would put the template on the main axis,
            // where it shrink-wraps and every `*` Grid track inside it collapses.
            let wrapper = match source.kind {
                ItemsHostKind::DataGrid => unreachable!("handled above"),
                ItemsHostKind::ItemsControl => world
                    .spawn(Node {
                        flex_direction: FlexDirection::Column,
                        ..Default::default()
                    })
                    .id(),
                ItemsHostKind::ListBox => {
                    let wrapper = world
                        .spawn((
                            Node {
                                flex_direction: FlexDirection::Column,
                                padding: UiRect::axes(Val::Px(4.0), Val::Px(2.0)),
                                ..Default::default()
                            },
                            crate::components::PfElementKind("ListBoxItem".to_string()),
                            PfListBoxItem,
                            Interaction::default(),
                            BackgroundColor(Color::NONE),
                        ))
                        .id();
                    let list = entity;
                    world.entity_mut(wrapper).observe(
                        move |click: On<Pointer<Click>>, mut lists: Query<&mut PfListBox>| {
                            let this = click.entity;
                            if let Ok(mut state) = lists.get_mut(list) {
                                state.selected = Some(this);
                            }
                        },
                    );
                    wrapper
                }
                ItemsHostKind::ComboBox => {
                    let wrapper = world
                        .spawn((
                            Node {
                                padding: UiRect::axes(Val::Px(6.0), Val::Px(3.0)),
                                ..Default::default()
                            },
                            crate::components::PfComboItem {
                                combo: entity,
                                index,
                            },
                            Interaction::default(),
                            BackgroundColor(Color::NONE),
                        ))
                        .id();
                    let combo = entity;
                    world.entity_mut(wrapper).observe(
                        move |_click: On<Pointer<Click>>, mut commands: Commands| {
                            commands.queue(move |world: &mut World| {
                                crate::instantiate::select_combo_index(world, combo, index);
                            });
                        },
                    );
                    wrapper
                }
            };

            // Content: template expansion or display text.
            match &source.template {
                Some(template) => {
                    world
                        .entity_mut(wrapper)
                        .insert(DataContext(item_ctx.clone()));
                    if let Err(e) = crate::instantiate::instantiate_template(
                        world,
                        wrapper,
                        template,
                        source.scopes.as_deref(),
                    ) {
                        warn!("bevy_pf: item template failed: {e}");
                    }
                }
                None => {
                    let text = item_ctx
                        .read_path(source.display_member.as_deref().unwrap_or(""))
                        .map(|v| v.to_display())
                        .unwrap_or_default();
                    let label = spawn_runtime_text(world, &text);
                    world.entity_mut(wrapper).add_children(&[label]);
                }
            }
            // ItemContainerStyle lands on the container, after content so
            // font-flavored setters see the generated children.
            if let Some(style) = &source.container_style {
                crate::instantiate::apply_style_runtime(
                    world,
                    wrapper,
                    "ListBoxItem",
                    style,
                    source.scopes.as_deref(),
                );
            }
            items.push(wrapper);
        }

        // WPF gives each generated item container the full cross-axis extent of
        // a vertically-stacking items panel (`HorizontalContentAlignment`
        // defaults to `Stretch` on the container). Without this the wrapper
        // shrink-wraps its template, so every `*` Grid track *inside* an item
        // collapses to content width and a trailing flush-right button ends up
        // jammed against the label.
        //
        // Only for column panels: in a `WrapPanel` (a wrapping flex row) the
        // cross axis is height, and WPF measures a wrapped item to its natural
        // height, not the line height.
        if column_stacking(world, container) {
            for &item in &items {
                if let Some(mut node) = world.get_mut::<Node>(item) {
                    node.align_self = bevy::ui::AlignSelf::Stretch;
                }
            }
        }

        world.entity_mut(container).add_children(&items);

        // Re-point the selection at the row in the same position, or clear it
        // when the list shrank past it. Without this the ListBox holds a
        // despawned entity and reports "something is selected" forever.
        if let Some(mut list) = world.get_mut::<crate::components::PfListBox>(entity)
            && list.selected.is_some()
        {
            list.selected = previous_selection.and_then(|i| items.get(i).copied());
        }
        if let Some(mut combo) = world.get_mut::<crate::components::PfComboBox>(entity)
            && combo.selected.is_some_and(|i| i >= items.len())
        {
            combo.selected = None;
        }
    }
}

/// Whether an items panel stacks its children top-to-bottom, and so hands them
/// its full width.
fn column_stacking(world: &World, container: Entity) -> bool {
    world.get::<Node>(container).is_some_and(|node| {
        matches!(
            node.flex_direction,
            bevy::ui::FlexDirection::Column | bevy::ui::FlexDirection::ColumnReverse
        ) && node.flex_wrap == bevy::ui::FlexWrap::NoWrap
    })
}

/// Spawn a plain text node with the engine's default text style (used for
/// runtime-generated items, where no lexical font inheritance exists).
pub(crate) fn spawn_runtime_text(world: &mut World, text: &str) -> Entity {
    // Generated rows sit under the overlay root, so there is no control to
    // inherit Foreground from; the theme supplies the colour instead. This
    // was hardcoded to BLACK, which is right for the default light theme and
    // invisible on any dark one.
    let color = world
        .get_resource::<crate::components::PfControlTheme>()
        .map(|theme| theme.item_text)
        .unwrap_or(Color::BLACK);
    world
        .spawn((
            Node::default(),
            bevy::ui::widget::Text::new(text),
            bevy::text::TextFont {
                font_size: bevy::text::FontSize::Px(12.0),
                font: crate::fonts::default_font(),
                ..Default::default()
            },
            bevy::text::TextColor(color),
        ))
        .id()
}