Krolyxon
Tag v0.1.1

rmptuisrceventhandler.rs

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