mirror of
https://github.com/Zedfrigg/ironbar.git
synced 2025-07-04 04:01: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:
parent
57b57ed002
commit
c702f6fffa
33 changed files with 1081 additions and 1063 deletions
|
@ -1,3 +1,4 @@
|
|||
use std::cell::RefMut;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::Arc;
|
||||
|
@ -14,6 +15,7 @@ use tracing::error;
|
|||
use crate::clients::music::{
|
||||
self, MusicClient, PlayerState, PlayerUpdate, ProgressTick, Status, Track,
|
||||
};
|
||||
use crate::clients::Clients;
|
||||
use crate::gtk_helpers::IronbarGtkExt;
|
||||
use crate::image::{new_icon_button, new_icon_label, ImageProvider};
|
||||
use crate::modules::PopupButton;
|
||||
|
@ -67,16 +69,18 @@ pub struct SongUpdate {
|
|||
display_string: String,
|
||||
}
|
||||
|
||||
async fn get_client(
|
||||
fn get_client(
|
||||
mut clients: RefMut<'_, Clients>,
|
||||
player_type: PlayerType,
|
||||
host: &str,
|
||||
host: String,
|
||||
music_dir: PathBuf,
|
||||
) -> Box<Arc<dyn MusicClient>> {
|
||||
match player_type {
|
||||
PlayerType::Mpd => music::get_client(music::ClientType::Mpd { host, music_dir }),
|
||||
PlayerType::Mpris => music::get_client(music::ClientType::Mpris {}),
|
||||
}
|
||||
.await
|
||||
) -> Arc<dyn MusicClient> {
|
||||
let client_type = match player_type {
|
||||
PlayerType::Mpd => music::ClientType::Mpd { host, music_dir },
|
||||
PlayerType::Mpris => music::ClientType::Mpris,
|
||||
};
|
||||
|
||||
clients.music(client_type)
|
||||
}
|
||||
|
||||
impl Module<Button> for MusicModule {
|
||||
|
@ -98,19 +102,21 @@ impl Module<Button> for MusicModule {
|
|||
let re = Regex::new(r"\{([\w-]+)}")?;
|
||||
let tokens = get_tokens(&re, self.format.as_str());
|
||||
|
||||
let client = get_client(
|
||||
context.ironbar.clients.borrow_mut(),
|
||||
self.player_type,
|
||||
self.host.clone(),
|
||||
self.music_dir.clone(),
|
||||
);
|
||||
|
||||
// receive player updates
|
||||
{
|
||||
let player_type = self.player_type;
|
||||
let host = self.host.clone();
|
||||
let music_dir = self.music_dir.clone();
|
||||
let tx = context.tx.clone();
|
||||
let client = client.clone();
|
||||
|
||||
spawn(async move {
|
||||
loop {
|
||||
let mut rx = {
|
||||
let client = get_client(player_type, &host, music_dir.clone()).await;
|
||||
client.subscribe_change()
|
||||
};
|
||||
let mut rx = client.subscribe_change();
|
||||
|
||||
while let Ok(update) = rx.recv().await {
|
||||
match update {
|
||||
|
@ -143,7 +149,6 @@ impl Module<Button> for MusicModule {
|
|||
progress_tick
|
||||
))
|
||||
),
|
||||
PlayerUpdate::Disconnect => break,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -152,13 +157,8 @@ impl Module<Button> for MusicModule {
|
|||
|
||||
// listen to ui events
|
||||
{
|
||||
let player_type = self.player_type;
|
||||
let host = self.host.clone();
|
||||
let music_dir = self.music_dir.clone();
|
||||
|
||||
spawn(async move {
|
||||
while let Some(event) = rx.recv().await {
|
||||
let client = get_client(player_type, &host, music_dir.clone()).await;
|
||||
let res = match event {
|
||||
PlayerCommand::Previous => client.prev(),
|
||||
PlayerCommand::Play => client.play(),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue