From abb05ab7b80393958413c67dd94270fa9336a8f1 Mon Sep 17 00:00:00 2001 From: partizan Date: Thu, 8 Jul 2021 04:01:06 +0300 Subject: [PATCH] refactor(startup): Use SETTINGS to store window geometry (#806) --- src/bridge/mod.rs | 3 +-- src/cmd_line.rs | 9 ++++--- src/main.rs | 8 +++--- src/settings/mod.rs | 2 ++ src/settings/window_geometry.rs | 43 ++++++++++++++++++++++++++++++ src/window/mod.rs | 45 -------------------------------- src/window/window_wrapper/mod.rs | 27 ++++++++++--------- 7 files changed, 68 insertions(+), 69 deletions(-) create mode 100644 src/settings/window_geometry.rs diff --git a/src/bridge/mod.rs b/src/bridge/mod.rs index 41147c5..5a408c8 100644 --- a/src/bridge/mod.rs +++ b/src/bridge/mod.rs @@ -18,7 +18,6 @@ use tokio::runtime::Runtime; use crate::channel_utils::*; use crate::settings::*; -use crate::window::window_geometry_or_default; use crate::{cmd_line::CmdLineSettings, error_handling::ResultPanicExplanation}; pub use events::*; use handler::NeovimHandler; @@ -162,7 +161,6 @@ async fn start_neovim_runtime( redraw_event_sender: LoggingTx, running: Arc, ) { - let (width, height) = window_geometry_or_default(); let handler = NeovimHandler::new(ui_command_sender.clone(), redraw_event_sender.clone()); let (mut nvim, io_handler) = match connection_mode() { ConnectionMode::Child => create::new_child_cmd(&mut create_nvim_command(), handler).await, @@ -276,6 +274,7 @@ async fn start_neovim_runtime( .await .ok(); + let WindowGeometry { width, height } = SETTINGS.get::().geometry; let mut options = UiAttachOptions::new(); options.set_linegrid_external(true); diff --git a/src/cmd_line.rs b/src/cmd_line.rs index 1751902..6ac0578 100644 --- a/src/cmd_line.rs +++ b/src/cmd_line.rs @@ -11,7 +11,7 @@ pub struct CmdLineSettings { pub files_to_open: Vec, pub disowned: bool, - pub geometry: Option, + pub geometry: WindowGeometry, pub wsl: bool, pub remote_tcp: Option, pub multi_grid: bool, @@ -28,7 +28,7 @@ impl Default for CmdLineSettings { neovim_args: vec![], files_to_open: vec![], disowned: false, - geometry: None, + geometry: DEFAULT_WINDOW_GEOMETRY, wsl: false, remote_tcp: None, multi_grid: false, @@ -38,7 +38,7 @@ impl Default for CmdLineSettings { } } -pub fn handle_command_line_arguments() { +pub fn handle_command_line_arguments() -> Result<(), String> { let clapp = App::new("Neovide") .version(crate_version!()) .author(crate_authors!()) @@ -130,7 +130,8 @@ pub fn handle_command_line_arguments() { remote_tcp: matches.value_of("remote_tcp").map(|i| i.to_owned()), disowned: matches.is_present("disowned"), wsl: matches.is_present("wsl"), - geometry: matches.value_of("geometry").map(|i| i.to_owned()), frameless: matches.is_present("frameless") || std::env::var("NEOVIDE_FRAMELESS").is_ok(), + geometry: parse_window_geometry(matches.value_of("geometry").map(|i| i.to_owned()))?, }); + Ok(()) } diff --git a/src/main.rs b/src/main.rs index 24c9924..5043189 100644 --- a/src/main.rs +++ b/src/main.rs @@ -41,10 +41,9 @@ use editor::start_editor; use renderer::{cursor_renderer::CursorSettings, RendererSettings}; #[cfg(not(test))] use settings::SETTINGS; -use window::{create_window, window_geometry, WindowSettings}; +use window::{create_window, WindowSettings}; pub use channel_utils::*; -pub const INITIAL_DIMENSIONS: (u64, u64) = (100, 50); fn main() { // ----------- @@ -116,9 +115,8 @@ fn main() { // Multiple other parts of the app "queue_next_frame" function to ensure animations continue // properly or updates to the graphics are pushed to the screen. - cmd_line::handle_command_line_arguments(); //Will exit if -h or -v - - if let Err(err) = window_geometry() { + //Will exit if -h or -v + if let Err(err) = cmd_line::handle_command_line_arguments() { eprintln!("{}", err); return; } diff --git a/src/settings/mod.rs b/src/settings/mod.rs index 8edd98a..5457ca6 100644 --- a/src/settings/mod.rs +++ b/src/settings/mod.rs @@ -3,11 +3,13 @@ use std::collections::HashMap; use std::convert::TryInto; mod from_value; +mod window_geometry; pub use from_value::FromValue; 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}; use crate::bridge::TxWrapper; use crate::error_handling::ResultPanicExplanation; diff --git a/src/settings/window_geometry.rs b/src/settings/window_geometry.rs new file mode 100644 index 0000000..4db967b --- /dev/null +++ b/src/settings/window_geometry.rs @@ -0,0 +1,43 @@ +#[derive(Debug, Clone)] +pub struct WindowGeometry { + pub width: u64, + pub height: u64, +} + +pub const DEFAULT_WINDOW_GEOMETRY: WindowGeometry = WindowGeometry { + width: 100, + height: 50, +}; + +pub fn parse_window_geometry(geometry: Option) -> Result { + geometry.map_or(Ok(DEFAULT_WINDOW_GEOMETRY), |input| { + let invalid_parse_err = format!( + "Invalid geometry: {}\nValid format: x", + input + ); + + input + .split('x') + .map(|dimension| { + dimension + .parse::() + .map_err(|_| invalid_parse_err.as_str()) + .and_then(|dimension| { + if dimension > 0 { + Ok(dimension) + } else { + Err("Invalid geometry: Window dimensions should be greater than 0.") + } + }) + }) + .collect::, &str>>() + .and_then(|dimensions| { + if let [width, height] = dimensions[..] { + Ok(WindowGeometry { width, height }) + } else { + Err(invalid_parse_err.as_str()) + } + }) + .map_err(|msg| msg.to_owned()) + }) +} diff --git a/src/window/mod.rs b/src/window/mod.rs index 3d88dae..0634f9c 100644 --- a/src/window/mod.rs +++ b/src/window/mod.rs @@ -4,11 +4,8 @@ mod window_wrapper; use crate::{ bridge::UiCommand, channel_utils::*, - cmd_line::CmdLineSettings, editor::{DrawCommand, WindowCommand}, renderer::Renderer, - settings::SETTINGS, - INITIAL_DIMENSIONS, }; use glutin::dpi::PhysicalSize; use std::sync::{atomic::AtomicBool, mpsc::Receiver, Arc}; @@ -17,47 +14,6 @@ pub use window_wrapper::start_loop; pub use settings::*; -pub fn window_geometry() -> Result<(u64, u64), String> { - //TODO: Maybe move this parsing into cmd_line... - SETTINGS - .get::() - .geometry - .map_or(Ok(INITIAL_DIMENSIONS), |input| { - let invalid_parse_err = format!( - "Invalid geometry: {}\nValid format: x", - input - ); - - input - .split('x') - .map(|dimension| { - dimension - .parse::() - .map_err(|_| invalid_parse_err.as_str()) - .and_then(|dimension| { - if dimension > 0 { - Ok(dimension) - } else { - Err("Invalid geometry: Window dimensions should be greater than 0.") - } - }) - }) - .collect::, &str>>() - .and_then(|dimensions| { - if let [width, height] = dimensions[..] { - Ok((width, height)) - } else { - Err(invalid_parse_err.as_str()) - } - }) - .map_err(|msg| msg.to_owned()) - }) -} - -pub fn window_geometry_or_default() -> (u64, u64) { - window_geometry().unwrap_or(INITIAL_DIMENSIONS) -} - #[cfg(target_os = "windows")] fn windows_fix_dpi() { use winapi::shared::windef::DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2; @@ -96,6 +52,5 @@ pub fn create_window( window_command_receiver, ui_command_sender, running, - window_geometry_or_default(), ); } diff --git a/src/window/window_wrapper/mod.rs b/src/window/window_wrapper/mod.rs index f544497..494184a 100644 --- a/src/window/window_wrapper/mod.rs +++ b/src/window/window_wrapper/mod.rs @@ -25,9 +25,14 @@ use glutin::platform::unix::WindowBuilderExtUnix; use super::{handle_new_grid_size, settings::WindowSettings}; use crate::{ - bridge::UiCommand, channel_utils::*, cmd_line::CmdLineSettings, editor::DrawCommand, - editor::WindowCommand, redraw_scheduler::REDRAW_SCHEDULER, renderer::Renderer, - settings::SETTINGS, + bridge::UiCommand, + channel_utils::*, + cmd_line::CmdLineSettings, + editor::DrawCommand, + editor::WindowCommand, + redraw_scheduler::REDRAW_SCHEDULER, + renderer::Renderer, + settings::{WindowGeometry, SETTINGS}, }; use image::{load_from_memory, GenericImageView, Pixel}; use keyboard_manager::KeyboardManager; @@ -183,7 +188,6 @@ pub fn start_loop( window_command_receiver: Receiver, ui_command_sender: LoggingTx, running: Arc, - initial_size: (u64, u64), ) { let icon = { let icon_data = Asset::get("neovide.ico").expect("Failed to read icon data"); @@ -223,11 +227,11 @@ pub fn start_loop( let renderer = Renderer::new(batched_draw_command_receiver, scale_factor); let window = windowed_context.window(); - let initial_inner_size = - get_initial_window_size(initial_size, (renderer.font_width, renderer.font_height)); - if !window.is_maximized() { - window.set_inner_size(initial_inner_size); + window.set_inner_size(get_initial_window_size(( + renderer.font_width, + renderer.font_height, + ))); } log::info!( @@ -279,11 +283,8 @@ pub fn start_loop( }); } -fn get_initial_window_size( - dimensions: (u64, u64), - font_dimesions: (u64, u64), -) -> PhysicalSize { - let (width, height) = dimensions; +fn get_initial_window_size(font_dimesions: (u64, u64)) -> PhysicalSize { + let WindowGeometry { width, height } = SETTINGS.get::().geometry; let (font_width, font_height) = font_dimesions; PhysicalSize::new((width * font_width) as u32, (height * font_height) as u32) }