Added playback_state config option (#783)

* Added playback_state config option.
It can be used to pre-define a certain playback state for the player.

* Changed playback_state to use enums and removed redundant check for the "Playing" state.
This commit is contained in:
Bettehem
2022-04-28 11:03:17 +03:00
committed by GitHub
parent 0bbb4932e5
commit dfecb759a0
3 changed files with 37 additions and 9 deletions

View File

@@ -308,7 +308,7 @@ runtime use the command prompt by typing `:reload`.
Possible configuration values are: Possible configuration values are:
| Name | Description | Possible values | Default | | Name | Description | Possible values | Default |
| :----------------------- | :------------------------------------------ | :------------------------------------------------ | :---------: | | :----------------------- | :------------------------------------------ |:--------------------------------------------------|:-----------:|
| `command_key` | Key to open command line | Single character | `:` | | `command_key` | Key to open command line | Single character | `:` |
| `initial_screen` | Screen to show after startup | `"library"`, `"search"`, `"queue"`, `"cover"`[^2] | `"library"` | | `initial_screen` | Screen to show after startup | `"library"`, `"search"`, `"queue"`, `"cover"`[^2] | `"library"` |
| `use_nerdfont` | Turn nerdfont glyphs on/off | `true`, `false` | `false` | | `use_nerdfont` | Turn nerdfont glyphs on/off | `true`, `false` | `false` |
@@ -326,6 +326,7 @@ Possible configuration values are:
| `gapless` | Enable gapless playback | `true`, `false` | `true` | | `gapless` | Enable gapless playback | `true`, `false` | `true` |
| `shuffle` | Set default shuffle state | `true`, `false` | `false` | | `shuffle` | Set default shuffle state | `true`, `false` | `false` |
| `repeat` | Set default repeat mode | `off`, `track`, `playlist` | `off` | | `repeat` | Set default repeat mode | `off`, `track`, `playlist` | `off` |
| `playback_state` | Set default playback state | `"Stopped"`, `"Paused"`, `"Playing"`, `"Default"` | `"Paused"` |
| `[theme]` | Custom theme | See [custom theme](#theming) | | | `[theme]` | Custom theme | See [custom theme](#theming) | |
| `[keybindings]` | Custom keybindings | See [custom keybindings](#custom-keybindings) | | | `[keybindings]` | Custom keybindings | See [custom keybindings](#custom-keybindings) | |

View File

@@ -15,6 +15,14 @@ use crate::serialization::{Serializer, CBOR, TOML};
pub const CLIENT_ID: &str = "d420a117a32841c2b3474932e49fb54b"; pub const CLIENT_ID: &str = "d420a117a32841c2b3474932e49fb54b";
pub const CACHE_VERSION: u16 = 1; pub const CACHE_VERSION: u16 = 1;
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq)]
pub enum PlaybackState {
Playing,
Paused,
Stopped,
Default,
}
#[derive(Clone, Serialize, Deserialize, Debug, Default)] #[derive(Clone, Serialize, Deserialize, Debug, Default)]
pub struct ConfigValues { pub struct ConfigValues {
pub command_key: Option<char>, pub command_key: Option<char>,
@@ -37,6 +45,7 @@ pub struct ConfigValues {
pub shuffle: Option<bool>, pub shuffle: Option<bool>,
pub repeat: Option<queue::RepeatSetting>, pub repeat: Option<queue::RepeatSetting>,
pub cover_max_scale: Option<f32>, pub cover_max_scale: Option<f32>,
pub playback_state: Option<PlaybackState>,
} }
#[derive(Serialize, Deserialize, Debug, Default, Clone)] #[derive(Serialize, Deserialize, Debug, Default, Clone)]
@@ -83,17 +92,19 @@ pub struct UserState {
pub queuestate: QueueState, pub queuestate: QueueState,
pub playlist_orders: HashMap<String, SortingOrder>, pub playlist_orders: HashMap<String, SortingOrder>,
pub cache_version: u16, pub cache_version: u16,
pub playback_state: PlaybackState,
} }
impl Default for UserState { impl Default for UserState {
fn default() -> Self { fn default() -> Self {
UserState { UserState {
volume: u16::max_value(), volume: u16::MAX,
shuffle: false, shuffle: false,
repeat: queue::RepeatSetting::None, repeat: queue::RepeatSetting::None,
queuestate: QueueState::default(), queuestate: QueueState::default(),
playlist_orders: HashMap::new(), playlist_orders: HashMap::new(),
cache_version: 0, cache_version: 0,
playback_state: PlaybackState::Default,
} }
} }
} }
@@ -129,6 +140,10 @@ impl Config {
userstate.repeat = repeat; userstate.repeat = repeat;
} }
if let Some(playback_state) = values.playback_state.clone() {
userstate.playback_state = playback_state;
}
Self { Self {
filename: filename.to_string(), filename: filename.to_string(),
values: RwLock::new(values), values: RwLock::new(values),

View File

@@ -9,6 +9,7 @@ use notify_rust::Notification;
use rand::prelude::*; use rand::prelude::*;
use strum_macros::Display; use strum_macros::Display;
use crate::config::PlaybackState;
use crate::model::playable::Playable; use crate::model::playable::Playable;
use crate::spotify::Spotify; use crate::spotify::Spotify;
use crate::{config::Config, spotify::PlayerEvent}; use crate::{config::Config, spotify::PlayerEvent};
@@ -42,21 +43,32 @@ pub struct Queue {
impl Queue { impl Queue {
pub fn new(spotify: Spotify, cfg: Arc<Config>) -> Queue { pub fn new(spotify: Spotify, cfg: Arc<Config>) -> Queue {
let state = cfg.state().queuestate.clone(); let queue_state = cfg.state().queuestate.clone();
let playback_state = cfg.state().playback_state.clone();
let queue = Queue { let queue = Queue {
queue: Arc::new(RwLock::new(state.queue)), queue: Arc::new(RwLock::new(queue_state.queue)),
spotify: spotify.clone(), spotify: spotify.clone(),
current_track: RwLock::new(state.current_track), current_track: RwLock::new(queue_state.current_track),
random_order: RwLock::new(state.random_order), random_order: RwLock::new(queue_state.random_order),
cfg, cfg,
notification_id: Arc::new(AtomicU32::new(0)), notification_id: Arc::new(AtomicU32::new(0)),
}; };
if let Some(playable) = queue.get_current() { if let Some(playable) = queue.get_current() {
spotify.load(&playable, false, state.track_progress.as_millis() as u32); spotify.load(
&playable,
playback_state == PlaybackState::Playing,
queue_state.track_progress.as_millis() as u32,
);
spotify.update_track(); spotify.update_track();
spotify.pause(); match playback_state {
spotify.seek(state.track_progress.as_millis() as u32); PlaybackState::Stopped => {
spotify.stop();
}
PlaybackState::Paused | PlaybackState::Default | _ => {
spotify.pause();
}
}
} }
queue queue