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
|
|
|
|
2018-05-18 00:37:39 +00:00
|
|
|
use std::any::Any;
|
2018-07-25 22:00:29 +00:00
|
|
|
use std::cell::RefCell;
|
2016-10-02 22:22:29 +00:00
|
|
|
use std::ops::Deref;
|
|
|
|
use std::rc::Rc;
|
2019-02-28 23:54:12 +00:00
|
|
|
use crate::vec::Vec2;
|
|
|
|
use crate::Cursive;
|
2017-10-08 23:56:15 +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-25 06:00:13 +00:00
|
|
|
#[derive(Clone)]
|
2019-02-28 23:55:02 +00:00
|
|
|
pub struct Callback(Rc<Box<dyn Fn(&mut Cursive)>>);
|
2016-07-25 06:00:13 +00:00
|
|
|
// TODO: remove the Box when Box<T: Sized> -> Rc<T> is possible
|
|
|
|
|
2018-04-17 05:39:16 +00:00
|
|
|
/// A boxed callback that can be run on `&mut Any`.
|
2019-02-28 23:55:02 +00:00
|
|
|
pub type AnyCb<'a> = Box<FnMut(&mut dyn Any) + 'a>;
|
2018-04-17 05:39:16 +00:00
|
|
|
|
2018-11-09 18:40:06 +00:00
|
|
|
/// A trigger that only selects some types of events.
|
2019-02-28 23:55:02 +00:00
|
|
|
pub struct EventTrigger(Box<dyn Fn(&Event) -> bool>);
|
2018-11-09 18:40:06 +00:00
|
|
|
|
|
|
|
impl EventTrigger {
|
|
|
|
/// Create a new `EventTrigger` using the given function as filter.
|
|
|
|
pub fn from_fn<F>(f: F) -> Self
|
|
|
|
where
|
|
|
|
F: 'static + Fn(&Event) -> bool,
|
|
|
|
{
|
|
|
|
EventTrigger(Box::new(f))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks if this trigger applies to the given `Event`.
|
|
|
|
pub fn apply(&self, event: &Event) -> bool {
|
|
|
|
(self.0)(event)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an `EventTrigger` that only accepts arrow keys.
|
|
|
|
///
|
|
|
|
/// Only bare arrow keys without modifiers (Shift, Ctrl, Alt) will be accepted.
|
|
|
|
pub fn arrows() -> Self {
|
|
|
|
Self::from_fn(|e| match e {
|
|
|
|
Event::Key(Key::Left)
|
|
|
|
| Event::Key(Key::Down)
|
|
|
|
| Event::Key(Key::Up)
|
|
|
|
| Event::Key(Key::Right) => true,
|
|
|
|
_ => false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an `EventTrigger` that only accepts mouse events.
|
|
|
|
pub fn mouse() -> Self {
|
|
|
|
Self::from_fn(|e| match e {
|
|
|
|
Event::Mouse { .. } => true,
|
|
|
|
_ => false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an `EventTrigger` that accepts any event.
|
|
|
|
pub fn any() -> Self {
|
|
|
|
Self::from_fn(|_| true)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an `EventTrigger` that doesn't accept any event.
|
|
|
|
pub fn none() -> Self {
|
2018-11-09 19:27:46 +00:00
|
|
|
Self::from_fn(|_| false)
|
2018-11-09 18:40:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an `EventTrigger` that applies if either `self` or `other` applies.
|
|
|
|
pub fn or<O>(self, other: O) -> Self
|
|
|
|
where
|
|
|
|
O: Into<EventTrigger>,
|
|
|
|
{
|
|
|
|
let other = other.into();
|
|
|
|
Self::from_fn(move |e| self.apply(e) || other.apply(e))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Event> for EventTrigger {
|
|
|
|
fn from(event: Event) -> Self {
|
|
|
|
Self::from_fn(move |e| *e == event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<char> for EventTrigger {
|
|
|
|
fn from(c: char) -> Self {
|
|
|
|
Self::from(Event::from(c))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Key> for EventTrigger {
|
|
|
|
fn from(k: Key) -> Self {
|
|
|
|
Self::from(Event::from(k))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F> From<F> for EventTrigger
|
|
|
|
where
|
|
|
|
F: 'static + Fn(&Event) -> bool,
|
|
|
|
{
|
|
|
|
fn from(f: F) -> Self {
|
|
|
|
Self::from_fn(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-25 06:00:13 +00:00
|
|
|
impl Callback {
|
|
|
|
/// Wraps the given function into a `Callback` object.
|
2018-03-16 22:50:56 +00:00
|
|
|
pub fn from_fn<F>(f: F) -> Self
|
2017-12-15 07:45:14 +00:00
|
|
|
where
|
2018-03-16 22:50:56 +00:00
|
|
|
F: 'static + Fn(&mut Cursive),
|
2017-12-15 07:45:14 +00:00
|
|
|
{
|
|
|
|
Callback(Rc::new(Box::new(move |siv| {
|
|
|
|
f(siv);
|
|
|
|
})))
|
2016-07-25 06:00:13 +00:00
|
|
|
}
|
2018-03-15 18:42:49 +00:00
|
|
|
|
2018-07-25 22:00:29 +00:00
|
|
|
/// Wrap a `FnMut` into a `Callback` object.
|
|
|
|
///
|
|
|
|
/// If this methods tries to call itself, nested calls will be no-ops.
|
|
|
|
pub fn from_fn_mut<F>(f: F) -> Self
|
|
|
|
where
|
|
|
|
F: 'static + FnMut(&mut Cursive),
|
|
|
|
{
|
|
|
|
let cb = RefCell::new(f);
|
|
|
|
|
|
|
|
Self::from_fn(move |s| {
|
|
|
|
if let Ok(mut cb) = cb.try_borrow_mut() {
|
|
|
|
(&mut *cb)(s);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-03-15 18:42:49 +00:00
|
|
|
/// Returns a dummy callback that doesn't run anything.
|
|
|
|
pub fn dummy() -> Self {
|
|
|
|
Callback::from_fn(|_| ())
|
|
|
|
}
|
2016-07-25 06:00:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for Callback {
|
2019-02-28 23:55:02 +00:00
|
|
|
type Target = Box<dyn Fn(&mut Cursive)>;
|
|
|
|
fn deref<'a>(&'a self) -> &'a Box<dyn Fn(&mut Cursive)> {
|
2016-07-25 06:00:13 +00:00
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-28 23:55:02 +00:00
|
|
|
impl From<Rc<Box<dyn Fn(&mut Cursive)>>> for Callback {
|
|
|
|
fn from(f: Rc<Box<dyn Fn(&mut Cursive)>>) -> Self {
|
2016-07-25 06:00:13 +00:00
|
|
|
Callback(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-28 23:55:02 +00:00
|
|
|
impl From<Box<dyn Fn(&mut Cursive) + Send>> for Callback {
|
|
|
|
fn from(f: Box<dyn Fn(&mut Cursive) + Send>) -> Self {
|
2016-07-25 06:00:13 +00:00
|
|
|
Callback(Rc::new(f))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-28 23:55:02 +00:00
|
|
|
impl From<Box<dyn Fn(&mut Cursive)>> for Callback {
|
|
|
|
fn from(f: Box<dyn Fn(&mut Cursive)>) -> Self {
|
2016-07-25 06:00:13 +00:00
|
|
|
Callback(Rc::new(f))
|
|
|
|
}
|
|
|
|
}
|
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-25 06:00:13 +00:00
|
|
|
Consumed(Option<Callback>), // TODO: make this a FnOnce?
|
2016-07-02 22:02:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl EventResult {
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Convenient method to create `Consumed(Some(f))`
|
2018-03-16 22:50:56 +00:00
|
|
|
pub fn with_cb<F>(f: F) -> Self
|
2017-12-15 07:45:14 +00:00
|
|
|
where
|
2018-03-16 22:50:56 +00:00
|
|
|
F: 'static + Fn(&mut Cursive),
|
2017-12-15 07:45:14 +00:00
|
|
|
{
|
2016-07-25 06:00:13 +00:00
|
|
|
EventResult::Consumed(Some(Callback::from_fn(f)))
|
2016-07-02 22:02:42 +00:00
|
|
|
}
|
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,
|
2017-11-29 10:07:42 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` if `self` contains a callback.
|
|
|
|
pub fn has_callback(&self) -> bool {
|
|
|
|
match *self {
|
|
|
|
EventResult::Consumed(Some(_)) => true,
|
|
|
|
_ => false,
|
2016-07-16 20:25:21 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-12 00:12:00 +00:00
|
|
|
|
|
|
|
/// Process this result if it is a callback.
|
|
|
|
///
|
|
|
|
/// Does nothing otherwise.
|
|
|
|
pub fn process(self, s: &mut Cursive) {
|
|
|
|
if let EventResult::Consumed(Some(cb)) = self {
|
|
|
|
cb(s);
|
|
|
|
}
|
|
|
|
}
|
2017-08-23 23:43:17 +00:00
|
|
|
|
2018-07-08 19:48:37 +00:00
|
|
|
/// Returns `self` if it is not `EventResult::Ignored`, otherwise returns `f()`.
|
2017-08-23 23:43:17 +00:00
|
|
|
pub fn or_else<F>(self, f: F) -> Self
|
|
|
|
where
|
|
|
|
F: FnOnce() -> EventResult,
|
|
|
|
{
|
|
|
|
match self {
|
|
|
|
EventResult::Ignored => f(),
|
|
|
|
other => other,
|
|
|
|
}
|
|
|
|
}
|
2018-09-05 15:51:32 +00:00
|
|
|
|
|
|
|
/// Returns an event result that combines `self` and `other`.
|
|
|
|
pub fn and(self, other: Self) -> Self {
|
|
|
|
match (self, other) {
|
|
|
|
(EventResult::Ignored, result)
|
|
|
|
| (result, EventResult::Ignored) => result,
|
|
|
|
(EventResult::Consumed(None), EventResult::Consumed(cb))
|
|
|
|
| (EventResult::Consumed(cb), EventResult::Consumed(None)) => {
|
|
|
|
EventResult::Consumed(cb)
|
|
|
|
}
|
|
|
|
(
|
|
|
|
EventResult::Consumed(Some(cb1)),
|
|
|
|
EventResult::Consumed(Some(cb2)),
|
|
|
|
) => EventResult::with_cb(move |siv| {
|
|
|
|
(cb1)(siv);
|
|
|
|
(cb2)(siv);
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
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
|
2017-08-23 23:43:17 +00:00
|
|
|
#[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-10-09 23:52:23 +00:00
|
|
|
/// Pause Break key
|
|
|
|
PauseBreak,
|
|
|
|
|
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-10-09 23:52:23 +00:00
|
|
|
/// F0 key
|
|
|
|
F0,
|
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 {
|
2016-10-09 23:52:23 +00:00
|
|
|
0 => Key::F0,
|
2016-07-11 01:27:26 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-08 23:56:15 +00:00
|
|
|
/// One of the buttons present on the mouse
|
|
|
|
#[derive(PartialEq, Eq, Clone, Copy, Hash, Debug)]
|
|
|
|
pub enum MouseButton {
|
|
|
|
/// The left button, used for main actions.
|
|
|
|
Left,
|
|
|
|
/// Middle button, probably the wheel. Often pastes text in X11 on linux.
|
|
|
|
Middle,
|
|
|
|
/// The right button, for special actions.
|
|
|
|
Right,
|
|
|
|
|
2017-10-11 00:49:53 +00:00
|
|
|
/// Fourth button if the mouse supports it.
|
|
|
|
Button4,
|
|
|
|
/// Fifth button if the mouse supports it.
|
|
|
|
Button5,
|
|
|
|
|
2017-10-08 23:56:15 +00:00
|
|
|
// TODO: handle more buttons?
|
2018-02-17 01:21:24 +00:00
|
|
|
#[doc(hidden)]
|
|
|
|
Other,
|
2017-10-08 23:56:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Represents a possible event sent by the mouse.
|
|
|
|
#[derive(PartialEq, Eq, Clone, Copy, Hash, Debug)]
|
|
|
|
pub enum MouseEvent {
|
|
|
|
/// A button was pressed.
|
|
|
|
Press(MouseButton),
|
|
|
|
/// A button was released.
|
|
|
|
Release(MouseButton),
|
2017-10-11 00:49:53 +00:00
|
|
|
/// A button is being held.
|
|
|
|
Hold(MouseButton),
|
2017-10-08 23:56:15 +00:00
|
|
|
/// The wheel was moved up.
|
|
|
|
WheelUp,
|
|
|
|
/// The wheel was moved down.
|
|
|
|
WheelDown,
|
|
|
|
}
|
|
|
|
|
2017-10-11 00:49:53 +00:00
|
|
|
impl MouseEvent {
|
|
|
|
/// Returns the button used by this event, if any.
|
|
|
|
///
|
|
|
|
/// Returns `None` if `self` is `WheelUp` or `WheelDown`.
|
2018-06-21 21:44:30 +00:00
|
|
|
pub fn button(self) -> Option<MouseButton> {
|
|
|
|
match self {
|
2017-12-15 07:45:14 +00:00
|
|
|
MouseEvent::Press(btn)
|
|
|
|
| MouseEvent::Release(btn)
|
|
|
|
| MouseEvent::Hold(btn) => Some(btn),
|
2017-10-11 00:49:53 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2017-10-11 22:06:35 +00:00
|
|
|
|
|
|
|
/// Returns `true` if `self` is an event that can grab focus.
|
|
|
|
///
|
|
|
|
/// This includes `Press`, `WheelUp` and `WheelDown`.
|
|
|
|
pub fn grabs_focus(self) -> bool {
|
|
|
|
match self {
|
2017-12-15 07:45:14 +00:00
|
|
|
MouseEvent::Press(_)
|
|
|
|
| MouseEvent::WheelUp
|
|
|
|
| MouseEvent::WheelDown => true,
|
2017-10-11 22:06:35 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2017-10-11 00:49:53 +00:00
|
|
|
}
|
|
|
|
|
2015-06-08 22:38:10 +00:00
|
|
|
/// Represents an event as seen by the application.
|
2017-08-23 23:43:17 +00:00
|
|
|
#[derive(PartialEq, Eq, Clone, Hash, Debug)]
|
2015-05-28 01:04:33 +00:00
|
|
|
pub enum Event {
|
2016-07-25 06:00:13 +00:00
|
|
|
/// Event fired when the window is resized.
|
2016-07-11 01:27:26 +00:00
|
|
|
WindowResize,
|
2015-05-28 01:04:33 +00:00
|
|
|
|
2016-07-25 06:00:13 +00:00
|
|
|
/// Event fired regularly when a auto-refresh is set.
|
|
|
|
Refresh,
|
|
|
|
|
2017-10-08 23:56:15 +00:00
|
|
|
// TODO: have Char(modifier, char) and Key(modifier, key) enums?
|
2016-07-25 06:00:13 +00:00
|
|
|
/// A character was entered (includes numbers, punctuation, ...).
|
2016-07-11 01:27:26 +00:00
|
|
|
Char(char),
|
2016-07-25 06:00:13 +00:00
|
|
|
/// A character was entered with the Ctrl key pressed.
|
2016-07-11 01:27:26 +00:00
|
|
|
CtrlChar(char),
|
2016-07-25 06:00:13 +00:00
|
|
|
/// A character was entered with the Alt key pressed.
|
2016-07-11 01:27:26 +00:00
|
|
|
AltChar(char),
|
2015-05-28 01:04:33 +00:00
|
|
|
|
2016-07-25 06:00:13 +00:00
|
|
|
/// A non-character key was pressed.
|
2016-07-11 01:27:26 +00:00
|
|
|
Key(Key),
|
2016-07-25 06:00:13 +00:00
|
|
|
/// A non-character key was pressed with the Shift key pressed.
|
2016-07-11 01:27:26 +00:00
|
|
|
Shift(Key),
|
2016-07-25 06:00:13 +00:00
|
|
|
/// A non-character key was pressed with the Alt key pressed.
|
2016-07-11 01:27:26 +00:00
|
|
|
Alt(Key),
|
2016-07-25 06:00:13 +00:00
|
|
|
/// A non-character key was pressed with the Shift and Alt keys pressed.
|
2016-07-11 01:27:26 +00:00
|
|
|
AltShift(Key),
|
2016-07-25 06:00:13 +00:00
|
|
|
/// A non-character key was pressed with the Ctrl key pressed.
|
2016-07-11 01:27:26 +00:00
|
|
|
Ctrl(Key),
|
2016-07-25 06:00:13 +00:00
|
|
|
/// A non-character key was pressed with the Ctrl and Shift keys pressed.
|
2016-07-11 01:27:26 +00:00
|
|
|
CtrlShift(Key),
|
2016-07-25 06:00:13 +00:00
|
|
|
/// A non-character key was pressed with the Ctrl and Alt keys pressed.
|
2016-07-11 01:27:26 +00:00
|
|
|
CtrlAlt(Key),
|
|
|
|
|
2017-10-08 23:56:15 +00:00
|
|
|
/// A mouse event was sent.
|
|
|
|
Mouse {
|
|
|
|
/// Position of the top-left corner of the view receiving this event.
|
|
|
|
offset: Vec2,
|
|
|
|
/// Position of the mouse when this event was fired.
|
|
|
|
position: Vec2,
|
|
|
|
/// The mouse event itself.
|
|
|
|
event: MouseEvent,
|
|
|
|
},
|
|
|
|
|
|
|
|
// TODO: use a backend-dependent type for the unknown values?
|
2016-07-11 01:27:26 +00:00
|
|
|
/// An unknown event was received.
|
2017-03-25 18:01:25 +00:00
|
|
|
Unknown(Vec<u8>),
|
2016-10-12 00:47:44 +00:00
|
|
|
|
2017-12-30 21:55:04 +00:00
|
|
|
// Maybe add a `Custom(Rc<Any>)` ?
|
|
|
|
|
2017-10-15 04:18:50 +00:00
|
|
|
// Having a doc-hidden event prevents people from having exhaustive
|
|
|
|
// matches, allowing us to add events in the future.
|
2016-10-12 00:47:44 +00:00
|
|
|
#[doc(hidden)]
|
|
|
|
/// The application is about to exit.
|
|
|
|
Exit,
|
2015-05-28 01:04:33 +00:00
|
|
|
}
|
|
|
|
|
2017-10-11 18:13:22 +00:00
|
|
|
impl Event {
|
|
|
|
/// Returns the position of the mouse, if `self` is a mouse event.
|
|
|
|
pub fn mouse_position(&self) -> Option<Vec2> {
|
2017-10-13 18:22:02 +00:00
|
|
|
if let Event::Mouse { position, .. } = *self {
|
2018-04-17 05:39:16 +00:00
|
|
|
Some(position)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a mutable reference to the position of the mouse/
|
|
|
|
///
|
|
|
|
/// Returns `None` if `self` is not a mouse event.
|
|
|
|
pub fn mouse_position_mut(&mut self) -> Option<&mut Vec2> {
|
2018-05-18 00:37:39 +00:00
|
|
|
if let Event::Mouse {
|
|
|
|
ref mut position, ..
|
|
|
|
} = *self
|
|
|
|
{
|
2017-10-11 18:13:22 +00:00
|
|
|
Some(position)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Update `self` with the given offset.
|
|
|
|
///
|
|
|
|
/// If `self` is a mouse event, adds `top_left` to its offset.
|
|
|
|
/// Otherwise, do nothing.
|
|
|
|
pub fn relativize<V>(&mut self, top_left: V)
|
|
|
|
where
|
|
|
|
V: Into<Vec2>,
|
|
|
|
{
|
2018-05-18 00:37:39 +00:00
|
|
|
if let Event::Mouse { ref mut offset, .. } = *self {
|
2017-10-11 18:13:22 +00:00
|
|
|
*offset = *offset + top_left;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a cloned, relativized event.
|
|
|
|
///
|
|
|
|
/// If `self` is a mouse event, adds `top_left` to its offset.
|
|
|
|
/// Otherwise, returns a simple clone.
|
|
|
|
pub fn relativized<V>(&self, top_left: V) -> Self
|
|
|
|
where
|
|
|
|
V: Into<Vec2>,
|
|
|
|
{
|
|
|
|
let mut result = self.clone();
|
|
|
|
result.relativize(top_left);
|
|
|
|
result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|