refactor saved settings to be an enum to enable persistence of maximized state

macos-click-through
Keith Simmons 3 years ago
parent 494f8924e9
commit 22cb5bc05f

@ -10,7 +10,8 @@ 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, parse_window_geometry, save_window_geometry, DEFAULT_WINDOW_GEOMETRY, load_last_window_settings, parse_window_geometry, save_window_geometry,
PersistentWindowSettings, DEFAULT_WINDOW_GEOMETRY,
}; };
use crate::bridge::TxWrapper; use crate::bridge::TxWrapper;

@ -12,12 +12,15 @@ pub const DEFAULT_WINDOW_GEOMETRY: Dimensions = Dimensions {
height: 50, height: 50,
}; };
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize, Debug)]
struct PersistentWindowSettings { pub enum PersistentWindowSettings {
Maximized,
Windowed {
#[serde(default)] #[serde(default)]
position: PhysicalPosition<i32>, position: PhysicalPosition<i32>,
#[serde(default = "default_size")] #[serde(default = "default_size")]
size: Dimensions, size: Dimensions,
},
} }
fn default_size() -> Dimensions { fn default_size() -> Dimensions {
@ -54,37 +57,32 @@ fn load_settings() -> Result<PersistentSettings, String> {
serde_json::from_str(&json).map_err(|e| e.to_string()) serde_json::from_str(&json).map_err(|e| e.to_string())
} }
pub fn try_to_load_last_window_size() -> Result<Dimensions, String> { pub fn load_last_window_settings() -> Result<PersistentWindowSettings, String> {
let settings = load_settings()?; let settings = load_settings()?;
let loaded_geometry = settings.window.size; let mut loaded_settings = settings.window;
log::debug!("Loaded Window Size: {:?}", loaded_geometry); log::debug!("Loaded window settings: {:?}", loaded_settings);
if loaded_geometry.width == 0 || loaded_geometry.height == 0 { if let PersistentWindowSettings::Windowed { size, .. } = &mut loaded_settings {
log::warn!("Invalid Saved Window Size. Reverting to default"); if size.width == 0 || size.height == 0 {
Ok(DEFAULT_WINDOW_GEOMETRY) *size = DEFAULT_WINDOW_GEOMETRY;
} else {
Ok(loaded_geometry)
} }
}
pub fn load_last_window_position() -> PhysicalPosition<i32> {
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()
} }
Ok(loaded_settings)
} }
pub fn save_window_geometry( pub fn save_window_geometry(
maximized: bool,
grid_size: Option<Dimensions>, grid_size: Option<Dimensions>,
position: Option<PhysicalPosition<i32>>, position: Option<PhysicalPosition<i32>>,
) { ) {
let window_settings = SETTINGS.get::<WindowSettings>(); let window_settings = SETTINGS.get::<WindowSettings>();
let settings = PersistentSettings { let settings = PersistentSettings {
window: PersistentWindowSettings { window: if maximized && window_settings.remember_window_size {
PersistentWindowSettings::Maximized
} else {
PersistentWindowSettings::Windowed {
size: { size: {
window_settings window_settings
.remember_window_size .remember_window_size
@ -99,6 +97,7 @@ pub fn save_window_geometry(
.flatten() .flatten()
.unwrap_or_default() .unwrap_or_default()
}, },
}
}, },
}; };
@ -110,8 +109,16 @@ pub fn save_window_geometry(
} }
pub fn parse_window_geometry(geometry: Option<String>) -> Result<Dimensions, String> { pub fn parse_window_geometry(geometry: Option<String>) -> Result<Dimensions, String> {
let saved_window_size = let saved_window_size = load_last_window_settings()
try_to_load_last_window_size().or::<String>(Ok(DEFAULT_WINDOW_GEOMETRY)); .and_then(|window_settings| {
if let PersistentWindowSettings::Windowed { size, .. } = window_settings {
Ok(size)
} else {
Err(String::from("Window was maximized"))
}
})
.or::<String>(Ok(DEFAULT_WINDOW_GEOMETRY));
geometry.map_or(saved_window_size, |input| { geometry.map_or(saved_window_size, |input| {
let invalid_parse_err = format!( let invalid_parse_err = format!(
"Invalid geometry: {}\nValid format: <width>x<height>", "Invalid geometry: {}\nValid format: <width>x<height>",

@ -30,7 +30,9 @@ use crate::{
redraw_scheduler::REDRAW_SCHEDULER, redraw_scheduler::REDRAW_SCHEDULER,
renderer::Renderer, renderer::Renderer,
running_tracker::*, running_tracker::*,
settings::{load_last_window_position, save_window_geometry, SETTINGS}, settings::{
load_last_window_settings, save_window_geometry, PersistentWindowSettings, SETTINGS,
},
utils::Dimensions, utils::Dimensions,
}; };
use image::{load_from_memory, GenericImageView, Pixel}; use image::{load_from_memory, GenericImageView, Pixel};
@ -275,21 +277,37 @@ pub fn create_window(
let event_loop = EventLoop::new(); let event_loop = EventLoop::new();
let cmd_line_settings = SETTINGS.get::<CmdLineSettings>(); let cmd_line_settings = SETTINGS.get::<CmdLineSettings>();
let winit_window_builder = window::WindowBuilder::new()
let mut maximized = cmd_line_settings.maximized;
let mut previous_position = None;
if let Ok(last_window_settings) = load_last_window_settings() {
match last_window_settings {
PersistentWindowSettings::Maximized => {
maximized = true;
}
PersistentWindowSettings::Windowed { position, .. } => {
previous_position = Some(position);
}
}
}
let mut winit_window_builder = window::WindowBuilder::new()
.with_title("Neovide") .with_title("Neovide")
.with_window_icon(Some(icon)) .with_window_icon(Some(icon))
.with_maximized(cmd_line_settings.maximized) .with_maximized(maximized)
.with_transparent(true) .with_transparent(true)
.with_decorations(!cmd_line_settings.frameless) .with_decorations(!cmd_line_settings.frameless);
.with_position(load_last_window_position());
if let Some(previous_position) = previous_position {
if !maximized {
winit_window_builder = winit_window_builder.with_position(previous_position);
}
}
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
let winit_window_builder = winit_window_builder let winit_window_builder = winit_window_builder
.with_app_id(SETTINGS.get::<CmdLineSettings>().wayland_app_id) .with_app_id(cmd_line_settings.wayland_app_id)
.with_class( .with_class("neovide".to_string(), cmd_line_settings.x11_wm_class);
"neovide".to_string(),
SETTINGS.get::<CmdLineSettings>().x11_wm_class,
);
let windowed_context = ContextBuilder::new() let windowed_context = ContextBuilder::new()
.with_pixel_format(24, 8) .with_pixel_format(24, 8)
@ -333,13 +351,11 @@ 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() {
let window = window_wrapper.windowed_context.window();
save_window_geometry( save_window_geometry(
window.is_maximized(),
window_wrapper.saved_grid_size, window_wrapper.saved_grid_size,
window_wrapper window.outer_position().ok(),
.windowed_context
.window()
.outer_position()
.ok(),
); );
std::process::exit(0); std::process::exit(0);
} }

Loading…
Cancel
Save