mirror of
https://github.com/emilk/egui.git
synced 2026-08-31 05:40:03 -04:00
Remove the directories dependency (#4904)
eframe now has its own logic to find the storage_dir to persist the app when the persistence feature is enabled, instead of using the directories crate. The directory should be the same as before (verified with a unit test). * Closes <https://github.com/emilk/egui/issues/4884> * [x] I have followed the instructions in the PR template
This commit is contained in:
@@ -351,8 +351,8 @@ pub struct NativeOptions {
|
||||
/// persisted (only if the "persistence" feature is enabled).
|
||||
pub persist_window: bool,
|
||||
|
||||
/// The folder where `eframe` will store the app state. If not set, eframe will get the paths
|
||||
/// from [directories].
|
||||
/// The folder where `eframe` will store the app state. If not set, eframe will use a default
|
||||
/// data storage path for each target system.
|
||||
pub persistence_path: Option<std::path::PathBuf>,
|
||||
|
||||
/// Controls whether to apply dithering to minimize banding artifacts.
|
||||
|
||||
@@ -10,13 +10,80 @@ use std::{
|
||||
/// [`egui::ViewportBuilder::app_id`] of [`crate::NativeOptions::viewport`]
|
||||
/// or the title argument to [`crate::run_native`].
|
||||
///
|
||||
/// On native the path is picked using [`directories::ProjectDirs::data_dir`](https://docs.rs/directories/5.0.1/directories/struct.ProjectDirs.html#method.data_dir) which is:
|
||||
/// On native, the path is:
|
||||
/// * Linux: `/home/UserName/.local/share/APP_ID`
|
||||
/// * macOS: `/Users/UserName/Library/Application Support/APP_ID`
|
||||
/// * Windows: `C:\Users\UserName\AppData\Roaming\APP_ID`
|
||||
/// * Windows: `C:\Users\UserName\AppData\Roaming\APP_ID\data`
|
||||
pub fn storage_dir(app_id: &str) -> Option<PathBuf> {
|
||||
directories::ProjectDirs::from("", "", app_id)
|
||||
.map(|proj_dirs| proj_dirs.data_dir().to_path_buf())
|
||||
use egui::os::OperatingSystem as OS;
|
||||
use std::env::var_os;
|
||||
match OS::from_target_os() {
|
||||
OS::Nix => var_os("XDG_DATA_HOME")
|
||||
.map(PathBuf::from)
|
||||
.filter(|p| p.is_absolute())
|
||||
.or_else(|| home::home_dir().map(|p| p.join(".local").join("share")))
|
||||
.map(|p| {
|
||||
p.join(
|
||||
app_id
|
||||
.to_lowercase()
|
||||
.replace(|c: char| c.is_ascii_whitespace(), ""),
|
||||
)
|
||||
}),
|
||||
OS::Mac => home::home_dir().map(|p| {
|
||||
p.join("Library")
|
||||
.join("Application Support")
|
||||
.join(app_id.replace(|c: char| c.is_ascii_whitespace(), "-"))
|
||||
}),
|
||||
OS::Windows => roaming_appdata().map(|p| p.join(app_id).join("data")),
|
||||
OS::Unknown | OS::Android | OS::IOS => None,
|
||||
}
|
||||
}
|
||||
|
||||
// Adapted from
|
||||
// https://github.com/rust-lang/cargo/blob/6e11c77384989726bb4f412a0e23b59c27222c34/crates/home/src/windows.rs#L19-L37
|
||||
#[cfg(all(windows, not(target_vendor = "uwp")))]
|
||||
#[allow(unsafe_code)]
|
||||
fn roaming_appdata() -> Option<PathBuf> {
|
||||
use std::ffi::OsString;
|
||||
use std::os::windows::ffi::OsStringExt;
|
||||
use std::ptr;
|
||||
use std::slice;
|
||||
|
||||
use windows_sys::Win32::Foundation::S_OK;
|
||||
use windows_sys::Win32::System::Com::CoTaskMemFree;
|
||||
use windows_sys::Win32::UI::Shell::{
|
||||
FOLDERID_RoamingAppData, SHGetKnownFolderPath, KF_FLAG_DONT_VERIFY,
|
||||
};
|
||||
|
||||
extern "C" {
|
||||
fn wcslen(buf: *const u16) -> usize;
|
||||
}
|
||||
unsafe {
|
||||
let mut path = ptr::null_mut();
|
||||
match SHGetKnownFolderPath(
|
||||
&FOLDERID_RoamingAppData,
|
||||
KF_FLAG_DONT_VERIFY as u32,
|
||||
0,
|
||||
&mut path,
|
||||
) {
|
||||
S_OK => {
|
||||
let path_slice = slice::from_raw_parts(path, wcslen(path));
|
||||
let s = OsString::from_wide(&path_slice);
|
||||
CoTaskMemFree(path.cast());
|
||||
Some(PathBuf::from(s))
|
||||
}
|
||||
_ => {
|
||||
// Free any allocated memory even on failure. A null ptr is a no-op for `CoTaskMemFree`.
|
||||
CoTaskMemFree(path.cast());
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(not(windows), target_vendor = "uwp"))]
|
||||
fn roaming_appdata() -> Option<PathBuf> {
|
||||
None
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
@@ -171,3 +238,23 @@ where
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn directories_storage_dir(app_id: &str) -> Option<PathBuf> {
|
||||
directories::ProjectDirs::from("", "", app_id)
|
||||
.map(|proj_dirs| proj_dirs.data_dir().to_path_buf())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn storage_path_matches_directories() {
|
||||
use super::storage_dir;
|
||||
for app_id in [
|
||||
"MyApp", "My App", "my_app", "my-app", "My.App", "my/app", "my:app", r"my\app",
|
||||
] {
|
||||
assert_eq!(directories_storage_dir(app_id), storage_dir(app_id));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user