Krolyxon
Commit c15bb22

rmptuisrceventhandler.rs

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