application: Use gio::ActionEntryBuilder for actions

Allows us to drop the action! macro and usage of the clone! macro
in the application file.

The first occurrence of clone! is now in window.rs, so the comment
explaining it has moved there.
This commit is contained in:
Christopher Davis 2022-11-23 13:51:27 -05:00 • committed by Sophie Herold
parent 29198b68f9
commit aa781d3864
2 changed files with 29 additions and 43 deletions

View file

@ -22,8 +22,6 @@ use crate::i18n::*;
use adw::prelude::*; use adw::prelude::*;
use adw::subclass::prelude::*; use adw::subclass::prelude::*;
use glib::clone;
use gtk_macros::*;
use crate::config; use crate::config;
use crate::window::LpWindow; use crate::window::LpWindow;
@ -109,48 +107,28 @@ impl LpApplication {
} }
pub fn setup_actions(&self) { pub fn setup_actions(&self) {
// action! is a macro from gtk_macros // gio::ActionEntryBuilder allows us to build and store an action on an object
// that creates a GSimpleAction with a callback. // that implements gio::ActionMap. Here we build the application's actions and
// clone! is a macro from glib-rs that allows // add them with add_action_entries().
// you to easily handle references in callbacks let actions = [
// without refcycles or leaks. gio::ActionEntryBuilder::new("about")
// .activate(|app: &Self, _, _| app.show_about())
// When you don't want the callback to keep the .build(),
// Object alive, pass as @weak. Otherwise, pass gio::ActionEntryBuilder::new("help")
// as @strong. Most of the time you will want .activate(|app: &Self, _, _| app.show_help())
// to use @weak. .build(),
action!( gio::ActionEntryBuilder::new("quit")
self, .activate(|app: &Self, _, _| app.quit())
"about", .build(),
clone!(@weak self as app => move |_, _| { gio::ActionEntryBuilder::new("new-window")
app.show_about(); .activate(|app: &Self, _, _| {
}) let win = LpWindow::new(app);
); win.show();
})
.build(),
];
action!( self.add_action_entries(actions).unwrap();
self,
"help",
clone!(@weak self as app => move |_, _| {
app.show_help();
})
);
action!(
self,
"quit",
clone!(@weak self as app => move |_, _| {
app.quit();
})
);
action!(
self,
"new-window",
clone!(@weak self as app => move |_, _| {
let win = LpWindow::new(&app);
win.show();
})
);
self.set_accels_for_action("app.help", &["F1"]); self.set_accels_for_action("app.help", &["F1"]);
self.set_accels_for_action("app.quit", &["<Primary>Q"]); self.set_accels_for_action("app.quit", &["<Primary>Q"]);

View file

@ -159,6 +159,14 @@ mod imp {
.chain_property::<LpImage>("best-fit") .chain_property::<LpImage>("best-fit")
.watch( .watch(
glib::Object::NONE, glib::Object::NONE,
// clone! is a macro from glib-rs that allows
// you to easily handle references in callbacks
// without refcycles or leaks.
//
// When you don't want the callback to keep the
// Object alive, pass as @weak. Otherwise, pass
// as @strong. Most of the time you will want
// to use @weak.
glib::clone!(@weak obj => move || { glib::clone!(@weak obj => move || {
let enabled = obj let enabled = obj
.imp() .imp()