1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
#![doc = include_str!("../readme.md")]
pub mod crossterm;
pub mod util;
/// Default key bindings when focused.
/// Also handles all events that are MouseOnly.
#[derive(Debug, Default)]
pub struct FocusKeys;
/// Handler for mouse events only.
/// * Useful when writing a new key binding.
/// * FocusKeys should only be handled by the focused widget,
/// while mouse events are not bound by the focus.
#[derive(Debug, Default)]
pub struct MouseOnly;
///
/// A very broad trait for an event handler for widgets.
///
/// As widget types are only short-lived, this trait should be implemented
/// for the state type. Thereby it can modify any state, and it can
/// return an arbitrary result, that fits the widget.
///
pub trait HandleEvent<Event, KeyMap, R: ConsumedEvent> {
/// Handle an event.
///
/// * self - Should be the widget state.
/// * event - Event
/// * keymap - Which keymapping. Predefined are FocusKeys and MouseOnly.
fn handle(&mut self, event: &Event, keymap: KeyMap) -> R;
}
/// When composing several widgets, the minimum information from the outcome
/// of the inner widget is, whether it used & consumed the event.
///
/// This allows shortcutting the event-handling and prevents double
/// interactions with the event. The inner widget can special case an event,
/// and the fallback behaviour on the outer layer should not run too.
pub trait ConsumedEvent {
fn is_consumed(&self) -> bool;
}
impl<V, E> ConsumedEvent for Result<V, E>
where
V: ConsumedEvent,
{
fn is_consumed(&self) -> bool {
match self {
Ok(v) => v.is_consumed(),
Err(_) => true,
}
}
}
impl<V> ConsumedEvent for Option<V>
where
V: ConsumedEvent,
{
fn is_consumed(&self) -> bool {
match self {
Some(v) => v.is_consumed(),
None => true,
}
}
}