fix(explore): increase size will cause panic

pull/9/head
wongjiahau 2 years ago
parent b38a941955
commit 56056e8556

@ -147,6 +147,7 @@ struct State {
focus: bool, focus: bool,
open: bool, open: bool,
current_root: PathBuf, current_root: PathBuf,
area_width: u16,
} }
impl State { impl State {
@ -155,6 +156,7 @@ impl State {
focus, focus,
current_root, current_root,
open: true, open: true,
area_width: 0,
} }
} }
} }
@ -296,6 +298,8 @@ impl Explorer {
"] Change root to current folder", "] Change root to current folder",
"^o Go to previous root", "^o Go to previous root",
"R Refresh", "R Refresh",
"+ Increase size",
"- Decrease size",
"q Close", "q Close",
] ]
.into_iter() .into_iter()
@ -522,6 +526,8 @@ impl Explorer {
} }
let width = area.width.min(self.column_width + 2); let width = area.width.min(self.column_width + 2);
self.state.area_width = area.width;
let side_area = match position { let side_area = match position {
ExplorerPositionEmbed::Left => Rect { width, ..area }, ExplorerPositionEmbed::Left => Rect { width, ..area },
ExplorerPositionEmbed::Right => Rect { ExplorerPositionEmbed::Right => Rect {
@ -545,7 +551,7 @@ impl Explorer {
} }
}; };
surface.set_stringn( surface.set_stringn(
list_area.x, list_area.x.saturating_sub(1),
list_area.y, list_area.y,
" Explorer: press ? for help", " Explorer: press ? for help",
list_area.width.into(), list_area.width.into(),
@ -807,6 +813,18 @@ impl Explorer {
pub fn column_width(&self) -> u16 { pub fn column_width(&self) -> u16 {
self.column_width self.column_width
} }
fn increase_size(&mut self) {
const EDITOR_MIN_WIDTH: u16 = 10;
self.column_width = std::cmp::min(
self.state.area_width.saturating_sub(EDITOR_MIN_WIDTH),
self.column_width.saturating_add(1),
)
}
fn decrease_size(&mut self) {
self.column_width = self.column_width.saturating_sub(1)
}
} }
impl Component for Explorer { impl Component for Explorer {
@ -870,8 +888,8 @@ impl Component for Explorer {
cx.editor.set_error(error.to_string()) cx.editor.set_error(error.to_string())
} }
} }
key!('-') => self.column_width = self.column_width.saturating_sub(1), key!('-') => self.decrease_size(),
key!('+') => self.column_width = self.column_width.saturating_add(1), key!('+') => self.increase_size(),
_ => { _ => {
self.tree self.tree
.handle_event(Event::Key(key_event), cx, &mut self.state); .handle_event(Event::Key(key_event), cx, &mut self.state);

Loading…
Cancel
Save