fluxframe-gui 0.12.1

GTK4/libadwaita GUI client for the FluxFrame control socket.
//! Lightweight window-geometry persistence.
//!
//! We deliberately avoid `gio::Settings` here because that would
//! require an installed GSchema XML file at
//! `~/.local/share/glib-2.0/schemas/io.fluxframe.gui.gschema.xml`
//! plus a `glib-compile-schemas` step. A plain JSON file in
//! `$XDG_CONFIG_HOME/fluxframe/gui.json` is good enough for a single
//! window's width/height/maximised state and works out of the box
//! without any installer scaffolding.

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

use serde::{Deserialize, Serialize};

/// Minimum window dimensions enforced on load. 360 px is the narrowest
/// width the chain page's param rows lay out in.
const MIN_WIDTH: i32 = 360;
const MIN_HEIGHT: i32 = 150;
/// Upper guard against a hand-edited gui.json with absurd values.
/// 8192 covers any reasonable desktop resolution while leaving plenty
/// of headroom.
const MAX_WIDTH: i32 = 8192;
const MAX_HEIGHT: i32 = 8192;

/// UI policy: preview occupies 1/N of the window height by default.
const PREVIEW_SPLIT_DEFAULT_FRACTION: i32 = 3;

/// Persisted window geometry — what we record between runs.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct WindowState {
    /// Width in CSS pixels (gtk's logical unit).
    pub width: i32,
    /// Height in CSS pixels.
    pub height: i32,
    /// Whether the window was maximised when it last closed.
    pub maximized: bool,
    /// Pixel offset of the `gtk::Paned` divider between the live
    /// preview pane and the chain editor, measured from the top of
    /// the Paned. `None` means "no preference recorded yet — compute
    /// `height / PREVIEW_SPLIT_DEFAULT_FRACTION` at apply time".
    /// `serde` defaults to `None` when the field is missing, which
    /// makes old gui.json files (pre-Paned) still load cleanly.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub preview_split: Option<i32>,
}

impl Default for WindowState {
    fn default() -> Self {
        Self {
            width: 720,
            height: 540,
            maximized: false,
            preview_split: None,
        }
    }
}

impl WindowState {
    /// Resolve the initial `gtk::Paned::position` to apply.
    ///
    /// Persisted positive values are clamped to the current window
    /// height so a tall-window-saved split doesn't push the preview
    /// off-screen when the window opens at a smaller size. `None` and
    /// any non-positive value fall back to one
    /// `PREVIEW_SPLIT_DEFAULT_FRACTION`-th of the current window
    /// height — matches the user's "upper third" expectation.
    pub fn resolved_preview_split(&self) -> i32 {
        match self.preview_split {
            Some(p) if p > 0 => p.clamp(MIN_HEIGHT / PREVIEW_SPLIT_DEFAULT_FRACTION, self.height),
            _ => (self.height / PREVIEW_SPLIT_DEFAULT_FRACTION)
                .max(MIN_HEIGHT / PREVIEW_SPLIT_DEFAULT_FRACTION),
        }
    }
}

/// Clamp width/height into the supported bounds. Self-healing for
/// pathological values coming from a corrupt or hand-edited gui.json
/// (negative, zero, or absurdly large).
fn sanitize(mut state: WindowState) -> WindowState {
    state.width = state.width.clamp(MIN_WIDTH, MAX_WIDTH);
    state.height = state.height.clamp(MIN_HEIGHT, MAX_HEIGHT);
    // Defensive normalisation: a hand-edited non-positive value lands
    // here as `Some(<=0)`. Collapse it to `None` so a round-trip
    // through save/load doesn't preserve the absurd value, and so
    // `resolved_preview_split` deals with a single "unset" shape.
    if state.preview_split.is_some_and(|p| p <= 0) {
        state.preview_split = None;
    }
    state
}

// Linux-only by construction: XDG_CONFIG_HOME / $HOME/.config
// (the daemon itself depends on V4L2); a future Windows port should
// consult dirs::config_dir() instead.
/// Resolve the on-disk path for the persisted state.
///
/// Honours a non-empty `$XDG_CONFIG_HOME`; falls back to
/// `$HOME/.config`. The `fluxframe` subdirectory is created on first
/// save.
fn config_path() -> Option<PathBuf> {
    let base = fluxframe_core::paths::config_home()?;
    Some(config_path_in(&base))
}

/// Build the on-disk path inside an explicit XDG config root.
///
/// Split out from [`config_path`] so unit tests can target a
/// scratch dir without mutating the process-wide `XDG_CONFIG_HOME`
/// env var — the workspace forbids `unsafe_code`, which rules out the
/// Rust 2024 `std::env::set_var` (now `unsafe`).
fn config_path_in(base: &Path) -> PathBuf {
    base.join("fluxframe").join("gui.json")
}

