Skip to main content

tauri/menu/
predefined.rs

1// Copyright 2019-2024 Tauri Programme within The Commons Conservancy
2// SPDX-License-Identifier: Apache-2.0
3// SPDX-License-Identifier: MIT
4
5use std::sync::Arc;
6
7use super::run_item_main_thread;
8use super::{AboutMetadata, PredefinedMenuItem};
9use crate::menu::PredefinedMenuItemInner;
10use crate::run_main_thread;
11use crate::{menu::MenuId, AppHandle, Manager, Runtime};
12
13impl<R: Runtime> PredefinedMenuItem<R> {
14  /// Separator menu item
15  pub fn separator<M: Manager<R>>(manager: &M) -> crate::Result<Self> {
16    let handle = manager.app_handle();
17    let app_handle = handle.clone();
18
19    let item = run_main_thread!(handle, || {
20      let item = muda::PredefinedMenuItem::separator();
21      PredefinedMenuItemInner {
22        id: item.id().clone(),
23        inner: Some(item),
24        app_handle,
25      }
26    })?;
27
28    Ok(Self(Arc::new(item)))
29  }
30
31  /// Copy menu item
32  pub fn copy<M: Manager<R>>(manager: &M, text: Option<&str>) -> crate::Result<Self> {
33    let handle = manager.app_handle();
34    let app_handle = handle.clone();
35
36    let text = text.map(|t| t.to_owned());
37
38    let item = run_main_thread!(handle, || {
39      let item = muda::PredefinedMenuItem::copy(text.as_deref());
40      PredefinedMenuItemInner {
41        id: item.id().clone(),
42        inner: Some(item),
43        app_handle,
44      }
45    })?;
46
47    Ok(Self(Arc::new(item)))
48  }
49
50  /// Cut menu item
51  pub fn cut<M: Manager<R>>(manager: &M, text: Option<&str>) -> crate::Result<Self> {
52    let handle = manager.app_handle();
53    let app_handle = handle.clone();
54
55    let text = text.map(|t| t.to_owned());
56
57    let item = run_main_thread!(handle, || {
58      let item = muda::PredefinedMenuItem::cut(text.as_deref());
59      PredefinedMenuItemInner {
60        id: item.id().clone(),
61        inner: Some(item),
62        app_handle,
63      }
64    })?;
65
66    Ok(Self(Arc::new(item)))
67  }
68
69  /// Paste menu item
70  pub fn paste<M: Manager<R>>(manager: &M, text: Option<&str>) -> crate::Result<Self> {
71    let handle = manager.app_handle();
72    let app_handle = handle.clone();
73
74    let text = text.map(|t| t.to_owned());
75
76    let item = run_main_thread!(handle, || {
77      let item = muda::PredefinedMenuItem::paste(text.as_deref());
78      PredefinedMenuItemInner {
79        id: item.id().clone(),
80        inner: Some(item),
81        app_handle,
82      }
83    })?;
84
85    Ok(Self(Arc::new(item)))
86  }
87
88  /// SelectAll menu item
89  pub fn select_all<M: Manager<R>>(manager: &M, text: Option<&str>) -> crate::Result<Self> {
90    let handle = manager.app_handle();
91    let app_handle = handle.clone();
92
93    let text = text.map(|t| t.to_owned());
94
95    let item = run_main_thread!(handle, || {
96      let item = muda::PredefinedMenuItem::select_all(text.as_deref());
97      PredefinedMenuItemInner {
98        id: item.id().clone(),
99        inner: Some(item),
100        app_handle,
101      }
102    })?;
103
104    Ok(Self(Arc::new(item)))
105  }
106
107  /// Undo menu item
108  ///
109  /// ## Platform-specific:
110  ///
111  /// - **Windows / Linux:** Unsupported.
112  pub fn undo<M: Manager<R>>(manager: &M, text: Option<&str>) -> crate::Result<Self> {
113    let handle = manager.app_handle();
114    let app_handle = handle.clone();
115
116    let text = text.map(|t| t.to_owned());
117
118    let item = run_main_thread!(handle, || {
119      let item = muda::PredefinedMenuItem::undo(text.as_deref());
120      PredefinedMenuItemInner {
121        id: item.id().clone(),
122        inner: Some(item),
123        app_handle,
124      }
125    })?;
126
127    Ok(Self(Arc::new(item)))
128  }
129  /// Redo menu item
130  ///
131  /// ## Platform-specific:
132  ///
133  /// - **Windows / Linux:** Unsupported.
134  pub fn redo<M: Manager<R>>(manager: &M, text: Option<&str>) -> crate::Result<Self> {
135    let handle = manager.app_handle();
136    let app_handle = handle.clone();
137
138    let text = text.map(|t| t.to_owned());
139
140    let item = run_main_thread!(handle, || {
141      let item = muda::PredefinedMenuItem::redo(text.as_deref());
142      PredefinedMenuItemInner {
143        id: item.id().clone(),
144        inner: Some(item),
145        app_handle,
146      }
147    })?;
148
149    Ok(Self(Arc::new(item)))
150  }
151
152  /// Minimize window menu item
153  ///
154  /// ## Platform-specific:
155  ///
156  /// - **Linux:** Unsupported.
157  pub fn minimize<M: Manager<R>>(manager: &M, text: Option<&str>) -> crate::Result<Self> {
158    let handle = manager.app_handle();
159    let app_handle = handle.clone();
160
161    let text = text.map(|t| t.to_owned());
162
163    let item = run_main_thread!(handle, || {
164      let item = muda::PredefinedMenuItem::minimize(text.as_deref());
165      PredefinedMenuItemInner {
166        id: item.id().clone(),
167        inner: Some(item),
168        app_handle,
169      }
170    })?;
171
172    Ok(Self(Arc::new(item)))
173  }
174
175  /// Maximize window menu item
176  ///
177  /// ## Platform-specific:
178  ///
179  /// - **Linux:** Unsupported.
180  pub fn maximize<M: Manager<R>>(manager: &M, text: Option<&str>) -> crate::Result<Self> {
181    let handle = manager.app_handle();
182    let app_handle = handle.clone();
183
184    let text = text.map(|t| t.to_owned());
185
186    let item = run_main_thread!(handle, || {
187      let item = muda::PredefinedMenuItem::maximize(text.as_deref());
188      PredefinedMenuItemInner {
189        id: item.id().clone(),
190        inner: Some(item),
191        app_handle,
192      }
193    })?;
194
195    Ok(Self(Arc::new(item)))
196  }
197
198  /// Fullscreen menu item
199  ///
200  /// ## Platform-specific:
201  ///
202  /// - **Windows / Linux:** Unsupported.
203  pub fn fullscreen<M: Manager<R>>(manager: &M, text: Option<&str>) -> crate::Result<Self> {
204    let handle = manager.app_handle();
205    let app_handle = handle.clone();
206
207    let text = text.map(|t| t.to_owned());
208
209    let item = run_main_thread!(handle, || {
210      let item = muda::PredefinedMenuItem::fullscreen(text.as_deref());
211      PredefinedMenuItemInner {
212        id: item.id().clone(),
213        inner: Some(item),
214        app_handle,
215      }
216    })?;
217
218    Ok(Self(Arc::new(item)))
219  }
220
221  /// Hide window menu item
222  ///
223  /// ## Platform-specific:
224  ///
225  /// - **Linux:** Unsupported.
226  pub fn hide<M: Manager<R>>(manager: &M, text: Option<&str>) -> crate::Result<Self> {
227    let handle = manager.app_handle();
228    let app_handle = handle.clone();
229
230    let text = text.map(|t| t.to_owned());
231
232    let item = run_main_thread!(handle, || {
233      let item = muda::PredefinedMenuItem::hide(text.as_deref());
234      PredefinedMenuItemInner {
235        id: item.id().clone(),
236        inner: Some(item),
237        app_handle,
238      }
239    })?;
240
241    Ok(Self(Arc::new(item)))
242  }
243
244  /// Hide other windows menu item
245  ///
246  /// ## Platform-specific:
247  ///
248  /// - **Linux:** Unsupported.
249  pub fn hide_others<M: Manager<R>>(manager: &M, text: Option<&str>) -> crate::Result<Self> {
250    let handle = manager.app_handle();
251    let app_handle = handle.clone();
252
253    let text = text.map(|t| t.to_owned());
254
255    let item = run_main_thread!(handle, || {
256      let item = muda::PredefinedMenuItem::hide_others(text.as_deref());
257      PredefinedMenuItemInner {
258        id: item.id().clone(),
259        inner: Some(item),
260        app_handle,
261      }
262    })?;
263
264    Ok(Self(Arc::new(item)))
265  }
266
267  /// Show all app windows menu item
268  ///
269  /// ## Platform-specific:
270  ///
271  /// - **Windows / Linux:** Unsupported.
272  pub fn show_all<M: Manager<R>>(manager: &M, text: Option<&str>) -> crate::Result<Self> {
273    let handle = manager.app_handle();
274    let app_handle = handle.clone();
275
276    let text = text.map(|t| t.to_owned());
277
278    let item = run_main_thread!(handle, || {
279      let item = muda::PredefinedMenuItem::show_all(text.as_deref());
280      PredefinedMenuItemInner {
281        id: item.id().clone(),
282        inner: Some(item),
283        app_handle,
284      }
285    })?;
286
287    Ok(Self(Arc::new(item)))
288  }
289
290  /// Close window menu item
291  ///
292  /// ## Platform-specific:
293  ///
294  /// - **Linux:** Unsupported.
295  pub fn close_window<M: Manager<R>>(manager: &M, text: Option<&str>) -> crate::Result<Self> {
296    let handle = manager.app_handle();
297    let app_handle = handle.clone();
298
299    let text = text.map(|t| t.to_owned());
300
301    let item = run_main_thread!(handle, || {
302      let item = muda::PredefinedMenuItem::close_window(text.as_deref());
303      PredefinedMenuItemInner {
304        id: item.id().clone(),
305        inner: Some(item),
306        app_handle,
307      }
308    })?;
309
310    Ok(Self(Arc::new(item)))
311  }
312
313  /// Quit app menu item
314  ///
315  /// ## Platform-specific:
316  ///
317  /// - **Linux:** Unsupported.
318  pub fn quit<M: Manager<R>>(manager: &M, text: Option<&str>) -> crate::Result<Self> {
319    let handle = manager.app_handle();
320    let app_handle = handle.clone();
321
322    let text = text.map(|t| t.to_owned());
323
324    let item = run_main_thread!(handle, || {
325      let item = muda::PredefinedMenuItem::quit(text.as_deref());
326      PredefinedMenuItemInner {
327        id: item.id().clone(),
328        inner: Some(item),
329        app_handle,
330      }
331    })?;
332
333    Ok(Self(Arc::new(item)))
334  }
335
336  /// About app menu item
337  pub fn about<M: Manager<R>>(
338    manager: &M,
339    text: Option<&str>,
340    metadata: Option<AboutMetadata<'_>>,
341  ) -> crate::Result<Self> {
342    let handle = manager.app_handle();
343    let app_handle = handle.clone();
344
345    let text = text.map(|t| t.to_owned());
346
347    let metadata = match metadata {
348      Some(m) => Some(m.try_into()?),
349      None => None,
350    };
351
352    let item = run_main_thread!(handle, || {
353      let item = muda::PredefinedMenuItem::about(text.as_deref(), metadata);
354      PredefinedMenuItemInner {
355        id: item.id().clone(),
356        inner: Some(item),
357        app_handle,
358      }
359    })?;
360
361    Ok(Self(Arc::new(item)))
362  }
363
364  /// Services menu item
365  ///
366  /// ## Platform-specific:
367  ///
368  /// - **Windows / Linux:** Unsupported.
369  pub fn services<M: Manager<R>>(manager: &M, text: Option<&str>) -> crate::Result<Self> {
370    let handle = manager.app_handle();
371    let app_handle = handle.clone();
372
373    let text = text.map(|t| t.to_owned());
374
375    let item = run_main_thread!(handle, || {
376      let item = muda::PredefinedMenuItem::services(text.as_deref());
377      PredefinedMenuItemInner {
378        id: item.id().clone(),
379        inner: Some(item),
380        app_handle,
381      }
382    })?;
383
384    Ok(Self(Arc::new(item)))
385  }
386
387  /// Bring All to Front menu item
388  ///
389  /// ## Platform-specific:
390  ///
391  /// - **Windows / Linux:** Unsupported.
392  pub fn bring_all_to_front<M: Manager<R>>(manager: &M, text: Option<&str>) -> crate::Result<Self> {
393    let handle = manager.app_handle();
394    let app_handle = handle.clone();
395
396    let text = text.map(|t| t.to_owned());
397
398    let item = run_main_thread!(handle, || {
399      let item = muda::PredefinedMenuItem::bring_all_to_front(text.as_deref());
400      PredefinedMenuItemInner {
401        id: item.id().clone(),
402        inner: Some(item),
403        app_handle,
404      }
405    })?;
406
407    Ok(Self(Arc::new(item)))
408  }
409
410  /// Returns a unique identifier associated with this menu item.
411  pub fn id(&self) -> &MenuId {
412    &self.0.id
413  }
414
415  /// Get the text for this menu item.
416  pub fn text(&self) -> crate::Result<String> {
417    run_item_main_thread!(self, |self_: Self| (*self_.0).as_ref().text())
418  }
419
420  /// Set the text for this menu item. `text` could optionally contain
421  /// an `&` before a character to assign this character as the mnemonic
422  /// for this menu item. To display a `&` without assigning a mnemenonic, use `&&`.
423  pub fn set_text<S: AsRef<str>>(&self, text: S) -> crate::Result<()> {
424    let text = text.as_ref().to_string();
425    run_item_main_thread!(self, |self_: Self| (*self_.0).as_ref().set_text(text))
426  }
427
428  /// The application handle associated with this type.
429  pub fn app_handle(&self) -> &AppHandle<R> {
430    &self.0.app_handle
431  }
432}