use crate::model::{settings, state};
use toml_edit::DocumentMut;
const KEYS: [(&str, &str); 2] = [("layout", "space"), ("layouts", "spaces")];
pub fn run() {
carry_directory();
clear_session_panels();
let Some(path) = state::path() else {
return;
};
let Some(mut document) = std::fs::read_to_string(&path)
.ok()
.and_then(|body| body.parse::<DocumentMut>().ok())
else {
return;
};
if rename_keys(&mut document) {
let _ = std::fs::write(&path, document.to_string());
}
}
fn carry_directory() {
let Ok(dir) = settings::dir() else {
return;
};
let (from, to) = (dir.join("layouts"), dir.join("spaces"));
if !from.is_dir() || to.exists() {
return;
}
let _ = std::fs::rename(from, to);
}
pub fn rename_keys(state: &mut DocumentMut) -> bool {
let mut renamed = false;
for (from, to) in KEYS {
let Some(value) = state.get(from).and_then(|held| held.as_value()).cloned() else {
continue;
};
state.remove(from);
if !state.contains_key(to) {
state[to] = toml_edit::value(value);
}
renamed = true;
}
renamed
}
fn panels_path() -> Option<std::path::PathBuf> {
settings::dir()
.ok()
.map(|dir| dir.join("right-panels.json"))
}
fn clear_session_panels() {
let Some(path) = panels_path() else {
return;
};
let Ok(body) = std::fs::read_to_string(&path) else {
return;
};
let Some(width) = legacy_width(&body) else {
return;
};
let kept = match width {
Some(width) => format!("{{\"width\":{width}}}"),
None => "{}".to_string(),
};
let _ = std::fs::write(&path, kept);
}
pub fn legacy_width(body: &str) -> Option<Option<f64>> {
let file: serde_json::Value = serde_json::from_str(body).ok()?;
let legacy = file
.get("projects")?
.as_object()?
.values()
.any(|held| held.get("tabs").is_none());
legacy.then(|| file.get("width").and_then(serde_json::Value::as_f64))
}