Use Default trait for creating Widget's that don't need arguments.

This commit is contained in:
Jordan Petridis 2017-12-17 13:08:51 +02:00
parent 440badf1eb
commit 75fe0f8ff5
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
7 changed files with 68 additions and 54 deletions

View File

@ -60,8 +60,8 @@ impl ShowStack {
header: header.clone(),
});
let pop = ShowsPopulated::new_initialized(show.clone(), header);
let widget = ShowWidget::new();
let pop = ShowsPopulated::new(show.clone(), header);
let widget = ShowWidget::default();
let empty = EmptyView::new();
show.stack.add_named(&pop.container, "podcasts");
@ -90,7 +90,7 @@ impl ShowStack {
let vis = self.stack.get_visible_child_name().unwrap();
let old = self.stack.get_child_by_name("podcasts").unwrap();
let pop = ShowsPopulated::new();
let pop = ShowsPopulated::default();
pop.init(Rc::new(self.clone()), self.header.clone());
self.stack.remove(&old);
@ -109,7 +109,7 @@ impl ShowStack {
pub fn replace_widget(&self, pd: &Podcast) {
let old = self.stack.get_child_by_name("widget").unwrap();
let new = ShowWidget::new_initialized(Rc::new(self.clone()), self.header.clone(), pd);
let new = ShowWidget::new(Rc::new(self.clone()), self.header.clone(), pd);
self.stack.remove(&old);
self.stack.add_named(&new.container, "widget");

View File

@ -19,8 +19,8 @@ pub struct Header {
show_title: gtk::Label,
}
impl Header {
pub fn new() -> Rc<Header> {
impl Default for Header {
fn default() -> Header {
let builder = gtk::Builder::new_from_resource("/org/gnome/hammond/gtk/headerbar.ui");
let header: gtk::HeaderBar = builder.get_object("headerbar").unwrap();
@ -33,14 +33,23 @@ impl Header {
switch.set_halign(gtk::Align::Center);
switch.show();
Rc::new(Header {
Header {
container: header,
refresh,
add_toggle,
switch,
back_button,
show_title,
})
}
}
}
impl Header {
#[allow(dead_code)]
pub fn new(content: Rc<Content>) -> Rc<Header> {
let h = Header::default();
h.init(content);
Rc::new(h)
}
pub fn init(&self, content: Rc<Content>) {

View File

@ -24,6 +24,7 @@ use hammond_data::utils::checkup;
use gtk::prelude::*;
use gio::{ActionMapExt, ApplicationExt, MenuExt, SimpleActionExt};
use std::rc::Rc;
// http://gtk-rs.org/tuto/closures
#[macro_export]
@ -52,8 +53,6 @@ mod content;
mod utils;
mod static_resource;
// THIS IS STILL A PROTOTYPE.
fn build_ui(app: &gtk::Application) {
let menu = gio::Menu::new();
menu.append("Quit", "app.quit");
@ -64,14 +63,8 @@ fn build_ui(app: &gtk::Application) {
let window = gtk::ApplicationWindow::new(app);
window.set_default_size(1150, 650);
// TODO: this will blow horribly
// let ct = content::ContentState::new().unwrap();
// let stack = ct.get_stack();
// let ct = content::Content::new_initialized();
// Get the headerbar
let header = headerbar::Header::new();
let header = Rc::new(headerbar::Header::default());
let ct = content::Content::new(header.clone());
header.init(ct.clone());
window.set_titlebar(&header.container);

View File

@ -5,11 +5,17 @@ pub struct EmptyView {
pub container: gtk::Box,
}
impl EmptyView {
pub fn new() -> EmptyView {
impl Default for EmptyView {
fn default() -> Self {
let builder = gtk::Builder::new_from_resource("/org/gnome/hammond/gtk/empty_view.ui");
let view: gtk::Box = builder.get_object("empty_view").unwrap();
EmptyView { container: view }
}
}
impl EmptyView {
pub fn new() -> EmptyView {
EmptyView::default()
}
}

View File

@ -19,18 +19,8 @@ pub struct ShowsPopulated {
viewport: gtk::Viewport,
}
#[derive(Debug)]
struct ShowsChild {
container: gtk::Box,
title: gtk::Label,
cover: gtk::Image,
banner: gtk::Image,
number: gtk::Label,
child: gtk::FlowBoxChild,
}
impl ShowsPopulated {
pub fn new() -> ShowsPopulated {
impl Default for ShowsPopulated {
fn default() -> Self {
let builder = gtk::Builder::new_from_resource("/org/gnome/hammond/gtk/shows_view.ui");
let container: gtk::Box = builder.get_object("fb_parent").unwrap();
let flowbox: gtk::FlowBox = builder.get_object("flowbox").unwrap();
@ -42,10 +32,11 @@ impl ShowsPopulated {
viewport,
}
}
}
#[allow(dead_code)]
pub fn new_initialized(show: Rc<ShowStack>, header: Rc<Header>) -> ShowsPopulated {
let pop = ShowsPopulated::new();
impl ShowsPopulated {
pub fn new(show: Rc<ShowStack>, header: Rc<Header>) -> ShowsPopulated {
let pop = ShowsPopulated::default();
pop.init(show, header);
pop
}
@ -74,7 +65,7 @@ impl ShowsPopulated {
if let Ok(pds) = podcasts {
pds.iter().for_each(|parent| {
let flowbox_child = ShowsChild::new_initialized(parent);
let flowbox_child = ShowsChild::new(parent);
self.flowbox.add(&flowbox_child.child);
});
self.flowbox.show_all();
@ -86,11 +77,20 @@ impl ShowsPopulated {
}
}
impl ShowsChild {
fn new() -> ShowsChild {
#[derive(Debug)]
struct ShowsChild {
container: gtk::Box,
title: gtk::Label,
cover: gtk::Image,
banner: gtk::Image,
number: gtk::Label,
child: gtk::FlowBoxChild,
}
impl Default for ShowsChild {
fn default() -> Self {
let builder = gtk::Builder::new_from_resource("/org/gnome/hammond/gtk/shows_child.ui");
// Copy of gnome-music AlbumWidget
let container: gtk::Box = builder.get_object("fb_child").unwrap();
let title: gtk::Label = builder.get_object("pd_title").unwrap();
let cover: gtk::Image = builder.get_object("pd_cover").unwrap();
@ -109,6 +109,15 @@ impl ShowsChild {
child,
}
}
}
impl ShowsChild {
pub fn new(pd: &Podcast) -> ShowsChild {
let child = ShowsChild::default();
child.init(pd);
child
}
fn init(&self, pd: &Podcast) {
self.title.set_text(pd.title());
@ -122,13 +131,6 @@ impl ShowsChild {
self.configure_banner(pd);
}
pub fn new_initialized(pd: &Podcast) -> ShowsChild {
let child = ShowsChild::new();
child.init(pd);
child
}
fn configure_banner(&self, pd: &Podcast) {
let bann =
Pixbuf::new_from_resource_at_scale("/org/gnome/hammond/banner.png", 256, 256, true);

View File

@ -47,8 +47,8 @@ struct EpisodeWidget {
progress_label: gtk::Label,
}
impl EpisodeWidget {
fn new() -> EpisodeWidget {
impl Default for EpisodeWidget {
fn default() -> Self {
let builder = gtk::Builder::new_from_resource("/org/gnome/hammond/gtk/episode_widget.ui");
let container: gtk::Box = builder.get_object("episode_container").unwrap();
@ -95,9 +95,11 @@ impl EpisodeWidget {
progress_label,
}
}
}
pub fn new_initialized(episode: &mut EpisodeWidgetQuery, pd: &Podcast) -> EpisodeWidget {
let widget = EpisodeWidget::new();
impl EpisodeWidget {
pub fn new(episode: &mut EpisodeWidgetQuery, pd: &Podcast) -> EpisodeWidget {
let widget = EpisodeWidget::default();
widget.init(episode, pd);
widget
}
@ -284,7 +286,7 @@ pub fn episodes_listbox(pd: &Podcast) -> Result<gtk::ListBox> {
let list = gtk::ListBox::new();
episodes.into_iter().for_each(|mut ep| {
let widget = EpisodeWidget::new_initialized(&mut ep, pd);
let widget = EpisodeWidget::new(&mut ep, pd);
list.add(&widget.container);
let sep = gtk::Separator::new(gtk::Orientation::Vertical);

View File

@ -28,8 +28,8 @@ pub struct ShowWidget {
episodes: gtk::Frame,
}
impl ShowWidget {
pub fn new() -> ShowWidget {
impl Default for ShowWidget {
fn default() -> Self {
let builder = gtk::Builder::new_from_resource("/org/gnome/hammond/gtk/show_widget.ui");
let container: gtk::Box = builder.get_object("container").unwrap();
let episodes: gtk::Frame = builder.get_object("episodes").unwrap();
@ -54,9 +54,11 @@ impl ShowWidget {
episodes,
}
}
}
pub fn new_initialized(shows: Rc<ShowStack>, header: Rc<Header>, pd: &Podcast) -> ShowWidget {
let pdw = ShowWidget::new();
impl ShowWidget {
pub fn new(shows: Rc<ShowStack>, header: Rc<Header>, pd: &Podcast) -> ShowWidget {
let pdw = ShowWidget::default();
pdw.init(shows, header, pd);
pdw
}