refactor buffer resize to single function

pull/8546/head
Termina94 2 years ago
parent 64311f77df
commit d8fbff3856

@ -6,7 +6,7 @@ use crate::{
input::KeyEvent, input::KeyEvent,
register::Registers, register::Registers,
theme::{self, Theme}, theme::{self, Theme},
tree::{self, Tree}, tree::{self, Dimension, Resize, Tree},
view::ViewPosition, view::ViewPosition,
Align, Document, DocumentId, View, ViewId, Align, Document, DocumentId, View, ViewId,
}; };
@ -1646,19 +1646,19 @@ impl Editor {
} }
pub fn grow_buffer_width(&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) { 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) { 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) { 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) { pub fn buffer_focus_mode(&mut self) {

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

Loading…
Cancel
Save