EpisodeWidget: Move state machine implementations into a separate module.

This commit is contained in:
Jordan Petridis 2018-02-09 09:13:41 +02:00
parent e22a78fac6
commit 23979b8f22
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
3 changed files with 87 additions and 83 deletions

View File

@ -15,6 +15,7 @@ use hammond_data::utils::get_download_folder;
use app::Action;
use manager;
use widgets::episode_states::*;
use std::path::Path;
use std::sync::{Arc, Mutex};
@ -40,89 +41,6 @@ lazy_static! {
static ref NOW: DateTime<Utc> = Utc::now();
}
#[derive(Debug, Clone)]
struct Normal;
#[derive(Debug, Clone)]
struct GreyedOut;
#[derive(Debug, Clone)]
struct Title<S> {
title: gtk::Label,
state: S,
}
impl<S> Title<S> {
fn set_title(&self, s: &str) {
self.title.set_text(s);
}
}
impl Title<Normal> {
fn new(title: gtk::Label) -> Self {
Title {
title,
state: Normal {},
}
}
}
impl From<Title<Normal>> for Title<GreyedOut> {
fn from(machine: Title<Normal>) -> Self {
machine
.title
.get_style_context()
.map(|c| c.add_class("dim-label"));
Title {
title: machine.title,
state: GreyedOut {},
}
}
}
impl From<Title<GreyedOut>> for Title<Normal> {
fn from(machine: Title<GreyedOut>) -> Self {
machine
.title
.get_style_context()
.map(|c| c.remove_class("dim-label"));
Title {
title: machine.title,
state: Normal {},
}
}
}
#[derive(Debug, Clone)]
enum TitleMachine {
Normal(Title<Normal>),
GreyedOut(Title<GreyedOut>),
}
impl TitleMachine {
fn new(label: gtk::Label, is_played: bool) -> Self {
let m = TitleMachine::Normal(Title::<Normal>::new(label));
m.determine_state(is_played)
}
fn determine_state(self, is_played: bool) -> Self {
match (self, is_played) {
(title @ TitleMachine::Normal(_), false) => title,
(title @ TitleMachine::GreyedOut(_), true) => title,
(TitleMachine::Normal(val), true) => TitleMachine::GreyedOut(val.into()),
(TitleMachine::GreyedOut(val), false) => TitleMachine::Normal(val.into()),
}
}
fn set_title(&self, s: &str) {
match *self {
TitleMachine::Normal(ref val) => val.set_title(s),
TitleMachine::GreyedOut(ref val) => val.set_title(s),
}
}
}
#[derive(Debug, Clone)]
pub struct EpisodeWidget {
pub container: gtk::Box,

View File

@ -0,0 +1,85 @@
use gtk;
use gtk::prelude::*;
#[derive(Debug, Clone)]
pub struct Normal;
#[derive(Debug, Clone)]
pub struct GreyedOut;
#[derive(Debug, Clone)]
pub struct Title<S> {
title: gtk::Label,
state: S,
}
impl<S> Title<S> {
fn set_title(&self, s: &str) {
self.title.set_text(s);
}
}
impl Title<Normal> {
fn new(title: gtk::Label) -> Self {
Title {
title,
state: Normal {},
}
}
}
impl From<Title<Normal>> for Title<GreyedOut> {
fn from(machine: Title<Normal>) -> Self {
machine
.title
.get_style_context()
.map(|c| c.add_class("dim-label"));
Title {
title: machine.title,
state: GreyedOut {},
}
}
}
impl From<Title<GreyedOut>> for Title<Normal> {
fn from(machine: Title<GreyedOut>) -> Self {
machine
.title
.get_style_context()
.map(|c| c.remove_class("dim-label"));
Title {
title: machine.title,
state: Normal {},
}
}
}
#[derive(Debug, Clone)]
pub enum TitleMachine {
Normal(Title<Normal>),
GreyedOut(Title<GreyedOut>),
}
impl TitleMachine {
pub fn new(label: gtk::Label, is_played: bool) -> Self {
let m = TitleMachine::Normal(Title::<Normal>::new(label));
m.determine_state(is_played)
}
pub fn determine_state(self, is_played: bool) -> Self {
match (self, is_played) {
(title @ TitleMachine::Normal(_), false) => title,
(title @ TitleMachine::GreyedOut(_), true) => title,
(TitleMachine::Normal(val), true) => TitleMachine::GreyedOut(val.into()),
(TitleMachine::GreyedOut(val), false) => TitleMachine::Normal(val.into()),
}
}
pub fn set_title(&self, s: &str) {
match *self {
TitleMachine::Normal(ref val) => val.set_title(s),
TitleMachine::GreyedOut(ref val) => val.set_title(s),
}
}
}

View File

@ -1,5 +1,6 @@
mod show;
mod episode;
mod episode_states;
pub use self::episode::EpisodeWidget;
pub use self::show::ShowWidget;