@@ -4,8 +4,53 @@ use helix_core::unicode::{segmentation::UnicodeSegmentation, width::UnicodeWidth
44use serde::de::{self, Deserialize, Deserializer};
55use std::fmt;
66
7−use crate::keyboard::{KeyCode, KeyModifiers};
7+pub use crate::keyboard::{KeyCode, KeyModifiers};
88
9+#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash)]
10+pub enum Event {
11+ Key(KeyEvent),
12+ Mouse(MouseEvent),
13+ Resize(u16, u16),
14+}
15+
16+#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash)]
17+pub struct MouseEvent {
18+ /// The kind of mouse event that was caused.
19+ pub kind: MouseEventKind,
20+ /// The column that the event occurred on.
21+ pub column: u16,
22+ /// The row that the event occurred on.
23+ pub row: u16,
24+ /// The key modifiers active when the event occurred.
25+ pub modifiers: KeyModifiers,
26+}
27+
28+#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash)]
29+pub enum MouseEventKind {
30+ /// Pressed mouse button. Contains the button that was pressed.
31+ Down(MouseButton),
32+ /// Released mouse button. Contains the button that was released.
33+ Up(MouseButton),
34+ /// Moved the mouse cursor while pressing the contained mouse button.
35+ Drag(MouseButton),
36+ /// Moved the mouse cursor while not pressing a mouse button.
37+ Moved,
38+ /// Scrolled mouse wheel downwards (towards the user).
39+ ScrollDown,
40+ /// Scrolled mouse wheel upwards (away from the user).
41+ ScrollUp,
42+}
43+
44+/// Represents a mouse button.
45+#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash)]
46+pub enum MouseButton {
47+ /// Left mouse button.
48+ Left,
49+ /// Right mouse button.
50+ Right,
51+ /// Middle mouse button.
52+ Middle,
53+}
954/// Represents a key event.
1055// We use a newtype here because we want to customize Deserialize and Display.
1156#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)]
@@ -219,6 +264,61 @@ impl<'de> Deserialize<'de> for KeyEvent {
219264 }
220265}
221266
267+#[cfg(feature = "term")]
268+impl From<crossterm::event::Event> for Event {
269+ fn from(event: crossterm::event::Event) -> Self {
270+ match event {
271+ crossterm::event::Event::Key(key) => Self::Key(key.into()),
272+ crossterm::event::Event::Mouse(mouse) => Self::Mouse(mouse.into()),
273+ crossterm::event::Event::Resize(w, h) => Self::Resize(w, h),
274+ }
275+ }
276+}
277+
278+#[cfg(feature = "term")]
279+impl From<crossterm::event::MouseEvent> for MouseEvent {
280+ fn from(
281+ crossterm::event::MouseEvent {
282+ kind,
283+ column,
284+ row,
285+ modifiers,
286+ }: crossterm::event::MouseEvent,
287+ ) -> Self {
288+ Self {
289+ kind: kind.into(),
290+ column,
291+ row,
292+ modifiers: modifiers.into(),
293+ }
294+ }
295+}
296+
297+#[cfg(feature = "term")]
298+impl From<crossterm::event::MouseEventKind> for MouseEventKind {
299+ fn from(kind: crossterm::event::MouseEventKind) -> Self {
300+ match kind {
301+ crossterm::event::MouseEventKind::Down(button) => Self::Down(button.into()),
302+ crossterm::event::MouseEventKind::Up(button) => Self::Down(button.into()),
303+ crossterm::event::MouseEventKind::Drag(button) => Self::Drag(button.into()),
304+ crossterm::event::MouseEventKind::Moved => Self::Moved,
305+ crossterm::event::MouseEventKind::ScrollDown => Self::ScrollDown,
306+ crossterm::event::MouseEventKind::ScrollUp => Self::ScrollUp,
307+ }
308+ }
309+}
310+
311+#[cfg(feature = "term")]
312+impl From<crossterm::event::MouseButton> for MouseButton {
313+ fn from(button: crossterm::event::MouseButton) -> Self {
314+ match button {
315+ crossterm::event::MouseButton::Left => MouseButton::Left,
316+ crossterm::event::MouseButton::Right => MouseButton::Right,
317+ crossterm::event::MouseButton::Middle => MouseButton::Middle,
318+ }
319+ }
320+}
321+
222322#[cfg(feature = "term")]
223323impl From<crossterm::event::KeyEvent> for KeyEvent {
224324 fn from(crossterm::event::KeyEvent { code, modifiers }: crossterm::event::KeyEvent) -> Self {