Skip to content

Commit 41d5d6a

Browse files
authored
feat(core): window menus (#1745)
1 parent 10715e6 commit 41d5d6a

File tree

11 files changed

+558
-58
lines changed

11 files changed

+558
-58
lines changed

.changes/menu.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
Implemented window menus APIs.

core/tauri/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ thiserror = "1.0.24"
2424
once_cell = "1.7.2"
2525
tauri-macros = { version = "1.0.0-beta-rc.1", path = "../tauri-macros" }
2626
tauri-utils = { version = "1.0.0-beta-rc.1", path = "../tauri-utils" }
27-
wry = { git = "https://github.com/tauri-apps/wry", rev = "0570dcab90087af5b1d29218d9d25186a7ade357" }
27+
wry = { git = "https://github.com/tauri-apps/wry", rev = "6bc97aff525644b83a3a00537316c46d7afb985b" }
2828
rand = "0.8"
2929
reqwest = { version = "0.11", features = [ "json", "multipart" ] }
3030
tempfile = "3"

core/tauri/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,12 @@ pub use {
5959
Invoke, InvokeError, InvokeHandler, InvokeMessage, InvokeResolver, InvokeResponse, OnPageLoad,
6060
PageLoadPayload, SetupHook,
6161
},
62-
self::runtime::app::{App, Builder},
62+
self::runtime::app::{App, Builder, WindowMenuEvent},
6363
self::runtime::flavors::wry::Wry,
6464
self::runtime::monitor::Monitor,
65-
self::runtime::webview::{WebviewAttributes, WindowBuilder},
65+
self::runtime::webview::{
66+
CustomMenuItem, Menu, MenuItem, MenuItemId, WebviewAttributes, WindowBuilder,
67+
},
6668
self::runtime::window::{
6769
export::{
6870
dpi::{LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize, Pixel, Position, Size},

core/tauri/src/runtime/app.rs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
flavors::wry::Wry,
1111
manager::{Args, WindowManager},
1212
tag::Tag,
13-
webview::{CustomProtocol, WebviewAttributes, WindowBuilder},
13+
webview::{CustomProtocol, Menu, MenuItemId, WebviewAttributes, WindowBuilder},
1414
window::PendingWindow,
1515
Dispatch, Runtime,
1616
},
@@ -23,6 +23,26 @@ use std::{collections::HashMap, sync::Arc};
2323
#[cfg(feature = "updater")]
2424
use crate::updater;
2525

26+
pub(crate) type GlobalMenuEventListener<P> = Box<dyn Fn(WindowMenuEvent<P>) + Send + Sync>;
27+
28+
/// A menu event that was triggered on a window.
29+
pub struct WindowMenuEvent<P: Params> {
30+
pub(crate) menu_item_id: MenuItemId,
31+
pub(crate) window: Window<P>,
32+
}
33+
34+
impl<P: Params> WindowMenuEvent<P> {
35+
/// The menu item id.
36+
pub fn menu_item_id(&self) -> MenuItemId {
37+
self.menu_item_id
38+
}
39+
40+
/// The window that the menu belongs to.
41+
pub fn window(&self) -> &Window<P> {
42+
&self.window
43+
}
44+
}
45+
2646
/// A handle to the currently running application.
2747
///
2848
/// This type implements [`Manager`] which allows for manipulation of global application items.
@@ -154,6 +174,12 @@ where
154174

155175
/// App state.
156176
state: StateManager,
177+
178+
/// The menu set to all windows.
179+
menu: Vec<Menu>,
180+
181+
/// Menu event handlers that listens to all windows.
182+
menu_event_listeners: Vec<GlobalMenuEventListener<Args<E, L, A, R>>>,
157183
}
158184

159185
impl<E, L, A, R> Builder<E, L, A, R>
@@ -173,6 +199,8 @@ where
173199
plugins: PluginStore::default(),
174200
uri_scheme_protocols: Default::default(),
175201
state: StateManager::new(),
202+
menu: Vec::new(),
203+
menu_event_listeners: Vec::new(),
176204
}
177205
}
178206

@@ -286,6 +314,21 @@ where
286314
self
287315
}
288316

317+
/// Sets the menu to use on all windows.
318+
pub fn menu(mut self, menu: Vec<Menu>) -> Self {
319+
self.menu = menu;
320+
self
321+
}
322+
323+
/// Registers a menu event handler for all windows.
324+
pub fn on_menu_event<F: Fn(WindowMenuEvent<Args<E, L, A, R>>) + Send + Sync + 'static>(
325+
mut self,
326+
handler: F,
327+
) -> Self {
328+
self.menu_event_listeners.push(Box::new(handler));
329+
self
330+
}
331+
289332
/// Registers a URI scheme protocol available to all webviews.
290333
/// Leverages [setURLSchemeHandler](https://developer.apple.com/documentation/webkit/wkwebviewconfiguration/2875766-seturlschemehandler) on macOS,
291334
/// [AddWebResourceRequestedFilter](https://docs.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2.addwebresourcerequestedfilter?view=webview2-dotnet-1.0.774.44) on Windows
@@ -321,6 +364,8 @@ where
321364
self.on_page_load,
322365
self.uri_scheme_protocols,
323366
self.state,
367+
self.menu,
368+
self.menu_event_listeners,
324369
);
325370

326371
// set up all the windows defined in the config

0 commit comments

Comments
 (0)