refactor buffer resize to single function

This commit is contained in:
Termina94 2022-07-12 21:56:52 +01:00
parent 64311f77df
commit d8fbff3856
2 changed files with 49 additions and 36 deletions

View File

@ -6,7 +6,7 @@
input::KeyEvent,
register::Registers,
theme::{self, Theme},
tree::{self, Tree},
tree::{self, Dimension, Resize, Tree},
view::ViewPosition,
Align, Document, DocumentId, View, ViewId,
};
@ -1646,19 +1646,19 @@ pub fn transpose_view(&mut self) {
}
pub fn grow_buffer_width(&mut self) {
self.tree.grow_buffer_width();
self.tree.resize_buffer(Resize::Grow, Dimension::Width);
}
pub fn shrink_buffer_width(&mut self) {
self.tree.shrink_buffer_width();
self.tree.resize_buffer(Resize::Shrink, Dimension::Width);
}
pub fn grow_buffer_height(&mut self) {
self.tree.grow_buffer_height();
self.tree.resize_buffer(Resize::Grow, Dimension::Height);
}
pub fn shrink_buffer_height(&mut self) {
self.tree.shrink_buffer_height();
self.tree.resize_buffer(Resize::Shrink, Dimension::Height);
}
pub fn buffer_focus_mode(&mut self) {

View File

@ -29,6 +29,16 @@ pub enum Content {
Container(Box<Container>),
}
pub enum Resize {
Shrink,
Grow,
}
pub enum Dimension {
Width,
Height,
}
impl Node {
pub fn container(layout: Layout) -> Self {
Self {
@ -687,38 +697,41 @@ fn get_active_node_bounds_mut(
None
}
pub fn grow_buffer_width(&mut self) {
if let Some(bounds) = self.get_active_node_bounds_mut(Layout::Vertical) {
if bounds.width < 20 {
bounds.width += 1;
self.recalculate();
pub fn resize_buffer(&mut self, resize_type: Resize, dimension: Dimension) {
match dimension {
Dimension::Width => {
if let Some(bounds) = self.get_active_node_bounds_mut(Layout::Vertical) {
match resize_type {
Resize::Shrink => {
if bounds.width > 1 {
bounds.width -= 1;
}
}
Resize::Grow => {
if bounds.width < 20 {
bounds.width += 1;
}
}
};
self.recalculate();
}
}
}
}
pub fn shrink_buffer_width(&mut self) {
if let Some(bounds) = self.get_active_node_bounds_mut(Layout::Vertical) {
if bounds.width > 1 {
bounds.width -= 1;
self.recalculate();
}
}
}
pub fn grow_buffer_height(&mut self) {
if let Some(bounds) = self.get_active_node_bounds_mut(Layout::Horizontal) {
if bounds.height < 20 {
bounds.height += 1;
self.recalculate();
}
}
}
pub fn shrink_buffer_height(&mut self) {
if let Some(bounds) = self.get_active_node_bounds_mut(Layout::Horizontal) {
if bounds.height > 1 {
bounds.height -= 1;
self.recalculate();
Dimension::Height => {
if let Some(bounds) = self.get_active_node_bounds_mut(Layout::Horizontal) {
match resize_type {
Resize::Shrink => {
if bounds.height > 1 {
bounds.height -= 1;
}
}
Resize::Grow => {
if bounds.height < 20 {
bounds.height += 1;
}
}
};
self.recalculate();
}
}
}
}