refactor(startup): Use SETTINGS to store window geometry (#806)

macos-click-through
partizan 3 years ago committed by GitHub
parent ebe01fc445
commit abb05ab7b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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<RedrawEvent>,
running: Arc<AtomicBool>,
) {
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::<CmdLineSettings>().geometry;
let mut options = UiAttachOptions::new();
options.set_linegrid_external(true);

@ -11,7 +11,7 @@ pub struct CmdLineSettings {
pub files_to_open: Vec<String>,
pub disowned: bool,
pub geometry: Option<String>,
pub geometry: WindowGeometry,
pub wsl: bool,
pub remote_tcp: Option<String>,
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(())
}

@ -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;
}

@ -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;

@ -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<String>) -> Result<WindowGeometry, String> {
geometry.map_or(Ok(DEFAULT_WINDOW_GEOMETRY), |input| {
let invalid_parse_err = format!(
"Invalid geometry: {}\nValid format: <width>x<height>",
input
);
input
.split('x')
.map(|dimension| {
dimension
.parse::<u64>()
.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::<Result<Vec<_>, &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())
})
}

@ -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::<CmdLineSettings>()
.geometry
.map_or(Ok(INITIAL_DIMENSIONS), |input| {
let invalid_parse_err = format!(
"Invalid geometry: {}\nValid format: <width>x<height>",
input
);
input
.split('x')
.map(|dimension| {
dimension
.parse::<u64>()
.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::<Result<Vec<_>, &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(),
);
}

@ -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<WindowCommand>,
ui_command_sender: LoggingTx<UiCommand>,
running: Arc<AtomicBool>,
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<u32> {
let (width, height) = dimensions;
fn get_initial_window_size(font_dimesions: (u64, u64)) -> PhysicalSize<u32> {
let WindowGeometry { width, height } = SETTINGS.get::<CmdLineSettings>().geometry;
let (font_width, font_height) = font_dimesions;
PhysicalSize::new((width * font_width) as u32, (height * font_height) as u32)
}

Loading…
Cancel
Save