From 46bd23cf66d504503d416f3d091ad68d1bbf72bc Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Sat, 10 Feb 2018 08:00:12 +0200 Subject: [PATCH] EpisodeWidget: Add a StateMachine that manages Play and Download Buttons. --- hammond-gtk/src/widgets/episode_states.rs | 162 +++++++++++++++++++++- 1 file changed, 156 insertions(+), 6 deletions(-) diff --git a/hammond-gtk/src/widgets/episode_states.rs b/hammond-gtk/src/widgets/episode_states.rs index 7f470f7..070b45c 100644 --- a/hammond-gtk/src/widgets/episode_states.rs +++ b/hammond-gtk/src/widgets/episode_states.rs @@ -1,7 +1,19 @@ +// TODO: Things that should be done. +// +// * Wherever there's a function that take 2 or more arguments of the same type, +// eg: fn new(total_size: gtk::Label, local_size: gtk::Label ..) +// 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 chrono; use gtk; use gtk::prelude::*; +#[derive(Debug, Clone)] +pub struct Shown; +#[derive(Debug, Clone)] +pub struct Hidden; + #[derive(Debug, Clone)] pub struct Normal; #[derive(Debug, Clone)] @@ -87,12 +99,6 @@ impl TitleMachine { } } } - -#[derive(Debug, Clone)] -pub struct Shown; -#[derive(Debug, Clone)] -pub struct Hidden; - #[derive(Debug, Clone)] pub struct Duration { // TODO: make duration and separator diff types @@ -296,6 +302,7 @@ impl From> for Size { } } +#[derive(Debug, Clone)] pub enum SizeMachine { LocalShown(Size), TotallShown(Size), @@ -322,3 +329,146 @@ impl SizeMachine { unimplemented!() } } +#[derive(Debug, Clone)] +pub struct Download; + +#[derive(Debug, Clone)] +pub struct Play; + +#[derive(Debug, Clone)] +// FIXME: Needs better name. +// Should each button also has it's own type and machine? +pub struct DownloadPlay { + play: gtk::Button, + download: gtk::Button, + state: S, +} + +impl DownloadPlay { + fn new(play: gtk::Button, download: gtk::Button) -> Self { + play.hide(); + download.show(); + + DownloadPlay { + play, + download, + state: Download {}, + } + } +} + +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 {}, + } + } +} + +pub enum DownloadPlayMachine { + Play(DownloadPlay), + Download(DownloadPlay), + Hidden(DownloadPlay), +} + +impl DownloadPlayMachine { + pub fn new(play: gtk::Button, download: gtk::Button) -> Self { + DownloadPlayMachine::Download(DownloadPlay::::new(play, download)) + } + + pub fn determine_state(self, downloaded: bool, should_hide: bool) -> Self { + match (self, downloaded, should_hide) { + (DownloadPlayMachine::Play(val), true, false) => DownloadPlayMachine::Play(val.into()), + (DownloadPlayMachine::Play(val), false, false) => { + DownloadPlayMachine::Download(val.into()) + } + (DownloadPlayMachine::Download(val), true, false) => { + DownloadPlayMachine::Play(val.into()) + } + (DownloadPlayMachine::Download(val), false, false) => { + DownloadPlayMachine::Download(val.into()) + } + (DownloadPlayMachine::Hidden(val), true, false) => { + DownloadPlayMachine::Play(val.into()) + } + (DownloadPlayMachine::Hidden(val), false, false) => { + DownloadPlayMachine::Download(val.into()) + } + (DownloadPlayMachine::Play(val), _, true) => DownloadPlayMachine::Hidden(val.into()), + (DownloadPlayMachine::Download(val), _, true) => { + DownloadPlayMachine::Hidden(val.into()) + } + (DownloadPlayMachine::Hidden(val), _, true) => DownloadPlayMachine::Hidden(val.into()), + } + } +}