diff --git a/hammond-gtk/src/widgets/episode.rs b/hammond-gtk/src/widgets/episode.rs index 5e8350b..ed81e5c 100644 --- a/hammond-gtk/src/widgets/episode.rs +++ b/hammond-gtk/src/widgets/episode.rs @@ -23,7 +23,7 @@ use std::sync::{Arc, Mutex}; use std::sync::mpsc::Sender; lazy_static! { - static ref SIZE_OPTS: Arc = { + pub static ref SIZE_OPTS: Arc = { // Declare a custom humansize option struct // See: https://docs.rs/humansize/1.0.2/humansize/file_size_opts/struct.FileSizeOpts.html Arc::new(size_opts::FileSizeOpts { @@ -224,11 +224,12 @@ impl EpisodeWidget { fn set_total_size(&self, bytes: Option) -> Result<(), Error> { let size = bytes.ok_or_else(|| format_err!("Size is None."))?; if size == 0 { - bail!("Size is 0."); + bail!("Size is of 0 MB."); } let s = size.file_size(SIZE_OPTS.clone()) .map_err(|err| format_err!("{}", err))?; + self.total_size.set_text(&s); self.total_size.show(); self.separator2.show(); diff --git a/hammond-gtk/src/widgets/episode_states.rs b/hammond-gtk/src/widgets/episode_states.rs index d2ad65b..3a70560 100644 --- a/hammond-gtk/src/widgets/episode_states.rs +++ b/hammond-gtk/src/widgets/episode_states.rs @@ -9,6 +9,8 @@ use chrono; use gtk; use gtk::prelude::*; +use widgets::episode::SIZE_OPTS; + #[derive(Debug, Clone)] pub struct UnInitialized; @@ -246,19 +248,40 @@ pub struct Size { state: S, } -impl Size { +impl Size { fn set_size(self, s: &str) -> Size { self.size.set_text(s); self.separator.show(); - self.into() + Size { + size: self.size, + separator: self.separator, + state: Shown {}, + } } -} -impl Size { - fn set_size(self, s: &str) -> Size { - self.size.set_text(s); + // https://play.rust-lang.org/?gist=1acffaf62743eeb85be1ae6ecf474784&version=stable + // It might be possible to make a generic definition with Specialization. + // https://github.com/rust-lang/rust/issues/31844 + fn into_shown(self) -> Size { + self.size.show(); self.separator.show(); - self.into() + + Size { + size: self.size, + separator: self.separator, + state: Shown {}, + } + } + + fn into_hidden(self) -> Size { + self.size.hide(); + self.separator.hide(); + + Size { + size: self.size, + separator: self.separator, + state: Hidden {}, + } } } @@ -273,67 +296,12 @@ impl Size { state: UnInitialized {}, } } - - fn set_size(self, s: &str) -> Size { - self.size.set_text(s); - self.separator.show(); - self.into() - } } -impl From> for Size { - fn from(f: Size) -> Self { - f.size.hide(); - f.separator.hide(); +// pub trait Playable {} - Size { - size: f.size, - separator: f.separator, - state: f.state.into(), - } - } -} - -impl From> for Size { - fn from(f: Size) -> Self { - f.size.show(); - f.separator.show(); - - Size { - size: f.size, - separator: f.separator, - state: f.state.into(), - } - } -} - -impl From> for Size { - /// This is suposed to be called only from Size::::set_size. - fn from(f: Size) -> Self { - f.size.show(); - f.separator.show(); - - Size { - size: f.size, - separator: f.separator, - state: f.state.into(), - } - } -} - -impl From> for Size { - /// This is suposed to be called only from Size::::set_size. - fn from(f: Size) -> Self { - f.size.hide(); - f.separator.hide(); - - Size { - size: f.size, - separator: f.separator, - state: f.state.into(), - } - } -} +// impl Playable for Download {} +// impl Playable for Play {} #[derive(Debug, Clone)] pub struct Download; @@ -350,6 +318,44 @@ pub struct DownloadPlay { state: S, } +impl DownloadPlay { + // https://play.rust-lang.org/?gist=1acffaf62743eeb85be1ae6ecf474784&version=stable + // It might be possible to make a generic definition with Specialization. + // https://github.com/rust-lang/rust/issues/31844 + fn into_playable(self) -> DownloadPlay { + self.play.show(); + self.download.hide(); + + DownloadPlay { + play: self.play, + download: self.download, + state: Play {}, + } + } + + fn into_fetchable(self) -> DownloadPlay { + self.play.hide(); + self.download.show(); + + DownloadPlay { + play: self.play, + download: self.download, + state: Download {}, + } + } + + fn into_hidden(self) -> DownloadPlay { + self.play.hide(); + self.download.hide(); + + DownloadPlay { + play: self.play, + download: self.download, + state: Hidden {}, + } + } +} + impl DownloadPlay { fn new(play: gtk::Button, download: gtk::Button) -> Self { play.hide(); @@ -363,123 +369,6 @@ impl DownloadPlay { } } -impl From> for DownloadPlay { - fn from(f: DownloadPlay) -> Self { - f.play.hide(); - f.download.show(); - - DownloadPlay { - play: f.play, - download: f.download, - state: Download {}, - } - } -} - -impl From> for DownloadPlay { - fn from(f: DownloadPlay) -> Self { - f.play.show(); - f.download.hide(); - - DownloadPlay { - play: f.play, - download: f.download, - state: Play {}, - } - } -} - -impl From> for DownloadPlay { - fn from(f: DownloadPlay) -> Self { - f.play.hide(); - f.download.hide(); - - DownloadPlay { - play: f.play, - download: f.download, - state: Hidden {}, - } - } -} - -impl From> for DownloadPlay { - fn from(f: DownloadPlay) -> Self { - f.play.hide(); - f.download.hide(); - - DownloadPlay { - play: f.play, - download: f.download, - state: Hidden {}, - } - } -} - -impl From> for DownloadPlay { - fn from(f: DownloadPlay) -> Self { - f.play.hide(); - f.download.show(); - - DownloadPlay { - play: f.play, - download: f.download, - state: Download {}, - } - } -} - -impl From> for DownloadPlay { - fn from(f: DownloadPlay) -> Self { - f.play.show(); - f.download.show(); - - DownloadPlay { - play: f.play, - download: f.download, - state: Play {}, - } - } -} - -impl From> for DownloadPlay { - fn from(f: DownloadPlay) -> Self { - f.play.hide(); - f.download.show(); - - DownloadPlay { - play: f.play, - download: f.download, - state: Download {}, - } - } -} - -impl From> for DownloadPlay { - fn from(f: DownloadPlay) -> Self { - f.play.show(); - f.download.show(); - - DownloadPlay { - play: f.play, - download: f.download, - state: Play {}, - } - } -} - -impl From> for DownloadPlay { - fn from(f: DownloadPlay) -> Self { - f.play.hide(); - f.download.hide(); - - DownloadPlay { - play: f.play, - download: f.download, - state: Hidden {}, - } - } -} - #[derive(Debug, Clone)] pub struct Progress { bar: gtk::ProgressBar, @@ -489,6 +378,38 @@ pub struct Progress { state: S, } +impl Progress { + fn into_shown(self) -> Progress { + self.bar.show(); + self.cancel.show(); + self.local_size.show(); + self.prog_separator.show(); + + Progress { + bar: self.bar, + cancel: self.cancel, + local_size: self.local_size, + prog_separator: self.prog_separator, + state: Shown {}, + } + } + + fn into_hidden(self) -> Progress { + self.bar.hide(); + self.cancel.hide(); + self.local_size.hide(); + self.prog_separator.hide(); + + Progress { + bar: self.bar, + cancel: self.cancel, + local_size: self.local_size, + prog_separator: self.prog_separator, + state: Hidden {}, + } + } +} + impl Progress { fn new( bar: gtk::ProgressBar, @@ -511,74 +432,6 @@ impl Progress { } } -impl From> for Progress { - fn from(f: Progress) -> Self { - f.bar.show(); - f.cancel.show(); - f.local_size.show(); - f.prog_separator.show(); - - Progress { - bar: f.bar, - cancel: f.cancel, - local_size: f.local_size, - prog_separator: f.prog_separator, - state: Shown {}, - } - } -} - -impl From> for Progress { - fn from(f: Progress) -> Self { - f.bar.hide(); - f.cancel.hide(); - f.local_size.hide(); - f.prog_separator.hide(); - - Progress { - bar: f.bar, - cancel: f.cancel, - local_size: f.local_size, - prog_separator: f.prog_separator, - state: Hidden {}, - } - } -} - -impl From> for Progress { - fn from(f: Progress) -> Self { - f.bar.show(); - f.cancel.show(); - f.local_size.show(); - f.prog_separator.show(); - - Progress { - bar: f.bar, - cancel: f.cancel, - local_size: f.local_size, - prog_separator: f.prog_separator, - state: Shown {}, - } - } -} - -impl From> for Progress { - fn from(f: Progress) -> Self { - f.bar.hide(); - f.cancel.hide(); - f.local_size.hide(); - f.prog_separator.hide(); - - Progress { - bar: f.bar, - cancel: f.cancel, - local_size: f.local_size, - prog_separator: f.prog_separator, - state: Hidden {}, - } - } -} - #[derive(Debug, Clone)] pub struct Media { dl: DownloadPlay, @@ -586,83 +439,103 @@ pub struct Media { progress: Progress, } -// From New from InProgress -impl From> for Media { - fn from(f: Media) -> Self { +type New = Media