Skip to content

Commit 1519fff

Browse files
committed
Tray icon loading path
1 parent 790072c commit 1519fff

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

build.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use std::fs;
2+
use std::path::Path;
3+
4+
fn main() {
5+
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
6+
let src = Path::new(&manifest_dir).join("assets");
7+
let out_dir = std::env::var("OUT_DIR").unwrap();
8+
let profile = std::env::var("PROFILE").unwrap_or_else(|_| "debug".to_string());
9+
let bin_dir = extract_matching_parent_dir(&out_dir, &profile).expect("Failed to find bin dir");
10+
let dst = bin_dir.join("assets");
11+
if src.exists() {
12+
copy_dir_all(&src, &dst).expect("Failed to copy assets directory");
13+
}
14+
}
15+
16+
fn copy_dir_all(src: &Path, dst: &Path) -> std::io::Result<()> {
17+
if !dst.exists() {
18+
fs::create_dir_all(dst)?;
19+
}
20+
for entry in fs::read_dir(src)? {
21+
let entry = entry?;
22+
let file_type = entry.file_type()?;
23+
let src_path = entry.path();
24+
let dst_path = dst.join(entry.file_name());
25+
if file_type.is_dir() {
26+
copy_dir_all(&src_path, &dst_path)?;
27+
} else {
28+
fs::copy(&src_path, &dst_path)?;
29+
}
30+
}
31+
Ok(())
32+
}
33+
34+
pub fn extract_matching_parent_dir<P: AsRef<std::path::Path>>(path: P, match_name: &str) -> std::io::Result<std::path::PathBuf> {
35+
path.as_ref()
36+
.ancestors()
37+
.find(|p| p.file_name() == Some(std::ffi::OsStr::new(match_name)))
38+
.map(|p| p.to_path_buf())
39+
.ok_or_else(|| {
40+
std::io::Error::new(
41+
std::io::ErrorKind::NotFound,
42+
format!("No parent directory matching '{match_name}' found"),
43+
)
44+
})
45+
}

src/main.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -519,9 +519,10 @@ async fn main() -> Result<(), BoxError> {
519519
});
520520

521521
let tray_icon_closur = || -> Result<(tray_icon::TrayIcon, tray_icon::menu::MenuItem, tray_icon::menu::MenuItem), BoxError> {
522-
// TODO: File path must be changed in production
523-
let path = concat!(env!("CARGO_MANIFEST_DIR"), "/assets/icon.png");
524-
let icon = util::load_icon(path)?;
522+
let exe_path = std::env::current_exe()?;
523+
let exe_dir = exe_path.parent().ok_or("Failed to get exe dir")?;
524+
let icon_path = exe_dir.join("assets").join("icon.png");
525+
let icon = util::load_icon(icon_path)?;
525526

526527
let show_item = tray_icon::menu::MenuItem::new("Show main window", true, None);
527528
let quit_item = tray_icon::menu::MenuItem::new("Quit", true, None);

0 commit comments

Comments
 (0)