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
use crate::{
runtime::{menu::MenuUpdate, Dispatch, MenuId, Runtime},
Params,
};
use std::collections::HashMap;
#[cfg_attr(doc_cfg, doc(cfg(feature = "menu")))]
#[derive(Debug, Clone)]
pub struct MenuEvent<I: MenuId> {
pub(crate) menu_item_id: I,
}
#[cfg(feature = "menu")]
impl<I: MenuId> MenuEvent<I> {
pub fn menu_item_id(&self) -> &I {
&self.menu_item_id
}
}
crate::manager::default_args! {
pub struct MenuHandle<P: Params> {
pub(crate) ids: HashMap<u16, P::MenuId>,
pub(crate) dispatcher: <P::Runtime as Runtime>::Dispatcher,
}
}
impl<P: Params> Clone for MenuHandle<P> {
fn clone(&self) -> Self {
Self {
ids: self.ids.clone(),
dispatcher: self.dispatcher.clone(),
}
}
}
crate::manager::default_args! {
pub struct MenuItemHandle<P: Params> {
id: u16,
dispatcher: <P::Runtime as Runtime>::Dispatcher,
}
}
impl<P: Params> Clone for MenuItemHandle<P> {
fn clone(&self) -> Self {
Self {
id: self.id,
dispatcher: self.dispatcher.clone(),
}
}
}
impl<P: Params> MenuHandle<P> {
pub fn get_item(&self, id: &P::MenuId) -> MenuItemHandle<P> {
for (raw, item_id) in self.ids.iter() {
if item_id == id {
return MenuItemHandle {
id: *raw,
dispatcher: self.dispatcher.clone(),
};
}
}
panic!("item id not found")
}
pub fn show(&self) -> crate::Result<()> {
self.dispatcher.show_menu().map_err(Into::into)
}
pub fn hide(&self) -> crate::Result<()> {
self.dispatcher.hide_menu().map_err(Into::into)
}
pub fn is_visible(&self) -> crate::Result<bool> {
self.dispatcher.is_menu_visible().map_err(Into::into)
}
pub fn toggle(&self) -> crate::Result<()> {
if self.is_visible()? {
self.hide()
} else {
self.show()
}
}
}
impl<P: Params> MenuItemHandle<P> {
pub fn set_enabled(&self, enabled: bool) -> crate::Result<()> {
self
.dispatcher
.update_menu_item(self.id, MenuUpdate::SetEnabled(enabled))
.map_err(Into::into)
}
pub fn set_title<S: Into<String>>(&self, title: S) -> crate::Result<()> {
self
.dispatcher
.update_menu_item(self.id, MenuUpdate::SetTitle(title.into()))
.map_err(Into::into)
}
pub fn set_selected(&self, selected: bool) -> crate::Result<()> {
self
.dispatcher
.update_menu_item(self.id, MenuUpdate::SetSelected(selected))
.map_err(Into::into)
}
#[cfg(target_os = "macos")]
#[cfg_attr(doc_cfg, doc(cfg(target_os = "macos")))]
pub fn set_native_image(&self, image: crate::NativeImage) -> crate::Result<()> {
self
.dispatcher
.update_menu_item(self.id, MenuUpdate::SetNativeImage(image))
.map_err(Into::into)
}
}