Skip to content

Commit 5ca462f

Browse files
authored
feat(core): add path resolver API to the App and AppHandle structs (#2015)
1 parent 86d0aaa commit 5ca462f

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

.changes/path-resolver.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
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.

core/tauri/src/app.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub(crate) mod tray;
88

99
use crate::{
1010
api::assets::Assets,
11-
api::config::WindowUrl,
11+
api::config::{Config, WindowUrl},
1212
hooks::{InvokeHandler, OnPageLoad, PageLoadPayload, SetupHook},
1313
manager::{Args, WindowManager},
1414
plugin::{Plugin, PluginStore},
@@ -22,7 +22,9 @@ use crate::{
2222
Context, Invoke, Manager, StateManager, Window,
2323
};
2424

25-
use std::{collections::HashMap, sync::Arc};
25+
use tauri_utils::PackageInfo;
26+
27+
use std::{collections::HashMap, path::PathBuf, sync::Arc};
2628

2729
#[cfg(feature = "menu")]
2830
use crate::runtime::menu::Menu;
@@ -85,6 +87,25 @@ impl<P: Params> GlobalWindowEvent<P> {
8587
}
8688
}
8789

90+
/// The path resolver is a helper for the application-specific [`crate::api::path`] APIs.
91+
#[derive(Debug, Clone)]
92+
pub struct PathResolver {
93+
config: Arc<Config>,
94+
package_info: PackageInfo,
95+
}
96+
97+
impl PathResolver {
98+
/// Returns the path to the resource directory of this app.
99+
pub fn resource_dir(&self) -> Option<PathBuf> {
100+
crate::api::path::resource_dir(&self.package_info)
101+
}
102+
103+
/// Returns the path to the suggested directory for your app config files.
104+
pub fn app_dir(&self) -> Option<PathBuf> {
105+
crate::api::path::app_dir(&self.config)
106+
}
107+
}
108+
88109
crate::manager::default_args! {
89110
/// A handle to the currently running application.
90111
///
@@ -187,6 +208,14 @@ macro_rules! shared_app_impl {
187208
.clone()
188209
.expect("tray not configured; use the `Builder#system_tray` API first.")
189210
}
211+
212+
/// The path resolver for the application.
213+
pub fn path_resolver(&self) -> PathResolver {
214+
PathResolver {
215+
config: self.manager.config(),
216+
package_info: self.manager.package_info().clone(),
217+
}
218+
}
190219
}
191220
};
192221
}

0 commit comments

Comments
 (0)