Remember window dimensions #622 (#816)

* Implementation for #622

* Update src/window/settings.rs

Co-authored-by: partizan <serg.partizan@gmail.com>

* applied suggestions from @last-partizan

* applied suggestions from @Kethku

* applied more suggestions from @last-partizan

Co-authored-by: partizan <serg.partizan@gmail.com>
macos-click-through
Benson Li 3 years ago committed by GitHub
parent 6abe057d6e
commit 21a8adc4a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

16
Cargo.lock generated

@ -1547,6 +1547,8 @@ dependencies = [
"regex",
"rmpv",
"rust-embed",
"serde",
"serde_json",
"skia-safe",
"swash",
"tokio",
@ -2237,6 +2239,20 @@ name = "serde"
version = "1.0.126"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec7505abeacaec74ae4778d9d9328fe5a5d04253220a85c4ee022239fc996d03"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.126"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "963a7dbc9895aeac7ac90e74f34a5d5261828f79df35cbed41e10189d3804d43"
dependencies = [
"proc-macro2 1.0.26",
"quote 1.0.9",
"syn 1.0.72",
]
[[package]]
name = "serde_json"

@ -17,6 +17,8 @@ embed-fonts = []
debug-renderer = []
[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
neovide-derive = { path = "neovide-derive" }
euclid = "0.20.7"
lru = "0.4.3"

@ -9,7 +9,9 @@ use log::trace;
use nvim_rs::Neovim;
use parking_lot::RwLock;
pub use rmpv::Value;
pub use window_geometry::{parse_window_geometry, WindowGeometry, DEFAULT_WINDOW_GEOMETRY};
pub use window_geometry::{
maybe_save_window_size, parse_window_geometry, WindowGeometry, DEFAULT_WINDOW_GEOMETRY,
};
use crate::bridge::TxWrapper;
use crate::error_handling::ResultPanicExplanation;

@ -1,16 +1,64 @@
#[derive(Debug, Clone)]
use crate::renderer::Renderer;
use crate::settings::SETTINGS;
use crate::window::WindowSettings;
use glutin::dpi::PhysicalSize;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct WindowGeometry {
pub width: u64,
pub height: u64,
}
#[cfg(unix)]
const SETTINGS_PATH: &str = ".local/share/nvim/neovide-settings.json";
#[cfg(windows)]
const SETTINGS_PATH: &str = "AppData/Local/nvim-data/neovide-settings.json";
fn neovim_std_datapath() -> PathBuf {
let mut settings_path = dirs::home_dir().unwrap();
settings_path.push(SETTINGS_PATH);
settings_path
}
pub fn try_to_load_last_window_size() -> Result<WindowGeometry, String> {
let settings_path = neovim_std_datapath();
let serialized_size = std::fs::read_to_string(&settings_path).map_err(|e| e.to_string())?;
let deserialize_size: WindowGeometry =
serde_json::from_str(&serialized_size).map_err(|e| e.to_string())?;
log::debug!("Loaded Window Size: {:?}", deserialize_size);
Ok(deserialize_size)
}
pub const DEFAULT_WINDOW_GEOMETRY: WindowGeometry = WindowGeometry {
width: 100,
height: 50,
};
pub fn maybe_save_window_size(window_size: PhysicalSize<u32>, renderer: &Renderer) {
let saved_window_size = if SETTINGS.get::<WindowSettings>().remember_window_size {
WindowGeometry {
width: (window_size.width as f32 / renderer.font_width as f32) as u64,
height: (window_size.height as f32 / renderer.font_height as f32) as u64,
}
} else {
WindowGeometry {
width: DEFAULT_WINDOW_GEOMETRY.width as u64,
height: DEFAULT_WINDOW_GEOMETRY.height as u64,
}
};
let settings_path = neovim_std_datapath();
let serialized_size = serde_json::to_string(&saved_window_size).unwrap();
log::debug!("Saved Window Size: {}", serialized_size);
std::fs::write(settings_path, serialized_size).unwrap();
}
pub fn parse_window_geometry(geometry: Option<String>) -> Result<WindowGeometry, String> {
geometry.map_or(Ok(DEFAULT_WINDOW_GEOMETRY), |input| {
let saved_window_size =
try_to_load_last_window_size().or::<String>(Ok(DEFAULT_WINDOW_GEOMETRY));
geometry.map_or(saved_window_size, |input| {
let invalid_parse_err = format!(
"Invalid geometry: {}\nValid format: <width>x<height>",
input

@ -8,6 +8,7 @@ pub struct WindowSettings {
pub fullscreen: bool,
pub iso_layout: bool,
pub scroll_dead_zone: f32,
pub remember_window_size: bool,
}
impl Default for WindowSettings {
@ -22,6 +23,7 @@ impl Default for WindowSettings {
.neovim_args
.contains(&String::from("--noIdle")),
scroll_dead_zone: 0.0,
remember_window_size: false,
}
}
}

@ -33,7 +33,7 @@ use crate::{
editor::WindowCommand,
redraw_scheduler::REDRAW_SCHEDULER,
renderer::Renderer,
settings::{WindowGeometry, SETTINGS},
settings::{maybe_save_window_size, WindowGeometry, SETTINGS},
};
use image::{load_from_memory, GenericImageView, Pixel};
use keyboard_manager::KeyboardManager;
@ -279,6 +279,7 @@ pub fn start_loop(
event_loop.run(move |e, _window_target, control_flow| {
if !running.load(Ordering::Relaxed) {
maybe_save_window_size(window_wrapper.saved_inner_size, &window_wrapper.renderer);
std::process::exit(0);
}

Loading…
Cancel
Save