Skip to content

Commit

Permalink
fixing duplicate events windows issue
Browse files Browse the repository at this point in the history
This is a first attempt at fixing issue
that causes applications on windows
to receive two events on every press,
since crossterm would send two events
on windows with different kinds.
This change simply passes the event kind
further, which makes application code
responsible for correctly reacting on
the received kind of event
  • Loading branch information
strowk committed Sep 28, 2023
1 parent bb2fe5f commit fa784b9
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
8 changes: 7 additions & 1 deletion examples/demo/components/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use super::{get_block, Msg};

use tuirealm::command::{Cmd, CmdResult};
use tuirealm::event::Key;
use tuirealm::event::{Key, KeyEventKind};
use tuirealm::event::{KeyEvent, KeyModifiers};
use tuirealm::props::{Alignment, Borders, Color, Style, TextModifiers};
use tuirealm::tui::layout::Rect;
Expand Down Expand Up @@ -200,14 +200,17 @@ impl Component<Msg, NoUserEvent> for LetterCounter {
Event::Keyboard(KeyEvent {
code: Key::Char(ch),
modifiers: KeyModifiers::NONE,
kind: KeyEventKind::Press
}) if ch.is_alphabetic() => Cmd::Submit,
Event::Keyboard(KeyEvent {
code: Key::Tab,
modifiers: KeyModifiers::NONE,
kind: KeyEventKind::Press
}) => return Some(Msg::LetterCounterBlur), // Return focus lost
Event::Keyboard(KeyEvent {
code: Key::Esc,
modifiers: KeyModifiers::NONE,
kind: KeyEventKind::Press
}) => return Some(Msg::AppClose),
_ => Cmd::None,
};
Expand Down Expand Up @@ -252,14 +255,17 @@ impl Component<Msg, NoUserEvent> for DigitCounter {
Event::Keyboard(KeyEvent {
code: Key::Char(ch),
modifiers: KeyModifiers::NONE,
kind: KeyEventKind::Press
}) if ch.is_digit(10) => Cmd::Submit,
Event::Keyboard(KeyEvent {
code: Key::Tab,
modifiers: KeyModifiers::NONE,
kind: KeyEventKind::Press
}) => return Some(Msg::DigitCounterBlur), // Return focus lost
Event::Keyboard(KeyEvent {
code: Key::Esc,
modifiers: KeyModifiers::NONE,
kind: KeyEventKind::Press
}) => return Some(Msg::AppClose),
_ => Cmd::None,
};
Expand Down
14 changes: 14 additions & 0 deletions src/adapter/crossterm/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
//!
//! event adapter for crossterm

use crate::event::KeyEventKind;

use super::{Event, Key, KeyEvent, KeyModifiers, MediaKeyCode};

use crossterm::event::{
Event as XtermEvent, KeyCode as XtermKeyCode, KeyEvent as XtermKeyEvent,
KeyModifiers as XtermKeyModifiers, MediaKeyCode as XtermMediaKeyCode,
KeyEventKind as XtermKeyEventKind
};

impl<U> From<XtermEvent> for Event<U>
Expand All @@ -30,6 +33,17 @@ impl From<XtermKeyEvent> for KeyEvent {
Self {
code: e.code.into(),
modifiers: e.modifiers.into(),
kind: e.kind.into()
}
}
}

impl From<XtermKeyEventKind> for KeyEventKind {
fn from(value: XtermKeyEventKind) -> Self {
match value {
XtermKeyEventKind::Press => KeyEventKind::Press,
XtermKeyEventKind::Repeat => KeyEventKind::Repeat,
XtermKeyEventKind::Release => KeyEventKind::Release,
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/adapter/termion/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
//!
//! event adapter for termion

use crate::event::KeyEventKind;

use super::{Event, Key, KeyEvent, KeyModifiers};

use termion::event::{Event as TonEvent, Key as TonKey};
Expand Down Expand Up @@ -50,7 +52,7 @@ impl From<TonKey> for KeyEvent {
TonKey::Esc => Key::Esc,
TonKey::__IsNotComplete => Key::Null,
};
Self { code, modifiers }
Self { code, modifiers, kind: KeyEventKind.Unknown }
}
}

Expand Down
11 changes: 10 additions & 1 deletion src/core/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ pub enum NoUserEvent {}
pub struct KeyEvent {
pub code: Key,
pub modifiers: KeyModifiers,
pub kind: KeyEventKind
}

#[derive(Debug, Eq, PartialEq, Copy, Clone, PartialOrd, Hash)]
pub enum KeyEventKind {
Unknown,
Press,
Release,
Repeat
}

/// A keyboard event
Expand Down Expand Up @@ -159,7 +168,7 @@ bitflags! {

impl KeyEvent {
pub fn new(code: Key, modifiers: KeyModifiers) -> Self {
Self { code, modifiers }
Self { code, modifiers, kind: KeyEventKind::Unknown }
}
}

Expand Down

0 comments on commit fa784b9

Please sign in to comment.