waft_winui/controls/command_bar/
element.rs1use super::{AppBarButton, AppBarSeparator, AppBarToggleButton};
6use crate::InputDevice;
7use waft_foundation::Listener;
8use waft_widgets::FocusNodeRef;
9
10#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
12pub enum CommandBarDefaultLabelPosition {
13 #[default]
15 Bottom,
16 Right,
18 Collapsed,
20}
21
22#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
24pub enum CommandBarLabelPosition {
25 #[default]
27 Default,
28 Collapsed,
30}
31
32#[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 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 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 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 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 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
158pub(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#[derive(Clone, Copy, Debug, Default, PartialEq)]
171pub(crate) struct ElementPresentation {
172 pub(crate) is_compact: bool,
174 pub(crate) use_overflow_style: bool,
176 pub(crate) default_label_position: CommandBarDefaultLabelPosition,
178 pub(crate) with_icons: bool,
180 pub(crate) with_toggle_buttons: bool,
182 pub(crate) with_keyboard_accelerator_text: bool,
184 pub(crate) input_mode: InputDevice,
186 pub(crate) keyboard_accelerator_text_min_width: f64,
188 pub(crate) in_command_bar: bool,
190}
191
192#[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 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#[derive(Clone, Debug)]
242pub(crate) struct ElementHost {
243 pub(crate) executed: Listener,
245 pub(crate) focus_node: FocusNodeRef,
247}