Removed the multiple declarations of the clone macro.

This commit is contained in:
Jordan Petridis 2017-11-03 17:42:48 +02:00
parent 9787ce7979
commit 72ac709cda
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
6 changed files with 28 additions and 106 deletions

View File

@ -7,24 +7,6 @@ use hammond_data::index_feed::Database;
use podcasts_view::update_podcasts_view;
use utils;
// http://gtk-rs.org/tuto/closures
macro_rules! clone {
(@param _) => ( _ );
(@param $x:ident) => ( $x );
($($n:ident),+ => move || $body:expr) => (
{
$( let $n = $n.clone(); )+
move || $body
}
);
($($n:ident),+ => move |$($p:tt),+| $body:expr) => (
{
$( let $n = $n.clone(); )+
move |$(clone!(@param $p),)+| $body
}
);
}
pub fn get_headerbar(db: &Database, stack: &gtk::Stack) -> gtk::HeaderBar {
let builder = include_str!("../gtk/headerbar.ui");
let builder = gtk::Builder::new_from_string(builder);

View File

@ -21,6 +21,25 @@ use std::sync::{Arc, Mutex};
use gtk::prelude::*;
use gio::{ActionMapExt, ApplicationExt, MenuExt, SimpleActionExt};
// http://gtk-rs.org/tuto/closures
#[macro_export]
macro_rules! clone {
(@param _) => ( _ );
(@param $x:ident) => ( $x );
($($n:ident),+ => move || $body:expr) => (
{
$( let $n = $n.clone(); )+
move || $body
}
);
($($n:ident),+ => move |$($p:tt),+| $body:expr) => (
{
$( let $n = $n.clone(); )+
move |$(clone!(@param $p),)+| $body
}
);
}
mod views;
mod widgets;
mod headerbar;
@ -62,18 +81,15 @@ fn build_ui(app: &gtk::Application) {
app.add_action(&quit);
// Setup the dbcheckup in the app menu.
let db2 = Arc::clone(&db);
let check = gio::SimpleAction::new("check", None);
check.connect_activate(move |_, _| {
let _ = dbcheckup::run(&db2);
});
check.connect_activate(clone!(db => move |_, _| {
let _ = dbcheckup::run(&db);
}));
app.add_action(&check);
// Get the headerbar
let header = headerbar::get_headerbar(&db, &stack);
// TODO: add delay, cause else there's lock contention for the db obj.
// utils::refresh_db(db.clone(), stack.clone());
window.set_titlebar(&header);
window.show_all();

View File

@ -13,24 +13,6 @@ use std::sync::mpsc::{channel, Receiver};
use views::podcasts_view;
// http://gtk-rs.org/tuto/closures
macro_rules! clone {
(@param _) => ( _ );
(@param $x:ident) => ( $x );
($($n:ident),+ => move || $body:expr) => (
{
$( let $n = $n.clone(); )+
move || $body
}
);
($($n:ident),+ => move |$($p:tt),+| $body:expr) => (
{
$( let $n = $n.clone(); )+
move |$(clone!(@param $p),)+| $body
}
);
}
// Create a thread local storage that will store the arguments to be transfered.
thread_local!(
static GLOBAL: RefCell<Option<(Database,
@ -68,10 +50,9 @@ pub fn refresh_feed(db: &Database, stack: &gtk::Stack, source: &mut Source) {
*global.borrow_mut() = Some((db, stack, receiver));
}));
let mut source = source.clone();
// TODO: add timeout option and error reporting.
thread::spawn(clone!(db => move || {
let foo_ = hammond_data::index_feed::refresh_source(&db, &mut source);
thread::spawn(clone!(db, source => move || {
let foo_ = hammond_data::index_feed::refresh_source(&db, &mut source.clone());
if let Ok(x) = foo_ {
let Feed(mut req, s) = x;

View File

@ -6,24 +6,6 @@ use hammond_data::index_feed::Database;
use widgets::podcast::*;
// http://gtk-rs.org/tuto/closures
macro_rules! clone {
(@param _) => ( _ );
(@param $x:ident) => ( $x );
($($n:ident),+ => move || $body:expr) => (
{
$( let $n = $n.clone(); )+
move || $body
}
);
($($n:ident),+ => move |$($p:tt),+| $body:expr) => (
{
$( let $n = $n.clone(); )+
move |$(clone!(@param $p),)+| $body
}
);
}
fn show_empty_view(stack: &gtk::Stack) {
let builder = include_str!("../../gtk/empty_view.ui");
let builder = gtk::Builder::new_from_string(builder);

View File

@ -1,7 +1,7 @@
use open;
use hammond_data::dbqueries;
use hammond_data::models::Episode;
use hammond_data::models::{Episode, Podcast};
use hammond_downloader::downloader;
use hammond_data::index_feed::Database;
use hammond_data::dbcheckup::*;
@ -19,26 +19,6 @@ use gtk;
use gtk::prelude::*;
use gtk::{ContainerExt, TextBufferExt};
// http://gtk-rs.org/tuto/closures
// FIXME: Atm this macro is copied into every module.
// Figure out how to propely define once and export it instead.
macro_rules! clone {
(@param _) => ( _ );
(@param $x:ident) => ( $x );
($($n:ident),+ => move || $body:expr) => (
{
$( let $n = $n.clone(); )+
move || $body
}
);
($($n:ident),+ => move |$($p:tt),+| $body:expr) => (
{
$( let $n = $n.clone(); )+
move |$(clone!(@param $p),)+| $body
}
);
}
thread_local!(
static GLOBAL: RefCell<Option<((
gtk::Button, gtk::Button, gtk::Button, Receiver<bool>,
@ -218,15 +198,14 @@ fn receive() -> glib::Continue {
glib::Continue(false)
}
pub fn episodes_listbox(db: &Database, pd_title: &str) -> Result<gtk::ListBox> {
pub fn episodes_listbox(db: &Database, pd: &Podcast) -> Result<gtk::ListBox> {
let conn = db.lock().unwrap();
let pd = dbqueries::load_podcast_from_title(&conn, pd_title)?;
let mut episodes = dbqueries::get_pd_episodes(&conn, &pd)?;
drop(conn);
let list = gtk::ListBox::new();
episodes.iter_mut().for_each(|ep| {
let w = epidose_widget(db, ep, pd_title);
let w = epidose_widget(db, ep, pd.title());
list.add(&w)
});

View File

@ -12,24 +12,6 @@ use hammond_data::dbqueries;
use widgets::episode::episodes_listbox;
use podcasts_view::update_podcasts_view;
// http://gtk-rs.org/tuto/closures
macro_rules! clone {
(@param _) => ( _ );
(@param $x:ident) => ( $x );
($($n:ident),+ => move || $body:expr) => (
{
$( let $n = $n.clone(); )+
move || $body
}
);
($($n:ident),+ => move |$($p:tt),+| $body:expr) => (
{
$( let $n = $n.clone(); )+
move |$(clone!(@param $p),)+| $body
}
);
}
fn podcast_widget(db: &Database, stack: &gtk::Stack, pd: &Podcast) -> gtk::Box {
// Adapted from gnome-music AlbumWidget
let pd_widget_source = include_str!("../../gtk/podcast_widget.ui");
@ -52,7 +34,7 @@ fn podcast_widget(db: &Database, stack: &gtk::Stack, pd: &Podcast) -> gtk::Box {
}));
title_label.set_text(pd.title());
let listbox = episodes_listbox(db, pd.title());
let listbox = episodes_listbox(db, &pd);
if let Ok(l) = listbox {
view.add(&l);
}