Browse Source

fix(core): system tray icon path resolution (Linux) (#1763)

Lucas Fernandes Nogueira 4 years ago
parent
commit
5edda4ba08
3 changed files with 30 additions and 17 deletions
  1. 12 14
      core/tauri-codegen/src/context.rs
  2. 18 2
      core/tauri/src/api/path.rs
  3. 0 1
      core/tauri/src/lib.rs

+ 12 - 14
core/tauri-codegen/src/context.rs

@@ -84,24 +84,22 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
     let mut system_tray_icon_path = tray.icon_path.clone();
     system_tray_icon_path.set_extension("png");
     if dev {
-      let system_tray_icon_file_name = system_tray_icon_path
-        .file_name()
-        .expect("failed to get tray path file_name")
-        .to_string_lossy()
-        .to_string();
-      quote!(Some(
-        ::tauri::platform::resource_dir(&#package_info)
-          .expect("failed to read resource dir")
-          .join(
-            #system_tray_icon_file_name
-          )
-      ))
-    } else {
       let system_tray_icon_path = config_parent
         .join(system_tray_icon_path)
         .display()
         .to_string();
       quote!(Some(::std::path::PathBuf::from(#system_tray_icon_path)))
+    } else {
+      let system_tray_icon_file_path = system_tray_icon_path.to_string_lossy().to_string();
+      quote!(
+        Some(
+          #root::api::path::resolve_path(
+            &#config, &#package_info,
+            #system_tray_icon_file_path,
+            Some(#root::api::path::BaseDirectory::Resource)
+          ).expect("failed to resolve resource dir")
+        )
+      )
     }
   } else {
     quote!(None)
@@ -122,10 +120,10 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
 
   // double braces are purposeful to force the code into a block expression
   Ok(quote!(#root::Context {
+    system_tray_icon: #system_tray_icon,
     config: #config,
     assets: ::std::sync::Arc::new(#assets),
     default_window_icon: #default_window_icon,
-    system_tray_icon: #system_tray_icon,
     package_info: #package_info,
   }))
 }

+ 18 - 2
core/tauri/src/api/path.rs

@@ -4,7 +4,7 @@
 
 use std::{
   env,
-  path::{Path, PathBuf},
+  path::{Component, Path, PathBuf},
 };
 
 use crate::{Config, PackageInfo};
@@ -85,6 +85,7 @@ pub fn resolve_path<P: AsRef<Path>>(
   dir: Option<BaseDirectory>,
 ) -> crate::api::Result<PathBuf> {
   if let Some(base_dir) = dir {
+    let resolve_resource = matches!(base_dir, BaseDirectory::Resource);
     let base_dir_path = match base_dir {
       BaseDirectory::Audio => audio_dir(),
       BaseDirectory::Cache => cache_dir(),
@@ -107,7 +108,22 @@ pub fn resolve_path<P: AsRef<Path>>(
       BaseDirectory::Current => Some(env::current_dir()?),
     };
     if let Some(mut base_dir_path_value) = base_dir_path {
-      base_dir_path_value.push(path);
+      // use the same path resolution mechanism as the bundler's resource injection algorithm
+      if resolve_resource {
+        let mut resource_path = PathBuf::new();
+        for component in path.as_ref().components() {
+          match component {
+            Component::Prefix(_) => {}
+            Component::RootDir => resource_path.push("_root_"),
+            Component::CurDir => {}
+            Component::ParentDir => resource_path.push("_up_"),
+            Component::Normal(p) => resource_path.push(p),
+          }
+        }
+        base_dir_path_value.push(resource_path);
+      } else {
+        base_dir_path_value.push(path);
+      }
       Ok(base_dir_path_value)
     } else {
       Err(crate::api::Error::Path(

+ 0 - 1
core/tauri/src/lib.rs

@@ -76,7 +76,6 @@ pub use {
   },
   self::state::{State, StateManager},
   self::window::{MenuEvent, Monitor, Window},
-  tauri_utils::platform,
 };
 
 /// Reads the config file at compile time and generates a [`Context`] based on its content.