use std::{fmt, rc::Rc};
use waft_foundation::{App, Listener};
use waft_widgets::{IntoWidget, KeyRef, WidgetRef};
pub type MenuCheckedChanged = Rc<dyn Fn(&mut App, bool)>;
#[derive(Clone)]
pub enum MenuFlyoutItemKind {
Item,
Toggle {
is_checked: bool,
changed: MenuCheckedChanged,
},
Radio {
is_checked: bool,
checked: Listener,
},
SubItem {
items: Vec<MenuFlyoutItem>,
},
Separator,
}
impl fmt::Debug for MenuFlyoutItemKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Item => f.write_str("Item"),
Self::Toggle { is_checked, .. } => f.debug_tuple("Toggle").field(is_checked).finish(),
Self::Radio { is_checked, .. } => f.debug_tuple("Radio").field(is_checked).finish(),
Self::SubItem { items } => f.debug_tuple("SubItem").field(items).finish(),
Self::Separator => f.write_str("Separator"),
}
}
}
#[derive(Clone)]
pub struct MenuFlyoutItem {
pub text: String,
pub icon: Option<WidgetRef>,
pub is_enabled: bool,
pub keyboard_accelerator_text_override: String,
pub click: Option<Listener>,
pub prevent_dismiss_on_pointer: bool,
pub kind: MenuFlyoutItemKind,
pub key: Option<KeyRef>,
}
impl fmt::Debug for MenuFlyoutItem {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("MenuFlyoutItem")
.field("text", &self.text)
.field("kind", &self.kind)
.field("is_enabled", &self.is_enabled)
.finish_non_exhaustive()
}
}
impl MenuFlyoutItem {
pub fn new(text: impl Into<String>, click: Listener) -> Self {
Self {
text: text.into(),
icon: None,
is_enabled: true,
keyboard_accelerator_text_override: String::new(),
click: Some(click),
prevent_dismiss_on_pointer: false,
kind: MenuFlyoutItemKind::Item,
key: None,
}
}
pub fn toggle(
text: impl Into<String>,
is_checked: bool,
changed: impl Fn(&mut App, bool) + 'static,
) -> Self {
let mut item = Self::new(text, Listener::new(|_| {}));
item.click = None;
item.kind = MenuFlyoutItemKind::Toggle {
is_checked,
changed: Rc::new(changed),
};
item
}
pub fn radio(text: impl Into<String>, is_checked: bool, checked: Listener) -> Self {
let mut item = Self::new(text, Listener::new(|_| {}));
item.click = None;
item.kind = MenuFlyoutItemKind::Radio {
is_checked,
checked,
};
item
}
pub fn sub_item(text: impl Into<String>, items: Vec<MenuFlyoutItem>) -> Self {
let mut item = Self::new(text, Listener::new(|_| {}));
item.click = None;
item.kind = MenuFlyoutItemKind::SubItem { items };
item
}
pub fn separator() -> Self {
let mut item = Self::new("", Listener::new(|_| {}));
item.click = None;
item.kind = MenuFlyoutItemKind::Separator;
item
}
pub fn icon<K>(mut self, icon: impl IntoWidget<K>) -> Self {
self.icon = Some(icon.into_widget());
self
}
pub fn is_enabled(mut self, enabled: bool) -> Self {
self.is_enabled = enabled;
self
}
pub fn keyboard_accelerator_text_override(mut self, text: impl Into<String>) -> Self {
self.keyboard_accelerator_text_override = text.into();
self
}
pub fn click(mut self, click: Listener) -> Self {
self.click = Some(click);
self
}
pub fn prevent_dismiss_on_pointer(mut self, value: bool) -> Self {
self.prevent_dismiss_on_pointer = value;
self
}
pub fn key(mut self, key: KeyRef) -> Self {
self.key = Some(key);
self
}
}