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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
//! XAML `AppBarButton` with the `DefaultAppBarButtonStyle` of `AppBarButton_themeresources.xaml`, and `AppBarButton_Partial.cpp` with `AppBarButtonHelpers.h`.
//!
//! The template is `Root` holding `AppBarButtonInnerBorder`, the focus target with an 83 ms `BrushTransition` on its background, under `ContentRoot`, a grid of three columns and two rows: `ContentViewbox` (the `Icon` scaled to `AppBarButtonContentHeight`), `TextLabel` below it, `OverflowTextLabel`, `KeyboardAcceleratorTextLabel` and `SubItemChevronPanel`. `ApplicationViewStates` moves and hides those parts by label position and overflow (see [`ApplicationViewState`]); `CommonStates` swaps the brushes, with its `Overflow*` states only for an overflow menu that has icons.

use super::element::{
    ApplicationViewState, CommandBarLabelPosition, ElementHost, ElementPresentation,
};
use crate::{
    Brush, ColorTransition, ColumnDefinition, CommonState, CommonStates, ControlBorder,
    ControlStates, FluentSymbol, FocusVisual, FontIcon, Grid, GridCell, GridLength, InputDevice,
    Margin, RowDefinition, TextBlockStyle, ThemeResources, ToolTip, ToolTipService,
    control_text_style,
};
use crate::{Flyout, FlyoutPlacementMode, FlyoutShowOptions, FlyoutTarget};
use std::{fmt, rc::Rc};
use waft_embedder::{Color, FontWeight, TextAlign};
use waft_foundation::{App, Listener, Ref};
use waft_painting::{AlignmentGeometry, EdgeInsetsGeometry, TextStyle};
use waft_rendering::BoxConstraints;
use waft_widgets::*;

/// The `FocusVisualMargin` the style sets.
pub(crate) const APP_BAR_BUTTON_FOCUS_VISUAL_MARGIN: [f64; 4] = [-3.0; 4];
/// The style's `Width`, which `LabelOnRight` and the overflow style clear.
pub(crate) const APP_BAR_BUTTON_WIDTH: f64 = 68.0;

/// XAML `AppBarButton`: a command with an icon and a label, for a `CommandBar`.
#[derive(Clone)]
pub struct AppBarButton {
    /// `Label`: the text under or beside the icon, and the item's text in the overflow.
    pub label: String,
    /// `Click`.
    pub click: Listener,
    /// `Icon`: the glyph or image shown, scaled to the button's icon height.
    pub icon: Option<WidgetRef>,
    /// `LabelPosition`.
    pub label_position: CommandBarLabelPosition,
    /// `Flyout`: opened on click; the bar stays open.
    pub flyout: Option<Ref<Flyout>>,
    /// `IsEnabled`.
    pub is_enabled: bool,
    /// `DynamicOverflowOrder`: when the button moves to the overflow, lowest first; 0 moves with the rightmost commands.
    pub dynamic_overflow_order: i32,
    /// `KeyboardAcceleratorTextOverride`: shown beside the label in the overflow, and in the tooltip in the bar.
    pub keyboard_accelerator_text_override: String,
    pub(crate) presentation: ElementPresentation,
    pub(crate) host: Option<ElementHost>,
    /// Identity retained across parent rebuilds.
    pub key: Option<KeyRef>,
}

impl AppBarButton {
    /// Creates a button with a `Label` and a `Click` callback.
    pub fn new(label: impl Into<String>, click: Listener) -> Self {
        Self {
            label: label.into(),
            click,
            icon: None,
            label_position: CommandBarLabelPosition::Default,
            flyout: None,
            is_enabled: true,
            dynamic_overflow_order: 0,
            keyboard_accelerator_text_override: String::new(),
            presentation: ElementPresentation::default(),
            host: None,
            key: None,
        }
    }

    /// Sets the XAML `Icon`.
    pub fn icon<K>(mut self, icon: impl IntoWidget<K>) -> Self {
        self.icon = Some(icon.into_widget());
        self
    }

    /// Sets the XAML `LabelPosition`.
    pub fn label_position(mut self, position: CommandBarLabelPosition) -> Self {
        self.label_position = position;
        self
    }

