waft-winui 0.8.2

WinUI 3 controls and Fluent styling for the Waft UI framework
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
516
517
//! `CommandBar`'s `ContentRoot` grid and `CommandBar::MeasureOverrideForDynamicOverflow`: the `ContentControl`, the `PrimaryItemsControl` row of primary commands against the right, and the `MoreButton` in its own column, with the primary commands that do not fit moved into the overflow as the bar measures.
//!
//! WinUI moves commands between collections during measure and measures again until layout settles; here every primary command stays a child of the panel and the panel runs the same measure, then [`DynamicOverflow::measure`], again in one layout until nothing moves, showing the commands still in the bar. The commands it moved are laid out but neither painted nor hit, and the bar's state is told after the layout to rebuild them into the overflow. A later editor must keep the panel the only writer of the model during layout, and the state its only reader until then.

use super::dynamic_overflow::{DynamicOverflow, OverflowMeasure};
use super::layout_root::AppBarMetrics;
use std::any::{Any, TypeId};
use std::cell::RefCell;
use std::fmt;
use std::rc::Rc;
use waft_embedder::{Matrix4, Offset, Size};
use waft_foundation::App;
use waft_rendering::*;
use waft_widgets::*;

/// How the `MoreButton` is shown: `EffectiveOverflowButtonVisibility` from what the state knows when it builds, and the overflow's contents, which only the layout knows.
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct MoreButtonVisibility {
    /// Shown whatever the overflow holds.
    pub(crate) shown: bool,
    /// `OverflowButtonVisibility` is `Auto`: shown while the overflow holds anything.
    pub(crate) when_overflowing: bool,
}

/// What the panel lays out by.
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct ContentRootLayout {
    /// `DynamicOverflowStates`: `DynamicOverflowEnabled` swaps the columns' widths, so the primary commands get what the content leaves.
    pub(crate) dynamic_overflow_enabled: bool,
    pub(crate) more_button: MoreButtonVisibility,
    /// `AppBarMoreButtonColumnMinWidth`.
    pub(crate) more_button_column_min_width: f64,
    /// `ContentRoot.MinHeight` and `PrimaryItemsControl.MinHeight`: `AppBarThemeCompactHeight`.
    pub(crate) min_height: f64,
    /// The `MoreButton`'s `VerticalAlignment` is `Stretch`, in the open states and their transitions.
    pub(crate) more_button_stretches: bool,
    /// `FlowDirection` is right to left: the grid's columns run from the right.
    pub(crate) rtl: bool,
}

/// `ContentRoot`'s children: `ContentControl`, every primary command, `MoreButton`.
pub(crate) struct ContentRoot {
    pub(crate) layout: ContentRootLayout,
    pub(crate) overflow: Rc<RefCell<DynamicOverflow>>,
    pub(crate) metrics: Rc<AppBarMetrics>,
    /// Told after a layout that moved commands or raised `DynamicOverflowItemsChanging`.
    pub(crate) overflow_changed: Rc<dyn Fn(&mut App)>,
    pub(crate) children: Vec<WidgetRef>,
}

impl fmt::Debug for ContentRoot {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ContentRoot")
            .field("layout", &self.layout)
            .field("children", &self.children.len())
            .finish_non_exhaustive()
    }
}

impl RenderObjectWidget for ContentRoot {
    type RenderObject = RenderContentRoot;

    fn create_render_object(&self, app: &mut App, _context: BuildContext) -> AnyRenderObject {
        RenderHandle::new_box(
            app,
            RenderContentRoot {
                render_object: RenderObjectData::new(),
                render_box: RenderBoxData::new(),
                container: ContainerRenderObjectData::new(),
                layout: self.layout,
                overflow: self.overflow.clone(),
                metrics: self.metrics.clone(),
                overflow_changed: self.overflow_changed.clone(),
                shown_more_button: false,
            },
        )
        .as_object()
    }

    fn update_render_object(
        &self,
        app: &mut App,
        _context: BuildContext,
        render: RenderHandle<RenderContentRoot>,
    ) {
        let panel = render.get_mut(app);
        panel.overflow = self.overflow.clone();
        panel.metrics = self.metrics.clone();
        panel.overflow_changed = self.overflow_changed.clone();
        if panel.layout != self.layout {
            panel.layout = self.layout;
        }
        // The model may have been reset with new commands; the panel measures again.
        render.as_object().mark_needs_layout(app);
    }
}

