feat: add volume control with the right stick and keys
Right stick up and down change the volume in steps of 5 and clicking it mutes; + (or =), - and m do the same on a keyboard, on every screen. The stick's dominant axis wins, so a diagonal push does not seek and change the volume at once. Any step unmutes. The level goes to playbin's volume on a cubic scale, so steps sound even, and to its mute. A pill shows the level for a moment after each change, the now-playing screen shows it under the times, and it is saved in settings.toml once the changes stop and on quit. Hint icons for the right stick and the keys are from Kenney's Input Prompts 1.5, like the others.
This commit is contained in:
+89
@@ -65,6 +65,12 @@ pub fn shutdown() {
|
||||
let app = APP.with(|cell| cell.borrow_mut().take());
|
||||
if let Some(mut app) = app {
|
||||
app.save_queue();
|
||||
// A volume change made just before quitting is still waiting to save.
|
||||
if app.volume_save_timer.running()
|
||||
&& let Err(e) = config::save_settings(&app.settings)
|
||||
{
|
||||
log::warn!("saving settings: {e:#}");
|
||||
}
|
||||
if let Some(i) = app.inhibitor.take() {
|
||||
i.shutdown();
|
||||
}
|
||||
@@ -247,6 +253,10 @@ pub struct App {
|
||||
last_save: Instant,
|
||||
login_focus: i32,
|
||||
toast_timer: Timer,
|
||||
/// Hides the volume overlay a moment after the last change.
|
||||
volume_timer: Timer,
|
||||
/// Saves the volume once a run of changes is over.
|
||||
volume_save_timer: Timer,
|
||||
/// Keyboard keys or the buttons of the controller used last.
|
||||
prompts: Prompts,
|
||||
/// Decoded hint icons, so a selection move does not parse SVG again.
|
||||
@@ -323,6 +333,8 @@ impl App {
|
||||
last_save: Instant::now(),
|
||||
login_focus: 0,
|
||||
toast_timer: Timer::default(),
|
||||
volume_timer: Timer::default(),
|
||||
volume_save_timer: Timer::default(),
|
||||
art_shown: None,
|
||||
flip_timer: Timer::default(),
|
||||
prompts: Prompts::Keyboard,
|
||||
@@ -352,6 +364,7 @@ impl App {
|
||||
self.toast(format!("Playback unavailable: {e:#}"));
|
||||
}
|
||||
}
|
||||
self.apply_volume();
|
||||
|
||||
match config::load() {
|
||||
Ok(Some(cfg)) => {
|
||||
@@ -509,6 +522,16 @@ impl App {
|
||||
w.set_fullscreen(!w.is_fullscreen());
|
||||
return;
|
||||
}
|
||||
// Everywhere, the login screen included.
|
||||
Action::VolumeUp | Action::VolumeDown => {
|
||||
self.change_volume(a == Action::VolumeUp);
|
||||
return;
|
||||
}
|
||||
Action::ToggleMute => {
|
||||
self.settings.muted = !self.settings.muted;
|
||||
self.volume_changed();
|
||||
return;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
match self.screen {
|
||||
@@ -935,6 +958,53 @@ impl App {
|
||||
self.ui.set_np_backdrop_right(right);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- volume
|
||||
|
||||
fn change_volume(&mut self, up: bool) {
|
||||
let v = step_volume(self.settings.volume, up);
|
||||
// Any change unmutes, as on a phone: turning it up while muted should
|
||||
// be heard.
|
||||
let unmute = self.settings.muted;
|
||||
if v == self.settings.volume && !unmute {
|
||||
self.show_volume();
|
||||
return;
|
||||
}
|
||||
self.settings.volume = v;
|
||||
self.settings.muted = false;
|
||||
self.volume_changed();
|
||||
}
|
||||
|
||||
fn volume_changed(&mut self) {
|
||||
self.apply_volume();
|
||||
self.show_volume();
|
||||
// Saved once the changes stop, not for every step of a held stick.
|
||||
self.volume_save_timer
|
||||
.start(slint::TimerMode::SingleShot, Duration::from_millis(800), || {
|
||||
with_app(|app| {
|
||||
if let Err(e) = config::save_settings(&app.settings) {
|
||||
app.toast(format!("Could not save settings: {e:#}"));
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
fn apply_volume(&self) {
|
||||
if let Some(p) = &self.player {
|
||||
p.set_volume(self.settings.volume);
|
||||
p.set_muted(self.settings.muted);
|
||||
}
|
||||
self.ui.set_volume(self.settings.volume as i32);
|
||||
self.ui.set_muted(self.settings.muted);
|
||||
}
|
||||
|
||||
fn show_volume(&mut self) {
|
||||
self.ui.set_volume_shown(true);
|
||||
self.volume_timer
|
||||
.start(slint::TimerMode::SingleShot, Duration::from_millis(1500), || {
|
||||
with_app(|app| app.ui.set_volume_shown(false))
|
||||
});
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- theme
|
||||
|
||||
/// The palette the UI should show: from the playing track's cover when
|
||||
@@ -1428,6 +1498,8 @@ impl App {
|
||||
h(Control::PlayPause, "Play / Pause");
|
||||
h(Control::Skip, "Skip");
|
||||
h(Control::Seek, "Seek");
|
||||
h(Control::Volume, "Volume");
|
||||
h(Control::Mute, "Mute");
|
||||
}
|
||||
}
|
||||
self.ui.set_hints(ModelRc::new(VecModel::from(hints)));
|
||||
@@ -2181,6 +2253,12 @@ fn settings_data(s: &Settings) -> PageData {
|
||||
}
|
||||
}
|
||||
|
||||
/// One step of the volume, kept within 0 to 100.
|
||||
fn step_volume(v: u8, up: bool) -> u8 {
|
||||
let v = v.min(100);
|
||||
if up { (v + 5).min(100) } else { v.saturating_sub(5) }
|
||||
}
|
||||
|
||||
/// One step of the cover backdrop setting, kept within 0 to 100.
|
||||
fn step_backdrop(v: u8, up: bool) -> u8 {
|
||||
let v = v.min(100);
|
||||
@@ -2396,6 +2474,17 @@ mod tests {
|
||||
assert_ne!(a, c);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn volume_steps_stay_in_range() {
|
||||
assert_eq!(step_volume(50, true), 55);
|
||||
assert_eq!(step_volume(50, false), 45);
|
||||
assert_eq!(step_volume(100, true), 100);
|
||||
assert_eq!(step_volume(98, true), 100);
|
||||
assert_eq!(step_volume(0, false), 0);
|
||||
assert_eq!(step_volume(3, false), 0);
|
||||
assert_eq!(step_volume(200, false), 95);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_backdrop_looks_like_before_it_was_a_setting() {
|
||||
assert_eq!(backdrop_look(config::COVER_BACKDROP_DEFAULT), (0.16, 0.67, 0.95));
|
||||
|
||||
Reference in New Issue
Block a user