on master help sign in
Add custom event type replacing crossterm's Event (#3169)
634b6d4Gokul Soumya
helix-term/src/application.rs+5−6
@@ -12,7 +12,7 @@ use serde_json::json;
12use crate::{
12use crate::{
13 args::Args,
13 args::Args,
14 commands::apply_workspace_edit,
14 commands::apply_workspace_edit,
15− compositor::Compositor,
15+ compositor::{Compositor, Event},
16 config::Config,
16 config::Config,
17 job::Jobs,
17 job::Jobs,
18 keymap::Keymaps,
18 keymap::Keymaps,
@@ -29,7 +29,7 @@ use std::{
29use anyhow::{Context, Error};
29use anyhow::{Context, Error};
30
30
31use crossterm::{
31use crossterm::{
32− event::{DisableMouseCapture, EnableMouseCapture, Event},
32+ event::{DisableMouseCapture, EnableMouseCapture, Event as CrosstermEvent},
33 execute, terminal,
33 execute, terminal,
34 tty::IsTty,
34 tty::IsTty,
35};
35};
@@ -418,7 +418,7 @@ impl Application {
418 }
418 }
419 }
419 }
420
420
421− pub fn handle_terminal_events(&mut self, event: Result<Event, crossterm::ErrorKind>) {
421+ pub fn handle_terminal_events(&mut self, event: Result<CrosstermEvent, crossterm::ErrorKind>) {
422 let mut cx = crate::compositor::Context {
422 let mut cx = crate::compositor::Context {
423 editor: &mut self.editor,
423 editor: &mut self.editor,
424 jobs: &mut self.jobs,
424 jobs: &mut self.jobs,
@@ -426,13 +426,12 @@ impl Application {
426 };
426 };
427 // Handle key events
427 // Handle key events
428 let should_redraw = match event {
428 let should_redraw = match event {
429− Ok(Event::Resize(width, height)) => {
429+ Ok(CrosstermEvent::Resize(width, height)) => {
430 self.compositor.resize(width, height);
430 self.compositor.resize(width, height);
431−
432 self.compositor
431 self.compositor
433 .handle_event(Event::Resize(width, height), &mut cx)
432 .handle_event(Event::Resize(width, height), &mut cx)
434 }
433 }
435− Ok(event) => self.compositor.handle_event(event, &mut cx),
434+ Ok(event) => self.compositor.handle_event(event.into(), &mut cx),
436 Err(x) => panic!("{}", x),
435 Err(x) => panic!("{}", x),
437 };
436 };
438
437
helix-term/src/commands.rs+1−1
@@ -4846,7 +4846,7 @@ fn replay_macro(cx: &mut Context) {
4846 cx.callback = Some(Box::new(move |compositor, cx| {
4846 cx.callback = Some(Box::new(move |compositor, cx| {
4847 for _ in 0..count {
4847 for _ in 0..count {
4848 for &key in keys.iter() {
4848 for &key in keys.iter() {
4849− compositor.handle_event(crossterm::event::Event::Key(key.into()), cx);
4849+ compositor.handle_event(compositor::Event::Key(key), cx);
4850 }
4850 }
4851 }
4851 }
4852 // The macro under replay is cleared at the end of the callback, not in the
4852 // The macro under replay is cleared at the end of the callback, not in the
helix-term/src/compositor.rs+3−4
@@ -4,8 +4,6 @@
4use helix_core::Position;
4use helix_core::Position;
5use helix_view::graphics::{CursorKind, Rect};
5use helix_view::graphics::{CursorKind, Rect};
6
6
7−use crossterm::event::Event;
8−
9#[cfg(feature = "integration")]
7#[cfg(feature = "integration")]
10use tui::backend::TestBackend;
8use tui::backend::TestBackend;
11use tui::buffer::Buffer as Surface;
9use tui::buffer::Buffer as Surface;
@@ -18,9 +16,10 @@ pub enum EventResult {
18 Consumed(Option<Callback>),
16 Consumed(Option<Callback>),
19}
17}
20
18
19+use crate::job::Jobs;
21use helix_view::Editor;
20use helix_view::Editor;
22
21
23−use crate::job::Jobs;
22+pub use helix_view::input::Event;
24
23
25pub struct Context<'a> {
24pub struct Context<'a> {
26 pub editor: &'a mut Editor,
25 pub editor: &'a mut Editor,
@@ -161,7 +160,7 @@ impl Compositor {
161 pub fn handle_event(&mut self, event: Event, cx: &mut Context) -> bool {
160 pub fn handle_event(&mut self, event: Event, cx: &mut Context) -> bool {
162 // If it is a key event and a macro is being recorded, push the key event to the recording.
161 // If it is a key event and a macro is being recorded, push the key event to the recording.
163 if let (Event::Key(key), Some((_, keys))) = (event, &mut cx.editor.macro_recording) {
162 if let (Event::Key(key), Some((_, keys))) = (event, &mut cx.editor.macro_recording) {
164− keys.push(key.into());
163+ keys.push(key);
165 }
164 }
166
165
167 let mut callbacks = Vec::new();
166 let mut callbacks = Vec::new();
helix-term/src/ui/completion.rs+6−3
@@ -1,5 +1,4 @@
1−use crate::compositor::{Component, Context, EventResult};
1+use crate::compositor::{Component, Context, Event, EventResult};
2−use crossterm::event::{Event, KeyCode, KeyEvent};
3use helix_view::editor::CompleteAction;
2use helix_view::editor::CompleteAction;
4use tui::buffer::Buffer as Surface;
3use tui::buffer::Buffer as Surface;
5use tui::text::Spans;
4use tui::text::Spans;
@@ -7,7 +6,11 @@ use tui::text::Spans;
7use std::borrow::Cow;
6use std::borrow::Cow;
8
7
9use helix_core::{Change, Transaction};
8use helix_core::{Change, Transaction};
9+use helix_view::{
10+ graphics::Rect,
11+ input::{KeyCode, KeyEvent},
10−use helix_view::{graphics::Rect, Document, Editor};
12+ Document, Editor,
13+};
11
14
12use crate::commands;
15use crate::commands;
13use crate::ui::{menu, Markdown, Menu, Popup, PromptEvent};
16use crate::ui::{menu, Markdown, Menu, Popup, PromptEvent};
helix-term/src/ui/editor.rs+6−8
@@ -1,6 +1,6 @@
1use crate::{
1use crate::{
2 commands,
2 commands,
3− compositor::{Component, Context, EventResult},
3+ compositor::{Component, Context, Event, EventResult},
4 job, key,
4 job, key,
5 keymap::{KeymapResult, Keymaps},
5 keymap::{KeymapResult, Keymaps},
6 ui::{Completion, ProgressSpinners},
6 ui::{Completion, ProgressSpinners},
@@ -19,13 +19,12 @@ use helix_view::{
19 document::Mode,
19 document::Mode,
20 editor::{CompleteAction, CursorShapeConfig},
20 editor::{CompleteAction, CursorShapeConfig},
21 graphics::{Color, CursorKind, Modifier, Rect, Style},
21 graphics::{Color, CursorKind, Modifier, Rect, Style},
22− input::KeyEvent,
22+ input::{KeyEvent, MouseButton, MouseEvent, MouseEventKind},
23 keyboard::{KeyCode, KeyModifiers},
23 keyboard::{KeyCode, KeyModifiers},
24 Document, Editor, Theme, View,
24 Document, Editor, Theme, View,
25};
25};
26use std::borrow::Cow;
26use std::borrow::Cow;
27
27
28−use crossterm::event::{Event, MouseButton, MouseEvent, MouseEventKind};
29use tui::buffer::Buffer as Surface;
28use tui::buffer::Buffer as Surface;
30
29
31use super::lsp::SignatureHelp;
30use super::lsp::SignatureHelp;
@@ -966,7 +965,7 @@ impl EditorView {
966 if let Some((pos, view_id)) = pos_and_view(editor, row, column) {
965 if let Some((pos, view_id)) = pos_and_view(editor, row, column) {
967 let doc = editor.document_mut(editor.tree.get(view_id).doc).unwrap();
966 let doc = editor.document_mut(editor.tree.get(view_id).doc).unwrap();
968
967
969− if modifiers == crossterm::event::KeyModifiers::ALT {
968+ if modifiers == KeyModifiers::ALT {
970 let selection = doc.selection(view_id).clone();
969 let selection = doc.selection(view_id).clone();
971 doc.set_selection(view_id, selection.push(Range::point(pos)));
970 doc.set_selection(view_id, selection.push(Range::point(pos)));
972 } else {
971 } else {
@@ -1068,7 +1067,7 @@ impl EditorView {
1068 let line = coords.row + view.offset.row;
1067 let line = coords.row + view.offset.row;
1069 if let Ok(pos) = doc.text().try_line_to_char(line) {
1068 if let Ok(pos) = doc.text().try_line_to_char(line) {
1070 doc.set_selection(view_id, Selection::point(pos));
1069 doc.set_selection(view_id, Selection::point(pos));
1071− if modifiers == crossterm::event::KeyModifiers::ALT {
1070+ if modifiers == KeyModifiers::ALT {
1072 commands::MappableCommand::dap_edit_log.execute(cxt);
1071 commands::MappableCommand::dap_edit_log.execute(cxt);
1073 } else {
1072 } else {
1074 commands::MappableCommand::dap_edit_condition.execute(cxt);
1073 commands::MappableCommand::dap_edit_condition.execute(cxt);
@@ -1087,7 +1086,7 @@ impl EditorView {
1087 return EventResult::Ignored(None);
1086 return EventResult::Ignored(None);
1088 }
1087 }
1089
1088
1090− if modifiers == crossterm::event::KeyModifiers::ALT {
1089+ if modifiers == KeyModifiers::ALT {
1091 commands::MappableCommand::replace_selections_with_primary_clipboard
1090 commands::MappableCommand::replace_selections_with_primary_clipboard
1092 .execute(cxt);
1091 .execute(cxt);
1093
1092
@@ -1132,9 +1131,8 @@ impl Component for EditorView {
1132 // Handling it here but not re-rendering will cause flashing
1131 // Handling it here but not re-rendering will cause flashing
1133 EventResult::Consumed(None)
1132 EventResult::Consumed(None)
1134 }
1133 }
1135− Event::Key(key) => {
1134+ Event::Key(mut key) => {
1136 cx.editor.reset_idle_timer();
1135 cx.editor.reset_idle_timer();
1137− let mut key = KeyEvent::from(key);
1138 canonicalize_key(&mut key);
1136 canonicalize_key(&mut key);
1139
1137
1140 // clear status
1138 // clear status
helix-term/src/ui/menu.rs+2−3
@@ -1,10 +1,9 @@
1use std::{borrow::Cow, path::PathBuf};
1use std::{borrow::Cow, path::PathBuf};
2
2
3use crate::{
3use crate::{
4− compositor::{Callback, Component, Compositor, Context, EventResult},
4+ compositor::{Callback, Component, Compositor, Context, Event, EventResult},
5 ctrl, key, shift,
5 ctrl, key, shift,
6};
6};
7−use crossterm::event::Event;
8use tui::{buffer::Buffer as Surface, text::Spans, widgets::Table};
7use tui::{buffer::Buffer as Surface, text::Spans, widgets::Table};
9
8
10pub use tui::widgets::{Cell, Row};
9pub use tui::widgets::{Cell, Row};
@@ -237,7 +236,7 @@ impl<T: Item + 'static> Component for Menu<T> {
237 compositor.pop();
236 compositor.pop();
238 }));
237 }));
239
238
240− match event.into() {
239+ match event {
241 // esc or ctrl-c aborts the completion and closes the menu
240 // esc or ctrl-c aborts the completion and closes the menu
242 key!(Esc) | ctrl!('c') => {
241 key!(Esc) | ctrl!('c') => {
243 (self.callback_fn)(cx.editor, self.selection(), MenuEvent::Abort);
242 (self.callback_fn)(cx.editor, self.selection(), MenuEvent::Abort);
helix-term/src/ui/overlay.rs+1−2
@@ -1,4 +1,3 @@
1−use crossterm::event::Event;
2use helix_core::Position;
1use helix_core::Position;
3use helix_view::{
2use helix_view::{
4 graphics::{CursorKind, Rect},
3 graphics::{CursorKind, Rect},
@@ -6,7 +5,7 @@ use helix_view::{
6};
5};
7use tui::buffer::Buffer;
6use tui::buffer::Buffer;
8
7
9−use crate::compositor::{Component, Context, EventResult};
8+use crate::compositor::{Component, Context, Event, EventResult};
10
9
11/// Contains a component placed in the center of the parent component
10/// Contains a component placed in the center of the parent component
12pub struct Overlay<T> {
11pub struct Overlay<T> {
helix-term/src/ui/picker.rs+2−3
@@ -1,9 +1,8 @@
1use crate::{
1use crate::{
2− compositor::{Component, Compositor, Context, EventResult},
2+ compositor::{Component, Compositor, Context, Event, EventResult},
3 ctrl, key, shift,
3 ctrl, key, shift,
4 ui::{self, EditorView},
4 ui::{self, EditorView},
5};
5};
6−use crossterm::event::Event;
7use tui::{
6use tui::{
8 buffer::Buffer as Surface,
7 buffer::Buffer as Surface,
9 widgets::{Block, BorderType, Borders},
8 widgets::{Block, BorderType, Borders},
@@ -502,7 +501,7 @@ impl<T: Item + 'static> Component for Picker<T> {
502 compositor.last_picker = compositor.pop();
501 compositor.last_picker = compositor.pop();
503 })));
502 })));
504
503
505− match key_event.into() {
504+ match key_event {
506 shift!(Tab) | key!(Up) | ctrl!('p') => {
505 shift!(Tab) | key!(Up) | ctrl!('p') => {
507 self.move_by(1, Direction::Backward);
506 self.move_by(1, Direction::Backward);
508 }
507 }
helix-term/src/ui/popup.rs+3−4
@@ -1,9 +1,8 @@
1use crate::{
1use crate::{
2 commands::Open,
2 commands::Open,
3− compositor::{Callback, Component, Context, EventResult},
3+ compositor::{Callback, Component, Context, Event, EventResult},
4 ctrl, key,
4 ctrl, key,
5};
5};
6−use crossterm::event::Event;
7use tui::buffer::Buffer as Surface;
6use tui::buffer::Buffer as Surface;
8
7
9use helix_core::Position;
8use helix_core::Position;
@@ -149,7 +148,7 @@ impl<T: Component> Component for Popup<T> {
149 _ => return EventResult::Ignored(None),
148 _ => return EventResult::Ignored(None),
150 };
149 };
151
150
152− if key!(Esc) == key.into() && self.ignore_escape_key {
151+ if key!(Esc) == key && self.ignore_escape_key {
153 return EventResult::Ignored(None);
152 return EventResult::Ignored(None);
154 }
153 }
155
154
@@ -158,7 +157,7 @@ impl<T: Component> Component for Popup<T> {
158 compositor.remove(self.id.as_ref());
157 compositor.remove(self.id.as_ref());
159 });
158 });
160
159
161− match key.into() {
160+ match key {
162 // esc or ctrl-c aborts the completion and closes the menu
161 // esc or ctrl-c aborts the completion and closes the menu
163 key!(Esc) | ctrl!('c') => {
162 key!(Esc) | ctrl!('c') => {
164 let _ = self.contents.handle_event(event, cx);
163 let _ = self.contents.handle_event(event, cx);
helix-term/src/ui/prompt.rs+2−3
@@ -1,6 +1,5 @@
1−use crate::compositor::{Component, Compositor, Context, EventResult};
1+use crate::compositor::{Component, Compositor, Context, Event, EventResult};
2use crate::{alt, ctrl, key, shift, ui};
2use crate::{alt, ctrl, key, shift, ui};
3−use crossterm::event::Event;
4use helix_view::input::KeyEvent;
3use helix_view::input::KeyEvent;
5use helix_view::keyboard::KeyCode;
4use helix_view::keyboard::KeyCode;
6use std::{borrow::Cow, ops::RangeFrom};
5use std::{borrow::Cow, ops::RangeFrom};
@@ -479,7 +478,7 @@ impl Component for Prompt {
479 compositor.pop();
478 compositor.pop();
480 })));
479 })));
481
480
482− match event.into() {
481+ match event {
483 ctrl!('c') | key!(Esc) => {
482 ctrl!('c') | key!(Esc) => {
484 (self.callback_fn)(cx, &self.line, PromptEvent::Abort);
483 (self.callback_fn)(cx, &self.line, PromptEvent::Abort);
485 return close_fn;
484 return close_fn;
helix-view/src/input.rs+101−1
@@ -4,8 +4,53 @@ use helix_core::unicode::{segmentation::UnicodeSegmentation, width::UnicodeWidth
4use serde::de::{self, Deserialize, Deserializer};
4use serde::de::{self, Deserialize, Deserializer};
5use std::fmt;
5use std::fmt;
6
6
7−use crate::keyboard::{KeyCode, KeyModifiers};
7+pub use crate::keyboard::{KeyCode, KeyModifiers};
8
8
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+}
9/// Represents a key event.
54/// Represents a key event.
10// We use a newtype here because we want to customize Deserialize and Display.
55// We use a newtype here because we want to customize Deserialize and Display.
11#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)]
56#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)]
@@ -219,6 +264,61 @@ impl<'de> Deserialize<'de> for KeyEvent {
219 }
264 }
220}
265}
221
266
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+
222#[cfg(feature = "term")]
322#[cfg(feature = "term")]
223impl From<crossterm::event::KeyEvent> for KeyEvent {
323impl From<crossterm::event::KeyEvent> for KeyEvent {
224 fn from(crossterm::event::KeyEvent { code, modifiers }: crossterm::event::KeyEvent) -> Self {
324 fn from(crossterm::event::KeyEvent { code, modifiers }: crossterm::event::KeyEvent) -> Self {
helix-view/src/keyboard.rs−1
@@ -55,7 +55,6 @@ impl From<crossterm::event::KeyModifiers> for KeyModifiers {
55
55
56/// Represents a key.
56/// Represents a key.
57#[derive(Debug, PartialOrd, Ord, PartialEq, Eq, Clone, Copy, Hash)]
57#[derive(Debug, PartialOrd, Ord, PartialEq, Eq, Clone, Copy, Hash)]
58−#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
59pub enum KeyCode {
58pub enum KeyCode {
60 /// Backspace key.
59 /// Backspace key.
61 Backspace,
60 Backspace,