use crate::tui::app::{App, ClickTarget};
use crate::tui::terminal::{WHEEL_LINES, hit_test, rect_contains, resolve_click};
use crate::tui::views::diff::state::{BodyMode, DiffFocus};
use crossterm::event::{KeyCode, KeyModifiers, MouseButton, MouseEvent, MouseEventKind};
const HSCROLL_COLS: i32 = 8;
pub async fn handle_mouse(app: &mut App, me: MouseEvent) {
let files_rect = app.diff().files_rect.get();
let body_rect = app.diff().body_rect.get();
let over_body = body_rect.is_some_and(|r| rect_contains(r, me.column, me.row));
let over_files = files_rect.is_some_and(|r| rect_contains(r, me.column, me.row));
let shift = me.modifiers.contains(KeyModifiers::SHIFT);
match me.kind {
MouseEventKind::Down(MouseButton::Left) if over_files => {
let hit = {
let rects = app.diff().file_row_rects.borrow();
hit_test(&rects, me.column, me.row)
};
if let Some(idx) = hit {
app.diff_mut().select_at(idx);
app.request_diff_for_selected();
let _ = resolve_click(app, ClickTarget::DiffFile(idx));
}
}
MouseEventKind::ScrollRight if over_body => app.diff_mut().body_h_scroll_by(HSCROLL_COLS),
MouseEventKind::ScrollLeft if over_body => app.diff_mut().body_h_scroll_by(-HSCROLL_COLS),
MouseEventKind::ScrollDown if over_body && shift => {
app.diff_mut().body_h_scroll_by(HSCROLL_COLS);
}
MouseEventKind::ScrollUp if over_body && shift => {
app.diff_mut().body_h_scroll_by(-HSCROLL_COLS)
}
MouseEventKind::ScrollDown if over_body => app.diff_mut().body_scroll_by(WHEEL_LINES),
MouseEventKind::ScrollUp if over_body => app.diff_mut().body_scroll_by(-WHEEL_LINES),
MouseEventKind::ScrollDown if over_files => {
for _ in 0..WHEEL_LINES {
app.diff_mut().select_next();
}
app.request_diff_for_selected();
}
MouseEventKind::ScrollUp if over_files => {
for _ in 0..WHEEL_LINES {
app.diff_mut().select_prev();
}
app.request_diff_for_selected();
}
_ => {}
}
}
pub async fn handle_key(app: &mut App, code: KeyCode, modifiers: KeyModifiers) {
if modifiers.contains(KeyModifiers::CONTROL) && matches!(code, KeyCode::Char('c')) {
app.quit();
return;
}
match code {
KeyCode::Char('q') => {
app.quit();
return;
}
KeyCode::Char('r') => {
app.diff_mut().mark_stale();
app.request_diff_reload();
return;
}
KeyCode::Tab | KeyCode::BackTab => {
app.diff_mut().toggle_focus();
return;
}
KeyCode::Char('w') => {
app.diff_mut().toggle_wrap();
return;
}
KeyCode::Char('v') => {
app.diff_mut().toggle_body_mode();
if app.diff().body_mode() == BodyMode::Read {
app.request_read_for_selected();
}
return;
}
KeyCode::Char('L') => {
if app.diff().lazygit_available {
app.request_lazygit();
} else {
app.flash("install lazygit to enable the L keybind");
}
return;
}
KeyCode::Char(']') => {
if app.diff().body_mode() == BodyMode::Read {
return;
}
app.diff_mut().set_focus(DiffFocus::Body);
app.diff_mut().jump_hunk_next();
return;
}
KeyCode::Char('[') => {
if app.diff().body_mode() == BodyMode::Read {
return;
}
app.diff_mut().set_focus(DiffFocus::Body);
app.diff_mut().jump_hunk_prev();
return;
}
_ => {}
}
match app.diff().focus() {
DiffFocus::Files => match code {
KeyCode::Down | KeyCode::Char('j') => {
app.diff_mut().select_next();
app.request_diff_for_selected();
}
KeyCode::Up | KeyCode::Char('k') => {
app.diff_mut().select_prev();
app.request_diff_for_selected();
}
KeyCode::Enter | KeyCode::Right | KeyCode::Char('l') => {
app.diff_mut().set_focus(DiffFocus::Body);
}
KeyCode::Char('e') => {
if let Some(file) = app.diff().selected_file() {
let path = std::path::PathBuf::from(&file.path);
app.request_open_in_editor(path);
}
}
_ => {}
},
DiffFocus::Body => match code {
KeyCode::Down | KeyCode::Char('j') => app.diff_mut().body_scroll_by(1),
KeyCode::Up | KeyCode::Char('k') => app.diff_mut().body_scroll_by(-1),
KeyCode::PageDown => {
let half = (app.diff().body_height.get() / 2).max(1) as i32;
app.diff_mut().body_scroll_by(half);
}
KeyCode::PageUp => {
let half = (app.diff().body_height.get() / 2).max(1) as i32;
app.diff_mut().body_scroll_by(-half);
}
KeyCode::Home => app.diff_mut().body_scroll_to_top(),
KeyCode::End | KeyCode::Char('G') => app.diff_mut().body_scroll_to_bottom(),
KeyCode::Char('g') if app.diff_mut().consume_g_chord() => {
app.diff_mut().body_scroll_to_top();
}
KeyCode::Left | KeyCode::Char('h') | KeyCode::Esc => {
app.diff_mut().set_focus(DiffFocus::Files);
}
_ => {}
},
}
}