    /// Sets the XAML `Flyout`.
    pub fn flyout(mut self, flyout: Ref<Flyout>) -> Self {
        self.flyout = Some(flyout);
        self
    }

    /// Sets the XAML `IsEnabled`.
    pub fn is_enabled(mut self, enabled: bool) -> Self {
        self.is_enabled = enabled;
        self
    }

    /// Sets the XAML `DynamicOverflowOrder`.
    pub fn dynamic_overflow_order(mut self, order: i32) -> Self {
        self.dynamic_overflow_order = order;
        self
    }

    /// Sets the XAML `KeyboardAcceleratorTextOverride`.
    pub fn keyboard_accelerator_text_override(mut self, text: impl Into<String>) -> Self {
        self.keyboard_accelerator_text_override = text.into();
        self
    }

    /// Sets the identity retained across rebuilds.
    pub fn key(mut self, key: KeyRef) -> Self {
        self.key = Some(key);
        self
    }

    /// `AppBarButton::OnClick`, then `Button::OnClick`'s `OpenAssociatedFlyout`: a button without a flyout closes its bar, `Click` is raised, and a flyout opens below the button or, in the overflow, beside it (`OpenSubMenu`).
    fn on_click(&self, target: BuildContext) -> Listener {
        let (click, flyout) = (self.click.clone(), self.flyout.clone());
        let host = self.host.clone();
        let in_overflow = self.presentation.use_overflow_style;
        Listener::new(move |app| {
            if flyout.is_none()
                && let Some(host) = &host
            {
                host.executed.call(app);
            }
            click.call(app);
            if let Some(flyout) = &flyout {
                if in_overflow {
                    let options = FlyoutShowOptions {
                        placement: Some(FlyoutPlacementMode::RightEdgeAlignedTop),
                        show_mode: app.get(flyout).show_mode,
                        ..Default::default()
                    };
                    flyout.show_at_with_options(app, target, options);
                } else {
                    flyout.show_at(app, target);
                }
            }
        })
    }
}

impl fmt::Debug for AppBarButton {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("AppBarButton")
            .field("label", &self.label)
            .field("label_position", &self.label_position)
            .field("is_enabled", &self.is_enabled)
            .field("dynamic_overflow_order", &self.dynamic_overflow_order)
            .finish_non_exhaustive()
    }
}

impl StatelessWidget for AppBarButton {
    fn key(&self) -> Option<&KeyRef> {
        self.key.as_ref()
    }

    fn build(&self, _app: &mut App, _context: BuildContext) -> WidgetRef {
        let widget = self.clone();
        FlyoutTarget::new(Builder::new(move |_app, target| {
            let template_widget = widget.clone();
            let mut states =
                CommonStates::new(widget.on_click(target), move |app, context, states| {
                    let sub_menu_open = template_widget
                        .flyout
                        .as_ref()
                        .is_some_and(|flyout| flyout.is_open(app));
                    template(app, context, &template_widget, states, sub_menu_open)
                })
                .is_enabled(widget.is_enabled);
            if let Some(host) = &widget.host {
                states = states.focus_node(host.focus_node.clone());
            }
            let button = match widget.flyout.clone() {
                // `OverflowSubMenuOpened` follows the flyout's opening and closing.
                Some(flyout) if widget.presentation.use_overflow_style => {
                    let states = states.clone();
                    ListenableBuilder::new(Rc::new(flyout), move |_, _, _| {
                        states.clone().into_widget()
                    })
                    .into_widget()
                }
                _ => states.into_widget(),
            };
            with_tool_tip(
                button,
                &widget.label,
                &widget.keyboard_accelerator_text_override,
                &widget.presentation,
            )
        }))
        .into_widget()
    }
}

/// `AppBarButtonHelpers::UpdateToolTip`: in the bar, a button with accelerator text gets the tooltip "Label (Accelerator)" (`KEYBOARD_ACCELERATOR_TEXT_TOOLTIP`).
pub(crate) fn with_tool_tip(
    button: WidgetRef,
    label: &str,
    accelerator: &str,
    presentation: &ElementPresentation,
) -> WidgetRef {
    if presentation.use_overflow_style || accelerator.is_empty() {
        return button;
    }
    ToolTipService::new(button, ToolTip::text(format!("{label} ({accelerator})"))).into_widget()
}

