App: Replace action macro with a generic fuction.

This commit is contained in:
Jordan Petridis 2018-07-27 18:28:46 +03:00
parent 838320785e
commit e4e35e4c57
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6

View File

@ -1,7 +1,7 @@
#![allow(new_without_default)] #![allow(new_without_default)]
use gio::{self, prelude::*, ApplicationFlags, SettingsBindFlags, SettingsExt, SimpleAction}; use gio::{self, prelude::*, ActionMapExt, SettingsExt};
use glib; use glib::{self, Variant};
use gtk; use gtk;
use gtk::prelude::*; use gtk::prelude::*;
@ -25,18 +25,18 @@ use std::sync::Arc;
pub const APP_ID: &str = "org.gnome.Podcasts"; pub const APP_ID: &str = "org.gnome.Podcasts";
/// Creates an action named $called in the action map $on with the handler $handle /// Creates an action named `name` in the action map `T with the handler `F`
macro_rules! action { fn action<T, F>(thing: &T, name: &str, action: F)
($on:expr, $called:expr, $handle:expr) => {{ where
// Create a stateless, parameterless action T: ActionMapExt,
let act = SimpleAction::new($called, None); for<'r, 's> F: Fn(&'r gio::SimpleAction, &'s Option<Variant>) + 'static,
// Connect the handler {
act.connect_activate($handle); // Create a stateless, parameterless action
// Add it to the map let act = gio::SimpleAction::new(name, None);
$on.add_action(&act); // Connect the handler
// Return the action act.connect_activate(action);
act // Add it to the map
}}; thing.add_action(&act);
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -154,7 +154,7 @@ impl App {
"dark-theme", "dark-theme",
&gtk_settings, &gtk_settings,
"gtk-application-prefer-dark-theme", "gtk-application-prefer-dark-theme",
SettingsBindFlags::DEFAULT, gio::SettingsBindFlags::DEFAULT,
); );
} }
@ -195,7 +195,7 @@ 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.
action!(win, "refresh", clone!(sender => move |_, _| { action(win, "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());
@ -205,18 +205,18 @@ impl App {
self.instance.set_accels_for_action("win.refresh", &["<primary>r"]); self.instance.set_accels_for_action("win.refresh", &["<primary>r"]);
// Create the `OPML` import action // Create the `OPML` import action
action!(win, "import", clone!(sender, win => move |_, _| { action(win, "import", clone!(sender, win => move |_, _| {
utils::on_import_clicked(&win, &sender) utils::on_import_clicked(&win, &sender)
})); }));
// Create the action that shows a `gtk::AboutDialog` // Create the action that shows a `gtk::AboutDialog`
action!(win, "about", clone!(win => move |_, _| about_dialog(&win))); action(win, "about", clone!(win => move |_, _| about_dialog(&win)));
// Create the quit action // Create the quit action
action!(win, "quit", clone!(instance => move |_, _| instance.quit())); action(win, "quit", clone!(instance => move |_, _| instance.quit()));
self.instance.set_accels_for_action("win.quit", &["<primary>q"]); self.instance.set_accels_for_action("win.quit", &["<primary>q"]);
action!( action(
win, win,
"preferences", "preferences",
clone!(win, settings => move |_, _| { clone!(win, settings => move |_, _| {
@ -227,7 +227,7 @@ impl App {
self.instance.set_accels_for_action("win.preferences", &["<primary>e"]); self.instance.set_accels_for_action("win.preferences", &["<primary>e"]);
// Create the menu action // Create the menu action
action!(win, "menu",clone!(header => move |_, _| header.open_menu())); action(win, "menu",clone!(header => move |_, _| header.open_menu()));
// Bind the hamburger menu button to `F10` // Bind the hamburger menu button to `F10`
self.instance.set_accels_for_action("win.menu", &["F10"]); self.instance.set_accels_for_action("win.menu", &["F10"]);
} }
@ -297,7 +297,7 @@ impl App {
} }
pub fn run() { pub fn run() {
let application = gtk::Application::new(APP_ID, ApplicationFlags::empty()) let application = gtk::Application::new(APP_ID, gio::ApplicationFlags::empty())
.expect("Application Initialization failed..."); .expect("Application Initialization failed...");
let weak_app = application.downgrade(); let weak_app = application.downgrade();