Skip to content

Commit

Permalink
Allow changing of volume in different steps (#629)
Browse files Browse the repository at this point in the history
Resolves #616
  • Loading branch information
DOD-101 authored Dec 8, 2024
1 parent 5087bde commit ff4eaac
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 19 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,7 @@ List of supported commands:
| `Repeat` | cycle the repeat mode | `C-r` |
| `ToggleFakeTrackRepeatMode` | toggle fake track repeat mode | `M-r` |
| `Shuffle` | toggle the shuffle mode | `C-s` |
| `VolumeUp` | increase playback volume by 5% | `+` |
| `VolumeDown` | decrease playback volume by 5% | `-` |
| `VolumeChange` | change playback volume by an offset (default shortcuts use 5%) | `+`, `-` |
| `Mute` | toggle playback volume between 0% and previous level | `_` |
| `SeekForward` | seek forward by 5s | `>` |
| `SeekBackward` | seek backward by 5s | `<` |
Expand Down
3 changes: 3 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,9 @@ key_sequence = "M-enter"
[[keymaps]]
command = "None"
key_sequence = "q"
[[keymaps]]
command = { VolumeChange = { offset = 1 } }
key_sequence = "-"
```

## Actions
Expand Down
15 changes: 10 additions & 5 deletions spotify_player/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ pub enum Command {
Repeat,
ToggleFakeTrackRepeatMode,
Shuffle,
VolumeUp,
VolumeDown,
VolumeChange {
offset: i32,
},
Mute,
SeekForward,
SeekBackward,
Expand Down Expand Up @@ -284,7 +285,11 @@ pub fn construct_episode_actions(episode: &Episode, _data: &DataReadGuard) -> Ve
}

impl Command {
pub fn desc(self) -> &'static str {
pub fn desc(self) -> String {
if let Self::VolumeChange { offset } = self {
return format!("change playback volume by {offset}");
}

match self {
Self::None => "do nothing",
Self::NextTrack => "next track",
Expand All @@ -294,8 +299,6 @@ impl Command {
Self::Repeat => "cycle the repeat mode",
Self::ToggleFakeTrackRepeatMode => "toggle fake track repeat mode",
Self::Shuffle => "toggle the shuffle mode",
Self::VolumeUp => "increase playback volume by 5%",
Self::VolumeDown => "decrease playback volume by 5%",
Self::Mute => "toggle playback volume between 0% and previous level",
Self::SeekForward => "seek forward by 5s",
Self::SeekBackward => "seek backward by 5s",
Expand Down Expand Up @@ -355,6 +358,8 @@ impl Command {
Self::MovePlaylistItemUp => "move playlist item up one position",
Self::MovePlaylistItemDown => "move playlist item down one position",
Self::CreatePlaylist => "create a new playlist",
Self::VolumeChange { offset: _ } => unreachable!(),
}
.to_string()
}
}
4 changes: 2 additions & 2 deletions spotify_player/src/config/keymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ impl Default for KeymapConfig {
},
Keymap {
key_sequence: "+".into(),
command: Command::VolumeUp,
command: Command::VolumeChange { offset: 5 },
},
Keymap {
key_sequence: "-".into(),
command: Command::VolumeDown,
command: Command::VolumeChange { offset: -5 },
},
Keymap {
key_sequence: "_".into(),
Expand Down
12 changes: 2 additions & 10 deletions spotify_player/src/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,18 +547,10 @@ fn handle_global_command(
Command::Shuffle => {
client_pub.send(ClientRequest::Player(PlayerRequest::Shuffle))?;
}
Command::VolumeUp => {
Command::VolumeChange { offset } => {
if let Some(ref playback) = state.player.read().buffered_playback {
if let Some(volume) = playback.volume {
let volume = std::cmp::min(volume + 5, 100_u32);
client_pub.send(ClientRequest::Player(PlayerRequest::Volume(volume as u8)))?;
}
}
}
Command::VolumeDown => {
if let Some(ref playback) = state.player.read().buffered_playback {
if let Some(volume) = playback.volume {
let volume = volume.saturating_sub(5_u32);
let volume = std::cmp::min(volume as i32 + offset, 100_i32);
client_pub.send(ClientRequest::Player(PlayerRequest::Volume(volume as u8)))?;
}
}
Expand Down

0 comments on commit ff4eaac

Please sign in to comment.