use std::cell::{Cell, RefCell};
use std::collections::HashMap;
use std::fmt::{self, Debug};
use std::rc::Rc;
use waft_embedder::{
IconImage, MenuEntry, MenuId, MenuItem, MenuItemCheck, MenuShortcut, ProvidedItem,
ShortcutModifiers, TargetPlatform,
};
use waft_foundation::{App, Handle, VoidCallback};
use waft_services::LogicalKeyboardKey;
use crate::WidgetsBinding;
use crate::framework::{
BuildContext, IntoWidget, KeyRef, State, StateData, StatefulWidget, WidgetRef,
};
use crate::widgets::actions::{Actions, IntentRef};
use crate::widgets::basic::SizedBox;
use crate::widgets::focus_manager::FocusManager;
use crate::widgets::shortcuts::{CharacterActivator, SingleActivator};
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ShortcutSerialization {
trigger: Option<LogicalKeyboardKey>,
character: Option<String>,
alt: Option<bool>,
control: Option<bool>,
meta: Option<bool>,
shift: Option<bool>,
internal: MenuShortcut,
}
impl ShortcutSerialization {
pub fn character(
character: impl Into<String>,
alt: bool,
control: bool,
meta: bool,
) -> ShortcutSerialization {
let character = character.into();
debug_assert_eq!(character.chars().count(), 1);
let key = character.chars().next().map_or(0, u64::from);
ShortcutSerialization {
internal: MenuShortcut {
key,
modifiers: ShortcutModifiers {
meta,
control,
alt,
shift: false,
},
},
character: Some(character),
trigger: None,
alt: Some(alt),
control: Some(control),
meta: Some(meta),
shift: None,
}
}
pub fn modifier(
trigger: LogicalKeyboardKey,
alt: bool,
control: bool,
meta: bool,
shift: bool,
) -> ShortcutSerialization {
debug_assert!(
![
LogicalKeyboardKey::ALT,
LogicalKeyboardKey::ALT_LEFT,
LogicalKeyboardKey::ALT_RIGHT,
LogicalKeyboardKey::CONTROL,
LogicalKeyboardKey::CONTROL_LEFT,
LogicalKeyboardKey::CONTROL_RIGHT,
LogicalKeyboardKey::META,
LogicalKeyboardKey::META_LEFT,
LogicalKeyboardKey::META_RIGHT,
LogicalKeyboardKey::SHIFT,
LogicalKeyboardKey::SHIFT_LEFT,
LogicalKeyboardKey::SHIFT_RIGHT,
]
.contains(&trigger),
"Specifying a modifier key as a trigger is not allowed. Use provided boolean \
parameters instead."
);
ShortcutSerialization {
internal: MenuShortcut {
key: trigger.key_id,
modifiers: ShortcutModifiers {
meta,
control,
alt,
shift,
},
},
trigger: Some(trigger),
character: None,
alt: Some(alt),
control: Some(control),
meta: Some(meta),
shift: Some(shift),
}
}
pub fn trigger(&self) -> Option<LogicalKeyboardKey> {
self.trigger
}
pub fn character_value(&self) -> Option<&str> {
self.character.as_deref()
}
pub fn alt(&self) -> Option<bool> {
self.alt
}
pub fn control(&self) -> Option<bool> {
self.control
}
pub fn meta(&self) -> Option<bool> {
self.meta
}
pub fn shift(&self) -> Option<bool> {
self.shift
}
pub fn to_menu_shortcut(&self) -> MenuShortcut {
self.internal
}
}
pub trait MenuSerializableShortcut: Debug {
fn serialize_for_menu(&self) -> ShortcutSerialization;
}
pub type MenuSerializableShortcutRef = Rc<dyn MenuSerializableShortcut>;
impl MenuSerializableShortcut for SingleActivator {
fn serialize_for_menu(&self) -> ShortcutSerialization {
ShortcutSerialization::modifier(self.trigger, self.alt, self.control, self.meta, self.shift)
}
}
impl MenuSerializableShortcut for CharacterActivator {
fn serialize_for_menu(&self) -> ShortcutSerialization {
ShortcutSerialization::character(self.character.clone(), self.alt, self.control, self.meta)
}
}
pub trait PlatformMenuDelegate {
fn set_menus(&self, app: &mut App, top_level_menus: &[PlatformMenuItemRef]);
fn clear_menus(&self, app: &mut App);
fn debug_lock_delegate(&self, context: BuildContext) -> bool;
fn debug_unlock_delegate(&self, context: BuildContext) -> bool;
}
pub type PlatformMenuDelegateRef = Rc<dyn PlatformMenuDelegate>;
pub type MenuItemSerializableIdGenerator<'a> = dyn FnMut(PlatformMenuItemRef) -> MenuId + 'a;
pub struct DefaultPlatformMenuDelegate {
id_map: RefCell<HashMap<MenuId, PlatformMenuItemRef>>,
serial: Cell<MenuId>,
locked_context: Cell<Option<BuildContext>>,
}
impl DefaultPlatformMenuDelegate {
pub fn new(app: &mut App) -> Rc<DefaultPlatformMenuDelegate> {
let delegate = Rc::new(DefaultPlatformMenuDelegate {
id_map: RefCell::default(),
serial: Cell::new(0),
locked_context: Cell::new(None),
});
let handler = Rc::downgrade(&delegate);
app.platform_callbacks_mut().on_menu_selected = Some(Rc::new(move |app, id| {
if let Some(delegate) = handler.upgrade() {
delegate.method_call_handler(app, id);
}
}));
delegate
}
fn get_id(&self, item: PlatformMenuItemRef) -> MenuId {
self.serial.set(self.serial.get() + 1);
self.id_map.borrow_mut().insert(self.serial.get(), item);
self.serial.get()
}
fn method_call_handler(&self, app: &mut App, id: MenuId) {
let item = self.id_map.borrow().get(&id).cloned();
debug_assert!(
item.is_some(),
"Received a menu choice for a menu item with an ID that was not recognized: {id}"
);
let Some(item) = item else {
return;
};
select_menu_item(app, &item);
}
}
pub(crate) fn select_menu_item(app: &mut App, item: &PlatformMenuItemRef) {
debug_assert!(
item.on_selected().is_none() || item.on_selected_intent().is_none(),
"Only one of PlatformMenuItem.onSelected or PlatformMenuItem.onSelectedIntent may be \
specified"
);
if let Some(on_selected) = item.on_selected().cloned() {
on_selected(app);
}
if let Some(intent) = item.on_selected_intent().cloned() {
let context = FocusManager::instance(app)
.primary_focus(app)
.and_then(|focus| focus.context(app))
.expect("a menu item's intent is invoked on the primary focus");
Actions::maybe_invoke(app, context, &*intent);
}
}
impl PlatformMenuDelegate for DefaultPlatformMenuDelegate {
fn clear_menus(&self, app: &mut App) {
self.set_menus(app, &[]);
}
fn set_menus(&self, app: &mut App, top_level_menus: &[PlatformMenuItemRef]) {
self.id_map.borrow_mut().clear();
let mut representation = Vec::new();
for child_item in top_level_menus {
representation
.extend(Rc::clone(child_item).to_menu_entries(self, &mut |item| self.get_id(item)));
}
let platform = app.platform();
if let Some(menu_bar) = platform.menu_bar() {
menu_bar.set_menus(&representation);
}
}
fn debug_lock_delegate(&self, context: BuildContext) -> bool {
if cfg!(debug_assertions) {
if self
.locked_context
.get()
.is_some_and(|locked| locked != context)
{
return false;
}
self.locked_context.set(Some(context));
}
true
}
fn debug_unlock_delegate(&self, context: BuildContext) -> bool {
if cfg!(debug_assertions) {
if self
.locked_context
.get()
.is_some_and(|locked| locked != context)
{
return false;
}
self.locked_context.set(None);
}
true
}
}
pub struct PlatformMenuBar {
pub key: Option<KeyRef>,
pub child: Option<WidgetRef>,
pub menus: Vec<PlatformMenuItemRef>,
}
impl PlatformMenuBar {
pub fn new(menus: Vec<PlatformMenuItemRef>) -> PlatformMenuBar {
PlatformMenuBar {
key: None,
child: None,
menus,
}
}
pub fn key(mut self, key: KeyRef) -> PlatformMenuBar {
self.key = Some(key);
self
}
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> PlatformMenuBar {
self.child = Some(child.into_widget());
self
}
}
impl Debug for PlatformMenuBar {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("PlatformMenuBar")
.field("menus", &self.menus)
.finish_non_exhaustive()
}
}
impl StatefulWidget for PlatformMenuBar {
type State = PlatformMenuBarState;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_state(&self) -> PlatformMenuBarState {
PlatformMenuBarState {
state: StateData::new(),
descendants: Vec::new(),
}
}
}
pub struct PlatformMenuBarState {
state: StateData<PlatformMenuBar>,
descendants: Vec<PlatformMenuItemRef>,
}
impl PlatformMenuBarState {
fn update_menu(self: Handle<Self>, app: &mut App) {
let menus = self.widget(app).menus.clone();
let delegate = WidgetsBinding::instance(app).platform_menu_delegate(app);
delegate.set_menus(app, &menus);
}
}
impl State for PlatformMenuBarState {
type Widget = PlatformMenuBar;
crate::state_accessors!();
fn init_state(self: Handle<Self>, app: &mut App) {
let delegate = WidgetsBinding::instance(app).platform_menu_delegate(app);
let context = self.context(app);
debug_assert!(
delegate.debug_lock_delegate(context),
"More than one active PlatformMenuBar detected. Only one active platform-rendered \
menu bar is allowed at a time."
);
delegate.clear_menus(app);
self.update_menu(app);
}
fn dispose(self: Handle<Self>, app: &mut App) {
let delegate = WidgetsBinding::instance(app).platform_menu_delegate(app);
let context = self.context(app);
debug_assert!(
delegate.debug_unlock_delegate(context),
"tried to unlock the DefaultPlatformMenuDelegate more than once with context \
{context:?}."
);
delegate.clear_menus(app);
}
fn did_update_widget(self: Handle<Self>, app: &mut App, _old_widget: &PlatformMenuBar) {
let mut new_descendants = Vec::new();
for item in &self.widget(app).menus {
new_descendants.push(Rc::clone(item));
new_descendants.extend(item.descendants());
}
if !same_items(&new_descendants, &app.get(self).descendants) {
app.get_mut(self).descendants = new_descendants;
self.update_menu(app);
}
}
fn build(self: Handle<Self>, app: &mut App, _context: BuildContext) -> WidgetRef {
self.widget(app)
.child
.clone()
.unwrap_or_else(|| SizedBox::new().into_widget())
}
}
pub(crate) fn same_items(a: &[PlatformMenuItemRef], b: &[PlatformMenuItemRef]) -> bool {
a.len() == b.len() && a.iter().zip(b).all(|(a, b)| Rc::ptr_eq(a, b))
}
pub type PlatformMenuItemRef = Rc<dyn PlatformMenuItem>;
pub trait PlatformMenuItem: Debug {
fn label(&self) -> &str;
fn tooltip(&self) -> Option<&str> {
None
}
fn shortcut(&self) -> Option<&MenuSerializableShortcutRef> {
None
}
fn on_selected(&self) -> Option<&VoidCallback> {
None
}
fn on_open(&self) -> Option<&VoidCallback> {
None
}
fn on_close(&self) -> Option<&VoidCallback> {
None
}
fn on_selected_intent(&self) -> Option<&IntentRef> {
None
}
fn check(&self) -> MenuItemCheck {
MenuItemCheck::Plain
}
fn icon(&self) -> Option<&IconImage> {
None
}
fn descendants(&self) -> Vec<PlatformMenuItemRef> {
Vec::new()
}
fn members(&self) -> Vec<PlatformMenuItemRef> {
Vec::new()
}
fn to_menu_entries(
self: Rc<Self>,
delegate: &dyn PlatformMenuDelegate,
get_id: &mut MenuItemSerializableIdGenerator<'_>,
) -> Vec<MenuEntry>;
}
pub struct PlatformMenuItemBase;
impl PlatformMenuItemBase {
pub fn serialize(
item: PlatformMenuItemRef,
_delegate: &dyn PlatformMenuDelegate,
get_id: &mut MenuItemSerializableIdGenerator<'_>,
) -> MenuEntry {
let shortcut = item
.shortcut()
.map(|shortcut| shortcut.serialize_for_menu().to_menu_shortcut());
let menu_item = MenuItem {
label: item.label().to_owned(),
enabled: item.on_selected().is_some() || item.on_selected_intent().is_some(),
check: item.check(),
shortcut,
icon: item.icon().cloned(),
};
MenuEntry::Item {
id: get_id(item),
item: menu_item,
}
}
}
pub struct PlainPlatformMenuItem {
pub label: String,
pub tooltip: Option<String>,
pub shortcut: Option<MenuSerializableShortcutRef>,
pub on_selected: Option<VoidCallback>,
pub on_selected_intent: Option<IntentRef>,
pub check: MenuItemCheck,
pub icon: Option<IconImage>,
}
impl PlainPlatformMenuItem {
pub fn new(label: impl Into<String>) -> PlainPlatformMenuItem {
PlainPlatformMenuItem {
label: label.into(),
tooltip: None,
shortcut: None,
on_selected: None,
on_selected_intent: None,
check: MenuItemCheck::Plain,
icon: None,
}
}
pub fn tooltip(mut self, tooltip: impl Into<String>) -> PlainPlatformMenuItem {
self.tooltip = Some(tooltip.into());
self
}
pub fn shortcut(mut self, shortcut: MenuSerializableShortcutRef) -> PlainPlatformMenuItem {
self.shortcut = Some(shortcut);
self
}
pub fn on_selected(mut self, on_selected: VoidCallback) -> PlainPlatformMenuItem {
debug_assert!(
self.on_selected_intent.is_none(),
"Only one of onSelected or onSelectedIntent may be specified"
);
self.on_selected = Some(on_selected);
self
}
pub fn on_selected_intent(mut self, intent: IntentRef) -> PlainPlatformMenuItem {
debug_assert!(
self.on_selected.is_none(),
"Only one of onSelected or onSelectedIntent may be specified"
);
self.on_selected_intent = Some(intent);
self
}
pub fn check(mut self, check: MenuItemCheck) -> PlainPlatformMenuItem {
self.check = check;
self
}
pub fn icon(mut self, icon: IconImage) -> PlainPlatformMenuItem {
self.icon = Some(icon);
self
}
}
impl Debug for PlainPlatformMenuItem {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("PlatformMenuItem")
.field("label", &self.label)
.field("tooltip", &self.tooltip)
.field("shortcut", &self.shortcut)
.field("enabled", &self.on_selected.is_some())
.finish()
}
}
impl PlatformMenuItem for PlainPlatformMenuItem {
fn label(&self) -> &str {
&self.label
}
fn tooltip(&self) -> Option<&str> {
self.tooltip.as_deref()
}
fn shortcut(&self) -> Option<&MenuSerializableShortcutRef> {
self.shortcut.as_ref()
}
fn on_selected(&self) -> Option<&VoidCallback> {
self.on_selected.as_ref()
}
fn on_selected_intent(&self) -> Option<&IntentRef> {
self.on_selected_intent.as_ref()
}
fn check(&self) -> MenuItemCheck {
self.check
}
fn icon(&self) -> Option<&IconImage> {
self.icon.as_ref()
}
fn to_menu_entries(
self: Rc<Self>,
delegate: &dyn PlatformMenuDelegate,
get_id: &mut MenuItemSerializableIdGenerator<'_>,
) -> Vec<MenuEntry> {
vec![PlatformMenuItemBase::serialize(self, delegate, get_id)]
}
}
pub struct PlatformMenu {
pub label: String,
pub tooltip: Option<String>,
pub on_open: Option<VoidCallback>,
pub on_close: Option<VoidCallback>,
pub menus: Vec<PlatformMenuItemRef>,
}
impl PlatformMenu {
pub fn new(label: impl Into<String>, menus: Vec<PlatformMenuItemRef>) -> PlatformMenu {
PlatformMenu {
label: label.into(),
tooltip: None,
on_open: None,
on_close: None,
menus,
}
}
pub fn tooltip(mut self, tooltip: impl Into<String>) -> PlatformMenu {
self.tooltip = Some(tooltip.into());
self
}
pub fn on_open(mut self, on_open: VoidCallback) -> PlatformMenu {
self.on_open = Some(on_open);
self
}
pub fn on_close(mut self, on_close: VoidCallback) -> PlatformMenu {
self.on_close = Some(on_close);
self
}
pub fn get_descendants(item: &PlatformMenu) -> Vec<PlatformMenuItemRef> {
let mut descendants = Vec::new();
for child in &item.menus {
descendants.push(Rc::clone(child));
descendants.extend(child.descendants());
}
descendants
}
pub fn serialize(
item: Rc<PlatformMenu>,
delegate: &dyn PlatformMenuDelegate,
get_id: &mut MenuItemSerializableIdGenerator<'_>,
) -> MenuEntry {
let mut result: Vec<MenuEntry> = Vec::new();
for child_item in &item.menus {
result.extend(Rc::clone(child_item).to_menu_entries(delegate, get_id));
}
strip_dividers(&mut result);
let enabled = !item.menus.is_empty();
MenuEntry::Submenu {
label: item.label.clone(),
id: get_id(item),
enabled,
entries: result,
}
}
}
pub(crate) fn strip_dividers(result: &mut Vec<MenuEntry>) {
let mut previous_is_divider: Option<bool> = None;
result.retain(|entry| {
let is_divider = matches!(entry, MenuEntry::Separator);
if previous_is_divider.is_none() && is_divider {
return false;
}
if previous_is_divider == Some(true) && is_divider {
return false;
}
previous_is_divider = Some(is_divider);
true
});
if matches!(result.last(), Some(MenuEntry::Separator)) {
result.pop();
}
}
impl Debug for PlatformMenu {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("PlatformMenu")
.field("label", &self.label)
.field("enabled", &!self.menus.is_empty())
.field("menus", &self.menus)
.finish()
}
}
impl PlatformMenuItem for PlatformMenu {
fn label(&self) -> &str {
&self.label
}
fn tooltip(&self) -> Option<&str> {
self.tooltip.as_deref()
}
fn on_open(&self) -> Option<&VoidCallback> {
self.on_open.as_ref()
}
fn on_close(&self) -> Option<&VoidCallback> {
self.on_close.as_ref()
}
fn descendants(&self) -> Vec<PlatformMenuItemRef> {
PlatformMenu::get_descendants(self)
}
fn to_menu_entries(
self: Rc<Self>,
delegate: &dyn PlatformMenuDelegate,
get_id: &mut MenuItemSerializableIdGenerator<'_>,
) -> Vec<MenuEntry> {
vec![PlatformMenu::serialize(self, delegate, get_id)]
}
}
pub struct PlatformMenuItemGroup {
pub members: Vec<PlatformMenuItemRef>,
}
impl PlatformMenuItemGroup {
pub fn new(members: Vec<PlatformMenuItemRef>) -> PlatformMenuItemGroup {
PlatformMenuItemGroup { members }
}
pub fn serialize(
group: &dyn PlatformMenuItem,
delegate: &dyn PlatformMenuDelegate,
get_id: &mut MenuItemSerializableIdGenerator<'_>,
) -> Vec<MenuEntry> {
let mut entries = vec![MenuEntry::Separator];
for item in group.members() {
entries.extend(item.to_menu_entries(delegate, get_id));
}
entries.push(MenuEntry::Separator);
entries
}
}
impl Debug for PlatformMenuItemGroup {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("PlatformMenuItemGroup")
.field("members", &self.members)
.finish()
}
}
impl PlatformMenuItem for PlatformMenuItemGroup {
fn label(&self) -> &str {
""
}
fn members(&self) -> Vec<PlatformMenuItemRef> {
self.members.clone()
}
fn to_menu_entries(
self: Rc<Self>,
delegate: &dyn PlatformMenuDelegate,
get_id: &mut MenuItemSerializableIdGenerator<'_>,
) -> Vec<MenuEntry> {
debug_assert!(
!self.members.is_empty(),
"There must be at least one member in a PlatformMenuItemGroup"
);
PlatformMenuItemGroup::serialize(&*self, delegate, get_id)
}
}
pub struct PlatformProvidedMenuItem {
pub type_: PlatformProvidedMenuItemType,
pub enabled: bool,
}
impl PlatformProvidedMenuItem {
pub fn new(type_: PlatformProvidedMenuItemType) -> PlatformProvidedMenuItem {
PlatformProvidedMenuItem {
type_,
enabled: true,
}
}
pub fn enabled(mut self, enabled: bool) -> PlatformProvidedMenuItem {
self.enabled = enabled;
self
}
pub fn has_menu(app: &App, menu: PlatformProvidedMenuItemType) -> bool {
match app.platform().target_platform() {
TargetPlatform::Android
| TargetPlatform::IOS
| TargetPlatform::Fuchsia
| TargetPlatform::Linux
| TargetPlatform::Windows => false,
TargetPlatform::MacOS => menu.provided_item().is_some(),
}
}
}
impl Debug for PlatformProvidedMenuItem {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("PlatformProvidedMenuItem")
.field("type", &self.type_)
.field("enabled", &self.enabled)
.finish()
}
}
impl PlatformMenuItem for PlatformProvidedMenuItem {
fn label(&self) -> &str {
""
}
fn to_menu_entries(
self: Rc<Self>,
_delegate: &dyn PlatformMenuDelegate,
get_id: &mut MenuItemSerializableIdGenerator<'_>,
) -> Vec<MenuEntry> {
let Some(item) = self.type_.provided_item() else {
debug_assert!(
false,
"No platform provides a menu for {:?}. Call PlatformProvidedMenuItem::has_menu \
to determine this before instantiating one.",
self.type_
);
return Vec::new();
};
let enabled = self.enabled;
vec![MenuEntry::Provided {
id: get_id(self),
item,
enabled,
}]
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum PlatformProvidedMenuItemType {
About,
Quit,
ServicesSubmenu,
Hide,
HideOtherApplications,
ShowAllApplications,
StartSpeaking,
StopSpeaking,
ToggleFullScreen,
MinimizeWindow,
ZoomWindow,
ArrangeWindowsInFront,
Undo,
Redo,
Cut,
Copy,
Paste,
SelectAll,
Delete,
}
impl PlatformProvidedMenuItemType {
fn provided_item(self) -> Option<ProvidedItem> {
Some(match self {
PlatformProvidedMenuItemType::About => ProvidedItem::About,
PlatformProvidedMenuItemType::Quit => ProvidedItem::Quit,
PlatformProvidedMenuItemType::ServicesSubmenu => ProvidedItem::Services,
PlatformProvidedMenuItemType::Hide => ProvidedItem::Hide,
PlatformProvidedMenuItemType::HideOtherApplications => ProvidedItem::HideOthers,
PlatformProvidedMenuItemType::ShowAllApplications => ProvidedItem::ShowAll,
PlatformProvidedMenuItemType::StartSpeaking
| PlatformProvidedMenuItemType::StopSpeaking => return None,
PlatformProvidedMenuItemType::ToggleFullScreen => ProvidedItem::ToggleFullScreen,
PlatformProvidedMenuItemType::MinimizeWindow => ProvidedItem::Minimize,
PlatformProvidedMenuItemType::ZoomWindow => ProvidedItem::Zoom,
PlatformProvidedMenuItemType::ArrangeWindowsInFront => ProvidedItem::BringAllToFront,
PlatformProvidedMenuItemType::Undo => ProvidedItem::Undo,
PlatformProvidedMenuItemType::Redo => ProvidedItem::Redo,
PlatformProvidedMenuItemType::Cut => ProvidedItem::Cut,
PlatformProvidedMenuItemType::Copy => ProvidedItem::Copy,
PlatformProvidedMenuItemType::Paste => ProvidedItem::Paste,
PlatformProvidedMenuItemType::SelectAll => ProvidedItem::SelectAll,
PlatformProvidedMenuItemType::Delete => ProvidedItem::Delete,
})
}
}
#[cfg(test)]
mod tests {
use std::cell::RefCell;
use std::time::Duration;
use waft_foundation::AppCell;
use waft_services::LogicalKeyboardKey;
use waft_test::{MockMenuBar, TestPlatform, TestView};
use super::*;
use crate::binding::run_app;
use crate::framework::IntoWidget;
use crate::test_harness::pump_every_view;
fn app_with_menu_bar(menu_bar: &MockMenuBar) -> Rc<AppCell> {
let platform = TestPlatform::new()
.on(TargetPlatform::MacOS)
.with_view(Rc::new(TestView::new(800.0, 600.0)))
.with_menu_bar(Rc::new(menu_bar.clone()));
AppCell::with_platform(Rc::new(platform))
}
fn mount(cell: &AppCell, widget: WidgetRef) {
run_app(&mut cell.borrow_mut(), widget);
cell.elapse(Duration::ZERO);
pump_every_view(&mut cell.borrow_mut(), Duration::ZERO);
}
fn menus(chosen: &Rc<RefCell<Vec<&'static str>>>) -> Vec<PlatformMenuItemRef> {
let open = Rc::clone(chosen);
vec![Rc::new(PlatformMenu::new(
"File",
vec![
Rc::new(
PlainPlatformMenuItem::new("Open")
.shortcut(Rc::new(
SingleActivator::new(LogicalKeyboardKey::KEY_O).meta(true),
))
.on_selected(Rc::new(move |_app| open.borrow_mut().push("Open"))),
),
Rc::new(PlatformMenuItemGroup::new(vec![Rc::new(
PlainPlatformMenuItem::new("Save"),
)])),
Rc::new(PlatformProvidedMenuItem::new(
PlatformProvidedMenuItemType::Quit,
)),
],
))]
}
#[test]
fn the_menu_bar_reaches_the_host_and_a_choice_reaches_its_item() {
let menu_bar = MockMenuBar::new();
let cell = app_with_menu_bar(&menu_bar);
let chosen = Rc::new(RefCell::new(Vec::new()));
mount(&cell, PlatformMenuBar::new(menus(&chosen)).into_widget());
let set = menu_bar.menus();
let [
MenuEntry::Submenu {
label,
enabled,
entries,
..
},
] = &set[..]
else {
panic!("one top-level menu: {set:?}");
};
assert_eq!(label, "File");
assert!(enabled);
let [
MenuEntry::Item { item: open, .. },
MenuEntry::Separator,
MenuEntry::Item { item: save, .. },
MenuEntry::Separator,
MenuEntry::Provided { item, .. },
] = &entries[..]
else {
panic!("Open, the group between separators, and Quit: {entries:?}");
};
assert!(open.enabled);
assert_eq!(
open.shortcut,
Some(MenuShortcut {
key: LogicalKeyboardKey::KEY_O.key_id,
modifiers: ShortcutModifiers {
meta: true,
..ShortcutModifiers::default()
},
})
);
assert!(!save.enabled, "an item with no callback is disabled");
assert_eq!(*item, ProvidedItem::Quit);
let open_id = menu_bar.id_of(&["File", "Open"]).expect("Open has an id");
choose(&cell, open_id);
assert_eq!(*chosen.borrow(), ["Open"]);
}
fn choose(cell: &AppCell, id: MenuId) {
let mut app = cell.borrow_mut();
let callback = app
.platform_callbacks()
.on_menu_selected
.clone()
.expect("the default delegate hears choices");
callback(&mut app, id);
}
#[test]
fn leading_trailing_and_doubled_dividers_are_stripped() {
let menu_bar = MockMenuBar::new();
let cell = app_with_menu_bar(&menu_bar);
let item = || -> PlatformMenuItemRef { Rc::new(PlainPlatformMenuItem::new("Item")) };
let group =
|members| -> PlatformMenuItemRef { Rc::new(PlatformMenuItemGroup::new(members)) };
mount(
&cell,
PlatformMenuBar::new(vec![Rc::new(PlatformMenu::new(
"Menu",
vec![
group(vec![item()]),
group(vec![item()]),
group(vec![item()]),
],
))])
.into_widget(),
);
let [MenuEntry::Submenu { entries, .. }] = &menu_bar.menus()[..] else {
panic!("one menu");
};
let shape: Vec<bool> = entries
.iter()
.map(|entry| matches!(entry, MenuEntry::Separator))
.collect();
assert_eq!(shape, [false, true, false, true, false]);
}
#[test]
fn a_platform_without_a_menu_bar_shows_the_menus_nowhere() {
let platform = TestPlatform::new()
.on(TargetPlatform::Windows)
.with_view(Rc::new(TestView::new(800.0, 600.0)));
let cell = AppCell::with_platform(Rc::new(platform));
let chosen = Rc::new(RefCell::new(Vec::new()));
mount(&cell, PlatformMenuBar::new(menus(&chosen)).into_widget());
assert!(
!PlatformProvidedMenuItem::has_menu(&cell.borrow(), PlatformProvidedMenuItemType::Quit),
"Windows provides no Quit item"
);
}
}