From 08d8408adcd46e57e3fd64954f93930e0479c587 Mon Sep 17 00:00:00 2001 From: daa Date: Wed, 11 Oct 2017 18:55:29 +0300 Subject: [PATCH] Fix crush on startup (#17, #13) Solution: on_resize/ui_attach events must always update model, so model manipulation commands will always works right --- src/shell.rs | 24 +++++++++++++++++------- src/ui_model/mod.rs | 14 ++++++++++++++ 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/shell.rs b/src/shell.rs index f98e913..dab3c2d 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -88,7 +88,7 @@ impl State { let font_ctx = render::Context::new(FontDescription::from_string(DEFAULT_FONT_NAME)); State { - model: UiModel::new(1, 1), + model: UiModel::empty(), color_model: ColorModel::new(), nvim: Rc::new(RefCell::new(NeovimClient::new())), cur_attrs: None, @@ -135,6 +135,16 @@ impl State { self.nvim.clone() } + pub fn start_init(&self) -> bool { + let mut nvim = self.nvim.borrow_mut(); + if nvim.is_uninitialized() { + nvim.set_in_progress(); + true + } else { + false + } + } + pub fn set_detach_cb(&mut self, cb: Option) where F: FnMut() + Send + 'static, @@ -791,12 +801,10 @@ fn draw_initializing(state: &State, ctx: &cairo::Context) { } fn init_nvim(state_ref: &Arc>) { - let state = state_ref.borrow_mut(); - let mut nvim = state.nvim.borrow_mut(); - if nvim.is_uninitialized() { - nvim.set_in_progress(); - + let mut state = state_ref.borrow_mut(); + if state.start_init() { let (cols, rows) = state.calc_nvim_size(); + state.model = UiModel::new(rows as u64, cols as u64); state.resize_state.set( ResizeState::NvimResizeRequest(cols, rows), ); @@ -830,7 +838,9 @@ impl RedrawEvents for State { fn on_resize(&mut self, columns: u64, rows: u64) -> RepaintMode { match self.resize_state.get() { - ResizeState::NvimResizeTimer(..) => (), + ResizeState::NvimResizeTimer(..) => { + self.model = UiModel::new(rows, columns); + }, ResizeState::Wait | ResizeState::NvimResizeRequest(..) => { if self.model.columns != columns as usize || self.model.rows != rows as usize { diff --git a/src/ui_model/mod.rs b/src/ui_model/mod.rs index 1585a48..4b4a472 100644 --- a/src/ui_model/mod.rs +++ b/src/ui_model/mod.rs @@ -41,6 +41,20 @@ impl UiModel { } } + pub fn empty() -> UiModel { + UiModel { + columns: 0, + rows: 0, + cur_row: 0, + cur_col: 0, + model: Box::new([]), + top: 0, + bot: 0, + left: 0, + right: 0, + } + } + #[inline] pub fn model(&self) -> &[Line] { &self.model