use std::collections::BTreeMap;
use std::path::PathBuf;
use remedy_ui_core::reactive::{JsonValue, Persist, PersistStore};
use remedy_ui_core::{Point, Size};
pub const GEOMETRY_KEY: &str = "remedy-ui.windows";
#[derive(Clone, Debug, Default)]
pub struct GeometryStore {
path: Option<PathBuf>,
store: Option<PersistStore>,
entries: BTreeMap<String, (Point, Size)>,
}
impl PartialEq for GeometryStore {
fn eq(&self, other: &Self) -> bool {
let place = |s: &GeometryStore| {
(
s.path.clone(),
s.store.as_ref().map(|p| (p.dir().to_path_buf(), p.storage_key_of(GEOMETRY_KEY))),
)
};
self.entries == other.entries && place(self) == place(other)
}
}
fn valid(x: f32, y: f32, w: f32, h: f32) -> bool {
[x, y, w, h].iter().all(|v| v.is_finite()) && w > 0.0 && h > 0.0
}
impl GeometryStore {
pub fn open(path: PathBuf) -> Self {
let mut entries = BTreeMap::new();
if let Ok(text) = std::fs::read_to_string(&path) {
for line in text.lines() {
let mut parts = line.splitn(5, ' ');
let nums: Vec<f32> = parts.by_ref().take(4).filter_map(|p| p.parse().ok()).collect();
if let ([x, y, w, h], Some(key)) = (nums.as_slice(), parts.next())
&& valid(*x, *y, *w, *h)
{
entries.insert(key.to_owned(), (Point::new(*x, *y), Size::new(*w, *h)));
}
}
}
GeometryStore { path: Some(path), store: None, entries }
}
pub fn persisted_in(store: &PersistStore) -> Self {
let mut entries = BTreeMap::new();
if let Ok(Some(JsonValue::Array(rows))) = store.load::<JsonValue>(&Persist::new(GEOMETRY_KEY)) {
for row in rows {
let JsonValue::Array(cells) = row else { continue };
let num = |i: usize| cells.get(i).and_then(JsonValue::as_f64).map(|v| v as f32);
if let (Some(JsonValue::String(key)), Some(x), Some(y), Some(w), Some(h)) =
(cells.first(), num(1), num(2), num(3), num(4))
&& valid(x, y, w, h)
{
entries.insert(key.clone(), (Point::new(x, y), Size::new(w, h)));
}
}
}
GeometryStore { path: None, store: Some(store.clone()), entries }
}
pub fn in_memory() -> Self {
Self::default()
}
pub fn get(&self, key: &str) -> Option<(Point, Size)> {
self.entries.get(key).copied()
}
pub fn set(&mut self, key: &str, position: Point, size: Size) {
self.entries.insert(key.to_owned(), (position, size));
}
pub fn remove(&mut self, key: &str) {
self.entries.remove(key);
}
pub fn save(&self) -> std::io::Result<()> {
if let Some(store) = &self.store {
let rows: Vec<JsonValue> = self
.entries
.iter()
.map(|(k, (p, s))| {
JsonValue::Array(vec![
JsonValue::from(k.as_str()),
JsonValue::from(p.x),
JsonValue::from(p.y),
JsonValue::from(s.width),
JsonValue::from(s.height),
])
})
.collect();
return store.save(&Persist::new(GEOMETRY_KEY), &rows).map_err(std::io::Error::other);
}
let Some(path) = &self.path else { return Ok(()) };
if let Some(dir) = path.parent() {
std::fs::create_dir_all(dir)?;
}
let mut out = String::new();
for (k, (p, s)) in &self.entries {
out.push_str(&format!("{} {} {} {} {}\n", p.x, p.y, s.width, s.height, k.replace('\n', " ")));
}
let tmp = path.with_extension("tmp");
std::fs::write(&tmp, out)?;
std::fs::rename(&tmp, path)
}
}
pub(crate) fn data_dir(name: &str) -> Option<PathBuf> {
#[cfg(target_os = "android")]
{
let _ = name;
remedy_ui_platform::bridge_android::data_dir()
}
#[cfg(not(target_os = "android"))]
{
folder(name)
}
}
#[cfg_attr(not(feature = "image"), allow(dead_code))]
pub(crate) fn cache_dir(name: &str) -> Option<PathBuf> {
#[cfg(target_os = "android")]
{
let _ = name;
remedy_ui_platform::bridge_android::data_dir().and_then(|d| d.parent().map(|p| p.join("cache")))
}
#[cfg(not(target_os = "android"))]
{
if cfg!(all(target_arch = "wasm32", target_os = "unknown")) {
return None;
}
let env = |k: &str| std::env::var_os(k).filter(|v| !v.is_empty()).map(PathBuf::from);
if cfg!(windows) {
env("LOCALAPPDATA").map(|b| b.join(name).join("cache"))
} else if cfg!(target_os = "macos") {
env("HOME").map(|h| h.join("Library").join("Caches").join(name))
} else {
env("XDG_CACHE_HOME").or_else(|| env("HOME").map(|h| h.join(".cache"))).map(|b| b.join(name))
}
}
}
#[cfg(not(target_os = "android"))]
fn folder(name: &str) -> Option<PathBuf> {
if cfg!(all(target_arch = "wasm32", target_os = "unknown")) {
return None;
}
let env = |k: &str| std::env::var_os(k).filter(|v| !v.is_empty()).map(PathBuf::from);
let base = if cfg!(windows) {
env("APPDATA")
} else if cfg!(target_os = "macos") {
env("HOME").map(|h| h.join("Library").join("Application Support"))
} else {
env("XDG_CONFIG_HOME").or_else(|| env("HOME").map(|h| h.join(".config")))
}?;
Some(base.join(name))
}