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.
macos-click-through
Fred Sundvik 3 years ago
parent 36479dd45b
commit 8c2099496a

@ -10,8 +10,7 @@ use nvim_rs::Neovim;
use parking_lot::RwLock; use parking_lot::RwLock;
pub use rmpv::Value; pub use rmpv::Value;
pub use window_geometry::{ pub use window_geometry::{
load_last_window_position, maybe_save_window_position, maybe_save_window_size, load_last_window_position, parse_window_geometry, save_window_geometry, DEFAULT_WINDOW_GEOMETRY,
parse_window_geometry, DEFAULT_WINDOW_GEOMETRY,
}; };
use crate::bridge::TxWrapper; use crate::bridge::TxWrapper;

@ -2,6 +2,7 @@ use crate::settings::SETTINGS;
use crate::utils::Dimensions; use crate::utils::Dimensions;
use crate::window::WindowSettings; use crate::window::WindowSettings;
use glutin::dpi::PhysicalPosition; use glutin::dpi::PhysicalPosition;
use serde::{Deserialize, Serialize};
use std::path::PathBuf; use std::path::PathBuf;
#[cfg(unix)] #[cfg(unix)]
use xdg; use xdg;
@ -13,6 +14,23 @@ pub const DEFAULT_WINDOW_GEOMETRY: Dimensions = Dimensions {
height: 50, height: 50,
}; };
#[derive(Serialize, Deserialize)]
struct PersistentWindowSettings {
#[serde(default)]
position: PhysicalPosition<i32>,
#[serde(default = "default_size")]
size: Dimensions,
}
fn default_size() -> Dimensions {
DEFAULT_WINDOW_GEOMETRY
}
#[derive(Serialize, Deserialize)]
struct PersistentSettings {
window: PersistentWindowSettings,
}
#[cfg(windows)] #[cfg(windows)]
fn neovim_std_datapath() -> PathBuf { fn neovim_std_datapath() -> PathBuf {
let mut data_path = dirs::home_dir().unwrap(); let mut data_path = dirs::home_dir().unwrap();
@ -32,11 +50,15 @@ fn settings_path() -> PathBuf {
settings_path settings_path
} }
pub fn try_to_load_last_window_size() -> Result<Dimensions, String> { fn load_settings() -> Result<PersistentSettings, String> {
let settings_path = neovim_std_datapath(); let settings_path = settings_path();
let json = std::fs::read_to_string(&settings_path).map_err(|e| e.to_string())?; 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<Dimensions, String> {
let settings = load_settings()?;
let loaded_geometry = settings.window.size;
log::debug!("Loaded Window Size: {:?}", loaded_geometry); log::debug!("Loaded Window Size: {:?}", loaded_geometry);
if loaded_geometry.width == 0 || loaded_geometry.height == 0 { if loaded_geometry.width == 0 || loaded_geometry.height == 0 {
@ -48,50 +70,44 @@ pub fn try_to_load_last_window_size() -> Result<Dimensions, String> {
} }
pub fn load_last_window_position() -> PhysicalPosition<i32> { pub fn load_last_window_position() -> PhysicalPosition<i32> {
let settings_path = neovim_std_datapath(); let settings = load_settings();
let json = std::fs::read_to_string(&settings_path).map_err(|e| e.to_string()); if let Ok(settings) = settings {
if json.is_err() { let loaded_position = settings.window.position;
return PhysicalPosition::default(); log::debug!("Loaded Window Position: {:?}", loaded_position);
} loaded_position
let json = json.unwrap(); } else {
PhysicalPosition::default()
let loaded_position: Result<PhysicalPosition<i32>, _> =
serde_json::from_str(&json).map_err(|e| e.to_string());
if loaded_position.is_err() {
return 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<Dimensions>) { pub fn save_window_geometry(
let settings = SETTINGS.get::<WindowSettings>(); grid_size: Option<Dimensions>,
let saved_window_size = if settings.remember_window_size { position: Option<PhysicalPosition<i32>>,
grid_size.unwrap_or(DEFAULT_WINDOW_GEOMETRY) ) {
} else { let window_settings = SETTINGS.get::<WindowSettings>();
DEFAULT_WINDOW_GEOMETRY 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(); let settings_path = settings_path();
std::fs::create_dir_all(neovim_std_datapath()).unwrap(); std::fs::create_dir_all(neovim_std_datapath()).unwrap();
let json = serde_json::to_string(&saved_window_size).unwrap(); let json = serde_json::to_string(&settings).unwrap();
log::debug!("Saved Window Size: {}", json); log::debug!("Saved Window Settings: {}", json);
std::fs::write(settings_path, json).unwrap();
}
pub fn maybe_save_window_position(position: Option<PhysicalPosition<i32>>) {
let settings = SETTINGS.get::<WindowSettings>();
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);
std::fs::write(settings_path, json).unwrap(); std::fs::write(settings_path, json).unwrap();
} }

@ -30,9 +30,7 @@ use crate::{
redraw_scheduler::REDRAW_SCHEDULER, redraw_scheduler::REDRAW_SCHEDULER,
renderer::Renderer, renderer::Renderer,
running_tracker::*, running_tracker::*,
settings::{ settings::{load_last_window_position, save_window_geometry, SETTINGS},
load_last_window_position, maybe_save_window_position, maybe_save_window_size, SETTINGS,
},
utils::Dimensions, utils::Dimensions,
}; };
use image::{load_from_memory, GenericImageView, Pixel}; use image::{load_from_memory, GenericImageView, Pixel};
@ -325,8 +323,8 @@ pub fn create_window(
event_loop.run(move |e, _window_target, control_flow| { event_loop.run(move |e, _window_target, control_flow| {
if !RUNNING_TRACKER.is_running() { if !RUNNING_TRACKER.is_running() {
maybe_save_window_size(window_wrapper.saved_grid_size); save_window_geometry(
maybe_save_window_position( window_wrapper.saved_grid_size,
window_wrapper window_wrapper
.windowed_context .windowed_context
.window() .window()

Loading…
Cancel
Save