From 0dad6dd5b8395d3089bed022a4b8830f7cae7d9f Mon Sep 17 00:00:00 2001 From: pml68 Date: Mon, 30 Dec 2024 02:15:10 +0100 Subject: feat: add config loading, with theming support, limited to Palette for now --- iced_builder/src/environment.rs | 51 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 iced_builder/src/environment.rs (limited to 'iced_builder/src/environment.rs') diff --git a/iced_builder/src/environment.rs b/iced_builder/src/environment.rs new file mode 100644 index 0000000..52e7ca5 --- /dev/null +++ b/iced_builder/src/environment.rs @@ -0,0 +1,51 @@ +use std::env; +use std::path::PathBuf; + +pub const CONFIG_FILE_NAME: &str = "config.toml"; + +pub fn config_dir() -> PathBuf { + portable_dir().unwrap_or_else(platform_specific_config_dir) +} + +pub fn data_dir() -> PathBuf { + portable_dir().unwrap_or_else(|| { + dirs_next::data_dir() + .expect("expected valid data dir") + .join("iced-builder") + }) +} + +fn portable_dir() -> Option { + let exe = env::current_exe().ok()?; + let dir = exe.parent()?; + + dir.join(CONFIG_FILE_NAME) + .is_file() + .then(|| dir.to_path_buf()) +} + +fn platform_specific_config_dir() -> PathBuf { + #[cfg(target_os = "macos")] + { + xdg_config_dir().unwrap_or_else(|| { + dirs_next::config_dir() + .expect("expected valid config dir") + .join("iced-builder") + }) + } + #[cfg(not(target_os = "macos"))] + { + dirs_next::config_dir() + .expect("expected valid config dir") + .join("iced-builder") + } +} + +#[cfg(target_os = "macos")] +fn xdg_config_dir() -> Option { + let config_dir = xdg::BaseDirectories::with_prefix("iced-builder") + .ok() + .and_then(|xdg| xdg.find_config_file(CONFIG_FILE_NAME))?; + + config_dir.parent().map(|p| p.to_path_buf()) +} -- cgit v1.2.3