Skip to main content

ActionMapper

Struct ActionMapper 

Source
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):

PriorityConditionKeyAction
1modal_openEscDismissModal
2modal_openCtrl+CDismissModal
3input_nonemptyCtrl+CClearInput
4task_runningCtrl+CCancelTask
5idleCtrl+CQuit (configurable)
6view_overlayEscCloseOverlay
7input_nonemptyEscClearInput
8task_runningEscCancelTask
9alwaysEsc EscToggleTreeView
10alwaysCtrl+DSoftQuit
11alwaysCtrl+QHardQuit

§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

Source

pub fn new(config: ActionConfig) -> Self

Create a new action mapper with the given configuration.

Source

pub fn with_defaults() -> Self

Create a new action mapper with default configuration.

Source

pub fn from_env() -> Self

Create a new action mapper loading config from environment.

Source

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 process
  • state - Current application state flags
  • now - Current timestamp for sequence detection
Source

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 flags
  • now - Current timestamp
Source

pub fn is_pending_esc(&self) -> bool

Whether the mapper is waiting for a second Esc.

Source

pub fn reset(&mut self)

Reset the sequence detector state.

Any pending Esc is discarded.

Source

pub fn config(&self) -> &ActionConfig

Get a reference to the current configuration.

Source

pub fn set_config(&mut self, config: ActionConfig)

Update the configuration.

Trait Implementations§

Source§

impl Debug for ActionMapper

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.