feat: Support alternative keymappings for all keys, featuring hjkl movements

This commit is contained in:
2025-03-17 22:02:15 -06:00
parent c633347ecc
commit 0048d71b74
77 changed files with 1247 additions and 304 deletions
+24 -23
View File
@@ -1,9 +1,9 @@
use radarr_handlers::RadarrHandler;
use sonarr_handlers::SonarrHandler;
use crate::app::key_binding::DEFAULT_KEYBINDINGS;
use crate::app::App;
use crate::event::Key;
use crate::matches_key;
use crate::models::{HorizontallyScrollableText, Route};
mod radarr_handlers;
@@ -22,40 +22,42 @@ pub trait KeyEventHandler<'a, 'b, T: Into<Route> + Copy> {
fn handle_key_event(&mut self) {
let key = self.get_key();
match key {
_ if key == DEFAULT_KEYBINDINGS.up.key => {
_ if matches_key!(up, key, self.ignore_alt_navigation()) => {
if self.is_ready() {
self.handle_scroll_up();
}
}
_ if key == DEFAULT_KEYBINDINGS.down.key => {
_ if matches_key!(down, key, self.ignore_alt_navigation()) => {
if self.is_ready() {
self.handle_scroll_down();
}
}
_ if key == DEFAULT_KEYBINDINGS.home.key => {
_ if matches_key!(home, key) => {
if self.is_ready() {
self.handle_home();
}
}
_ if key == DEFAULT_KEYBINDINGS.end.key => {
_ if matches_key!(end, key) => {
if self.is_ready() {
self.handle_end();
}
}
_ if key == DEFAULT_KEYBINDINGS.delete.key => {
_ if matches_key!(delete, key) => {
if self.is_ready() {
self.handle_delete();
}
}
_ if key == DEFAULT_KEYBINDINGS.left.key || key == DEFAULT_KEYBINDINGS.right.key => {
_ if matches_key!(left, key, self.ignore_alt_navigation())
|| matches_key!(right, key, self.ignore_alt_navigation()) =>
{
self.handle_left_right_action()
}
_ if key == DEFAULT_KEYBINDINGS.submit.key => {
_ if matches_key!(submit, key) => {
if self.is_ready() {
self.handle_submit();
}
}
_ if key == DEFAULT_KEYBINDINGS.esc.key => self.handle_esc(),
_ if matches_key!(esc, key) => self.handle_esc(),
_ => {
if self.is_ready() {
self.handle_char_key_event();
@@ -71,6 +73,7 @@ pub trait KeyEventHandler<'a, 'b, T: Into<Route> + Copy> {
fn accepts(active_block: T) -> bool;
fn new(key: Key, app: &'a mut App<'b>, active_block: T, context: Option<T>) -> Self;
fn get_key(&self) -> Key;
fn ignore_alt_navigation(&self) -> bool;
fn is_ready(&self) -> bool;
fn handle_scroll_up(&mut self);
fn handle_scroll_down(&mut self);
@@ -84,12 +87,12 @@ pub trait KeyEventHandler<'a, 'b, T: Into<Route> + Copy> {
}
pub fn handle_events(key: Key, app: &mut App<'_>) {
if key == DEFAULT_KEYBINDINGS.next_servarr.key {
if matches_key!(next_servarr, key) {
app.reset();
app.server_tabs.next();
app.pop_and_push_navigation_stack(app.server_tabs.get_active_route());
app.cancellation_token.cancel();
} else if key == DEFAULT_KEYBINDINGS.previous_servarr.key {
} else if matches_key!(previous_servarr, key) {
app.reset();
app.server_tabs.previous();
app.pop_and_push_navigation_stack(app.server_tabs.get_active_route());
@@ -115,17 +118,15 @@ fn handle_clear_errors(app: &mut App<'_>) {
fn handle_prompt_toggle(app: &mut App<'_>, key: Key) {
match key {
_ if key == DEFAULT_KEYBINDINGS.left.key || key == DEFAULT_KEYBINDINGS.right.key => {
match app.get_current_route() {
Route::Radarr(_, _) => {
app.data.radarr_data.prompt_confirm = !app.data.radarr_data.prompt_confirm
}
Route::Sonarr(_, _) => {
app.data.sonarr_data.prompt_confirm = !app.data.sonarr_data.prompt_confirm
}
_ => (),
_ if matches_key!(left, key) || matches_key!(right, key) => match app.get_current_route() {
Route::Radarr(_, _) => {
app.data.radarr_data.prompt_confirm = !app.data.radarr_data.prompt_confirm
}
}
Route::Sonarr(_, _) => {
app.data.sonarr_data.prompt_confirm = !app.data.sonarr_data.prompt_confirm
}
_ => (),
},
_ => (),
}
}
@@ -149,7 +150,7 @@ macro_rules! handle_text_box_left_right_keys {
macro_rules! handle_text_box_keys {
($self:expr, $key:expr, $input:expr) => {
match $self.key {
_ if $key == $crate::app::key_binding::DEFAULT_KEYBINDINGS.backspace.key => {
_ if $crate::matches_key!(backspace, $key) => {
$input.pop();
}
Key::Char(character) => {
@@ -165,7 +166,7 @@ macro_rules! handle_prompt_left_right_keys {
($self:expr, $confirm_prompt:expr, $data:ident) => {
if $self.app.data.$data.selected_block.get_active_block() == $confirm_prompt {
handle_prompt_toggle($self.app, $self.key);
} else if $self.key == $crate::app::key_binding::DEFAULT_KEYBINDINGS.left.key {
} else if $crate::matches_key!(left, $self.key) {
$self.app.data.$data.selected_block.left();
} else {
$self.app.data.$data.selected_block.right();