Skip to content

Commit f8af48f

Browse files
authored
Make it possible to enable streaming only in daemon mode (#242)
Make `enable_streaming` an enum with a `DaemonOnly` option. To keep backward compatibility, `true` and `false` are also accepted. Fixes #238.
1 parent 3457a0b commit f8af48f

File tree

4 files changed

+45
-6
lines changed

4 files changed

+45
-6
lines changed

docs/config.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ All configuration files should be placed inside the application's configuration
3636
| `page_size_in_rows` | a page's size expressed as a number of rows (for page-navigation commands) | `20` |
3737
| `track_table_item_max_len` | the maximum length of a column in a track table | `32` |
3838
| `enable_media_control` | enable application media control support (`media-control` feature only) | `true` (Linux), `false` (Windows and MacOS) |
39-
| `enable_streaming` | create a device for streaming (streaming feature only) | `true` |
39+
| `enable_streaming` | create a device for streaming (streaming feature only) | `Always` |
4040
| `enable_cover_image_cache` | store album's cover images in the cache folder | `true` |
4141
| `default_device` | the default device to connect to on startup if no playing device found | `spotify-player` |
4242
| `play_icon` | the icon to indicate playing state of a Spotify item | `` |
@@ -72,8 +72,9 @@ All configuration files should be placed inside the application's configuration
7272

7373
- An example of event that triggers a playback update is the one happening when the current track ends.
7474
- `copy_command` is represented by a struct with two fields `command` and `args`. For example, `copy_command = { command = "xclip", args = ["-sel", "c"] }`. The copy command should read input from **standard input**.
75+
- `enable_streaming` can be either `Always`, `Never` or `DaemonOnly`. For backwards compatibility, `true` and `false` are still accepted as aliases for `Always` and `Never`.
7576
- `playback_window_position` can only be either `Top` or `Bottom`.
76-
- `border_type` can be either `Hidden`, `Plain`, `Rounded`, `Double`, `Thick`.
77+
- `border_type` can be either `Hidden`, `Plain`, `Rounded`, `Double` or `Thick`.
7778
- `progress_bar_type` can be either `Rectangle` or `Line`.
7879

7980
#### Media control

examples/app.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ cover_image_refresh_duration_in_ms = 2000
1414
page_size_in_rows = 20
1515
track_table_item_max_len = 32
1616
enable_media_control = false
17-
enable_streaming = true
17+
enable_streaming = "Always"
1818
enable_cover_image_cache = true
1919
default_device = "spotify-player"
2020
play_icon = ""

spotify_player/src/config/mod.rs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub struct AppConfig {
7171
pub enable_media_control: bool,
7272

7373
#[cfg(feature = "streaming")]
74-
pub enable_streaming: bool,
74+
pub enable_streaming: StreamingType,
7575

7676
pub enable_cover_image_cache: bool,
7777

@@ -127,6 +127,42 @@ pub struct NotifyFormat {
127127
pub body: String,
128128
}
129129

130+
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
131+
#[serde(from = "StreamingTypeOrBool")]
132+
pub enum StreamingType {
133+
Always,
134+
DaemonOnly,
135+
Never,
136+
}
137+
config_parser_impl!(StreamingType);
138+
139+
// For backward compatibility, to accept booleans for enable_streaming
140+
#[derive(Deserialize)]
141+
enum RawStreamingType {
142+
Always,
143+
DaemonOnly,
144+
Never,
145+
}
146+
147+
#[derive(Deserialize)]
148+
#[serde(untagged)]
149+
enum StreamingTypeOrBool {
150+
Bool(bool),
151+
Type(RawStreamingType),
152+
}
153+
154+
impl From<StreamingTypeOrBool> for StreamingType {
155+
fn from(v: StreamingTypeOrBool) -> Self {
156+
match v {
157+
StreamingTypeOrBool::Bool(true) => StreamingType::Always,
158+
StreamingTypeOrBool::Bool(false) => StreamingType::Never,
159+
StreamingTypeOrBool::Type(RawStreamingType::Always) => StreamingType::Always,
160+
StreamingTypeOrBool::Type(RawStreamingType::DaemonOnly) => StreamingType::DaemonOnly,
161+
StreamingTypeOrBool::Type(RawStreamingType::Never) => StreamingType::Never,
162+
}
163+
}
164+
}
165+
130166
impl Default for AppConfig {
131167
fn default() -> Self {
132168
Self {
@@ -203,7 +239,7 @@ impl Default for AppConfig {
203239
enable_media_control: true,
204240

205241
#[cfg(feature = "streaming")]
206-
enable_streaming: true,
242+
enable_streaming: StreamingType::Always,
207243

208244
enable_cover_image_cache: true,
209245

spotify_player/src/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ async fn init_spotify(
7878
) -> Result<()> {
7979
// if `streaming` feature is enabled, create a new streaming connection
8080
#[cfg(feature = "streaming")]
81-
if state.app_config.enable_streaming {
81+
if state.app_config.enable_streaming == config::StreamingType::Always
82+
|| (state.app_config.enable_streaming == config::StreamingType::DaemonOnly && is_daemon)
83+
{
8284
client.new_streaming_connection(state).await;
8385
}
8486

0 commit comments

Comments
 (0)