Krolyxon
Commit 520dad7

rmptuisrceventhandler.rs

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