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 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
//! This module contains a platform independent application menu implementation.
use crate::{
platform::{MenuApi, MenuItemApi, Wrapper},
platform_impl::{MenuImpl, MenuItemImpl},
ContextOwner,
Icon,
};
/// Menu item action.
#[derive(Debug)]
pub enum Action {
/// This variant will send an event with the specified action name into the
/// event loop.
///
/// Use this variant when you want to capture menu event by an event
/// handler.
Event(String),
/// An action callback for a menu item.
Callback(fn()),
}
impl Action {
/// Creates a new action of the event type.
///
/// # Parameters:
/// * `name` - Action name.
pub fn new_event<S>(name: S) -> Self
where
S: Into<String>,
{
Self::Event(name.into())
}
/// Creates a new action callback.
///
/// # Parameters:
/// * `callback` - Action callback.
pub fn new_callback(callback: fn()) -> Self { Self::Callback(callback) }
}
/// This structure represents short codes (menu hotkeys) for different
/// platforms.
#[derive(Debug, Default)]
pub struct ShortCode {
pub(crate) macos: Option<String>,
}
impl ShortCode {
/// Returns a short code for macOS platform or `None`.
pub fn macos_code(&self) -> Option<&String> { self.macos.as_ref() }
}
/// Application menu item.
#[derive(Debug)]
pub struct MenuItem(MenuItemImpl);
impl MenuItem {
/// Returns a new builder instance.
pub fn builder() -> MenuItemBuilder { MenuItemBuilder::new() }
fn new(ctx: &impl ContextOwner) -> Self { Self(MenuItemImpl::new(ctx, false)) }
/// Creates a new menu separator.
///
/// # Parameters:
/// * `ctx` - ContextOnwer
pub fn separator(ctx: &impl ContextOwner) -> Self { Self(MenuItemImpl::new(ctx, true)) }
}
impl MenuItem {
/// Sets a new menu item title.
///
/// # Parameters:
/// * `title` - Title.
pub fn set_title<S>(&mut self, title: S)
where
S: Into<String>,
{
self.0.set_title(title.into());
}
/// Returns a menu item title.
pub fn title(&self) -> String { self.0.title() }
/// Sets a menu item action.
///
/// # Parameters:
/// * `action` - Menu item action.
pub fn set_action(&mut self, action: Option<Action>) { self.0.set_action(action); }
/// Sets a submenu.
///
/// # Parameters:
/// * `submenu` - Submenu of the current menu item.
pub fn set_submenu(&mut self, submenu: Option<Menu>) { self.0.set_submenu(submenu); }
/// Returns a submenu.
pub fn submenu(&self) -> Option<&Menu> { self.0.submenu() }
/// Returns a mutable submenu.
pub fn submenu_mut(&mut self) -> Option<&mut Menu> { self.0.submenu_mut() }
/// Checks if a menu item has a submenu.
pub fn has_submenu(&self) -> bool { self.0.has_submenu() }
/// Sets short codes for different platforms.
///
/// # Parameters:
/// * `short_code` - Short codes.
pub fn set_short_code(&mut self, short_code: ShortCode) { self.0.set_short_code(short_code); }
/// Returns short codes for different platforms.
pub fn short_code(&self) -> &ShortCode { self.0.short_code() }
/// Turns on/off a menu item.
///
/// # Parameters:
/// * `enabled` - Enable flag.
pub fn set_enabled(&mut self, enabled: bool) { self.0.set_enabled(enabled); }
/// Returns if a menu item is turned on/off.
pub fn enabled(&self) -> bool { self.0.enabled() }
/// Sets a tooltip for the menu item.
///
/// # Parameters:
/// * `tooltip` - Tooltip message.
pub fn set_tooltip(&mut self, tooltip: Option<String>) { self.0.set_tooltip(tooltip); }
/// Returns a tooltip of the menu item.
pub fn tooltip(&self) -> Option<String> { self.0.tooltip() }
/// Sets a menu item icon.
///
/// # Parameters:
/// * `icon` - Menu item icon.
pub fn set_icon(&mut self, icon: Option<Icon>) { self.0.set_icon(icon); }
/// Returns a menu item icon.
pub fn icon(&self) -> Option<&Icon> { self.0.icon() }
}
impl Wrapper<MenuItemImpl> for MenuItem {
#[inline]
fn get_impl(&self) -> &MenuItemImpl { &self.0 }
#[inline]
fn get_impl_mut(&mut self) -> &mut MenuItemImpl { &mut self.0 }
}
/// Menu item builder.
#[derive(Debug, Default)]
pub struct MenuItemBuilder {
title: Option<String>,
action: Option<Action>,
submenu: Option<Menu>,
short_code: ShortCode,
enabled: Option<bool>,
icon: Option<Icon>,
}
impl MenuItemBuilder {
fn new() -> Self {
Self {
..Default::default()
}
}
/// Sets a title for the item under building.
///
/// # Parameters:
/// * `title` - Title.
pub fn with_title<S>(mut self, title: S) -> MenuItemBuilder
where
S: Into<String>,
{
self.title = Some(title.into());
self
}
/// Sets an action for the item under building.
///
/// # Parameters:
/// * `action` - Action.
pub fn with_action(mut self, action: Action) -> MenuItemBuilder {
self.action = Some(action);
self
}
/// Sets a submenu for the item under building.
///
/// # Parameters:
/// * `submenu` - Menu item's submenu.
pub fn with_submenu(mut self, submenu: Menu) -> MenuItemBuilder {
self.submenu = Some(submenu);
self
}
/// Sets short codes for the item under building.
///
/// # Parameters:
/// * `short_code` - Short codes.
pub fn with_macos_short_code<S>(mut self, short_code: S) -> MenuItemBuilder
where
S: Into<String>,
{
self.short_code.macos = Some(short_code.into());
self
}
/// Turns on/off the item under building.
///
/// # Parameters:
/// * `enabled` - Enable flag.
pub fn with_enabled(mut self, enabled: bool) -> MenuItemBuilder {
self.enabled = Some(enabled);
self
}
/// Sets an icon for the item under building.
///
/// # Parameters:
/// * `icon` - Menu item icon.
pub fn with_icon(mut self, icon: Icon) -> MenuItemBuilder {
self.icon = Some(icon);
self
}
/// Build a new menu item with specified options.
///
/// # Parameters:
/// * `ctx` - Context owner.
pub fn build(self, ctx: &impl ContextOwner) -> MenuItem {
let mut item = MenuItem::new(ctx);
if let Some(title) = self.title {
item.set_title(title);
}
if self.action.is_some() {
item.set_action(self.action);
}
if self.submenu.is_some() {
item.set_submenu(self.submenu);
}
item.set_short_code(self.short_code);
if let Some(enabled) = self.enabled {
item.set_enabled(enabled);
}
if self.icon.is_some() {
item.set_icon(self.icon);
}
item
}
}
/// Application menu/submenu.
#[derive(Debug)]
pub struct Menu(MenuImpl);
impl Menu {
/// Returns a new builder instance.
pub fn builder() -> MenuBuilder { MenuBuilder::new() }
fn new(ctx: &impl ContextOwner, items: Vec<MenuItem>) -> Self {
Self(MenuImpl::new(ctx, items))
}
}
impl Wrapper<MenuImpl> for Menu {
#[inline]
fn get_impl(&self) -> &MenuImpl { &self.0 }
#[inline]
fn get_impl_mut(&mut self) -> &mut MenuImpl { &mut self.0 }
}
/// Menu item builder.
#[derive(Debug)]
pub struct MenuBuilder {
items: Vec<MenuItem>,
}
impl MenuBuilder {
#[inline]
fn new() -> Self {
Self {
items: Vec::new()
}
}
/// Add a new item to the menu under building.
///
/// # Parameters:
/// * `item` - Menu item.
pub fn with_item(mut self, item: MenuItem) -> MenuBuilder {
self.items.push(item);
self
}
/// Build a new menu with registered items.
///
/// # Parameters:
/// * `ctx` - Context owner.
pub fn build(self, ctx: &impl ContextOwner) -> Menu { Menu::new(ctx, self.items) }
}