From 9466c5ea102d07949b3a9d4214436f1fbae1c8ce Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Fri, 1 Jun 2018 21:24:28 +0300 Subject: [PATCH] EpisodeWidget: Wire the cancel button. --- hammond-gtk/src/widgets/episode.rs | 57 ++++++++++++++--------- hammond-gtk/src/widgets/episode_states.rs | 28 ----------- 2 files changed, 34 insertions(+), 51 deletions(-) diff --git a/hammond-gtk/src/widgets/episode.rs b/hammond-gtk/src/widgets/episode.rs index c1ba356..18a85dd 100644 --- a/hammond-gtk/src/widgets/episode.rs +++ b/hammond-gtk/src/widgets/episode.rs @@ -284,16 +284,46 @@ impl EpisodeWidget { // Check if the episode is being downloaded let id = episode.rowid(); - let active_dl = || -> Result, Error> { + let active_dl = move || -> Result, Error> { let m = manager::ACTIVE_DOWNLOADS .read() .map_err(|_| format_err!("Failed to get a lock on the mutex."))?; Ok(m.get(&id).cloned()) - }()?; + }; + + if let Some(prog) = active_dl()? { + // FIXME: Add again the callback ugly hack that makes things work somehow + + // Wire the cancel button + widget + .buttons + .cancel + .connect_clicked(clone!(prog, widget, sender => move |_| { + // Cancel the download + if let Ok(mut m) = prog.lock() { + m.cancel(); + } + + // Cancel is not instant so we have to wait a bit + timeout_add(50, clone!(widget, sender => move || { + if let Ok(thing) = active_dl() { + if thing.is_none() { + // Recalculate the widget state + dbqueries::get_episode_widget_from_rowid(id) + .map_err(From::from) + .and_then(|ep| Self::determine_buttons_state(&widget, &ep, &sender)) + .map_err(|err| error!("Error: {}", err)) + .ok(); + + return glib::Continue(false) + } + } + + glib::Continue(true) + })); + })); - if let Some(_dl) = active_dl { - // FIXME: Wire cancel button // FIXME: Wire Total Size label // Change the widget layout/state @@ -393,25 +423,6 @@ fn determine_media_state( }); gtk::timeout_add(250, callback); - lock.cancel_connect_clicked(clone!(prog, media_machine => move |_| { - if let Ok(mut m) = prog.lock() { - m.cancel(); - } - - if let Ok(mut lock) = media_machine.try_borrow_mut() { - if let Ok(episode) = dbqueries::get_episode_widget_from_rowid(id) { - take_mut::take(lock.deref_mut(), |media| { - media.determine_state( - episode.length(), - false, - episode.local_uri().is_some(), - ) - }); - } - } - })); - drop(lock); - // Setup a callback that will update the progress bar. update_progressbar_callback(&prog, &media_machine, id); diff --git a/hammond-gtk/src/widgets/episode_states.rs b/hammond-gtk/src/widgets/episode_states.rs index c3aabb0..dc32f2b 100644 --- a/hammond-gtk/src/widgets/episode_states.rs +++ b/hammond-gtk/src/widgets/episode_states.rs @@ -5,7 +5,6 @@ // Wrap the types into Struct-tuples and imple deref so it won't be possible to pass // the wrong argument to the wrong position. -use glib; use gtk; use gtk::prelude::*; @@ -190,10 +189,6 @@ impl Progress { self.local_size.set_text(local_size); self.bar.set_fraction(fraction); } - - fn cancel_connect_clicked(&self, f: F) -> glib::SignalHandlerId { - self.cancel.connect_clicked(f) - } } #[derive(Debug, Clone)] @@ -354,17 +349,6 @@ impl ButtonsState { (PlayableWithoutSize(m), Some(s)) => Playable(m.into_playable(&s)), } } - - fn cancel_connect_clicked(&self, f: F) -> glib::SignalHandlerId { - use self::ButtonsState::*; - - match *self { - New(ref val) => val.progress.cancel_connect_clicked(f), - NewWithoutSize(ref val) => val.progress.cancel_connect_clicked(f), - Playable(ref val) => val.progress.cancel_connect_clicked(f), - PlayableWithoutSize(ref val) => val.progress.cancel_connect_clicked(f), - } - } } #[derive(Debug, Clone)] @@ -374,18 +358,6 @@ pub enum MediaMachine { } impl MediaMachine { - pub fn cancel_connect_clicked( - &self, - f: F, - ) -> glib::SignalHandlerId { - use self::MediaMachine::*; - - match *self { - Initialized(ref val) => val.cancel_connect_clicked(f), - InProgress(ref val) => val.progress.cancel_connect_clicked(f), - } - } - pub fn determine_state(self, bytes: Option, is_active: bool, is_downloaded: bool) -> Self { use self::ButtonsState::*; use self::MediaMachine::*;