1
0
Fork 0
mirror of https://github.com/Zedfrigg/ironbar.git synced 2025-08-20 08:11:03 +02:00

refactor: major client code changes

This does away with `lazy_static` singletons for all the clients, instead putting them all inside a `Clients` struct on the `Ironbar` struct.

Client code has been refactored in places to accommodate this, and module code has been updated to get the clients the new way.

The Wayland client has been re-written from the ground up to remove a lot of the needless complications, provide a nicer interface and reduce some duplicate data.

The MPD music client has been overhauled to use the `mpd_utils` crate, which simplifies the code within Ironbar and should offer more robustness and better recovery when connection is lost to the server.

The launcher module in particular has been affected by the refactor.
This commit is contained in:
Jake Stanger 2024-01-07 23:50:10 +00:00
parent 57b57ed002
commit c702f6fffa
No known key found for this signature in database
GPG key ID: C51FC8F9CB0BEA61
33 changed files with 1081 additions and 1063 deletions

View file

@ -1,4 +1,5 @@
use color_eyre::Result;
use std::fmt::Debug;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;
@ -19,8 +20,6 @@ pub enum PlayerUpdate {
/// Triggered at regular intervals while a track is playing.
/// Used to keep track of the progress through the current track.
ProgressTick(ProgressTick),
/// Triggered when the client disconnects from the player.
Disconnect,
}
#[derive(Clone, Debug)]
@ -56,7 +55,7 @@ pub struct ProgressTick {
pub elapsed: Option<Duration>,
}
pub trait MusicClient {
pub trait MusicClient: Debug + Send + Sync {
fn play(&self) -> Result<()>;
fn pause(&self) -> Result<()>;
fn next(&self) -> Result<()>;
@ -68,18 +67,15 @@ pub trait MusicClient {
fn subscribe_change(&self) -> broadcast::Receiver<PlayerUpdate>;
}
pub enum ClientType<'a> {
Mpd { host: &'a str, music_dir: PathBuf },
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub enum ClientType {
Mpd { host: String, music_dir: PathBuf },
Mpris,
}
pub async fn get_client(client_type: ClientType<'_>) -> Box<Arc<dyn MusicClient>> {
pub fn create_client(client_type: ClientType) -> Arc<dyn MusicClient> {
match client_type {
ClientType::Mpd { host, music_dir } => Box::new(
mpd::get_client(host, music_dir)
.await
.expect("Failed to connect to MPD client"),
),
ClientType::Mpris => Box::new(mpris::get_client()),
ClientType::Mpd { host, music_dir } => Arc::new(mpd::Client::new(host, music_dir)),
ClientType::Mpris => Arc::new(mpris::Client::new()),
}
}