diff --git a/src/editor/mod.rs b/src/editor/mod.rs index 303997f..08e5f45 100644 --- a/src/editor/mod.rs +++ b/src/editor/mod.rs @@ -248,22 +248,26 @@ impl Editor { } } - pub fn update_dim(&mut self, h: usize, w: usize) { - self.editor_dim = (h, w); - self.update_view_dim(); - } - - pub fn update_view_dim(&mut self) { - // TODO: generalize for non-terminal UI. Maybe this isn't where it - // belongs, in fact. But for now, this is the easiest place to put - // it. + /// Updates the view dimensions, and returns whether that + /// actually changed anything. + pub fn update_dim(&mut self, h: usize, w: usize) -> bool { let line_count_digits = digit_count(self.buffer.line_count() as u32, 10) as usize; - // Minus 1 vertically for the header, minus one more than the digits in - // the line count for the gutter. - self.view_dim = ( - self.editor_dim.0 - 1, - self.editor_dim.1 - line_count_digits - 1, - ); + if self.editor_dim.0 != h || self.editor_dim.1 != w { + self.editor_dim = (h, w); + + // Minus 1 vertically for the header, minus one more than the digits in + // the line count for the gutter. + self.view_dim = ( + self.editor_dim.0 - 1, + self.editor_dim.1 - line_count_digits - 1, + ); + return true; + } else if self.view_dim.1 != (self.editor_dim.1 - line_count_digits - 1) { + self.view_dim.1 = self.editor_dim.1 - line_count_digits - 1; + return true; + } else { + return false; + } } pub fn undo(&mut self) { diff --git a/src/term_ui/mod.rs b/src/term_ui/mod.rs index bf960c4..011e5ca 100644 --- a/src/term_ui/mod.rs +++ b/src/term_ui/mod.rs @@ -59,18 +59,17 @@ macro_rules! ui_loop { // Check for screen resize let (w, h) = termion::terminal_size().unwrap(); - if $term_ui.width != w as usize || $term_ui.height != h as usize { + let needs_update = $term_ui + .editor + .update_dim(h as usize - 1, w as usize); + if needs_update { $term_ui.width = w as usize; $term_ui.height = h as usize; - $term_ui - .editor - .update_dim($term_ui.height - 1, $term_ui.width); - $term_ui.editor.update_view_dim(); + $term_ui.screen.resize(w as usize, h as usize); $term_ui .editor .formatter .set_wrap_width($term_ui.editor.view_dim.1); - $term_ui.screen.resize(w as usize, h as usize); should_redraw = true; } @@ -132,7 +131,6 @@ impl TermUI { self.width = w as usize; self.height = h as usize; self.editor.update_dim(self.height - 1, self.width); - self.editor.update_view_dim(); self.editor.formatter.set_wrap_width(self.editor.view_dim.1); self.screen.resize(w as usize, h as usize);