Skip to content

Commit 4c4acc3

Browse files
feat: implement Default for Menu, closes #2398 (#4291)
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
1 parent 2bb4faa commit 4c4acc3

File tree

24 files changed

+223
-72
lines changed

24 files changed

+223
-72
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"cli.rs": patch
3+
"cli.js": patch
4+
---
5+
6+
Use the default window menu in the app template.

.changes/default-menu.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri": "patch"
3+
"tauri-runtime": "patch"
4+
---
5+
6+
Add `Menu::os_default` which will create a menu filled with default menu items and submenus.

core/tauri-runtime/src/menu.rs

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,131 @@ impl Menu {
186186
Default::default()
187187
}
188188

189+
/// Creates a menu filled with default menu items and submenus.
190+
///
191+
/// ## Platform-specific:
192+
///
193+
/// - **Windows**:
194+
/// - File
195+
/// - CloseWindow
196+
/// - Quit
197+
/// - Edit
198+
/// - Cut
199+
/// - Copy
200+
/// - Paste
201+
/// - Window
202+
/// - Minimize
203+
/// - CloseWindow
204+
///
205+
/// - **Linux**:
206+
/// - File
207+
/// - CloseWindow
208+
/// - Quit
209+
/// - Window
210+
/// - Minimize
211+
/// - CloseWindow
212+
///
213+
/// - **macOS**:
214+
/// - App
215+
/// - About
216+
/// - Separator
217+
/// - Services
218+
/// - Separator
219+
/// - Hide
220+
/// - HideOthers
221+
/// - ShowAll
222+
/// - Separator
223+
/// - Quit
224+
/// - File
225+
/// - CloseWindow
226+
/// - Edit
227+
/// - Undo
228+
/// - Redo
229+
/// - Separator
230+
/// - Cut
231+
/// - Copy
232+
/// - Paste
233+
/// - SelectAll
234+
/// - View
235+
/// - EnterFullScreen
236+
/// - Window
237+
/// - Minimize
238+
/// - Zoom
239+
/// - Separator
240+
/// - CloseWindow
241+
pub fn os_default(#[allow(unused)] app_name: &str) -> Self {
242+
let mut menu = Menu::new();
243+
#[cfg(target_os = "macos")]
244+
{
245+
menu = menu.add_submenu(Submenu::new(
246+
app_name,
247+
Menu::new()
248+
.add_native_item(MenuItem::About(
249+
app_name.to_string(),
250+
AboutMetadata::default(),
251+
))
252+
.add_native_item(MenuItem::Separator)
253+
.add_native_item(MenuItem::Services)
254+
.add_native_item(MenuItem::Separator)
255+
.add_native_item(MenuItem::Hide)
256+
.add_native_item(MenuItem::HideOthers)
257+
.add_native_item(MenuItem::ShowAll)
258+
.add_native_item(MenuItem::Separator)
259+
.add_native_item(MenuItem::Quit),
260+
));
261+
}
262+
263+
let mut file_menu = Menu::new();
264+
file_menu = file_menu.add_native_item(MenuItem::CloseWindow);
265+
#[cfg(not(target_os = "macos"))]
266+
{
267+
file_menu = file_menu.add_native_item(MenuItem::Quit);
268+
}
269+
menu = menu.add_submenu(Submenu::new("File", file_menu));
270+
271+
#[cfg(not(target_os = "linux"))]
272+
let mut edit_menu = Menu::new();
273+
#[cfg(target_os = "macos")]
274+
{
275+
edit_menu = edit_menu.add_native_item(MenuItem::Undo);
276+
edit_menu = edit_menu.add_native_item(MenuItem::Redo);
277+
edit_menu = edit_menu.add_native_item(MenuItem::Separator);
278+
}
279+
#[cfg(not(target_os = "linux"))]
280+
{
281+
edit_menu = edit_menu.add_native_item(MenuItem::Cut);
282+
edit_menu = edit_menu.add_native_item(MenuItem::Copy);
283+
edit_menu = edit_menu.add_native_item(MenuItem::Paste);
284+
}
285+
#[cfg(target_os = "macos")]
286+
{
287+
edit_menu = edit_menu.add_native_item(MenuItem::SelectAll);
288+
}
289+
#[cfg(not(target_os = "linux"))]
290+
{
291+
menu = menu.add_submenu(Submenu::new("Edit", edit_menu));
292+
}
293+
#[cfg(target_os = "macos")]
294+
{
295+
menu = menu.add_submenu(Submenu::new(
296+
"View",
297+
Menu::new().add_native_item(MenuItem::EnterFullScreen),
298+
));
299+
}
300+
301+
let mut window_menu = Menu::new();
302+
window_menu = window_menu.add_native_item(MenuItem::Minimize);
303+
#[cfg(target_os = "macos")]
304+
{
305+
window_menu = window_menu.add_native_item(MenuItem::Zoom);
306+
window_menu = window_menu.add_native_item(MenuItem::Separator);
307+
}
308+
window_menu = window_menu.add_native_item(MenuItem::CloseWindow);
309+
menu = menu.add_submenu(Submenu::new("Window", window_menu));
310+
311+
menu
312+
}
313+
189314
/// Creates a new window menu with the given items.
190315
///
191316
/// # Examples

