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
314
315
316
317
318
319
320
321
322
323
324
325
326
// Copyright 2022-2022 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
use std::{cell::RefCell, mem, rc::Rc, sync::Arc};
use crate::{
accelerator::{Accelerator, KeyAccelerator, MenuAccelerator},
icon::{Icon, NativeIcon},
platform_impl::PlatformMenuItem,
util, IconMenuItemBuilder, IconType, IsMenuItem, MenuId, MenuItemAction, MenuItemKind,
StateCell, TextStyle,
};
/// An icon menu item inside a [`Menu`] or [`Submenu`]
/// and usually contains an icon and a text.
///
/// [`Menu`]: crate::Menu
/// [`Submenu`]: crate::Submenu
#[derive(Clone)]
pub struct IconMenuItem {
pub(crate) id: Arc<MenuId>,
pub(crate) state: StateCell<IconMenuItemState>,
pub(crate) platform: Rc<RefCell<PlatformMenuItem>>,
}
/// Shared state of an [`IconMenuItem`].
#[derive(Debug, Clone)]
pub(crate) struct IconMenuItemState {
pub text: String,
pub enabled: bool,
pub icon: Option<IconType>,
pub accelerator: Option<MenuAccelerator>,
pub styled_text: Option<Vec<(String, TextStyle)>>,
}
impl crate::sealed::Sealed for IconMenuItem {}
impl IsMenuItem for IconMenuItem {
fn kind(&self) -> MenuItemKind {
MenuItemKind::Icon(self.clone())
}
fn id(&self) -> &MenuId {
self.id()
}
fn into_id(self) -> MenuId {
self.into_id()
}
}
impl IconMenuItem {
/// Returns a new [`IconMenuItemBuilder`].
pub fn builder() -> IconMenuItemBuilder {
IconMenuItemBuilder::new()
}
/// Create a new icon menu item.
///
/// - `text` could optionally contain an `&` before a character to assign this character as the mnemonic
/// for this icon menu item. To display a `&` without assigning a mnemenonic, use `&&`.
pub fn new<S: AsRef<str>>(
text: S,
enabled: bool,
icon: Option<Icon>,
accelerator: Option<Accelerator>,
) -> Self {
Self::new_inner(
None,
text.as_ref(),
enabled,
icon.map(IconType::Custom),
accelerator.map(MenuAccelerator::Physical),
)
}
/// Create a new icon menu item with the specified id.
///
/// - `text` could optionally contain an `&` before a character to assign this character as the mnemonic
/// for this icon menu item. To display a `&` without assigning a mnemenonic, use `&&`.
pub fn with_id<I: Into<MenuId>, S: AsRef<str>>(
id: I,
text: S,
enabled: bool,
icon: Option<Icon>,
accelerator: Option<Accelerator>,
) -> Self {
Self::new_inner(
Some(id.into()),
text.as_ref(),
enabled,
icon.map(IconType::Custom),
accelerator.map(MenuAccelerator::Physical),
)
}
/// Create a new icon menu item but with a native icon.
///
/// See [`IconMenuItem::new`] for more info.
///
/// ## Platform-specific
///
/// - **macOS**: Known variants map to AppKit image names. Use [`NativeIcon::Raw`] or
/// `NativeIcon::from_name` to pass an AppKit [`NSImage.Name`] string.
/// - **Windows**: Known variants map to stock shell icons where an equivalent exists. Use
/// [`NativeIcon::Raw`] or `NativeIcon::from_id` to pass a raw [`SHSTOCKICONID`] value.
/// - **GTK 3 / GTK 4**: Known variants map to freedesktop-style icon theme names. Use
/// [`NativeIcon::Raw`] or `NativeIcon::from_name` to pass an icon theme name resolved by
/// `GtkIconTheme` ([GTK 3][gtk3-icon-theme], [GTK 4][gtk4-icon-theme]).
///
/// [`NSImage.Name`]: https://developer.apple.com/documentation/appkit/nsimage/name-swift.typealias
/// [`SHSTOCKICONID`]: https://learn.microsoft.com/en-us/windows/win32/api/shellapi/ne-shellapi-shstockiconid
/// [`SHGetStockIconInfo`]: https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shgetstockiconinfo
/// [gtk3-icon-theme]: https://docs.gtk.org/gtk3/class.IconTheme.html
/// [gtk4-icon-theme]: https://docs.gtk.org/gtk4/class.IconTheme.html
/// [Icon Naming Specification]: https://specifications.freedesktop.org/icon-naming-spec/latest/
pub fn with_native_icon<S: AsRef<str>>(
text: S,
enabled: bool,
native_icon: Option<NativeIcon>,
accelerator: Option<Accelerator>,
) -> Self {
Self::new_inner(
None,
text.as_ref(),
enabled,
native_icon.map(IconType::Native),
accelerator.map(MenuAccelerator::Physical),
)
}
/// Create a new icon menu item but with the specified id and a native icon.
///
/// See [`IconMenuItem::new`] for more info.
///
/// ## Platform-specific
///
/// - **macOS**: Known variants map to AppKit image names. Use [`NativeIcon::Raw`] or
/// `NativeIcon::from_name` to pass an AppKit [`NSImage.Name`] string.
/// - **Windows**: Known variants map to stock shell icons where an equivalent exists. Use
/// [`NativeIcon::Raw`] or `NativeIcon::from_id` to pass a raw [`SHSTOCKICONID`] value.
/// - **GTK 3 / GTK 4**: Known variants map to freedesktop-style icon theme names. Use
/// [`NativeIcon::Raw`] or `NativeIcon::from_name` to pass an icon theme name resolved by
/// `GtkIconTheme` ([GTK 3][gtk3-icon-theme], [GTK 4][gtk4-icon-theme]).
///
/// [`NSImage.Name`]: https://developer.apple.com/documentation/appkit/nsimage/name-swift.typealias
/// [`SHSTOCKICONID`]: https://learn.microsoft.com/en-us/windows/win32/api/shellapi/ne-shellapi-shstockiconid
/// [`SHGetStockIconInfo`]: https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shgetstockiconinfo
/// [gtk3-icon-theme]: https://docs.gtk.org/gtk3/class.IconTheme.html
/// [gtk4-icon-theme]: https://docs.gtk.org/gtk4/class.IconTheme.html
/// [Icon Naming Specification]: https://specifications.freedesktop.org/icon-naming-spec/latest/
pub fn with_id_and_native_icon<I: Into<MenuId>, S: AsRef<str>>(
id: I,
text: S,
enabled: bool,
native_icon: Option<NativeIcon>,
accelerator: Option<Accelerator>,
) -> Self {
Self::new_inner(
Some(id.into()),
text.as_ref(),
enabled,
native_icon.map(IconType::Native),
accelerator.map(MenuAccelerator::Physical),
)
}
fn new_inner(
id: Option<MenuId>,
text: &str,
enabled: bool,
icon: Option<IconType>,
accelerator: Option<MenuAccelerator>,
) -> Self {
let id = util::next_id(id);
let state = IconMenuItemState {
text: text.to_string(),
enabled,
icon,
accelerator,
styled_text: None,
};
let click = MenuItemAction::Emit(id.clone());
let platform = PlatformMenuItem::new(click);
Self {
id: Arc::new(id),
state: StateCell::new(state),
platform: Rc::new(RefCell::new(platform)),
}
}
/// Returns a unique identifier associated with this submenu.
pub fn id(&self) -> &MenuId {
&self.id
}
/// Get the text for this check menu item.
pub fn text(&self) -> String {
let text = self.platform.borrow().text();
text.unwrap_or_else(|| self.state.borrow().text.clone())
}
/// Set the text for this check menu item. `text` could optionally contain
/// an `&` before a character to assign this character as the mnemonic
/// for this check menu item. To display a `&` without assigning a mnemenonic, use `&&`.
pub fn set_text<S: AsRef<str>>(&self, text: S) {
let accelerator = {
let mut state = self.state.borrow_mut();
state.text = text.as_ref().to_string();
state.styled_text = None;
state.accelerator.clone()
};
self.platform
.borrow_mut()
.set_text(text.as_ref(), accelerator.as_ref())
}
/// Set the item's label as styled parts. On Windows and Linux the parts render as plain text.
pub fn set_styled_text<S: AsRef<str>>(&self, parts: impl IntoIterator<Item = (S, TextStyle)>) {
let parts = parts
.into_iter()
.map(|(text, style)| (text.as_ref().to_string(), style))
.collect::<Vec<_>>();
let (text, accelerator) = {
let mut state = self.state.borrow_mut();
state.text = parts.iter().map(|(text, _)| text.as_str()).collect();
state.styled_text = Some(parts.clone());
(state.text.clone(), state.accelerator.clone())
};
self.platform
.borrow_mut()
.set_styled_text(&text, &parts, accelerator.as_ref())
}
/// Get whether this check menu item is enabled or not.
pub fn is_enabled(&self) -> bool {
let enabled = self.platform.borrow().is_enabled();
enabled.unwrap_or_else(|| self.state.borrow().enabled)
}
/// Enable or disable this check menu item.
pub fn set_enabled(&self, enabled: bool) {
self.state.borrow_mut().enabled = enabled;
self.platform.borrow_mut().set_enabled(enabled)
}
/// Set this icon menu item accelerator.
///
/// (Note that setting an accelerator will override any existing [.set_key_accelerator()](Self::set_key_accelerator))
pub fn set_accelerator(&self, accelerator: Option<Accelerator>) -> crate::Result<()> {
self.set_accelerator_inner(accelerator.map(MenuAccelerator::Physical))
}
/// Set this icon menu item accelerator using a [`KeyAccelerator`].
///
/// (Note that setting a key_accelerator will override any existing [.set_accelerator()](Self::set_accelerator))
pub fn set_key_accelerator(&self, accelerator: Option<KeyAccelerator>) -> crate::Result<()> {
self.set_accelerator_inner(accelerator.map(MenuAccelerator::Logical))
}
fn set_accelerator_inner(&self, accelerator: Option<MenuAccelerator>) -> crate::Result<()> {
let text = {
let mut state = self.state.borrow_mut();
state.accelerator = accelerator.clone();
state.text.clone()
};
self.platform
.borrow_mut()
.set_accelerator(&text, accelerator.as_ref())
}
/// Change this menu item icon or remove it.
///
/// (Note that setting an icon will override any existing [.set_native_icon()](Self::set_native_icon))
pub fn set_icon(&self, icon: Option<Icon>) {
let icon = {
let mut state = self.state.borrow_mut();
state.icon = icon.map(IconType::Custom);
state.icon.clone()
};
self.platform.borrow_mut().set_icon(icon.as_ref())
}
/// Change this menu item icon to a native image or remove it.
///
/// ## Platform-specific
///
/// - **macOS**: Known variants map to AppKit image names. Use [`NativeIcon::Raw`] or
/// `NativeIcon::from_name` to pass an AppKit [`NSImage.Name`] string.
/// - **Windows**: Known variants map to stock shell icons where an equivalent exists. Use
/// [`NativeIcon::Raw`] or `NativeIcon::from_id` to pass a raw [`SHSTOCKICONID`] value.
/// - **GTK 3 / GTK 4**: Known variants map to freedesktop-style icon theme names. Use
/// [`NativeIcon::Raw`] or `NativeIcon::from_name` to pass an icon theme name resolved by
/// `GtkIconTheme` ([GTK 3][gtk3-icon-theme], [GTK 4][gtk4-icon-theme]).
///
/// [`NSImage.Name`]: https://developer.apple.com/documentation/appkit/nsimage/name-swift.typealias
/// [`SHSTOCKICONID`]: https://learn.microsoft.com/en-us/windows/win32/api/shellapi/ne-shellapi-shstockiconid
/// [`SHGetStockIconInfo`]: https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shgetstockiconinfo
/// [gtk3-icon-theme]: https://docs.gtk.org/gtk3/class.IconTheme.html
/// [gtk4-icon-theme]: https://docs.gtk.org/gtk4/class.IconTheme.html
/// [Icon Naming Specification]: https://specifications.freedesktop.org/icon-naming-spec/latest/
///
/// (Note that setting a native icon will override any existing [.set_icon()](Self::set_icon))
pub fn set_native_icon(&self, icon: Option<NativeIcon>) {
let icon = {
let mut state = self.state.borrow_mut();
state.icon = icon.map(IconType::Native);
state.icon.clone()
};
self.platform.borrow_mut().set_icon(icon.as_ref())
}
/// Convert this menu item into its menu ID.
pub fn into_id(mut self) -> MenuId {
if let Some(id) = Arc::get_mut(&mut self.id) {
mem::take(id)
} else {
self.id().clone()
}
}
}