Skip to main content

waft_winui/controls/menu_flyout/
item.rs

1//! MenuFlyoutItemBase's item family expressed as immutable widget configuration.
2
3use std::{fmt, rc::Rc};
4use waft_foundation::{App, Listener};
5use waft_widgets::{IntoWidget, KeyRef, WidgetRef};
6
7/// Requested checked value after activating a toggle menu item.
8pub type MenuCheckedChanged = Rc<dyn Fn(&mut App, bool)>;
9
10/// The source item classes share text, icon and enabled properties.
11#[derive(Clone)]
12pub enum MenuFlyoutItemKind {
13    /// MenuFlyoutItem invokes its Click callback.
14    Item,
15
16    /// ToggleMenuFlyoutItem requests the opposite checked value before Click.
17    Toggle {
18        /// Checked state supplied by the owner.
19        is_checked: bool,
20
21        /// Updates the owner's checked value.
22        changed: MenuCheckedChanged,
23    },
24
25    /// RadioMenuFlyoutItem can request selection but cannot uncheck itself.
26    Radio {
27        /// Whether this option is selected.
28        is_checked: bool,
29
30        /// Updates the owner's selected option, as with RadioButton.
31        checked: Listener,
32    },
33
34    /// MenuFlyoutSubItem opens a cascading collection.
35    SubItem {
36        /// Items displayed in the child menu.
37        items: Vec<MenuFlyoutItem>,
38    },
39
40    /// MenuFlyoutSeparator contributes no focus stop or activation.
41    Separator,
42}
43
44impl fmt::Debug for MenuFlyoutItemKind {
45    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46        match self {
47            Self::Item => f.write_str("Item"),
48            Self::Toggle { is_checked, .. } => f.debug_tuple("Toggle").field(is_checked).finish(),
49            Self::Radio { is_checked, .. } => f.debug_tuple("Radio").field(is_checked).finish(),
50            Self::SubItem { items } => f.debug_tuple("SubItem").field(items).finish(),
51            Self::Separator => f.write_str("Separator"),
52        }
53    }
54}
55
56/// Configuration for one member of the MenuFlyoutItemBase family.
57#[derive(Clone)]
58pub struct MenuFlyoutItem {
59    /// Text presented in the item's main column.
60    pub text: String,
61
62    /// Optional icon widget fitted into the shared icon column.
63    pub icon: Option<WidgetRef>,
64
65    /// Whether this item can receive focus or be invoked.
66    pub is_enabled: bool,
67
68    /// Explicit shortcut label in the right-hand column.
69    pub keyboard_accelerator_text_override: String,
70
71    /// Called after the checked-value callback and before closing the root menu.
72    pub click: Option<Listener>,
73
74    /// Leaves the menu open after invocation, following PreventDismissOnPointer.
75    pub prevent_dismiss_on_pointer: bool,
76
77    /// Chooses the source item behavior and template.
78    pub kind: MenuFlyoutItemKind,
79
80    /// Identity used to retain this item's state when the collection changes.
81    pub key: Option<KeyRef>,
82}
83
84impl fmt::Debug for MenuFlyoutItem {
85    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
86        f.debug_struct("MenuFlyoutItem")
87            .field("text", &self.text)
88            .field("kind", &self.kind)
89            .field("is_enabled", &self.is_enabled)
90            .finish_non_exhaustive()
91    }
92}
93
94impl MenuFlyoutItem {
95    /// Creates an ordinary command item.
96    pub fn new(text: impl Into<String>, click: Listener) -> Self {
97        Self {
98            text: text.into(),
99            icon: None,
100            is_enabled: true,
101            keyboard_accelerator_text_override: String::new(),
102            click: Some(click),
103            prevent_dismiss_on_pointer: false,
104            kind: MenuFlyoutItemKind::Item,
105            key: None,
106        }
107    }
108
109    /// Creates the ToggleMenuFlyoutItem variant with an owner-supplied value.
110    pub fn toggle(
111        text: impl Into<String>,
112        is_checked: bool,
113        changed: impl Fn(&mut App, bool) + 'static,
114    ) -> Self {
115        let mut item = Self::new(text, Listener::new(|_| {}));
116        item.click = None;
117        item.kind = MenuFlyoutItemKind::Toggle {
118            is_checked,
119            changed: Rc::new(changed),
120        };
121        item
122    }
123
124    /// Creates the RadioMenuFlyoutItem variant; the owner updates the selected option.
125    pub fn radio(text: impl Into<String>, is_checked: bool, checked: Listener) -> Self {
126        let mut item = Self::new(text, Listener::new(|_| {}));
127        item.click = None;
128        item.kind = MenuFlyoutItemKind::Radio {
129            is_checked,
130            checked,
131        };
132        item
133    }
134
135    /// Creates the MenuFlyoutSubItem variant.
136    pub fn sub_item(text: impl Into<String>, items: Vec<MenuFlyoutItem>) -> Self {
137        let mut item = Self::new(text, Listener::new(|_| {}));
138        item.click = None;
139        item.kind = MenuFlyoutItemKind::SubItem { items };
140        item
141    }
142
143    /// Creates a non-interactive MenuFlyoutSeparator.
144    pub fn separator() -> Self {
145        let mut item = Self::new("", Listener::new(|_| {}));
146        item.click = None;
147        item.kind = MenuFlyoutItemKind::Separator;
148        item
149    }
150
151    /// Sets the widget displayed by the template’s icon presenter.
152    #[cfg_attr(debug_assertions, track_caller)]
153    pub fn icon<K>(mut self, icon: impl IntoWidget<K>) -> Self {
154        self.icon = Some(icon.into_widget());
155        self
156    }
157
158    /// Enables or disables activation and focus.
159    pub fn is_enabled(mut self, enabled: bool) -> Self {
160        self.is_enabled = enabled;
161        self
162    }
163
164    /// Sets the displayed shortcut label; key bindings are supplied separately.
165    pub fn keyboard_accelerator_text_override(mut self, text: impl Into<String>) -> Self {
166        self.keyboard_accelerator_text_override = text.into();
167        self
168    }
169
170    /// Sets Click for checked variants as well as ordinary items.
171    pub fn click(mut self, click: Listener) -> Self {
172        self.click = Some(click);
173        self
174    }
175
176    /// Keeps the root menu open after invocation.
177    pub fn prevent_dismiss_on_pointer(mut self, value: bool) -> Self {
178        self.prevent_dismiss_on_pointer = value;
179        self
180    }
181
182    /// Sets collection identity independently of item position.
183    pub fn key(mut self, key: KeyRef) -> Self {
184        self.key = Some(key);
185        self
186    }
187}