From 8c2099496af7259fd98d364737cf1ecb1a3d1cf4 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 1 Jan 2022 18:20:58 +0200 Subject: [PATCH] Fix window settings load and save Some of the load and save paths were fixed. And the json is now structured to properly support saving and loading of both position and size data, and is easily extendible for more fields and objects in the future. --- src/settings/mod.rs | 3 +- src/settings/window_geometry.rs | 96 +++++++++++++++++++-------------- src/window/mod.rs | 8 ++- 3 files changed, 60 insertions(+), 47 deletions(-) diff --git a/src/settings/mod.rs b/src/settings/mod.rs index 94ec412..866450b 100644 --- a/src/settings/mod.rs +++ b/src/settings/mod.rs @@ -10,8 +10,7 @@ use nvim_rs::Neovim; use parking_lot::RwLock; pub use rmpv::Value; pub use window_geometry::{ - load_last_window_position, maybe_save_window_position, maybe_save_window_size, - parse_window_geometry, DEFAULT_WINDOW_GEOMETRY, + load_last_window_position, parse_window_geometry, save_window_geometry, DEFAULT_WINDOW_GEOMETRY, }; use crate::bridge::TxWrapper; diff --git a/src/settings/window_geometry.rs b/src/settings/window_geometry.rs index d3c76ad..a7d91af 100644 --- a/src/settings/window_geometry.rs +++ b/src/settings/window_geometry.rs @@ -2,6 +2,7 @@ use crate::settings::SETTINGS; use crate::utils::Dimensions; use crate::window::WindowSettings; use glutin::dpi::PhysicalPosition; +use serde::{Deserialize, Serialize}; use std::path::PathBuf; #[cfg(unix)] use xdg; @@ -13,6 +14,23 @@ pub const DEFAULT_WINDOW_GEOMETRY: Dimensions = Dimensions { height: 50, }; +#[derive(Serialize, Deserialize)] +struct PersistentWindowSettings { + #[serde(default)] + position: PhysicalPosition, + #[serde(default = "default_size")] + size: Dimensions, +} + +fn default_size() -> Dimensions { + DEFAULT_WINDOW_GEOMETRY +} + +#[derive(Serialize, Deserialize)] +struct PersistentSettings { + window: PersistentWindowSettings, +} + #[cfg(windows)] fn neovim_std_datapath() -> PathBuf { let mut data_path = dirs::home_dir().unwrap(); @@ -32,11 +50,15 @@ fn settings_path() -> PathBuf { settings_path } -pub fn try_to_load_last_window_size() -> Result { - let settings_path = neovim_std_datapath(); +fn load_settings() -> Result { + let settings_path = settings_path(); let json = std::fs::read_to_string(&settings_path).map_err(|e| e.to_string())?; + serde_json::from_str(&json).map_err(|e| e.to_string()) +} - let loaded_geometry: Dimensions = serde_json::from_str(&json).map_err(|e| e.to_string())?; +pub fn try_to_load_last_window_size() -> Result { + let settings = load_settings()?; + let loaded_geometry = settings.window.size; log::debug!("Loaded Window Size: {:?}", loaded_geometry); if loaded_geometry.width == 0 || loaded_geometry.height == 0 { @@ -48,50 +70,44 @@ pub fn try_to_load_last_window_size() -> Result { } pub fn load_last_window_position() -> PhysicalPosition { - let settings_path = neovim_std_datapath(); - let json = std::fs::read_to_string(&settings_path).map_err(|e| e.to_string()); - if json.is_err() { - return PhysicalPosition::default(); - } - let json = json.unwrap(); - - let loaded_position: Result, _> = - serde_json::from_str(&json).map_err(|e| e.to_string()); - if loaded_position.is_err() { - return PhysicalPosition::default(); + let settings = load_settings(); + if let Ok(settings) = settings { + let loaded_position = settings.window.position; + log::debug!("Loaded Window Position: {:?}", loaded_position); + loaded_position + } else { + PhysicalPosition::default() } - let loaded_position = loaded_position.unwrap(); - log::debug!("Loaded Window Position: {:?}", loaded_position); - - loaded_position } -pub fn maybe_save_window_size(grid_size: Option) { - let settings = SETTINGS.get::(); - let saved_window_size = if settings.remember_window_size { - grid_size.unwrap_or(DEFAULT_WINDOW_GEOMETRY) - } else { - DEFAULT_WINDOW_GEOMETRY +pub fn save_window_geometry( + grid_size: Option, + position: Option>, +) { + let window_settings = SETTINGS.get::(); + let settings = PersistentSettings { + window: PersistentWindowSettings { + size: { + window_settings + .remember_window_size + .then(|| grid_size) + .flatten() + .unwrap_or(DEFAULT_WINDOW_GEOMETRY) + }, + position: { + window_settings + .remember_window_position + .then(|| position) + .flatten() + .unwrap_or_default() + }, + }, }; let settings_path = settings_path(); std::fs::create_dir_all(neovim_std_datapath()).unwrap(); - let json = serde_json::to_string(&saved_window_size).unwrap(); - log::debug!("Saved Window Size: {}", json); - std::fs::write(settings_path, json).unwrap(); -} - -pub fn maybe_save_window_position(position: Option>) { - let settings = SETTINGS.get::(); - let saved_window_position = if settings.remember_window_position { - position.unwrap_or_default() - } else { - PhysicalPosition::default() - }; - - let settings_path = neovim_std_datapath(); - let json = serde_json::to_string(&saved_window_position).unwrap(); - log::debug!("Saved Window Position: {}", json); + let json = serde_json::to_string(&settings).unwrap(); + log::debug!("Saved Window Settings: {}", json); std::fs::write(settings_path, json).unwrap(); } diff --git a/src/window/mod.rs b/src/window/mod.rs index bacbccd..a5ae9f0 100644 --- a/src/window/mod.rs +++ b/src/window/mod.rs @@ -30,9 +30,7 @@ use crate::{ redraw_scheduler::REDRAW_SCHEDULER, renderer::Renderer, running_tracker::*, - settings::{ - load_last_window_position, maybe_save_window_position, maybe_save_window_size, SETTINGS, - }, + settings::{load_last_window_position, save_window_geometry, SETTINGS}, utils::Dimensions, }; use image::{load_from_memory, GenericImageView, Pixel}; @@ -325,8 +323,8 @@ pub fn create_window( event_loop.run(move |e, _window_target, control_flow| { if !RUNNING_TRACKER.is_running() { - maybe_save_window_size(window_wrapper.saved_grid_size); - maybe_save_window_position( + save_window_geometry( + window_wrapper.saved_grid_size, window_wrapper .windowed_context .window()