/// `CommonStates` of the template, the base `ButtonBase` states and the `Overflow*` states `ChangeVisualState` adds for an overflow menu with icons.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum AppBarButtonCommonState {
    Normal,
    PointerOver,
    Pressed,
    Disabled,
    OverflowNormal,
    OverflowPointerOver,
    OverflowPressed,
    OverflowSubMenuOpened,
}

impl AppBarButtonCommonState {
    /// `ChangeVisualState`: the base state, then, in an overflow with icons, the overflow state for an enabled button.
    fn of(presentation: &ElementPresentation, states: ControlStates, sub_menu_open: bool) -> Self {
        let base = match states.common {
            CommonState::Normal => Self::Normal,
            CommonState::PointerOver => Self::PointerOver,
            CommonState::Pressed => Self::Pressed,
            CommonState::Disabled => Self::Disabled,
        };
        if !(presentation.use_overflow_style && presentation.with_icons) {
            return base;
        }
        if sub_menu_open {
            Self::OverflowSubMenuOpened
        } else {
            match base {
                Self::Pressed => Self::OverflowPressed,
                Self::PointerOver => Self::OverflowPointerOver,
                Self::Disabled => Self::Disabled,
                _ => Self::OverflowNormal,
            }
        }
    }
}

/// The brushes one `CommonStates` state sets.
#[derive(Clone, Copy, Debug)]
struct AppBarButtonBrushes {
    background: Color,
    border: Color,
    foreground: Color,
    chevron: Color,
    accelerator: Color,
}

fn brushes(r: &ThemeResources, state: AppBarButtonCommonState) -> AppBarButtonBrushes {
    use AppBarButtonCommonState::*;
    let set = |background, border, foreground, chevron, accelerator| AppBarButtonBrushes {
        background,
        border,
        foreground,
        chevron,
        accelerator,
    };
    match state {
        // `Normal` and `OverflowNormal` set no brush: the style's own.
        Normal | OverflowNormal => set(
            r.app_bar_button_background,
            r.app_bar_button_border_brush,
            r.app_bar_button_foreground,
            r.app_bar_button_sub_item_chevron_foreground,
            r.app_bar_button_keyboard_accelerator_text_foreground,
        ),
        PointerOver | OverflowPointerOver => set(
            r.app_bar_button_background_pointer_over,
            r.app_bar_button_border_brush_pointer_over,
            r.app_bar_button_foreground_pointer_over,
            r.app_bar_button_sub_item_chevron_foreground_pointer_over,
            r.app_bar_button_keyboard_accelerator_text_foreground_pointer_over,
        ),
        Pressed | OverflowPressed => set(
            r.app_bar_button_background_pressed,
            r.app_bar_button_border_brush_pressed,
            r.app_bar_button_foreground_pressed,
            r.app_bar_button_sub_item_chevron_foreground_pressed,
            r.app_bar_button_keyboard_accelerator_text_foreground_pressed,
        ),
        Disabled => set(
            r.app_bar_button_background_disabled,
            r.app_bar_button_border_brush_disabled,
            r.app_bar_button_foreground_disabled,
            r.app_bar_button_sub_item_chevron_foreground_disabled,
            r.app_bar_button_keyboard_accelerator_text_foreground_disabled,
        ),
        OverflowSubMenuOpened => set(
            r.app_bar_button_background_sub_menu_opened,
            r.app_bar_button_border_brush_sub_menu_opened,
            r.app_bar_button_foreground_sub_menu_opened,
            r.app_bar_button_sub_item_chevron_foreground_sub_menu_opened,
            r.app_bar_button_keyboard_accelerator_text_foreground_sub_menu_opened,
        ),
    }
}

