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,41 +697,44 @@ impl Tree {
None None
} }
pub fn grow_buffer_width(&mut self) { 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) { 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 { if bounds.width < 20 {
bounds.width += 1; 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(); self.recalculate();
} }
} }
} Dimension::Height => {
pub fn grow_buffer_height(&mut self) {
if let Some(bounds) = self.get_active_node_bounds_mut(Layout::Horizontal) { 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 { if bounds.height < 20 {
bounds.height += 1; 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(); self.recalculate();
} }
} }
} }
}
pub fn buffer_focus_mode(&mut self) { pub fn buffer_focus_mode(&mut self) {
if let Some(bounds) = self.get_active_node_bounds_mut(Layout::Horizontal) { if let Some(bounds) = self.get_active_node_bounds_mut(Layout::Horizontal) {

Loading…
Cancel
Save