use crate::services::hotkey::Hotkey;
use crate::services::hotkey::HotkeyScope;
use crossterm::event::KeyCode;
pub trait HotkeyHandler {
fn handle_hotkey(&mut self, key: &KeyCode, scope: &HotkeyScope) -> bool;
fn can_handle(&self, hotkey: &Hotkey, key: &KeyCode, scope: &HotkeyScope) -> bool {
let hotkey_key = hotkey.key.to_lowercase();
let matches_key = match key {
KeyCode::Char(c) => hotkey_key == c.to_string().to_lowercase(),
KeyCode::Tab => hotkey_key == "tab",
KeyCode::Enter => hotkey_key == "enter",
KeyCode::Esc => hotkey_key == "escape" || hotkey_key == "esc",
KeyCode::Up => hotkey_key == "up",
KeyCode::Down => hotkey_key == "down",
KeyCode::Left => hotkey_key == "left",
KeyCode::Right => hotkey_key == "right",
KeyCode::Backspace => hotkey_key == "backspace",
_ => false,
};
if !matches_key {
return false;
}
match &hotkey.scope {
HotkeyScope::Global => true,
HotkeyScope::Modal(m) => matches!(scope, HotkeyScope::Modal(s) if s == m),
HotkeyScope::Tab(t) => matches!(scope, HotkeyScope::Tab(s) if s == t),
HotkeyScope::Custom(c) => matches!(scope, HotkeyScope::Custom(s) if s == c),
}
}
}