Skip to content

Commit 634b6d4

Browse files
Add custom event type replacing crossterm's Event (#3169)
Ported over from 61365df 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]: #3110 Co-authored-by: Blaž Hrastnik <[email protected]>
1 parent 4ce5a94 commit 634b6d4

File tree

12 files changed

+132
-39
lines changed

12 files changed

+132
-39
lines changed

helix-term/src/application.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use serde_json::json;
1212
use crate::{
1313
args::Args,
1414
commands::apply_workspace_edit,
15-
compositor::Compositor,
15+
compositor::{Compositor, Event},
1616
config::Config,
1717
job::Jobs,
1818
keymap::Keymaps,
@@ -29,7 +29,7 @@ use std::{
2929
use anyhow::{Context, Error};
3030

3131
use crossterm::{
32-
event::{DisableMouseCapture, EnableMouseCapture, Event},
32+
event::{DisableMouseCapture, EnableMouseCapture, Event as CrosstermEvent},
3333
execute, terminal,
3434
tty::IsTty,
3535
};
@@ -418,21 +418,20 @@ impl Application {
418418
}
419419
}
420420

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>) {
422422
let mut cx = crate::compositor::Context {
423423
editor: &mut self.editor,
424424
jobs: &mut self.jobs,
425425
scroll: None,
426426
};
427427
// Handle key events
428428
let should_redraw = match event {
429-
Ok(Event::Resize(width, height)) => {
429+
Ok(CrosstermEvent::Resize(width, height)) => {
430430
self.compositor.resize(width, height);
431-
432431
self.compositor
433432
.handle_event(Event::Resize(width, height), &mut cx)
434433
}
435-
Ok(event) => self.compositor.handle_event(event, &mut cx),
434+
Ok(event) => self.compositor.handle_event(event.into(), &mut cx),
436435
Err(x) => panic!("{}", x),
437436
};
438437

helix-term/src/commands.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4846,7 +4846,7 @@ fn replay_macro(cx: &mut Context) {
48464846
cx.callback = Some(Box::new(move |compositor, cx| {
48474847
for _ in 0..count {
48484848
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);
48504850
}
48514851
}
48524852
// The macro under replay is cleared at the end of the callback, not in the

helix-term/src/compositor.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
use helix_core::Position;
55
use helix_view::graphics::{CursorKind, Rect};
66

7-
use crossterm::event::Event;
8-
97
#[cfg(feature = "integration")]
108
use tui::backend::TestBackend;
119
use tui::buffer::Buffer as Surface;
@@ -18,9 +16,10 @@ pub enum EventResult {
1816
Consumed(Option<Callback>),
1917
}
2018

19+
use crate::job::Jobs;
2120
use helix_view::Editor;
2221

23-
use crate::job::Jobs;
22+
pub use helix_view::input::Event;
2423

2524
pub struct Context<'a> {
2625
pub editor: &'a mut Editor,
@@ -161,7 +160,7 @@ impl Compositor {
161160
pub fn handle_event(&mut self, event: Event, cx: &mut Context) -> bool {
162161
// If it is a key event and a macro is being recorded, push the key event to the recording.
163162
if let (Event::Key(key), Some((_, keys))) = (event, &mut cx.editor.macro_recording) {
164-
keys.push(key.into());
163+
keys.push(key);
165164
}
166165

167166
let mut callbacks = Vec::new();

helix-term/src/ui/completion.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
use crate::compositor::{Component, Context, EventResult};
2-
use crossterm::event::{Event, KeyCode, KeyEvent};
1+
use crate::compositor::{Component, Context, Event, EventResult};
32
use helix_view::editor::CompleteAction;
43
use tui::buffer::Buffer as Surface;
54
use tui::text::Spans;
65

76
use std::borrow::Cow;
87

98
use helix_core::{Change, Transaction};
10-
use helix_view::{graphics::Rect, Document, Editor};
9+
use helix_view::{
10+
graphics::Rect,
11+
input::{KeyCode, KeyEvent},
12+
Document, Editor,
13+
};
1114

1215
use crate::commands;
1316
use crate::ui::{menu, Markdown, Menu, Popup, PromptEvent};

helix-term/src/ui/editor.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
commands,
3-
compositor::{Component, Context, EventResult},
3+
compositor::{Component, Context, Event, EventResult},
44
job, key,
55
keymap::{KeymapResult, Keymaps},
66
ui::{Completion, ProgressSpinners},
@@ -19,13 +19,12 @@ use helix_view::{
1919
document::Mode,
2020
editor::{CompleteAction, CursorShapeConfig},
2121
graphics::{Color, CursorKind, Modifier, Rect, Style},
22-
input::KeyEvent,
22+
input::{KeyEvent, MouseButton, MouseEvent, MouseEventKind},
2323
keyboard::{KeyCode, KeyModifiers},
2424
Document, Editor, Theme, View,
2525
};
2626
use std::borrow::Cow;
2727

28-
use crossterm::event::{Event, MouseButton, MouseEvent, MouseEventKind};
2928
use tui::buffer::Buffer as Surface;
3029

3130
use super::lsp::SignatureHelp;
@@ -966,7 +965,7 @@ impl EditorView {
966965
if let Some((pos, view_id)) = pos_and_view(editor, row, column) {
967966
let doc = editor.document_mut(editor.tree.get(view_id).doc).unwrap();
968967

969-
if modifiers == crossterm::event::KeyModifiers::ALT {
968+
if modifiers == KeyModifiers::ALT {
970969
let selection = doc.selection(view_id).clone();
971970
doc.set_selection(view_id, selection.push(Range::point(pos)));
972971
} else {
@@ -1068,7 +1067,7 @@ impl EditorView {
10681067
let line = coords.row + view.offset.row;
10691068
if let Ok(pos) = doc.text().try_line_to_char(line) {
10701069
doc.set_selection(view_id, Selection::point(pos));
1071-
if modifiers == crossterm::event::KeyModifiers::ALT {
1070+
if modifiers == KeyModifiers::ALT {
10721071
commands::MappableCommand::dap_edit_log.execute(cxt);
10731072
} else {
10741073
commands::MappableCommand::dap_edit_condition.execute(cxt);
@@ -1087,7 +1086,7 @@ impl EditorView {
10871086
return EventResult::Ignored(None);
10881087
}
10891088

1090-
if modifiers == crossterm::event::KeyModifiers::ALT {
1089+
if modifiers == KeyModifiers::ALT {
10911090
commands::MappableCommand::replace_selections_with_primary_clipboard
10921091
.execute(cxt);
10931092

@@ -1132,9 +1131,8 @@ impl Component for EditorView {
11321131
// Handling it here but not re-rendering will cause flashing
11331132
EventResult::Consumed(None)
11341133
}
1135-
Event::Key(key) => {
1134+
Event::Key(mut key) => {
11361135
cx.editor.reset_idle_timer();
1137-
let mut key = KeyEvent::from(key);
11381136
canonicalize_key(&mut key);
11391137

11401138
// clear status

helix-term/src/ui/menu.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use std::{borrow::Cow, path::PathBuf};
22

33
use crate::{
4-
compositor::{Callback, Component, Compositor, Context, EventResult},
4+
compositor::{Callback, Component, Compositor, Context, Event, EventResult},
55
ctrl, key, shift,
66
};
7-
use crossterm::event::Event;
87
use tui::{buffer::Buffer as Surface, text::Spans, widgets::Table};
98

109
pub use tui::widgets::{Cell, Row};
@@ -237,7 +236,7 @@ impl<T: Item + 'static> Component for Menu<T> {
237236
compositor.pop();
238237
}));
239238

240-
match event.into() {
239+
match event {
241240
// esc or ctrl-c aborts the completion and closes the menu
242241
key!(Esc) | ctrl!('c') => {
243242
(self.callback_fn)(cx.editor, self.selection(), MenuEvent::Abort);

helix-term/src/ui/overlay.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
use crossterm::event::Event;
21
use helix_core::Position;
32
use helix_view::{
43
graphics::{CursorKind, Rect},
54
Editor,
65
};
76
use tui::buffer::Buffer;
87

9-
use crate::compositor::{Component, Context, EventResult};
8+
use crate::compositor::{Component, Context, Event, EventResult};
109

1110
/// Contains a component placed in the center of the parent component
1211
pub struct Overlay<T> {

helix-term/src/ui/picker.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use crate::{
2-
compositor::{Component, Compositor, Context, EventResult},
2+
compositor::{Component, Compositor, Context, Event, EventResult},
33
ctrl, key, shift,
44
ui::{self, EditorView},
55
};
6-
use crossterm::event::Event;
76
use tui::{
87
buffer::Buffer as Surface,
98
widgets::{Block, BorderType, Borders},
@@ -502,7 +501,7 @@ impl<T: Item + 'static> Component for Picker<T> {
502501
compositor.last_picker = compositor.pop();
503502
})));
504503

505-
match key_event.into() {
504+
match key_event {
506505
shift!(Tab) | key!(Up) | ctrl!('p') => {
507506
self.move_by(1, Direction::Backward);
508507
}

helix-term/src/ui/popup.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use crate::{
22
commands::Open,
3-
compositor::{Callback, Component, Context, EventResult},
3+
compositor::{Callback, Component, Context, Event, EventResult},
44
ctrl, key,
55
};
6-
use crossterm::event::Event;
76
use tui::buffer::Buffer as Surface;
87

98
use helix_core::Position;
@@ -149,7 +148,7 @@ impl<T: Component> Component for Popup<T> {
149148
_ => return EventResult::Ignored(None),
150149
};
151150

152-
if key!(Esc) == key.into() && self.ignore_escape_key {
151+
if key!(Esc) == key && self.ignore_escape_key {
153152
return EventResult::Ignored(None);
154153
}
155154

@@ -158,7 +157,7 @@ impl<T: Component> Component for Popup<T> {
158157
compositor.remove(self.id.as_ref());
159158
});
160159

161-
match key.into() {
160+
match key {
162161
// esc or ctrl-c aborts the completion and closes the menu
163162
key!(Esc) | ctrl!('c') => {
164163
let _ = self.contents.handle_event(event, cx);

helix-term/src/ui/prompt.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use crate::compositor::{Component, Compositor, Context, EventResult};
1+
use crate::compositor::{Component, Compositor, Context, Event, EventResult};
22
use crate::{alt, ctrl, key, shift, ui};
3-
use crossterm::event::Event;
43
use helix_view::input::KeyEvent;
54
use helix_view::keyboard::KeyCode;
65
use std::{borrow::Cow, ops::RangeFrom};
@@ -479,7 +478,7 @@ impl Component for Prompt {
479478
compositor.pop();
480479
})));
481480

482-
match event.into() {
481+
match event {
483482
ctrl!('c') | key!(Esc) => {
484483
(self.callback_fn)(cx, &self.line, PromptEvent::Abort);
485484
return close_fn;

helix-view/src/input.rs

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,53 @@ use helix_core::unicode::{segmentation::UnicodeSegmentation, width::UnicodeWidth
44
use serde::de::{self, Deserialize, Deserializer};
55
use 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")]
223323
impl From<crossterm::event::KeyEvent> for KeyEvent {
224324
fn from(crossterm::event::KeyEvent { code, modifiers }: crossterm::event::KeyEvent) -> Self {

helix-view/src/keyboard.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ impl From<crossterm::event::KeyModifiers> for KeyModifiers {
5555

5656
/// Represents a key.
5757
#[derive(Debug, PartialOrd, Ord, PartialEq, Eq, Clone, Copy, Hash)]
58-
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5958
pub enum KeyCode {
6059
/// Backspace key.
6160
Backspace,

0 commit comments

Comments
 (0)