mod item;
mod item_widget;
mod presenter;
mod template;
use crate::InputDevice;
pub use item::{MenuCheckedChanged, MenuFlyoutItem, MenuFlyoutItemKind};
use presenter::{MenuFlyoutPresenter, MenuFlyoutPresenterState};
use crate::*;
use std::{fmt, rc::Rc};
use waft_embedder::Size;
use waft_foundation::{App, Handle, Listener, Ref};
use waft_services::KeyEvent;
use waft_widgets::*;
fn menu_flyout_presenter_sizes(r: &ThemeResources) -> (Size, Size) {
(
Size::new(r.flyout_theme_min_width, r.menu_flyout_theme_min_height),
Size::new(f64::INFINITY, f64::INFINITY),
)
}
pub(crate) type MenuKeyHandler =
Rc<dyn Fn(&mut App, Handle<MenuFlyout>, &KeyEvent) -> KeyEventResult>;
pub struct MenuFlyout {
flyout: Ref<Flyout>,
items: Vec<MenuFlyoutItem>,
presenter: Option<Handle<MenuFlyoutPresenterState>>,
root: Option<Handle<MenuFlyout>>,
owner_item: Option<Handle<item_widget::MenuItemState>>,
input: InputDevice,
disposed: bool,
pub(crate) key_handler: Option<MenuKeyHandler>,
}
impl fmt::Debug for MenuFlyout {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("MenuFlyout")
.field("items", &self.items)
.field("disposed", &self.disposed)
.finish_non_exhaustive()
}
}
impl MenuFlyout {
pub(crate) fn pointer_input(app: &mut App, kind: waft_embedder::PointerDeviceKind) {
InputDevice::pointer(app, kind);
}
pub fn new(app: &mut App, items: Vec<MenuFlyoutItem>) -> Ref<Self> {
Self::with_root(app, items, None)
}
fn with_root(
app: &mut App,
items: Vec<MenuFlyoutItem>,
root: Option<Handle<Self>>,
) -> Ref<Self> {
InputDevice::initialize(app);
let flyout = Flyout::new(app, SizedBox::shrink());
let owner = app.create(Self {
flyout: flyout.clone(),
items,
presenter: None,
root,
owner_item: None,
input: InputDevice::None,
disposed: false,
key_handler: None,
});
let menu = owner.handle();
let base = app.get_mut(&flyout);
base.is_sub_menu = root.is_some();
base.presenter_sizes = menu_flyout_presenter_sizes;
base.on_opening = Some(Listener::new(move |app| menu.on_opening(app)));
base.on_closed = Some(Listener::new(move |app| {
if let Some(presenter) = app.get(menu).presenter {
presenter.prepare_close(app);
}
}));
base.flyout_presenter_style = Some(Rc::new(move |_, _, _| {
MenuFlyoutPresenter { menu }.into_widget()
}));
owner
}
pub fn as_flyout(self: Handle<Self>, app: &App) -> &Ref<Flyout> {
&app.get(self).flyout
}
fn flyout(self: Handle<Self>, app: &App) -> Handle<Flyout> {
self.as_flyout(app).handle()
}
pub fn items(self: Handle<Self>, app: &mut App, items: Vec<MenuFlyoutItem>) {
assert!(
!app.get(self).disposed,
"cannot update a disposed MenuFlyout"
);
app.get_mut(self).items = items;
self.flyout(app).rebuild_content(app);
}
pub fn show_at(self: Handle<Self>, app: &mut App, target: BuildContext) {
self.flyout(app).show_at(app, target);
}
pub fn show_at_with_options(
self: Handle<Self>,
app: &mut App,
target: BuildContext,
options: FlyoutShowOptions,
) {
self.flyout(app).show_at_with_options(app, target, options);
}
pub fn hide(self: Handle<Self>, app: &mut App) {
self.flyout(app).hide(app);
}
pub fn is_open(self: Handle<Self>, app: &App) -> bool {
self.flyout(app).is_open(app)
}
pub fn dispose(self: Handle<Self>, app: &mut App) {
if app.get(self).disposed {
return;
}
app.get_mut(self).disposed = true;
self.flyout(app).dispose(app);
}
fn on_opening(self: Handle<Self>, app: &mut App) {
let input = if let Some(root) = app.get(self).root {
app.get(root).input
} else {
InputDevice::current(app)
};
app.get_mut(self).input = input;
self.flyout(app)
.menu_point_placement(app, input == InputDevice::Touch);
if let Some(presenter) = app.get(self).presenter {
presenter.prepare_open(app);
}
}
pub(crate) fn focused_item_has_children(self: Handle<Self>, app: &App) -> bool {
app.get(self).presenter.is_some_and(|presenter| {
app.get(presenter).containers.values().any(|item| {
item.focus_node(app).has_primary_focus(app)
&& matches!(&item.widget(app).item.kind, MenuFlyoutItemKind::SubItem { items } if !items.is_empty())
})
})
}
fn owner_key(self: Handle<Self>, app: &mut App, event: &KeyEvent) -> KeyEventResult {
let root = self.root(app);
app.get(root)
.key_handler
.clone()
.map_or(KeyEventResult::Ignored, |handler| handler(app, self, event))
}
fn root(self: Handle<Self>, app: &App) -> Handle<Self> {
app.get(self).root.unwrap_or(self)
}
}