Skip to content

Commit

Permalink
feat(core): add path resolver API to the App and AppHandle structs (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog authored Jun 19, 2021
1 parent 86d0aaa commit 5ca462f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changes/path-resolver.md
Original file line number Diff line number Diff line change
@@ -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.
33 changes: 31 additions & 2 deletions core/tauri/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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;
Expand Down Expand Up @@ -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.
///
Expand Down Expand Up @@ -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(),
}
}
}
};
}
Expand Down

1 comment on commit 5ca462f

@parker-codes
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😁 Thanks!

Please sign in to comment.