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
//! Application system menu.
use bitflags::bitflags;
use zng_txt::Txt;
use crate::image::ImageId;
/// Represents a menu command or submenu header.
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
#[non_exhaustive]
pub enum MenuItem {
/// Clickable action.
#[non_exhaustive]
Command {
/// Unique ID for this command within all menu items in the app.
///
/// If this id is empty the menu item is disabled.
id: Txt,
/// Display text.
label: Txt,
},
/// Submenu.
#[non_exhaustive]
SubMenu {
/// Display text.
label: Txt,
/// Children items.
children: Vec<MenuItem>,
},
/// Separation line.
Separator,
}
impl MenuItem {
/// New command.
pub fn command(id: impl Into<Txt>, label: impl Into<Txt>) -> Self {
Self::Command {
id: id.into(),
label: label.into(),
}
}
/// New submenu.
pub fn sub_menu(label: impl Into<Txt>, children: Vec<MenuItem>) -> Self {
Self::SubMenu {
label: label.into(),
children,
}
}
}
/// Represents a system application menu.
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
#[non_exhaustive]
pub struct AppMenu {
/// The menu items.
///
/// If empty no application menu is set.
pub children: Vec<MenuItem>,
}
impl AppMenu {
/// New.
pub fn new(children: Vec<MenuItem>) -> Self {
Self { children }
}
/// Value that represents no app menu.
pub fn none() -> Self {
Self::new(vec![])
}
}
/// Represents a *tray icon* status indicator.
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
#[non_exhaustive]
pub struct TrayIcon {
/// Icon image.
///
/// The tray icon will only be inserted when this image is valid and loaded.
pub icon: ImageId,
/// Optional context menu.
///
/// If not empty a context menu shows on context clock.
pub context_menu: Vec<MenuItem>,
/// A command ID for a primary click on the icon.
///
/// If set an [`Event::MenuCommand`] notifies on click, otherwise the context menu also opens on primary click.
///
/// [`Event::MenuCommand`]: crate::types::Event::MenuCommand
pub primary_command_id: Txt,
}
impl TrayIcon {
/// New.
pub fn new(icon: ImageId, context_menu: Vec<MenuItem>) -> Self {
Self {
icon,
context_menu,
primary_command_id: Txt::from_static(""),
}
}
/// Value that indicates no tray icon.
pub fn none() -> Self {
Self::new(ImageId::INVALID, vec![])
}
}
bitflags! {
/// System menu capability.
#[derive(Debug, Clone, Copy, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct MenuCapability: u32 {
/// View-process can set application menu items.
///
/// The application menu is shown outside the app windows, usually at the top of the main screen in macOS and Gnome desktops.
const APP_MENU = 1;
/// View-process can set tray icon with context menu.
///
/// This is a small status indicator icon displayed near the notifications area.
const TRAY_ICON = 1 << 1;
}
}