Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add volume modification by mouse scrolling #604

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions spotify_player/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,20 @@ impl Client {
playback.volume = Some(volume as u32);
playback.mute_state = None;
}
PlayerRequest::VolumeUp => {
let volume = std::cmp::min(playback.volume.unwrap_or_default() + 5, 100_u32);
self.volume(volume as u8, device_id).await?;

playback.volume = Some(volume);
playback.mute_state = None;
}
PlayerRequest::VolumeDown => {
let volume = playback.volume.unwrap_or_default().saturating_sub(5_u32);
self.volume(volume as u8, device_id).await?;

playback.volume = Some(volume);
playback.mute_state = None;
}
PlayerRequest::ToggleMute => {
let new_mute_state = match playback.mute_state {
None => {
Expand Down
2 changes: 2 additions & 0 deletions spotify_player/src/client/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ pub enum PlayerRequest {
Repeat,
Shuffle,
Volume(u8),
VolumeUp,
VolumeDown,
ToggleMute,
TransferPlayback(String, bool),
StartPlayback(Playback, Option<bool>),
Expand Down
63 changes: 44 additions & 19 deletions spotify_player/src/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,27 +58,52 @@ fn handle_mouse_event(
client_pub: &flume::Sender<ClientRequest>,
state: &SharedState,
) -> Result<()> {
// a left click event
if let crossterm::event::MouseEventKind::Down(crossterm::event::MouseButton::Left) = event.kind
{
tracing::debug!("Handling mouse event: {event:?}");
let rect = state.ui.lock().playback_progress_bar_rect;
if event.row == rect.y {
// calculate the seek position (in ms) based on the mouse click position,
// the progress bar's width and the track's duration (in ms)
let duration = state
.player
.read()
.current_playing_track()
.map(|t| t.duration);
if let Some(duration) = duration {
let position_ms =
(duration.num_milliseconds()) * (event.column as i64) / (rect.width as i64);
client_pub.send(ClientRequest::Player(PlayerRequest::SeekTrack(
chrono::Duration::try_milliseconds(position_ms).unwrap(),
)))?;
match event.kind {
// a left click event
crossterm::event::MouseEventKind::Down(crossterm::event::MouseButton::Left) =>
{
tracing::debug!("Handling mouse event: {event:?}");
let rect = state.ui.lock().playback_progress_bar_rect;
if event.row == rect.y {
// calculate the seek position (in ms) based on the mouse click position,
// the progress bar's width and the track's duration (in ms)
let duration = state
.player
.read()
.current_playing_track()
.map(|t| t.duration);
if let Some(duration) = duration {
let position_ms =
(duration.num_milliseconds()) * (event.column as i64) / (rect.width as i64);
client_pub.send(ClientRequest::Player(PlayerRequest::SeekTrack(
chrono::Duration::try_milliseconds(position_ms).unwrap(),
)))?;
}
}
}
// a mouse scroll up event
crossterm::event::MouseEventKind::ScrollUp => {
// the key sequence m s u stands for MouseScrollUp,
let mouse_scroll_up = "m s u".into();
let mouse_volume_up_is_set = &config::get_config().keymap_config.find_command_or_action_from_key_sequence(&mouse_scroll_up);

// if the MouseScrollUp is set up, raise the volume
if mouse_volume_up_is_set.is_some() {
client_pub.send(ClientRequest::Player(PlayerRequest::VolumeUp))?;
}
}
// a mouse scroll down event
crossterm::event::MouseEventKind::ScrollDown => {
// the key sequence m s d stands for MouseScrollDown
let mouse_scroll_down = "m s d".into();
let mouse_volume_down_is_set = &config::get_config().keymap_config.find_command_or_action_from_key_sequence(&mouse_scroll_down);

// if MouseScrollDown is set up, lower the volume
if mouse_volume_down_is_set.is_some() {
client_pub.send(ClientRequest::Player(PlayerRequest::VolumeDown))?;
}
}
_ => {}
}
Ok(())
}
Expand Down
Loading