/// Read the persisted window state from disk.
///
/// Returns the default on any failure (missing file, unreadable,
/// malformed JSON). All failures are logged at debug level so
/// first-run noise stays quiet. Successfully loaded state is clamped
/// to `[MIN_*, MAX_*]` bounds so hand-edited absurdities never reach
/// gtk.
pub(crate) fn load() -> WindowState {
    let Some(path) = config_path() else {
        tracing::debug!("no XDG_CONFIG_HOME or HOME — skipping load");
        return WindowState::default();
    };
    load_from(&path)
}

/// Read the persisted window state from an explicit path.
///
/// Test seam — see [`config_path_in`] for why this split exists.
fn load_from(path: &Path) -> WindowState {
    match std::fs::read_to_string(path) {
        Ok(s) => match serde_json::from_str::<WindowState>(&s) {
            Ok(state) => {
                tracing::debug!(?path, ?state, "loaded window state");
                sanitize(state)
            }
            Err(e) => {
                tracing::debug!(error = %e, ?path, "window state parse failed, using default");
                WindowState::default()
            }
        },
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => WindowState::default(),
        Err(e) => {
            tracing::debug!(error = %e, ?path, "window state read failed, using default");
            WindowState::default()
        }
    }
}

/// Persist the window state to disk.
///
/// Creates the parent directory if it does not exist. Writes go
/// through a sibling `*.tmp` file followed by `rename` so a crash or
/// power loss mid-write cannot corrupt the live `gui.json`; the
/// `sanitize` step in `load` heals any leftover damage from a
/// pre-atomic-write file. Failures are logged at warn level
/// (persistence is best-effort, not critical).
pub(crate) fn save(state: &WindowState) {
    let Some(path) = config_path() else {
        return;
    };
    save_to(&path, state);
}

