From 5ca462f6ccc6c970a6f2c8c6c1bc0e3343a52bfb Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Sat, 19 Jun 2021 14:47:30 -0300 Subject: [PATCH] feat(core): add path resolver API to the App and AppHandle structs (#2015) --- .changes/path-resolver.md | 5 +++++ core/tauri/src/app.rs | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 .changes/path-resolver.md diff --git a/.changes/path-resolver.md b/.changes/path-resolver.md new file mode 100644 index 000000000..2fc012fb1 --- /dev/null +++ b/.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. diff --git a/core/tauri/src/app.rs b/core/tauri/src/app.rs index 42af37451..24e1f5bb2 100644 --- a/core/tauri/src/app.rs +++ b/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 GlobalWindowEvent

{ } } +/// The path resolver is a helper for the application-specific [`crate::api::path`] APIs. +#[derive(Debug, Clone)] +pub struct PathResolver { + config: Arc, + package_info: PackageInfo, +} + +impl PathResolver { + /// Returns the path to the resource directory of this app. + pub fn resource_dir(&self) -> Option { + 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 { + 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(), + } + } } }; }