use super::*;
pub(super) fn viewer_key(app_state: &mut AppState, key: KeyEvent) {
match key.code {
KeyCode::Esc => app_state.close_viewer(),
KeyCode::F(3) => app_state.find(!key.modifiers.contains(KeyModifiers::SHIFT), false),
KeyCode::Char('x') | KeyCode::Char('X') => app_state.viewer_next_mode(),
KeyCode::Char('f') | KeyCode::Char('F') => app_state.viewer_toggle_fill(),
KeyCode::Char('w') | KeyCode::Char('W') => app_state.viewer_toggle_wrap(),
KeyCode::Char('+') | KeyCode::Char('=') => app_state.viewer_zoom(true),
KeyCode::Char('-') | KeyCode::Char('_') => app_state.viewer_zoom(false),
KeyCode::Down => app_state.viewer_scroll_down(),
KeyCode::Up => app_state.viewer_scroll_up(),
KeyCode::Left => app_state.viewer_scroll_left(),
KeyCode::Right => app_state.viewer_scroll_right(),
KeyCode::PageDown if app_state.viewer_shows_image() => app_state.viewer_step_image(true),
KeyCode::PageUp if app_state.viewer_shows_image() => app_state.viewer_step_image(false),
KeyCode::PageDown => app_state.viewer_page_down(),
KeyCode::PageUp => app_state.viewer_page_up(),
KeyCode::Home => app_state.viewer_home(),
KeyCode::End => app_state.viewer_end(),
_ => {}
}
}
pub(super) fn diff_key(app_state: &mut AppState, key: KeyEvent) {
let page = app_state.diff_viewport_height.max(1) as isize;
match key.code {
KeyCode::Esc => app_state.close_diff(),
KeyCode::F(3) => {
let forward = !key.modifiers.contains(KeyModifiers::SHIFT);
if let Some(state) = app_state.diff_mut() {
state.step(forward);
}
}
KeyCode::Char('n') | KeyCode::Char('N') => {
if let Some(state) = app_state.diff_mut() {
state.step(true);
}
}
KeyCode::Char('p') | KeyCode::Char('P') => {
if let Some(state) = app_state.diff_mut() {
state.step(false);
}
}
KeyCode::Down => app_state.diff_scroll(1, 0),
KeyCode::Up => app_state.diff_scroll(-1, 0),
KeyCode::PageDown => app_state.diff_scroll(page, 0),
KeyCode::PageUp => app_state.diff_scroll(-page, 0),
KeyCode::Home => app_state.diff_scroll(isize::MIN, isize::MIN),
KeyCode::End => app_state.diff_scroll(isize::MAX, 0),
KeyCode::Left => app_state.diff_scroll(0, -4),
KeyCode::Right => app_state.diff_scroll(0, 4),
_ => {}
}
}
pub(super) fn editor_key(app_state: &mut AppState, key: KeyEvent) {
if let Some(state) = app_state.editor_mut() {
state.auto_scroll = true;
}
let extend = key.modifiers.contains(KeyModifiers::SHIFT);
match key.code {
KeyCode::Esc => {
if app_state.editor_is_modified() {
app_state.dialog = Some(Dialog::SaveChanges { then_quit: false });
} else {
app_state.close_editor();
}
}
KeyCode::F(2) => {
save_then(app_state, AfterSave::Stay);
}
KeyCode::F(3) => app_state.find(!extend, false),
KeyCode::F(4) => app_state.open_replace(),
KeyCode::Up => {
app_state.editor_prepare_move(extend);
app_state.editor_cursor_up();
}
KeyCode::Down => {
app_state.editor_prepare_move(extend);
app_state.editor_cursor_down();
}
KeyCode::Left => {
app_state.editor_prepare_move(extend);
app_state.editor_cursor_left();
}
KeyCode::Right => {
app_state.editor_prepare_move(extend);
app_state.editor_cursor_right();
}
KeyCode::Home => {
app_state.editor_prepare_move(extend);
app_state.editor_home();
}
KeyCode::End => {
app_state.editor_prepare_move(extend);
app_state.editor_end();
}
KeyCode::PageUp => {
app_state.editor_prepare_move(extend);
app_state.editor_page_up();
}
KeyCode::PageDown => {
app_state.editor_prepare_move(extend);
app_state.editor_page_down();
}
KeyCode::Enter => app_state.editor_enter(),
KeyCode::Backspace => app_state.editor_backspace(),
KeyCode::Insert if key.modifiers.contains(KeyModifiers::CONTROL) => app_state.editor_copy(),
KeyCode::Insert if extend => app_state.editor_paste(),
KeyCode::Delete if extend => app_state.editor_cut(),
KeyCode::Delete => app_state.editor_delete(),
KeyCode::Tab => app_state.editor_insert_char('\t'),
KeyCode::Char(c) => app_state.editor_insert_char(c),
_ => {}
}
}