Use a macro when setting up simple SimpleActions

This commit is contained in:
Zander Brown 2018-05-29 14:54:58 +01:00 committed by Jordan Petridis
parent 7115eb573c
commit 2c203acbd2

View File

@ -22,6 +22,20 @@ use widgets::{about_dialog, mark_all_notif, remove_show_notif};
use std::rc::Rc; use std::rc::Rc;
use std::sync::Arc; use std::sync::Arc;
/// Creates an action named $called in the action map $on with the handler $handle
macro_rules! action {
($on:expr, $called:expr, $handle:expr) => {{
// Create a stateless, parameterless action
let act = SimpleAction::new($called, None);
// Connect the handler
act.connect_activate($handle);
// Add it to the map
$on.add_action(&act);
// Return the action
act
}};
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum Action { pub enum Action {
RefreshAllViews, RefreshAllViews,
@ -90,11 +104,9 @@ impl App {
// Create the headerbar // Create the headerbar
let header = Rc::new(Header::new(&content, &window, &sender)); let header = Rc::new(Header::new(&content, &window, &sender));
let menu = SimpleAction::new("menu", None); action!(window, "menu", clone!(header => move |_, _| {
menu.connect_activate(clone!(header => move |_, _| {
header.open_menu(); header.open_menu();
})); }));
window.add_action(&menu);
// Add the content main stack to the overlay. // Add the content main stack to the overlay.
let overlay = gtk::Overlay::new(); let overlay = gtk::Overlay::new();
@ -232,37 +244,41 @@ impl App {
// Create the `refresh` action. // Create the `refresh` action.
// //
// This will trigger a refresh of all the shows in the database. // This will trigger a refresh of all the shows in the database.
let refresh = SimpleAction::new("refresh", None); action!(
refresh.connect_activate(clone!(sender => move |_, _| { app,
"refresh",
clone!(sender => move |_, _| {
gtk::idle_add(clone!(sender => move || { gtk::idle_add(clone!(sender => move || {
let s: Option<Vec<_>> = None; let s: Option<Vec<_>> = None;
utils::refresh(s, sender.clone()); utils::refresh(s, sender.clone());
glib::Continue(false) glib::Continue(false)
})); }));
})); })
app.add_action(&refresh); );
app.set_accels_for_action("app.refresh", &["<primary>r"]); app.set_accels_for_action("app.refresh", &["<primary>r"]);
// Create the `OPML` import action // Create the `OPML` import action
let import = SimpleAction::new("import", None); action!(
import.connect_activate(clone!(sender, app => move |_, _| { app,
"import",
clone!(sender, app => move |_, _| {
let window = app.get_active_window().expect("Failed to get active window"); let window = app.get_active_window().expect("Failed to get active window");
utils::on_import_clicked(&window, &sender); utils::on_import_clicked(&window, &sender);
})); })
app.add_action(&import); );
// Create the action that shows a `gtk::AboutDialog` // Create the action that shows a `gtk::AboutDialog`
let about = SimpleAction::new("about", None); action!(
about.connect_activate(clone!(app => move |_, _| { app,
"about",
clone!(app => move |_, _| {
let window = app.get_active_window().expect("Failed to get active window"); let window = app.get_active_window().expect("Failed to get active window");
about_dialog(&window); about_dialog(&window);
})); })
app.add_action(&about); );
// Create the quit action // Create the quit action
let quit = SimpleAction::new("quit", None); action!(app, "quit", clone!(app => move |_, _| app.quit()));
quit.connect_activate(clone!(app => move |_, _| app.quit()));
app.add_action(&quit);
app.set_accels_for_action("app.quit", &["<primary>q"]); app.set_accels_for_action("app.quit", &["<primary>q"]);
// Bind the hamburger menu button to `F10` // Bind the hamburger menu button to `F10`