diff options
author | Ville Hakulinen <ville.hakulinen@gmail.com> | 2020-07-05 14:25:30 +0300 |
---|---|---|
committer | Ville Hakulinen <ville.hakulinen@gmail.com> | 2020-07-12 01:57:30 +0300 |
commit | 550602af079cb4ebf19e4cffde0e103869584f81 (patch) | |
tree | 21935e6af11405c9fefa87769656fece1749a257 | |
parent | af9e9990c114bbe1ccbad8cf7db07af6135d6a0a (diff) |
Add width and height to GridMetrics
-rw-r--r-- | src/ui/grid/grid.rs | 31 | ||||
-rw-r--r-- | src/ui/state.rs | 35 |
2 files changed, 35 insertions, 31 deletions
diff --git a/src/ui/grid/grid.rs b/src/ui/grid/grid.rs index a698cbb..963f235 100644 --- a/src/ui/grid/grid.rs +++ b/src/ui/grid/grid.rs @@ -19,10 +19,19 @@ use crate::ui::grid::render; use crate::ui::grid::row::Row; pub struct GridMetrics { + // Row count in the grid. pub rows: f64, + // Col count in the grid. pub cols: f64, + // Height of a cell. pub cell_height: f64, + // Width of a cell. pub cell_width: f64, + + // Width of the whole grid as required by the cell width and cols. + pub height: f64, + // Height of the whole grid as required by the cell height and rows. + pub width: f64, } pub enum ScrollDirection { @@ -364,20 +373,20 @@ impl Grid { pub fn get_grid_metrics(&self) -> GridMetrics { let ctx = self.context.borrow(); - // TODO(ville): At least on other than the base grid, we might want to get the metrics from - // the internal datastructures and not from calculations based on the drawingarea's size. - //let w = self.da.get_allocated_width(); - //let h = self.da.get_allocated_height(); - //let cols = (w / ctx.cell_metrics.width as i32) as u64; - //let rows = (h / ctx.cell_metrics.height as i32) as u64; - let row = ctx.rows.get(0).unwrap(); + let rows = ctx.rows.len() as f64; + let cols = row.len() as f64; + let cell_width = ctx.cell_metrics.width; + let cell_height = ctx.cell_metrics.height; + GridMetrics { - rows: ctx.rows.len() as f64, - cols: row.len() as f64, - cell_width: ctx.cell_metrics.width, - cell_height: ctx.cell_metrics.height, + rows, + cols, + cell_width, + cell_height, + width: cols * cell_width, + height: rows * cell_height, } } diff --git a/src/ui/state.rs b/src/ui/state.rs index a6dd89d..6ef09f2 100644 --- a/src/ui/state.rs +++ b/src/ui/state.rs @@ -167,9 +167,10 @@ impl UIState { self.windows.values().find(|w| w.grid_id == grid.id) { let grid_metrics = grid.get_grid_metrics(); - let width = grid_metrics.cols * grid_metrics.cell_width; - let height = grid_metrics.rows * grid_metrics.cell_height; - w.resize((width.ceil() as i32, height.ceil() as i32)); + w.resize(( + grid_metrics.width.ceil() as i32, + grid_metrics.height.ceil() as i32, + )); } } else { let grid = Grid::new( @@ -579,13 +580,10 @@ impl UIState { let anchor_metrics = anchor_grid.get_grid_metrics(); let grid_metrics = grid.get_grid_metrics(); - let width = grid_metrics.cols * grid_metrics.cell_width; - let height = grid_metrics.rows * grid_metrics.cell_height; - let (x, y) = win_float_anchor_pos( &evt, &anchor_metrics, - (width, height), + (grid_metrics.width, grid_metrics.height), (x_offset, y_offset), ); @@ -598,21 +596,18 @@ impl UIState { if new_size.0.is_some() || new_size.1.is_some() { let nvim = nvim.clone(); let grid = evt.grid; + let cols = new_size.0.unwrap_or_else(|| grid_metrics.cols) as i64; + let rows = new_size.1.unwrap_or_else(|| grid_metrics.rows) as i64; spawn_local(async move { - if let Err(err) = nvim - .ui_try_resize_grid( - grid, - new_size.0.unwrap_or_else(|| grid_metrics.cols) as i64, - new_size.1.unwrap_or_else(|| grid_metrics.rows) as i64, - ) - .await + if let Err(err) = + nvim.ui_try_resize_grid(grid, cols, rows).await { error!("Failed to resize grid({}): {}", grid, err); } }); } - window.set_position(x, y, width, height); + window.set_position(x, y, grid_metrics.width, grid_metrics.height); window.show(); } @@ -636,12 +631,13 @@ impl UIState { }); let grid_metrics = grid.get_grid_metrics(); - let width = grid_metrics.cols * grid_metrics.cell_width; - let height = grid_metrics.rows * grid_metrics.cell_height; window.set_external( &parent_win, - (width.ceil() as i32, height.ceil() as i32), + ( + grid_metrics.width.ceil() as i32, + grid_metrics.height.ceil() as i32, + ), ); // NOTE(ville): Without this, "new" grids (e.g. once added to a external @@ -668,8 +664,7 @@ impl UIState { let base_grid = self.grids.get(&1).unwrap(); let base_metrics = base_grid.get_grid_metrics(); let grid = self.grids.get(&e.grid).unwrap(); - let h = base_metrics.rows * base_metrics.cell_height - - e.row as f64 * base_metrics.cell_height; + let h = base_metrics.height - e.row as f64 * base_metrics.cell_height; self.msg_window.set_pos(&grid, e.row as f64, h); } |