/// The `ControlTemplate` for one view state and set of common states.
fn template(
    app: &mut App,
    context: BuildContext,
    widget: &AppBarButton,
    states: ControlStates,
    sub_menu_open: bool,
) -> WidgetRef {
    let r = ThemeResources::of(app, context);
    let presentation = widget.presentation;
    let view = ApplicationViewState::of(&presentation, widget.label_position);
    let common = AppBarButtonCommonState::of(&presentation, states, sub_menu_open);
    let brushes = brushes(&r, common);
    let overflow_common = matches!(
        common,
        AppBarButtonCommonState::OverflowNormal
            | AppBarButtonCommonState::OverflowPointerOver
            | AppBarButtonCommonState::OverflowPressed
            | AppBarButtonCommonState::OverflowSubMenuOpened
    );
    let touch = presentation.input_mode == InputDevice::Touch;
    let label_style =
        |size: f64, color: Color| control_text_style(&r, size, FontWeight::NORMAL, color);
    let mut cells = Vec::new();

    // ContentViewbox > Content
    if !matches!(
        view,
        ApplicationViewState::Overflow | ApplicationViewState::OverflowWithToggleButtons
    ) {
        let icon = widget.icon.clone().map(|icon| {
            DefaultTextStyle::new(
                label_style(r.control_content_theme_font_size, brushes.foreground),
                icon,
            )
            .into_widget()
        });
        cells.push(GridCell::new(content_viewbox(&r, view, icon, false)).into_widget());
    }
    // TextLabel
    match view {
        ApplicationViewState::FullSize => cells.push(
            GridCell::new(Margin::new(
                r.app_bar_button_text_label_margin,
                Text::new(widget.label.clone())
                    .style(label_style(
                        r.app_bar_button_label_font_size,
                        brushes.foreground,
                    ))
                    .text_align(TextAlign::Center),
            ))
            .row(1)
            .into_widget(),
        ),
        ApplicationViewState::LabelOnRight => cells.push(
            GridCell::new(Margin::new(
                r.app_bar_button_text_label_on_right_margin,
                Text::new(widget.label.clone())
                    .style(label_style(
                        r.app_bar_button_label_font_size,
                        brushes.foreground,
                    ))
                    .text_align(TextAlign::Left),
            ))
            .column(1)
            .into_widget(),
        ),
        _ => {}
    }
    // OverflowTextLabel
    if view.is_overflow() {
        let margin = match view {
            ApplicationViewState::Overflow => [12.0, 0.0, 12.0, 0.0],
            ApplicationViewState::OverflowWithToggleButtonsAndMenuIcons => [76.0, 0.0, 12.0, 0.0],
            _ => [38.0, 0.0, 12.0, 0.0],
        };
        let padding = if touch {
            r.app_bar_button_overflow_text_touch_margin
        } else {
            r.app_bar_button_overflow_text_label_padding
        };
        cells.push(
            GridCell::new(overflow_text_label(
                &widget.label,
                label_style(r.control_content_theme_font_size, brushes.foreground),
                margin,
                padding,
            ))
            .into_widget(),
        );
    }
    // KeyboardAcceleratorTextLabel
    if let Some(cell) = keyboard_accelerator_text_label(
        &r,
        &presentation,
        &widget.keyboard_accelerator_text_override,
        brushes.accelerator,
    ) {
        cells.push(GridCell::new(cell).column(1).into_widget());
    }
    // SubItemChevronPanel: `HasFlyout` sets `AppBarButtonHasFlyoutChevronVisibility`, which a bar's resources collapse.
    if widget.flyout.is_some() && !presentation.in_command_bar {
        let chevron = if overflow_common {
            // OverflowSubItemChevron
            Margin::new(
                r.app_bar_button_secondary_sub_item_chevron_margin,
                Align::new()
                    .alignment(AlignmentGeometry::CENTER)
                    .height_factor(1.0)
                    .child(
                        FontIcon::symbol(FluentSymbol::ChevronRight)
                            .font_size(r.app_bar_button_secondary_sub_item_chevron_font_size)
                            .mirrored_when_right_to_left(true)
                            .foreground(r.app_bar_button_sub_item_chevron_foreground),
                    ),
            )
        } else {
            // SubItemChevron
            Margin::new(
                if view == ApplicationViewState::LabelOnRight {
                    r.app_bar_button_sub_item_chevron_label_on_right_margin
                } else {
                    r.app_bar_button_sub_item_chevron_margin
                },
                Align::new()
                    .alignment(AlignmentGeometry::TOP_CENTER)
                    .height_factor(1.0)
                    .child(
                        FontIcon::symbol(FluentSymbol::ChevronRight)
                            .font_size(r.app_bar_button_sub_item_chevron_font_size)
                            .mirrored_when_right_to_left(true)
                            .foreground(brushes.chevron),
                    ),
            )
        };
        cells.push(GridCell::new(chevron).column(2).into_widget());
    }

    let content_root =
        ConstrainedBox::new(BoxConstraints::new().min_height(content_root_min_height(&r, view)))
            .child(
                Grid::new()
                    .column_definitions([
                        ColumnDefinition::new(GridLength::STAR),
                        ColumnDefinition::new(GridLength::AUTO),
                        ColumnDefinition::new(GridLength::AUTO),
                    ])
                    .row_definitions([
                        RowDefinition::new(GridLength::AUTO),
                        RowDefinition::new(GridLength::AUTO),
                    ])
                    .children(cells),
            );
    let inner_border = inner_border(
        &r,
        Brush::Solid(brushes.background),
        Brush::Solid(brushes.border),
        0.0,
        crate::BackgroundSizing::InnerBorderEdge,
        states.focused,
    );
    root(
        view,
        Margin::new(inner_border_margin(&r, view), inner_border),
        content_root.into_widget(),
    )
}

