1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License in the LICENSE-APACHE file or at:
// https://www.apache.org/licenses/LICENSE-2.0
//! Menu widgets
//!
//! The following serve as menu roots:
//!
//! - [`crate::ComboBox`]
//! - [`MenuBar`]
//!
//! Any implementation of the [`Menu`] trait may be used as a menu item:
//!
//! - [`SubMenu`]
//! - [`MenuEntry`]
//! - [`MenuToggle`]
//! - [`Separator`]
use crate::adapt::MapAny;
use crate::Separator;
use kas::prelude::*;
use std::fmt::Debug;
mod menu_entry;
mod menubar;
mod submenu;
pub use menu_entry::{MenuEntry, MenuToggle};
pub use menubar::{MenuBar, MenuBuilder};
pub use submenu::SubMenu;
/// Return value of [`Menu::sub_items`]
#[derive(Default)]
pub struct SubItems<'a> {
/// Primary label
pub label: Option<&'a mut dyn Layout>,
/// Secondary label, often used to show shortcut key
pub label2: Option<&'a mut dyn Layout>,
/// Sub-menu indicator
pub submenu: Option<&'a mut dyn Layout>,
/// Icon
pub icon: Option<&'a mut dyn Layout>,
/// Toggle mark
pub toggle: Option<&'a mut dyn Layout>,
}
/// Trait governing menus, sub-menus and menu-entries
///
/// Implementations will automatically receive nav focus on mouse-hover, thus
/// should ensure that [`Layout::find_id`] returns the identifier of the widget
/// which should be focussed, and that this widget has
/// [`Events::navigable`] return true.
#[autoimpl(for<T: trait + ?Sized> Box<T>)]
pub trait Menu: Widget {
/// Access row items for aligned layout
///
/// If this returns sub-items, then these items are aligned in the menu view. This involves
/// (1) calling `Self::size_rules` and `Self::set_rect` like usual, and (2) running an external
/// layout solver on these items (which also calls `size_rules` and `set_rect` on each item).
/// This is redundant, but ensures the expectations on [`Layout::size_rules`] and
/// [`Layout::set_rect`] are met.
///
/// Note further: if this returns `Some(_)`, then spacing for menu item frames is added
/// "magically" by the caller. The implementor should draw a frame as follows:
/// ```
/// # use kas::geom::Rect;
/// # use kas::theme::{DrawCx, FrameStyle};
/// # struct S;
/// # impl S {
/// # fn rect(&self) -> Rect { Rect::ZERO }
/// fn draw(&mut self, mut draw: DrawCx) {
/// draw.frame(self.rect(), FrameStyle::MenuEntry, Default::default());
/// // draw children here
/// }
/// # }
/// ```
// TODO: adding frame spacing like this is quite hacky. Find a better approach?
fn sub_items(&mut self) -> Option<SubItems> {
None
}
/// Report whether a submenu (if any) is open
///
/// By default, this is `false`.
fn menu_is_open(&self) -> bool {
false
}
/// Open or close a sub-menu, including parents
///
/// Given `Some(id) = target`, the sub-menu with this `id` should open its
/// menu; if it has child-menus, these should close; and if any ancestors
/// are menus, these should open.
///
/// `target == None` implies that all menus should close.
///
/// When opening menus and `set_focus` is true, the first navigable child
/// of the newly opened menu will be given focus. This is used for keyboard
/// navigation only.
fn set_menu_path(
&mut self,
cx: &mut EventCx,
data: &Self::Data,
target: Option<&Id>,
set_focus: bool,
) {
let _ = (cx, data, target, set_focus);
}
}
impl<A, W: Menu<Data = ()>> Menu for MapAny<A, W> {
fn sub_items(&mut self) -> Option<SubItems> {
self.inner.sub_items()
}
fn menu_is_open(&self) -> bool {
self.inner.menu_is_open()
}
fn set_menu_path(&mut self, cx: &mut EventCx, _: &A, target: Option<&Id>, set_focus: bool) {
self.inner.set_menu_path(cx, &(), target, set_focus);
}
}
/// A boxed menu
pub type BoxedMenu<Data> = Box<dyn Menu<Data = Data>>;
/// Builder for a [`SubMenu`]
///
/// Access through [`MenuBar::builder`].
pub struct SubMenuBuilder<'a, Data> {
menu: &'a mut Vec<BoxedMenu<Data>>,
}
impl<'a, Data> SubMenuBuilder<'a, Data> {
/// Append an item
#[inline]
pub fn push_item(&mut self, item: BoxedMenu<Data>) {
self.menu.push(item);
}
/// Append an item, chain style
#[inline]
pub fn item(mut self, item: BoxedMenu<Data>) -> Self {
self.push_item(item);
self
}
}
impl<'a, Data: 'static> SubMenuBuilder<'a, Data> {
/// Append a [`MenuEntry`]
pub fn push_entry<S: Into<AccessString>, M>(&mut self, label: S, msg: M)
where
M: Clone + Debug + 'static,
{
self.menu
.push(Box::new(MapAny::new(MenuEntry::new_msg(label, msg))));
}
/// Append a [`MenuEntry`], chain style
#[inline]
pub fn entry<S: Into<AccessString>, M>(mut self, label: S, msg: M) -> Self
where
M: Clone + Debug + 'static,
{
self.push_entry(label, msg);
self
}
/// Append a [`MenuToggle`]
pub fn push_toggle<M: Debug + 'static>(
&mut self,
label: impl Into<AccessString>,
state_fn: impl Fn(&ConfigCx, &Data) -> bool + 'static,
msg_fn: impl Fn(bool) -> M + 'static,
) {
self.menu
.push(Box::new(MenuToggle::new_msg(label, state_fn, msg_fn)));
}
/// Append a [`MenuToggle`], chain style
pub fn toggle<M: Debug + 'static>(
mut self,
label: impl Into<AccessString>,
state_fn: impl Fn(&ConfigCx, &Data) -> bool + 'static,
msg_fn: impl Fn(bool) -> M + 'static,
) -> Self {
self.push_toggle(label, state_fn, msg_fn);
self
}
/// Append a [`Separator`]
pub fn push_separator(&mut self) {
self.menu.push(Box::new(MapAny::new(Separator::new())));
}
/// Append a [`Separator`], chain style
#[inline]
pub fn separator(mut self) -> Self {
self.push_separator();
self
}
/// Append a [`SubMenu`]
///
/// This submenu prefers opens to the right.
pub fn push_submenu<F>(&mut self, label: impl Into<AccessString>, f: F)
where
F: FnOnce(SubMenuBuilder<Data>),
{
self.push_submenu_dir(label, f, Direction::Right);
}
/// Append a [`SubMenu`], chain style
///
/// This submenu prefers opens to the right.
pub fn submenu<F>(mut self, label: impl Into<AccessString>, f: F) -> Self
where
F: FnOnce(SubMenuBuilder<Data>),
{
self.push_submenu(label, f);
self
}
/// Append a [`SubMenu`]
///
/// This submenu prefers to open in the specified direction.
pub fn push_submenu_dir<F>(&mut self, label: impl Into<AccessString>, f: F, dir: Direction)
where
F: FnOnce(SubMenuBuilder<Data>),
{
let mut menu = Vec::new();
f(SubMenuBuilder { menu: &mut menu });
self.menu.push(Box::new(SubMenu::new(label, menu, dir)));
}
/// Append a [`SubMenu`], chain style
///
/// This submenu prefers to open in the specified direction.
#[inline]
pub fn submenu_dir<F>(mut self, label: impl Into<AccessString>, f: F, dir: Direction) -> Self
where
F: FnOnce(SubMenuBuilder<Data>),
{
self.push_submenu_dir(label, f, dir);
self
}
}