predefined.rs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // Copyright 2019-2023 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. use super::AboutMetadata;
  5. use crate::{menu::MenuId, run_main_thread, AppHandle, Manager, Runtime};
  6. /// A predefined (native) menu item which has a predfined behavior by the OS or by this crate.
  7. pub struct PredefinedMenuItem<R: Runtime> {
  8. pub(crate) id: MenuId,
  9. pub(crate) inner: muda::PredefinedMenuItem,
  10. pub(crate) app_handle: AppHandle<R>,
  11. }
  12. impl<R: Runtime> Clone for PredefinedMenuItem<R> {
  13. fn clone(&self) -> Self {
  14. Self {
  15. id: self.id.clone(),
  16. inner: self.inner.clone(),
  17. app_handle: self.app_handle.clone(),
  18. }
  19. }
  20. }
  21. /// # Safety
  22. ///
  23. /// We make sure it always runs on the main thread.
  24. unsafe impl<R: Runtime> Sync for PredefinedMenuItem<R> {}
  25. unsafe impl<R: Runtime> Send for PredefinedMenuItem<R> {}
  26. impl<R: Runtime> super::sealed::IsMenuItemBase for PredefinedMenuItem<R> {
  27. fn inner(&self) -> &dyn muda::IsMenuItem {
  28. &self.inner
  29. }
  30. }
  31. impl<R: Runtime> super::IsMenuItem<R> for PredefinedMenuItem<R> {
  32. fn kind(&self) -> super::MenuItemKind<R> {
  33. super::MenuItemKind::Predefined(self.clone())
  34. }
  35. fn id(&self) -> &MenuId {
  36. self.id()
  37. }
  38. }
  39. impl<R: Runtime> PredefinedMenuItem<R> {
  40. /// Separator menu item
  41. pub fn separator<M: Manager<R>>(manager: &M) -> Self {
  42. let inner = muda::PredefinedMenuItem::separator();
  43. Self {
  44. id: inner.id().clone(),
  45. inner,
  46. app_handle: manager.app_handle().clone(),
  47. }
  48. }
  49. /// Copy menu item
  50. pub fn copy<M: Manager<R>>(manager: &M, text: Option<&str>) -> Self {
  51. let inner = muda::PredefinedMenuItem::copy(text);
  52. Self {
  53. id: inner.id().clone(),
  54. inner,
  55. app_handle: manager.app_handle().clone(),
  56. }
  57. }
  58. /// Cut menu item
  59. pub fn cut<M: Manager<R>>(manager: &M, text: Option<&str>) -> Self {
  60. let inner = muda::PredefinedMenuItem::cut(text);
  61. Self {
  62. id: inner.id().clone(),
  63. inner,
  64. app_handle: manager.app_handle().clone(),
  65. }
  66. }
  67. /// Paste menu item
  68. pub fn paste<M: Manager<R>>(manager: &M, text: Option<&str>) -> Self {
  69. let inner = muda::PredefinedMenuItem::paste(text);
  70. Self {
  71. id: inner.id().clone(),
  72. inner,
  73. app_handle: manager.app_handle().clone(),
  74. }
  75. }
  76. /// SelectAll menu item
  77. pub fn select_all<M: Manager<R>>(manager: &M, text: Option<&str>) -> Self {
  78. let inner = muda::PredefinedMenuItem::select_all(text);
  79. Self {
  80. id: inner.id().clone(),
  81. inner,
  82. app_handle: manager.app_handle().clone(),
  83. }
  84. }
  85. /// Undo menu item
  86. ///
  87. /// ## Platform-specific:
  88. ///
  89. /// - **Windows / Linux:** Unsupported.
  90. pub fn undo<M: Manager<R>>(manager: &M, text: Option<&str>) -> Self {
  91. let inner = muda::PredefinedMenuItem::undo(text);
  92. Self {
  93. id: inner.id().clone(),
  94. inner,
  95. app_handle: manager.app_handle().clone(),
  96. }
  97. }
  98. /// Redo menu item
  99. ///
  100. /// ## Platform-specific:
  101. ///
  102. /// - **Windows / Linux:** Unsupported.
  103. pub fn redo<M: Manager<R>>(manager: &M, text: Option<&str>) -> Self {
  104. let inner = muda::PredefinedMenuItem::redo(text);
  105. Self {
  106. id: inner.id().clone(),
  107. inner,
  108. app_handle: manager.app_handle().clone(),
  109. }
  110. }
  111. /// Minimize window menu item
  112. ///
  113. /// ## Platform-specific:
  114. ///
  115. /// - **Linux:** Unsupported.
  116. pub fn minimize<M: Manager<R>>(manager: &M, text: Option<&str>) -> Self {
  117. let inner = muda::PredefinedMenuItem::minimize(text);
  118. Self {
  119. id: inner.id().clone(),
  120. inner,
  121. app_handle: manager.app_handle().clone(),
  122. }
  123. }
  124. /// Maximize window menu item
  125. ///
  126. /// ## Platform-specific:
  127. ///
  128. /// - **Linux:** Unsupported.
  129. pub fn maximize<M: Manager<R>>(manager: &M, text: Option<&str>) -> Self {
  130. let inner = muda::PredefinedMenuItem::maximize(text);
  131. Self {
  132. id: inner.id().clone(),
  133. inner,
  134. app_handle: manager.app_handle().clone(),
  135. }
  136. }
  137. /// Fullscreen menu item
  138. ///
  139. /// ## Platform-specific:
  140. ///
  141. /// - **Windows / Linux:** Unsupported.
  142. pub fn fullscreen<M: Manager<R>>(manager: &M, text: Option<&str>) -> Self {
  143. let inner = muda::PredefinedMenuItem::fullscreen(text);
  144. Self {
  145. id: inner.id().clone(),
  146. inner,
  147. app_handle: manager.app_handle().clone(),
  148. }
  149. }
  150. /// Hide window menu item
  151. ///
  152. /// ## Platform-specific:
  153. ///
  154. /// - **Linux:** Unsupported.
  155. pub fn hide<M: Manager<R>>(manager: &M, text: Option<&str>) -> Self {
  156. let inner = muda::PredefinedMenuItem::hide(text);
  157. Self {
  158. id: inner.id().clone(),
  159. inner,
  160. app_handle: manager.app_handle().clone(),
  161. }
  162. }
  163. /// Hide other windows menu item
  164. ///
  165. /// ## Platform-specific:
  166. ///
  167. /// - **Linux:** Unsupported.
  168. pub fn hide_others<M: Manager<R>>(manager: &M, text: Option<&str>) -> Self {
  169. let inner = muda::PredefinedMenuItem::hide_others(text);
  170. Self {
  171. id: inner.id().clone(),
  172. inner,
  173. app_handle: manager.app_handle().clone(),
  174. }
  175. }
  176. /// Show all app windows menu item
  177. ///
  178. /// ## Platform-specific:
  179. ///
  180. /// - **Windows / Linux:** Unsupported.
  181. pub fn show_all<M: Manager<R>>(manager: &M, text: Option<&str>) -> Self {
  182. let inner = muda::PredefinedMenuItem::show_all(text);
  183. Self {
  184. id: inner.id().clone(),
  185. inner,
  186. app_handle: manager.app_handle().clone(),
  187. }
  188. }
  189. /// Close window menu item
  190. ///
  191. /// ## Platform-specific:
  192. ///
  193. /// - **Linux:** Unsupported.
  194. pub fn close_window<M: Manager<R>>(manager: &M, text: Option<&str>) -> Self {
  195. let inner = muda::PredefinedMenuItem::show_all(text);
  196. Self {
  197. id: inner.id().clone(),
  198. inner,
  199. app_handle: manager.app_handle().clone(),
  200. }
  201. }
  202. /// Quit app menu item
  203. ///
  204. /// ## Platform-specific:
  205. ///
  206. /// - **Linux:** Unsupported.
  207. pub fn quit<M: Manager<R>>(manager: &M, text: Option<&str>) -> Self {
  208. let inner = muda::PredefinedMenuItem::quit(text);
  209. Self {
  210. id: inner.id().clone(),
  211. inner,
  212. app_handle: manager.app_handle().clone(),
  213. }
  214. }
  215. /// About app menu item
  216. pub fn about<M: Manager<R>>(
  217. manager: &M,
  218. text: Option<&str>,
  219. metadata: Option<AboutMetadata>,
  220. ) -> Self {
  221. let inner = muda::PredefinedMenuItem::about(text, metadata.map(Into::into));
  222. Self {
  223. id: inner.id().clone(),
  224. inner,
  225. app_handle: manager.app_handle().clone(),
  226. }
  227. }
  228. /// Services menu item
  229. ///
  230. /// ## Platform-specific:
  231. ///
  232. /// - **Windows / Linux:** Unsupported.
  233. pub fn services<M: Manager<R>>(manager: &M, text: Option<&str>) -> Self {
  234. let inner = muda::PredefinedMenuItem::services(text);
  235. Self {
  236. id: inner.id().clone(),
  237. inner,
  238. app_handle: manager.app_handle().clone(),
  239. }
  240. }
  241. /// Returns a unique identifier associated with this menu item.
  242. pub fn id(&self) -> &MenuId {
  243. &self.id
  244. }
  245. /// Get the text for this menu item.
  246. pub fn text(&self) -> crate::Result<String> {
  247. run_main_thread!(self, |self_: Self| self_.inner.text())
  248. }
  249. /// Set the text for this menu item. `text` could optionally contain
  250. /// an `&` before a character to assign this character as the mnemonic
  251. /// for this menu item. To display a `&` without assigning a mnemenonic, use `&&`.
  252. pub fn set_text<S: AsRef<str>>(&self, text: S) -> crate::Result<()> {
  253. let text = text.as_ref().to_string();
  254. run_main_thread!(self, |self_: Self| self_.inner.set_text(text))
  255. }
  256. /// The application handle associated with this type.
  257. pub fn app_handle(&self) -> &AppHandle<R> {
  258. &self.app_handle
  259. }
  260. }