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

feat: Add feature to use ksni crate for tray icons on Linux #12319

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,5 @@ opt-level = "s"
[patch.crates-io]
schemars_derive = { git = 'https://github.com/tauri-apps/schemars.git', branch = 'feat/preserve-description-newlines' }
tauri = { path = "./crates/tauri" }
tray-icon = { git = "https://github.com/dfaust/tray-icon", branch = "ksni" }
muda = { git = "https://github.com/dfaust/muda", branch = "ksni" }
4 changes: 3 additions & 1 deletion crates/tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ features = [
"protocol-asset",
"test",
"specta",
"linux-ksni",
]
rustc-args = ["--cfg", "docsrs"]
rustdoc-args = ["--cfg", "docsrs"]
Expand Down Expand Up @@ -175,7 +176,7 @@ test = []
compression = ["tauri-macros/compression", "tauri-utils/compression"]
wry = ["tauri-runtime-wry"]
objc-exception = ["tauri-runtime-wry/objc-exception"]
linux-libxdo = ["tray-icon/libxdo", "muda/libxdo"]
linux-libxdo = ["muda/libxdo"]
isolation = ["tauri-utils/isolation", "tauri-macros/isolation", "uuid"]
custom-protocol = ["tauri-macros/custom-protocol"]
native-tls = ["reqwest/native-tls"]
Expand All @@ -197,6 +198,7 @@ image-ico = ["image/ico"]
image-png = ["image/png"]
macos-proxy = ["tauri-runtime-wry/macos-proxy"]
specta = ["dep:specta", "dep:specta-util"]
linux-ksni = ["tray-icon?/linux-ksni"]

