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
// Copyright 2022-2022 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
use crate::{
accelerator::{Accelerator, KeyAccelerator, MenuAccelerator},
icon::{Icon, NativeIcon},
IconMenuItem, MenuId, TextStyle,
};
/// A builder type for [`IconMenuItem`]
#[derive(Clone, Debug)]
pub struct IconMenuItemBuilder {
text: String,
enabled: bool,
id: Option<MenuId>,
accelerator: Option<MenuAccelerator>,
icon: Option<Icon>,
native_icon: Option<NativeIcon>,
styled_text: Option<Vec<(String, TextStyle)>>,
}
impl Default for IconMenuItemBuilder {
fn default() -> Self {
Self {
text: String::new(),
enabled: true,
id: None,
accelerator: None,
icon: None,
native_icon: None,
styled_text: None,
}
}
}
impl IconMenuItemBuilder {
pub fn new() -> Self {
Default::default()
}
/// Set the id this icon menu item.
pub fn id(mut self, id: MenuId) -> Self {
self.id.replace(id);
self
}
/// Set the text for this icon menu item.
///
/// See [`IconMenuItem::set_text`] for more info.
pub fn text<S: Into<String>>(mut self, text: S) -> Self {
self.text = text.into();
self
}
/// Enable or disable this menu item.
pub fn enabled(mut self, enabled: bool) -> Self {
self.enabled = enabled;
self
}
/// Set this icon menu item icon.
pub fn icon(mut self, icon: Option<Icon>) -> Self {
self.icon = icon;
self.native_icon = None;
self
}
/// Set this icon menu item native icon.
///
/// ## 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 native_icon(mut self, icon: Option<NativeIcon>) -> Self {
self.native_icon = icon;
self.icon = None;
self
}
/// Set this icon menu item accelerator.
///
/// (Note that setting an accelerator will override any existing [.key_accelerator()](Self::key_accelerator))
pub fn accelerator<A: TryInto<Accelerator>>(
mut self,
accelerator: Option<A>,
) -> crate::Result<Self>
where
crate::Error: From<<A as TryInto<Accelerator>>::Error>,
{
self.accelerator = accelerator
.map(|a| a.try_into().map(MenuAccelerator::Physical))
.transpose()?;
Ok(self)
}
/// Set this icon menu item accelerator using a [`KeyAccelerator`].
///
/// (Note that setting a key_accelerator will override any existing [.accelerator()](Self::accelerator))
pub fn key_accelerator<A: TryInto<KeyAccelerator>>(
mut self,
accelerator: Option<A>,
) -> crate::Result<Self>
where
crate::Error: From<<A as TryInto<KeyAccelerator>>::Error>,
{
self.accelerator = accelerator
.map(|a| a.try_into().map(MenuAccelerator::Logical))
.transpose()?;
Ok(self)
}
/// Set the text for this menu item as a sequence of styled text, so one part of the
/// label can be de-emphasized relative to the rest.
///
/// Overrides any text set with [`.text()`](Self::text).
///
/// See [`IconMenuItem::set_styled_text`] for more info.
pub fn styled_text<S: Into<String>>(
mut self,
parts: impl IntoIterator<Item = (S, TextStyle)>,
) -> Self {
self.styled_text = Some(
parts
.into_iter()
.map(|(text, style)| (text.into(), style))
.collect(),
);
self
}
/// Build this icon menu item.
pub fn build(self) -> IconMenuItem {
let item = if let Some(id) = self.id {
if self.icon.is_some() {
IconMenuItem::with_id(id, self.text, self.enabled, self.icon, None)
} else {
IconMenuItem::with_id_and_native_icon(
id,
self.text,
self.enabled,
self.native_icon,
None,
)
}
} else if self.icon.is_some() {
IconMenuItem::new(self.text, self.enabled, self.icon, None)
} else {
IconMenuItem::with_native_icon(self.text, self.enabled, self.native_icon, None)
};
if let Some(accelerator) = self.accelerator {
let _ = match accelerator {
MenuAccelerator::Physical(accelerator) => item.set_accelerator(Some(accelerator)),
MenuAccelerator::Logical(accelerator) => {
item.set_key_accelerator(Some(accelerator))
}
};
}
if let Some(parts) = self.styled_text {
item.set_styled_text(parts);
}
item
}
}