//! Typed terminal events. //! //! Events are the input vocabulary consumed by [`App`](crate::App): key //! presses, mouse input, resizes, and caller-defined custom events. Concrete //! backends translate their native event streams into these values. use crate::Size; use crate::layout::Point; /// Input or application event. #[derive(Debug, Clone, PartialEq, Eq)] pub enum Event { /// Key input. Key(Key), /// Mouse input. Mouse(Mouse), /// Terminal resize. Resize(Size), /// Bracketed paste or pasted text. Paste(String), /// Timer tick. Tick, /// User-defined event. User(User), /// Unsupported terminal input. Unsupported, } /// Keyboard input. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum Key { /// Printable character. Char(char), /// Control character. Ctrl(char), /// Alt-modified character. Alt(char), /// Escape. Esc, /// Enter. Enter, /// Tab. Tab, /// Back-tab. BackTab, /// Backspace. Backspace, /// Delete. Delete, /// Insert. Insert, /// Left arrow. Left, /// Right arrow. Right, /// Up arrow. Up, /// Down arrow. Down, /// Home. Home, /// End. End, /// Page up. PageUp, /// Page down. PageDown, /// Function key. F(u8), /// Null key. Null, /// Unknown key. Unknown, } /// Mouse input. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum Mouse { /// Button press. Press { /// Pressed button. button: MouseButton, /// Position. at: Point, /// Active modifier keys. modifiers: Modifiers, }, /// Button release. Release { /// Position. at: Point, /// Active modifier keys. modifiers: Modifiers, }, /// Mouse drag or hold. Hold { /// Position. at: Point, /// Active modifier keys. modifiers: Modifiers, }, } /// Mouse button. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum MouseButton { /// Left button. Left, /// Right button. Right, /// Middle button. Middle, /// Wheel up. WheelUp, /// Wheel down. WheelDown, /// Unknown button. Unknown, } /// Active keyboard modifiers attached to an input event. #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)] pub struct Modifiers { /// Packed modifier flags. bits: u8, } impl Modifiers { /// No modifiers are active. pub const NONE: Self = Self { bits: 0 }; /// Shift is active. pub const SHIFT: Self = Self { bits: 1 << 0 }; /// Alt or Meta is active. pub const ALT: Self = Self { bits: 1 << 1 }; /// Control is active. pub const CONTROL: Self = Self { bits: 1 << 2 }; /// Returns whether all bits in `modifier` are active. pub const fn contains(self, modifier: Self) -> bool { self.bits & modifier.bits == modifier.bits } /// Returns whether no modifiers are active. pub const fn is_empty(self) -> bool { self.bits == 0 } } impl std::ops::BitOr for Modifiers { type Output = Self; fn bitor(self, rhs: Self) -> Self::Output { Self { bits: self.bits | rhs.bits, } } } impl std::ops::BitOrAssign for Modifiers { fn bitor_assign(&mut self, rhs: Self) { self.bits |= rhs.bits; } }