Skip to content

Commit 23f499d

Browse files
author
Emil Hammarstrom
committed
Handle cases where Shift+Tab does not issue a BackTab KeyEvent
1 parent 80c1114 commit 23f499d

File tree

1 file changed

+48
-37
lines changed

1 file changed

+48
-37
lines changed

src/handler.rs

Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,50 @@ use tokio::sync::mpsc::UnboundedSender;
1212

1313
use tui_input::backend::crossterm::EventHandler;
1414

15+
fn forward_tab(app: &mut App) {
16+
match app.focused_block {
17+
FocusedBlock::Adapter => {
18+
app.focused_block = FocusedBlock::PairedDevices;
19+
app.reset_devices_state();
20+
}
21+
FocusedBlock::PairedDevices => {
22+
if let Some(selected_controller) = app.controller_state.selected() {
23+
let controller = &app.controllers[selected_controller];
24+
if controller.new_devices.is_empty() {
25+
app.focused_block = FocusedBlock::Adapter;
26+
} else {
27+
app.focused_block = FocusedBlock::NewDevices;
28+
}
29+
}
30+
}
31+
FocusedBlock::NewDevices => app.focused_block = FocusedBlock::Adapter,
32+
_ => {}
33+
}
34+
}
35+
36+
fn backward_tab(app: &mut App) {
37+
match app.focused_block {
38+
FocusedBlock::Adapter => {
39+
if let Some(selected_controller) = app.controller_state.selected() {
40+
let controller = &app.controllers[selected_controller];
41+
if controller.new_devices.is_empty() {
42+
app.focused_block = FocusedBlock::PairedDevices;
43+
} else {
44+
app.focused_block = FocusedBlock::NewDevices;
45+
}
46+
}
47+
}
48+
FocusedBlock::PairedDevices => {
49+
app.focused_block = FocusedBlock::Adapter;
50+
}
51+
FocusedBlock::NewDevices => {
52+
app.focused_block = FocusedBlock::PairedDevices;
53+
app.reset_devices_state();
54+
}
55+
_ => {}
56+
}
57+
}
58+
1559
pub async fn handle_key_events(
1660
key_event: KeyEvent,
1761
app: &mut App,
@@ -80,45 +124,12 @@ pub async fn handle_key_events(
80124
}
81125

82126
// Switch focus
83-
KeyCode::Tab => match app.focused_block {
84-
FocusedBlock::Adapter => {
85-
app.focused_block = FocusedBlock::PairedDevices;
86-
app.reset_devices_state();
87-
}
88-
FocusedBlock::PairedDevices => {
89-
if let Some(selected_controller) = app.controller_state.selected() {
90-
let controller = &app.controllers[selected_controller];
91-
if controller.new_devices.is_empty() {
92-
app.focused_block = FocusedBlock::Adapter;
93-
} else {
94-
app.focused_block = FocusedBlock::NewDevices;
95-
}
96-
}
97-
}
98-
FocusedBlock::NewDevices => app.focused_block = FocusedBlock::Adapter,
99-
_ => {}
127+
KeyCode::Tab => match key_event.modifiers {
128+
KeyModifiers::SHIFT => backward_tab(app),
129+
_ => forward_tab(app),
100130
},
101131

102-
KeyCode::BackTab => match app.focused_block {
103-
FocusedBlock::Adapter => {
104-
if let Some(selected_controller) = app.controller_state.selected() {
105-
let controller = &app.controllers[selected_controller];
106-
if controller.new_devices.is_empty() {
107-
app.focused_block = FocusedBlock::PairedDevices;
108-
} else {
109-
app.focused_block = FocusedBlock::NewDevices;
110-
}
111-
}
112-
}
113-
FocusedBlock::PairedDevices => {
114-
app.focused_block = FocusedBlock::Adapter;
115-
}
116-
FocusedBlock::NewDevices => {
117-
app.focused_block = FocusedBlock::PairedDevices;
118-
app.reset_devices_state();
119-
}
120-
_ => {}
121-
},
132+
KeyCode::BackTab => backward_tab(app),
122133

123134
KeyCode::Char('h') => match app.focused_block {
124135
FocusedBlock::Adapter => {

0 commit comments

Comments
 (0)