/// `AppBarButtonInnerBorder`'s `Margin` in each view state, shared by `AppBarToggleButtonInnerBorder`.
pub(crate) fn inner_border_margin(r: &ThemeResources, view: ApplicationViewState) -> [f64; 4] {
    match view {
        ApplicationViewState::Compact => r.app_bar_button_inner_border_compact_margin,
        view if view.is_overflow() => r.app_bar_button_inner_border_overflow_margin,
        _ => r.app_bar_button_inner_border_margin,
    }
}

/// `ContentRoot.MinHeight` in each view state.
pub(crate) fn content_root_min_height(r: &ThemeResources, view: ApplicationViewState) -> f64 {
    match view {
        ApplicationViewState::LabelOnRight | ApplicationViewState::LabelCollapsed => {
            r.app_bar_theme_compact_height
        }
        view if view.is_overflow() => 0.0,
        _ => r.app_bar_theme_min_height,
    }
}

/// The inner border, the template's focus target: `Background` with its 83 ms `BrushTransition`, `BorderBrush`, `CornerRadius` and the system focus visual at `FocusVisualMargin`.
pub(crate) fn inner_border(
    r: &ThemeResources,
    background: Brush,
    border: Brush,
    border_thickness: f64,
    background_sizing: crate::BackgroundSizing,
    focused: bool,
) -> WidgetRef {
    let corner_radius = r.control_corner_radius;
    let background_color = background.representative_color();
    let presenter = ColorTransition::new(
        background_color,
        r.control_faster_animation_duration,
        move |_app, background| {
            ControlBorder::new(Brush::Solid(background), border)
                .border_thickness(border_thickness)
                .corner_radius_corners(corner_radius)
                .background_sizing(background_sizing)
                .into_widget()
        },
    );
    FocusVisual::new(presenter, r.theme)
        .visible(focused)
        .margin(APP_BAR_BUTTON_FOCUS_VISUAL_MARGIN)
        .corner_radius(corner_radius[0])
        .into_widget()
}

/// `Root`: the inner border under `ContentRoot`, at the style's `Width` of 68 except in `LabelOnRight` and the overflow, where the width is the content's or the menu's.
pub(crate) fn root(
    view: ApplicationViewState,
    inner_border: Margin,
    content_root: WidgetRef,
) -> WidgetRef {
    let root = Grid::new().children([inner_border.into_widget(), content_root]);
    if view == ApplicationViewState::LabelOnRight || view.is_overflow() {
        root.into_widget()
    } else {
        SizedBox::new()
            .width(APP_BAR_BUTTON_WIDTH)
            .child(root)
            .into_widget()
    }
}

