Skip to content

Commit

Permalink
feat(linux): store tray icons in XDG_RUNTIME_DIR (#449)
Browse files Browse the repository at this point in the history
* feat(linux): Store tray icons in XDG_RUNTIME_DIR

This is preferred over /tmp, because this directory is only readable for the current user. While /tmp is shared with all users.

* Add changes file
  • Loading branch information
Beanow committed Jun 25, 2022
1 parent dfae373 commit 0125382
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .changes/linux-runtime-dir.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"tao": patch
---

On Linux, store tray icons in `$XDG_RUNTIME_DIR`.
This is preferred over `/tmp`, because this directory (typically `/run/user/{uid}`)
is only readable for the current user. While `/tmp` is shared with all users.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ targets = [

[features]
default = [ ]
tray = [ "libappindicator" ]
dox = [ "gtk/dox" ]
tray = [ "libappindicator", "dirs-next" ]

[dependencies]
instant = "0.1"
Expand Down Expand Up @@ -116,6 +116,7 @@ gdk-sys = "0.15"
gdkx11-sys = "0.15"
gdk-pixbuf = { version = "0.15", features = [ "v2_36_8" ] }
libappindicator = { version = "0.7", optional = true }
dirs-next = { version = "2.0.0", optional = true }
x11-dl = "2.18"
uuid = { version = "0.8", features = [ "v4" ] }
png = "0.17"
16 changes: 15 additions & 1 deletion src/platform_impl/linux/system_tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,24 @@ impl Drop for SystemTray {
}

fn temp_icon_path() -> std::io::Result<(PathBuf, PathBuf)> {
let mut parent_path = std::env::temp_dir();
let mut parent_path = dirs_next::runtime_dir().unwrap_or_else(|| std::env::temp_dir());

parent_path.push("tao");
std::fs::create_dir_all(&parent_path)?;
let mut icon_path = parent_path.clone();
icon_path.push(format!("tray-icon-{}.png", uuid::Uuid::new_v4()));
Ok((parent_path, icon_path))
}

#[test]
fn temp_icon_path_prefers_runtime() {
let runtime_dir = env!("XDG_RUNTIME_DIR");

let (dir1, _file1) = temp_icon_path().unwrap();
std::env::remove_var("XDG_RUNTIME_DIR");
let (dir2, _file2) = temp_icon_path().unwrap();
std::env::set_var("XDG_RUNTIME_DIR", runtime_dir);

assert_eq!(dir1, PathBuf::from(format!("{}/tao", runtime_dir)));
assert_eq!(dir2, PathBuf::from("/tmp/tao"));
}

0 comments on commit 0125382

Please sign in to comment.