Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add mouse scrolling in file preview #527

Merged
merged 1 commit into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 26 additions & 15 deletions src/commands/preview_cursor_move.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use crate::error::AppResult;
use crate::preview::preview_file::PreviewFileState;

fn preview_cursor_move(context: &mut AppContext, new_index: usize) -> AppResult {
let file_path: Option<PathBuf> = {
let curr_tab = context.tab_context_ref().curr_tab_ref();
let curr_list = curr_tab.curr_list_ref();
let curr_entry = curr_list.and_then(|c| c.curr_entry_ref());
curr_entry.map(|e| e.file_path().to_path_buf())
};
let file_path: Option<PathBuf> = context
.tab_context_ref()
.curr_tab_ref()
.curr_list_ref()
.and_then(|c| c.curr_entry_ref())
.map(|e| e.file_path().to_path_buf());

let preview_context = context.preview_context_mut();
if let Some(file_path) = file_path {
Expand All @@ -25,10 +25,12 @@ fn preview_cursor_move(context: &mut AppContext, new_index: usize) -> AppResult

pub fn preview_up(context: &mut AppContext, u: usize) -> AppResult {
let new_index = {
let curr_tab = context.tab_context_ref().curr_tab_ref();
let curr_list = curr_tab.curr_list_ref();
let curr_entry = curr_list.and_then(|c| c.curr_entry_ref());
let file_path = curr_entry.map(|e| e.file_path());
let file_path = context
.tab_context_ref()
.curr_tab_ref()
.curr_list_ref()
.and_then(|c| c.curr_entry_ref())
.map(|e| e.file_path());

let preview_context = context.preview_context_ref();
if let Some(file_path) = file_path {
Expand All @@ -55,17 +57,26 @@ pub fn preview_up(context: &mut AppContext, u: usize) -> AppResult {

pub fn preview_down(context: &mut AppContext, u: usize) -> AppResult {
let new_index = {
let curr_tab = context.tab_context_ref().curr_tab_ref();
let curr_list = curr_tab.curr_list_ref();
let curr_entry = curr_list.and_then(|c| c.curr_entry_ref());
let file_path = curr_entry.map(|e| e.file_path());
let file_path = context
.tab_context_ref()
.curr_tab_ref()
.curr_list_ref()
.and_then(|c| c.curr_entry_ref())
.map(|e| e.file_path());

let preview_context = context.preview_context_ref();
if let Some(file_path) = file_path {
// TODO: scroll in child list
if let Some(PreviewFileState::Success(data)) =
preview_context.previews_ref().get(file_path)
{
Some(data.index + u)
if (data.index as isize)
< (data.output.split('\n').count() as isize - u as isize - 3)
{
Some(data.index + u)
} else {
None
}
} else {
None
}
Expand Down
10 changes: 8 additions & 2 deletions src/event/process_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,10 @@ pub fn process_mouse(
context.message_queue_mut().push_error(e.to_string());
}
} else {
// TODO: scroll in child list
let command = Command::PreviewCursorMoveUp { offset: 1 };
if let Err(e) = command.execute(context, backend, keymap_t) {
context.message_queue_mut().push_error(e.to_string());
}
}
}
MouseEvent::Press(MouseButton::WheelDown, x, _) => {
Expand All @@ -288,7 +291,10 @@ pub fn process_mouse(
context.message_queue_mut().push_error(e.to_string());
}
} else {
// TODO: scroll in child list
let command = Command::PreviewCursorMoveDown { offset: 1 };
if let Err(e) = command.execute(context, backend, keymap_t) {
context.message_queue_mut().push_error(e.to_string());
}
}
}
MouseEvent::Press(button @ MouseButton::Left, x, y)
Expand Down
Loading