From 6036562af2eedefc790266eb987342647dfd4042 Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Tue, 17 Jul 2018 16:08:20 +0300 Subject: [PATCH] HomeEpisode: Do not initialize the Image until it needs to be drawn. --- hammond-gtk/src/widgets/home_view.rs | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/hammond-gtk/src/widgets/home_view.rs b/hammond-gtk/src/widgets/home_view.rs index fe892ed..a9bd175 100644 --- a/hammond-gtk/src/widgets/home_view.rs +++ b/hammond-gtk/src/widgets/home_view.rs @@ -13,6 +13,7 @@ use app::Action; use utils::{self, lazy_load_full}; use widgets::EpisodeWidget; +use std::cell::Cell; use std::rc::Rc; use std::sync::Mutex; @@ -216,14 +217,27 @@ impl HomeEpisode { } fn init(&self, show_id: i32) { - self.set_cover(show_id) - .map_err(|err| error!("Failed to set a cover: {}", err)) - .ok(); - + self.set_cover(show_id); self.container.pack_start(&self.episode, true, true, 6); } - fn set_cover(&self, show_id: i32) -> Result<(), Error> { - utils::set_image_from_path(&self.image, show_id, 64) + fn set_cover(&self, show_id: i32) { + // The closure above is a regular `Fn` closure. + // which means we can't mutate stuff inside it easily, + // so Cell is used. + // + // `Option` along with the `.take()` method ensure + // that the function will only be run once, during the first execution. + let show_id = Cell::new(Some(show_id)); + + self.image.connect_draw(move |image, _| { + show_id.take().map(|id| { + utils::set_image_from_path(image, id, 64) + .map_err(|err| error!("Failed to set a cover: {}", err)) + .ok(); + }); + + gtk::Inhibit(false) + }); } }