summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeff Zhao <jeff.no.zhao@gmail.com>2021-06-20 20:42:19 -0400
committerJeff Zhao <jeff.no.zhao@gmail.com>2021-06-20 20:42:19 -0400
commit2cbb09549f487e77c8144accce4c21aec3bd315d (patch)
treec84e52e2674e1c0c5408facaacc1e0101972bfe9
parent2e4a838128a39e377290e4ccae6ee32af1020b37 (diff)
parent68a6bf78cfe6d0a46f15f2e80e917ffb69b8c963 (diff)
Merge branch 'main' of github.com:kamiyaa/joshuto
-rw-r--r--src/fs/dirlist.rs11
-rw-r--r--src/ui/widgets/tui_dirlist.rs2
-rw-r--r--src/ui/widgets/tui_dirlist_detailed.rs2
-rw-r--r--src/util/input.rs52
4 files changed, 42 insertions, 25 deletions
diff --git a/src/fs/dirlist.rs b/src/fs/dirlist.rs
index b80e05b..e8dc90b 100644
--- a/src/fs/dirlist.rs
+++ b/src/fs/dirlist.rs
@@ -143,6 +143,17 @@ impl JoshutoDirList {
self.get_curr_mut_(self.index?)
}
+ /// For a given number of entries, visible in a UI, this method returns the index of the entry
+ /// with which the UI should start to list the entries.
+ ///
+ /// This method assures that the cursor is always in the viewport of the UI.
+ pub fn first_index_for_viewport(&self, viewport_height: usize) -> usize {
+ match self.index {
+ Some(index) => index / viewport_height as usize * viewport_height as usize,
+ None => 0,
+ }
+ }
+
fn get_curr_mut_(&mut self, index: usize) -> Option<&mut JoshutoDirEntry> {
if index < self.contents.len() {
Some(&mut self.contents[index])
diff --git a/src/ui/widgets/tui_dirlist.rs b/src/ui/widgets/tui_dirlist.rs
index 7d9f888..f66c9f0 100644
--- a/src/ui/widgets/tui_dirlist.rs
+++ b/src/ui/widgets/tui_dirlist.rs
@@ -36,7 +36,7 @@ impl<'a> Widget for TuiDirList<'a> {
}
let curr_index = self.dirlist.index.unwrap();
- let skip_dist = curr_index / area.height as usize * area.height as usize;
+ let skip_dist = self.dirlist.first_index_for_viewport(area.height as usize);
let drawing_width = area.width as usize;
diff --git a/src/ui/widgets/tui_dirlist_detailed.rs b/src/ui/widgets/tui_dirlist_detailed.rs
index 796ead4..2c0337d 100644
--- a/src/ui/widgets/tui_dirlist_detailed.rs
+++ b/src/ui/widgets/tui_dirlist_detailed.rs
@@ -41,7 +41,7 @@ impl<'a> Widget for TuiDirListDetailed<'a> {
};
let drawing_width = area.width as usize;
- let skip_dist = curr_index / area.height as usize * area.height as usize;
+ let skip_dist = self.dirlist.first_index_for_viewport(area.height as usize);
// draw every entry
self.dirlist
diff --git a/src/util/input.rs b/src/util/input.rs
index 1d4b469..7a6d9ad 100644
--- a/src/util/input.rs
+++ b/src/util/input.rs
@@ -16,7 +16,13 @@ pub fn process_mouse(event: MouseEvent, context: &mut AppContext, backend: &mut
let constraints: &[Constraint; 3] = &context.config_ref().display_options_ref().default_layout;
let layout_rect = Layout::default()
.direction(Direction::Horizontal)
- .vertical_margin(1)
+ .vertical_margin(
+ if context.config_ref().display_options_ref().show_borders() {
+ 2
+ } else {
+ 1
+ },
+ )
.constraints(constraints.as_ref())
.split(f_size);
@@ -54,28 +60,28 @@ pub fn process_mouse(event: MouseEvent, context: &mut AppContext, backend: &mut
MouseEvent::Press(MouseButton::Left, x, y)
if y > layout_rect[1].y && y <= layout_rect[1].y + layout_rect[1].height =>
{
- if x < layout_rect[1].x {
- if let Some(dirlist) = context.tab_context_ref().curr_tab_ref().curr_list_ref() {
- if let Some(curr_index) = dirlist.index {
- let skip_dist = curr_index / layout_rect[1].height as usize
- * layout_rect[1].height as usize;
-
- let new_index = skip_dist + (y - layout_rect[1].y - 1) as usize;
- if let Err(e) = parent_cursor_move::parent_cursor_move(new_index, context) {
- context.push_msg(e.to_string());
- }
- }
- }
- } else if x < layout_rect[2].x {
- if let Some(dirlist) = context.tab_context_ref().curr_tab_ref().curr_list_ref() {
- if let Some(curr_index) = dirlist.index {
- let skip_dist = curr_index / layout_rect[1].height as usize
- * layout_rect[1].height as usize;
-
- let new_index = skip_dist + (y - layout_rect[1].y - 1) as usize;
- if let Err(e) = cursor_move::cursor_move(new_index, context) {
- context.push_msg(e.to_string());
- }
+ if x < layout_rect[2].x {
+ let (dirlist, is_parent) = if x < layout_rect[1].x {
+ (
+ context.tab_context_ref().curr_tab_ref().parent_list_ref(),
+ true,
+ )
+ } else {
+ (
+ context.tab_context_ref().curr_tab_ref().curr_list_ref(),
+ false,
+ )
+ };
+ if let Some(dirlist) = dirlist {
+ let skip_dist =
+ dirlist.first_index_for_viewport(layout_rect[1].height as usize);
+ let new_index = skip_dist + (y - layout_rect[1].y - 1) as usize;
+ if let Err(e) = if is_parent {
+ parent_cursor_move::parent_cursor_move(new_index, context)
+ } else {
+ cursor_move::cursor_move(new_index, context)
+ } {
+ context.push_msg(e.to_string());
}
}
} else {