Skip to content

Commit

Permalink
fix(music): remote mpris album art not showing
Browse files Browse the repository at this point in the history
Fixes #55.
  • Loading branch information
JakeStanger committed Jan 30, 2023
1 parent 15f0857 commit 5772711
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async_once = "0.2.6"
indexmap = "1.9.1"
futures-util = "0.3.21"
chrono = "0.4.19"
reqwest = {version = "0.11.14", features = ["default"] }
reqwest = {version = "0.11.14" }
regex = { version = "1.6.0", default-features = false, features = ["std"] }
stray = { version = "0.1.2" }
dirs = "4.0.0"
Expand Down
2 changes: 1 addition & 1 deletion src/clients/music/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct Track {
pub disc: Option<u64>,
pub genre: Option<String>,
pub track: Option<u64>,
pub cover_path: Option<PathBuf>,
pub cover_path: Option<String>,
}

#[derive(Clone, Debug)]
Expand Down
18 changes: 11 additions & 7 deletions src/clients/music/mpd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,16 @@ impl MpdClient {
fn convert_song(song: &Song, music_dir: &Path) -> Track {
let (track, disc) = song.number();

let cover_path = music_dir.join(
song.file_path()
.parent()
.expect("Song path should not be root")
.join("cover.jpg"),
);
let cover_path = music_dir
.join(
song.file_path()
.parent()
.expect("Song path should not be root")
.join("cover.jpg"),
)
.into_os_string()
.into_string()
.ok();

Track {
title: song.title().map(std::string::ToString::to_string),
Expand All @@ -136,7 +140,7 @@ impl MpdClient {
genre: try_get_first_tag(song, &Tag::Genre).map(std::string::ToString::to_string),
disc: Some(disc),
track: Some(track),
cover_path: Some(cover_path),
cover_path,
}
}
}
Expand Down
6 changes: 1 addition & 5 deletions src/clients/music/mpris.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use color_eyre::Result;
use lazy_static::lazy_static;
use mpris::{DBusError, Event, Metadata, PlaybackStatus, Player, PlayerFinder};
use std::collections::HashSet;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use std::thread::sleep;
use std::time::Duration;
Expand Down Expand Up @@ -260,10 +259,7 @@ impl From<Metadata> for Track {
.and_then(mpris::MetadataValue::as_str_array)
.and_then(|arr| arr.first().map(|val| (*val).to_string())),
track: value.track_number().map(|track| track as u64),
cover_path: value
.art_url()
.map(|path| path.replace("file://", ""))
.map(PathBuf::from),
cover_path: value.art_url().map(|s| s.to_string()),
}
}
}
Expand Down
24 changes: 14 additions & 10 deletions src/modules/music/mod.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
mod config;

use std::path::PathBuf;
use crate::clients::music::{self, MusicClient, PlayerState, PlayerUpdate, Status, Track};
use crate::image::ImageProvider;
use crate::modules::{Module, ModuleInfo, ModuleUpdateEvent, ModuleWidget, WidgetContext};
use crate::popup::Popup;
use crate::{send_async, try_send};
use color_eyre::Result;
use glib::Continue;
use gtk::gdk_pixbuf::Pixbuf;
use gtk::prelude::*;
use gtk::{Button, Image, Label, Orientation, Scale};
use gtk::{Button, IconTheme, Label, Orientation, Scale};
use regex::Regex;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;
use tokio::spawn;
Expand Down Expand Up @@ -213,13 +213,15 @@ impl Module<Button> for MusicModule {
tx: Sender<Self::ReceiveMessage>,
rx: glib::Receiver<Self::SendMessage>,
) -> Option<gtk::Box> {
let icon_theme = IconTheme::new();

let container = gtk::Box::builder()
.orientation(Orientation::Horizontal)
.spacing(10)
.name("popup-music")
.build();

let album_image = Image::builder()
let album_image = gtk::Image::builder()
.width_request(128)
.height_request(128)
.name("album-art")
Expand Down Expand Up @@ -304,16 +306,18 @@ impl Module<Button> for MusicModule {
let new_cover = update.song.cover_path;
if prev_cover != new_cover {
prev_cover = new_cover.clone();
match new_cover.map(|cover_path| {
Pixbuf::from_file_at_scale(cover_path, 128, 128, true)
}) {
Some(Ok(pixbuf)) => album_image.set_from_pixbuf(Some(&pixbuf)),
let res = match new_cover.map(|cover_path| ImageProvider::parse(cover_path, &icon_theme, 128))
{
Some(Ok(image)) => image.load_into_image(album_image.clone()),
Some(Err(err)) => {
error!("{:?}", err);
album_image.set_from_pixbuf(None);
Err(err)
}
None => album_image.set_from_pixbuf(None),
None => Ok(album_image.set_from_pixbuf(None)),
};
if let Err(err) = res {
error!("{err:?}");
}
}

title_label
Expand Down

0 comments on commit 5772711

Please sign in to comment.