/// `ContentViewbox`: the icon scaled to `AppBarButtonContentHeight`, across the column, or a 16 pixel square at the start of an overflow item with icons (`max` the toggle button's `MaxWidth` and `MaxHeight` rather than the button's `Width` and `Height`).
pub(crate) fn content_viewbox(
    r: &ThemeResources,
    view: ApplicationViewState,
    icon: Option<WidgetRef>,
    toggle: bool,
) -> WidgetRef {
    let scaled = |icon: Option<WidgetRef>| match icon {
        Some(icon) => FittedBox::new().child(icon).into_widget(),
        None => SizedBox::shrink().into_widget(),
    };
    match view {
        ApplicationViewState::OverflowWithMenuIcons
        | ApplicationViewState::OverflowWithToggleButtonsAndMenuIcons => {
            let margin = if view == ApplicationViewState::OverflowWithMenuIcons && !toggle {
                [12.0, 0.0, 12.0, 0.0]
            } else {
                [38.0, 0.0, 12.0, 0.0]
            };
            let square = if toggle {
                ConstrainedBox::new(BoxConstraints::new().max_width(16.0).max_height(16.0))
                    .child(scaled(icon))
                    .into_widget()
            } else {
                SizedBox::new()
                    .width(16.0)
                    .height(16.0)
                    .child(scaled(icon))
                    .into_widget()
            };
            Margin::new(
                margin,
                Align::new()
                    .alignment(AlignmentGeometry::CENTER_LEFT)
                    .height_factor(1.0)
                    .child(square),
            )
            .into_widget()
        }
        _ => {
            let margin = if view == ApplicationViewState::LabelOnRight {
                r.app_bar_button_content_viewbox_margin
            } else {
                r.app_bar_button_content_viewbox_collapsed_margin
            };
            Margin::new(
                margin,
                SizedBox::new()
                    .height(r.app_bar_button_content_height)
                    .child(scaled(icon)),
            )
            .into_widget()
        }
    }
}

/// `OverflowTextLabel`: the label on one clipped line, centred down the item, inside its margin and padding.
pub(crate) fn overflow_text_label(
    label: &str,
    style: TextStyle,
    margin: [f64; 4],
    padding: [f64; 4],
) -> WidgetRef {
    let [left, top, right, bottom] = padding;
    Margin::new(
        margin,
        Align::new()
            .alignment(AlignmentGeometry::CENTER_LEFT)
            .height_factor(1.0)
            .child(
                Padding::new(EdgeInsetsGeometry::from_ltrb(left, top, right, bottom)).child(
                    Text::new(label.to_string())
                        .style(style)
                        .max_lines(1)
                        .soft_wrap(false)
                        .text_align(TextAlign::Left),
                ),
            ),
    )
    .into_widget()
}

/// `KeyboardAcceleratorTextLabel`, shown in `KeyboardAcceleratorTextVisible`: an overflow item of a menu with accelerator text, at least `KeyboardAcceleratorTextMinWidth` wide, at the end of the item.
pub(crate) fn keyboard_accelerator_text_label(
    r: &ThemeResources,
    presentation: &ElementPresentation,
    text: &str,
    color: Color,
) -> Option<WidgetRef> {
    if !(presentation.with_keyboard_accelerator_text && presentation.use_overflow_style) {
        return None;
    }
    Some(
        Margin::new(
            [24.0, 0.0, 12.0, 0.0],
            Align::new()
                .alignment(AlignmentGeometry::CENTER_RIGHT)
                .height_factor(1.0)
                .width_factor(1.0)
                .child(
                    ConstrainedBox::new(
                        BoxConstraints::new()
                            .min_width(presentation.keyboard_accelerator_text_min_width),
                    )
                    .child(
                        Text::new(text.to_string())
                            .style(TextBlockStyle::Caption.text_style(r, color))
                            .max_lines(1)
                            .soft_wrap(false),
                    ),
                ),
        )
        .into_widget(),
    )
}