aboutsummaryrefslogtreecommitdiff
path: root/src/event/handler.rs
blob: 09784582cf1e84c449a1b065ce6d660254518ec2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
use crate::{
    app::{App, AppResult, SelectedTab},
    ui::InputMode,
};
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use std::time::Duration;

use super::{pl_append_keys, pl_rename_keys, search_keys};

pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
    // searching, playlist renaming, playlist appending
    if app.inputmode == InputMode::Editing {
        search_keys::handle_search_keys(key_event, app)?;
    } else if app.inputmode == InputMode::PlaylistRename {
        pl_rename_keys::handle_pl_rename_keys(key_event, app)?;
    } else if app.playlist_popup {
        pl_append_keys::hande_pl_append_keys(key_event, app)?;
    }

    // General KeyMaps
    match key_event.code {
        // Quit
        KeyCode::Char('q') => app.quit(),
        KeyCode::Char('c') | KeyCode::Char('C') => {
            if key_event.modifiers == KeyModifiers::CONTROL {
                app.quit();
            } else {
                app.conn.conn.clear()?;
                app.conn.update_status();
                app.queue_list.list.clear();
                app.queue_list.reset_index();
            }
        }

        // Playback controls
        // Toggle Pause
        KeyCode::Char('p') => app.conn.toggle_pause(),

        // Pause
        KeyCode::Char('s') => app.conn.pause(),

        // Toggle rpeat
        KeyCode::Char('r') => {
            app.conn.toggle_repeat();
            app.conn.update_status();
        }

        // Toggle random
        KeyCode::Char('z') => {
            app.conn.toggle_random();
            app.conn.update_status();
        }

        // Dmenu prompt
        KeyCode::Char('D') => app.conn.play_dmenu()?,

        // add to queue
        KeyCode::Char('a') => app.playlist_popup = true,

        // Fast forward
        KeyCode::Char('f') => {
            let place = app.conn.conn.status().unwrap().song.unwrap().pos;
            let (pos, _) = app.conn.conn.status().unwrap().time.unwrap();
            let pos = Duration::from_secs(pos.as_secs().wrapping_add(2));
            app.conn.conn.seek(place, pos)?;
        }

        // backward
        KeyCode::Char('b') => {
            let place = app.conn.conn.status().unwrap().song.unwrap().pos;
            let (pos, _) = app.conn.conn.status().unwrap().time.unwrap();
            let pos = Duration::from_secs(pos.as_secs().wrapping_add(2));
            app.conn.conn.seek(place, pos)?;
        }

        // Cycle through tabs
        KeyCode::Tab => {
            app.cycle_tabls();
        }

        // Directory browser tab
        KeyCode::Char('1') => {
            app.selected_tab = SelectedTab::Queue;
        }

        // Playing queue tab
        KeyCode::Char('2') => {
            app.selected_tab = SelectedTab::DirectoryBrowser;
        }

        // Playlists tab
        KeyCode::Char('3') => {
            app.selected_tab = SelectedTab::Playlists;
        }

        // Play next song
        KeyCode::Char('>') => {
            if !app.queue_list.list.is_empty() {
                app.conn.conn.next()?;
                app.update_queue();
            }
        }

        // Play previous song
        KeyCode::Char('<') => {
            if !app.queue_list.list.is_empty() {
                app.conn.conn.prev()?;
                app.update_queue();
            }
        }

        // Volume controls
        KeyCode::Char('=') => {
            app.conn.inc_volume(2);
            app.conn.update_status();
        }

        KeyCode::Char('-') => {
            app.conn.dec_volume(2);
            app.conn.update_status();
        }

        // Update MPD database
        KeyCode::Char('U') => {
            app.conn.conn.rescan()?;
            app.browser.update_directory(&mut app.conn)?;
        }

        // Search for songs
        KeyCode::Char('/') => {
            if app.inputmode == InputMode::Normal {
                app.inputmode = InputMode::Editing;
            } else {
                app.inputmode = InputMode::Normal;
            }
        }

        // Add or Remove from Current Playlist
        KeyCode::Char(' ') | KeyCode::Backspace => {
            app.handle_add_or_remove_from_current_playlist()?;
        }
        _ => {}
    }

    // Tab specific keymaps
    match app.selected_tab {
        SelectedTab::Queue => {
            match key_event.code {
                // Go Up
                KeyCode::Char('j') | KeyCode::Down => app.queue_list.next(),

                // Go down
                KeyCode::Char('k') | KeyCode::Up => app.queue_list.prev(),

                // Next directory
                KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right => {
                    app.conn.conn.switch(app.queue_list.index as u32)?;
                    app.conn.update_status();
                }

                // Delete highlighted song from the queue
                KeyCode::Char('d') => {
                    if app.queue_list.index >= app.queue_list.list.len() - 1
                        && app.queue_list.index != 0
                    {
                        app.queue_list.index -= 1;
                    }

                    app.conn.conn.delete(app.queue_list.index as u32)?;
                    app.update_queue();
                }

                // Swap highlighted song with next one
                KeyCode::Char('J') => {
                    let current: u32 = app.queue_list.index as u32;
                    let next: u32 = if (current + 1) as usize == app.queue_list.list.len() {
                        app.queue_list.index as u32
                    } else {
                        app.queue_list.index += 1;
                        (current + 1) as u32
                    };
                    app.conn.conn.swap(current, next)?;
                    app.update_queue();
                }

                // Swap highlighted song with previous one
                KeyCode::Char('K') => {
                    let current: u32 = app.queue_list.index as u32;
                    let prev: u32 = if current == 0 {
                        app.queue_list.index as u32
                    } else {
                        app.queue_list.index -= 1;
                        (current - 1) as u32
                    };
                    app.conn.conn.swap(current, prev)?;
                    app.update_queue();
                }

                // go to top of list
                KeyCode::Char('g') => app.queue_list.index = 0,

                // go to bottom of list
                KeyCode::Char('G') => app.queue_list.index = app.queue_list.list.len() - 1,

                _ => {}
            }
        }

        SelectedTab::DirectoryBrowser => {
            match key_event.code {
                // Go Up
                KeyCode::Char('j') | KeyCode::Down => app.browser.next(),

                // Go down
                KeyCode::Char('k') | KeyCode::Up => app.browser.prev(),

                // Next directory
                KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right => {
                    // app.update_queue();
                    app.handle_enter()?;
                    app.conn.update_status();
                }

                // head back to previous directory
                KeyCode::Char('h') | KeyCode::Left => app.browser.handle_go_back(&mut app.conn)?,

                // go to top of list
                KeyCode::Char('g') => app.browser.selected = 0,

                // go to bottom of list
                KeyCode::Char('G') => app.browser.selected = app.browser.filetree.len() - 1,

                _ => {}
            }
        }

        SelectedTab::Playlists => {
            match key_event.code {
                // Go Up
                KeyCode::Char('j') | KeyCode::Down => app.pl_list.next(),

                // Go down
                KeyCode::Char('k') | KeyCode::Up => app.pl_list.prev(),

                // go to top of list
                KeyCode::Char('g') => app.pl_list.index = 0,

                // go to bottom of list
                KeyCode::Char('G') => app.pl_list.index = app.pl_list.list.len() - 1,

                // Change playlist name
                KeyCode::Char('e') => app.change_playlist_name()?,

                // add to current playlist
                KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right | KeyCode::Char(' ') => {
                    // app.update_queue();
                    app.conn
                        .load_playlist(app.pl_list.list.get(app.pl_list.index).unwrap())?;
                    app.conn.update_status();
                }
                _ => {}
            }
        }
    }
    Ok(())
}