feat: add wip support for media keys

This commit is contained in:
Tal 2024-10-03 23:19:00 +02:00
parent 86f88ff34f
commit 706ba97428
3 changed files with 25 additions and 7 deletions

View File

@ -39,7 +39,7 @@ pub enum Messages {
/// Pauses the [Sink]. This will also unpause it if it is paused. /// Pauses the [Sink]. This will also unpause it if it is paused.
Pause, Pause,
/// Change the volume of playback /// Change the volume of playback.
ChangeVolume(f32), ChangeVolume(f32),
} }

View File

@ -14,7 +14,7 @@ use crate::Args;
use super::Player; use super::Player;
use crossterm::{ use crossterm::{
cursor::{Hide, MoveTo, MoveToColumn, MoveUp, Show}, cursor::{Hide, MoveTo, MoveToColumn, MoveUp, Show},
event::{self, KeyCode, KeyModifiers}, event::{self, KeyCode, KeyModifiers, KeyboardEnhancementFlags, PushKeyboardEnhancementFlags},
style::{Print, Stylize}, style::{Print, Stylize},
terminal::{self, Clear, ClearType, EnterAlternateScreen, LeaveAlternateScreen}, terminal::{self, Clear, ClearType, EnterAlternateScreen, LeaveAlternateScreen},
}; };
@ -100,12 +100,19 @@ async fn interface(player: Arc<Player>) -> eyre::Result<()> {
pub async fn start(queue: Arc<Player>, sender: Sender<Messages>, args: Args) -> eyre::Result<()> { pub async fn start(queue: Arc<Player>, sender: Sender<Messages>, args: Args) -> eyre::Result<()> {
crossterm::execute!(stdout(), Hide)?; crossterm::execute!(stdout(), Hide)?;
terminal::enable_raw_mode()?;
if args.alternate { if args.alternate {
crossterm::execute!(stdout(), EnterAlternateScreen, MoveTo(0, 0))?; crossterm::execute!(stdout(), EnterAlternateScreen, MoveTo(0, 0))?;
} }
terminal::enable_raw_mode()?;
if terminal::supports_keyboard_enhancement()? {
crossterm::execute!(
stdout(),
PushKeyboardEnhancementFlags(KeyboardEnhancementFlags::DISAMBIGUATE_ESCAPE_CODES,)
)?;
}
task::spawn(interface(Arc::clone(&queue))); task::spawn(interface(Arc::clone(&queue)));
loop { loop {
@ -119,7 +126,7 @@ pub async fn start(queue: Arc<Player>, sender: Sender<Messages>, args: Args) ->
KeyCode::Right => Messages::ChangeVolume(0.01), KeyCode::Right => Messages::ChangeVolume(0.01),
KeyCode::Down => Messages::ChangeVolume(-0.1), KeyCode::Down => Messages::ChangeVolume(-0.1),
KeyCode::Left => Messages::ChangeVolume(-0.01), KeyCode::Left => Messages::ChangeVolume(-0.01),
KeyCode::Char(character) => match character { KeyCode::Char(character) => match character.to_ascii_lowercase() {
// Ctrl+C // Ctrl+C
'c' if event.modifiers == KeyModifiers::CONTROL => break, 'c' if event.modifiers == KeyModifiers::CONTROL => break,
@ -137,6 +144,18 @@ pub async fn start(queue: Arc<Player>, sender: Sender<Messages>, args: Args) ->
'-' | '_' => Messages::ChangeVolume(-0.1), '-' | '_' => Messages::ChangeVolume(-0.1),
_ => continue, _ => continue,
}, },
// Media keys
KeyCode::Media(media) => match media {
event::MediaKeyCode::Play => Messages::Pause,
event::MediaKeyCode::Pause => Messages::Pause,
event::MediaKeyCode::PlayPause => Messages::Pause,
event::MediaKeyCode::Stop => Messages::Pause,
event::MediaKeyCode::TrackNext => Messages::Next,
event::MediaKeyCode::LowerVolume => Messages::ChangeVolume(-0.1),
event::MediaKeyCode::RaiseVolume => Messages::ChangeVolume(0.1),
event::MediaKeyCode::MuteVolume => Messages::ChangeVolume(-1.0),
_ => continue,
},
_ => continue, _ => continue,
}; };

View File

@ -102,11 +102,10 @@ pub fn action(player: &Arc<Player>, width: usize) -> String {
} }
} }
/// Creates the bottom controls bar, and also spaces it properly.
pub fn controls(width: usize) -> String { pub fn controls(width: usize) -> String {
let controls = [["[s]", "kip"], ["[p]", "ause"], ["[q]", "uit"]]; let controls = [["[s]", "kip"], ["[p]", "ause"], ["[q]", "uit"]];
let len: usize = controls.concat().iter().map(|x| x.len()).sum(); let len: usize = controls.concat().iter().map(|x| x.len()).sum();
let controls = controls.map(|x| format!("{}{}", x[0].bold(), x[1])); let controls = controls.map(|x| format!("{}{}", x[0].bold(), x[1]));
controls.join(&" ".repeat((width - len) / (controls.len() - 1))) controls.join(&" ".repeat((width - len) / (controls.len() - 1)))