[[example]]
name = "commands"
Expand Down
1 change: 1 addition & 0 deletions crates/tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
//! - **image-png**: Adds support to parse `.png` image, see [`Image`].
//! - **macos-proxy**: Adds support for [`WebviewBuilder::proxy_url`] on macOS. Requires macOS 14+.
//! - **specta**: Add support for [`specta::specta`](https://docs.rs/specta/%5E2.0.0-rc.9/specta/attr.specta.html) with Tauri arguments such as [`State`](crate::State), [`Window`](crate::Window) and [`AppHandle`](crate::AppHandle)
//! - **linux-ksni**: Enables the experimental `linux-ksni` feature of the `tray-icon` crate, which uses the xdg standard for system tray icons on Linux.
//!
//! ## Cargo allowlist features
//!
Expand Down
25 changes: 12 additions & 13 deletions crates/tauri/src/tray/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,6 @@ pub struct TrayIconBuilder<R: Runtime> {

impl<R: Runtime> TrayIconBuilder<R> {
/// Creates a new tray icon builder.
///
/// ## Platform-specific:
///
/// - **Linux:** Sometimes the icon won't be visible unless a menu is set.
/// Setting an empty [`Menu`](crate::menu::Menu) is enough.
pub fn new() -> Self {
Self {
inner: tray_icon::TrayIconBuilder::new(),
Expand All @@ -232,11 +227,6 @@ impl<R: Runtime> TrayIconBuilder<R> {
}

/// Creates a new tray icon builder with the specified id.
///
/// ## Platform-specific:
///
/// - **Linux:** Sometimes the icon won't be visible unless a menu is set.
/// Setting an empty [`Menu`](crate::menu::Menu) is enough.
pub fn with_id<I: Into<TrayIconId>>(id: I) -> Self {
let mut builder = Self::new();
builder.inner = builder.inner.with_id(id);
Expand All @@ -259,6 +249,7 @@ impl<R: Runtime> TrayIconBuilder<R> {
///
/// - **Linux:** Sometimes the icon won't be visible unless a menu is set.
/// Setting an empty [`Menu`](crate::menu::Menu) is enough.
/// Works with feature `linux-ksni`.
pub fn icon(mut self, icon: Image<'_>) -> Self {
let icon = icon.try_into().ok();
if let Some(icon) = icon {
Expand All @@ -271,7 +262,7 @@ impl<R: Runtime> TrayIconBuilder<R> {
///
/// ## Platform-specific:
///
/// - **Linux:** Unsupported.
/// - **Linux:** Unsupported. Works with feature `linux-ksni`.
pub fn tooltip<S: AsRef<str>>(mut self, s: S) -> Self {
self.inner = self.inner.with_tooltip(s);
self
Expand All @@ -286,16 +277,20 @@ impl<R: Runtime> TrayIconBuilder<R> {
/// updated information. In general, it shouldn't be shown unless a
/// user requests it as it can take up a significant amount of space
/// on the user's panel. This may not be shown in all visualizations.
/// Works with feature `linux-ksni`.
/// - **Windows:** Unsupported.
pub fn title<S: AsRef<str>>(mut self, title: S) -> Self {
self.inner = self.inner.with_title(title);
self
}

/// Set tray icon temp dir path. **Linux only**.
///
/// Not availabe with feature `linux-ksni`.
///
/// On Linux, we need to write the icon to the disk and usually it will
/// be `$XDG_RUNTIME_DIR/tray-icon` or `$TEMP/tray-icon`.
#[cfg(not(feature = "linux-ksni"))]
pub fn temp_dir_path<P: AsRef<Path>>(mut self, s: P) -> Self {
self.inner = self.inner.with_temp_dir_path(s);
self
Expand Down Expand Up @@ -509,7 +504,7 @@ impl<R: Runtime> TrayIcon<R> {
///
/// ## Platform-specific:
///
/// - **Linux**: once a menu is set it cannot be removed so `None` has no effect
/// - **Linux**: Once a menu is set it cannot be removed so `None` has no effect. Works with feature `linux-ksni`.
pub fn set_menu<M: ContextMenu + 'static>(&self, menu: Option<M>) -> crate::Result<()> {
run_item_main_thread!(self, |self_: Self| {
self_.inner.set_menu(menu.map(|m| m.inner_context_owned()))
Expand All @@ -520,7 +515,7 @@ impl<R: Runtime> TrayIcon<R> {
///
/// ## Platform-specific:
///
/// - **Linux:** Unsupported
/// - **Linux:** Unsupported. Works with feature `linux-ksni`.
pub fn set_tooltip<S: AsRef<str>>(&self, tooltip: Option<S>) -> crate::Result<()> {
let s = tooltip.map(|s| s.as_ref().to_string());
run_item_main_thread!(self, |self_: Self| self_.inner.set_tooltip(s))?.map_err(Into::into)
Expand All @@ -535,6 +530,7 @@ impl<R: Runtime> TrayIcon<R> {
/// updated information. In general, it shouldn't be shown unless a
/// user requests it as it can take up a significant amount of space
/// on the user's panel. This may not be shown in all visualizations.
/// Works with feature `linux-ksni`.
/// - **Windows:** Unsupported
pub fn set_title<S: AsRef<str>>(&self, title: Option<S>) -> crate::Result<()> {
let s = title.map(|s| s.as_ref().to_string());
Expand All @@ -547,9 +543,12 @@ impl<R: Runtime> TrayIcon<R> {
}

/// Sets the tray icon temp dir path. **Linux only**.
///
/// Not availabe with feature `linux-ksni`.
///
/// On Linux, we need to write the icon to the disk and usually it will
/// be `$XDG_RUNTIME_DIR/tray-icon` or `$TEMP/tray-icon`.
#[cfg(not(feature = "linux-ksni"))]
pub fn set_temp_dir_path<P: AsRef<Path>>(&self, path: Option<P>) -> crate::Result<()> {
#[allow(unused)]
let p = path.map(|p| p.as_ref().to_path_buf());
Expand Down
3 changes: 3 additions & 0 deletions crates/tauri/src/tray/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ fn new<R: Runtime>(
if let Some(title) = options.title {
builder = builder.title(title);
}
#[cfg(not(feature = "linux-ksni"))]
if let Some(temp_dir_path) = options.temp_dir_path {
builder = builder.temp_dir_path(temp_dir_path);
}
Expand Down Expand Up @@ -191,6 +192,7 @@ fn set_visible<R: Runtime>(
tray.set_visible(visible)
}

#[cfg(not(feature = "linux-ksni"))]
#[command(root = "crate")]
fn set_temp_dir_path<R: Runtime>(
webview: Webview<R>,
Expand Down Expand Up @@ -235,6 +237,7 @@ pub(crate) fn init<R: Runtime>() -> TauriPlugin<R> {
set_tooltip,
set_title,
set_visible,
#[cfg(not(feature = "linux-ksni"))]
set_temp_dir_path,
set_icon_as_template,
set_show_menu_on_left_click,
Expand Down
Loading