From 2106b69c1578ac35d8c66d04001f4376c0d390ce Mon Sep 17 00:00:00 2001 From: daa Date: Sat, 22 Jul 2017 16:27:17 +0300 Subject: [PATCH] Try to keep window size on font change --- src/shell.rs | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/src/shell.rs b/src/shell.rs index b0a7079..d6a1546 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -8,7 +8,7 @@ use cairo; use pangocairo::CairoContextExt; use pango; use pango::FontDescription; -use gdk::{ModifierType, EventConfigure, EventButton, EventMotion, EventType, EventScroll}; +use gdk::{ModifierType, EventButton, EventMotion, EventType, EventScroll}; use gdk_sys; use glib; use gtk; @@ -68,6 +68,7 @@ pub struct State { line_height: Option, char_width: Option, request_resize: bool, + request_nvim_resize: bool, resize_timer: Option, options: ShellOptions, @@ -105,6 +106,7 @@ impl State { char_width: None, resize_timer: None, request_resize: false, + request_nvim_resize: false, options, @@ -185,6 +187,10 @@ impl State { self.request_resize = true; } + fn request_nvim_resize(&mut self) { + self.request_nvim_resize = true; + } + fn close_popup_menu(&self) { if self.popup_menu.borrow().is_open() { let mut nvim = self.nvim(); @@ -385,7 +391,10 @@ impl Shell { let ref_state = self.state.clone(); state .drawing_area - .connect_configure_event(move |_, ev| gtk_configure_event(&ref_state, ev)); + .connect_configure_event(move |_, _| { + try_nvim_resize(&ref_state); + false + }); } #[cfg(unix)] @@ -540,6 +549,11 @@ fn update_line_metrics(state_arc: &Arc>, ctx: &cairo::Context) { fn gtk_draw(state_arc: &Arc>, ctx: &cairo::Context) -> Inhibit { update_line_metrics(state_arc, ctx); + + if state_arc.borrow_mut().request_nvim_resize { + try_nvim_resize(state_arc); + } + init_nvim(state_arc); let mut state = state_arc.borrow_mut(); @@ -895,15 +909,17 @@ fn split_color(indexed_color: u64) -> Color { Color(r / 255.0, g / 255.0, b / 255.0) } -fn gtk_configure_event(state: &Arc>, _: &EventConfigure) -> bool { +fn try_nvim_resize(state: &Arc>) { let mut state_ref = state.borrow_mut(); + state_ref.request_nvim_resize = false; + if let Some(timer) = state_ref.resize_timer { glib::source_remove(timer); } if !state_ref.nvim.borrow().is_initialized() { - return false; + return; } if let Some((columns, rows)) = state_ref.calc_nvim_size() { @@ -915,13 +931,12 @@ fn gtk_configure_event(state: &Arc>, _: &EventConfigure) -> bool if state_ref.model.rows != rows || state_ref.model.columns != columns { if let Err(err) = state_ref.nvim().ui_try_resize(columns as u64, rows as u64) { - println!("Error trying resize nvim {}", err); + error!("Error trying resize nvim {}", err); } } Continue(false) })); } - false } impl RedrawEvents for State { @@ -1103,7 +1118,7 @@ impl RedrawEvents for State { impl GuiApi for State { fn set_font(&mut self, font_desc: &str) { self.set_font_desc(font_desc); - self.request_resize(); + self.request_nvim_resize(); let mut settings = self.settings.borrow_mut(); settings.set_font_source(FontSource::Rpc);