From fa33138d66d81a1028b3f6ab7f137d24410baa5f Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Mon, 11 Dec 2017 11:53:29 +0200 Subject: [PATCH] Start switching to the new stack architecture. --- hammond-gtk/src/content.rs | 184 ++++++++++++----------------- hammond-gtk/src/main.rs | 3 +- hammond-gtk/src/views/podcasts.rs | 35 +++--- hammond-gtk/src/widgets/podcast.rs | 1 - 4 files changed, 97 insertions(+), 126 deletions(-) diff --git a/hammond-gtk/src/content.rs b/hammond-gtk/src/content.rs index 64063cc..c75d4a1 100644 --- a/hammond-gtk/src/content.rs +++ b/hammond-gtk/src/content.rs @@ -8,78 +8,60 @@ use widgets::podcast::PodcastWidget; use views::podcasts::PopulatedView; use views::empty::EmptyView; -#[derive(Debug)] pub struct Content { pub stack: gtk::Stack, - pub widget: PodcastWidget, - pub podcasts: PopulatedView, - pub empty: EmptyView, + shows: ShowStateWrapper, + episodes: EpisodeStateWrapper, } impl Content { pub fn new() -> Content { let stack = gtk::Stack::new(); + let shows = ShowStateWrapper::new(); + let episodes = EpisodeStateWrapper::new(); - let widget = PodcastWidget::new(); - let podcasts = PopulatedView::new(); - let empty = EmptyView::new(); + let shows_stack = shows.get_stack(); + let ep_stack = episodes.get_stack(); - stack.add_titled(&widget.container, "widget", "Episodes"); - stack.add_titled(&podcasts.container, "podcasts", "Shows"); - stack.add_named(&empty.container, "empty"); + stack.add_titled(&ep_stack, "episodes", "Episodes"); + stack.add_titled(&shows_stack, "shows", "Shows"); Content { stack, - widget, - empty, - podcasts, + shows, + episodes, } } - pub fn new_initialized() -> Content { - let ct = Content::new(); - ct.init(); - ct - } + // pub fn new_initialized() -> Content { + // let ct = Content::new(); + // ct.init(); + // ct + // } - pub fn init(&self) { - self.podcasts.init(&self.stack); - if self.podcasts.flowbox.get_children().is_empty() { - self.stack.set_visible_child_name("empty"); - return; - } + // pub fn init(&self) { + // self.podcasts.init(); + // if self.podcasts.flowbox.get_children().is_empty() { + // self.stack.set_visible_child_name("empty"); + // return; + // } - self.stack.set_visible_child_name("podcasts"); - } + // self.stack.set_visible_child_name("podcasts"); + // } - fn replace_widget(&mut self, pdw: PodcastWidget) { - let vis = self.stack.get_visible_child_name().unwrap(); - let old = self.stack.get_child_by_name("widget").unwrap(); - self.stack.remove(&old); + // fn replace_widget(&mut self, pdw: PodcastWidget) { + // let vis = self.stack.get_visible_child_name().unwrap(); + // let old = self.stack.get_child_by_name("widget").unwrap(); + // self.stack.remove(&old); - self.widget = pdw; - self.stack - .add_titled(&self.widget.container, "widget", "Episodes"); - self.stack.set_visible_child_name(&vis); - old.destroy(); - } - - fn replace_podcasts(&mut self, pop: PopulatedView) { - let vis = self.stack.get_visible_child_name().unwrap(); - let old = self.stack.get_child_by_name("podcasts").unwrap(); - self.stack.remove(&old); - - self.podcasts = pop; - self.stack - .add_titled(&self.podcasts.container, "podcasts", "Shows"); - self.stack.set_visible_child_name(&vis); - old.destroy(); - } + // self.widget = pdw; + // self.stack + // .add_titled(&self.widget.container, "widget", "Episodes"); + // self.stack.set_visible_child_name(&vis); + // old.destroy(); + // } } -#[derive(Debug)] -// Experiementing with Wrapping gtk::Stack into a State machine. -// Gonna revist it when TryInto trais is stabilized. pub struct ContentState { content: Content, state: S, @@ -89,8 +71,6 @@ pub trait UpdateView { fn update(&mut self); } -pub struct Empty; - #[derive(Debug)] pub struct PodcastsView {} @@ -104,34 +84,7 @@ impl UpdateView for ContentState { let pd = dbqueries::get_podcast_from_id(id.parse::().unwrap()).unwrap(); let pdw = PodcastWidget::new_initialized(&self.content.stack, &pd); - self.content.replace_widget(pdw); - } -} - -impl ContentState { - #[allow(dead_code)] - pub fn new() -> Result, ContentState> { - let content = Content::new(); - - content.podcasts.init(&content.stack); - if content.podcasts.flowbox.get_children().is_empty() { - content.stack.set_visible_child_name("empty"); - return Err(ContentState { - content, - state: Empty {}, - }); - } - - content.stack.set_visible_child_name("podcasts"); - Ok(ContentState { - content, - state: PodcastsView {}, - }) - } - - #[allow(dead_code)] - pub fn get_stack(&self) -> gtk::Stack { - self.content.stack.clone() + // self.content.replace_widget(pdw); } } @@ -149,25 +102,8 @@ fn replace_podcasts(stack: >k::Stack, pop: &PopulatedView) { old.destroy(); } -#[allow(dead_code)] -pub fn show_widget(stack: >k::Stack) { - stack.set_visible_child_name("widget") -} - -pub fn show_podcasts(stack: >k::Stack) { - stack.set_visible_child_name("podcasts") -} - -pub fn show_empty(stack: >k::Stack) { - stack.set_visible_child_name("empty") -} - pub fn update_podcasts(stack: >k::Stack) { - let pods = PopulatedView::new_initialized(stack); - - if pods.flowbox.get_children().is_empty() { - show_empty(stack) - } + let pods = PopulatedView::new_initialized(); replace_podcasts(stack, &pods); } @@ -191,10 +127,10 @@ pub fn update_widget_preserve_vis(stack: >k::Stack, pd: &Podcast) { stack.set_visible_child_name(&vis) } -pub fn on_podcasts_child_activate(stack: >k::Stack, pd: &Podcast) { - update_widget(stack, pd); - stack.set_visible_child_full("widget", gtk::StackTransitionType::SlideLeft); -} +// pub fn on_podcasts_child_activate(stack: >k::Stack, pd: &Podcast) { +// update_widget(stack, pd); +// stack.set_visible_child_full("widget", gtk::StackTransitionType::SlideLeft); +// } // FIXME: Rename and remove aliases type ShowsPopulated = PopulatedView; @@ -203,7 +139,7 @@ type EpisodesPopulated = PodcastWidget; type EpisodesEmpty = EmptyView; struct Populated; -// struct Empty; +struct Empty; // struct Shows; // struct Episodes; @@ -229,7 +165,7 @@ impl ShowsMachine { let old = self.stack.get_child_by_name("shows").unwrap(); self.stack.remove(&old); - let pop = ShowsPopulated::new_initialized(&self.stack); + let pop = ShowsPopulated::new_initialized(); self.populated = pop; self.stack .add_titled(&self.populated.container, "shows", "Shows"); @@ -351,8 +287,10 @@ enum EpisodeStateWrapper { impl ShowStateWrapper { fn new() -> Self { let stack = gtk::Stack::new(); - let pop = ShowsPopulated::new_initialized(&stack); + let pop = ShowsPopulated::new_initialized(); let empty = EmptyView::new(); + stack.add_named(&pop.container, "populated"); + stack.add_named(&empty.container, "empty"); if pop.flowbox.get_children().is_empty() { stack.set_visible_child_name("empty"); @@ -363,7 +301,7 @@ impl ShowStateWrapper { state: Empty {}, }) } else { - stack.set_visible_child_name("shows"); + stack.set_visible_child_name("populated"); ShowStateWrapper::Populated(ShowsMachine { empty, @@ -387,13 +325,45 @@ impl ShowStateWrapper { ShowStateWrapper::Empty(ref mut val) => val.update(), } } + + fn get_stack(&self) -> gtk::Stack { + match *self { + ShowStateWrapper::Populated(ref val) => val.stack.clone(), + ShowStateWrapper::Empty(ref val) => val.stack.clone(), + } + } } impl EpisodeStateWrapper { + // FIXME: + fn new() -> Self { + let pop = PodcastWidget::new(); + let empty = EmptyView::new(); + let stack = gtk::Stack::new(); + + stack.add_named(&pop.container, "populated"); + stack.add_named(&empty.container, "empty"); + stack.set_visible_child_name("empty"); + + EpisodeStateWrapper::Empty(EpisodesMachine { + empty, + populated: pop, + stack, + state: Empty {}, + }) + } + fn switch(self) -> Self { match self { EpisodeStateWrapper::Populated(val) => EpisodeStateWrapper::Empty(val.into()), EpisodeStateWrapper::Empty(val) => EpisodeStateWrapper::Populated(val.into()), } } + + fn get_stack(&self) -> gtk::Stack { + match *self { + EpisodeStateWrapper::Populated(ref val) => val.stack.clone(), + EpisodeStateWrapper::Empty(ref val) => val.stack.clone(), + } + } } diff --git a/hammond-gtk/src/main.rs b/hammond-gtk/src/main.rs index f4fd89b..0cf43dd 100644 --- a/hammond-gtk/src/main.rs +++ b/hammond-gtk/src/main.rs @@ -67,7 +67,8 @@ fn build_ui(app: >k::Application) { // let ct = content::ContentState::new().unwrap(); // let stack = ct.get_stack(); - let ct = content::Content::new_initialized(); + // let ct = content::Content::new_initialized(); + let ct = content::Content::new(); let stack = ct.stack; window.add(&stack); diff --git a/hammond-gtk/src/views/podcasts.rs b/hammond-gtk/src/views/podcasts.rs index 711be87..0767d72 100644 --- a/hammond-gtk/src/views/podcasts.rs +++ b/hammond-gtk/src/views/podcasts.rs @@ -8,7 +8,7 @@ use hammond_data::Podcast; use utils::get_pixbuf_from_path; -use content; +// use content; #[derive(Debug, Clone)] pub struct PopulatedView { @@ -42,24 +42,25 @@ impl PopulatedView { } #[allow(dead_code)] - pub fn new_initialized(stack: >k::Stack) -> PopulatedView { + pub fn new_initialized() -> PopulatedView { let pop = PopulatedView::new(); - pop.init(stack); + pop.init(); pop } - pub fn init(&self, stack: >k::Stack) { - use gtk::WidgetExt; + pub fn init(&self) { + // pub fn init(&self, stack: >k::Stack) { + // use gtk::WidgetExt; - // TODO: handle unwraps. - self.flowbox - .connect_child_activated(clone!(stack => move |_, child| { - // This is such an ugly hack... - // let id = child.get_name().unwrap().parse::().unwrap(); - let id = WidgetExt::get_name(child).unwrap().parse::().unwrap(); - let parent = dbqueries::get_podcast_from_id(id).unwrap(); - on_flowbox_child_activate(&stack, &parent); - })); + // // TODO: handle unwraps. + // self.flowbox + // .connect_child_activated(clone!(stack => move |_, child| { + // // This is such an ugly hack... + // // let id = child.get_name().unwrap().parse::().unwrap(); + // let id = WidgetExt::get_name(child).unwrap().parse::().unwrap(); + // let parent = dbqueries::get_podcast_from_id(id).unwrap(); + // on_flowbox_child_activate(&stack, &parent); + // })); // Populate the flowbox with the Podcasts. self.populate_flowbox(); } @@ -139,6 +140,6 @@ impl PodcastChild { } } -fn on_flowbox_child_activate(stack: >k::Stack, parent: &Podcast) { - content::on_podcasts_child_activate(stack, parent) -} +// fn on_flowbox_child_activate(stack: >k::Stack, parent: &Podcast) { +// content::on_podcasts_child_activate(stack, parent) +// } diff --git a/hammond-gtk/src/widgets/podcast.rs b/hammond-gtk/src/widgets/podcast.rs index 206ffd9..b447384 100644 --- a/hammond-gtk/src/widgets/podcast.rs +++ b/hammond-gtk/src/widgets/podcast.rs @@ -112,7 +112,6 @@ fn on_unsub_button_clicked(stack: >k::Stack, pd: &Podcast, unsub_button: >k: }; } content::update_podcasts(stack); - content::show_podcasts(stack); } fn on_played_button_clicked(stack: >k::Stack, pd: &Podcast) {