examples/api/src-tauri/Cargo.lock

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/commands/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,9 @@ fn borrow_cmd_async(argument: &str) -> &str {
157157
}
158158

159159
fn main() {
160+
let context = tauri::generate_context!("../../examples/commands/tauri.conf.json");
160161
tauri::Builder::default()
162+
.menu(tauri::Menu::os_default(&context.package_info().name))
161163
.manage(MyState {
162164
value: 0,
163165
label: "Tauri!".into(),
@@ -187,8 +189,6 @@ fn main() {
187189
future_simple_command_with_result,
188190
async_stateful_command_with_result,
189191
])
190-
.run(tauri::generate_context!(
191-
"../../examples/commands/tauri.conf.json"
192-
))
192+
.run(context)
193193
.expect("error while running tauri application");
194194
}

examples/helloworld/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
)]
99

1010
fn main() {
11+
let context = tauri::generate_context!("../../examples/helloworld/tauri.conf.json");
1112
tauri::Builder::default()
12-
.run(tauri::generate_context!(
13-
"../../examples/helloworld/tauri.conf.json"
14-
))
13+
.menu(tauri::Menu::os_default(&context.package_info().name))
14+
.run(context)
1515
.expect("error while running tauri application");
1616
}

examples/isolation/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ fn main() {
2121

2222
#[cfg(feature = "isolation")]
2323
fn main() {
24+
let context = tauri::generate_context!("../../examples/isolation/tauri.conf.json");
2425
tauri::Builder::default()
26+
.menu(tauri::Menu::os_default(&context.package_info().name))
2527
.invoke_handler(tauri::generate_handler![ping])
26-
.run(tauri::generate_context!(
27-
"../../examples/isolation/tauri.conf.json"
28-
))
28+
.run(context)
2929
.expect("error while running tauri application");
3030
}

examples/multiwindow/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
use tauri::WindowBuilder;
1111

1212
fn main() {
13+
let context = tauri::generate_context!("../../examples/multiwindow/tauri.conf.json");
1314
tauri::Builder::default()
15+
.menu(tauri::Menu::os_default(&context.package_info().name))
1416
.on_page_load(|window, _payload| {
1517
let label = window.label().to_string();
1618
window.listen("clicked".to_string(), move |_payload| {
@@ -27,8 +29,6 @@ fn main() {
2729
.build()?;
2830
Ok(())
2931
})
30-
.run(tauri::generate_context!(
31-
"../../examples/multiwindow/tauri.conf.json"
32-
))
32+
.run(context)
3333
.expect("failed to run tauri application");
3434
}

examples/navigation/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
)]
99

1010
fn main() {
11+
let context = tauri::generate_context!("../../examples/navigation/tauri.conf.json");
1112
tauri::Builder::default()
12-
.run(tauri::generate_context!(
13-
"../../examples/navigation/tauri.conf.json"
14-
))
13+
.menu(tauri::Menu::os_default(&context.package_info().name))
14+
.run(context)
1515
.expect("error while running tauri application");
1616
}

examples/parent-window/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ async fn create_child_window(id: String, window: Window) {
2424
}
2525

2626
fn main() {
27+
let context = tauri::generate_context!("../../examples/parent-window/tauri.conf.json");
2728
tauri::Builder::default()
29+
.menu(tauri::Menu::os_default(&context.package_info().name))
2830
.on_page_load(|window, _payload| {
2931
let label = window.label().to_string();
3032
window.listen("clicked".to_string(), move |_payload| {
@@ -39,8 +41,6 @@ fn main() {
3941
.build()?;
4042
Ok(())
4143
})
42-
.run(tauri::generate_context!(
43-
"../../examples/parent-window/tauri.conf.json"
44-
))
44+
.run(context)
4545
.expect("failed to run tauri application");
4646
}

0 commit comments

Comments
 (0)