Skip to content

Commit

Permalink
feat(core): window menus (#1745)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed May 8, 2021
1 parent 10715e6 commit 41d5d6a
Show file tree
Hide file tree
Showing 11 changed files with 558 additions and 58 deletions.
5 changes: 5 additions & 0 deletions .changes/menu.md
@@ -0,0 +1,5 @@
---
"tauri": patch
---

Implemented window menus APIs.
2 changes: 1 addition & 1 deletion core/tauri/Cargo.toml
Expand Up @@ -24,7 +24,7 @@ thiserror = "1.0.24"
once_cell = "1.7.2"
tauri-macros = { version = "1.0.0-beta-rc.1", path = "../tauri-macros" }
tauri-utils = { version = "1.0.0-beta-rc.1", path = "../tauri-utils" }
wry = { git = "https://github.com/tauri-apps/wry", rev = "0570dcab90087af5b1d29218d9d25186a7ade357" }
wry = { git = "https://github.com/tauri-apps/wry", rev = "6bc97aff525644b83a3a00537316c46d7afb985b" }
rand = "0.8"
reqwest = { version = "0.11", features = [ "json", "multipart" ] }
tempfile = "3"
Expand Down
6 changes: 4 additions & 2 deletions core/tauri/src/lib.rs
Expand Up @@ -59,10 +59,12 @@ pub use {
Invoke, InvokeError, InvokeHandler, InvokeMessage, InvokeResolver, InvokeResponse, OnPageLoad,
PageLoadPayload, SetupHook,
},
self::runtime::app::{App, Builder},
self::runtime::app::{App, Builder, WindowMenuEvent},
self::runtime::flavors::wry::Wry,
self::runtime::monitor::Monitor,
self::runtime::webview::{WebviewAttributes, WindowBuilder},
self::runtime::webview::{
CustomMenuItem, Menu, MenuItem, MenuItemId, WebviewAttributes, WindowBuilder,
},
self::runtime::window::{
export::{
dpi::{LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize, Pixel, Position, Size},
Expand Down
47 changes: 46 additions & 1 deletion core/tauri/src/runtime/app.rs
Expand Up @@ -10,7 +10,7 @@ use crate::{
flavors::wry::Wry,
manager::{Args, WindowManager},
tag::Tag,
webview::{CustomProtocol, WebviewAttributes, WindowBuilder},
webview::{CustomProtocol, Menu, MenuItemId, WebviewAttributes, WindowBuilder},
window::PendingWindow,
Dispatch, Runtime,
},
Expand All @@ -23,6 +23,26 @@ use std::{collections::HashMap, sync::Arc};
#[cfg(feature = "updater")]
use crate::updater;

pub(crate) type GlobalMenuEventListener<P> = Box<dyn Fn(WindowMenuEvent<P>) + Send + Sync>;

/// A menu event that was triggered on a window.
pub struct WindowMenuEvent<P: Params> {
pub(crate) menu_item_id: MenuItemId,
pub(crate) window: Window<P>,
}

impl<P: Params> WindowMenuEvent<P> {
/// The menu item id.
pub fn menu_item_id(&self) -> MenuItemId {
self.menu_item_id
}

/// The window that the menu belongs to.
pub fn window(&self) -> &Window<P> {
&self.window
}
}

/// A handle to the currently running application.
///
/// This type implements [`Manager`] which allows for manipulation of global application items.
Expand Down Expand Up @@ -154,6 +174,12 @@ where

/// App state.
state: StateManager,

/// The menu set to all windows.
menu: Vec<Menu>,

/// Menu event handlers that listens to all windows.
menu_event_listeners: Vec<GlobalMenuEventListener<Args<E, L, A, R>>>,
}

impl<E, L, A, R> Builder<E, L, A, R>
Expand All @@ -173,6 +199,8 @@ where
plugins: PluginStore::default(),
uri_scheme_protocols: Default::default(),
state: StateManager::new(),
menu: Vec::new(),
menu_event_listeners: Vec::new(),
}
}

Expand Down Expand Up @@ -286,6 +314,21 @@ where
self
}

/// Sets the menu to use on all windows.
pub fn menu(mut self, menu: Vec<Menu>) -> Self {
self.menu = menu;
self
}

/// Registers a menu event handler for all windows.
pub fn on_menu_event<F: Fn(WindowMenuEvent<Args<E, L, A, R>>) + Send + Sync + 'static>(
mut self,
handler: F,
) -> Self {
self.menu_event_listeners.push(Box::new(handler));
self
}

/// Registers a URI scheme protocol available to all webviews.
/// Leverages [setURLSchemeHandler](https://developer.apple.com/documentation/webkit/wkwebviewconfiguration/2875766-seturlschemehandler) on macOS,
/// [AddWebResourceRequestedFilter](https://docs.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2.addwebresourcerequestedfilter?view=webview2-dotnet-1.0.774.44) on Windows
Expand Down Expand Up @@ -321,6 +364,8 @@ where
self.on_page_load,
self.uri_scheme_protocols,
self.state,
self.menu,
self.menu_event_listeners,
);

// set up all the windows defined in the config
Expand Down

0 comments on commit 41d5d6a

Please sign in to comment.