1use std::{cell::RefCell, mem, rc::Rc};
6
7use crate::{
8 accelerator::Accelerator,
9 icon::{Icon, NativeIcon},
10 sealed::IsMenuItemBase,
11 IsMenuItem, MenuId, MenuItemKind,
12};
13
14#[derive(Clone)]
20pub struct IconMenuItem {
21 pub(crate) id: Rc<MenuId>,
22 pub(crate) inner: Rc<RefCell<crate::platform_impl::MenuChild>>,
23}
24
25impl IsMenuItemBase for IconMenuItem {}
26impl IsMenuItem for IconMenuItem {
27 fn kind(&self) -> MenuItemKind {
28 MenuItemKind::Icon(self.clone())
29 }
30
31 fn id(&self) -> &MenuId {
32 self.id()
33 }
34
35 fn into_id(self) -> MenuId {
36 self.into_id()
37 }
38}
39
40impl IconMenuItem {
41 pub fn new<S: AsRef<str>>(
46 text: S,
47 enabled: bool,
48 icon: Option<Icon>,
49 accelerator: Option<Accelerator>,
50 ) -> Self {
51 let item = crate::platform_impl::MenuChild::new_icon(
52 text.as_ref(),
53 enabled,
54 icon,
55 accelerator,
56 None,
57 );
58 Self {
59 id: Rc::new(item.id().clone()),
60 inner: Rc::new(RefCell::new(item)),
61 }
62 }
63
64 pub fn with_id<I: Into<MenuId>, S: AsRef<str>>(
69 id: I,
70 text: S,
71 enabled: bool,
72 icon: Option<Icon>,
73 accelerator: Option<Accelerator>,
74 ) -> Self {
75 let id = id.into();
76 Self {
77 id: Rc::new(id.clone()),
78 inner: Rc::new(RefCell::new(crate::platform_impl::MenuChild::new_icon(
79 text.as_ref(),
80 enabled,
81 icon,
82 accelerator,
83 Some(id),
84 ))),
85 }
86 }
87
88 pub fn with_native_icon<S: AsRef<str>>(
96 text: S,
97 enabled: bool,
98 native_icon: Option<NativeIcon>,
99 accelerator: Option<Accelerator>,
100 ) -> Self {
101 let item = crate::platform_impl::MenuChild::new_native_icon(
102 text.as_ref(),
103 enabled,
104 native_icon,
105 accelerator,
106 None,
107 );
108 Self {
109 id: Rc::new(item.id().clone()),
110 inner: Rc::new(RefCell::new(item)),
111 }
112 }
113
114 pub fn with_id_and_native_icon<I: Into<MenuId>, S: AsRef<str>>(
122 id: I,
123 text: S,
124 enabled: bool,
125 native_icon: Option<NativeIcon>,
126 accelerator: Option<Accelerator>,
127 ) -> Self {
128 let id = id.into();
129 Self {
130 id: Rc::new(id.clone()),
131 inner: Rc::new(RefCell::new(
132 crate::platform_impl::MenuChild::new_native_icon(
133 text.as_ref(),
134 enabled,
135 native_icon,
136 accelerator,
137 Some(id),
138 ),
139 )),
140 }
141 }
142
143 pub fn id(&self) -> &MenuId {
145 &self.id
146 }
147
148 pub fn text(&self) -> String {
150 self.inner.borrow().text()
151 }
152
153 pub fn set_text<S: AsRef<str>>(&self, text: S) {
157 self.inner.borrow_mut().set_text(text.as_ref())
158 }
159
160 pub fn is_enabled(&self) -> bool {
162 self.inner.borrow().is_enabled()
163 }
164
165 pub fn set_enabled(&self, enabled: bool) {
167 self.inner.borrow_mut().set_enabled(enabled)
168 }
169
170 pub fn set_accelerator(&self, accelerator: Option<Accelerator>) -> crate::Result<()> {
172 self.inner.borrow_mut().set_accelerator(accelerator)
173 }
174
175 pub fn set_icon(&self, icon: Option<Icon>) {
177 self.inner.borrow_mut().set_icon(icon)
178 }
179
180 pub fn set_native_icon(&self, _icon: Option<NativeIcon>) {
186 #[cfg(target_os = "macos")]
187 self.inner.borrow_mut().set_native_icon(_icon)
188 }
189
190 pub fn into_id(mut self) -> MenuId {
192 if let Some(id) = Rc::get_mut(&mut self.id) {
194 mem::take(id)
195 } else {
196 self.id().clone()
197 }
198 }
199}