Explorar el Código

feat(core): add path resolver API to the App and AppHandle structs (#2015)

Lucas Fernandes Nogueira hace 4 años
padre
commit
5ca462f6cc
Se han modificado 2 ficheros con 36 adiciones y 2 borrados
  1. 5 0
      .changes/path-resolver.md
  2. 31 2
      core/tauri/src/app.rs

+ 5 - 0
.changes/path-resolver.md

@@ -0,0 +1,5 @@
+---
+"tauri": patch
+---
+
+Adds a `PathResolver` struct to simplify the usage of the `tauri::api::path::{app_dir, resource_dir}` APIs, accessible through the `App` and `AppHandle` `path_resolver` methods.

+ 31 - 2
core/tauri/src/app.rs

@@ -8,7 +8,7 @@ pub(crate) mod tray;
 
 use crate::{
   api::assets::Assets,
-  api::config::WindowUrl,
+  api::config::{Config, WindowUrl},
   hooks::{InvokeHandler, OnPageLoad, PageLoadPayload, SetupHook},
   manager::{Args, WindowManager},
   plugin::{Plugin, PluginStore},
@@ -22,7 +22,9 @@ use crate::{
   Context, Invoke, Manager, StateManager, Window,
 };
 
-use std::{collections::HashMap, sync::Arc};
+use tauri_utils::PackageInfo;
+
+use std::{collections::HashMap, path::PathBuf, sync::Arc};
 
 #[cfg(feature = "menu")]
 use crate::runtime::menu::Menu;
@@ -85,6 +87,25 @@ impl<P: Params> GlobalWindowEvent<P> {
   }
 }
 
+/// The path resolver is a helper for the application-specific [`crate::api::path`] APIs.
+#[derive(Debug, Clone)]
+pub struct PathResolver {
+  config: Arc<Config>,
+  package_info: PackageInfo,
+}
+
+impl PathResolver {
+  /// Returns the path to the resource directory of this app.
+  pub fn resource_dir(&self) -> Option<PathBuf> {
+    crate::api::path::resource_dir(&self.package_info)
+  }
+
+  /// Returns the path to the suggested directory for your app config files.
+  pub fn app_dir(&self) -> Option<PathBuf> {
+    crate::api::path::app_dir(&self.config)
+  }
+}
+
 crate::manager::default_args! {
   /// A handle to the currently running application.
   ///
@@ -187,6 +208,14 @@ macro_rules! shared_app_impl {
           .clone()
           .expect("tray not configured; use the `Builder#system_tray` API first.")
       }
+
+      /// The path resolver for the application.
+      pub fn path_resolver(&self) -> PathResolver {
+        PathResolver {
+          config: self.manager.config(),
+          package_info: self.manager.package_info().clone(),
+        }
+      }
     }
   };
 }