Krolyxon
Commit ebc4428

rmptuisrceventhandler.rs

299 lines · 10.4 KB · Rust

1use crate::{
2 app::{App, AppResult, SelectedTab},
3 ui::InputMode,
4};
5use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
6use std::time::Duration;
7
8use super::{pl_append_keys, pl_rename_keys, search_keys};
9
10pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
11 // searching, playlist renaming, playlist appending
12 if app.inputmode == InputMode::Editing {
13 search_keys::handle_search_keys(key_event, app)?;
14 } else if app.inputmode == InputMode::PlaylistRename {
15 pl_rename_keys::handle_pl_rename_keys(key_event, app)?;
16 } else if app.playlist_popup {
17 pl_append_keys::hande_pl_append_keys(key_event, app)?;
18 } else {
19 // General KeyMaps
20 match key_event.code {
21 // Quit
22 KeyCode::Char('q') => app.quit(),
23 KeyCode::Char('c') | KeyCode::Char('C') => {
24 if key_event.modifiers == KeyModifiers::CONTROL {
25 app.quit();
26 } else {
27 app.conn.conn.clear()?;
28 app.conn.update_status();
29 app.queue_list.list.clear();
30 app.queue_list.reset_index();
31 }
32 }
33
34 // Playback controls
35 // Toggle Pause
36 KeyCode::Char('p') => {
37 app.conn.toggle_pause();
38 app.conn.update_status();
39 }
40
41 // Pause
42 KeyCode::Char('s') => {
43 app.conn.pause();
44 app.conn.update_status();
45 }
46
47 // Toggle rpeat
48 KeyCode::Char('r') => {
49 app.conn.toggle_repeat();
50 app.conn.update_status();
51 }
52
53 // Toggle random
54 KeyCode::Char('z') => {
55 app.conn.toggle_random();
56 app.conn.update_status();
57 }
58
59 // Dmenu prompt
60 KeyCode::Char('D') => {
61 app.conn.play_dmenu()?;
62 app.conn.update_status();
63 }
64
65 // add to queue
66 KeyCode::Char('a') => app.playlist_popup = true,
67
68 // Fast forward
69 KeyCode::Char('f') => {
70 if !app.queue_list.list.is_empty() {
71 let status = app.conn.conn.status().unwrap_or_default();
72 let place = status.song.unwrap_or_default().pos;
73 let (pos, _) = status.time.unwrap_or_default();
74 let pos = Duration::from_secs(pos.as_secs().wrapping_add(2));
75 app.conn.conn.seek(place, pos)?;
76 app.conn.update_status();
77 }
78 }
79
80 // backward
81 KeyCode::Char('b') => {
82 if !app.queue_list.list.is_empty() {
83 let status = app.conn.conn.status().unwrap_or_default();
84 let place = status.song.unwrap_or_default().pos;
85 let (pos, _) = status.time.unwrap_or_default();
86 let pos = Duration::from_secs(pos.as_secs().wrapping_sub(2));
87 app.conn.conn.seek(place, pos)?;
88 app.conn.update_status();
89 }
90 }
91
92 // Cycle through tabs
93 KeyCode::Tab => {
94 app.cycle_tabls();
95 }
96
97 // Directory browser tab
98 KeyCode::Char('1') => {
99 app.selected_tab = SelectedTab::Queue;
100 }
101
102 // Playing queue tab
103 KeyCode::Char('2') => {
104 app.selected_tab = SelectedTab::DirectoryBrowser;
105 }
106
107 // Playlists tab
108 KeyCode::Char('3') => {
109 app.selected_tab = SelectedTab::Playlists;
110 }
111
112 // Play next song
113 KeyCode::Char('>') => {
114 if !app.queue_list.list.is_empty() {
115 app.conn.conn.next()?;
116 app.update_queue();
117 app.conn.update_status();
118 }
119 }
120
121 // Play previous song
122 KeyCode::Char('<') => {
123 if !app.queue_list.list.is_empty() {
124 app.conn.conn.prev()?;
125 app.update_queue();
126 app.conn.update_status();
127 }
128 }
129
130 // Volume controls
131 KeyCode::Char('=') | KeyCode::Char('+') => {
132 app.conn.inc_volume(2);
133 app.conn.update_status();
134 }
135
136 KeyCode::Char('-') => {
137 app.conn.dec_volume(2);
138 app.conn.update_status();
139 }
140
141 // Update MPD database
142 KeyCode::Char('U') => {
143 app.conn.conn.rescan()?;
144 app.should_update_song_list = true;
145 }
146
147 // Search for songs
148 KeyCode::Char('/') => {
149 if app.inputmode == InputMode::Normal {
150 app.inputmode = InputMode::Editing;
151 } else {
152 app.inputmode = InputMode::Normal;
153 }
154 }
155
156 // Add or Remove from Current Playlist
157 KeyCode::Char(' ') => {
158 app.handle_add_or_remove_from_current_playlist()?;
159 }
160 _ => {}
161 }
162
163 // Tab specific keymaps
164 match app.selected_tab {
165 SelectedTab::Queue => {
166 match key_event.code {
167 // Go Up
168 KeyCode::Char('j') | KeyCode::Down => app.queue_list.next(),
169
170 // Go down
171 KeyCode::Char('k') | KeyCode::Up => app.queue_list.prev(),
172
173 // Next directory
174 KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right => {
175 app.conn.conn.switch(app.queue_list.index as u32)?;
176 app.conn.update_status();
177 }
178
179 // Delete highlighted song from the queue
180 KeyCode::Char('d') => {
181 if app.queue_list.index >= app.queue_list.list.len()
182 && app.queue_list.index != 0
183 {
184 app.queue_list.index -= 1;
185 }
186
187 app.conn.conn.delete(app.queue_list.index as u32)?;
188
189 if app.queue_list.index >= app.queue_list.list.len().saturating_sub(1)
190 && app.queue_list.index != 0
191 {
192 app.queue_list.index -= 1;
193 }
194
195 app.conn.update_status();
196 app.update_queue();
197 }
198
199 // Swap highlighted song with next one
200 KeyCode::Char('J') => {
201 let current: u32 = app.queue_list.index as u32;
202 let next: u32 = if (current + 1) as usize == app.queue_list.list.len() {
203 app.queue_list.index as u32
204 } else {
205 app.queue_list.index += 1;
206 current + 1
207 };
208 app.conn.conn.swap(current, next)?;
209 app.update_queue();
210 app.conn.update_status();
211 }
212
213 // Swap highlighted song with previous one
214 KeyCode::Char('K') => {
215 let current: u32 = app.queue_list.index as u32;
216 let prev: u32 = if current == 0 {
217 app.queue_list.index as u32
218 } else {
219 app.queue_list.index -= 1;
220 current - 1
221 };
222 app.conn.conn.swap(current, prev)?;
223 app.update_queue();
224 app.conn.update_status();
225 }
226
227 // go to top of list
228 KeyCode::Char('g') => app.queue_list.index = 0,
229
230 // go to bottom of list
231 KeyCode::Char('G') => app.queue_list.index = app.queue_list.list.len() - 1,
232
233 _ => {}
234 }
235 }
236
237 SelectedTab::DirectoryBrowser => {
238 match key_event.code {
239 // Go Up
240 KeyCode::Char('j') | KeyCode::Down => app.browser.next(),
241
242 // Go down
243 KeyCode::Char('k') | KeyCode::Up => app.browser.prev(),
244
245 // Next directory
246 KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right => {
247 // app.update_queue();
248 app.handle_enter()?;
249 app.conn.update_status();
250 }
251
252 // head back to previous directory
253 KeyCode::Char('h') | KeyCode::Left => {
254 app.browser.handle_go_back(&mut app.conn)?
255 }
256
257 // go to top of list
258 KeyCode::Char('g') => app.browser.selected = 0,
259
260 // go to bottom of list
261 KeyCode::Char('G') => app.browser.selected = app.browser.filetree.len() - 1,
262
263 _ => {}
264 }
265 }
266
267 SelectedTab::Playlists => {
268 match key_event.code {
269 // Go Up
270 KeyCode::Char('j') | KeyCode::Down => app.pl_list.next(),
271
272 // Go down
273 KeyCode::Char('k') | KeyCode::Up => app.pl_list.prev(),
274
275 // go to top of list
276 KeyCode::Char('g') => app.pl_list.index = 0,
277
278 // go to bottom of list
279 KeyCode::Char('G') => app.pl_list.index = app.pl_list.list.len() - 1,
280
281 // Change playlist name
282 KeyCode::Char('e') => app.change_playlist_name()?,
283
284 // add to current playlist
285 KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right | KeyCode::Char(' ') => {
286 // app.update_queue();
287 if !app.pl_list.list.is_empty() {
288 app.conn
289 .load_playlist(app.pl_list.list.get(app.pl_list.index).unwrap())?;
290 app.conn.update_status();
291 }
292 }
293 _ => {}
294 }
295 }
296 }
297 }
298 Ok(())
299}