pub struct ComposedKeyHandler<H: KeyHandler> { /* private fields */ }Expand description
A composable key handler wrapper with pre-processing hooks.
ComposedKeyHandler wraps an inner handler and allows adding hooks
that run before the inner handler processes keys. This enables:
- Intercepting specific keys before they reach the inner handler
- Adding logging or debugging behavior
- Implementing layered key handling (e.g., modal modes on top of base handler)
§Example
ⓘ
let base_handler = DefaultKeyHandler::new(KeyBindings::emacs());
let handler = ComposedKeyHandler::new(base_handler)
.with_pre_hook(|key, _ctx| {
// Intercept F1 for help
if key.code == KeyCode::F(1) {
return Some(AppKeyResult::Action(AppKeyAction::custom(ShowHelp)));
}
None // Let inner handler process
});Implementations§
Source§impl<H: KeyHandler> ComposedKeyHandler<H>
impl<H: KeyHandler> ComposedKeyHandler<H>
Sourcepub fn with_pre_hook<F>(self, hook: F) -> Self
pub fn with_pre_hook<F>(self, hook: F) -> Self
Add a hook that runs before the inner handler.
If the hook returns Some(result), that result is used and the
inner handler is skipped. If the hook returns None, processing
continues to the next hook or the inner handler.
Hooks are called in the order they were added.
§Arguments
hook- A function that receives the key event and context, returningSome(AppKeyResult)to handle the key orNoneto pass through
§Example
ⓘ
let handler = ComposedKeyHandler::new(DefaultKeyHandler::default())
.with_pre_hook(|key, ctx| {
// Log all key presses
eprintln!("Key: {:?}", key);
None // Don't consume, let inner handler process
})
.with_pre_hook(|key, _ctx| {
// Intercept Ctrl+H for custom help
if key.code == KeyCode::Char('h') && key.modifiers == KeyModifiers::CONTROL {
return Some(AppKeyResult::Action(AppKeyAction::custom(MyHelp)));
}
None
});Trait Implementations§
Source§impl<H: KeyHandler> KeyHandler for ComposedKeyHandler<H>
impl<H: KeyHandler> KeyHandler for ComposedKeyHandler<H>
Source§fn handle_key(&mut self, key: KeyEvent, context: &KeyContext) -> AppKeyResult
fn handle_key(&mut self, key: KeyEvent, context: &KeyContext) -> AppKeyResult
Handle a key event. Read more
Source§fn status_hint(&self) -> Option<String>
fn status_hint(&self) -> Option<String>
Get a status hint to display in the status bar. Read more
Source§fn bindings(&self) -> &KeyBindings
fn bindings(&self) -> &KeyBindings
Get a reference to the key bindings. Read more
Auto Trait Implementations§
impl<H> Freeze for ComposedKeyHandler<H>where
H: Freeze,
impl<H> !RefUnwindSafe for ComposedKeyHandler<H>
impl<H> Send for ComposedKeyHandler<H>
impl<H> !Sync for ComposedKeyHandler<H>
impl<H> Unpin for ComposedKeyHandler<H>where
H: Unpin,
impl<H> !UnwindSafe for ComposedKeyHandler<H>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more