impl MultiChildRenderObjectWidget for ContentRoot {
    fn children(&self) -> &[WidgetRef] {
        &self.children
    }
}

#[derive(Debug, Default)]
pub(crate) struct ContentRootParentData {
    box_parent_data: BoxParentData,
    container_parent_data: ContainerParentData<AnyRenderBox>,
}

impl ParentData for ContentRootParentData {
    fn detach(&mut self) {
        ContainerParentDataMixin::detach(self);
    }

    fn provide(&self, id: TypeId) -> Option<&dyn Any> {
        if id == TypeId::of::<ContentRootParentData>() {
            return Some(self);
        }
        self.box_parent_data.provide(id)
    }

    fn provide_mut(&mut self, id: TypeId) -> Option<&mut dyn Any> {
        if id == TypeId::of::<ContentRootParentData>() {
            return Some(self);
        }
        self.box_parent_data.provide_mut(id)
    }
}

impl ContainerParentDataMixin for ContentRootParentData {
    type ChildType = AnyRenderBox;

    fn container_parent_data(&self) -> &ContainerParentData<AnyRenderBox> {
        &self.container_parent_data
    }

    fn container_parent_data_mut(&mut self) -> &mut ContainerParentData<AnyRenderBox> {
        &mut self.container_parent_data
    }
}

impl ContainerBoxParentData for ContentRootParentData {
    fn box_parent_data(&self) -> &BoxParentData {
        &self.box_parent_data
    }

    fn box_parent_data_mut(&mut self) -> &mut BoxParentData {
        &mut self.box_parent_data
    }
}

impl fmt::Display for ContentRootParentData {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.box_parent_data)
    }
}

/// The sizes one measure of the panel found.
struct Measure {
    more_button_shown: bool,
    more_button: Size,
    /// The `MoreButton`'s column: `Auto` with its minimum width.
    more_column: f64,
    content: Size,
    /// Each primary command's desired size.
    commands: Vec<Size>,
    /// The width of the commands still in the bar.
    commands_width: f64,
    commands_height: f64,
}

/// The render object behind [`ContentRoot`].
pub(crate) struct RenderContentRoot {
    render_object: RenderObjectData,
    render_box: RenderBoxData,
    container: ContainerRenderObjectData<AnyRenderBox>,
    layout: ContentRootLayout,
    overflow: Rc<RefCell<DynamicOverflow>>,
    metrics: Rc<AppBarMetrics>,
    overflow_changed: Rc<dyn Fn(&mut App)>,
    shown_more_button: bool,
}

impl RenderContentRoot {
    fn children(self: RenderHandle<Self>, app: &App) -> Vec<AnyRenderBox> {
        let mut children = Vec::new();
        let mut child = self.first_child(app);
        while let Some(current) = child {
            children.push(current);
            child = self.child_after(app, current);
        }
        children
    }

    /// The children painted and hit, in paint order: the content, the commands still in the bar, the `MoreButton` when shown.
    fn shown(self: RenderHandle<Self>, app: &App) -> Vec<AnyRenderBox> {
        let children = self.children(app);
        let Some((content, rest)) = children.split_first() else {
            return Vec::new();
        };
        let Some((more, commands)) = rest.split_last() else {
            return vec![*content];
        };
        let overflow = self.get(app).overflow.clone();
        let overflow = overflow.borrow();
        let mut shown = vec![*content];
        shown.extend(
            overflow
                .dynamic_primary
                .iter()
                .filter_map(|index| commands.get(*index).copied()),
        );
        if self.get(app).shown_more_button {
            shown.push(*more);
        }
        shown
    }

