Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/tauri-cli/src/interface/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1036,7 +1036,7 @@ impl RustAppSettings {
.product_name
.clone()
.unwrap_or_else(|| cargo_package_settings.name.clone()),
version: version.clone(),
version,
description: cargo_package_settings
.description
.clone()
Expand Down
4 changes: 2 additions & 2 deletions crates/tauri/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ macro_rules! shared_app_impl {
I: ?Sized,
TrayIconId: PartialEq<&'a I>,
{
self.manager.tray.tray_by_id(id)
self.manager.tray.tray_by_id(self.app_handle(), id)
}

/// Removes a tray icon using the provided id from tauri's internal state and returns it.
Expand All @@ -755,7 +755,7 @@ macro_rules! shared_app_impl {
I: ?Sized,
TrayIconId: PartialEq<&'a I>,
{
self.manager.tray.remove_tray_by_id(id)
self.manager.tray.remove_tray_by_id(self.app_handle(), id)
}

/// Gets the app's configuration, defined on the `tauri.conf.json` file.
Expand Down
56 changes: 38 additions & 18 deletions crates/tauri/src/manager/tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

use std::{collections::HashMap, fmt, sync::Mutex};
use std::{
collections::HashMap,
fmt,
sync::{Arc, Mutex},
};

use crate::{
app::GlobalTrayIconEventListener,
image::Image,
tray::{TrayIcon, TrayIconEvent, TrayIconId},
AppHandle, Runtime,
AppHandle, Manager, Resource, ResourceId, Runtime,
};

pub struct TrayManager<R: Runtime> {
pub(crate) icon: Option<Image<'static>>,
/// Tray icons
pub(crate) icons: Mutex<Vec<TrayIcon<R>>>,
pub(crate) icons: Mutex<Vec<(TrayIconId, ResourceId)>>,
/// Global Tray icon event listeners.
pub(crate) global_event_listeners: Mutex<Vec<GlobalTrayIconEventListener<AppHandle<R>>>>,
/// Tray icon event listeners.
Expand All @@ -41,30 +45,46 @@ impl<R: Runtime> TrayManager<R> {
.push(Box::new(handler));
}

pub fn tray_by_id<'a, I>(&self, id: &'a I) -> Option<TrayIcon<R>>
pub fn tray_by_id<'a, I>(&self, app: &AppHandle<R>, id: &'a I) -> Option<TrayIcon<R>>
where
I: ?Sized,
TrayIconId: PartialEq<&'a I>,
{
self
.icons
.lock()
.unwrap()
.iter()
.find(|t| t.id() == &id)
.cloned()
let icons = self.icons.lock().unwrap();
icons.iter().find_map(|(tray_icon_id, rid)| {
if tray_icon_id == &id {
let icon = app.resources_table().get::<TrayIcon<R>>(*rid).ok()?;
Some(Arc::unwrap_or_clone(icon))
} else {
None
}
})
}

pub fn tray_resource_by_id<'a, I>(&self, id: &'a I) -> Option<ResourceId>
where
I: ?Sized,
TrayIconId: PartialEq<&'a I>,
{
let icons = self.icons.lock().unwrap();
icons.iter().find_map(|(tray_icon_id, rid)| {
if tray_icon_id == &id {
Some(*rid)
} else {
None
}
})
}

pub fn remove_tray_by_id<'a, I>(&self, id: &'a I) -> Option<TrayIcon<R>>
pub fn remove_tray_by_id<'a, I>(&self, app: &AppHandle<R>, id: &'a I) -> Option<TrayIcon<R>>
where
I: ?Sized,
TrayIconId: PartialEq<&'a I>,
{
let mut icons = self.icons.lock().unwrap();
let idx = icons.iter().position(|t| t.id() == &id);
if let Some(idx) = idx {
return Some(icons.swap_remove(idx));
}
None
let rid = self.tray_resource_by_id(id)?;
let icon = app.resources_table().take::<TrayIcon<R>>(rid).ok()?;
let icon_to_return = icon.clone();
icon.close();
Some(Arc::unwrap_or_clone(icon_to_return))
}
}
42 changes: 25 additions & 17 deletions crates/tauri/src/tray/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ use crate::app::{GlobalMenuEventListener, GlobalTrayIconEventListener};
use crate::menu::ContextMenu;
use crate::menu::MenuEvent;
use crate::resources::Resource;
use crate::UnsafeSend;
use crate::{
image::Image, menu::run_item_main_thread, AppHandle, Manager, PhysicalPosition, Rect, Runtime,
};
use crate::{ResourceId, UnsafeSend};
use serde::Serialize;
use std::path::Path;
pub use tray_icon::TrayIconId;
Expand Down Expand Up @@ -358,8 +358,10 @@ impl<R: Runtime> TrayIconBuilder<R> {
self.inner.id()
}

