aboutsummaryrefslogtreecommitdiff
path: root/helix-view/src/input.rs
diff options
context:
space:
mode:
authorGokul Soumya2022-08-09 01:31:26 +0000
committerGitHub2022-08-09 01:31:26 +0000
commit634b6d455f064116c609e7cfa1b2b45e51f40029 (patch)
treead06ce4c4f0131d74cc19b6beed000ff043664d8 /helix-view/src/input.rs
parent4ce5a94552d887b9b14000dfdd1abe76a6d5f315 (diff)
Add custom event type replacing crossterm's Event (#3169)
Ported over from 61365dfbf3 in the `gui` branch. This will allow adding our own events, most notably an idle timer event (useful for adding debounced input in [dynamic pickers][1] used by interactive global search and workspace symbols). [1]: https://github.com/helix-editor/helix/pull/3110 Co-authored-by: Blaž Hrastnik <blaz@mxxn.io>
Diffstat (limited to 'helix-view/src/input.rs')
-rw-r--r--helix-view/src/input.rs102
1 files changed, 101 insertions, 1 deletions
diff --git a/helix-view/src/input.rs b/helix-view/src/input.rs
index 093006c4..9ae3ce70 100644
--- a/helix-view/src/input.rs
+++ b/helix-view/src/input.rs
@@ -4,8 +4,53 @@ use helix_core::unicode::{segmentation::UnicodeSegmentation, width::UnicodeWidth
use serde::de::{self, Deserialize, Deserializer};
use std::fmt;
-use crate::keyboard::{KeyCode, KeyModifiers};
+pub use crate::keyboard::{KeyCode, KeyModifiers};
+#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash)]
+pub enum Event {
+ Key(KeyEvent),
+ Mouse(MouseEvent),
+ Resize(u16, u16),
+}
+
+#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash)]
+pub struct MouseEvent {
+ /// The kind of mouse event that was caused.
+ pub kind: MouseEventKind,
+ /// The column that the event occurred on.
+ pub column: u16,
+ /// The row that the event occurred on.
+ pub row: u16,
+ /// The key modifiers active when the event occurred.
+ pub modifiers: KeyModifiers,
+}
+
+#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash)]
+pub enum MouseEventKind {
+ /// Pressed mouse button. Contains the button that was pressed.
+ Down(MouseButton),
+ /// Released mouse button. Contains the button that was released.
+ Up(MouseButton),
+ /// Moved the mouse cursor while pressing the contained mouse button.
+ Drag(MouseButton),
+ /// Moved the mouse cursor while not pressing a mouse button.
+ Moved,
+ /// Scrolled mouse wheel downwards (towards the user).
+ ScrollDown,
+ /// Scrolled mouse wheel upwards (away from the user).
+ ScrollUp,
+}
+
+/// Represents a mouse button.
+#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash)]
+pub enum MouseButton {
+ /// Left mouse button.
+ Left,
+ /// Right mouse button.
+ Right,
+ /// Middle mouse button.
+ Middle,
+}
/// Represents a key event.
// We use a newtype here because we want to customize Deserialize and Display.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)]
@@ -220,6 +265,61 @@ impl<'de> Deserialize<'de> for KeyEvent {
}
#[cfg(feature = "term")]
+impl From<crossterm::event::Event> for Event {
+ fn from(event: crossterm::event::Event) -> Self {
+ match event {
+ crossterm::event::Event::Key(key) => Self::Key(key.into()),
+ crossterm::event::Event::Mouse(mouse) => Self::Mouse(mouse.into()),
+ crossterm::event::Event::Resize(w, h) => Self::Resize(w, h),
+ }
+ }
+}
+
+#[cfg(feature = "term")]
+impl From<crossterm::event::MouseEvent> for MouseEvent {
+ fn from(
+ crossterm::event::MouseEvent {
+ kind,
+ column,
+ row,
+ modifiers,
+ }: crossterm::event::MouseEvent,
+ ) -> Self {
+ Self {
+ kind: kind.into(),
+ column,
+ row,
+ modifiers: modifiers.into(),
+ }
+ }
+}
+
+#[cfg(feature = "term")]
+impl From<crossterm::event::MouseEventKind> for MouseEventKind {
+ fn from(kind: crossterm::event::MouseEventKind) -> Self {
+ match kind {
+ crossterm::event::MouseEventKind::Down(button) => Self::Down(button.into()),
+ crossterm::event::MouseEventKind::Up(button) => Self::Down(button.into()),
+ crossterm::event::MouseEventKind::Drag(button) => Self::Drag(button.into()),
+ crossterm::event::MouseEventKind::Moved => Self::Moved,
+ crossterm::event::MouseEventKind::ScrollDown => Self::ScrollDown,
+ crossterm::event::MouseEventKind::ScrollUp => Self::ScrollUp,
+ }
+ }
+}
+
+#[cfg(feature = "term")]
+impl From<crossterm::event::MouseButton> for MouseButton {
+ fn from(button: crossterm::event::MouseButton) -> Self {
+ match button {
+ crossterm::event::MouseButton::Left => MouseButton::Left,
+ crossterm::event::MouseButton::Right => MouseButton::Right,
+ crossterm::event::MouseButton::Middle => MouseButton::Middle,
+ }
+ }
+}
+
+#[cfg(feature = "term")]
impl From<crossterm::event::KeyEvent> for KeyEvent {
fn from(crossterm::event::KeyEvent { code, modifiers }: crossterm::event::KeyEvent) -> Self {
if code == crossterm::event::KeyCode::BackTab {