Skip to content

Commit b671071

Browse files
committed
fix(windows): Handle keypresses only once
On Windows the UI would often jump around when pressing navigation buttons. That's because crossterm sends a Press and a Release event for each keystroke on Windows. Signed-off-by: Daniel Schaefer <dhs@frame.work>
1 parent bfb7d69 commit b671071

File tree

4 files changed

+13
-7
lines changed

4 files changed

+13
-7
lines changed

src/tui.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pub mod theme;
55
use std::sync::Arc;
66

77
use ratatui::{
8-
crossterm::event::{Event, KeyCode},
8+
crossterm::event::{Event, KeyCode, KeyEventKind},
99
layout::{Constraint, Flex, Layout},
1010
prelude::Backend,
1111
style::Style,
@@ -98,7 +98,7 @@ impl Tui {
9898

9999
pub fn handle_input(&mut self, event: Event) -> color_eyre::Result<Option<AppEvent>> {
100100
let top_level_event = match &event {
101-
Event::Key(key) => match key.code {
101+
Event::Key(key) if key.kind == KeyEventKind::Press => match key.code {
102102
KeyCode::Char('q') => Some(AppEvent::Quit),
103103
KeyCode::Char('b') => {
104104
self.previous_theme();

src/tui/component/brightness_panel.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::sync::Arc;
22

33
use ratatui::{
4-
crossterm::event::{Event, KeyCode},
4+
crossterm::event::{Event, KeyCode, KeyEventKind},
55
layout::{Constraint, Layout, Rect},
66
prelude::*,
77
style::Styled,
@@ -194,6 +194,9 @@ impl Component for BrightnessPanelComponent {
194194

195195
if self.panel.is_selected() {
196196
if let Event::Key(key) = event {
197+
if key.kind != KeyEventKind::Press {
198+
return app_event;
199+
}
197200
match key.code {
198201
KeyCode::Down => self.panel.cycle_controls_down(),
199202
KeyCode::Up => self.panel.cycle_controls_up(),

src/tui/component/charge_panel.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use ratatui::{
2-
crossterm::event::{Event, KeyCode},
2+
crossterm::event::{Event, KeyCode, KeyEventKind},
33
layout::{Constraint, Layout, Rect},
44
prelude::*,
55
style::Styled,
@@ -311,6 +311,9 @@ impl Component for ChargePanelComponent {
311311

312312
if self.0.is_selected() {
313313
if let Event::Key(key) = event {
314+
if key.kind != KeyEventKind::Press {
315+
return app_event;
316+
}
314317
match key.code {
315318
KeyCode::Down => self.0.cycle_controls_down(),
316319
KeyCode::Up => self.0.cycle_controls_up(),

src/tui/component/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::sync::Arc;
22

33
use ratatui::{
4-
crossterm::event::{Event, KeyCode},
4+
crossterm::event::{Event, KeyCode, KeyEventKind},
55
layout::{Constraint, Layout, Rect},
66
Frame,
77
};
@@ -96,8 +96,8 @@ impl MainComponent {
9696

9797
impl Component for MainComponent {
9898
fn handle_input(&mut self, event: Event) -> Option<crate::app::AppEvent> {
99-
if let Event::Key(key) = event {
100-
if key.code == KeyCode::Tab {
99+
if let Event::Key(key) = &event {
100+
if key.kind == KeyEventKind::Press && key.code == KeyCode::Tab {
101101
self.switch_panels();
102102
}
103103
}

0 commit comments

Comments
 (0)