Krolyxon
Commit 98cfe4e

rmptuisrceventhandler.rs

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