use super::super::keymap::KeyMap;
use super::{BoardView, ExitMode};
use pinto::kanban_keys::KeyAction;
use ratatui::crossterm::event::{self, KeyCode, KeyModifiers};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum QuitIntent {
None,
Confirm(ExitMode),
Leave(ExitMode),
}
pub(super) fn quit_intent(keymap: &KeyMap, key: event::KeyEvent, confirm_quit: bool) -> QuitIntent {
let mode = if keymap.matches(KeyAction::Shell, key) {
ExitMode::Shell
} else if keymap.matches(KeyAction::Quit, key) {
ExitMode::Quit
} else {
return QuitIntent::None;
};
if confirm_quit {
QuitIntent::Confirm(mode)
} else {
QuitIntent::Leave(mode)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum PopupAction {
None,
Close,
ScrollUp,
ScrollDown,
SelectUp,
SelectDown,
SelectLeft,
SelectRight,
Edit,
}
pub(super) fn popup_action(keymap: &KeyMap, key: event::KeyEvent) -> PopupAction {
if keymap.matches(KeyAction::PopupClose, key) || keymap.matches(KeyAction::Details, key) {
PopupAction::Close
} else if keymap.matches(KeyAction::PopupSelectUp, key) {
PopupAction::SelectUp
} else if keymap.matches(KeyAction::PopupSelectDown, key) {
PopupAction::SelectDown
} else if keymap.matches(KeyAction::PopupSelectLeft, key) {
PopupAction::SelectLeft
} else if keymap.matches(KeyAction::PopupSelectRight, key) {
PopupAction::SelectRight
} else if keymap.matches(KeyAction::PopupScrollUp, key) {
PopupAction::ScrollUp
} else if keymap.matches(KeyAction::PopupScrollDown, key) {
PopupAction::ScrollDown
} else if keymap.matches(KeyAction::Edit, key) {
PopupAction::Edit
} else {
PopupAction::None
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum HelpKeyAction {
Close,
ScrollUp,
ScrollDown,
PassThrough,
}
pub(super) fn help_key_action(keymap: &KeyMap, key: event::KeyEvent) -> HelpKeyAction {
if keymap.matches(KeyAction::Help, key) {
HelpKeyAction::Close
} else if keymap.matches(KeyAction::PopupScrollUp, key) {
HelpKeyAction::ScrollUp
} else if keymap.matches(KeyAction::PopupScrollDown, key) {
HelpKeyAction::ScrollDown
} else {
HelpKeyAction::PassThrough
}
}
pub(super) fn should_close_help_after_key(
view: &BoardView,
keymap: &KeyMap,
key: event::KeyEvent,
) -> bool {
if view.is_popup_open() {
return popup_action(keymap, key) != PopupAction::None;
}
if view.is_input_active() {
let selecting_target = view.is_relation_input()
&& view.input_buffer().is_empty()
&& [
KeyAction::SelectLeft,
KeyAction::SelectRight,
KeyAction::SelectUp,
KeyAction::SelectDown,
]
.into_iter()
.any(|action| keymap.matches(action, key));
return selecting_target || text_entry_key_is_accepted(key);
}
if view.is_searching() {
return text_entry_key_is_accepted(key);
}
if view.search_filter().is_some() && keymap.matches(KeyAction::ClearFilter, key) {
return true;
}
[
KeyAction::Shell,
KeyAction::Quit,
KeyAction::SelectLeft,
KeyAction::SelectRight,
KeyAction::SelectUp,
KeyAction::SelectDown,
KeyAction::MoveLeft,
KeyAction::MoveRight,
KeyAction::ReorderUp,
KeyAction::ReorderDown,
KeyAction::ToggleExpand,
KeyAction::Add,
KeyAction::DependencyAdd,
KeyAction::DependencyRemove,
KeyAction::Parent,
KeyAction::Edit,
KeyAction::Reload,
KeyAction::Maximize,
KeyAction::Search,
KeyAction::RegexSearch,
KeyAction::Details,
]
.into_iter()
.any(|action| keymap.matches(action, key))
}
pub(super) fn text_entry_key_is_accepted(key: event::KeyEvent) -> bool {
match key.code {
KeyCode::Esc | KeyCode::Enter | KeyCode::Backspace => true,
KeyCode::Char(_) if !key.modifiers.contains(KeyModifiers::CONTROL) => true,
_ => false,
}
}