/// Builds and adds a new [`TrayIcon`] to the system tray.
pub fn build<M: Manager<R>>(self, manager: &M) -> crate::Result<TrayIcon<R>> {
pub(crate) fn build_inner(
self,
app_handle: &AppHandle<R>,
) -> crate::Result<(TrayIcon<R>, ResourceId)> {
let id = self.id().clone();

// SAFETY:
Expand All @@ -368,8 +370,7 @@ impl<R: Runtime> TrayIconBuilder<R> {
let unsafe_builder = UnsafeSend(self.inner);

let (tx, rx) = std::sync::mpsc::channel();
let unsafe_tray = manager
.app_handle()
let unsafe_tray = app_handle
.run_on_main_thread(move || {
// SAFETY: will only be accessed on main thread
let _ = tx.send(unsafe_builder.take().build().map(UnsafeSend));
Expand All @@ -379,15 +380,21 @@ impl<R: Runtime> TrayIconBuilder<R> {
let icon = TrayIcon {
id,
inner: unsafe_tray.take(),
app_handle: manager.app_handle().clone(),
app_handle: app_handle.clone(),
};

icon.register(
let rid = icon.register(
&icon.app_handle,
self.on_menu_event,
self.on_tray_icon_event,
);

Ok((icon, rid))
}

/// Builds and adds a new [`TrayIcon`] to the system tray.
pub fn build<M: Manager<R>>(self, manager: &M) -> crate::Result<TrayIcon<R>> {
let (icon, _rid) = self.build_inner(manager.app_handle())?;
Ok(icon)
}
}
Expand Down Expand Up @@ -426,7 +433,7 @@ impl<R: Runtime> TrayIcon<R> {
app_handle: &AppHandle<R>,
on_menu_event: Option<GlobalMenuEventListener<AppHandle<R>>>,
on_tray_icon_event: Option<GlobalTrayIconEventListener<TrayIcon<R>>>,
) {
) -> ResourceId {
if let Some(handler) = on_menu_event {
app_handle
.manager
Expand All @@ -447,13 +454,15 @@ impl<R: Runtime> TrayIcon<R> {
.insert(self.id.clone(), handler);
}

let rid = app_handle.resources_table().add(self.clone());
app_handle
.manager
.tray
.icons
.lock()
.unwrap()
.push(self.clone());
.push((self.id().clone(), rid));
rid
}

/// The application handle associated with this type.
Expand Down Expand Up @@ -598,14 +607,13 @@ impl<R: Runtime> TrayIcon<R> {

impl<R: Runtime> Resource for TrayIcon<R> {
fn close(self: std::sync::Arc<Self>) {
self
.app_handle
.state::<plugin::TrayIcons>()
.icons
.lock()
.unwrap()
.remove(&self.id.0);
self.app_handle.remove_tray_by_id(&self.id);
let mut icons = self.app_handle.manager.tray.icons.lock().unwrap();
for (i, (tray_icon_id, _rid)) in icons.iter_mut().enumerate() {
if tray_icon_id == &self.id {
icons.swap_remove(i);
return;
}
}
}
}

Expand Down
Loading
Loading