diff --git a/src/modules/music/config.rs b/src/modules/music/config.rs new file mode 100644 index 0000000..85d3f2d --- /dev/null +++ b/src/modules/music/config.rs @@ -0,0 +1,136 @@ +use crate::config::CommonConfig; +use dirs::{audio_dir, home_dir}; +use gtk::pango::EllipsizeMode as GtkEllipsizeMode; +use serde::Deserialize; +use std::path::PathBuf; + +#[derive(Debug, Deserialize, Clone)] +pub struct Icons { + /// Icon to display when playing. + #[serde(default = "default_icon_play")] + pub(crate) play: String, + /// Icon to display when paused. + #[serde(default = "default_icon_pause")] + pub(crate) pause: String, + /// Icon to display under volume slider + #[serde(default = "default_icon_volume")] + pub(crate) volume: String, +} + +impl Default for Icons { + fn default() -> Self { + Self { + pause: default_icon_pause(), + play: default_icon_play(), + volume: default_icon_volume(), + } + } +} + +#[derive(Debug, Deserialize, Clone, Copy)] +#[serde(rename_all = "snake_case")] +pub enum PlayerType { + Mpd, + Mpris, +} + +impl Default for PlayerType { + fn default() -> Self { + Self::Mpris + } +} + +#[derive(Debug, Deserialize, Clone, Copy)] +#[serde(rename_all = "snake_case")] +pub enum EllipsizeMode { + Start, + Middle, + End, +} + +impl From for GtkEllipsizeMode { + fn from(value: EllipsizeMode) -> Self { + match value { + EllipsizeMode::Start => Self::Start, + EllipsizeMode::Middle => Self::Middle, + EllipsizeMode::End => Self::End, + } + } +} + +#[derive(Debug, Deserialize, Clone, Copy)] +#[serde(untagged)] +pub enum TruncateMode { + Auto(EllipsizeMode), + MaxLength { + mode: EllipsizeMode, + length: Option, + }, +} + +impl TruncateMode { + pub(crate) const fn mode(&self) -> EllipsizeMode { + match self { + Self::MaxLength { mode, .. } | Self::Auto(mode) => *mode, + } + } + + pub(crate) const fn length(&self) -> Option { + match self { + Self::Auto(_) => None, + Self::MaxLength { length, .. } => *length, + } + } +} + +#[derive(Debug, Deserialize, Clone)] +pub struct MusicModule { + /// Type of player to connect to + #[serde(default)] + pub(crate) player_type: PlayerType, + + /// Format of current song info to display on the bar. + #[serde(default = "default_format")] + pub(crate) format: String, + + /// Player state icons + #[serde(default)] + pub(crate) icons: Icons, + + pub(crate) truncate: Option, + + // -- MPD -- + /// TCP or Unix socket address. + #[serde(default = "default_socket")] + pub(crate) host: String, + /// Path to root of music directory. + #[serde(default = "default_music_dir")] + pub(crate) music_dir: PathBuf, + + #[serde(flatten)] + pub common: Option, +} + +fn default_socket() -> String { + String::from("localhost:6600") +} + +fn default_format() -> String { + String::from("{icon} {title} / {artist}") +} + +fn default_icon_play() -> String { + String::from("") +} + +fn default_icon_pause() -> String { + String::from("") +} + +fn default_icon_volume() -> String { + String::from("墳") +} + +fn default_music_dir() -> PathBuf { + audio_dir().unwrap_or_else(|| home_dir().map(|dir| dir.join("Music")).unwrap_or_default()) +} diff --git a/src/modules/music.rs b/src/modules/music/mod.rs similarity index 82% rename from src/modules/music.rs rename to src/modules/music/mod.rs index 1387a7a..cb443b5 100644 --- a/src/modules/music.rs +++ b/src/modules/music/mod.rs @@ -1,17 +1,15 @@ +mod config; + use crate::clients::music::{self, MusicClient, PlayerState, PlayerUpdate, Status, Track}; -use crate::config::CommonConfig; use crate::modules::{Module, ModuleInfo, ModuleUpdateEvent, ModuleWidget, WidgetContext}; use crate::popup::Popup; use crate::{send_async, try_send}; use color_eyre::Result; -use dirs::{audio_dir, home_dir}; use glib::Continue; use gtk::gdk_pixbuf::Pixbuf; -use gtk::pango::EllipsizeMode as GtkEllipsizeMode; use gtk::prelude::*; use gtk::{Button, Image, Label, Orientation, Scale}; use regex::Regex; -use serde::Deserialize; use std::path::PathBuf; use std::sync::Arc; use std::time::Duration; @@ -19,6 +17,9 @@ use tokio::spawn; use tokio::sync::mpsc::{Receiver, Sender}; use tracing::error; +pub use self::config::MusicModule; +use self::config::{Icons, PlayerType}; + #[derive(Debug)] pub enum PlayerCommand { Previous, @@ -28,138 +29,6 @@ pub enum PlayerCommand { Volume(u8), } -#[derive(Debug, Deserialize, Clone)] -pub struct Icons { - /// Icon to display when playing. - #[serde(default = "default_icon_play")] - play: String, - /// Icon to display when paused. - #[serde(default = "default_icon_pause")] - pause: String, - /// Icon to display under volume slider - #[serde(default = "default_icon_volume")] - volume: String, -} - -impl Default for Icons { - fn default() -> Self { - Self { - pause: default_icon_pause(), - play: default_icon_play(), - volume: default_icon_volume(), - } - } -} - -#[derive(Debug, Deserialize, Clone, Copy)] -#[serde(rename_all = "snake_case")] -pub enum PlayerType { - Mpd, - Mpris, -} - -impl Default for PlayerType { - fn default() -> Self { - Self::Mpris - } -} - -#[derive(Debug, Deserialize, Clone, Copy)] -#[serde(rename_all = "snake_case")] -pub enum EllipsizeMode { - Start, - Middle, - End, -} - -impl From for GtkEllipsizeMode { - fn from(value: EllipsizeMode) -> Self { - match value { - EllipsizeMode::Start => Self::Start, - EllipsizeMode::Middle => Self::Middle, - EllipsizeMode::End => Self::End, - } - } -} - -#[derive(Debug, Deserialize, Clone, Copy)] -#[serde(untagged)] -enum TruncateMode { - Auto(EllipsizeMode), - MaxLength { - mode: EllipsizeMode, - length: Option, - }, -} - -impl TruncateMode { - fn mode(&self) -> EllipsizeMode { - match self { - TruncateMode::Auto(mode) => *mode, - TruncateMode::MaxLength { mode, .. } => *mode, - } - } - - fn length(&self) -> Option { - match self { - TruncateMode::Auto(_) => None, - TruncateMode::MaxLength { length, .. } => *length, - } - } -} - -#[derive(Debug, Deserialize, Clone)] -pub struct MusicModule { - /// Type of player to connect to - #[serde(default)] - player_type: PlayerType, - - /// Format of current song info to display on the bar. - #[serde(default = "default_format")] - format: String, - - /// Player state icons - #[serde(default)] - icons: Icons, - - truncate: Option, - - // -- MPD -- - /// TCP or Unix socket address. - #[serde(default = "default_socket")] - host: String, - /// Path to root of music directory. - #[serde(default = "default_music_dir")] - music_dir: PathBuf, - - #[serde(flatten)] - pub common: Option, -} - -fn default_socket() -> String { - String::from("localhost:6600") -} - -fn default_format() -> String { - String::from("{icon} {title} / {artist}") -} - -fn default_icon_play() -> String { - String::from("") -} - -fn default_icon_pause() -> String { - String::from("") -} - -fn default_icon_volume() -> String { - String::from("墳") -} - -fn default_music_dir() -> PathBuf { - audio_dir().unwrap_or_else(|| home_dir().map(|dir| dir.join("Music")).unwrap_or_default()) -} - /// Formats a duration given in seconds /// in hh:mm format fn format_time(duration: Duration) -> String {