Skip to content

Commit

Permalink
Don't panic if we get an unexpected key code
Browse files Browse the repository at this point in the history
It seems like we get media play/pause/mute/etc keys despite not
emitting DISAMBIGUATE_ESCAPE_CODES(#4125).

Make crossterm::event::KeyCode a TryInto for KeyCode instead of Into to
let us ignore keys we don't care about.
  • Loading branch information
groves committed Nov 3, 2022
1 parent 1bed2f3 commit 70286ff
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
12 changes: 10 additions & 2 deletions helix-term/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,14 +538,22 @@ impl Application {
jobs: &mut self.jobs,
scroll: None,
};
// Handle key events
let should_redraw = match event.unwrap() {
CrosstermEvent::Resize(width, height) => {
self.compositor.resize(width, height);
self.compositor
.handle_event(&Event::Resize(width, height), &mut cx)
}
event => self.compositor.handle_event(&event.into(), &mut cx),
event => match &event.try_into() {
Ok(event) => self.compositor.handle_event(event, &mut cx),
Err(err) => {
log::warn!(
"Unable to convert crossterm event to helix event. Ignoring. Cause: {}",
err
);
false
}
},
};

if should_redraw && !self.editor.should_close() {
Expand Down
28 changes: 15 additions & 13 deletions helix-view/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,16 +276,17 @@ 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()),
impl TryFrom<crossterm::event::Event> for Event {
type Error = anyhow::Error;
fn try_from(event: crossterm::event::Event) -> Result<Self, Error> {
Ok(match event {
crossterm::event::Event::Key(key) => Self::Key(key.try_into()?),
crossterm::event::Event::Mouse(mouse) => Self::Mouse(mouse.into()),
crossterm::event::Event::Resize(w, h) => Self::Resize(w, h),
crossterm::event::Event::FocusGained => Self::FocusGained,
crossterm::event::Event::FocusLost => Self::FocusLost,
crossterm::event::Event::Paste(s) => Self::Paste(s),
}
})
}
}

Expand Down Expand Up @@ -334,25 +335,26 @@ impl From<crossterm::event::MouseButton> for MouseButton {
}

#[cfg(feature = "term")]
impl From<crossterm::event::KeyEvent> for KeyEvent {
fn from(
impl TryFrom<crossterm::event::KeyEvent> for KeyEvent {
type Error = anyhow::Error;
fn try_from(
crossterm::event::KeyEvent {
code, modifiers, ..
}: crossterm::event::KeyEvent,
) -> Self {
) -> Result<Self, Error> {
if code == crossterm::event::KeyCode::BackTab {
// special case for BackTab -> Shift-Tab
let mut modifiers: KeyModifiers = modifiers.into();
modifiers.insert(KeyModifiers::SHIFT);
Self {
Ok(Self {
code: KeyCode::Tab,
modifiers,
}
})
} else {
Self {
code: code.into(),
Ok(Self {
code: code.try_into()?,
modifiers: modifiers.into(),
}
})
}
}
}
Expand Down
12 changes: 7 additions & 5 deletions helix-view/src/keyboard.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::{bail, Error};
use bitflags::bitflags;

bitflags! {
Expand Down Expand Up @@ -124,11 +125,12 @@ impl From<KeyCode> for crossterm::event::KeyCode {
}

#[cfg(feature = "term")]
impl From<crossterm::event::KeyCode> for KeyCode {
fn from(val: crossterm::event::KeyCode) -> Self {
impl TryFrom<crossterm::event::KeyCode> for KeyCode {
type Error = Error;
fn try_from(val: crossterm::event::KeyCode) -> Result<Self, Error> {
use crossterm::event::KeyCode as CKeyCode;

match val {
Ok(match val {
CKeyCode::Backspace => KeyCode::Backspace,
CKeyCode::Enter => KeyCode::Enter,
CKeyCode::Left => KeyCode::Left,
Expand All @@ -155,9 +157,9 @@ impl From<crossterm::event::KeyCode> for KeyCode {
| CKeyCode::Menu
| CKeyCode::KeypadBegin
| CKeyCode::Media(_)
| CKeyCode::Modifier(_) => unreachable!(
| CKeyCode::Modifier(_) => bail!(
"Shouldn't get this key without enabling DISAMBIGUATE_ESCAPE_CODES in crossterm"
),
}
})
}
}

0 comments on commit 70286ff

Please sign in to comment.