Skip to main content

egui_pop_api/
dirs.rs

1//! Where an application keeps its settings: the user's configuration
2//! folder, as each platform names it.
3//!
4//! | | Configuration |
5//! |---|---|
6//! | Linux and BSD | `$XDG_CONFIG_HOME/<app>` (`~/.config/<app>`) |
7//! | macOS | `~/Library/Application Support/<app>` |
8//! | Windows | `%APPDATA%\<app>` |
9//!
10//! `AppBuilder::persist_in_config` saves the settings, the layout and
11//! the window's place there. Inside a Flatpak sandbox,
12//! `XDG_CONFIG_HOME` points into the application's own directory, so
13//! the same code finds the sandbox's place. The state, the logs and the
14//! caches are elsewhere (`egui_pop_platform::dirs`).
15//!
16//! ```
17//! let dir = egui_pop_api::dirs::config_dir_with("demo", |name| match name {
18//!     "XDG_CONFIG_HOME" => Some("/config".into()),
19//!     _ => None,
20//! });
21//! if cfg!(all(unix, not(target_os = "macos"))) {
22//!     assert_eq!(dir, Some("/config/demo".into()));
23//! }
24//! ```
25
26use std::path::PathBuf;
27
28/// The application's configuration folder, from the environment `env`
29/// gives (`None`: the variables it needs are missing, or on the web).
30pub fn config_dir_with(app: &str, env: impl Fn(&str) -> Option<String>) -> Option<PathBuf> {
31    if cfg!(target_arch = "wasm32") {
32        return None;
33    }
34    let absolute = |v: Option<String>| v.map(PathBuf::from).filter(|p| p.is_absolute());
35    let base = if cfg!(windows) {
36        absolute(env("APPDATA"))?
37    } else if cfg!(target_os = "macos") {
38        absolute(env("HOME"))?.join("Library/Application Support")
39    } else {
40        match absolute(env("XDG_CONFIG_HOME")) {
41            Some(p) => p,
42            None => absolute(env("HOME"))?.join(".config"),
43        }
44    };
45    Some(base.join(app))
46}
47
48/// The application's configuration folder (see the module).
49pub fn config_dir(app: &str) -> Option<PathBuf> {
50    config_dir_with(app, |name| std::env::var(name).ok())
51}
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn the_folder_follows_the_platform() {
59        let env = |name: &str| match name {
60            "HOME" => Some("/home/me".to_string()),
61            "APPDATA" => Some("C:\\Users\\me\\AppData\\Roaming".to_string()),
62            _ => None,
63        };
64        let dir = config_dir_with("swatch", env).unwrap();
65        if cfg!(windows) {
66            assert!(dir.ends_with("Roaming\\swatch"), "{dir:?}");
67        } else if cfg!(target_os = "macos") {
68            assert_eq!(
69                dir,
70                PathBuf::from("/home/me/Library/Application Support/swatch")
71            );
72        } else {
73            assert_eq!(dir, PathBuf::from("/home/me/.config/swatch"));
74        }
75        // A relative or missing base names no place.
76        assert_eq!(config_dir_with("swatch", |_| None), None);
77        let relative =
78            |name: &str| (name == "XDG_CONFIG_HOME" || name == "HOME").then(|| "rel".to_string());
79        if cfg!(all(unix, not(target_os = "macos"))) {
80            assert_eq!(config_dir_with("swatch", relative), None);
81        }
82    }
83}