icedtea 0.17.2

Native desktop widgets and chrome for iced
Documentation
//! Save and restore window geometry, splits, docks, theme, and look.

use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};

use crate::density::{Density, DensityName};
use crate::layout::{Axis, SplitState};
use crate::m3::{ElevationPolicy, ShapePolicy};
use crate::theme::Tokens;

/// Window position and size.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct WindowGeom {
    pub x: f32,
    pub y: f32,
    pub width: f32,
    pub height: f32,
}

impl Default for WindowGeom {
    fn default() -> Self {
        Self {
            x: 0.0,
            y: 0.0,
            width: 960.0,
            height: 640.0,
        }
    }
}

/// Which edge a dock pane sits on.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum DockEdge {
    Left,
    Right,
    Top,
    Bottom,
}

/// Saved dock pane.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct DockLayout {
    pub edge: DockEdge,
    pub size: f32,
    pub visible: bool,
}

/// Full UI persistence blob.
///
/// ```
/// let mut ui = icedtea::persist::UiState::default();
/// ui.theme = "nord".into();
/// let json = ui.to_json().unwrap();
/// let back = icedtea::persist::UiState::from_json(&json).unwrap();
/// assert_eq!(back.theme, "nord");
/// ```
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct UiState {
    pub window: WindowGeom,
    pub splits: BTreeMap<String, f32>,
    pub docks: BTreeMap<String, DockLayout>,
    pub theme: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub family: Option<String>,
    #[serde(default)]
    pub follow_os: bool,
    pub density: DensityName,
    #[serde(default = "default_scale")]
    pub font_scale: f32,
    #[serde(default)]
    pub shape: ShapePolicy,
    #[serde(default)]
    pub elevation: ElevationPolicy,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub accent: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub workspace: Option<String>,
}

fn default_scale() -> f32 {
    1.0
}

impl Default for UiState {
    fn default() -> Self {
        Self {
            window: WindowGeom::default(),
            splits: BTreeMap::new(),
            docks: BTreeMap::new(),
            theme: "dark".into(),
            family: None,
            follow_os: true,
            density: DensityName::Default,
            font_scale: 1.0,
            shape: ShapePolicy::Desktop,
            elevation: ElevationPolicy::Desktop,
            accent: None,
            workspace: None,
        }
    }
}

impl UiState {
    /// Density, type scale, corners, and elevation from this blob.
    ///
    /// ```
    /// let mut ui = icedtea::persist::UiState::default();
    /// ui.font_scale = 1.25;
    /// ui.shape = icedtea::m3::ShapePolicy::Material;
    /// let tok = ui.look(icedtea::theme::named("dark").tokens);
    /// assert_eq!(tok.body(), 18.0);
    /// assert_eq!(tok.shape, icedtea::m3::ShapePolicy::Material);
    /// ```
    pub fn look(&self, tokens: Tokens) -> Tokens {
        tokens
            .with_density(Density::named(self.density))
            .with_font_scale(self.font_scale)
            .with_shape(self.shape)
            .with_elevation(self.elevation)
    }

    pub fn set_split(&mut self, id: impl Into<String>, state: SplitState) {
        self.splits.insert(id.into(), state.persist());
    }

    pub fn split(&self, id: &str, axis: Axis) -> SplitState {
        SplitState::restore(axis, self.splits.get(id).copied().unwrap_or(0.3))
    }

    pub fn to_json(&self) -> Result<String, serde_json::Error> {
        serde_json::to_string_pretty(self)
    }

    pub fn from_json(s: &str) -> Result<Self, serde_json::Error> {
        serde_json::from_str(s)
    }

    pub fn config_path(app_id: &str) -> PathBuf {
        config_dir(
            env_path("XDG_CONFIG_HOME"),
            env_path("HOME"),
            env_path("APPDATA"),
        )
        .join(app_id)
        .join("ui.json")
    }

    /// Read `path` and map the result into an application message.
    pub fn load_file<M: Send + 'static>(
        path: impl AsRef<Path>,
        to_msg: impl FnOnce(Result<Self, PersistError>) -> M,
    ) -> iced::Task<M> {
        iced::Task::done(to_msg(Self::read_path(path.as_ref())))
    }

    /// Write this blob to `path` and map the result into an application message.
    pub fn save_file<M: Send + 'static>(
        &self,
        path: impl AsRef<Path>,
        to_msg: impl FnOnce(Result<(), PersistError>) -> M,
    ) -> iced::Task<M> {
        iced::Task::done(to_msg(self.write_path(path.as_ref())))
    }

    fn read_path(path: &Path) -> Result<Self, PersistError> {
        let text = std::fs::read_to_string(path)?;
        Ok(Self::from_json(&text)?)
    }

    fn write_path(&self, path: &Path) -> Result<(), PersistError> {
        if let Some(dir) = parent_dir(path) {
            std::fs::create_dir_all(dir)?;
        }
        std::fs::write(path, self.to_json()?)?;
        Ok(())
    }
}

/// Directory to create before writing `path`, if any.
pub fn parent_dir(path: &Path) -> Option<&Path> {
    path.parent().filter(|d| !d.as_os_str().is_empty())
}

fn env_path(key: &str) -> Option<PathBuf> {
    std::env::var_os(key).map(PathBuf::from)
}

fn config_dir(
    xdg_config_home: Option<PathBuf>,
    home: Option<PathBuf>,
    appdata: Option<PathBuf>,
) -> PathBuf {
    xdg_config_home
        .or_else(|| home.map(|h| h.join(".config")))
        .or(appdata)
        .unwrap_or_else(|| PathBuf::from("."))
}

