diff options
author | Amir Arad <greenshade@gmail.com> | 2022-04-12 11:48:10 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-12 10:48:10 +0200 |
commit | f9278a9ab838a832b2926a07abaa6227ccec65d6 (patch) | |
tree | cfed04cdb2a6b1d554bc8467640d00c24b20936a | |
parent | bc44a77f1c80c27f5acc4be38853244977148bc5 (diff) |
refactor(tab): tiled_pane_grid (#1299)
* use `try_` methods when applicable
* fix bug
* extract commonresize check logic
* clean unused import
-rw-r--r-- | zellij-server/src/panes/tiled_panes/tiled_pane_grid.rs | 188 |
1 files changed, 64 insertions, 124 deletions
diff --git a/zellij-server/src/panes/tiled_panes/tiled_pane_grid.rs b/zellij-server/src/panes/tiled_panes/tiled_pane_grid.rs index 5bbaf65a4..27acc1e50 100644 --- a/zellij-server/src/panes/tiled_panes/tiled_pane_grid.rs +++ b/zellij-server/src/panes/tiled_panes/tiled_pane_grid.rs @@ -41,54 +41,33 @@ impl<'a> TiledPaneGrid<'a> { let mut pane_resizer = PaneResizer::new(self.panes.clone()); pane_resizer.layout(direction, space) } - pub fn resize_pane_left(&'a mut self, pane_id: &PaneId) { + pub fn resize_pane_left(&mut self, pane_id: &PaneId) { // TODO: find out by how much we actually reduced and only reduce by that much - if self.can_increase_pane_and_surroundings_left(pane_id, RESIZE_PERCENT) { - self.increase_pane_and_surroundings_left(pane_id, RESIZE_PERCENT); - // let mut pane_resizer = PaneResizer::new(self.panes.clone()); - let mut pane_resizer = PaneResizer::new(self.panes.clone()); - let _ = pane_resizer.layout(Direction::Horizontal, self.display_area.cols); - } else if self.can_reduce_pane_and_surroundings_left(pane_id, RESIZE_PERCENT) { - self.reduce_pane_and_surroundings_left(pane_id, RESIZE_PERCENT); - let mut pane_resizer = PaneResizer::new(self.panes.clone()); - let _ = pane_resizer.layout(Direction::Horizontal, self.display_area.cols); + if self.try_increase_pane_and_surroundings_left(pane_id, RESIZE_PERCENT) { + return; } + self.try_reduce_pane_and_surroundings_left(pane_id, RESIZE_PERCENT); } pub fn resize_pane_right(&mut self, pane_id: &PaneId) { // TODO: find out by how much we actually reduced and only reduce by that much - if self.can_increase_pane_and_surroundings_right(pane_id, RESIZE_PERCENT) { - self.increase_pane_and_surroundings_right(pane_id, RESIZE_PERCENT); - let mut pane_resizer = PaneResizer::new(self.panes.clone()); - let _ = pane_resizer.layout(Direction::Horizontal, self.display_area.cols); - } else if self.can_reduce_pane_and_surroundings_right(pane_id, RESIZE_PERCENT) { - self.reduce_pane_and_surroundings_right(pane_id, RESIZE_PERCENT); - let mut pane_resizer = PaneResizer::new(self.panes.clone()); - let _ = pane_resizer.layout(Direction::Horizontal, self.display_area.cols); + if self.try_increase_pane_and_surroundings_right(pane_id, RESIZE_PERCENT) { + return; } + self.try_reduce_pane_and_surroundings_right(pane_id, RESIZE_PERCENT); } pub fn resize_pane_down(&mut self, pane_id: &PaneId) { // TODO: find out by how much we actually reduced and only reduce by that much - if self.can_increase_pane_and_surroundings_down(pane_id, RESIZE_PERCENT) { - self.increase_pane_and_surroundings_down(pane_id, RESIZE_PERCENT); - let mut pane_resizer = PaneResizer::new(self.panes.clone()); - let _ = pane_resizer.layout(Direction::Vertical, self.display_area.rows); - } else if self.can_reduce_pane_and_surroundings_down(pane_id, RESIZE_PERCENT) { - self.reduce_pane_and_surroundings_down(pane_id, RESIZE_PERCENT); - let mut pane_resizer = PaneResizer::new(self.panes.clone()); - let _ = pane_resizer.layout(Direction::Vertical, self.display_area.rows); + if self.try_increase_pane_and_surroundings_down(pane_id, RESIZE_PERCENT) { + return; } + self.try_reduce_pane_and_surroundings_down(pane_id, RESIZE_PERCENT); } pub fn resize_pane_up(&mut self, pane_id: &PaneId) { // TODO: find out by how much we actually reduced and only reduce by that much - if self.can_increase_pane_and_surroundings_up(pane_id, RESIZE_PERCENT) { - self.increase_pane_and_surroundings_up(pane_id, RESIZE_PERCENT); - let mut pane_resizer = PaneResizer::new(self.panes.clone()); - let _ = pane_resizer.layout(Direction::Vertical, self.display_area.rows); - } else if self.can_reduce_pane_and_surroundings_up(pane_id, RESIZE_PERCENT) { - self.reduce_pane_and_surroundings_up(pane_id, RESIZE_PERCENT); - let mut pane_resizer = PaneResizer::new(self.panes.clone()); - let _ = pane_resizer.layout(Direction::Vertical, self.display_area.rows); + if self.try_increase_pane_and_surroundings_up(pane_id, RESIZE_PERCENT) { + return; } + self.try_reduce_pane_and_surroundings_up(pane_id, RESIZE_PERCENT); } pub fn resize_increase(&mut self, pane_id: &PaneId) { if self.try_increase_pane_and_surroundings_right_and_down(pane_id) { @@ -140,138 +119,99 @@ impl<'a> TiledPaneGrid<'a> { self.try_reduce_pane_and_surroundings_down(pane_id, RESIZE_PERCENT); } fn can_increase_pane_and_surroundings_right(&self, pane_id: &PaneId, increase_by: f64) -> bool { - let panes = self.panes.borrow(); if let Some(panes_to_the_right) = self.pane_ids_directly_right_of(pane_id) { - panes_to_the_right.iter().all(|id| { - let p = panes.get(id).unwrap(); - if let Some(cols) = p.position_and_size().cols.as_percent() { - let current_fixed_cols = p.position_and_size().cols.as_usize(); - let will_reduce_by = - ((self.display_area.cols as f64 / 100.0) * increase_by) as usize; - cols - increase_by >= RESIZE_PERCENT - && current_fixed_cols.saturating_sub(will_reduce_by) >= p.min_width() - } else { - false - } - }) + panes_to_the_right + .iter() + .all(|id| self.can_reduce_pane_width(id, increase_by)) } else { false } } fn can_increase_pane_and_surroundings_left(&self, pane_id: &PaneId, increase_by: f64) -> bool { - let panes = self.panes.borrow(); if let Some(panes_to_the_left) = self.pane_ids_directly_left_of(pane_id) { - panes_to_the_left.iter().all(|id| { - let p = panes.get(id).unwrap(); - if let Some(cols) = p.position_and_size().cols.as_percent() { - let current_fixed_cols = p.position_and_size().cols.as_usize(); - let will_reduce_by = - ((self.display_area.cols as f64 / 100.0) * increase_by) as usize; - cols - increase_by >= RESIZE_PERCENT - && current_fixed_cols.saturating_sub(will_reduce_by) >= p.min_width() - } else { - false - } - }) + panes_to_the_left + .iter() + .all(|id| self.can_reduce_pane_width(id, increase_by)) } else { false } } fn can_increase_pane_and_surroundings_down(&self, pane_id: &PaneId, increase_by: f64) -> bool { - let panes = self.panes.borrow(); if let Some(panes_below) = self.pane_ids_directly_below(pane_id) { - panes_below.iter().all(|id| { - let p = panes.get(id).unwrap(); - if let Some(rows) = p.position_and_size().rows.as_percent() { - let current_fixed_rows = p.position_and_size().rows.as_usize(); - let will_reduce_by = - ((self.display_area.rows as f64 / 100.0) * increase_by) as usize; - rows - increase_by >= RESIZE_PERCENT - && current_fixed_rows.saturating_sub(will_reduce_by) >= p.min_height() - } else { - false - } - }) + panes_below + .iter() + .all(|id| self.can_reduce_pane_height(id, increase_by)) } else { false } } - fn can_increase_pane_and_surroundings_up(&self, pane_id: &PaneId, increase_by: f64) -> bool { - let panes = self.panes.borrow(); if let Some(panes_above) = self.pane_ids_directly_above(pane_id) { - panes_above.iter().all(|id| { - let p = panes.get(id).unwrap(); - if let Some(rows) = p.position_and_size().rows.as_percent() { - let current_fixed_rows = p.position_and_size().rows.as_usize(); - let will_reduce_by = - ((self.display_area.rows as f64 / 100.0) * increase_by) as usize; - rows - increase_by >= RESIZE_PERCENT - && current_fixed_rows.saturating_sub(will_reduce_by) >= p.min_height() - } else { - false - } - }) + panes_above + .iter() + .all(|id| self.can_reduce_pane_height(id, increase_by)) } else { false } } - fn can_reduce_pane_and_surroundings_right(&self, pane_id: &PaneId, reduce_by: f64) -> bool { + fn can_reduce_pane_width(&self, pane_id: &PaneId, reduce_by: f64) -> bool { let panes = self.panes.borrow(); let pane = panes.get(pane_id).unwrap(); - if let Some(cols) = pane.position_and_size().cols.as_percent() { - let current_fixed_cols = pane.position_and_size().cols.as_usize(); - let will_reduce_by = ((self.display_area.cols as f64 / 100.0) * reduce_by) as usize; - let ids_left = self.pane_ids_directly_left_of(pane_id); - let flexible_left = self.ids_are_flexible(Direction::Horizontal, ids_left); + let current_fixed_cols = pane.position_and_size().cols.as_usize(); + let will_reduce_by = ((self.display_area.cols as f64 / 100.0) * reduce_by) as usize; + if current_fixed_cols.saturating_sub(will_reduce_by) < pane.min_width() { + false + } else if let Some(cols) = pane.position_and_size().cols.as_percent() { cols - reduce_by >= RESIZE_PERCENT - && flexible_left - && current_fixed_cols.saturating_sub(will_reduce_by) >= pane.min_width() } else { false } } - fn can_reduce_pane_and_surroundings_left(&self, pane_id: &PaneId, reduce_by: f64) -> bool { + fn can_reduce_pane_height(&self, pane_id: &PaneId, reduce_by: f64) -> bool { let panes = self.panes.borrow(); let pane = panes.get(pane_id).unwrap(); - if let Some(cols) = pane.position_and_size().cols.as_percent() { - let current_fixed_cols = pane.position_and_size().cols.as_usize(); - let will_reduce_by = ((self.display_area.cols as f64 / 100.0) * reduce_by) as usize; - let ids_right = self.pane_ids_directly_right_of(pane_id); - let flexible_right = self.ids_are_flexible(Direction::Horizontal, ids_right); - cols - reduce_by >= RESIZE_PERCENT - && flexible_right - && current_fixed_cols.saturating_sub(will_reduce_by) >= pane.min_width() + let current_fixed_rows = pane.position_and_size().rows.as_usize(); + let will_reduce_by = ((self.display_area.rows as f64 / 100.0) * reduce_by) as usize; + if current_fixed_rows.saturating_sub(will_reduce_by) < pane.min_height() { + false + } else if let Some(rows) = pane.position_and_size().rows.as_percent() { + rows - reduce_by >= RESIZE_PERCENT + } else { + false + } + } + fn can_reduce_pane_and_surroundings_right(&self, pane_id: &PaneId, reduce_by: f64) -> bool { + let ids_left = self.pane_ids_directly_left_of(pane_id); + let flexible_left = self.ids_are_flexible(Direction::Horizontal, ids_left); + if flexible_left { + self.can_reduce_pane_width(pane_id, reduce_by) + } else { + false + } + } + fn can_reduce_pane_and_surroundings_left(&self, pane_id: &PaneId, reduce_by: f64) -> bool { + let ids_right = self.pane_ids_directly_right_of(pane_id); + let flexible_right = self.ids_are_flexible(Direction::Horizontal, ids_right); + if flexible_right { + self.can_reduce_pane_width(pane_id, reduce_by) } else { false } } fn can_reduce_pane_and_surroundings_down(&self, pane_id: &PaneId, reduce_by: f64) -> bool { - let panes = self.panes.borrow(); - let pane = panes.get(pane_id).unwrap(); - if let Some(rows) = pane.position_and_size().rows.as_percent() { - let current_fixed_rows = pane.position_and_size().rows.as_usize(); - let will_reduce_by = ((self.display_area.rows as f64 / 100.0) * reduce_by) as usize; - let ids_above = self.pane_ids_directly_above(pane_id); - let flexible_above = self.ids_are_flexible(Direction::Vertical, ids_above); - rows - reduce_by >= RESIZE_PERCENT - && flexible_above - && current_fixed_rows.saturating_sub(will_reduce_by) >= pane.min_height() + let ids_above = self.pane_ids_directly_above(pane_id); + let flexible_above = self.ids_are_flexible(Direction::Vertical, ids_above); + if flexible_above { + self.can_reduce_pane_height(pane_id, reduce_by) } else { false } } fn can_reduce_pane_and_surroundings_up(&self, pane_id: &PaneId, reduce_by: f64) -> bool { - let panes = self.panes.borrow(); - let pane = panes.get(pane_id).unwrap(); - if let Some(rows) = pane.position_and_size().rows.as_percent() { - let current_fixed_rows = pane.position_and_size().rows.as_usize(); - let will_reduce_by = ((self.display_area.rows as f64 / 100.0) * reduce_by) as usize; - let ids_below = self.pane_ids_directly_below(pane_id); - let flexible_below = self.ids_are_flexible(Direction::Vertical, ids_below); - rows - reduce_by >= RESIZE_PERCENT - && flexible_below - && current_fixed_rows.saturating_sub(will_reduce_by) >= pane.min_height() + let ids_below = self.pane_ids_directly_below(pane_id); + let flexible_below = self.ids_are_flexible(Direction::Vertical, ids_below); + if flexible_below { + self.can_reduce_pane_height(pane_id, reduce_by) } else { false } |