From f2b90514899af2e55f4fca9ad759029aa7511e0e Mon Sep 17 00:00:00 2001 From: krolxon Date: Sat, 15 Jun 2024 00:06:35 +0530 Subject: change event to event_handler --- src/event/event.rs | 79 ----------- src/event/handler.rs | 335 -------------------------------------------- src/event/mod.rs | 6 - src/event/new_pl_keys.rs | 47 ------- src/event/pl_append_keys.rs | 115 --------------- src/event/pl_rename_keys.rs | 43 ------ src/event/search_keys.rs | 110 --------------- 7 files changed, 735 deletions(-) delete mode 100755 src/event/event.rs delete mode 100755 src/event/handler.rs delete mode 100755 src/event/mod.rs delete mode 100644 src/event/new_pl_keys.rs delete mode 100755 src/event/pl_append_keys.rs delete mode 100755 src/event/pl_rename_keys.rs delete mode 100755 src/event/search_keys.rs (limited to 'src/event') diff --git a/src/event/event.rs b/src/event/event.rs deleted file mode 100755 index 439c31b..0000000 --- a/src/event/event.rs +++ /dev/null @@ -1,79 +0,0 @@ -use crate::app::AppResult; -use crossterm::event::{self, Event as CrosstermEvent, KeyEvent, MouseEvent}; -use std::sync::mpsc; -use std::thread; -use std::time::{Duration, Instant}; - -/// Terminal events. -#[derive(Clone, Copy, Debug)] -pub enum Event { - /// Terminal tick. - Tick, - /// Key press. - Key(KeyEvent), - /// Mouse click/scroll. - Mouse(MouseEvent), - /// Terminal resize. - Resize(u16, u16), -} - -/// Terminal event handler. -#[allow(dead_code)] -#[derive(Debug)] -pub struct EventHandler { - /// Event sender channel. - sender: mpsc::Sender, - /// Event receiver channel. - receiver: mpsc::Receiver, - /// Event handler thread. - handler: thread::JoinHandle<()>, -} - -impl EventHandler { - /// Constructs a new instance of [`EventHandler`]. - pub fn new(tick_rate: u64) -> Self { - let tick_rate = Duration::from_millis(tick_rate); - let (sender, receiver) = mpsc::channel(); - let handler = { - let sender = sender.clone(); - thread::spawn(move || { - let mut last_tick = Instant::now(); - loop { - let timeout = tick_rate - .checked_sub(last_tick.elapsed()) - .unwrap_or(tick_rate); - - if event::poll(timeout).expect("failed to poll new events") { - match event::read().expect("unable to read event") { - CrosstermEvent::Key(e) => sender.send(Event::Key(e)), - CrosstermEvent::Mouse(e) => sender.send(Event::Mouse(e)), - CrosstermEvent::Resize(w, h) => sender.send(Event::Resize(w, h)), - CrosstermEvent::FocusGained => Ok(()), - CrosstermEvent::FocusLost => Ok(()), - CrosstermEvent::Paste(_) => unimplemented!(), - } - .expect("failed to send terminal event") - } - - if last_tick.elapsed() >= tick_rate { - sender.send(Event::Tick).expect("failed to send tick event"); - last_tick = Instant::now(); - } - } - }) - }; - Self { - sender, - receiver, - handler, - } - } - - /// Receive the next event from the handler thread. - /// - /// This function will always block the current thread if - /// there is no data available and it's possible for more data to be sent. - pub fn next(&self) -> AppResult { - Ok(self.receiver.recv()?) - } -} diff --git a/src/event/handler.rs b/src/event/handler.rs deleted file mode 100755 index f34a5f2..0000000 --- a/src/event/handler.rs +++ /dev/null @@ -1,335 +0,0 @@ -use crate::{ - app::{App, AppResult, SelectedTab}, - connection::VolumeStatus, - ui::InputMode, -}; -use crossterm::event::{KeyCode, KeyEvent, KeyModifiers, MouseEvent, MouseEventKind}; -use std::time::Duration; - -use super::{new_pl_keys, 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.inputmode == InputMode::NewPlaylist { - new_pl_keys::handle_new_pl_keys(key_event, app)?; - } else if app.playlist_popup { - pl_append_keys::hande_pl_append_keys(key_event, app)?; - } else { - // 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(); - app.conn.update_status(); - } - - // Pause - KeyCode::Char('s') => { - app.conn.pause(); - app.conn.update_status(); - } - - // 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()?; - app.conn.update_status(); - } - - // add to queue - KeyCode::Char('a') => app.playlist_popup = true, - - // Fast forward - KeyCode::Char('f') => { - if !app.queue_list.list.is_empty() { - let status = app.conn.conn.status().unwrap_or_default(); - let place = status.song.unwrap_or_default().pos; - let (pos, _) = status.time.unwrap_or_default(); - let pos = Duration::from_secs(pos.as_secs().wrapping_add(2)); - app.conn.conn.seek(place, pos)?; - app.conn.update_status(); - } - } - - // backward - KeyCode::Char('b') => { - if !app.queue_list.list.is_empty() { - let status = app.conn.conn.status().unwrap_or_default(); - let place = status.song.unwrap_or_default().pos; - let (pos, _) = status.time.unwrap_or_default(); - let pos = Duration::from_secs(pos.as_secs().wrapping_sub(2)); - app.conn.conn.seek(place, pos)?; - app.conn.update_status(); - } - } - - // 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(); - app.conn.update_status(); - } - } - - // Play previous song - KeyCode::Char('<') => { - if !app.queue_list.list.is_empty() { - app.conn.conn.prev()?; - app.update_queue(); - app.conn.update_status(); - } - } - - // Volume controls - KeyCode::Char('=') | KeyCode::Char('+') => { - app.conn.inc_volume(2); - app.conn.update_status(); - } - - KeyCode::Char('-') => { - app.conn.dec_volume(2); - app.conn.update_status(); - } - - // Toggle Mute - KeyCode::Char('m') => { - match app.conn.volume_status { - VolumeStatus::Muted(v) => { - app.conn.conn.volume(v)?; - app.conn.volume_status = VolumeStatus::Unmuted; - } - VolumeStatus::Unmuted => { - let current_volume = app.conn.status.volume; - app.conn.conn.volume(0)?; - app.conn.volume_status = VolumeStatus::Muted(current_volume); - } - } - app.conn.update_status(); - } - - // Update MPD database - KeyCode::Char('U') => { - app.conn.conn.rescan()?; - app.should_update_song_list = true; - } - - // 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(' ') => { - 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() - && app.queue_list.index != 0 - { - app.queue_list.index -= 1; - } - - app.conn.conn.delete(app.queue_list.index as u32)?; - - if app.queue_list.index >= app.queue_list.list.len().saturating_sub(1) - && app.queue_list.index != 0 - { - app.queue_list.index -= 1; - } - - app.conn.update_status(); - 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 - }; - app.conn.conn.swap(current, next)?; - app.update_queue(); - app.conn.update_status(); - } - - // 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 - }; - app.conn.conn.swap(current, prev)?; - app.update_queue(); - app.conn.update_status(); - } - - // 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, - - // Playlist Rename - KeyCode::Char('R') => { - app.inputmode = InputMode::PlaylistRename; - } - - // add to current playlist - KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right | KeyCode::Char(' ') => { - // app.update_queue(); - if !app.pl_list.list.is_empty() { - app.conn - .load_playlist(app.pl_list.list.get(app.pl_list.index).unwrap())?; - app.conn.update_status(); - } - } - _ => {} - } - } - } - } - Ok(()) -} - -pub fn handle_mouse_events(mouse_event: MouseEvent, app: &mut App) -> AppResult<()> { - match mouse_event.kind { - MouseEventKind::ScrollUp => app.handle_scroll_up(), - MouseEventKind::ScrollDown => app.handle_scroll_down(), - MouseEventKind::Down(button) => { - let (x, y) = (mouse_event.column, mouse_event.row); - if button == crossterm::event::MouseButton::Left { - app.handle_mouse_left_click(x, y)?; - } - } - _ => {} - } - Ok(()) -} diff --git a/src/event/mod.rs b/src/event/mod.rs deleted file mode 100755 index efc9b9e..0000000 --- a/src/event/mod.rs +++ /dev/null @@ -1,6 +0,0 @@ -pub mod event; -pub mod handler; -pub mod search_keys; -pub mod pl_rename_keys; -pub mod pl_append_keys; -pub mod new_pl_keys; diff --git a/src/event/new_pl_keys.rs b/src/event/new_pl_keys.rs deleted file mode 100644 index 9150bec..0000000 --- a/src/event/new_pl_keys.rs +++ /dev/null @@ -1,47 +0,0 @@ -use crate::{ - app::{App, AppResult}, - ui::InputMode, -}; -use crossterm::event::{KeyCode, KeyEvent}; - -pub fn handle_new_pl_keys(key_event: KeyEvent, app: &mut App) -> AppResult<()> { - match key_event.code { - KeyCode::Esc => { - app.pl_new_pl_input.clear(); - app.reset_cursor(); - app.inputmode = InputMode::Normal; - } - KeyCode::Char(to_insert) => { - app.enter_char(to_insert); - } - KeyCode::Enter => { - let pl_name = &app.pl_new_pl_input; - - for song in app.pl_new_pl_songs_buffer.iter() { - app.conn.conn.pl_push(pl_name, song)?; - } - app.pl_new_pl_input.clear(); - - app.pl_list.list = App::get_playlist(&mut app.conn.conn)?; - app.append_list = App::get_append_list(&mut app.conn.conn)?; - - app.reset_cursor(); - app.inputmode = InputMode::Normal; - } - - KeyCode::Backspace => { - app.delete_char(); - } - - KeyCode::Left => { - app.move_cursor_left(); - } - - KeyCode::Right => { - app.move_cursor_right(); - } - - _ => {} - } - Ok(()) -} diff --git a/src/event/pl_append_keys.rs b/src/event/pl_append_keys.rs deleted file mode 100755 index 3d2702c..0000000 --- a/src/event/pl_append_keys.rs +++ /dev/null @@ -1,115 +0,0 @@ -use crate::app::{App, AppResult, SelectedTab}; -use crate::ui::InputMode; -use crate::utils::FileExtension; -use crossterm::event::{KeyCode, KeyEvent}; -use std::path::Path; - -pub fn hande_pl_append_keys(key_event: KeyEvent, app: &mut App) -> AppResult<()> { - match key_event.code { - KeyCode::Char('q') | KeyCode::Esc => { - app.playlist_popup = false; - } - - KeyCode::Char('j') | KeyCode::Down => app.append_list.next(), - KeyCode::Char('k') | KeyCode::Up => app.append_list.prev(), - - KeyCode::Enter => { - // name of highlighted playlist in append list - let pl_name = &app.append_list.get_item_at_current_index(); - - match app.selected_tab { - SelectedTab::Queue => { - // Just exit out the menu if no item is selected in the Queue - if app.queue_list.list.is_empty() { - app.playlist_popup = false; - return Ok(()); - } - - if let Ok(songs) = app.conn.conn.songs(app.queue_list.index as u32) { - let option_song = songs.first(); - if let Some(song) = option_song { - if *pl_name == "Current Playlist" { - app.conn.conn.push(song)?; - app.update_queue(); - } else if *pl_name == "New Playlist" { - app.pl_new_pl_songs_buffer.clear(); - app.pl_new_pl_songs_buffer.push(song.clone()); - app.inputmode = InputMode::NewPlaylist; - } else { - app.conn.add_to_playlist(pl_name, song)?; - } - } - } - } - - SelectedTab::DirectoryBrowser => { - let (t, f) = app.browser.filetree.get(app.browser.selected).unwrap(); - if t == "file" { - let short_path = f; - if let Some(full_path) = app.conn.get_full_path(short_path) { - let song = app.conn.get_song_with_only_filename(full_path); - - if *pl_name == "Current Playlist" { - app.conn.conn.push(&song)?; - app.update_queue(); - } else if *pl_name == "New Playlist" { - app.pl_new_pl_songs_buffer.clear(); - app.pl_new_pl_songs_buffer.push(song.clone()); - app.inputmode = InputMode::NewPlaylist; - } else { - app.conn.add_to_playlist(pl_name, &song)?; - } - } - } else if t == "directory" { - let file = format!("{}/{}", app.browser.path, f); - app.pl_new_pl_songs_buffer.clear(); - for (t, f) in app.conn.conn.listfiles(&file)?.iter() { - // dir_vec.push((t, f)); - if t == "file" - && Path::new(&f).has_extension(&[ - "mp3", "ogg", "flac", "m4a", "wav", "aac", "opus", "ape", - "wma", "mpc", "aiff", "dff", "mp2", "mka", - ]) - { - let full_path = app.conn.get_full_path(f).unwrap_or_default(); - let song = app.conn.get_song_with_only_filename(full_path); - if *pl_name == "Current Playlist" { - app.conn.conn.push(&song)?; - } else if *pl_name == "New Playlist" { - app.pl_new_pl_songs_buffer.push(song.clone()); - app.inputmode = InputMode::NewPlaylist; - } else { - app.conn.add_to_playlist(pl_name, &song)?; - } - } - } - } - } - - SelectedTab::Playlists => { - let playlist_name = app.pl_list.get_item_at_current_index(); - if *pl_name == "Current Playlist" { - app.conn.load_playlist(playlist_name)?; - app.update_queue(); - } else if *pl_name == "New Playlist" { - app.inputmode = InputMode::NewPlaylist; - } else { - let songs = app.conn.conn.playlist(playlist_name)?; - for song in songs { - // We ignore the Err() since there could be songs in playlists, which do not exist in the db anymore. - // So instead of panicking, we just ignore if the song does not exists - app.conn.add_to_playlist(pl_name, &song).unwrap_or(()); - } - } - } - } - - // hide the playlist popup - app.playlist_popup = false; - app.append_list.index = 0; - } - _ => {} - } - - Ok(()) -} diff --git a/src/event/pl_rename_keys.rs b/src/event/pl_rename_keys.rs deleted file mode 100755 index c9fc050..0000000 --- a/src/event/pl_rename_keys.rs +++ /dev/null @@ -1,43 +0,0 @@ -use crate::{ - app::{App, AppResult}, - ui::InputMode, -}; -use crossterm::event::{KeyCode, KeyEvent}; - -pub fn handle_pl_rename_keys(key_event: KeyEvent, app: &mut App) -> AppResult<()> { - match key_event.code { - KeyCode::Esc => { - app.pl_newname_input.clear(); - app.reset_cursor(); - app.inputmode = InputMode::Normal; - } - KeyCode::Char(to_insert) => { - app.enter_char(to_insert); - } - KeyCode::Enter => { - app.conn.conn.pl_rename( - app.pl_list.get_item_at_current_index(), - &app.pl_newname_input, - )?; - app.pl_list.list = App::get_playlist(&mut app.conn.conn)?; - app.pl_newname_input.clear(); - app.reset_cursor(); - app.inputmode = InputMode::Normal; - } - - KeyCode::Backspace => { - app.delete_char(); - } - - KeyCode::Left => { - app.move_cursor_left(); - } - - KeyCode::Right => { - app.move_cursor_right(); - } - - _ => {} - } - Ok(()) -} diff --git a/src/event/search_keys.rs b/src/event/search_keys.rs deleted file mode 100755 index db59fa5..0000000 --- a/src/event/search_keys.rs +++ /dev/null @@ -1,110 +0,0 @@ -use crate::{ - app::{App, AppResult, SelectedTab}, - ui::InputMode, -}; -use crossterm::event::{KeyCode, KeyEvent}; -use rust_fuzzy_search::{self, fuzzy_search_sorted}; - -pub fn handle_search_keys(key_event: KeyEvent, app: &mut App) -> AppResult<()> { - match app.selected_tab { - SelectedTab::DirectoryBrowser => { - let list: Vec<&str> = app - .browser - .filetree - .iter() - .map(|(_, f)| f.as_str()) - .collect::>(); - - let res: Vec<(&str, f32)> = fuzzy_search_sorted(&app.search_input, &list); - let res = res.iter().map(|(x, _)| *x).collect::>(); - - for (i, (_, item)) in app.browser.filetree.iter().enumerate() { - if item.contains(res.first().unwrap()) { - app.browser.selected = i; - } - } - } - - SelectedTab::Queue => { - let list: Vec<&str> = app - .queue_list - .list - .iter() - .map(|f| f.file.as_str()) - .collect::>(); - let res: Vec<(&str, f32)> = fuzzy_search_sorted(&app.search_input, &list); - let res = res.iter().map(|(x, _)| *x).collect::>(); - - for (i, item) in app.queue_list.list.iter().enumerate() { - if item.file.contains(res.first().unwrap()) { - app.queue_list.index = i; - } - } - } - - SelectedTab::Playlists => { - let list: Vec<&str> = app - .pl_list - .list - .iter() - .map(|f| f.as_str()) - .collect::>(); - let res: Vec<(&str, f32)> = fuzzy_search_sorted(&app.search_input, &list); - let res = res.iter().map(|(x, _)| *x).collect::>(); - - for (i, item) in app.pl_list.list.iter().enumerate() { - if item.contains(res.first().unwrap()) { - app.pl_list.index = i; - } - } - } - } - - // Keybind for searching - // - // Keybinds for when the search prompt is visible - match key_event.code { - KeyCode::Esc => { - app.inputmode = InputMode::Normal; - } - KeyCode::Char(to_insert) => { - app.enter_char(to_insert); - } - KeyCode::Enter => { - let list: Vec<&str> = app - .browser - .filetree - .iter() - .map(|(_, f)| f.as_str()) - .collect::>(); - - let res: Vec<(&str, f32)> = fuzzy_search_sorted(&app.search_input, &list); - let (res, _) = res.first().unwrap(); - - for (i, (_, item)) in app.browser.filetree.iter().enumerate() { - if item.contains(res) { - app.browser.selected = i; - } - } - - app.search_input.clear(); - app.reset_cursor(); - app.inputmode = InputMode::Normal; - } - - KeyCode::Backspace => { - app.delete_char(); - } - - KeyCode::Left => { - app.move_cursor_left(); - } - - KeyCode::Right => { - app.move_cursor_right(); - } - - _ => {} - } - Ok(()) -} -- cgit v1.2.3