diff options
author | Ville Hakulinen <ville.hakulinen@gmail.com> | 2020-07-06 11:10:12 +0300 |
---|---|---|
committer | Ville Hakulinen <ville.hakulinen@gmail.com> | 2020-07-12 01:57:30 +0300 |
commit | 2c3a31cfa4294681cb50fcbe8745a2da43540052 (patch) | |
tree | 62c0d0ec6956ddda5e65c8ffa970a33eed31288b | |
parent | 550602af079cb4ebf19e4cffde0e103869584f81 (diff) |
Simplify grid resize
-rw-r--r-- | src/ui/grid/context.rs | 17 | ||||
-rw-r--r-- | src/ui/grid/grid.rs | 42 | ||||
-rw-r--r-- | src/ui/grid/row.rs | 10 | ||||
-rw-r--r-- | src/ui/state.rs | 2 |
4 files changed, 24 insertions, 47 deletions
diff --git a/src/ui/grid/context.rs b/src/ui/grid/context.rs index eb870f4..9018b29 100644 --- a/src/ui/grid/context.rs +++ b/src/ui/grid/context.rs @@ -120,16 +120,27 @@ impl Context { } /// Updates internals that are dependant on the drawing area. - pub fn update( + pub fn resize( &mut self, da: &DrawingArea, win: &gdk::Window, cols: usize, rows: usize, hl_defs: &HlDefs, - prev_cols: usize, - prev_rows: usize, ) { + let prev_rows = self.rows.len(); + let prev_cols = self.rows.get(0).map(|r| r.len()).unwrap_or(0); + + if self.rows.len() != rows { + self.rows.resize_with(rows, || Row::new(rows)); + } + + if self.rows.get(0).unwrap().len() != cols { + for row in self.rows.iter_mut() { + row.resize(cols); + } + } + let pctx = da.get_pango_context().unwrap(); pctx.set_font_description(&self.cell_metrics.font.as_pango_font()); diff --git a/src/ui/grid/grid.rs b/src/ui/grid/grid.rs index 963f235..02fcc76 100644 --- a/src/ui/grid/grid.rs +++ b/src/ui/grid/grid.rs @@ -16,7 +16,6 @@ use crate::ui::color::HlDefs; use crate::ui::font::Font; use crate::ui::grid::context::Context; use crate::ui::grid::render; -use crate::ui::grid::row::Row; pub struct GridMetrics { // Row count in the grid. @@ -406,48 +405,17 @@ impl Grid { pub fn resize( &self, win: &gdk::Window, - width: u64, - height: u64, + cols: u64, + rows: u64, hl_defs: &HlDefs, ) { let mut ctx = self.context.borrow_mut(); - - let width = width as usize; - let height = height as usize; - - let prev_height = ctx.rows.len(); - let prev_width = if let Some(row) = ctx.rows.get(0) { - row.len() - } else { - 0 - }; - - if ctx.rows.len() > height { - ctx.rows.truncate(height); - } else if ctx.rows.len() < height { - for _ in ctx.rows.len()..height { - ctx.rows.push(Row::new(width)); - } - } - - if ctx.rows.get(0).unwrap().len() < width { - for row in ctx.rows.iter_mut() { - row.grow(width); - } - } else { - for row in ctx.rows.iter_mut() { - row.truncate(width); - } - } - - ctx.update( + ctx.resize( &self.da, win, - width, - height, + cols as usize, + rows as usize, hl_defs, - prev_width, - prev_height, ); } diff --git a/src/ui/grid/row.rs b/src/ui/grid/row.rs index fa29964..e21507b 100644 --- a/src/ui/grid/row.rs +++ b/src/ui/grid/row.rs @@ -67,7 +67,7 @@ impl Row { self.cells = Row::create_empty_cells(self.len).into_boxed_slice(); } - pub fn grow(&mut self, new_size: usize) { + pub fn resize(&mut self, new_size: usize) { let mut n = self.cells.clone().into_vec(); n.resize_with(new_size, || Cell { text: String::from(" "), @@ -79,10 +79,6 @@ impl Row { self.len = self.cells.len(); } - pub fn truncate(&mut self, new_size: usize) { - self.grow(new_size); - } - /// Clears range from `from` to `to`. pub fn clear_range(&mut self, from: usize, to: usize) { for i in from..to { @@ -931,7 +927,7 @@ mod tests { #[test] fn test_row_grow() { let mut row = Row::new(10); - row.grow(15); + row.resize(15); assert_eq!(row.len, 15); assert_eq!( @@ -943,7 +939,7 @@ mod tests { #[test] fn test_row_truncate() { let mut row = Row::new(10); - row.truncate(5); + row.resize(5); assert_eq!(row.len, 5); assert_eq!( diff --git a/src/ui/state.rs b/src/ui/state.rs index 6ef09f2..9cdd767 100644 --- a/src/ui/state.rs +++ b/src/ui/state.rs @@ -1073,6 +1073,8 @@ mod tests { cell_width: row.cell_width, rows: 0.0, cols: 0.0, + width: 0.0, + height: 0.0, }, (row.width, row.height), (row.x_offset, row.y_offset), |