2015-05-15 19:16:58 +00:00
|
|
|
//! User-input events and their effects.
|
|
|
|
|
2015-05-28 01:04:33 +00:00
|
|
|
use std::fmt;
|
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 {
|
|
|
|
pub fn with_cb<F: 'static + Fn(&mut Cursive)>(f: F) -> Self {
|
|
|
|
EventResult::Consumed(Some(Rc::new(f)))
|
|
|
|
}
|
2015-05-19 02:41:35 +00:00
|
|
|
}
|
2015-05-28 01:04:33 +00:00
|
|
|
|
2015-06-08 22:38:10 +00:00
|
|
|
/// Represents a key, or a combination of keys.
|
2015-05-28 01:04:33 +00:00
|
|
|
#[derive(PartialEq,Eq,Clone,Copy,Hash)]
|
|
|
|
pub enum Key {
|
2015-05-28 19:22:39 +00:00
|
|
|
/// Both Enter 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,
|
2015-05-28 23:19:16 +00:00
|
|
|
ShiftTab,
|
2016-06-26 02:28:42 +00:00
|
|
|
Backspace,
|
|
|
|
|
2016-06-26 02:38:22 +00:00
|
|
|
/// Indicates the window was resized
|
|
|
|
///
|
|
|
|
/// (Not really a key)
|
|
|
|
Resize,
|
|
|
|
|
2016-06-26 02:28:42 +00:00
|
|
|
/// Escape key.
|
|
|
|
Esc,
|
|
|
|
/// The 5 in the center of the keypad, when numlock is disabled.
|
|
|
|
NumpadCenter,
|
|
|
|
|
2015-05-28 21:44:10 +00:00
|
|
|
Left,
|
2016-06-26 02:28:42 +00:00
|
|
|
/// Left arrow while shift is pressed.
|
|
|
|
ShiftLeft,
|
|
|
|
AltLeft,
|
|
|
|
AltShiftLeft,
|
|
|
|
CtrlLeft,
|
|
|
|
CtrlShiftLeft,
|
|
|
|
CtrlAltLeft,
|
|
|
|
|
2015-05-28 21:44:10 +00:00
|
|
|
Right,
|
2016-06-26 02:28:42 +00:00
|
|
|
/// Right arrow while shift is pressed.
|
|
|
|
ShiftRight,
|
|
|
|
AltRight,
|
|
|
|
AltShiftRight,
|
|
|
|
CtrlRight,
|
|
|
|
CtrlShiftRight,
|
|
|
|
CtrlAltRight,
|
|
|
|
|
2015-05-28 21:44:10 +00:00
|
|
|
Up,
|
2016-06-26 02:28:42 +00:00
|
|
|
ShiftUp,
|
|
|
|
AltUp,
|
|
|
|
AltShiftUp,
|
|
|
|
CtrlUp,
|
|
|
|
CtrlShiftUp,
|
|
|
|
CtrlAltUp,
|
|
|
|
|
2015-05-28 21:44:10 +00:00
|
|
|
Down,
|
2016-06-26 02:28:42 +00:00
|
|
|
ShiftDown,
|
|
|
|
AltDown,
|
|
|
|
AltShiftDown,
|
|
|
|
CtrlDown,
|
|
|
|
CtrlShiftDown,
|
|
|
|
CtrlAltDown,
|
|
|
|
|
2015-05-28 01:04:33 +00:00
|
|
|
PageUp,
|
2016-06-26 02:28:42 +00:00
|
|
|
ShiftPageUp,
|
|
|
|
AltPageUp,
|
|
|
|
AltShiftPageUp,
|
|
|
|
CtrlPageUp,
|
|
|
|
CtrlShiftPageUp,
|
|
|
|
CtrlAltPageUp,
|
|
|
|
|
2015-05-28 01:04:33 +00:00
|
|
|
PageDown,
|
2016-06-26 02:28:42 +00:00
|
|
|
ShiftPageDown,
|
|
|
|
AltPageDown,
|
|
|
|
AltShiftPageDown,
|
|
|
|
CtrlPageDown,
|
|
|
|
CtrlShiftPageDown,
|
|
|
|
CtrlAltPageDown,
|
|
|
|
|
2015-05-28 01:04:33 +00:00
|
|
|
Home,
|
2015-06-06 22:05:01 +00:00
|
|
|
ShiftHome,
|
2016-06-26 02:28:42 +00:00
|
|
|
AltHome,
|
|
|
|
AltShiftHome,
|
2015-06-01 17:49:43 +00:00
|
|
|
CtrlHome,
|
2015-06-06 22:05:01 +00:00
|
|
|
CtrlShiftHome,
|
2016-06-26 02:28:42 +00:00
|
|
|
CtrlAltHome,
|
|
|
|
|
2015-05-28 01:04:33 +00:00
|
|
|
End,
|
2015-06-06 22:05:01 +00:00
|
|
|
ShiftEnd,
|
2016-06-26 02:28:42 +00:00
|
|
|
AltEnd,
|
|
|
|
AltShiftEnd,
|
2015-06-01 17:49:43 +00:00
|
|
|
CtrlEnd,
|
2015-06-06 22:05:01 +00:00
|
|
|
CtrlShiftEnd,
|
2016-06-26 02:28:42 +00:00
|
|
|
CtrlAltEnd,
|
|
|
|
|
2015-05-28 19:22:39 +00:00
|
|
|
/// Delete key
|
2015-05-28 01:04:33 +00:00
|
|
|
Del,
|
2015-05-29 18:31:20 +00:00
|
|
|
ShiftDel,
|
2016-06-26 02:28:42 +00:00
|
|
|
AltDel,
|
|
|
|
AltShiftDel,
|
2015-05-29 18:31:20 +00:00
|
|
|
CtrlDel,
|
2015-06-06 22:05:01 +00:00
|
|
|
CtrlShiftDel,
|
2016-06-26 02:28:42 +00:00
|
|
|
|
2015-06-07 05:48:16 +00:00
|
|
|
/// Insert key.
|
2015-05-28 19:22:39 +00:00
|
|
|
Ins,
|
2015-06-07 05:48:16 +00:00
|
|
|
/// Insert key while ctrl is pressed.
|
2015-06-01 17:49:43 +00:00
|
|
|
CtrlIns,
|
2016-06-26 02:28:42 +00:00
|
|
|
AltIns,
|
|
|
|
CtrlAltIns,
|
|
|
|
|
2015-05-28 21:57:21 +00:00
|
|
|
F(u8),
|
2015-05-29 18:31:20 +00:00
|
|
|
ShiftF(u8),
|
2016-06-26 02:38:22 +00:00
|
|
|
AltF(u8),
|
|
|
|
CtrlF(u8),
|
2015-05-29 18:31:20 +00:00
|
|
|
CtrlShiftF(u8),
|
2015-05-28 21:44:10 +00:00
|
|
|
CtrlChar(char),
|
2015-05-28 01:04:33 +00:00
|
|
|
Unknown(i32),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Key {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match *self {
|
|
|
|
Key::Unknown(ch) => write!(f, "Unknown: {}", ch),
|
2015-05-28 21:44:10 +00:00
|
|
|
Key::CtrlChar(ch) => write!(f, "Ctrl-{}", ch),
|
2015-05-28 21:57:21 +00:00
|
|
|
Key::F(n) => write!(f, "F{}", n),
|
2015-05-29 18:31:20 +00:00
|
|
|
Key::ShiftF(n) => write!(f, "Shift-F{}", n),
|
2016-06-26 02:38:22 +00:00
|
|
|
Key::AltF(n) => write!(f, "Alt-F{}", n),
|
2015-05-29 18:31:20 +00:00
|
|
|
Key::CtrlF(n) => write!(f, "Ctrl-F{}", n),
|
|
|
|
Key::CtrlShiftF(n) => write!(f, "Ctrl-Shift-F{}", n),
|
2016-06-25 23:36:22 +00:00
|
|
|
key => {
|
|
|
|
write!(f,
|
|
|
|
"{}",
|
|
|
|
match key {
|
2016-06-26 02:38:22 +00:00
|
|
|
Key::Resize => "Screen resize",
|
2016-06-25 23:36:22 +00:00
|
|
|
Key::NumpadCenter => "Numpad center",
|
2016-06-26 02:28:42 +00:00
|
|
|
Key::Backspace => "Backspace",
|
|
|
|
Key::Enter => "Enter",
|
|
|
|
Key::Tab => "Tab",
|
|
|
|
Key::ShiftTab => "Shift-Tab",
|
|
|
|
|
|
|
|
Key::PageUp => "PageUp",
|
|
|
|
Key::ShiftPageUp => "Shift-PageUp",
|
|
|
|
Key::AltPageUp => "Alt-PageUp",
|
|
|
|
Key::AltShiftPageUp => "Alt-Shift-PageUp",
|
|
|
|
Key::CtrlPageUp => "Ctrl-PageUp",
|
|
|
|
Key::CtrlShiftPageUp => "Ctrl-Shift-PageUp",
|
|
|
|
Key::CtrlAltPageUp => "Ctrl-Alt-PageUp",
|
|
|
|
|
|
|
|
Key::PageDown => "PageDown",
|
|
|
|
Key::ShiftPageDown => "Shift-PageDown",
|
|
|
|
Key::AltPageDown => "Alt-PageDown",
|
|
|
|
Key::AltShiftPageDown => "Alt-Shift-PageDown",
|
|
|
|
Key::CtrlPageDown => "Ctrl-PageDown",
|
|
|
|
Key::CtrlShiftPageDown => "Ctrl-Shift-PageDown",
|
|
|
|
Key::CtrlAltPageDown => "Ctrl-Alt-PageDown",
|
|
|
|
|
2016-06-25 23:36:22 +00:00
|
|
|
Key::Left => "Left",
|
2016-06-26 02:28:42 +00:00
|
|
|
Key::ShiftLeft => "Shift-Left",
|
|
|
|
Key::AltLeft => "Alt-Left",
|
|
|
|
Key::AltShiftLeft => "Alt-Shift-Left",
|
|
|
|
Key::CtrlLeft => "Ctrl-Left",
|
2016-06-25 23:36:22 +00:00
|
|
|
Key::CtrlShiftLeft => "Ctrl-Shift-Left",
|
2016-06-26 02:28:42 +00:00
|
|
|
Key::CtrlAltLeft => "Ctrl-Alt-Left",
|
|
|
|
|
|
|
|
Key::Right => "Right",
|
|
|
|
Key::ShiftRight => "Shift-Right",
|
|
|
|
Key::AltRight => "Alt-Right",
|
|
|
|
Key::AltShiftRight => "Alt-Shift-Right",
|
|
|
|
Key::CtrlRight => "Ctrl-Right",
|
2016-06-25 23:36:22 +00:00
|
|
|
Key::CtrlShiftRight => "Ctrl-Shift-Right",
|
2016-06-26 02:28:42 +00:00
|
|
|
Key::CtrlAltRight => "Ctrl-Alt-Right",
|
|
|
|
|
|
|
|
Key::Down => "Down",
|
|
|
|
Key::ShiftDown => "Shift-Down",
|
|
|
|
Key::AltDown => "Alt-Down",
|
|
|
|
Key::AltShiftDown => "Alt-Shift-Down",
|
|
|
|
Key::CtrlDown => "Ctrl-Down",
|
2016-06-25 23:36:22 +00:00
|
|
|
Key::CtrlShiftDown => "Ctrl-Shift-Down",
|
2016-06-26 02:28:42 +00:00
|
|
|
Key::CtrlAltDown => "Ctrl-Alt-Down",
|
|
|
|
|
|
|
|
Key::Up => "Up",
|
2016-06-25 23:36:22 +00:00
|
|
|
Key::ShiftUp => "Shift-Up",
|
2016-06-26 02:28:42 +00:00
|
|
|
Key::AltUp => "Alt-Up",
|
|
|
|
Key::AltShiftUp => "Alt-Shift-Up",
|
|
|
|
Key::CtrlUp => "Ctrl-Up",
|
|
|
|
Key::CtrlShiftUp => "Ctrl-Shift-Up",
|
|
|
|
Key::CtrlAltUp => "Ctrl-Alt-Up",
|
|
|
|
|
|
|
|
Key::Del => "Del",
|
2016-06-25 23:36:22 +00:00
|
|
|
Key::ShiftDel => "Shift-Del",
|
2016-06-26 02:28:42 +00:00
|
|
|
Key::AltDel => "Alt-Del",
|
|
|
|
Key::AltShiftDel => "Alt-Shift-Del",
|
2016-06-25 23:36:22 +00:00
|
|
|
Key::CtrlDel => "Ctrl-Del",
|
|
|
|
Key::CtrlShiftDel => "Ctrl-Shift-Del",
|
2016-06-26 02:28:42 +00:00
|
|
|
|
2016-06-25 23:36:22 +00:00
|
|
|
Key::Home => "Home",
|
|
|
|
Key::ShiftHome => "Shift-Home",
|
2016-06-26 02:28:42 +00:00
|
|
|
Key::AltHome => "Alt-Home",
|
|
|
|
Key::AltShiftHome => "Alt-Shift-Home",
|
2016-06-25 23:36:22 +00:00
|
|
|
Key::CtrlHome => "Ctrl-Home",
|
|
|
|
Key::CtrlShiftHome => "Ctrl-Shift-Home",
|
2016-06-26 02:28:42 +00:00
|
|
|
Key::CtrlAltHome => "Ctrl-Alt-Home",
|
|
|
|
|
2016-06-25 23:36:22 +00:00
|
|
|
Key::End => "End",
|
|
|
|
Key::ShiftEnd => "Shift-End",
|
2016-06-26 02:28:42 +00:00
|
|
|
Key::AltEnd => "Alt-End",
|
|
|
|
Key::AltShiftEnd => "Alt-Shift-End",
|
2016-06-25 23:36:22 +00:00
|
|
|
Key::CtrlEnd => "Ctrl-End",
|
|
|
|
Key::CtrlShiftEnd => "Ctrl-Shift-End",
|
2016-06-26 02:28:42 +00:00
|
|
|
Key::CtrlAltEnd => "Ctrl-Alt-End",
|
|
|
|
|
2016-06-25 23:36:22 +00:00
|
|
|
Key::Ins => "Ins",
|
2016-06-26 02:28:42 +00:00
|
|
|
Key::AltIns => "Alt-Ins",
|
2016-06-25 23:36:22 +00:00
|
|
|
Key::CtrlIns => "Ctrl-Ins",
|
2016-06-26 02:28:42 +00:00
|
|
|
Key::CtrlAltIns => "Ctrl-Alt-Ins",
|
|
|
|
|
2016-06-25 23:36:22 +00:00
|
|
|
Key::Esc => "Esc",
|
|
|
|
_ => "Missing key label",
|
|
|
|
})
|
|
|
|
}
|
2015-05-28 01:04:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-08 22:38:10 +00:00
|
|
|
/// Represents an event as seen by the application.
|
|
|
|
///
|
2015-05-28 01:04:33 +00:00
|
|
|
#[derive(PartialEq,Eq,Clone,Copy,Hash)]
|
|
|
|
pub enum Event {
|
2015-06-08 22:38:10 +00:00
|
|
|
/// A text character was entered.
|
2016-06-28 05:40:11 +00:00
|
|
|
Char(char),
|
2015-06-08 22:38:10 +00:00
|
|
|
/// A key was pressed.
|
2016-06-28 05:40:11 +00:00
|
|
|
Key(Key),
|
2015-05-28 01:04:33 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 22:36:51 +00:00
|
|
|
/// Generic trait to convert a value to an event.
|
2015-05-28 01:04:33 +00:00
|
|
|
pub trait ToEvent {
|
|
|
|
fn to_event(self) -> Event;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToEvent for char {
|
|
|
|
fn to_event(self) -> Event {
|
2016-06-28 05:40:11 +00:00
|
|
|
Event::Char(self)
|
2015-05-28 01:04:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToEvent for Key {
|
|
|
|
fn to_event(self) -> Event {
|
2016-06-28 05:40:11 +00:00
|
|
|
Event::Key(self)
|
2015-05-28 01:04:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToEvent for Event {
|
|
|
|
fn to_event(self) -> Event {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|