2015-05-15 19:16:58 +00:00
|
|
|
//! User-input events and their effects.
|
2016-07-15 06:25:32 +00:00
|
|
|
//!
|
|
|
|
//! * Every user input the application receives is converted to an
|
|
|
|
//! [`Event`](./enum.Event.html).
|
|
|
|
//! * Each event is then given to the root, and descends the view tree down to
|
|
|
|
//! the view currently in focus, through the
|
|
|
|
//! [`on_event`](../view/trait.View.html#method.on_event) method.
|
|
|
|
//! * If the view consumes the event, it may return a callback to be
|
|
|
|
//! executed.
|
|
|
|
//! * Otherwise, it ignores the event, and the view parent can in turn
|
|
|
|
//! choose to consume it or not.
|
|
|
|
//! * If no view consumes the event, the
|
|
|
|
//! [global callback](../struct.Cursive.html#method.add_global_callback)
|
|
|
|
//! table is checked.
|
2015-05-15 19:16:58 +00:00
|
|
|
|
2015-05-15 00:41:17 +00:00
|
|
|
use std::rc::Rc;
|
|
|
|
|
2016-03-15 22:37:57 +00:00
|
|
|
use Cursive;
|
2015-05-16 00:56:38 +00:00
|
|
|
|
2015-05-15 00:41:17 +00:00
|
|
|
/// Callback is a function that can be triggered by an event.
|
|
|
|
/// It has a mutable access to the cursive root.
|
2016-07-02 22:02:42 +00:00
|
|
|
pub type Callback = Rc<Fn(&mut Cursive)>;
|
2015-05-15 00:41:17 +00:00
|
|
|
|
|
|
|
/// Answer to an event notification.
|
|
|
|
/// The event can be consumed or ignored.
|
|
|
|
pub enum EventResult {
|
|
|
|
/// The event was ignored. The parent can keep handling it.
|
|
|
|
Ignored,
|
|
|
|
/// The event was consumed. An optionnal callback to run is attached.
|
2016-07-02 22:02:42 +00:00
|
|
|
Consumed(Option<Callback>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EventResult {
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Convenient method to create `Consumed(Some(f))`
|
2016-07-02 22:02:42 +00:00
|
|
|
pub fn with_cb<F: 'static + Fn(&mut Cursive)>(f: F) -> Self {
|
|
|
|
EventResult::Consumed(Some(Rc::new(f)))
|
|
|
|
}
|
2016-07-16 20:25:21 +00:00
|
|
|
|
|
|
|
/// Returns `true` if `self` is `EventResult::Consumed`.
|
|
|
|
pub fn is_consumed(&self) -> bool {
|
|
|
|
match *self {
|
|
|
|
EventResult::Consumed(_) => true,
|
|
|
|
EventResult::Ignored => false,
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 02:41:35 +00:00
|
|
|
}
|
2015-05-28 01:04:33 +00:00
|
|
|
|
2016-07-11 01:27:26 +00:00
|
|
|
/// A non-character key on the keyboard
|
|
|
|
#[derive(PartialEq,Eq,Clone,Copy,Hash,Debug)]
|
2015-05-28 01:04:33 +00:00
|
|
|
pub enum Key {
|
2016-07-11 01:27:26 +00:00
|
|
|
/// Both Enter (or Return) and numpad Enter
|
2015-05-28 01:04:33 +00:00
|
|
|
Enter,
|
2015-05-28 19:22:39 +00:00
|
|
|
/// Tabulation key
|
2015-05-28 05:21:23 +00:00
|
|
|
Tab,
|
2016-07-11 01:27:26 +00:00
|
|
|
/// Backspace key
|
2016-06-26 02:28:42 +00:00
|
|
|
Backspace,
|
2016-07-11 01:27:26 +00:00
|
|
|
/// Escape key
|
2016-06-26 02:28:42 +00:00
|
|
|
Esc,
|
|
|
|
|
2016-07-11 01:27:26 +00:00
|
|
|
/// Left arrow
|
2015-05-28 21:44:10 +00:00
|
|
|
Left,
|
2016-07-11 01:27:26 +00:00
|
|
|
/// Right arrow
|
2015-05-28 21:44:10 +00:00
|
|
|
Right,
|
2016-07-11 01:27:26 +00:00
|
|
|
/// Up arrow
|
2015-05-28 21:44:10 +00:00
|
|
|
Up,
|
2016-07-11 01:27:26 +00:00
|
|
|
/// Down arrow
|
2015-05-28 21:44:10 +00:00
|
|
|
Down,
|
2016-06-26 02:28:42 +00:00
|
|
|
|
2016-07-11 01:27:26 +00:00
|
|
|
/// Insert key
|
|
|
|
Ins,
|
2015-05-28 19:22:39 +00:00
|
|
|
/// Delete key
|
2015-05-28 01:04:33 +00:00
|
|
|
Del,
|
2016-07-11 01:27:26 +00:00
|
|
|
/// Home key
|
|
|
|
Home,
|
|
|
|
/// End key
|
|
|
|
End,
|
|
|
|
/// Page Up key
|
|
|
|
PageUp,
|
|
|
|
/// Page Down key
|
|
|
|
PageDown,
|
2016-06-26 02:28:42 +00:00
|
|
|
|
2016-07-11 01:27:26 +00:00
|
|
|
/// The 5 in the center of the keypad, when numlock is disabled.
|
|
|
|
NumpadCenter,
|
2016-06-26 02:28:42 +00:00
|
|
|
|
2016-07-11 01:27:26 +00:00
|
|
|
/// F1 key
|
|
|
|
F1,
|
|
|
|
/// F2 key
|
|
|
|
F2,
|
|
|
|
/// F3 key
|
|
|
|
F3,
|
|
|
|
/// F4 key
|
|
|
|
F4,
|
|
|
|
/// F5 key
|
|
|
|
F5,
|
|
|
|
/// F6 key
|
|
|
|
F6,
|
|
|
|
/// F7 key
|
|
|
|
F7,
|
|
|
|
/// F8 key
|
|
|
|
F8,
|
|
|
|
/// F9 key
|
|
|
|
F9,
|
|
|
|
/// F10 key
|
|
|
|
F10,
|
|
|
|
/// F11 key
|
|
|
|
F11,
|
|
|
|
/// F12 key
|
|
|
|
F12,
|
2015-05-28 01:04:33 +00:00
|
|
|
}
|
|
|
|
|
2016-07-11 01:27:26 +00:00
|
|
|
impl Key {
|
|
|
|
/// Returns the function key corresponding to the given number
|
|
|
|
///
|
|
|
|
/// 1 -> F1, etc...
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// If `n == 0 || n > 12`
|
|
|
|
pub fn from_f(n: u8) -> Key {
|
|
|
|
match n {
|
|
|
|
1 => Key::F1,
|
|
|
|
2 => Key::F2,
|
|
|
|
3 => Key::F3,
|
|
|
|
4 => Key::F4,
|
|
|
|
5 => Key::F5,
|
|
|
|
6 => Key::F6,
|
|
|
|
7 => Key::F7,
|
|
|
|
8 => Key::F8,
|
|
|
|
9 => Key::F9,
|
|
|
|
10 => Key::F10,
|
|
|
|
11 => Key::F11,
|
|
|
|
12 => Key::F12,
|
|
|
|
_ => panic!("unknown function key: F{}", n),
|
2015-05-28 01:04:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-08 22:38:10 +00:00
|
|
|
/// Represents an event as seen by the application.
|
2016-07-11 01:27:26 +00:00
|
|
|
#[derive(PartialEq,Eq,Clone,Copy,Hash,Debug)]
|
2015-05-28 01:04:33 +00:00
|
|
|
pub enum Event {
|
2016-07-11 01:27:26 +00:00
|
|
|
/// Event fired when the window is resized
|
|
|
|
WindowResize,
|
2015-05-28 01:04:33 +00:00
|
|
|
|
2016-07-11 01:27:26 +00:00
|
|
|
/// A character was entered (includes numbers, punctuation, ...)
|
|
|
|
Char(char),
|
|
|
|
/// A character was entered with the Ctrl key pressed
|
|
|
|
CtrlChar(char),
|
|
|
|
/// A character was entered with the Alt key pressed
|
|
|
|
AltChar(char),
|
2015-05-28 01:04:33 +00:00
|
|
|
|
2016-07-11 01:27:26 +00:00
|
|
|
/// A non-character key was pressed
|
|
|
|
Key(Key),
|
|
|
|
/// A non-character key was pressed with the Shift key pressed
|
|
|
|
Shift(Key),
|
|
|
|
/// A non-character key was pressed with the Alt key pressed
|
|
|
|
Alt(Key),
|
|
|
|
/// A non-character key was pressed with the Shift and Alt keys pressed
|
|
|
|
AltShift(Key),
|
|
|
|
/// A non-character key was pressed with the Ctrl key pressed
|
|
|
|
Ctrl(Key),
|
|
|
|
/// A non-character key was pressed with the Ctrl and Shift keys pressed
|
|
|
|
CtrlShift(Key),
|
|
|
|
/// A non-character key was pressed with the Ctrl and Alt keys pressed
|
|
|
|
CtrlAlt(Key),
|
|
|
|
|
|
|
|
/// An unknown event was received.
|
|
|
|
Unknown(i32),
|
2015-05-28 01:04:33 +00:00
|
|
|
}
|
|
|
|
|
2016-07-11 01:27:26 +00:00
|
|
|
impl From<char> for Event {
|
|
|
|
fn from(c: char) -> Event {
|
|
|
|
Event::Char(c)
|
2015-05-28 01:04:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-11 01:27:26 +00:00
|
|
|
impl From<Key> for Event {
|
|
|
|
fn from(k: Key) -> Event {
|
|
|
|
Event::Key(k)
|
2015-05-28 01:04:33 +00:00
|
|
|
}
|
|
|
|
}
|