Skip to content

Commit 800ace7

Browse files
s00dvasfvitorLegend-Master
authored
Add comprehensive system tray menu and submenu icon examples to documentation (#3393)
Co-authored-by: Vitor Ayres <[email protected]> Co-authored-by: Tony <[email protected]>
1 parent 48f4844 commit 800ace7

File tree

3 files changed

+87
-13
lines changed

3 files changed

+87
-13
lines changed

src/content/docs/learn/system-tray.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,8 @@ See [`TrayIconEvent`][rust TrayIconEvent] for more information on the event type
308308
</TabItem>
309309
</Tabs>
310310

311+
For detailed information about creating menus, including menu items, submenus, and dynamic updates, see the [Window Menu](/learn/window-menu/) documentation.
312+
311313
[`TrayIcon.new`]: /reference/javascript/api/namespacetray/#new
312314
[`TrayIconOptions`]: /reference/javascript/api/namespacetray/#trayiconoptions
313315
[`TrayIconBuilder`]: https://docs.rs/tauri/2.0.0/tauri/tray/struct.TrayIconBuilder.html

src/content/docs/learn/window-customization.mdx

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,19 +121,34 @@ Put this at the top of your `<body>` tag:
121121
<div class="controls">
122122
<button id="titlebar-minimize" title="minimize">
123123
<!-- https://api.iconify.design/mdi:window-minimize.svg -->
124-
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
124+
<svg
125+
xmlns="http://www.w3.org/2000/svg"
126+
width="24"
127+
height="24"
128+
viewBox="0 0 24 24"
129+
>
125130
<path fill="currentColor" d="M19 13H5v-2h14z" />
126131
</svg>
127132
</button>
128133
<button id="titlebar-maximize" title="maximize">
129134
<!-- https://api.iconify.design/mdi:window-maximize.svg -->
130-
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
135+
<svg
136+
xmlns="http://www.w3.org/2000/svg"
137+
width="24"
138+
height="24"
139+
viewBox="0 0 24 24"
140+
>
131141
<path fill="currentColor" d="M4 4h16v16H4zm2 4v10h12V8z" />
132142
</svg>
133143
</button>
134144
<button id="titlebar-close" title="close">
135145
<!-- https://api.iconify.design/mdi:close.svg -->
136-
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
146+
<svg
147+
xmlns="http://www.w3.org/2000/svg"
148+
width="24"
149+
height="24"
150+
viewBox="0 0 24 24"
151+
>
137152
<path
138153
fill="currentColor"
139154
d="M13.46 12L19 17.54V19h-1.46L12 13.46L6.46 19H5v-1.46L10.54 12L5 6.46V5h1.46L12 10.54L17.54 5H19v1.46z"

src/content/docs/learn/window-menu.mdx

Lines changed: 67 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Native application menus can be attached to both to a window or system tray. Ava
1111

1212
## Creating a base-level menu
1313

14-
To create a base-level native window menu, and attach to a window:
14+
To create a base-level native window menu, and attach to a window. You can create various types of menu items including basic items, check items, and separators:
1515

1616
<Tabs>
1717
<TabItem label="JavaScript">
@@ -30,13 +30,36 @@ const menu = await Menu.new({
3030
console.log('quit pressed');
3131
},
3232
},
33+
{
34+
id: 'check_item',
35+
text: 'Check Item',
36+
checked: true,
37+
},
38+
{
39+
type: 'Separator',
40+
},
41+
{
42+
id: 'disabled_item',
43+
text: 'Disabled Item',
44+
enabled: false,
45+
},
46+
{
47+
id: 'status',
48+
text: 'Status: Processing...',
49+
},
3350
],
3451
});
3552

