diff options
author | DLFW <daniel@llin.info> | 2022-10-17 18:41:35 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-17 12:41:35 -0400 |
commit | e59ed84f5ac3ba864b6c0d2bcdb6a014d2d46e73 (patch) | |
tree | 3ee552f983b349a52d8df958871bf1dece18fbb1 /src/event | |
parent | be452d4c9abf7891111138000fe7683f169d69a5 (diff) |
Add ranger-like mouse control, incl. opening files (#207)
This enhances the mouse control and makes it similar to the behavior of
ranger.
* Entries in the “child-panel” can also “be clicked” now.
* Left-clicking entries in the parent or child-panel moves the
containing dir to the middle panel.
* Right-clicking entries in the parent or middle panel opens the entry.
This feature does not work properly in `hsplit`-mode.
A short user documentation has been added to `misc.md`.
Diffstat (limited to 'src/event')
-rw-r--r-- | src/event/process_event.rs | 85 |
1 files changed, 68 insertions, 17 deletions
diff --git a/src/event/process_event.rs b/src/event/process_event.rs index 5b759d7..4f566ea 100644 --- a/src/event/process_event.rs +++ b/src/event/process_event.rs @@ -226,6 +226,27 @@ pub fn process_unsupported( } } +enum Panel { + Parent, + Current, + Preview, +} + +fn children_cursor_move(context: &mut AppContext, new_index: usize) { + let mut new_index = new_index; + let ui_context = context.ui_context_ref().clone(); + let display_options = context.config_ref().display_options_ref().clone(); + if let Some(children_list) = context.tab_context_mut().curr_tab_mut().child_list_mut() { + if !children_list.is_empty() { + let dir_len = children_list.len(); + if new_index >= dir_len { + new_index = dir_len - 1; + } + children_list.set_index(Some(new_index), &ui_context, &display_options); + } + } +} + pub fn process_mouse( event: MouseEvent, context: &mut AppContext, @@ -278,37 +299,67 @@ pub fn process_mouse( // TODO: scroll in child list } } - 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[2].x { - let (dirlist, is_parent) = if x < layout_rect[1].x { + MouseEvent::Press(button @ MouseButton::Left, x, y) + | MouseEvent::Press(button @ MouseButton::Right, x, y) => { + if y > layout_rect[1].y && y <= layout_rect[1].y + layout_rect[1].height { + let (dirlist, panel) = if x < layout_rect[1].x { ( - context.tab_context_ref().curr_tab_ref().parent_list_ref(), - true, + context.tab_context_mut().curr_tab_mut().parent_list_ref(), + Some(Panel::Parent), + ) + } else if x < layout_rect[2].x { + ( + context.tab_context_mut().curr_tab_mut().curr_list_ref(), + Some(Panel::Current), ) } else { ( - context.tab_context_ref().curr_tab_ref().curr_list_ref(), - false, + context.tab_context_mut().curr_tab_mut().child_list_ref(), + Some(Panel::Preview), ) }; if let Some(dirlist) = dirlist { let skip_dist = dirlist.first_index_for_viewport(); let new_index = skip_dist + (y - layout_rect[1].y - 1) as usize; - if is_parent { - if let Err(e) = parent_cursor_move::parent_cursor_move(context, new_index) { - context.message_queue_mut().push_error(e.to_string()); + match panel { + Some(Panel::Parent) => { + if let Err(e) = + parent_cursor_move::parent_cursor_move(context, new_index) + { + context.message_queue_mut().push_error(e.to_string()); + }; + if button == MouseButton::Left { + let command = Command::ChangeDirectory { + path: path::PathBuf::from(".."), + }; + if let Err(e) = command.execute(context, backend, keymap_t) { + context.message_queue_mut().push_error(e.to_string()); + } + }; + } + Some(Panel::Current) => { + cursor_move::cursor_move(context, new_index); + if button == MouseButton::Right { + let command = Command::OpenFile; + if let Err(e) = command.execute(context, backend, keymap_t) { + context.message_queue_mut().push_error(e.to_string()); + } + } } - } else { - cursor_move::cursor_move(context, new_index); + Some(Panel::Preview) => { + children_cursor_move(context, new_index); + if button == MouseButton::Left { + let command = Command::OpenFile; + if let Err(e) = command.execute(context, backend, keymap_t) { + context.message_queue_mut().push_error(e.to_string()); + } + } + } + _ => {} } } - } else { } } - MouseEvent::Press(MouseButton::Left, _, y) - if y > layout_rect[1].y && y <= layout_rect[1].y + layout_rect[1].height => {} _ => {} } context.flush_event(); |