/// Persist the window state to an explicit path.
///
/// Test seam — see [`config_path_in`] for why this split exists.
fn save_to(path: &Path, state: &WindowState) {
    if let Some(parent) = path.parent()
        && let Err(e) = std::fs::create_dir_all(parent)
    {
        tracing::warn!(error = %e, ?parent, "failed to create config dir");
        return;
    }
    let payload = match serde_json::to_string_pretty(state) {
        Ok(s) => s,
        Err(e) => {
            tracing::warn!(error = %e, "window state serialise failed");
            return;
        }
    };
    let tmp_path = path.with_extension("json.tmp");
    if let Err(e) = std::fs::write(&tmp_path, payload) {
        tracing::warn!(error = %e, ?tmp_path, "window state write failed");
        return;
    }
    if let Err(e) = std::fs::rename(&tmp_path, path) {
        tracing::warn!(error = %e, ?path, "window state rename failed");
        // Best-effort cleanup; ignore failure.
        let _ = std::fs::remove_file(&tmp_path);
    }
}

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

    // These tests deliberately do NOT touch process env vars. The
    // workspace pins `unsafe_code = "forbid"`, which makes the
    // Rust 2024 `std::env::set_var` (now `unsafe`) unusable here.
    // Instead each test uses the `config_path_in` / `load_from` /
    // `save_to` seam to drive an isolated tempdir directly. That
    // also sidesteps the cargo-parallel-tests race that XDG env
    // mutation would otherwise introduce.

    #[test]
    fn load_missing_returns_default() {
        let tmp = tempfile::tempdir().expect("create tempdir");
        let path = config_path_in(tmp.path());
        let state = load_from(&path);
        let def = WindowState::default();
        assert_eq!(state.width, def.width);
        assert_eq!(state.height, def.height);
        assert_eq!(state.maximized, def.maximized);
    }

    #[test]
    fn save_then_load_roundtrip() {
        let tmp = tempfile::tempdir().expect("create tempdir");
        let path = config_path_in(tmp.path());
        let original = WindowState {
            width: 1024,
            height: 768,
            maximized: true,
            preview_split: Some(256),
        };
        save_to(&path, &original);
        let loaded = load_from(&path);
        assert_eq!(loaded.width, original.width);
        assert_eq!(loaded.height, original.height);
        assert_eq!(loaded.maximized, original.maximized);
        assert_eq!(loaded.preview_split, original.preview_split);
    }

    #[test]
    fn missing_preview_split_loads_as_unset() {
        let tmp = tempfile::tempdir().expect("create tempdir");
        let path = config_path_in(tmp.path());
        let dir = path.parent().expect("path has parent");
        std::fs::create_dir_all(dir).expect("create fluxframe dir");
        // Simulate a pre-Paned gui.json (no preview_split field).
        std::fs::write(
            &path,
            r#"{"width": 800, "height": 600, "maximized": false}"#,
        )
        .expect("write json");
        let state = load_from(&path);
        assert_eq!(state.preview_split, None);
        // height / PREVIEW_SPLIT_DEFAULT_FRACTION = 600 / 3 = 200
        assert_eq!(state.resolved_preview_split(), 200);
    }

    #[test]
    fn negative_preview_split_is_sanitized_to_unset() {
        let tmp = tempfile::tempdir().expect("create tempdir");
        let path = config_path_in(tmp.path());
        let dir = path.parent().expect("path has parent");
        std::fs::create_dir_all(dir).expect("create fluxframe dir");
        std::fs::write(
            &path,
            r#"{"width": 800, "height": 600, "maximized": false, "preview_split": -42}"#,
        )
        .expect("write json");
        let state = load_from(&path);
        assert_eq!(state.preview_split, None);
    }

    #[test]
    fn resolved_preview_split_clamps_above_height() {
        let state = WindowState {
            width: 800,
            height: 600,
            maximized: false,
            preview_split: Some(9999),
        };
        assert_eq!(state.resolved_preview_split(), 600);
    }

    #[test]
    fn resolved_preview_split_clamps_below_min() {
        let state = WindowState {
            width: 800,
            height: 600,
            maximized: false,
            preview_split: Some(10),
        };
        assert_eq!(
            state.resolved_preview_split(),
            MIN_HEIGHT / PREVIEW_SPLIT_DEFAULT_FRACTION,
        );
    }

    #[test]
    fn resolved_preview_split_passes_through_valid_range() {
        let state = WindowState {
            width: 800,
            height: 600,
            maximized: false,
            preview_split: Some(250),
        };
        assert_eq!(state.resolved_preview_split(), 250);
    }

    #[test]
    fn load_corrupt_json_returns_default() {
        let tmp = tempfile::tempdir().expect("create tempdir");
        let path = config_path_in(tmp.path());
        let dir = path.parent().expect("path has parent");
        std::fs::create_dir_all(dir).expect("create fluxframe dir");
        std::fs::write(&path, "not valid json").expect("write corrupt json");
        let state = load_from(&path);
        let def = WindowState::default();
        assert_eq!(state.width, def.width);
        assert_eq!(state.height, def.height);
        assert_eq!(state.maximized, def.maximized);
    }

    #[test]
    fn load_clamps_negative_dimensions() {
        let tmp = tempfile::tempdir().expect("create tempdir");
        let path = config_path_in(tmp.path());
        let dir = path.parent().expect("path has parent");
        std::fs::create_dir_all(dir).expect("create fluxframe dir");
        std::fs::write(&path, r#"{"width": -5, "height": 0, "maximized": false}"#)
            .expect("write json");
        let state = load_from(&path);
        assert_eq!(state.width, MIN_WIDTH);
        assert_eq!(state.height, MIN_HEIGHT);
        assert!(!state.maximized);
    }

    #[test]
    fn load_clamps_huge_dimensions() {
        let tmp = tempfile::tempdir().expect("create tempdir");
        let path = config_path_in(tmp.path());
        let dir = path.parent().expect("path has parent");
        std::fs::create_dir_all(dir).expect("create fluxframe dir");
        std::fs::write(
            &path,
            r#"{"width": 999999, "height": 888888, "maximized": true}"#,
        )
        .expect("write json");
        let state = load_from(&path);
        assert_eq!(state.width, MAX_WIDTH);
        assert_eq!(state.height, MAX_HEIGHT);
        assert!(state.maximized);
    }

    #[test]
    fn save_atomic_write_does_not_leave_tmp_on_success() {
        let tmp = tempfile::tempdir().expect("create tempdir");
        let path = config_path_in(tmp.path());
        let state = WindowState {
            width: 800,
            height: 600,
            maximized: false,
            preview_split: None,
        };
        save_to(&path, &state);
        let dir = path.parent().expect("path has parent");
        let entries: Vec<_> = std::fs::read_dir(dir)
            .expect("read fluxframe dir")
            .filter_map(Result::ok)
            .map(|e| e.file_name().to_string_lossy().into_owned())
            .collect();
        assert!(
            entries.iter().any(|n| n == "gui.json"),
            "gui.json should exist after save, got: {entries:?}",
        );
        assert!(
            !entries.iter().any(|n| std::path::Path::new(n)
                .extension()
                .is_some_and(|ext| ext.eq_ignore_ascii_case("tmp"))),
            "no .tmp file should remain after successful save, got: {entries:?}",
        );
    }
}