Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): wait for tray cleanup before exiting app, closes #5244 #5245

Merged
merged 3 commits into from Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/tray-cleanup.md
@@ -0,0 +1,5 @@
---
"tauri": "patch"
amrbashir marked this conversation as resolved.
Show resolved Hide resolved
---

Fix regression introduce in 1.1 which prevented removing tray icon when the app exits on Windows.
amrbashir marked this conversation as resolved.
Show resolved Hide resolved
15 changes: 10 additions & 5 deletions core/tauri-runtime-wry/src/lib.rs
Expand Up @@ -160,7 +160,10 @@ macro_rules! window_getter {
}};
}

fn send_user_message<T: UserEvent>(context: &Context<T>, message: Message<T>) -> Result<()> {
pub(crate) fn send_user_message<T: UserEvent>(
context: &Context<T>,
message: Message<T>,
) -> Result<()> {
if current_thread().id() == context.main_thread_id {
handle_user_message(
&context.main_thread.window_target,
Expand Down Expand Up @@ -1079,7 +1082,7 @@ pub enum TrayMessage {
#[cfg(target_os = "macos")]
UpdateIconAsTemplate(bool),
Create(SystemTray, Sender<Result<()>>),
Destroy,
Destroy(Sender<Result<()>>),
}

pub type CreateWebviewClosure<T> = Box<
Expand Down Expand Up @@ -1733,6 +1736,7 @@ impl<T: UserEvent> RuntimeHandle<T> for WryHandle<T> {
)?;
rx.recv().unwrap()?;
Ok(SystemTrayHandle {
context: self.context.clone(),
id,
proxy: self.context.proxy.clone(),
})
Expand Down Expand Up @@ -1921,6 +1925,7 @@ impl<T: UserEvent> Runtime<T> for Wry<T> {
);

Ok(SystemTrayHandle {
context: self.context.clone(),
id,
proxy: self.event_loop.create_proxy(),
})
Expand Down Expand Up @@ -2480,10 +2485,11 @@ fn handle_user_message<T: UserEvent>(
TrayMessage::Create(_tray, _tx) => {
// already handled
}
TrayMessage::Destroy => {
TrayMessage::Destroy(tx) => {
*tray_context.tray.lock().unwrap() = None;
tray_context.listeners.lock().unwrap().clear();
tray_context.items.lock().unwrap().clear();
tx.send(Ok(())).unwrap();
}
}
}
Expand Down Expand Up @@ -2612,14 +2618,13 @@ fn handle_event_loop<T: UserEvent>(
items.contains_key(&menu_id.0)
};
if has_menu {
listeners.replace(tray_context.listeners.clone());
listeners.replace(tray_context.listeners.lock().unwrap().clone());
tray_id = *id;
break;
}
}
drop(trays);
if let Some(listeners) = listeners {
let listeners = listeners.lock().unwrap();
let handlers = listeners.iter();
for handler in handlers {
handler(&event);
Expand Down
14 changes: 9 additions & 5 deletions core/tauri-runtime-wry/src/system_tray.rs
Expand Up @@ -27,7 +27,7 @@ pub use wry::application::platform::macos::{

use wry::application::system_tray::{SystemTray as WrySystemTray, SystemTrayBuilder};

use crate::{Error, Message, Result, TrayId, TrayMessage};
use crate::{send_user_message, Context, Error, Message, Result, TrayId, TrayMessage};

use tauri_runtime::{menu::MenuHash, SystemTray, UserEvent};

Expand Down Expand Up @@ -117,6 +117,7 @@ pub fn create_tray<T>(

#[derive(Debug, Clone)]
pub struct SystemTrayHandle<T: UserEvent> {
pub(crate) context: Context<T>,
pub(crate) id: TrayId,
pub(crate) proxy: EventLoopProxy<super::Message<T>>,
}
Expand Down Expand Up @@ -155,10 +156,13 @@ impl<T: UserEvent> TrayHandle for SystemTrayHandle<T> {
}

fn destroy(&self) -> Result<()> {
self
.proxy
.send_event(Message::Tray(self.id, TrayMessage::Destroy))
.map_err(|_| Error::FailedToSendMessage)
let (tx, rx) = std::sync::mpsc::channel();
send_user_message(
&self.context,
Message::Tray(self.id, TrayMessage::Destroy(tx)),
)?;
rx.recv().unwrap()?;
Ok(())
}
}

Expand Down