From babf2171f23986e3392292a3ecafb0e910968efa Mon Sep 17 00:00:00 2001 From: daa84 Date: Fri, 16 Feb 2018 12:57:26 +0300 Subject: [PATCH] Disable load window size option --- src/main.rs | 56 +++++++++++++++++++++++++++++++---------------------- src/ui.rs | 27 ++++++++++++++++---------- 2 files changed, 50 insertions(+), 33 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4ad492d..d97070c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,29 +1,29 @@ -extern crate gtk; -extern crate gtk_sys; -extern crate gio; +extern crate cairo; +extern crate env_logger; extern crate gdk; extern crate gdk_sys; +extern crate gio; #[macro_use] extern crate glib; extern crate glib_sys as glib_ffi; extern crate gobject_sys as gobject_ffi; -extern crate cairo; -extern crate pango; -extern crate pango_sys; -extern crate pangocairo; -extern crate pango_cairo_sys; -extern crate neovim_lib; -extern crate phf; +extern crate gtk; +extern crate gtk_sys; +extern crate htmlescape; #[macro_use] extern crate log; -extern crate env_logger; -extern crate htmlescape; +extern crate neovim_lib; +extern crate pango; +extern crate pango_cairo_sys; +extern crate pango_sys; +extern crate pangocairo; +extern crate phf; extern crate serde; #[macro_use] extern crate serde_derive; -extern crate toml; extern crate serde_json; +extern crate toml; mod sys; @@ -49,7 +49,6 @@ mod project; mod tabline; mod error; - use std::env; use std::time::Duration; use std::str::FromStr; @@ -61,6 +60,7 @@ use shell::ShellOptions; const BIN_PATH_ARG: &str = "--nvim-bin-path"; const TIMEOUT_ARG: &str = "--timeout"; +const DISABLE_WIN_STATE_RESTORE: &str = "--disable-win-restore"; fn main() { env_logger::init().expect("Can't initialize env_logger"); @@ -89,6 +89,7 @@ fn main() { let argv: Vec = args.iter() .filter(|a| !a.starts_with(BIN_PATH_ARG)) .filter(|a| !a.starts_with(TIMEOUT_ARG)) + .filter(|a| !a.starts_with(DISABLE_WIN_STATE_RESTORE)) .cloned() .collect(); app.run(&argv); @@ -102,7 +103,7 @@ fn open(app: >k::Application, files: &[gio::File], _: &str) { nvim_timeout(std::env::args()), )); - ui.init(app); + ui.init(app, !nvim_disable_win_state(std::env::args())); } } @@ -113,16 +114,15 @@ fn activate(app: >k::Application) { nvim_timeout(std::env::args()), )); - ui.init(app); + ui.init(app, !nvim_disable_win_state(std::env::args())); } fn nvim_bin_path(mut args: I) -> Option where I: Iterator, { - args.find(|a| a.starts_with(BIN_PATH_ARG)).and_then(|p| { - p.split('=').nth(1).map(str::to_owned) - }) + args.find(|a| a.starts_with(BIN_PATH_ARG)) + .and_then(|p| p.split('=').nth(1).map(str::to_owned)) } fn nvim_timeout(mut args: I) -> Option @@ -141,6 +141,15 @@ where .map(|timeout| Duration::from_secs(timeout)) } +fn nvim_disable_win_state(mut args: I) -> bool +where + I: Iterator, +{ + args.find(|a| a.starts_with(DISABLE_WIN_STATE_RESTORE)) + .map(|_| true) + .unwrap_or(false) +} + #[cfg(test)] mod tests { use super::*; @@ -157,14 +166,15 @@ mod tests { ); } - #[test] fn test_timeout_arg() { assert_eq!( Some(Duration::from_secs(100)), - nvim_timeout(vec!["neovim-gtk", "--timeout=100"].iter().map( - |s| s.to_string(), - )) + nvim_timeout( + vec!["neovim-gtk", "--timeout=100"] + .iter() + .map(|s| s.to_string(),) + ) ); } } diff --git a/src/ui.rs b/src/ui.rs index bd389f8..fd77e4b 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -35,6 +35,9 @@ macro_rules! clone { ); } +const DEFAULT_WIDTH: i32 = 800; +const DEFAULT_HEIGHT: i32 = 600; + pub struct Ui { initialized: bool, comps: Arc>, @@ -93,7 +96,7 @@ impl Ui { } } - pub fn init(&mut self, app: >k::Application) { + pub fn init(&mut self, app: >k::Application, restore_win_state: bool) { if self.initialized { return; } @@ -165,12 +168,17 @@ impl Ui { window.set_titlebar(Some(&header_bar)); } - window.set_default_size( - comps.window_state.current_width, - comps.window_state.current_height, - ); - if comps.window_state.is_maximized { - window.maximize(); + if restore_win_state { + if comps.window_state.is_maximized { + window.maximize(); + } + + window.set_default_size( + comps.window_state.current_width, + comps.window_state.current_height, + ); + } else { + window.set_default_size(DEFAULT_WIDTH, DEFAULT_HEIGHT); } } @@ -309,8 +317,8 @@ struct WindowState { impl WindowState { pub fn new() -> Self { WindowState { - current_width: 800, - current_height: 600, + current_width: DEFAULT_WIDTH, + current_height: DEFAULT_HEIGHT, is_maximized: false, } } @@ -328,7 +336,6 @@ impl SettingsLoader for WindowState { } } - pub struct UiMutex { thread: thread::ThreadId, data: RefCell,