diff options
author | qkzk <qu3nt1n@gmail.com> | 2022-12-11 16:40:13 +0100 |
---|---|---|
committer | qkzk <qu3nt1n@gmail.com> | 2022-12-11 16:40:13 +0100 |
commit | 6f74985065f6d4a1b01213b693fe7e7eb23c9046 (patch) | |
tree | 84f38261dd36dd6d6f7a8efb027263d53b7150a7 | |
parent | 923170af44c18feab1a16aae0d6e6139de7c5249 (diff) |
remove more actions from actioner. Need a rename
-rw-r--r-- | src/actioner.rs | 26 | ||||
-rw-r--r-- | src/content_window.rs | 8 | ||||
-rw-r--r-- | src/event_exec.rs | 66 |
3 files changed, 49 insertions, 51 deletions
diff --git a/src/actioner.rs b/src/actioner.rs index 12d7184..82dbd0c 100644 --- a/src/actioner.rs +++ b/src/actioner.rs @@ -26,12 +26,10 @@ impl Actioner { Event::Key(Key::WheelUp(_, _, _)) => EventExec::event_move_up(status), Event::Key(Key::WheelDown(_, _, _)) => EventExec::event_move_down(status), Event::Key(Key::SingleClick(MouseButton::Left, row, _)) => { - Self::left_click(status, row); - Ok(()) + EventExec::event_select_row(status, row) } Event::Key(Key::SingleClick(MouseButton::Right, row, _)) => { - Self::right_click(status, row); - Ok(()) + EventExec::event_right_click(status, row) } Event::User(_) => EventExec::refresh_selected_view(status), Event::Resize { width, height } => EventExec::resize(status, width, height), @@ -48,24 +46,10 @@ impl Actioner { } } - /// Select this file - fn left_click(status: &mut Status, row: u16) { - if let Mode::Normal = status.selected_non_mut().mode { - EventExec::event_select_row(status.selected(), row) - } - } - - /// Open a directory or a file - fn right_click(status: &mut Status, row: u16) { - if let Mode::Normal = status.selected_non_mut().mode { - let _ = EventExec::event_right_click(status.selected(), row); - } - } - /// Match read key to a relevent event, depending on keybindings. /// Keybindings are read from `Config`. - fn char(&self, status: &mut Status, k: Key) -> FmResult<()> { - match k { + fn char(&self, status: &mut Status, key_char: Key) -> FmResult<()> { + match key_char { Key::Char(c) => match status.selected_non_mut().mode { Mode::Newfile | Mode::Newdir | Mode::Chmod | Mode::Rename | Mode::Filter => { EventExec::event_text_insertion(status.selected(), c); @@ -79,7 +63,7 @@ impl Actioner { Mode::Goto | Mode::Exec | Mode::Search => { EventExec::event_text_insert_and_complete(status.selected(), c) } - Mode::Normal => match self.binds.get(&k) { + Mode::Normal => match self.binds.get(&key_char) { Some(event_char) => event_char.matcher(status), None => Ok(()), }, diff --git a/src/content_window.rs b/src/content_window.rs index af63ef0..0cea172 100644 --- a/src/content_window.rs +++ b/src/content_window.rs @@ -1,5 +1,6 @@ use std::cmp::{max, min}; +pub const RESERVED_ROWS: usize = 3; /// Holds the information about the displayed window of lines. /// When there's too much lines to display in one screen, we can scroll /// and this struct is responsible for that. @@ -23,9 +24,9 @@ impl ContentWindow { pub fn new(len: usize, height: usize) -> Self { ContentWindow { top: 0, - bottom: min(len, height - Self::RESERVED_ROWS), + bottom: min(len, height - RESERVED_ROWS), len, - height: height - Self::RESERVED_ROWS, + height: height - RESERVED_ROWS, } } @@ -34,11 +35,10 @@ impl ContentWindow { /// The space of the top element pub const WINDOW_MARGIN_TOP: usize = 2; /// How many rows are reserved at bottom - const RESERVED_ROWS: usize = 3; /// Set the height of file window. pub fn set_height(&mut self, height: usize) { - self.height = height - Self::RESERVED_ROWS; + self.height = height - RESERVED_ROWS; } /// Move the window one line up if possible. diff --git a/src/event_exec.rs b/src/event_exec.rs index e67d60b..2e31d62 100644 --- a/src/event_exec.rs +++ b/src/event_exec.rs @@ -6,7 +6,7 @@ use std::path::PathBuf; use crate::bulkrename::Bulkrename; use crate::compress::decompress; -use crate::content_window::ContentWindow; +use crate::content_window::{ContentWindow, RESERVED_ROWS}; use crate::copy_move::CopyMove; use crate::fileinfo::{FileKind, PathContent, SortBy}; use crate::filter::FilterKind; @@ -349,10 +349,14 @@ impl EventExec { tab.window.scroll_to(down_index); } - pub fn event_select_row(tab: &mut Tab, row: u16) { - tab.line_index = (row - 2).into(); - tab.path_content.select_index(tab.line_index); - tab.window.scroll_to(tab.line_index) + pub fn event_select_row(status: &mut Status, row: u16) -> FmResult<()> { + if let Mode::Normal = status.selected_non_mut().mode { + let tab = status.selected(); + tab.line_index = Self::row_to_index(row); + tab.path_content.select_index(tab.line_index); + tab.window.scroll_to(tab.line_index); + } + Ok(()) } pub fn event_shortcut_next(tab: &mut Tab) { @@ -583,30 +587,36 @@ impl EventExec { tab.mode = Mode::Shortcut } - pub fn event_right_click(tab: &mut Tab, row: u16) -> FmResult<()> { - if tab.path_content.files.is_empty() || row as usize > tab.path_content.files.len() + 1 { - return Err(FmError::new( - ErrorVariant::CUSTOM("event right click".to_owned()), - "not found", - )); - } - tab.line_index = (row - 2).into(); - tab.path_content.select_index(tab.line_index); - tab.window.scroll_to(tab.line_index); - if let FileKind::Directory = tab - .path_content - .selected_file() - .ok_or_else(|| { - FmError::new( + pub fn event_right_click(status: &mut Status, row: u16) -> FmResult<()> { + if let Mode::Normal = status.selected_non_mut().mode { + let tab = status.selected(); + if tab.path_content.files.is_empty() || row as usize > tab.path_content.files.len() + 1 + { + return Err(FmError::new( ErrorVariant::CUSTOM("event right click".to_owned()), "not found", - ) - })? - .file_kind - { - Self::exec_file(tab) + )); + } + tab.line_index = Self::row_to_index(row); + tab.path_content.select_index(tab.line_index); + tab.window.scroll_to(tab.line_index); + if let FileKind::Directory = tab + .path_content + .selected_file() + .ok_or_else(|| { + FmError::new( + ErrorVariant::CUSTOM("event right click".to_owned()), + "not found", + ) + })? + .file_kind + { + Self::exec_file(tab) + } else { + Self::event_open_file(tab) + } } else { - Self::event_open_file(tab) + Ok(()) } } @@ -1052,4 +1062,8 @@ impl EventExec { status.display_full = !status.display_full; Ok(()) } + + fn row_to_index(row: u16) -> usize { + row as usize - RESERVED_ROWS + } } |