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
use std::{cell::RefCell, mem, rc::Rc};
use crate::{
dpi::Position, sealed::IsMenuItemBase, util::AddOp, ContextMenu, IsMenuItem, MenuId,
MenuItemKind,
};
/// A menu that can be added to a [`Menu`] or another [`Submenu`].
///
/// [`Menu`]: crate::Menu
#[derive(Clone)]
pub struct Submenu {
pub(crate) id: Rc<MenuId>,
pub(crate) inner: Rc<RefCell<crate::platform_impl::MenuChild>>,
}
impl IsMenuItemBase for Submenu {}
impl IsMenuItem for Submenu {
fn kind(&self) -> MenuItemKind {
MenuItemKind::Submenu(self.clone())
}
fn id(&self) -> &MenuId {
self.id()
}
fn into_id(self) -> MenuId {
self.into_id()
}
}
impl Submenu {
/// Create a new submenu.
///
/// - `text` could optionally contain an `&` before a character to assign this character as the mnemonic
/// for this submenu. To display a `&` without assigning a mnemenonic, use `&&`.
pub fn new<S: AsRef<str>>(text: S, enabled: bool) -> Self {
let submenu = crate::platform_impl::MenuChild::new_submenu(text.as_ref(), enabled, None);
Self {
id: Rc::new(submenu.id().clone()),
inner: Rc::new(RefCell::new(submenu)),
}
}
/// Create a new submenu with the specified id.
///
/// - `text` could optionally contain an `&` before a character to assign this character as the mnemonic
/// for this submenu. To display a `&` without assigning a mnemenonic, use `&&`.
pub fn with_id<I: Into<MenuId>, S: AsRef<str>>(id: I, text: S, enabled: bool) -> Self {
let id = id.into();
Self {
id: Rc::new(id.clone()),
inner: Rc::new(RefCell::new(crate::platform_impl::MenuChild::new_submenu(
text.as_ref(),
enabled,
Some(id),
))),
}
}
/// Creates a new submenu with given `items`. It calls [`Submenu::new`] and [`Submenu::append_items`] internally.
pub fn with_items<S: AsRef<str>>(
text: S,
enabled: bool,
items: &[&dyn IsMenuItem],
) -> crate::Result<Self> {
let menu = Self::new(text, enabled);
menu.append_items(items)?;
Ok(menu)
}
/// Creates a new submenu with the specified id and given `items`. It calls [`Submenu::new`] and [`Submenu::append_items`] internally.
pub fn with_id_and_items<I: Into<MenuId>, S: AsRef<str>>(
id: I,
text: S,
enabled: bool,
items: &[&dyn IsMenuItem],
) -> crate::Result<Self> {
let menu = Self::with_id(id, text, enabled);
menu.append_items(items)?;
Ok(menu)
}
/// Returns a unique identifier associated with this submenu.
pub fn id(&self) -> &MenuId {
&self.id
}
/// Add a menu item to the end of this menu.
pub fn append(&self, item: &dyn IsMenuItem) -> crate::Result<()> {
self.inner.borrow_mut().add_menu_item(item, AddOp::Append)
}
/// Add menu items to the end of this submenu. It calls [`Submenu::append`] in a loop.
pub fn append_items(&self, items: &[&dyn IsMenuItem]) -> crate::Result<()> {
for item in items {
self.append(*item)?
}
Ok(())
}
/// Add a menu item to the beginning of this submenu.
pub fn prepend(&self, item: &dyn IsMenuItem) -> crate::Result<()> {
self.inner
.borrow_mut()
.add_menu_item(item, AddOp::Insert(0))
}
/// Add menu items to the beginning of this submenu.
/// It calls [`Menu::prepend`](crate::Menu::prepend) on the first element and
/// passes the rest to [`Menu::insert_items`](crate::Menu::insert_items) with position of `1`.
pub fn prepend_items(&self, items: &[&dyn IsMenuItem]) -> crate::Result<()> {
self.insert_items(items, 0)
}
/// Insert a menu item at the specified `postion` in the submenu.
pub fn insert(&self, item: &dyn IsMenuItem, position: usize) -> crate::Result<()> {
self.inner
.borrow_mut()
.add_menu_item(item, AddOp::Insert(position))
}
/// Insert menu items at the specified `postion` in the submenu.
pub fn insert_items(&self, items: &[&dyn IsMenuItem], position: usize) -> crate::Result<()> {
for (i, item) in items.iter().enumerate() {
self.insert(*item, position + i)?
}
Ok(())
}
/// Remove a menu item from this submenu.
pub fn remove(&self, item: &dyn IsMenuItem) -> crate::Result<()> {
self.inner.borrow_mut().remove(item)
}
/// Remove the menu item at the specified position from this submenu and returns it.
pub fn remove_at(&self, position: usize) -> Option<MenuItemKind> {
let mut items = self.items();
if items.len() > position {
let item = items.remove(position);
let _ = self.remove(item.as_ref());
Some(item)
} else {
None
}
}
/// Returns a list of menu items that has been added to this submenu.
pub fn items(&self) -> Vec<MenuItemKind> {
self.inner.borrow().items()
}
/// Get the text for this submenu.
pub fn text(&self) -> String {
self.inner.borrow().text()
}
/// Set the text for this submenu. `text` could optionally contain
/// an `&` before a character to assign this character as the mnemonic
/// for this submenu. To display a `&` without assigning a mnemenonic, use `&&`.
pub fn set_text<S: AsRef<str>>(&self, text: S) {
self.inner.borrow_mut().set_text(text.as_ref())
}
/// Get whether this submenu is enabled or not.
pub fn is_enabled(&self) -> bool {
self.inner.borrow().is_enabled()
}
/// Enable or disable this submenu.
pub fn set_enabled(&self, enabled: bool) {
self.inner.borrow_mut().set_enabled(enabled)
}
/// Convert this submenu into its menu ID.
pub fn into_id(mut self) -> MenuId {
// Note: `Rc::into_inner` is available from Rust 1.70
if let Some(id) = Rc::get_mut(&mut self.id) {
mem::take(id)
} else {
self.id().clone()
}
}
}
impl ContextMenu for Submenu {
fn hpopupmenu(&self) -> isize {
self.inner.borrow().hpopupmenu()
}
unsafe fn show_context_menu_for_hwnd(&self, hwnd: isize, position: Option<Position>) -> bool {
self.inner
.borrow_mut()
.show_context_menu_for_hwnd(hwnd, position)
}
unsafe fn attach_menu_subclass_for_hwnd(&self, hwnd: isize) {
self.inner.borrow().attach_menu_subclass_for_hwnd(hwnd)
}
unsafe fn detach_menu_subclass_from_hwnd(&self, hwnd: isize) {
self.inner.borrow().detach_menu_subclass_from_hwnd(hwnd)
}
}