Fix crush on startup (#17, #13)

Solution: on_resize/ui_attach events must always update model, so model manipulation commands will always works right
This commit is contained in:
daa 2017-10-11 18:55:29 +03:00
parent 0f4234a622
commit 08d8408adc
2 changed files with 31 additions and 7 deletions

View File

@ -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<F>(&mut self, cb: Option<F>)
where
F: FnMut() + Send + 'static,
@ -791,12 +801,10 @@ fn draw_initializing(state: &State, ctx: &cairo::Context) {
}
fn init_nvim(state_ref: &Arc<UiMutex<State>>) {
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 {

View File

@ -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