/// Persistence error.
#[derive(Debug, thiserror::Error)]
pub enum PersistError {
    #[error("io: {0}")]
    Io(#[from] std::io::Error),
    #[error("json: {0}")]
    Json(#[from] serde_json::Error),
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::layout::SplitState;

    #[test]
    fn json_roundtrip_and_split_restore() {
        let mut ui = UiState::default();
        assert!(ui.follow_os);
        ui.set_split("main", SplitState::new(Axis::Horizontal, 0.4));
        ui.docks.insert(
            "nav".into(),
            DockLayout {
                edge: DockEdge::Left,
                size: 220.0,
                visible: true,
            },
        );
        ui.theme = "light".into();
        ui.family = Some("github".into());
        ui.follow_os = true;
        ui.density = DensityName::Compact;
        ui.font_scale = 1.125;
        ui.shape = ShapePolicy::Material;
        ui.elevation = ElevationPolicy::Flat;
        ui.accent = Some("#88c0d0".into());
        ui.workspace = Some(r#"{"Leaf":{"id":"e","title":"E","min":80.0,"max":10000.0}}"#.into());
        ui.window.x = 12.0;
        let json = ui.to_json().unwrap();
        let back = UiState::from_json(&json).unwrap();
        assert_eq!(back, ui);
        assert!((back.split("main", Axis::Horizontal).ratio - 0.4).abs() < 0.02);
        assert_eq!(back.split("missing", Axis::Vertical).axis, Axis::Vertical);
        assert!(UiState::from_json("not-json").is_err());
        let p = UiState::config_path("icedtea-test");
        assert!(p.ends_with("ui.json"));
        let dir = std::env::temp_dir().join("icedtea-persist-test");
        let file = dir.join("ui.json");
        let mut save = None;
        let _ = ui.save_file(&file, |r| {
            save = Some(r);
        });
        save.unwrap().unwrap();
        let mut loaded = None;
        let _ = UiState::load_file(&file, |r| {
            loaded = Some(r);
        });
        let loaded = loaded.unwrap().unwrap();
        assert_eq!(loaded.theme, "light");
        assert_eq!(loaded.family.as_deref(), Some("github"));
        assert!(loaded.follow_os);
        let named_only = UiState::from_json(r#"{"window":{"x":0,"y":0,"width":1,"height":1},"splits":{},"docks":{},"theme":"nord","density":"Default"}"#).unwrap();
        assert_eq!(named_only.theme, "nord");
        assert!(named_only.family.is_none());
        assert!(!named_only.follow_os);
        assert!((named_only.font_scale - 1.0).abs() < f32::EPSILON);
        assert_eq!(named_only.shape, ShapePolicy::Desktop);
        assert_eq!(named_only.elevation, ElevationPolicy::Desktop);
        let looked = back.look(crate::theme::named("dark").tokens);
        assert!((looked.font_scale - 1.125).abs() < f32::EPSILON);
        assert_eq!(looked.shape, ShapePolicy::Material);
        assert_eq!(looked.elevation, ElevationPolicy::Flat);
        assert_eq!(looked.density.name, DensityName::Compact);
        assert!(named_only.accent.is_none());
        assert!(named_only.workspace.is_none());
        assert!((back.font_scale - 1.125).abs() < f32::EPSILON);
        assert_eq!(back.accent.as_deref(), Some("#88c0d0"));
        let mut missing = None;
        let _ = UiState::load_file(Path::new("/no/such/icedtea.json"), |r| {
            missing = Some(r);
        });
        assert!(missing.unwrap().is_err());
        let blocker = std::env::temp_dir().join("icedtea-persist-not-a-dir");
        std::fs::write(&blocker, b"x").unwrap();
        let mut blocked = None;
        let _ = ui.save_file(blocker.join("ui.json"), |r| {
            blocked = Some(r);
        });
        assert!(blocked.unwrap().is_err());
        let _ = std::fs::remove_file(&blocker);
        let rel = std::env::temp_dir().join("icedtea-rel-only.json");
        let mut rel_save = None;
        let _ = ui.save_file(&rel, |r| {
            rel_save = Some(r);
        });
        rel_save.unwrap().unwrap();
        let _ = std::fs::remove_file(&rel);
        let cwd_file = Path::new("icedtea-persist-cwd.json");
        let mut cwd_save = None;
        let _ = ui.save_file(cwd_file, |r| {
            cwd_save = Some(r);
        });
        cwd_save.unwrap().unwrap();
        let mut loaded_cwd = None;
        let _ = UiState::load_file(cwd_file, |r| {
            loaded_cwd = Some(r);
        });
        let loaded_cwd = loaded_cwd.unwrap().unwrap();
        assert_eq!(loaded_cwd.theme, "light");
        let _ = std::fs::remove_file(cwd_file);
        assert!(parent_dir(Path::new("ui.json")).is_none());
        assert!(parent_dir(&std::env::temp_dir().join("ui.json")).is_some());
        assert_eq!(
            config_dir(Some(PathBuf::from("xdg")), None, None),
            PathBuf::from("xdg")
        );
        assert_eq!(
            config_dir(None, Some(PathBuf::from("home")), None),
            PathBuf::from("home").join(".config")
        );
        assert_eq!(
            config_dir(None, None, Some(PathBuf::from("appdata"))),
            PathBuf::from("appdata")
        );
        assert_eq!(config_dir(None, None, None), PathBuf::from("."));
        let io_err = PersistError::from(std::io::Error::other("disk"));
        assert!(io_err.to_string().contains("io:"));
        let json_err = PersistError::from(serde_json::from_str::<UiState>("[").unwrap_err());
        assert!(json_err.to_string().contains("json:"));
        for edge in [
            DockEdge::Left,
            DockEdge::Right,
            DockEdge::Top,
            DockEdge::Bottom,
        ] {
            let _ = edge;
        }
    }
}