pub struct ActionMapper { /* private fields */ }Expand description
Maps key events to high-level actions based on application state.
The ActionMapper integrates the sequence detector and implements the
priority table from the keybinding policy specification (bd-2vne.1).
§Priority Order
Actions are resolved in priority order (first match wins):
| Priority | Condition | Key | Action |
|---|---|---|---|
| 1 | modal_open | Esc | DismissModal |
| 2 | modal_open | Ctrl+C | DismissModal |
| 3 | input_nonempty | Ctrl+C | ClearInput |
| 4 | task_running | Ctrl+C | CancelTask |
| 5 | idle | Ctrl+C | Quit (configurable) |
| 6 | view_overlay | Esc | CloseOverlay |
| 7 | input_nonempty | Esc | ClearInput |
| 8 | task_running | Esc | CancelTask |
| 9 | always | Esc Esc | ToggleTreeView |
| 10 | always | Ctrl+D | SoftQuit |
| 11 | always | Ctrl+Q | HardQuit |
§Usage
use std::time::Instant;
use ftui_core::keybinding::{ActionMapper, ActionConfig, AppState, Action};
use ftui_core::event::{KeyCode, KeyEvent, Modifiers};
let mut mapper = ActionMapper::new(ActionConfig::default());
let now = Instant::now();
let state = AppState::default();
let key = KeyEvent::new(KeyCode::Char('q')).with_modifiers(Modifiers::CTRL);
assert_eq!(mapper.map(&key, &state, now), [Action::HardQuit]);Implementations§
Source§impl ActionMapper
impl ActionMapper
Sourcepub fn new(config: ActionConfig) -> Self
pub fn new(config: ActionConfig) -> Self
Create a new action mapper with the given configuration.
Sourcepub fn with_defaults() -> Self
pub fn with_defaults() -> Self
Create a new action mapper with default configuration.
Sourcepub fn map(
&mut self,
event: &KeyEvent,
state: &AppState,
now: Instant,
) -> Vec<Action>
pub fn map( &mut self, event: &KeyEvent, state: &AppState, now: Instant, ) -> Vec<Action>
Map a key event to the actions it resolves to, in the order to apply them.
Usually one action. Empty while a lone Esc waits for a possible second
Esc (drive check_timeout on ticks), or when the
resolved action is a no-op (e.g. Ctrl+C when idle with
CtrlCIdleAction::Noop). Two when the key interrupts a pending Esc
that has an action in state: the Esc’s action first, then this key’s
own; both are resolved against the same state. An Esc with no action
(idle) contributes nothing, as it does on timeout, so
Action::PassThrough always means: handle event itself.
§Arguments
event- The key event to processstate- Current application state flagsnow- Current timestamp for sequence detection
Sourcepub fn check_timeout(
&mut self,
state: &AppState,
now: Instant,
) -> Option<Action>
pub fn check_timeout( &mut self, state: &AppState, now: Instant, ) -> Option<Action>
Check for sequence timeout and return pending action if expired.
Call this periodically (e.g., on tick) to handle single Esc after the timeout window closes.
§Arguments
state- Current application state flagsnow- Current timestamp
Sourcepub fn is_pending_esc(&self) -> bool
pub fn is_pending_esc(&self) -> bool
Whether the mapper is waiting for a second Esc.
Sourcepub fn config(&self) -> &ActionConfig
pub fn config(&self) -> &ActionConfig
Get a reference to the current configuration.
Sourcepub fn set_config(&mut self, config: ActionConfig)
pub fn set_config(&mut self, config: ActionConfig)
Update the configuration.