3653
// If a window was not created with an explicit menu or had one set explicitly,
3754
// this menu will be assigned to it.
38-
menu.setAsAppMenu().then((res) => {
55+
menu.setAsAppMenu().then(async (res) => {
3956
console.log('menu set success', res);
57+
58+
// Update individual menu item text
59+
const statusItem = await menu.get('status');
60+
if (statusItem) {
61+
await statusItem.setText('Status: Ready');
62+
}
4063
});
4164
```
4265

@@ -45,21 +68,32 @@ menu.setAsAppMenu().then((res) => {
4568
<TabItem label="Rust">
4669

4770
```rust
48-
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
49-
use tauri::menu::{MenuBuilder};
71+
use tauri::menu::MenuBuilder;
5072

5173
fn main() {
52-
tauri::Builder::default()
74+
tauri::Builder::default()
5375
.setup(|app| {
5476
let menu = MenuBuilder::new(app)
5577
.text("open", "Open")
5678
.text("close", "Close")
79+
.check("check_item", "Check Item")
80+
.separator()
81+
.text("disabled_item", "Disabled Item")
82+
.text("status", "Status: Processing...")
5783
.build()?;
5884

59-
app.set_menu(menu)?;
85+
app.set_menu(menu.clone())?;
86+
87+
// Update individual menu item text
88+
menu
89+
.get("status")
90+
.unwrap()
91+
.as_menuitem_unchecked()
92+
.set_text("Status: Ready")?;
6093

6194
Ok(())
6295
})
96+
.run(tauri::generate_context!());
6397
}
6498
```
6599

@@ -147,6 +181,10 @@ Multi-level menus allow you to group menu items under categories like "File," "E
147181

148182
**Note:** When using submenus on MacOS, all items must be grouped under a submenu. Top-level items will be ignored. Additionally, the first submenu will be placed under the application's about menu by default, regardless of the `text` label. You should include a submenu as the first entry (say, an "About" submenu) to fill this space.
149183

184+
:::note
185+
Icon support for submenus is available since Tauri 2.8.0.
186+
:::
187+
150188
<Tabs>
151189
<TabItem label="JavaScript">
152190

@@ -169,6 +207,7 @@ const aboutSubmenu = await Submenu.new({
169207

170208
const fileSubmenu = await Submenu.new({
171209
text: 'File',
210+
icon: 'folder', // Optional: Add an icon to the submenu
172211
items: [
173212
await MenuItem.new({
174213
id: 'new',
@@ -219,19 +258,29 @@ const menu = await Menu.new({
219258
});
220259

221260
menu.setAsAppMenu();
261+
262+
// You can also update the submenu icon dynamically
263+
fileSubmenu.setIcon('document');
264+
// Or set a native icon (only one type applies per platform)
265+
fileSubmenu.setNativeIcon('NSFolder');
222266
```
223267

224268
</TabItem>
225269

226270
<TabItem label="Rust">
227271

228272
```rust
229-
use tauri::menu::{CheckMenuItemBuilder, MenuBuilder, SubmenuBuilder};
273+
use tauri::{
274+
image::Image,
275+
menu::{CheckMenuItemBuilder, IconMenuItemBuilder, MenuBuilder, SubmenuBuilder},
276+
};
230277

231278
fn main() {
232-
tauri::Builder::default()
279+
tauri::Builder::default()
233280
.setup(|app| {
281+
let menu_image = Image::from_bytes(include_bytes!("../icons/menu.png")).unwrap();
234282
let file_menu = SubmenuBuilder::new(app, "File")
283+
.submenu_icon(menu_image)) // Optional: Add an icon to the submenu
235284
.text("open", "Open")
236285
.text("quit", "Quit")
237286
.build()?;
@@ -248,7 +297,7 @@ fn main() {
248297
.enabled(false)
249298
.build(app)?;
250299

251-
// Load icon from path
300+
// Load icon from path
252301
let icon_image = Image::from_bytes(include_bytes!("../icons/icon.png")).unwrap();
253302

254303
let icon_item = IconMenuItemBuilder::new("icon")
@@ -261,13 +310,21 @@ fn main() {
261310
.build()?;
262311

263312
let menu = MenuBuilder::new(app)
264-
.items(&[&file_menu, &other_item,&icon_item])
313+
.items(&[&file_menu, &other_item, &icon_item])
265314
.build()?;
266315

267316
app.set_menu(menu)?;
268317

318+
let menu_image_update =
319+
Image::from_bytes(include_bytes!("../icons/menu_update.png")).unwrap();
320+
// You can also update the submenu icon dynamically
321+
file_menu.set_icon(Some(menu_image_update))?;
322+
// Or set a native icon (only one type applies per platform)
323+
file_menu.set_native_icon(Some(tauri::menu::NativeIcon::Folder))?;
324+
269325
Ok(())
270326
})
327+
.run(tauri::generate_context!());
271328
}
272329
```
273330

0 commit comments

Comments
 (0)