From 7a46e1cac57cbc308c111bfe8fec37e1fd74ff66 Mon Sep 17 00:00:00 2001 From: daa84 Date: Tue, 11 Jul 2017 18:14:46 +0300 Subject: [PATCH 1/5] Starting point --- src/nvim.rs | 59 ++++++++++++++++++++++------ src/shell.rs | 106 ++++++++++++++++++++++++++++++++++----------------- 2 files changed, 119 insertions(+), 46 deletions(-) diff --git a/src/nvim.rs b/src/nvim.rs index eabcae5..0e4f800 100644 --- a/src/nvim.rs +++ b/src/nvim.rs @@ -172,15 +172,24 @@ impl ModeInfo { #[derive(Debug)] pub struct NvimInitError { source: Box, - cmd: String, + cmd: Option, } impl NvimInitError { + pub fn new_post_init(error: E) -> NvimInitError + where E: Into> + { + NvimInitError { + cmd: None, + source: error.into(), + } + } + pub fn new(cmd: &Command, error: E) -> NvimInitError where E: Into> { NvimInitError { - cmd: format!("{:?}", cmd), + cmd: Some(format!("{:?}", cmd)), source: error.into(), } } @@ -189,8 +198,8 @@ impl NvimInitError { format!("{}", self.source) } - pub fn cmd(&self) -> &str { - &self.cmd + pub fn cmd(&self) -> Option<&String> { + self.cmd.as_ref() } } @@ -210,11 +219,9 @@ impl error::Error for NvimInitError { } } -pub fn initialize(shell: Arc>, - nvim_bin_path: Option<&String>, - cols: u64, - rows: u64) - -> result::Result { +pub fn start(shell: Arc>, + nvim_bin_path: Option<&String>) + -> result::Result { let mut cmd = if let Some(path) = nvim_bin_path { Command::new(path) } else { @@ -250,15 +257,29 @@ pub fn initialize(shell: Arc>, nvim.session .start_event_loop_handler(NvimHandler::new(shell)); + + Ok(nvim) +} + +pub fn post_start_init(nvim: &mut Neovim, + open_path: Option<&String>, + cols: u64, + rows: u64) + -> result::Result<(), NvimInitError> { let mut opts = UiAttachOptions::new(); opts.set_popupmenu_external(false); opts.set_tabline_external(true); nvim.ui_attach(cols, rows, opts) - .map_err(|e| NvimInitError::new(&cmd, e))?; + .map_err(|e| NvimInitError::new_post_init(e))?; nvim.command("runtime! ginit.vim") - .map_err(|e| NvimInitError::new(&cmd, e))?; + .map_err(|e| NvimInitError::new_post_init(e))?; - Ok(nvim) + if let Some(path) = open_path { + nvim.command(&format!("e {}", path)) + .map_err(|e| NvimInitError::new_post_init(e))?; + } + + Ok(()) } pub struct NvimHandler { @@ -507,11 +528,19 @@ impl RepaintMode { enum NeovimClientWrapper { Uninitialized, + InitInProgress, Initialized(Neovim), Error, } impl NeovimClientWrapper { + pub fn is_uninitialized(&self) -> bool { + match *self { + NeovimClientWrapper::Uninitialized => true, + _ => false, + } + } + pub fn is_initialized(&self) -> bool { match *self { NeovimClientWrapper::Initialized(_) => true, @@ -529,6 +558,7 @@ impl NeovimClientWrapper { pub fn nvim(&self) -> &Neovim { match *self { NeovimClientWrapper::Initialized(ref nvim) => nvim, + NeovimClientWrapper::InitInProgress | NeovimClientWrapper::Uninitialized => panic!("Access to uninitialized neovim client"), NeovimClientWrapper::Error => { panic!("Access to neovim client that is not started due to some error") @@ -539,6 +569,7 @@ impl NeovimClientWrapper { pub fn nvim_mut(&mut self) -> &mut Neovim { match *self { NeovimClientWrapper::Initialized(ref mut nvim) => nvim, + NeovimClientWrapper::InitInProgress | NeovimClientWrapper::Uninitialized => panic!("Access to uninitialized neovim client"), NeovimClientWrapper::Error => { panic!("Access to neovim client that is not started due to some error") @@ -568,6 +599,10 @@ impl NeovimClient { self.nvim.is_initialized() } + pub fn is_uninitialized(&self) -> bool { + self.nvim.is_uninitialized() + } + pub fn is_error(&self) -> bool { self.nvim.is_error() } diff --git a/src/shell.rs b/src/shell.rs index cdba5cd..49a7e36 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -250,6 +250,7 @@ impl UiState { } } +#[derive(Clone)] pub struct ShellOptions { nvim_bin_path: Option, open_path: Option, @@ -545,46 +546,82 @@ fn gtk_draw(state_arc: &Arc>, ctx: &cairo::Context) -> Inhibit { Inhibit(false) } +fn init_nvim_async(state_arc: Arc>, + options: ShellOptions, + cols: usize, + rows: usize) { + // execute nvim + let mut nvim = match nvim::start(state_arc.clone(), options.nvim_bin_path.as_ref()) { + Ok(nvim) => nvim, + Err(err) => { + // TODO: process error // + return; + } + }; + + // add callback on session end + let guard = nvim.session.take_dispatch_guard(); + let state_ref = state_arc.clone(); + thread::spawn(move || { + guard.join().expect("Can't join dispatch thread"); + + idle_cb_call!(state_ref.detach_cb()); + }); + + // attach ui + let state_ref = state_arc.clone(); + let mut post_init = Some(move || { + let mut nvim = nvim; + if let Err(err) = nvim::post_start_init(&mut nvim, + options.open_path.as_ref(), + cols as u64, + rows as u64) { + // TODO: process error // + } + + let state = state_arc.borrow_mut(); + state.nvim.borrow_mut().set_nvim(nvim); + }); + + glib::idle_add(move || { + let cl = post_init.take(); + cl(); + Continue(false) + }); + + //{ + //Ok(nvim) => nvim, + //Err(err) => { + //nvim_client.set_error(); + //state + //.error_area + //.show_nvim_start_error(&err.source(), err.cmd()); + + //let stack = state.stack.clone(); + //gtk::idle_add(move || { + //stack.set_visible_child_name("Error"); + //Continue(false) + //}); + + //return; + //} + //}; + + + + //nvim_client.set_nvim(nvim); +} fn init_nvim(state_arc: &Arc>) { let state = state_arc.borrow(); - let mut nvim_client = state.nvim.borrow_mut(); - if !nvim_client.is_initialized() && !nvim_client.is_error() { + if state.nvim.borrow().is_uninitialized() { let (cols, rows) = state.calc_nvim_size().unwrap(); - let mut nvim = match nvim::initialize(state_arc.clone(), - state.options.nvim_bin_path.as_ref(), - cols as u64, - rows as u64) { - Ok(nvim) => nvim, - Err(err) => { - nvim_client.set_error(); - state.error_area.show_nvim_start_error(&err.source(), err.cmd()); - let stack = state.stack.clone(); - gtk::idle_add(move || { - stack.set_visible_child_name("Error"); - Continue(false) - }); + let state_arc = state_arc.clone(); + let options = state.options.clone(); + thread::spawn(move || { init_nvim_async(state_arc, options, cols, rows); }); - return; - } - }; - - if let Some(ref path) = state.options.open_path { - nvim.command(&format!("e {}", path)).report_err(&mut nvim); - } - - let guard = nvim.session.take_dispatch_guard(); - - let state_ref = state_arc.clone(); - thread::spawn(move || { - guard.join().expect("Can't join dispatch thread"); - - idle_cb_call!(state_ref.detach_cb()); - }); - - nvim_client.set_nvim(nvim); } } @@ -1008,7 +1045,8 @@ impl RedrawEvents for State { fn mode_info_set(&mut self, cursor_style_enabled: bool, - mode_info: Vec) -> RepaintMode { + mode_info: Vec) + -> RepaintMode { self.mode.set_info(cursor_style_enabled, mode_info); RepaintMode::Nothing } From ba20088bc6d2e3c24d34dd1a8df4b2089890b69a Mon Sep 17 00:00:00 2001 From: daa84 Date: Wed, 12 Jul 2017 18:32:10 +0300 Subject: [PATCH 2/5] Fix compilation, show errors --- src/error.rs | 13 +++++++++ src/nvim.rs | 50 ++++++++++++++++++---------------- src/shell.rs | 76 +++++++++++++++++++++++++++------------------------- 3 files changed, 80 insertions(+), 59 deletions(-) diff --git a/src/error.rs b/src/error.rs index ccda4bb..de58f88 100644 --- a/src/error.rs +++ b/src/error.rs @@ -28,6 +28,19 @@ impl ErrorArea { ErrorArea { base, label } } + pub fn show_nvim_init_error(&self, err: &str) { + error!("Can't initialize nvim: {}", err); + self.label.set_markup(&format!("Can't initialize nvim:\n\ + {}\n\n\ + Possible error reasons:\n\ + ● Not supported nvim version (minimum supported version is {})\n\ + ● Error in configuration file (init.vim or ginit.vim)\n\ + ● Wrong nvim binary path \ + (right path can be passed with --nvim-bin-path=path_here)", + encode_minimal(err), shell::MINIMUM_SUPPORTED_NVIM_VERSION)); + self.base.show_all(); + } + pub fn show_nvim_start_error(&self, err: &str, cmd: &str) { error!("Can't start nvim: {}\nCommand line: {}", err, cmd); self.label.set_markup(&format!("Can't start nvim instance:\n\ diff --git a/src/nvim.rs b/src/nvim.rs index 0e4f800..a0e7185 100644 --- a/src/nvim.rs +++ b/src/nvim.rs @@ -526,41 +526,41 @@ impl RepaintMode { } -enum NeovimClientWrapper { +enum NeovimClientState { Uninitialized, InitInProgress, Initialized(Neovim), Error, } -impl NeovimClientWrapper { +impl NeovimClientState { pub fn is_uninitialized(&self) -> bool { match *self { - NeovimClientWrapper::Uninitialized => true, + NeovimClientState::Uninitialized => true, _ => false, } } pub fn is_initialized(&self) -> bool { match *self { - NeovimClientWrapper::Initialized(_) => true, + NeovimClientState::Initialized(_) => true, _ => false, } } pub fn is_error(&self) -> bool { match *self { - NeovimClientWrapper::Error => true, + NeovimClientState::Error => true, _ => false, } } pub fn nvim(&self) -> &Neovim { match *self { - NeovimClientWrapper::Initialized(ref nvim) => nvim, - NeovimClientWrapper::InitInProgress | - NeovimClientWrapper::Uninitialized => panic!("Access to uninitialized neovim client"), - NeovimClientWrapper::Error => { + NeovimClientState::Initialized(ref nvim) => nvim, + NeovimClientState::InitInProgress | + NeovimClientState::Uninitialized => panic!("Access to uninitialized neovim client"), + NeovimClientState::Error => { panic!("Access to neovim client that is not started due to some error") } } @@ -568,10 +568,10 @@ impl NeovimClientWrapper { pub fn nvim_mut(&mut self) -> &mut Neovim { match *self { - NeovimClientWrapper::Initialized(ref mut nvim) => nvim, - NeovimClientWrapper::InitInProgress | - NeovimClientWrapper::Uninitialized => panic!("Access to uninitialized neovim client"), - NeovimClientWrapper::Error => { + NeovimClientState::Initialized(ref mut nvim) => nvim, + NeovimClientState::InitInProgress | + NeovimClientState::Uninitialized => panic!("Access to uninitialized neovim client"), + NeovimClientState::Error => { panic!("Access to neovim client that is not started due to some error") } } @@ -579,40 +579,44 @@ impl NeovimClientWrapper { } pub struct NeovimClient { - nvim: NeovimClientWrapper, + state: NeovimClientState, } impl NeovimClient { pub fn new() -> Self { - NeovimClient { nvim: NeovimClientWrapper::Uninitialized } + NeovimClient { state: NeovimClientState::Uninitialized } } - pub fn set_nvim(&mut self, nvim: Neovim) { - self.nvim = NeovimClientWrapper::Initialized(nvim); + pub fn set_initialized(&mut self, nvim: Neovim) { + self.state = NeovimClientState::Initialized(nvim); } pub fn set_error(&mut self) { - self.nvim = NeovimClientWrapper::Error; + self.state = NeovimClientState::Error; + } + + pub fn set_in_progress(&mut self) { + self.state = NeovimClientState::InitInProgress; } pub fn is_initialized(&self) -> bool { - self.nvim.is_initialized() + self.state.is_initialized() } pub fn is_uninitialized(&self) -> bool { - self.nvim.is_uninitialized() + self.state.is_uninitialized() } pub fn is_error(&self) -> bool { - self.nvim.is_error() + self.state.is_error() } pub fn nvim(&self) -> &Neovim { - self.nvim.nvim() + self.state.nvim() } pub fn nvim_mut(&mut self) -> &mut Neovim { - self.nvim.nvim_mut() + self.state.nvim_mut() } } diff --git a/src/shell.rs b/src/shell.rs index 49a7e36..f11fd6b 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -238,6 +238,14 @@ impl State { None } + + fn show_error_area(&self) { + let stack = self.stack.clone(); + gtk::idle_add(move || { + stack.set_visible_child_name("Error"); + Continue(false) + }); + } } pub struct UiState { @@ -554,7 +562,17 @@ fn init_nvim_async(state_arc: Arc>, let mut nvim = match nvim::start(state_arc.clone(), options.nvim_bin_path.as_ref()) { Ok(nvim) => nvim, Err(err) => { - // TODO: process error // + let source = err.source(); + let cmd = err.cmd().unwrap().to_owned(); + + glib::idle_add(move || { + let state = state_arc.borrow(); + state.nvim.borrow_mut().set_error(); + state.error_area.show_nvim_start_error(&source, &cmd); + state.show_error_area(); + + Continue(false) + }); return; } }; @@ -569,59 +587,45 @@ fn init_nvim_async(state_arc: Arc>, }); // attach ui - let state_ref = state_arc.clone(); - let mut post_init = Some(move || { - let mut nvim = nvim; + let mut nvim = Some(nvim); + glib::idle_add(move || { + let mut nvim = nvim.take().unwrap(); if let Err(err) = nvim::post_start_init(&mut nvim, options.open_path.as_ref(), cols as u64, rows as u64) { - // TODO: process error // + let source = err.source(); + + let state_ref = state_arc.clone(); + glib::idle_add(move || { + let state = state_ref.borrow(); + state.nvim.borrow_mut().set_error(); + state.error_area.show_nvim_init_error(&source); + state.show_error_area(); + + Continue(false) + }); + } else { + let state = state_arc.borrow_mut(); + state.nvim.borrow_mut().set_initialized(nvim); } - let state = state_arc.borrow_mut(); - state.nvim.borrow_mut().set_nvim(nvim); - }); - - glib::idle_add(move || { - let cl = post_init.take(); - cl(); Continue(false) }); - - //{ - //Ok(nvim) => nvim, - //Err(err) => { - //nvim_client.set_error(); - //state - //.error_area - //.show_nvim_start_error(&err.source(), err.cmd()); - - //let stack = state.stack.clone(); - //gtk::idle_add(move || { - //stack.set_visible_child_name("Error"); - //Continue(false) - //}); - - //return; - //} - //}; - - - - //nvim_client.set_nvim(nvim); } fn init_nvim(state_arc: &Arc>) { let state = state_arc.borrow(); - if state.nvim.borrow().is_uninitialized() { + let mut nvim = state.nvim.borrow_mut(); + if nvim.is_uninitialized() { + nvim.set_in_progress(); + let (cols, rows) = state.calc_nvim_size().unwrap(); let state_arc = state_arc.clone(); let options = state.options.clone(); thread::spawn(move || { init_nvim_async(state_arc, options, cols, rows); }); - } } From 4d3e9689be3f4a9bb0b829c8a1fc849317f89ebd Mon Sep 17 00:00:00 2001 From: daa84 Date: Thu, 13 Jul 2017 18:12:20 +0300 Subject: [PATCH 3/5] Code refactor --- src/shell.rs | 51 +++++++++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/src/shell.rs b/src/shell.rs index f11fd6b..69e2a9d 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -554,6 +554,33 @@ fn gtk_draw(state_arc: &Arc>, ctx: &cairo::Context) -> Inhibit { Inhibit(false) } +fn show_nvim_start_error(err: nvim::NvimInitError, state_arc: Arc>) { + let source = err.source(); + let cmd = err.cmd().unwrap().to_owned(); + + glib::idle_add(move || { + let state = state_arc.borrow(); + state.nvim.borrow_mut().set_error(); + state.error_area.show_nvim_start_error(&source, &cmd); + state.show_error_area(); + + Continue(false) + }); +} + +fn show_nvim_init_error(err: nvim::NvimInitError, state_arc: Arc>) { + let source = err.source(); + + glib::idle_add(move || { + let state = state_arc.borrow(); + state.nvim.borrow_mut().set_error(); + state.error_area.show_nvim_init_error(&source); + state.show_error_area(); + + Continue(false) + }); +} + fn init_nvim_async(state_arc: Arc>, options: ShellOptions, cols: usize, @@ -562,17 +589,7 @@ fn init_nvim_async(state_arc: Arc>, let mut nvim = match nvim::start(state_arc.clone(), options.nvim_bin_path.as_ref()) { Ok(nvim) => nvim, Err(err) => { - let source = err.source(); - let cmd = err.cmd().unwrap().to_owned(); - - glib::idle_add(move || { - let state = state_arc.borrow(); - state.nvim.borrow_mut().set_error(); - state.error_area.show_nvim_start_error(&source, &cmd); - state.show_error_area(); - - Continue(false) - }); + show_nvim_start_error(err, state_arc); return; } }; @@ -594,17 +611,7 @@ fn init_nvim_async(state_arc: Arc>, options.open_path.as_ref(), cols as u64, rows as u64) { - let source = err.source(); - - let state_ref = state_arc.clone(); - glib::idle_add(move || { - let state = state_ref.borrow(); - state.nvim.borrow_mut().set_error(); - state.error_area.show_nvim_init_error(&source); - state.show_error_area(); - - Continue(false) - }); + show_nvim_init_error(err, state_arc.clone()); } else { let state = state_arc.borrow_mut(); state.nvim.borrow_mut().set_initialized(nvim); From d0ca58d7be3fe76a92fc6bfce7d7c9c9606b7441 Mon Sep 17 00:00:00 2001 From: daa Date: Sat, 22 Jul 2017 13:32:56 +0300 Subject: [PATCH 4/5] Show loading message --- src/nvim.rs | 18 ++++++------ src/shell.rs | 77 ++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 66 insertions(+), 29 deletions(-) diff --git a/src/nvim.rs b/src/nvim.rs index a0e7185..93e5470 100644 --- a/src/nvim.rs +++ b/src/nvim.rs @@ -534,6 +534,13 @@ enum NeovimClientState { } impl NeovimClientState { + pub fn is_initializing(&self) -> bool { + match *self { + NeovimClientState::InitInProgress => true, + _ => false, + } + } + pub fn is_uninitialized(&self) -> bool { match *self { NeovimClientState::Uninitialized => true, @@ -548,13 +555,6 @@ impl NeovimClientState { } } - pub fn is_error(&self) -> bool { - match *self { - NeovimClientState::Error => true, - _ => false, - } - } - pub fn nvim(&self) -> &Neovim { match *self { NeovimClientState::Initialized(ref nvim) => nvim, @@ -607,8 +607,8 @@ impl NeovimClient { self.state.is_uninitialized() } - pub fn is_error(&self) -> bool { - self.state.is_error() + pub fn is_initializing(&self) -> bool { + self.state.is_initializing() } pub fn nvim(&self) -> &Neovim { diff --git a/src/shell.rs b/src/shell.rs index 69e2a9d..b0a7079 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -302,7 +302,7 @@ impl Shell { } pub fn init(&mut self) { - let mut state = self.state.borrow_mut(); + let state = self.state.borrow(); state.drawing_area.set_hexpand(true); state.drawing_area.set_vexpand(true); state.drawing_area.set_can_focus(true); @@ -386,8 +386,6 @@ impl Shell { state .drawing_area .connect_configure_event(move |_, ev| gtk_configure_event(&ref_state, ev)); - - state.cursor.as_mut().unwrap().start(); } #[cfg(unix)] @@ -545,10 +543,11 @@ fn gtk_draw(state_arc: &Arc>, ctx: &cairo::Context) -> Inhibit { init_nvim(state_arc); let mut state = state_arc.borrow_mut(); - // in case nvim not initialized - if !state.nvim.borrow().is_error() { + if state.nvim.borrow().is_initialized() { draw(&*state, ctx); request_window_resize(&mut *state); + } else if state.nvim.borrow().is_initializing() { + draw_initializing(&*state, ctx); } Inhibit(false) @@ -559,26 +558,26 @@ fn show_nvim_start_error(err: nvim::NvimInitError, state_arc: Arc let cmd = err.cmd().unwrap().to_owned(); glib::idle_add(move || { - let state = state_arc.borrow(); - state.nvim.borrow_mut().set_error(); - state.error_area.show_nvim_start_error(&source, &cmd); - state.show_error_area(); + let state = state_arc.borrow(); + state.nvim.borrow_mut().set_error(); + state.error_area.show_nvim_start_error(&source, &cmd); + state.show_error_area(); - Continue(false) - }); + Continue(false) + }); } fn show_nvim_init_error(err: nvim::NvimInitError, state_arc: Arc>) { let source = err.source(); glib::idle_add(move || { - let state = state_arc.borrow(); - state.nvim.borrow_mut().set_error(); - state.error_area.show_nvim_init_error(&source); - state.show_error_area(); + let state = state_arc.borrow(); + state.nvim.borrow_mut().set_error(); + state.error_area.show_nvim_init_error(&source); + state.show_error_area(); - Continue(false) - }); + Continue(false) + }); } fn init_nvim_async(state_arc: Arc>, @@ -613,8 +612,9 @@ fn init_nvim_async(state_arc: Arc>, rows as u64) { show_nvim_init_error(err, state_arc.clone()); } else { - let state = state_arc.borrow_mut(); + let mut state = state_arc.borrow_mut(); state.nvim.borrow_mut().set_initialized(nvim); + state.cursor.as_mut().unwrap().start(); } Continue(false) @@ -632,7 +632,7 @@ fn init_nvim(state_arc: &Arc>) { let state_arc = state_arc.clone(); let options = state.options.clone(); - thread::spawn(move || { init_nvim_async(state_arc, options, cols, rows); }); + thread::spawn(move || init_nvim_async(state_arc, options, cols, rows)); } } @@ -687,6 +687,43 @@ fn draw_backgound(state: &State, } } +fn draw_initializing(state: &State, ctx: &cairo::Context) { + let layout = ctx.create_pango_layout(); + let desc = state.create_pango_font(); + let alloc = state.drawing_area.get_allocation(); + let line_height = state.line_height.unwrap(); + let char_width = state.char_width.unwrap(); + + ctx.set_source_rgb(state.bg_color.0, state.bg_color.1, state.bg_color.2); + ctx.paint(); + + layout.set_font_description(&desc); + layout.set_text("Loading..", -1); + let (width, height) = layout.get_pixel_size(); + + let x = alloc.width as f64 / 2.0 - width as f64 / 2.0; + let y = alloc.height as f64 / 2.0 - height as f64 / 2.0; + + ctx.move_to(x, y); + ctx.set_source_rgb(state.fg_color.0, state.fg_color.1, state.fg_color.2); + ctx.update_pango_layout(&layout); + ctx.show_pango_layout(&layout); + + + ctx.move_to(x + width as f64, y); + state + .cursor + .as_ref() + .unwrap() + .draw(ctx, + state, + char_width, + line_height, + y, + false, + &state.bg_color); +} + fn draw(state: &State, ctx: &cairo::Context) { let layout = ctx.create_pango_layout(); let mut desc = state.create_pango_font(); @@ -753,7 +790,7 @@ fn draw(state: &State, ctx: &cairo::Context) { if !cell.ch.is_whitespace() { update_font_description(&mut desc, &cell.attrs); - layout.set_font_description(Some(&desc)); + layout.set_font_description(&desc); buf.clear(); buf.push(cell.ch); layout.set_text(&buf, -1); From 2106b69c1578ac35d8c66d04001f4376c0d390ce Mon Sep 17 00:00:00 2001 From: daa Date: Sat, 22 Jul 2017 16:27:17 +0300 Subject: [PATCH 5/5] 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);