Krolyxon
Commit cef5f73

rmptuisrceventhandler.rs

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