From 81a95d252713a87552a49fde7137f655b0420f6d Mon Sep 17 00:00:00 2001 From: Nick Dowsett Date: Fri, 29 Nov 2024 17:57:22 +0800 Subject: [PATCH] Start default keybinds implementation --- youtui/src/app/keycommand.rs | 8 +- youtui/src/config.rs | 1 + youtui/src/config/keybinds.rs | 154 +++++++++++++++++++++++++++++++--- 3 files changed, 149 insertions(+), 14 deletions(-) diff --git a/youtui/src/app/keycommand.rs b/youtui/src/app/keycommand.rs index cbe9a72..4da0ade 100644 --- a/youtui/src/app/keycommand.rs +++ b/youtui/src/app/keycommand.rs @@ -70,9 +70,15 @@ impl<'a, A: Action + 'a> From<&'a KeyCommand> for DisplayableCommand<'a> { } impl Keybind { - fn new(code: KeyCode, modifiers: KeyModifiers) -> Self { + pub fn new(code: KeyCode, modifiers: KeyModifiers) -> Self { Self { code, modifiers } } + pub fn new_unmodified(code: KeyCode) -> Self { + Self { + code, + modifiers: KeyModifiers::NONE, + } + } fn contains_keyevent(&self, keyevent: &KeyEvent) -> bool { match self.code { // If key code is a character it may have shift pressed, if that's the case ignore the diff --git a/youtui/src/config.rs b/youtui/src/config.rs index 7dfeccd..6b78d22 100644 --- a/youtui/src/config.rs +++ b/youtui/src/config.rs @@ -46,6 +46,7 @@ pub struct Config { #[derive(Default, Debug, Deserialize)] #[serde(default)] +/// Intermediate representation of Config for serde. pub struct ConfigIR { pub auth_type: AuthType, pub keybinds: YoutuiKeymapIR, diff --git a/youtui/src/config/keybinds.rs b/youtui/src/config/keybinds.rs index 6fbcb73..4638c98 100644 --- a/youtui/src/config/keybinds.rs +++ b/youtui/src/config/keybinds.rs @@ -1,10 +1,15 @@ use std::{collections::HashMap, convert::Infallible, str::FromStr}; +use crossterm::event::KeyModifiers; +use rodio::source::from_iter; use serde::{Deserialize, Serialize}; use crate::app::{ keycommand::{CommandVisibility, Keybind}, - ui::action::AppAction, + ui::{ + action::AppAction, + playlist::PlaylistAction::{DeleteSelected, PlaySelected, ViewBrowser}, + }, }; #[derive(Debug, PartialEq)] @@ -26,18 +31,18 @@ pub struct YoutuiKeymap { impl Default for YoutuiKeymap { fn default() -> Self { Self { - global: todo!(), - playlist: todo!(), - browser: todo!(), - browser_artists: todo!(), - browser_search: todo!(), - browser_songs: todo!(), - help: todo!(), - sort: todo!(), - filter: todo!(), - text_entry: todo!(), - list: todo!(), - log: todo!(), + global: default_global_keybinds(), + playlist: default_playlist_keybings(), + browser: default_browser_keybings(), + browser_artists: default_browser_artists_keybings(), + browser_search: default_browser_search_keybings(), + browser_songs: default_browser_songs_keybings(), + help: default_help_keybings(), + sort: default_sort_keybings(), + filter: default_filter_keybings(), + text_entry: default_text_entry_keybings(), + list: default_list_keybings(), + log: default_log_keybings(), } } } @@ -165,6 +170,29 @@ pub enum KeyEnum { Mode(HashMap>), } +impl KeyEnum { + fn new_key_defaulted(action: A) -> Self { + Self::Key(KeyEnumKey { + action, + value: Default::default(), + visibility: Default::default(), + }) + } + fn new_key(action: A, value: usize, visibility: CommandVisibility) -> Self { + Self::Key(KeyEnumKey { + action, + value, + visibility, + }) + } + fn new_mode(binds: I) -> Self + where + I: IntoIterator)>, + { + Self::Mode(FromIterator::from_iter(binds)) + } +} + impl TryFrom for KeyEnum { type Error = String; fn try_from(value: KeyEnumString) -> std::result::Result { @@ -228,3 +256,103 @@ enum ModeNameEnum { #[serde(untagged)] Name(String), } + +fn default_global_keybinds() -> HashMap> { + FromIterator::from_iter([ + ( + Keybind::new_unmodified(crossterm::event::KeyCode::Char('+')), + KeyEnum::new_key(AppAction::VolUp, 5, CommandVisibility::Standard), + ), + ( + Keybind::new_unmodified(crossterm::event::KeyCode::Char('-')), + KeyEnum::new_key(AppAction::VolDown, 5, CommandVisibility::Standard), + ), + ( + Keybind::new_unmodified(crossterm::event::KeyCode::Char('>')), + KeyEnum::new_key_defaulted(AppAction::NextSong), + ), + ( + Keybind::new_unmodified(crossterm::event::KeyCode::Char('<')), + KeyEnum::new_key_defaulted(AppAction::PrevSong), + ), + ( + Keybind::new_unmodified(crossterm::event::KeyCode::Char(']')), + KeyEnum::new_key(AppAction::SeekForwardS, 5, CommandVisibility::Standard), + ), + ( + Keybind::new_unmodified(crossterm::event::KeyCode::Char('[')), + KeyEnum::new_key(AppAction::SeekBackS, 5, CommandVisibility::Standard), + ), + ( + Keybind::new_unmodified(crossterm::event::KeyCode::F(1)), + KeyEnum::new_key(AppAction::ToggleHelp, 1, CommandVisibility::Global), + ), + ( + Keybind::new_unmodified(crossterm::event::KeyCode::F(10)), + KeyEnum::new_key(AppAction::Quit, 1, CommandVisibility::Standard), + ), + ( + Keybind::new_unmodified(crossterm::event::KeyCode::F(12)), + KeyEnum::new_key(AppAction::ViewLogs, 1, CommandVisibility::Standard), + ), + ( + Keybind::new_unmodified(crossterm::event::KeyCode::Char(' ')), + KeyEnum::new_key_defaulted(AppAction::Pause), + ), + ( + Keybind::new(crossterm::event::KeyCode::Char('c'), KeyModifiers::CONTROL), + KeyEnum::new_key_defaulted(AppAction::Quit), + ), + ]) +} +fn default_playlist_keybings() -> HashMap> { + FromIterator::from_iter([ + ( + Keybind::new_unmodified(crossterm::event::KeyCode::F(5)), + KeyEnum::new_key_defaulted(AppAction::Playlist(ViewBrowser)), + ), + ( + Keybind::new_unmodified(crossterm::event::KeyCode::Enter), + KeyEnum::new_mode([ + ( + Keybind::new_unmodified(crossterm::event::KeyCode::Enter), + KeyEnum::new_key_defaulted(AppAction::Playlist(PlaySelected)), + ), + ( + Keybind::new_unmodified(crossterm::event::KeyCode::Char('d')), + KeyEnum::new_key_defaulted(AppAction::Playlist(DeleteSelected)), + ), + ]), + ), + ]) +} +fn default_browser_keybings() -> HashMap> { + todo!() +} +fn default_browser_artists_keybings() -> HashMap> { + todo!() +} +fn default_browser_search_keybings() -> HashMap> { + todo!() +} +fn default_browser_songs_keybings() -> HashMap> { + todo!() +} +fn default_help_keybings() -> HashMap> { + todo!() +} +fn default_sort_keybings() -> HashMap> { + todo!() +} +fn default_filter_keybings() -> HashMap> { + todo!() +} +fn default_text_entry_keybings() -> HashMap> { + todo!() +} +fn default_log_keybings() -> HashMap> { + todo!() +} +fn default_list_keybings() -> HashMap> { + todo!() +}