Skip to main content

waft_winui/controls/command_bar/
element.rs

1//! `ICommandBarElement`, `ICommandBarLabeledElement` and `ICommandBarOverflowElement` (`CommandBar.idl`): what a `CommandBar` sets on the `AppBarButton`, `AppBarToggleButton` and `AppBarSeparator` it hosts, and the visual state each one picks from it (`AppBarButtonHelpers::ChangeCommonVisualStates`).
2//!
3//! An element is a value the application builds; the bar clones it into the primary row and, when it moves there, into the overflow, each copy with its own [`ElementPresentation`]. A later editor must keep the presentation and the [`ElementHost`] all the bar gives an element: an element reads nothing of the bar any other way.
4
5use super::{AppBarButton, AppBarSeparator, AppBarToggleButton};
6use crate::InputDevice;
7use waft_foundation::Listener;
8use waft_widgets::FocusNodeRef;
9
10/// XAML `CommandBarDefaultLabelPosition`: where the bar's buttons show their labels.
11#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
12pub enum CommandBarDefaultLabelPosition {
13    /// Under the icon, shown while the bar is open.
14    #[default]
15    Bottom,
16    /// To the right of the icon, always shown.
17    Right,
18    /// Not shown.
19    Collapsed,
20}
21
22/// XAML `CommandBarLabelPosition`: whether a button follows the bar's label position.
23#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
24pub enum CommandBarLabelPosition {
25    /// The bar's `DefaultLabelPosition`.
26    #[default]
27    Default,
28    /// No label, whatever the bar's position.
29    Collapsed,
30}
31
32/// An item of `PrimaryCommands` or `SecondaryCommands`: XAML's `ICommandBarElement`.
33#[derive(Clone, Debug)]
34pub enum CommandBarElement {
35    Button(AppBarButton),
36    ToggleButton(AppBarToggleButton),
37    Separator(AppBarSeparator),
38}
39
40impl From<AppBarButton> for CommandBarElement {
41    fn from(button: AppBarButton) -> Self {
42        CommandBarElement::Button(button)
43    }
44}
45
46impl From<AppBarToggleButton> for CommandBarElement {
47    fn from(button: AppBarToggleButton) -> Self {
48        CommandBarElement::ToggleButton(button)
49    }
50}
51
52impl From<AppBarSeparator> for CommandBarElement {
53    fn from(separator: AppBarSeparator) -> Self {
54        CommandBarElement::Separator(separator)
55    }
56}
57
58impl CommandBarElement {
59    /// `ICommandBarElement::DynamicOverflowOrder`.
60    pub fn dynamic_overflow_order(&self) -> i32 {
61        match self {
62            CommandBarElement::Button(button) => button.dynamic_overflow_order,
63            CommandBarElement::ToggleButton(button) => button.dynamic_overflow_order,
64            CommandBarElement::Separator(separator) => separator.dynamic_overflow_order,
65        }
66    }
67
68    pub(crate) fn is_separator(&self) -> bool {
69        matches!(self, CommandBarElement::Separator(_))
70    }
71
72    /// The `Label`, which a separator has none of.
73    pub(crate) fn label(&self) -> Option<&str> {
74        match self {
75            CommandBarElement::Button(button) => Some(&button.label),
76            CommandBarElement::ToggleButton(button) => Some(&button.label),
77            CommandBarElement::Separator(_) => None,
78        }
79    }
80
81    pub(crate) fn has_icon(&self) -> bool {
82        match self {
83            CommandBarElement::Button(button) => button.icon.is_some(),
84            CommandBarElement::ToggleButton(button) => button.icon.is_some(),
85            CommandBarElement::Separator(_) => false,
86        }
87    }
88
89    pub(crate) fn keyboard_accelerator_text(&self) -> &str {
90        match self {
91            CommandBarElement::Button(button) => &button.keyboard_accelerator_text_override,
92            CommandBarElement::ToggleButton(button) => &button.keyboard_accelerator_text_override,
93            CommandBarElement::Separator(_) => "",
94        }
95    }
96
97    pub(crate) fn is_focusable(&self) -> bool {
98        match self {
99            CommandBarElement::Button(button) => button.is_enabled,
100            CommandBarElement::ToggleButton(button) => button.is_enabled,
101            CommandBarElement::Separator(_) => false,
102        }
103    }
104
105    /// `ICommandBarLabeledElement::GetHasBottomLabel` and `GetHasRightLabel`: a labelled button whose effective label position is `position`.
106    pub(crate) fn has_label_at(
107        &self,
108        default: CommandBarDefaultLabelPosition,
109        position: CommandBarDefaultLabelPosition,
110    ) -> bool {
111        let (label, label_position) = match self {
112            CommandBarElement::Button(button) => (&button.label, button.label_position),
113            CommandBarElement::ToggleButton(button) => (&button.label, button.label_position),
114            CommandBarElement::Separator(_) => return false,
115        };
116        effective_label_position(label_position, default) == position && !label.is_empty()
117    }
118
119    /// The element as the bar hosts it, with `presentation` and the bar's `host` hooks.
120    pub(crate) fn hosted(
121        &self,
122        presentation: ElementPresentation,
123        host: ElementHost,
124    ) -> CommandBarElement {
125        match self.clone() {
126            CommandBarElement::Button(mut button) => {
127                button.presentation = presentation;
128                button.host = Some(host);
129                CommandBarElement::Button(button)
130            }
131            CommandBarElement::ToggleButton(mut button) => {
132                button.presentation = presentation;
133                button.host = Some(host);
134                CommandBarElement::ToggleButton(button)
135            }
136            CommandBarElement::Separator(mut separator) => {
137                separator.presentation = presentation;
138                CommandBarElement::Separator(separator)
139            }
140        }
141    }
142
143    /// What the bar compares between builds to tell whether its commands changed: each element's kind, label and `DynamicOverflowOrder`.
144    pub(crate) fn signature(&self) -> (u8, Option<String>, i32) {
145        let kind = match self {
146            CommandBarElement::Button(_) => 0,
147            CommandBarElement::ToggleButton(_) => 1,
148            CommandBarElement::Separator(_) => 2,
149        };
150        (
151            kind,
152            self.label().map(str::to_string),
153            self.dynamic_overflow_order(),
154        )
155    }
156}
157
158/// `GetEffectiveLabelPosition`: a `Collapsed` button has no label; any other follows the bar.
159pub(crate) fn effective_label_position(
160    label_position: CommandBarLabelPosition,
161    default: CommandBarDefaultLabelPosition,
162) -> CommandBarDefaultLabelPosition {
163    match label_position {
164        CommandBarLabelPosition::Collapsed => CommandBarDefaultLabelPosition::Collapsed,
165        CommandBarLabelPosition::Default => default,
166    }
167}
168
169/// What a hosting bar sets on an element: `IsCompact`, `UseOverflowStyle`, `SetDefaultLabelPosition`, `SetOverflowStyleParams`, `SetInputMode` and the `KeyboardAcceleratorTextMinWidth` template setting. An element outside a bar has the default: full size, labels at the bottom.
170#[derive(Clone, Copy, Debug, Default, PartialEq)]
171pub(crate) struct ElementPresentation {
172    /// `IsCompact`: the bar is closed, so a bottom label is hidden.
173    pub(crate) is_compact: bool,
174    /// `UseOverflowStyle`: the element is in the overflow menu.
175    pub(crate) use_overflow_style: bool,
176    /// The bar's `DefaultLabelPosition`.
177    pub(crate) default_label_position: CommandBarDefaultLabelPosition,
178    /// `m_isWithIcons`: an overflow item has an icon.
179    pub(crate) with_icons: bool,
180    /// `m_isWithToggleButtons`: an overflow item is a toggle button.
181    pub(crate) with_toggle_buttons: bool,
182    /// `m_isWithKeyboardAcceleratorText`: an overflow item shows accelerator text.
183    pub(crate) with_keyboard_accelerator_text: bool,
184    /// `m_inputDeviceTypeUsedToOpenOverflow`.
185    pub(crate) input_mode: InputDevice,
186    /// `TemplateSettings.KeyboardAcceleratorTextMinWidth`: the widest accelerator text in the overflow.
187    pub(crate) keyboard_accelerator_text_min_width: f64,
188    /// The bar's `AppBarButtonHasFlyoutChevronVisibility`, `Collapsed`, overrides the button's own.
189    pub(crate) in_command_bar: bool,
190}
191
192/// `ApplicationViewStates`, from `ChangeCommonVisualStates` for an element outside the overflow and from `ChangeVisualState` for one in it.
193#[derive(Clone, Copy, Debug, PartialEq, Eq)]
194pub(crate) enum ApplicationViewState {
195    FullSize,
196    Compact,
197    LabelOnRight,
198    LabelCollapsed,
199    Overflow,
200    OverflowWithToggleButtons,
201    OverflowWithMenuIcons,
202    OverflowWithToggleButtonsAndMenuIcons,
203}
204
205impl ApplicationViewState {
206    /// The state of a button with `label_position`, which `AppBarToggleButton` reaches only without toggle buttons, as it reports none.
207    pub(crate) fn of(
208        presentation: &ElementPresentation,
209        label_position: CommandBarLabelPosition,
210    ) -> ApplicationViewState {
211        if presentation.use_overflow_style {
212            return match (presentation.with_toggle_buttons, presentation.with_icons) {
213                (true, true) => ApplicationViewState::OverflowWithToggleButtonsAndMenuIcons,
214                (true, false) => ApplicationViewState::OverflowWithToggleButtons,
215                (false, true) => ApplicationViewState::OverflowWithMenuIcons,
216                (false, false) => ApplicationViewState::Overflow,
217            };
218        }
219        match effective_label_position(label_position, presentation.default_label_position) {
220            CommandBarDefaultLabelPosition::Right => ApplicationViewState::LabelOnRight,
221            CommandBarDefaultLabelPosition::Collapsed => ApplicationViewState::LabelCollapsed,
222            CommandBarDefaultLabelPosition::Bottom if presentation.is_compact => {
223                ApplicationViewState::Compact
224            }
225            CommandBarDefaultLabelPosition::Bottom => ApplicationViewState::FullSize,
226        }
227    }
228
229    pub(crate) fn is_overflow(self) -> bool {
230        matches!(
231            self,
232            ApplicationViewState::Overflow
233                | ApplicationViewState::OverflowWithToggleButtons
234                | ApplicationViewState::OverflowWithMenuIcons
235                | ApplicationViewState::OverflowWithToggleButtonsAndMenuIcons
236        )
237    }
238}
239
240/// What a bar hands an element it hosts besides its presentation.
241#[derive(Clone, Debug)]
242pub(crate) struct ElementHost {
243    /// `CommandBar::OnCommandExecutionStatic`: the click closes the bar, before the element's own `Click`.
244    pub(crate) executed: Listener,
245    /// The node the bar moves the keyboard focus with.
246    pub(crate) focus_node: FocusNodeRef,
247}