diff --git a/hammond-gtk/src/utils.rs b/hammond-gtk/src/utils.rs index 65a24d3..5c332d9 100644 --- a/hammond-gtk/src/utils.rs +++ b/hammond-gtk/src/utils.rs @@ -2,6 +2,7 @@ use gdk_pixbuf::Pixbuf; use gio::{Settings, SettingsExt}; +use glib; use gtk; use gtk::prelude::*; @@ -21,7 +22,7 @@ use hammond_downloader::downloader; use std::collections::{HashMap, HashSet}; use std::sync::{Mutex, RwLock}; use std::sync::Arc; -use std::sync::mpsc::Sender; +use std::sync::mpsc::*; use std::thread; use app::Action; @@ -142,7 +143,7 @@ lazy_static! { // TODO: maybe use something that would just scale to requested size? pub fn set_image_from_path( image: >k::Image, - pd: &PodcastCoverQuery, + pd: Arc, size: u32, ) -> Result<(), Error> { { @@ -158,13 +159,30 @@ pub fn set_image_from_path( } } - let img_path = downloader::cache_image(pd)?; - let px = Pixbuf::new_from_file_at_scale(&img_path, size as i32, size as i32, true)?; - let mut hashmap = CACHED_PIXBUFS - .write() - .map_err(|_| format_err!("Failed to lock pixbuf mutex."))?; - hashmap.insert((pd.id(), size), Mutex::new(SendCell::new(px.clone()))); - image.set_from_pixbuf(&px); + let (sender, receiver) = channel(); + let pd_ = pd.clone(); + thread::spawn(move || { + sender.send(downloader::cache_image(&pd_)).unwrap(); + }); + + let image = image.clone(); + gtk::timeout_add(200, move || { + if let Ok(path) = receiver.try_recv() { + if let Ok(path) = path { + if let Ok(px) = + Pixbuf::new_from_file_at_scale(&path, size as i32, size as i32, true) + { + if let Ok(mut hashmap) = CACHED_PIXBUFS.write() { + hashmap.insert((pd.id(), size), Mutex::new(SendCell::new(px.clone()))); + image.set_from_pixbuf(&px); + } + } + } + glib::Continue(false) + } else { + glib::Continue(true) + } + }); Ok(()) } diff --git a/hammond-gtk/src/views/episodes.rs b/hammond-gtk/src/views/episodes.rs index 5e3c7a0..e8f51c5 100644 --- a/hammond-gtk/src/views/episodes.rs +++ b/hammond-gtk/src/views/episodes.rs @@ -10,6 +10,7 @@ use app::Action; use utils::{get_ignored_shows, set_image_from_path}; use widgets::EpisodeWidget; +use std::sync::Arc; use std::sync::mpsc::Sender; #[derive(Debug, Clone)] @@ -227,7 +228,7 @@ impl EpisodesViewWidget { } fn set_cover(&self, podcast_id: i32) -> Result<(), Error> { - let pd = dbqueries::get_podcast_cover_from_id(podcast_id)?; - set_image_from_path(&self.image, &pd, 64) + let pd = Arc::new(dbqueries::get_podcast_cover_from_id(podcast_id)?); + set_image_from_path(&self.image, pd, 64) } } diff --git a/hammond-gtk/src/views/shows.rs b/hammond-gtk/src/views/shows.rs index 1abf4c8..67ba529 100644 --- a/hammond-gtk/src/views/shows.rs +++ b/hammond-gtk/src/views/shows.rs @@ -2,7 +2,7 @@ use failure::Error; use gtk; use gtk::prelude::*; -use hammond_data::Podcast; +use hammond_data::{Podcast, PodcastCoverQuery}; use hammond_data::dbqueries; use app::Action; @@ -57,7 +57,7 @@ impl ShowsPopulated { let ignore = get_ignored_shows()?; let podcasts = dbqueries::get_podcasts_filter(&ignore)?; - podcasts.into_iter().map(Arc::new).for_each(|parent| { + podcasts.into_iter().for_each(|parent| { let flowbox_child = ShowsChild::new(parent); self.flowbox.add(&flowbox_child.child); }); @@ -116,23 +116,23 @@ impl Default for ShowsChild { } impl ShowsChild { - pub fn new(pd: Arc) -> ShowsChild { + pub fn new(pd: Podcast) -> ShowsChild { let child = ShowsChild::default(); child.init(pd); child } - fn init(&self, pd: Arc) { + fn init(&self, pd: Podcast) { self.container.set_tooltip_text(pd.title()); + WidgetExt::set_name(&self.child, &pd.id().to_string()); - if let Err(err) = self.set_cover(pd.clone()) { + let pd = Arc::new(pd.into()); + if let Err(err) = self.set_cover(pd) { error!("Failed to set a cover: {}", err) } - - WidgetExt::set_name(&self.child, &pd.id().to_string()); } - fn set_cover(&self, pd: Arc) -> Result<(), Error> { - set_image_from_path(&self.cover, &pd.clone().into(), 256) + fn set_cover(&self, pd: Arc) -> Result<(), Error> { + set_image_from_path(&self.cover, pd, 256) } } diff --git a/hammond-gtk/src/widgets/show.rs b/hammond-gtk/src/widgets/show.rs index c6b458f..ca5ca04 100644 --- a/hammond-gtk/src/widgets/show.rs +++ b/hammond-gtk/src/widgets/show.rs @@ -113,7 +113,7 @@ impl ShowWidget { /// Set the show cover. fn set_cover(&self, pd: Arc) -> Result<(), Error> { - set_image_from_path(&self.cover, &pd.into(), 128) + set_image_from_path(&self.cover, Arc::new(pd.into()), 128) } /// Set the descripton text.