    /// `Grid::MeasureOverride` of `ContentRoot` and the inner grid at `width`, the `Auto` columns first.
    fn measure(self: RenderHandle<Self>, app: &mut App, width: f64) -> Measure {
        let layout = self.get(app).layout;
        let children = self.children(app);
        let (content, more) = (children[0], children[children.len() - 1]);
        let commands = &children[1..children.len() - 1];
        let unbounded = BoxConstraints::new();
        let (dynamic_primary, overflowing) = {
            let overflow = self.get(app).overflow.borrow();
            (
                overflow.dynamic_primary.clone(),
                !overflow.dynamic_secondary.is_empty(),
            )
        };
        let more_button_shown =
            layout.more_button.shown || (layout.more_button.when_overflowing && overflowing);
        more.layout(app, unbounded, true);
        let more_button = if more_button_shown {
            more.size(app)
        } else {
            Size::ZERO
        };
        let more_column = more_button.width().max(layout.more_button_column_min_width);
        // The `StackPanel` measures each command without a width limit.
        let mut sizes = Vec::with_capacity(commands.len());
        for command in commands {
            command.layout(app, unbounded, true);
            sizes.push(command.size(app));
        }
        let commands_width: f64 = dynamic_primary.iter().map(|i| sizes[*i].width()).sum();
        let commands_height = dynamic_primary
            .iter()
            .map(|i| sizes[*i].height())
            .fold(layout.min_height, f64::max);
        let content_size = if layout.dynamic_overflow_enabled || !width.is_finite() {
            content.layout(app, unbounded, true);
            content.size(app)
        } else {
            // `ContentControlColumnDefinition` is star: the content gets what the commands leave.
            let room = (width - more_column - commands_width).max(0.0);
            content.layout(app, BoxConstraints::new().max_width(room), true);
            content.size(app)
        };
        Measure {
            more_button_shown,
            more_button,
            more_column,
            content: content_size,
            commands: sizes,
            commands_width,
            commands_height,
        }
    }

    /// The widths `MeasureOverrideForDynamicOverflow` compares for one measure.
    fn overflow_measure(&self, measure: &Measure, width: f64) -> OverflowMeasure {
        let primary_column = (width - measure.more_column - measure.content.width()).max(0.0);
        OverflowMeasure {
            available: width,
            // `DesiredSize` is at most the available size.
            available_primary: measure.commands_width.min(primary_column),
            content_root_desired: measure.content.width()
                + measure.commands_width
                + measure.more_column,
            primary_desired: measure.commands_width,
        }
    }
}

impl ContainerRenderObjectMixin for RenderContentRoot {
    type ChildType = AnyRenderBox;
    type ParentDataType = ContentRootParentData;

    fn container_data(
        self: RenderHandle<Self>,
        app: &App,
    ) -> &ContainerRenderObjectData<AnyRenderBox> {
        &self.get(app).container
    }

    fn container_data_mut(
        self: RenderHandle<Self>,
        app: &mut App,
    ) -> &mut ContainerRenderObjectData<AnyRenderBox> {
        &mut self.get_mut(app).container
    }
}

impl RenderBoxContainerDefaultsMixin for RenderContentRoot {}

impl RenderObject for RenderContentRoot {
    waft_rendering::render_object_accessors!();

    fn perform_layout(self: RenderHandle<Self>, app: &mut App) {
        let constraints = self.constraints(app);
        let width = constraints.max_width;
        let layout = self.get(app).layout;
        let overflow = self.get(app).overflow.clone();
        let mut measure = self.measure(app, width);
        let mut moved = false;
        if layout.dynamic_overflow_enabled && width.is_finite() {
            // WinUI measures again after each move until the layout settles.
            for _ in 0..measure.commands.len() + 3 {
                let widths: Vec<f64> = measure.commands.iter().map(|size| size.width()).collect();
                let changed = overflow
                    .borrow_mut()
                    .measure(self.get(app).overflow_measure(&measure, width), &|index| {
                        widths[index]
                    });
                if !changed {
                    break;
                }
                moved = true;
                measure = self.measure(app, width);
            }
        }
        self.get_mut(app).shown_more_button = measure.more_button_shown;

        // ArrangeOverride
        let children = self.children(app);
        let (content, more) = (children[0], children[children.len() - 1]);
        let commands = &children[1..children.len() - 1];
        let full_width = if width.is_finite() {
            width
        } else {
            measure.content.width() + measure.commands_width + measure.more_column
        };
        let height = measure
            .content
            .height()
            .max(measure.commands_height)
            .max(measure.more_button.height())
            .max(layout.min_height);
        let size = constraints.constrain(Size::new(full_width, height));
        let first_column = (size.width() - measure.more_column).max(0.0);
        let rtl = layout.rtl;
        // A child at `offset` in left-to-right columns; right to left, its mirror image.
        let set_offset = |app: &mut App, child: AnyRenderBox, offset: Offset| {
            let offset = if rtl {
                Offset::new(
                    size.width() - offset.dx() - child.size(app).width(),
                    offset.dy(),
                )
            } else {
                offset
            };
            child
                .parent_data_of_mut::<ContentRootParentData>(app)
                .set_offset(offset);
        };
        // ContentControl: `HorizontalContentAlignment` Left, `VerticalContentAlignment` Top.
        set_offset(app, content, Offset::ZERO);
        // PrimaryItemsControl: `HorizontalAlignment` Right in its column.
        let commands_column_start = if layout.dynamic_overflow_enabled {
            measure.content.width()
        } else {
            (first_column - measure.commands_width).max(0.0)
        };
        let commands_column = (first_column - commands_column_start).max(0.0);
        let mut x = commands_column_start + (commands_column - measure.commands_width).max(0.0);
        let dynamic_primary = overflow.borrow().dynamic_primary.clone();
        let stretches: Vec<bool> = overflow.borrow().separators();
        for index in dynamic_primary {
            let command = commands[index];
            // A separator stretches to the row; a button keeps its `VerticalAlignment` of Top.
            if stretches[index] {
                command.layout(
                    app,
                    BoxConstraints::tight(Size::new(measure.commands[index].width(), height)),
                    true,
                );
            }
            set_offset(app, command, Offset::new(x, 0.0));
            x += measure.commands[index].width();
        }
        let metrics = self.get(app).metrics.clone();
        if measure.more_button_shown {
            let more_height = if layout.more_button_stretches {
                more.layout(
                    app,
                    BoxConstraints::tight(Size::new(measure.more_button.width(), height)),
                    true,
                );
                height
            } else {
                measure.more_button.height()
            };
            set_offset(app, more, Offset::new(first_column, 0.0));
            let origin = more
                .as_object()
                .parent_data_of::<ContentRootParentData>(app)
                .offset();
            metrics.more_button.set(Some(
                origin & Size::new(measure.more_button.width(), more_height),
            ));
        } else {
            metrics.more_button.set(None);
        }
        metrics.more_button_shown.set(measure.more_button_shown);
        self.set_size(app, size);
        if moved || !overflow.borrow().raised.is_empty() {
            let changed = self.get(app).overflow_changed.clone();
            changed(app);
        }
    }

