From 5451b59d20a1f121bd4716b70931495dea6bdf11 Mon Sep 17 00:00:00 2001 From: Jacob Mischka Date: Thu, 15 Jul 2021 17:29:24 -0500 Subject: [PATCH] Listen to VimResized event and resize the window when needed Fixes #156 --- src/shell.rs | 24 ++++++++++++++++++++++-- src/ui.rs | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/src/shell.rs b/src/shell.rs index c6317627..a55b0b5e 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -394,6 +394,23 @@ impl State { } } + pub fn should_resize(&self, columns: usize, rows: usize) -> bool { + columns != self.resize_request.1 as usize || rows != self.resize_request.0 as usize + } + + pub fn calc_window_size(&self, columns: usize, rows: usize) -> (usize, usize) { + let &CellMetrics { + line_height, + char_width, + .. + } = self.render_state.borrow().font_ctx.cell_metrics(); + + ( + (columns as f64 * char_width).ceil() as usize, + (rows as f64 * line_height).ceil() as usize, + ) + } + fn calc_nvim_size(&self) -> (usize, usize) { let &CellMetrics { line_height, @@ -1500,7 +1517,9 @@ impl State { } pub fn option_set(&mut self, name: String, val: Value) -> RepaintMode { - if let "guifont" = name.as_str() { self.set_font_from_value(val) }; + if let "guifont" = name.as_str() { + self.set_font_from_value(val) + }; RepaintMode::Nothing } @@ -1513,7 +1532,8 @@ impl State { for font in &fonts { let desc = FontDescription::from_string(&font); if desc.get_size() > 0 - && exists_fonts.contains(&desc.get_family().unwrap_or_else(|| "".into())) + && exists_fonts + .contains(&desc.get_family().unwrap_or_else(|| "".into())) { self.set_font_rpc(font); return; diff --git a/src/ui.rs b/src/ui.rs index 0ca02531..50b98b8c 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -250,6 +250,14 @@ impl Ui { move |args| set_completeopts(&*shell_ref, args), ); + let comps_ref = self.comps.clone(); + let shell_ref = self.shell.clone(); + let update_size = shell.state.borrow().subscribe( + SubscriptionKey::from("VimResized"), + &["&lines", "&columns"], + move |args| update_window_size(&*comps_ref, &*shell_ref, args), + ); + let comps_ref = self.comps.clone(); let shell_ref = self.shell.clone(); window.connect_delete_event(move |_, _| gtk_delete(&*comps_ref, &*shell_ref)); @@ -279,6 +287,7 @@ impl Ui { &update_title, &update_subtitle, &update_completeopt, + &update_size, ); })); @@ -300,6 +309,7 @@ impl Ui { update_title: &SubscriptionHandle, update_subtitle: &Option, update_completeopt: &SubscriptionHandle, + update_size: &SubscriptionHandle, ) { plug_manager .borrow_mut() @@ -308,6 +318,7 @@ impl Ui { shell.set_autocmds(); shell.run_now(&update_title); shell.run_now(&update_completeopt); + shell.run_now(&update_size); if let Some(ref update_subtitle) = update_subtitle { shell.run_now(&update_subtitle); } @@ -471,7 +482,9 @@ fn on_help_about(window: >k::ApplicationWindow) { let about = AboutDialog::new(); about.set_transient_for(Some(window)); about.set_program_name("NeovimGtk"); - about.set_version(Some(crate::GIT_BUILD_VERSION.unwrap_or(env!("CARGO_PKG_VERSION")))); + about.set_version(Some( + crate::GIT_BUILD_VERSION.unwrap_or(env!("CARGO_PKG_VERSION")), + )); about.set_logo_icon_name(Some("org.daa.NeovimGtk")); about.set_authors(&[env!("CARGO_PKG_AUTHORS")]); about.set_comments(Some(misc::about_comments().as_str())); @@ -544,6 +557,26 @@ fn update_window_title(comps: &Arc>, args: Vec) { window.set_title(filename); } +fn update_window_size(comps: &UiMutex, shell: &RefCell, args: Vec) { + let lines = &args[0]; + let cols = &args[1]; + + if let (Ok(lines), Ok(cols)) = (lines.parse::(), cols.parse::()) { + let state_ref = shell.borrow().state.clone(); + let state = state_ref.borrow(); + + if state.should_resize(cols, lines) { + let (width, height) = state.calc_window_size(cols, lines); + + let comps_ref = comps.clone(); + let comps = comps_ref.borrow(); + let window = comps.window.as_ref().unwrap(); + + window.resize(width as i32, height as i32); + } + } +} + #[derive(Serialize, Deserialize)] struct WindowState { current_width: i32,