Hammond-gtk: Inital split of content.rs into stacks module.
This commit is contained in:
parent
8cbae4050e
commit
bdda596806
@ -6,8 +6,8 @@ use gtk::prelude::*;
|
|||||||
use hammond_data::{Podcast, Source};
|
use hammond_data::{Podcast, Source};
|
||||||
use hammond_data::utils::checkup;
|
use hammond_data::utils::checkup;
|
||||||
|
|
||||||
use content::Content;
|
|
||||||
use headerbar::Header;
|
use headerbar::Header;
|
||||||
|
use stacks::Content;
|
||||||
use utils;
|
use utils;
|
||||||
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|||||||
@ -11,7 +11,7 @@ use std::sync::Arc;
|
|||||||
use std::sync::mpsc::Sender;
|
use std::sync::mpsc::Sender;
|
||||||
|
|
||||||
use app::Action;
|
use app::Action;
|
||||||
use content::Content;
|
use stacks::Content;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Header {
|
pub struct Header {
|
||||||
|
|||||||
@ -53,8 +53,9 @@ macro_rules! clone {
|
|||||||
|
|
||||||
mod views;
|
mod views;
|
||||||
mod widgets;
|
mod widgets;
|
||||||
|
mod stacks;
|
||||||
|
|
||||||
mod headerbar;
|
mod headerbar;
|
||||||
mod content;
|
|
||||||
mod app;
|
mod app;
|
||||||
|
|
||||||
mod utils;
|
mod utils;
|
||||||
|
|||||||
94
hammond-gtk/src/stacks/content.rs
Normal file
94
hammond-gtk/src/stacks/content.rs
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
use gtk;
|
||||||
|
use gtk::prelude::*;
|
||||||
|
|
||||||
|
use failure::Error;
|
||||||
|
|
||||||
|
use app::Action;
|
||||||
|
use stacks::EpisodeStack;
|
||||||
|
use stacks::ShowStack;
|
||||||
|
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::sync::mpsc::Sender;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct Content {
|
||||||
|
stack: gtk::Stack,
|
||||||
|
shows: Arc<ShowStack>,
|
||||||
|
episodes: Arc<EpisodeStack>,
|
||||||
|
sender: Sender<Action>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Content {
|
||||||
|
pub fn new(sender: Sender<Action>) -> Result<Content, Error> {
|
||||||
|
let stack = gtk::Stack::new();
|
||||||
|
let episodes = Arc::new(EpisodeStack::new(sender.clone())?);
|
||||||
|
let shows = Arc::new(ShowStack::new(sender.clone())?);
|
||||||
|
|
||||||
|
stack.add_titled(&episodes.stack, "episodes", "Episodes");
|
||||||
|
stack.add_titled(&shows.stack, "shows", "Shows");
|
||||||
|
|
||||||
|
Ok(Content {
|
||||||
|
stack,
|
||||||
|
shows,
|
||||||
|
episodes,
|
||||||
|
sender,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update(&self) {
|
||||||
|
self.update_episode_view();
|
||||||
|
self.update_shows_view();
|
||||||
|
self.update_widget()
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Maybe propagate the error?
|
||||||
|
pub fn update_episode_view(&self) {
|
||||||
|
if let Err(err) = self.episodes.update() {
|
||||||
|
error!("Something went wrong while trying to update the episode view.");
|
||||||
|
error!("Error: {}", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update_episode_view_if_baground(&self) {
|
||||||
|
if self.stack.get_visible_child_name() != Some("episodes".into()) {
|
||||||
|
self.update_episode_view();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update_shows_view(&self) {
|
||||||
|
if let Err(err) = self.shows.update_podcasts() {
|
||||||
|
error!("Something went wrong while trying to update the ShowsView.");
|
||||||
|
error!("Error: {}", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update_widget(&self) {
|
||||||
|
if let Err(err) = self.shows.update_widget() {
|
||||||
|
error!("Something went wrong while trying to update the Show Widget.");
|
||||||
|
error!("Error: {}", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update_widget_if_same(&self, pid: i32) {
|
||||||
|
if let Err(err) = self.shows.update_widget_if_same(pid) {
|
||||||
|
error!("Something went wrong while trying to update the Show Widget.");
|
||||||
|
error!("Error: {}", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update_widget_if_visible(&self) {
|
||||||
|
if self.stack.get_visible_child_name() == Some("shows".to_string())
|
||||||
|
&& self.shows.get_stack().get_visible_child_name() == Some("widget".to_string())
|
||||||
|
{
|
||||||
|
self.update_widget();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_stack(&self) -> gtk::Stack {
|
||||||
|
self.stack.clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_shows(&self) -> Arc<ShowStack> {
|
||||||
|
self.shows.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
75
hammond-gtk/src/stacks/episode.rs
Normal file
75
hammond-gtk/src/stacks/episode.rs
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
use gtk;
|
||||||
|
use gtk::Cast;
|
||||||
|
use gtk::prelude::*;
|
||||||
|
|
||||||
|
use failure::Error;
|
||||||
|
|
||||||
|
use views::empty::EmptyView;
|
||||||
|
use views::episodes::EpisodesView;
|
||||||
|
|
||||||
|
use app::Action;
|
||||||
|
|
||||||
|
use std::sync::mpsc::Sender;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct EpisodeStack {
|
||||||
|
// FIXME: remove pub
|
||||||
|
pub stack: gtk::Stack,
|
||||||
|
sender: Sender<Action>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EpisodeStack {
|
||||||
|
pub fn new(sender: Sender<Action>) -> Result<EpisodeStack, Error> {
|
||||||
|
let episodes = EpisodesView::new(sender.clone())?;
|
||||||
|
let empty = EmptyView::new();
|
||||||
|
let stack = gtk::Stack::new();
|
||||||
|
|
||||||
|
stack.add_named(&episodes.container, "episodes");
|
||||||
|
stack.add_named(&empty.container, "empty");
|
||||||
|
|
||||||
|
if episodes.is_empty() {
|
||||||
|
stack.set_visible_child_name("empty");
|
||||||
|
} else {
|
||||||
|
stack.set_visible_child_name("episodes");
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(EpisodeStack { stack, sender })
|
||||||
|
}
|
||||||
|
|
||||||
|
// Look into refactoring to a state-machine.
|
||||||
|
pub fn update(&self) -> Result<(), Error> {
|
||||||
|
let old = self.stack
|
||||||
|
.get_child_by_name("episodes")
|
||||||
|
.ok_or_else(|| format_err!("Faild to get \"episodes\" child from the stack."))?
|
||||||
|
.downcast::<gtk::Box>()
|
||||||
|
.map_err(|_| format_err!("Failed to downcast stack child to a Box."))?;
|
||||||
|
debug!("Name: {:?}", WidgetExt::get_name(&old));
|
||||||
|
|
||||||
|
let scrolled_window = old.get_children()
|
||||||
|
.first()
|
||||||
|
.ok_or_else(|| format_err!("Box container has no childs."))?
|
||||||
|
.clone()
|
||||||
|
.downcast::<gtk::ScrolledWindow>()
|
||||||
|
.map_err(|_| format_err!("Failed to downcast stack child to a ScrolledWindow."))?;
|
||||||
|
debug!("Name: {:?}", WidgetExt::get_name(&scrolled_window));
|
||||||
|
|
||||||
|
let eps = EpisodesView::new(self.sender.clone())?;
|
||||||
|
// Copy the vertical scrollbar adjustment from the old view into the new one.
|
||||||
|
scrolled_window
|
||||||
|
.get_vadjustment()
|
||||||
|
.map(|x| eps.set_vadjustment(&x));
|
||||||
|
|
||||||
|
self.stack.remove(&old);
|
||||||
|
self.stack.add_named(&eps.container, "episodes");
|
||||||
|
|
||||||
|
if eps.is_empty() {
|
||||||
|
self.stack.set_visible_child_name("empty");
|
||||||
|
} else {
|
||||||
|
self.stack.set_visible_child_name("episodes");
|
||||||
|
}
|
||||||
|
|
||||||
|
old.destroy();
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
7
hammond-gtk/src/stacks/mod.rs
Normal file
7
hammond-gtk/src/stacks/mod.rs
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
mod content;
|
||||||
|
mod episode;
|
||||||
|
mod show;
|
||||||
|
|
||||||
|
pub use self::content::Content;
|
||||||
|
pub use self::episode::EpisodeStack;
|
||||||
|
pub use self::show::ShowStack;
|
||||||
@ -8,106 +8,22 @@ use hammond_data::Podcast;
|
|||||||
use hammond_data::dbqueries;
|
use hammond_data::dbqueries;
|
||||||
|
|
||||||
use views::empty::EmptyView;
|
use views::empty::EmptyView;
|
||||||
use views::episodes::EpisodesView;
|
|
||||||
use views::shows::ShowsPopulated;
|
use views::shows::ShowsPopulated;
|
||||||
|
|
||||||
use app::Action;
|
use app::Action;
|
||||||
use widgets::show::ShowWidget;
|
use widgets::show::ShowWidget;
|
||||||
|
|
||||||
use std::sync::Arc;
|
|
||||||
use std::sync::mpsc::Sender;
|
use std::sync::mpsc::Sender;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct Content {
|
|
||||||
stack: gtk::Stack,
|
|
||||||
shows: Arc<ShowStack>,
|
|
||||||
episodes: Arc<EpisodeStack>,
|
|
||||||
sender: Sender<Action>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Content {
|
|
||||||
pub fn new(sender: Sender<Action>) -> Result<Content, Error> {
|
|
||||||
let stack = gtk::Stack::new();
|
|
||||||
let episodes = Arc::new(EpisodeStack::new(sender.clone())?);
|
|
||||||
let shows = Arc::new(ShowStack::new(sender.clone())?);
|
|
||||||
|
|
||||||
stack.add_titled(&episodes.stack, "episodes", "Episodes");
|
|
||||||
stack.add_titled(&shows.stack, "shows", "Shows");
|
|
||||||
|
|
||||||
Ok(Content {
|
|
||||||
stack,
|
|
||||||
shows,
|
|
||||||
episodes,
|
|
||||||
sender,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn update(&self) {
|
|
||||||
self.update_episode_view();
|
|
||||||
self.update_shows_view();
|
|
||||||
self.update_widget()
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Maybe propagate the error?
|
|
||||||
pub fn update_episode_view(&self) {
|
|
||||||
if let Err(err) = self.episodes.update() {
|
|
||||||
error!("Something went wrong while trying to update the episode view.");
|
|
||||||
error!("Error: {}", err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn update_episode_view_if_baground(&self) {
|
|
||||||
if self.stack.get_visible_child_name() != Some("episodes".into()) {
|
|
||||||
self.update_episode_view();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn update_shows_view(&self) {
|
|
||||||
if let Err(err) = self.shows.update_podcasts() {
|
|
||||||
error!("Something went wrong while trying to update the ShowsView.");
|
|
||||||
error!("Error: {}", err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn update_widget(&self) {
|
|
||||||
if let Err(err) = self.shows.update_widget() {
|
|
||||||
error!("Something went wrong while trying to update the Show Widget.");
|
|
||||||
error!("Error: {}", err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn update_widget_if_same(&self, pid: i32) {
|
|
||||||
if let Err(err) = self.shows.update_widget_if_same(pid) {
|
|
||||||
error!("Something went wrong while trying to update the Show Widget.");
|
|
||||||
error!("Error: {}", err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn update_widget_if_visible(&self) {
|
|
||||||
if self.stack.get_visible_child_name() == Some("shows".to_string())
|
|
||||||
&& self.shows.get_stack().get_visible_child_name() == Some("widget".to_string())
|
|
||||||
{
|
|
||||||
self.update_widget();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_stack(&self) -> gtk::Stack {
|
|
||||||
self.stack.clone()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_shows(&self) -> Arc<ShowStack> {
|
|
||||||
self.shows.clone()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct ShowStack {
|
pub struct ShowStack {
|
||||||
stack: gtk::Stack,
|
// FIXME: remove pub
|
||||||
|
pub stack: gtk::Stack,
|
||||||
sender: Sender<Action>,
|
sender: Sender<Action>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ShowStack {
|
impl ShowStack {
|
||||||
fn new(sender: Sender<Action>) -> Result<ShowStack, Error> {
|
pub fn new(sender: Sender<Action>) -> Result<ShowStack, Error> {
|
||||||
let stack = gtk::Stack::new();
|
let stack = gtk::Stack::new();
|
||||||
|
|
||||||
let show = ShowStack {
|
let show = ShowStack {
|
||||||
@ -263,65 +179,3 @@ impl ShowStack {
|
|||||||
self.stack.clone()
|
self.stack.clone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct EpisodeStack {
|
|
||||||
stack: gtk::Stack,
|
|
||||||
sender: Sender<Action>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl EpisodeStack {
|
|
||||||
fn new(sender: Sender<Action>) -> Result<EpisodeStack, Error> {
|
|
||||||
let episodes = EpisodesView::new(sender.clone())?;
|
|
||||||
let empty = EmptyView::new();
|
|
||||||
let stack = gtk::Stack::new();
|
|
||||||
|
|
||||||
stack.add_named(&episodes.container, "episodes");
|
|
||||||
stack.add_named(&empty.container, "empty");
|
|
||||||
|
|
||||||
if episodes.is_empty() {
|
|
||||||
stack.set_visible_child_name("empty");
|
|
||||||
} else {
|
|
||||||
stack.set_visible_child_name("episodes");
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(EpisodeStack { stack, sender })
|
|
||||||
}
|
|
||||||
|
|
||||||
// Look into refactoring to a state-machine.
|
|
||||||
pub fn update(&self) -> Result<(), Error> {
|
|
||||||
let old = self.stack
|
|
||||||
.get_child_by_name("episodes")
|
|
||||||
.ok_or_else(|| format_err!("Faild to get \"episodes\" child from the stack."))?
|
|
||||||
.downcast::<gtk::Box>()
|
|
||||||
.map_err(|_| format_err!("Failed to downcast stack child to a Box."))?;
|
|
||||||
debug!("Name: {:?}", WidgetExt::get_name(&old));
|
|
||||||
|
|
||||||
let scrolled_window = old.get_children()
|
|
||||||
.first()
|
|
||||||
.ok_or_else(|| format_err!("Box container has no childs."))?
|
|
||||||
.clone()
|
|
||||||
.downcast::<gtk::ScrolledWindow>()
|
|
||||||
.map_err(|_| format_err!("Failed to downcast stack child to a ScrolledWindow."))?;
|
|
||||||
debug!("Name: {:?}", WidgetExt::get_name(&scrolled_window));
|
|
||||||
|
|
||||||
let eps = EpisodesView::new(self.sender.clone())?;
|
|
||||||
// Copy the vertical scrollbar adjustment from the old view into the new one.
|
|
||||||
scrolled_window
|
|
||||||
.get_vadjustment()
|
|
||||||
.map(|x| eps.set_vadjustment(&x));
|
|
||||||
|
|
||||||
self.stack.remove(&old);
|
|
||||||
self.stack.add_named(&eps.container, "episodes");
|
|
||||||
|
|
||||||
if eps.is_empty() {
|
|
||||||
self.stack.set_visible_child_name("empty");
|
|
||||||
} else {
|
|
||||||
self.stack.set_visible_child_name("episodes");
|
|
||||||
}
|
|
||||||
|
|
||||||
old.destroy();
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue
Block a user