    fn visit_children(
        self: RenderHandle<Self>,
        app: &App,
        visitor: &mut dyn FnMut(AnyRenderObject),
    ) {
        ContainerRenderObjectMixin::visit_children(self, app, visitor)
    }

    fn did_attach(
        self: RenderHandle<Self>,
        app: &mut App,
        owner: waft_foundation::Handle<PipelineOwner>,
    ) {
        ContainerRenderObjectMixin::did_attach(self, app, owner);
    }

    fn did_detach(self: RenderHandle<Self>, app: &mut App) {
        ContainerRenderObjectMixin::did_detach(self, app);
    }

    fn redepth_children(self: RenderHandle<Self>, app: &mut App) {
        ContainerRenderObjectMixin::redepth_children(self, app)
    }

    fn paint(
        self: RenderHandle<Self>,
        app: &mut App,
        context: &mut PaintingContext,
        offset: Offset,
    ) {
        for child in self.shown(app) {
            let child_offset = child
                .as_object()
                .parent_data_of::<ContentRootParentData>(app)
                .offset();
            context.paint_child(app, child.as_object(), offset + child_offset);
        }
    }
}

impl RenderBox for RenderContentRoot {
    waft_rendering::render_box_accessors!();

    fn setup_parent_data(self: RenderHandle<Self>, app: &mut App, child: AnyRenderObject) {
        if !child.parent_data_is::<ContentRootParentData>(app) {
            child.set_parent_data(app, ContentRootParentData::default());
        }
    }

    fn apply_paint_transform(
        self: RenderHandle<Self>,
        app: &App,
        child: AnyRenderObject,
        transform: &mut Matrix4,
    ) {
        let offset = child.parent_data_of::<ContentRootParentData>(app).offset();
        *transform = transform.then(&Matrix4::translation(
            offset.dx() as f32,
            offset.dy() as f32,
        ));
    }

    fn compute_dry_layout(
        self: RenderHandle<Self>,
        _app: &mut App,
        constraints: BoxConstraints,
    ) -> Size {
        constraints.smallest()
    }

    fn hit_test_children(
        self: RenderHandle<Self>,
        app: &mut App,
        result: &mut BoxHitTestResult<'_>,
        position: Offset,
    ) -> bool {
        for child in self.shown(app).into_iter().rev() {
            let child_offset = child
                .as_object()
                .parent_data_of::<ContentRootParentData>(app)
                .offset();
            let hit = result.add_with_paint_offset(
                Some(child_offset),
                position,
                |result, transformed| child.hit_test(app, result, transformed),
            );
            if hit {
                return true;
            }
        }
        false
    }
}