Clamp volume values
@@ -17,6 +17,7 @@ use rspotify::spotify::model::search::{
use rspotify::spotify::model::track::{FullTrack, SavedTrack, SimplifiedTrack};
use rspotify::spotify::model::user::PrivateUser;
use rspotify::spotify::senum::{Country, RepeatState};
+use std::cmp::{max, min};
use std::collections::HashSet;
use std::time::Instant;
use tui::layout::Rect;
@@ -477,8 +478,10 @@ impl App {
pub fn increase_volume(&mut self) {
if let Some(context) = self.current_playback_context.clone() {
- let next_volume = context.device.volume_percent as u8 + 10;
- if next_volume <= 100 {
+ let current_volume = context.device.volume_percent as u8;
+ let next_volume = min(current_volume + 10, 100);
+
+ if next_volume != current_volume {
self.change_volume(next_volume);
}
}
@@ -486,10 +489,11 @@ impl App {
pub fn decrease_volume(&mut self) {
if let Some(context) = self.current_playback_context.clone() {
- let volume = context.device.volume_percent;
- if volume >= 10 {
- let next_volume = context.device.volume_percent as u8 - 10;
- self.change_volume(next_volume);
+ let current_volume = context.device.volume_percent as i8;
+ let next_volume = max(current_volume - 10, 0);
+
+ if next_volume != current_volume {
+ self.change_volume(next_volume as u8);
}
}
}
