1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
use crate::app::{MenuMode, PanelFocus};
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use std::collections::HashMap;
use std::time::{Duration, Instant};
use crate::app::mpd_handler::MPDAction;
/// Sequential key binding configuration
#[derive(Debug, Clone)]
pub struct SequentialKeyBinding {
pub sequence: Vec<(KeyModifiers, KeyCode)>,
pub action: MPDAction,
}
/// Key binding state for sequential input
#[derive(Debug, Clone, PartialEq)]
pub enum KeyState {
Idle,
Awaiting {
sequence: Vec<(KeyModifiers, KeyCode)>,
timeout: Instant,
},
}
/// Key binding definitions for MPD controls with sequential support
#[derive(Debug)]
pub struct KeyBinds {
global_map: HashMap<(KeyModifiers, KeyCode), MPDAction>,
queue_map: HashMap<(KeyModifiers, KeyCode), MPDAction>,
artists_map: HashMap<(KeyModifiers, KeyCode), MPDAction>,
albums_map: HashMap<(KeyModifiers, KeyCode), MPDAction>,
sequential_bindings: Vec<SequentialKeyBinding>,
current_state: KeyState,
default_timeout: Duration,
}
impl KeyBinds {
/// New constructor that supports sequential bindings
pub fn new_with_sequential(
global_map: HashMap<(KeyModifiers, KeyCode), MPDAction>,
queue_map: HashMap<(KeyModifiers, KeyCode), MPDAction>,
artists_map: HashMap<(KeyModifiers, KeyCode), MPDAction>,
albums_map: HashMap<(KeyModifiers, KeyCode), MPDAction>,
sequential_bindings: Vec<SequentialKeyBinding>,
) -> Self {
Self {
global_map,
queue_map,
artists_map,
albums_map,
sequential_bindings,
current_state: KeyState::Idle,
default_timeout: Duration::from_millis(1000),
}
}
/// Handle key events and return corresponding MPD commands with sequential support
pub fn handle_key(
&mut self,
key: KeyEvent,
mode: &MenuMode,
panel_focus: &PanelFocus,
) -> Option<MPDAction> {
let key_tuple = (key.modifiers, key.code);
// Handle sequential key state
if !matches!(self.current_state, KeyState::Idle) {
return self.handle_sequential_input(key_tuple, mode, panel_focus);
}
// Check global bindings first (highest priority)
if let Some(action) = self.global_map.get(&key_tuple) {
// Handle mode-specific logic for certain bindings
match (action, mode) {
(
MPDAction::PlaySelected,
MenuMode::Artists | MenuMode::Years | MenuMode::Genres,
) => {
// Don't allow play_selected in Artist, Years and Genres mode - it conflicts with navigation
return None;
}
_ => return Some(action.clone()),
}
}
// Check mode-specific bindings
match mode {
MenuMode::Queue => {
if let Some(action) = self.queue_map.get(&key_tuple) {
return Some(action.clone());
}
}
MenuMode::Artists => {
if let Some(action) = self.artists_map.get(&key_tuple) {
// Handle panel-specific logic for tracks mode
match (action, panel_focus) {
(MPDAction::SwitchPanelRight, PanelFocus::Artists) => {
return Some(MPDAction::SwitchPanelRight);
}
(MPDAction::SwitchPanelRight, PanelFocus::Albums) => {
return Some(MPDAction::ToggleAlbumExpansion);
}
_ => return Some(action.clone()),
}
}
}
MenuMode::Albums => {
// Check albums-specific bindings first
if let Some(action) = self.albums_map.get(&key_tuple) {
// Handle panel-specific logic for albums mode
match (action, panel_focus) {
// PlaySelected: In AlbumTracks panel adds song, in AlbumList switches panel
(MPDAction::PlaySelected, PanelFocus::AlbumTracks) => {
return Some(MPDAction::PlaySelected);
}
(MPDAction::PlaySelected, PanelFocus::AlbumList) => {
return Some(MPDAction::SwitchPanelRight);
}
// AddSongToQueue: In AlbumTracks adds song, in AlbumList adds album
(MPDAction::AddSongToQueue, _) => {
return Some(MPDAction::AddSongToQueue);
}
_ => return Some(action.clone()),
}
}
// Fall back to artists_map for navigation bindings
if let Some(action) = self.artists_map.get(&key_tuple) {
match (action, panel_focus) {
(MPDAction::SwitchPanelRight, PanelFocus::AlbumList) => {
return Some(MPDAction::SwitchPanelRight);
}
(MPDAction::SwitchPanelRight, PanelFocus::AlbumTracks) => {
// Already at rightmost panel, no action
return None;
}
// Skip album expansion actions in albums mode
(MPDAction::ToggleAlbumExpansion, _) => {
return None;
}
_ => return Some(action.clone()),
}
}
}
MenuMode::Years => {
if let Some(action) = self.artists_map.get(&key_tuple) {
// Handle panel-specific logic for years mode
match (action, panel_focus) {
(MPDAction::SwitchPanelRight, PanelFocus::YearList) => {
return Some(MPDAction::SwitchPanelRight);
}
(MPDAction::SwitchPanelRight, PanelFocus::YearAlbums) => {
return Some(MPDAction::ToggleAlbumExpansion);
}
_ => return Some(action.clone()),
}
}
}
MenuMode::Genres => {
if let Some(action) = self.artists_map.get(&key_tuple) {
// Handle panel-specific logic for genres mode
match (action, panel_focus) {
(MPDAction::SwitchPanelRight, PanelFocus::GenreList) => {
return Some(MPDAction::SwitchPanelRight);
}
(MPDAction::SwitchPanelRight, PanelFocus::GenreAlbums) => {
return Some(MPDAction::ToggleAlbumExpansion);
}
_ => return Some(action.clone()),
}
}
}
}
// Check if this key could start a sequential binding
if self.could_start_sequence(key_tuple) {
self.current_state = KeyState::Awaiting {
sequence: vec![key_tuple],
timeout: Instant::now() + self.default_timeout,
};
None // Waiting for more input
} else {
None
}
}
/// Handle input when in the middle of a sequential key sequence
fn handle_sequential_input(
&mut self,
key_tuple: (KeyModifiers, KeyCode),
mode: &MenuMode,
panel_focus: &PanelFocus,
) -> Option<MPDAction> {
match &mut self.current_state {
KeyState::Idle => None, // Should not happen
KeyState::Awaiting { sequence, timeout } => {
// Check timeout
if *timeout < Instant::now() {
self.current_state = KeyState::Idle;
// Try as single key now
return self.handle_key(
KeyEvent::new(key_tuple.1, key_tuple.0),
mode,
panel_focus,
);
}
sequence.push(key_tuple);
// Check for complete sequence match
for binding in &self.sequential_bindings {
if binding.sequence == *sequence {
self.current_state = KeyState::Idle;
return Some(binding.action.clone());
}
}
// Check if current sequence could still match something
let possible_matches: Vec<_> = self
.sequential_bindings
.iter()
.filter(|binding| {
sequence.len() <= binding.sequence.len()
&& binding.sequence.starts_with(sequence)
})
.collect();
if possible_matches.is_empty() {
// No match, reset sequence without fallback
self.current_state = KeyState::Idle;
None
} else {
// Continue waiting, update timeout
*timeout = Instant::now() + self.default_timeout;
None
}
}
}
}
/// Check if a key could start a sequential binding
fn could_start_sequence(&self, key_tuple: (KeyModifiers, KeyCode)) -> bool {
self.sequential_bindings
.iter()
.any(|binding| binding.sequence.first() == Some(&key_tuple))
}
/// Get current key sequence for UI display
pub fn get_current_sequence(&self) -> Vec<(KeyModifiers, KeyCode)> {
match &self.current_state {
KeyState::Awaiting { sequence, .. } => sequence.clone(),
_ => Vec::new(),
}
}
/// Check if currently awaiting input for a sequence
pub fn is_awaiting_input(&self) -> bool {
matches!(self.current_state, KeyState::Awaiting { .. })
}
/// Update method to handle timeouts (call this regularly)
pub fn update(&mut self) -> Option<MPDAction> {
if let KeyState::Awaiting { timeout, .. } = &self.current_state
&& *timeout < Instant::now()
{
self.current_state = KeyState::Idle;
// No action on timeout, just reset
}
None
}
}