Add a custom event type that's shared across backends

gui
Blaž Hrastnik 2 years ago
parent e0f9d86f49
commit 61365dfbf3
No known key found for this signature in database
GPG Key ID: 1238B9C4AD889640

@ -15,7 +15,7 @@ use helix_view::{align_view, editor::ConfigEvent, graphics::Rect, theme, Align,
use crate::{ use crate::{
args::Args, args::Args,
compositor::Compositor, compositor::{Compositor, Event},
config::Config, config::Config,
job::Jobs, job::Jobs,
keymap::Keymaps, keymap::Keymaps,
@ -31,7 +31,7 @@ use std::{
use anyhow::Error; use anyhow::Error;
use crossterm::{ use crossterm::{
event::{DisableMouseCapture, EnableMouseCapture, Event, EventStream}, event::{DisableMouseCapture, EnableMouseCapture, Event as CrosstermEvent, EventStream},
execute, terminal, execute, terminal,
tty::IsTty, tty::IsTty,
}; };
@ -397,14 +397,17 @@ impl Application {
} }
} }
pub fn handle_terminal_events(&mut self, event: Option<Result<Event, crossterm::ErrorKind>>) { pub fn handle_terminal_events(
&mut self,
event: Option<Result<CrosstermEvent, crossterm::ErrorKind>>,
) {
let mut cx = crate::compositor::Context { let mut cx = crate::compositor::Context {
editor: &mut self.editor, editor: &mut self.editor,
jobs: &mut self.jobs, jobs: &mut self.jobs,
}; };
// Handle key events // Handle key events
let should_redraw = match event { let should_redraw = match event {
Some(Ok(Event::Resize(width, height))) => { Some(Ok(CrosstermEvent::Resize(width, height))) => {
self.terminal self.terminal
.resize(Rect::new(0, 0, width, height)) .resize(Rect::new(0, 0, width, height))
.expect("Unable to resize terminal"); .expect("Unable to resize terminal");
@ -416,7 +419,7 @@ impl Application {
self.compositor self.compositor
.handle_event(Event::Resize(width, height), &mut cx) .handle_event(Event::Resize(width, height), &mut cx)
} }
Some(Ok(event)) => self.compositor.handle_event(event, &mut cx), Some(Ok(event)) => self.compositor.handle_event(event.into(), &mut cx),
Some(Err(x)) => panic!("{}", x), Some(Err(x)) => panic!("{}", x),
None => panic!(), None => panic!(),
}; };

@ -4570,7 +4570,7 @@ fn replay_macro(cx: &mut Context) {
cx.callback = Some(Box::new(move |compositor, cx| { cx.callback = Some(Box::new(move |compositor, cx| {
for _ in 0..count { for _ in 0..count {
for &key in keys.iter() { for &key in keys.iter() {
compositor.handle_event(crossterm::event::Event::Key(key.into()), cx); compositor.handle_event(compositor::Event::Key(key), cx);
} }
} }
})); }));

@ -4,9 +4,6 @@
use helix_core::Position; use helix_core::Position;
use helix_view::graphics::{CursorKind, Rect}; use helix_view::graphics::{CursorKind, Rect};
use crossterm::event::Event;
use tui::buffer::Buffer as Surface;
pub type Callback = Box<dyn FnOnce(&mut Compositor, &mut Context)>; pub type Callback = Box<dyn FnOnce(&mut Compositor, &mut Context)>;
// Cursive-inspired // Cursive-inspired
@ -15,21 +12,29 @@ pub enum EventResult {
Consumed(Option<Callback>), Consumed(Option<Callback>),
} }
use crate::job::Jobs;
use helix_view::Editor; use helix_view::Editor;
use crate::job::Jobs; pub use helix_view::input::Event;
pub struct Context<'a> { pub struct Context<'a> {
pub editor: &'a mut Editor, pub editor: &'a mut Editor,
pub jobs: &'a mut Jobs, pub jobs: &'a mut Jobs,
} }
pub struct RenderContext<'a> { mod term {
pub editor: &'a Editor, use super::*;
pub surface: &'a mut Surface, pub use tui::buffer::Buffer as Surface;
pub scroll: Option<usize>,
pub struct RenderContext<'a> {
pub editor: &'a Editor,
pub surface: &'a mut Surface,
pub scroll: Option<usize>,
}
} }
pub use term::*;
pub trait Component: Any + AnyComponent { pub trait Component: Any + AnyComponent {
/// Process input events, return true if handled. /// Process input events, return true if handled.
fn handle_event(&mut self, _event: Event, _ctx: &mut Context) -> EventResult { fn handle_event(&mut self, _event: Event, _ctx: &mut Context) -> EventResult {
@ -115,7 +120,7 @@ impl Compositor {
pub fn handle_event(&mut self, event: Event, cx: &mut Context) -> bool { pub fn handle_event(&mut self, event: Event, cx: &mut Context) -> bool {
// If it is a key event and a macro is being recorded, push the key event to the recording. // If it is a key event and a macro is being recorded, push the key event to the recording.
if let (Event::Key(key), Some((_, keys))) = (event, &mut cx.editor.macro_recording) { if let (Event::Key(key), Some((_, keys))) = (event, &mut cx.editor.macro_recording) {
keys.push(key.into()); keys.push(key);
} }
let mut callbacks = Vec::new(); let mut callbacks = Vec::new();

@ -1,11 +1,14 @@
use crate::compositor::{Component, Context, EventResult, RenderContext}; use crate::compositor::{Component, Context, Event, EventResult, RenderContext};
use crossterm::event::{Event, KeyCode, KeyEvent};
use helix_view::editor::CompleteAction; use helix_view::editor::CompleteAction;
use std::borrow::Cow; use std::borrow::Cow;
use helix_core::{Change, Transaction}; use helix_core::{Change, Transaction};
use helix_view::{graphics::Rect, Document, Editor}; use helix_view::{
graphics::Rect,
input::{KeyCode, KeyEvent},
Document, Editor,
};
use crate::commands; use crate::commands;
use crate::ui::{menu, Markdown, Menu, Popup, PromptEvent}; use crate::ui::{menu, Markdown, Menu, Popup, PromptEvent};

@ -1,6 +1,6 @@
use crate::{ use crate::{
commands, commands,
compositor::{Component, Context, EventResult, RenderContext}, compositor::{Component, Context, Event, EventResult, RenderContext},
key, key,
keymap::{KeymapResult, Keymaps}, keymap::{KeymapResult, Keymaps},
ui::{Completion, ProgressSpinners}, ui::{Completion, ProgressSpinners},
@ -21,13 +21,12 @@ use helix_view::{
document::{Mode, SCRATCH_BUFFER_NAME}, document::{Mode, SCRATCH_BUFFER_NAME},
editor::{CompleteAction, CursorShapeConfig}, editor::{CompleteAction, CursorShapeConfig},
graphics::{CursorKind, Modifier, Rect, Style}, graphics::{CursorKind, Modifier, Rect, Style},
input::KeyEvent, input::{KeyEvent, MouseButton, MouseEvent, MouseEventKind},
keyboard::{KeyCode, KeyModifiers}, keyboard::{KeyCode, KeyModifiers},
Document, Editor, Theme, View, Document, Editor, Theme, View,
}; };
use std::borrow::Cow; use std::borrow::Cow;
use crossterm::event::{Event, MouseButton, MouseEvent, MouseEventKind};
use tui::buffer::Buffer as Surface; use tui::buffer::Buffer as Surface;
pub struct EditorView { pub struct EditorView {
@ -977,7 +976,7 @@ impl EditorView {
if let Some((pos, view_id)) = result { if let Some((pos, view_id)) = result {
let doc = editor.document_mut(editor.tree.get(view_id).doc).unwrap(); let doc = editor.document_mut(editor.tree.get(view_id).doc).unwrap();
if modifiers == crossterm::event::KeyModifiers::ALT { if modifiers == KeyModifiers::ALT {
let selection = doc.selection(view_id).clone(); let selection = doc.selection(view_id).clone();
doc.set_selection(view_id, selection.push(Range::point(pos))); doc.set_selection(view_id, selection.push(Range::point(pos)));
} else { } else {
@ -1108,7 +1107,7 @@ impl EditorView {
let line = coords.row + view.offset.row; let line = coords.row + view.offset.row;
if let Ok(pos) = doc.text().try_line_to_char(line) { if let Ok(pos) = doc.text().try_line_to_char(line) {
doc.set_selection(view_id, Selection::point(pos)); doc.set_selection(view_id, Selection::point(pos));
if modifiers == crossterm::event::KeyModifiers::ALT { if modifiers == KeyModifiers::ALT {
commands::MappableCommand::dap_edit_log.execute(cxt); commands::MappableCommand::dap_edit_log.execute(cxt);
} else { } else {
commands::MappableCommand::dap_edit_condition.execute(cxt); commands::MappableCommand::dap_edit_condition.execute(cxt);
@ -1132,7 +1131,7 @@ impl EditorView {
return EventResult::Ignored(None); return EventResult::Ignored(None);
} }
if modifiers == crossterm::event::KeyModifiers::ALT { if modifiers == KeyModifiers::ALT {
commands::MappableCommand::replace_selections_with_primary_clipboard commands::MappableCommand::replace_selections_with_primary_clipboard
.execute(cxt); .execute(cxt);
@ -1181,9 +1180,8 @@ impl Component for EditorView {
// Handling it here but not re-rendering will cause flashing // Handling it here but not re-rendering will cause flashing
EventResult::Consumed(None) EventResult::Consumed(None)
} }
Event::Key(key) => { Event::Key(mut key) => {
cx.editor.reset_idle_timer(); cx.editor.reset_idle_timer();
let mut key = KeyEvent::from(key);
canonicalize_key(&mut key); canonicalize_key(&mut key);
// clear status // clear status

@ -1,8 +1,7 @@
use crate::{ use crate::{
compositor::{Callback, Component, Compositor, Context, EventResult, RenderContext}, compositor::{Callback, Component, Compositor, Context, Event, EventResult, RenderContext},
ctrl, key, shift, ctrl, key, shift,
}; };
use crossterm::event::Event;
use tui::widgets::Table; use tui::widgets::Table;
pub use tui::widgets::{Cell, Row}; pub use tui::widgets::{Cell, Row};
@ -210,7 +209,7 @@ impl<T: Item + 'static> Component for Menu<T> {
compositor.pop(); compositor.pop();
})); }));
match event.into() { match event {
// esc or ctrl-c aborts the completion and closes the menu // esc or ctrl-c aborts the completion and closes the menu
key!(Esc) | ctrl!('c') => { key!(Esc) | ctrl!('c') => {
(self.callback_fn)(cx.editor, self.selection(), MenuEvent::Abort); (self.callback_fn)(cx.editor, self.selection(), MenuEvent::Abort);

@ -1,11 +1,10 @@
use crossterm::event::Event;
use helix_core::Position; use helix_core::Position;
use helix_view::{ use helix_view::{
graphics::{CursorKind, Rect}, graphics::{CursorKind, Rect},
Editor, Editor,
}; };
use crate::compositor::{Component, Context, EventResult, RenderContext}; use crate::compositor::{Component, Context, Event, EventResult, RenderContext};
/// Contains a component placed in the center of the parent component /// Contains a component placed in the center of the parent component
pub struct Overlay<T> { pub struct Overlay<T> {

@ -1,9 +1,8 @@
use crate::{ use crate::{
compositor::{Component, Compositor, Context, EventResult, RenderContext}, compositor::{Component, Compositor, Context, Event, EventResult, RenderContext},
ctrl, key, shift, ctrl, key, shift,
ui::{self, EditorView}, ui::{self, EditorView},
}; };
use crossterm::event::Event;
use tui::widgets::{Block, BorderType, Borders}; use tui::widgets::{Block, BorderType, Borders};
use fuzzy_matcher::skim::SkimMatcherV2 as Matcher; use fuzzy_matcher::skim::SkimMatcherV2 as Matcher;
@ -496,7 +495,7 @@ impl<T: 'static> Component for Picker<T> {
compositor.last_picker = compositor.pop(); compositor.last_picker = compositor.pop();
}))); })));
match key_event.into() { match key_event {
shift!(Tab) | key!(Up) | ctrl!('p') => { shift!(Tab) | key!(Up) | ctrl!('p') => {
self.move_by(1, Direction::Backward); self.move_by(1, Direction::Backward);
} }

@ -1,8 +1,7 @@
use crate::{ use crate::{
compositor::{Callback, Component, Context, EventResult, RenderContext}, compositor::{Callback, Component, Context, Event, EventResult, RenderContext},
ctrl, key, ctrl, key,
}; };
use crossterm::event::Event;
use helix_core::Position; use helix_core::Position;
use helix_view::{ use helix_view::{
@ -124,7 +123,7 @@ impl<T: Component> Component for Popup<T> {
compositor.pop(); compositor.pop();
}); });
match key.into() { match key {
// esc or ctrl-c aborts the completion and closes the menu // esc or ctrl-c aborts the completion and closes the menu
key!(Esc) | ctrl!('c') => { key!(Esc) | ctrl!('c') => {
let _ = self.contents.handle_event(event, cx); let _ = self.contents.handle_event(event, cx);

@ -1,6 +1,5 @@
use crate::compositor::{Component, Compositor, Context, EventResult, RenderContext}; use crate::compositor::{Component, Compositor, Context, Event, EventResult, RenderContext};
use crate::{alt, ctrl, key, shift, ui}; use crate::{alt, ctrl, key, shift, ui};
use crossterm::event::Event;
use helix_view::input::KeyEvent; use helix_view::input::KeyEvent;
use helix_view::keyboard::KeyCode; use helix_view::keyboard::KeyCode;
use std::{borrow::Cow, ops::RangeFrom}; use std::{borrow::Cow, ops::RangeFrom};
@ -454,7 +453,7 @@ impl Component for Prompt {
compositor.pop(); compositor.pop();
}))); })));
match event.into() { match event {
ctrl!('c') | key!(Esc) => { ctrl!('c') | key!(Esc) => {
(self.callback_fn)(cx, &self.line, PromptEvent::Abort); (self.callback_fn)(cx, &self.line, PromptEvent::Abort);
return close_fn; return close_fn;

@ -4,8 +4,53 @@ use helix_core::unicode::width::UnicodeWidthStr;
use serde::de::{self, Deserialize, Deserializer}; use serde::de::{self, Deserialize, Deserializer};
use std::fmt; 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. /// Represents a key event.
// We use a newtype here because we want to customize Deserialize and Display. // We use a newtype here because we want to customize Deserialize and Display.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)] #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)]
@ -214,6 +259,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")] #[cfg(feature = "term")]
impl From<crossterm::event::KeyEvent> for KeyEvent { impl From<crossterm::event::KeyEvent> for KeyEvent {
fn from(crossterm::event::KeyEvent { code, modifiers }: crossterm::event::KeyEvent) -> Self { fn from(crossterm::event::KeyEvent { code, modifiers }: crossterm::event::KeyEvent) -> Self {

@ -55,7 +55,6 @@ impl From<crossterm::event::KeyModifiers> for KeyModifiers {
/// Represents a key. /// Represents a key.
#[derive(Debug, PartialOrd, Ord, PartialEq, Eq, Clone, Copy, Hash)] #[derive(Debug, PartialOrd, Ord, PartialEq, Eq, Clone, Copy, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum KeyCode { pub enum KeyCode {
/// Backspace key. /// Backspace key.
Backspace, Backspace,

Loading…
Cancel
Save