Skip to content

Commit 954460c

Browse files
authored
feat(core): MenuHandle show, hide, is_visible and toggle APIs (#1958)
1 parent cca8115 commit 954460c

14 files changed

Lines changed: 138 additions & 9 deletions

File tree

.changes/core-show-hide-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+
Adds `show`, `hide`, `is_visible` and `toggle` APIs to the `MenuHandle`.

.changes/runtime-show-hide-menu.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-runtime": patch
3+
"tauri-runtime-wry": patch
4+
---
5+
6+
Adds `show_menu`, `hide_menu` and `is_menu_visible` APIs to the `Dispatcher` trait.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ members = [
2323
]
2424

2525
[patch.crates-io]
26-
tao = { git = "https://github.com/tauri-apps/tao", rev = "5be88eb9488e3ad27194b5eff2ea31a473128f9c" }
26+
tao = { git = "https://github.com/tauri-apps/tao", rev = "66360eea4ec6af8a52afcebb7700f486a0092168" }
2727

2828
# default to small, optimized workspace release binaries
2929
[profile.release]

core/tauri-runtime-wry/src/lib.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ use std::{
5757
},
5858
};
5959

60+
#[cfg(feature = "menu")]
61+
use std::sync::atomic::{AtomicBool, Ordering};
62+
6063
#[cfg(any(feature = "menu", feature = "system-tray"))]
6164
mod menu;
6265
#[cfg(any(feature = "menu", feature = "system-tray"))]
@@ -456,6 +459,8 @@ enum WindowMessage {
456459
IsDecorated(Sender<bool>),
457460
IsResizable(Sender<bool>),
458461
IsVisible(Sender<bool>),
462+
#[cfg(feature = "menu")]
463+
IsMenuVisible(Sender<bool>),
459464
CurrentMonitor(Sender<Option<MonitorHandle>>),
460465
PrimaryMonitor(Sender<Option<MonitorHandle>>),
461466
AvailableMonitors(Sender<Vec<MonitorHandle>>),
@@ -469,6 +474,10 @@ enum WindowMessage {
469474
Unmaximize,
470475
Minimize,
471476
Unminimize,
477+
#[cfg(feature = "menu")]
478+
ShowMenu,
479+
#[cfg(feature = "menu")]
480+
HideMenu,
472481
Show,
473482
Hide,
474483
Close,
@@ -618,6 +627,11 @@ impl Dispatch for WryDispatcher {
618627
Ok(dispatcher_getter!(self, WindowMessage::IsVisible))
619628
}
620629

630+
#[cfg(feature = "menu")]
631+
fn is_menu_visible(&self) -> Result<bool> {
632+
Ok(dispatcher_getter!(self, WindowMessage::IsMenuVisible))
633+
}
634+
621635
fn current_monitor(&self) -> Result<Option<Monitor>> {
622636
Ok(
623637
dispatcher_getter!(self, WindowMessage::CurrentMonitor)
@@ -741,6 +755,24 @@ impl Dispatch for WryDispatcher {
741755
.map_err(|_| Error::FailedToSendMessage)
742756
}
743757

758+
#[cfg(feature = "menu")]
759+
fn show_menu(&self) -> Result<()> {
760+
self
761+
.context
762+
.proxy
763+
.send_event(Message::Window(self.window_id, WindowMessage::ShowMenu))
764+
.map_err(|_| Error::FailedToSendMessage)
765+
}
766+
767+
#[cfg(feature = "menu")]
768+
fn hide_menu(&self) -> Result<()> {
769+
self
770+
.context
771+
.proxy
772+
.send_event(Message::Window(self.window_id, WindowMessage::HideMenu))
773+
.map_err(|_| Error::FailedToSendMessage)
774+
}
775+
744776
fn show(&self) -> Result<()> {
745777
self
746778
.context
@@ -916,6 +948,8 @@ struct WebviewWrapper {
916948
inner: WebView,
917949
#[cfg(feature = "menu")]
918950
menu_items: HashMap<u32, WryCustomMenuItem>,
951+
#[cfg(feature = "menu")]
952+
is_menu_visible: AtomicBool,
919953
}
920954

921955
/// A Tauri [`Runtime`] wrapper around wry.
@@ -1275,6 +1309,10 @@ fn handle_event_loop(
12751309
WindowMessage::IsDecorated(tx) => tx.send(window.is_decorated()).unwrap(),
12761310
WindowMessage::IsResizable(tx) => tx.send(window.is_resizable()).unwrap(),
12771311
WindowMessage::IsVisible(tx) => tx.send(window.is_visible()).unwrap(),
1312+
#[cfg(feature = "menu")]
1313+
WindowMessage::IsMenuVisible(tx) => tx
1314+
.send(webview.is_menu_visible.load(Ordering::Relaxed))
1315+
.unwrap(),
12781316
WindowMessage::CurrentMonitor(tx) => tx.send(window.current_monitor()).unwrap(),
12791317
WindowMessage::PrimaryMonitor(tx) => tx.send(window.primary_monitor()).unwrap(),
12801318
WindowMessage::AvailableMonitors(tx) => {
@@ -1295,6 +1333,16 @@ fn handle_event_loop(
12951333
WindowMessage::Unmaximize => window.set_maximized(false),
12961334
WindowMessage::Minimize => window.set_minimized(true),
12971335
WindowMessage::Unminimize => window.set_minimized(false),
1336+
#[cfg(feature = "menu")]
1337+
WindowMessage::ShowMenu => {
1338+
window.show_menu();
1339+
webview.is_menu_visible.store(true, Ordering::Relaxed);
1340+
}
1341+
#[cfg(feature = "menu")]
1342+
WindowMessage::HideMenu => {
1343+
window.hide_menu();
1344+
webview.is_menu_visible.store(false, Ordering::Relaxed);
1345+
}
12981346
WindowMessage::Show => window.set_visible(true),
12991347
WindowMessage::Hide => window.set_visible(false),
13001348
WindowMessage::Close => {
@@ -1494,6 +1542,8 @@ fn create_webview<P: Params<Runtime = Wry>>(
14941542
inner: webview,
14951543
#[cfg(feature = "menu")]
14961544
menu_items,
1545+
#[cfg(feature = "menu")]
1546+
is_menu_visible: AtomicBool::new(true),
14971547
})
14981548
}
14991549

core/tauri-runtime/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,10 @@ pub trait Dispatch: Clone + Send + Sized + 'static {
301301
/// Gets the window's current vibility state.
302302
fn is_visible(&self) -> crate::Result<bool>;
303303

304+
/// Gets the window menu current visibility state.
305+
#[cfg(feature = "menu")]
306+
fn is_menu_visible(&self) -> crate::Result<bool>;
307+
304308
/// Returns the monitor on which the window currently resides.
305309
///
306310
/// Returns None if current monitor can't be detected.
@@ -350,6 +354,14 @@ pub trait Dispatch: Clone + Send + Sized + 'static {
350354
/// Unminimizes the window.
351355
fn unminimize(&self) -> crate::Result<()>;
352356

357+
/// Shows the window menu.
358+
#[cfg(feature = "menu")]
359+
fn show_menu(&self) -> crate::Result<()>;
360+
361+
/// Hides the window menu.
362+
#[cfg(feature = "menu")]
363+
fn hide_menu(&self) -> crate::Result<()>;
364+
353365
/// Shows the window.
354366
fn show(&self) -> crate::Result<()>;
355367

core/tauri/src/window/menu.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,30 @@ impl<P: Params> MenuHandle<P> {
7070
}
7171
panic!("item id not found")
7272
}
73+
74+
/// Shows the menu.
75+
pub fn show(&self) -> crate::Result<()> {
76+
self.dispatcher.show_menu().map_err(Into::into)
77+
}
78+
79+
/// Hides the menu.
80+
pub fn hide(&self) -> crate::Result<()> {
81+
self.dispatcher.hide_menu().map_err(Into::into)
82+
}
83+
84+
/// Whether the menu is visible or not.
85+
pub fn is_visible(&self) -> crate::Result<bool> {
86+
self.dispatcher.is_menu_visible().map_err(Into::into)
87+
}
88+
89+
/// Toggles the menu visibility.
90+
pub fn toggle(&self) -> crate::Result<()> {
91+
if self.is_visible()? {
92+
self.hide()
93+
} else {
94+
self.show()
95+
}
96+
}
7397
}
7498

7599
impl<P: Params> MenuItemHandle<P> {

examples/api/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
},
1919
"dependencies": {
2020
"@tauri-apps/api": "link:../../tooling/api/dist",
21+
"hotkeys-js": "^3.8.5",
2122
"sirv-cli": "1.0.11"
2223
}
2324
}

examples/api/public/build/bundle.js

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

examples/api/public/build/bundle.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/api/src-tauri/src/main.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ struct Reply {
2020
data: String,
2121
}
2222

23+
#[tauri::command]
24+
async fn menu_toggle(window: tauri::Window) {
25+
window.menu_handle().toggle().unwrap();
26+
}
27+
2328
fn main() {
2429
tauri::Builder::default()
2530
.on_page_load(|window, _| {
@@ -86,7 +91,8 @@ fn main() {
8691
})
8792
.invoke_handler(tauri::generate_handler![
8893
cmd::log_operation,
89-
cmd::perform_request
94+
cmd::perform_request,
95+
menu_toggle,
9096
])
9197
.run(tauri::generate_context!())
9298
.expect("error while running tauri application");

0 commit comments

Comments
 (0)