mirror of
https://github.com/tauri-apps/tauri.git
synced 2026-04-01 10:01:07 +02:00
committed by
GitHub
parent
f7acb061e4
commit
da8824318a
5
.changes/fix-tray-menu-ids-update.md
Normal file
5
.changes/fix-tray-menu-ids-update.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"tauri": patch
|
||||
---
|
||||
|
||||
Update tray menu id map when `SystemTrayHandle::set_menu` is called.
|
||||
@@ -1241,9 +1241,10 @@ impl<R: Runtime> Builder<R> {
|
||||
.expect("failed to run tray");
|
||||
|
||||
let tray_handle = tray::SystemTrayHandle {
|
||||
ids: Arc::new(ids.clone()),
|
||||
ids: Arc::new(std::sync::Mutex::new(ids)),
|
||||
inner: tray_handler,
|
||||
};
|
||||
let ids = tray_handle.ids.clone();
|
||||
app.tray_handle.replace(tray_handle.clone());
|
||||
app.handle.tray_handle.replace(tray_handle);
|
||||
for listener in self.system_tray_event_listeners {
|
||||
@@ -1258,7 +1259,7 @@ impl<R: Runtime> Builder<R> {
|
||||
let app_handle = app_handle.clone();
|
||||
let event = match event {
|
||||
RuntimeSystemTrayEvent::MenuItemClick(id) => tray::SystemTrayEvent::MenuItemClick {
|
||||
id: ids.get(id).unwrap().clone(),
|
||||
id: ids.lock().unwrap().get(id).unwrap().clone(),
|
||||
},
|
||||
RuntimeSystemTrayEvent::LeftClick { position, size } => {
|
||||
tray::SystemTrayEvent::LeftClick {
|
||||
|
||||
@@ -12,7 +12,10 @@ pub use crate::runtime::{
|
||||
|
||||
use tauri_macros::default_runtime;
|
||||
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
|
||||
pub(crate) fn get_menu_ids(map: &mut HashMap<MenuHash, MenuId>, menu: &SystemTrayMenu) {
|
||||
for item in &menu.items {
|
||||
@@ -80,7 +83,7 @@ pub enum SystemTrayEvent {
|
||||
#[default_runtime(crate::Wry, wry)]
|
||||
#[derive(Debug)]
|
||||
pub struct SystemTrayHandle<R: Runtime> {
|
||||
pub(crate) ids: Arc<HashMap<MenuHash, MenuId>>,
|
||||
pub(crate) ids: Arc<Mutex<HashMap<MenuHash, MenuId>>>,
|
||||
pub(crate) inner: R::TrayHandler,
|
||||
}
|
||||
|
||||
@@ -113,7 +116,7 @@ impl<R: Runtime> Clone for SystemTrayMenuItemHandle<R> {
|
||||
impl<R: Runtime> SystemTrayHandle<R> {
|
||||
/// Gets a handle to the menu item that has the specified `id`.
|
||||
pub fn get_item(&self, id: MenuIdRef<'_>) -> SystemTrayMenuItemHandle<R> {
|
||||
for (raw, item_id) in self.ids.iter() {
|
||||
for (raw, item_id) in self.ids.lock().unwrap().iter() {
|
||||
if item_id == id {
|
||||
return SystemTrayMenuItemHandle {
|
||||
id: *raw,
|
||||
@@ -131,7 +134,11 @@ impl<R: Runtime> SystemTrayHandle<R> {
|
||||
|
||||
/// Updates the tray menu.
|
||||
pub fn set_menu(&self, menu: SystemTrayMenu) -> crate::Result<()> {
|
||||
self.inner.set_menu(menu).map_err(Into::into)
|
||||
let mut ids = HashMap::new();
|
||||
get_menu_ids(&mut ids, &menu);
|
||||
self.inner.set_menu(menu)?;
|
||||
*self.ids.lock().unwrap() = ids;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Support [macOS tray icon template](https://developer.apple.com/documentation/appkit/nsimage/1520017-template?language=objc) to adjust automatically based on taskbar color.
|
||||
|
||||
Reference in New Issue
Block a user