window: Use gtk_widget_class_install_action ()

Simplifies our action handling a lot - we no longer need
to use the `action!` macro here, and we don't need
`lookup_action ()` to set the action state or activate
actions.
This commit is contained in:
Christopher Davis 2021-09-22 21:33:47 -07:00
parent c6ff46a578
commit f9ac6470ad

View file

@ -23,7 +23,6 @@ use glib::subclass::prelude::*;
use gtk::prelude::*; use gtk::prelude::*;
use gtk::subclass::prelude::*; use gtk::subclass::prelude::*;
use gtk::CompositeTemplate; use gtk::CompositeTemplate;
use gtk_macros::*;
use libadwaita::subclass::prelude::*; use libadwaita::subclass::prelude::*;
use crate::config; use crate::config;
@ -74,7 +73,29 @@ mod imp {
// bind_template() is a function generated by the // bind_template() is a function generated by the
// CompositeTemplate macro to bind all children at once. // CompositeTemplate macro to bind all children at once.
Self::bind_template(klass); Self::bind_template(klass);
// Set up actions
klass.install_action("win.toggle-fullscreen", None, move |win, _, _| {
win.set_fullscreened(!win.is_fullscreened());
});
klass.install_action("win.open", None, move |win, _, _| {
win.pick_file();
});
klass.install_action("win.open-with", None, move |win, _, _| {
win.open_with();
});
klass.install_action("win.set-wallpaper", None, move |win, _, _| {
win.set_wallpaper();
});
klass.install_action("win.print", None, move |win, _, _| {
win.print();
});
} }
fn instance_init(obj: &glib::subclass::InitializingObject<Self>) { fn instance_init(obj: &glib::subclass::InitializingObject<Self>) {
obj.init_template(); obj.init_template();
} }
@ -88,7 +109,7 @@ mod imp {
obj.add_css_class("devel"); obj.add_css_class("devel");
} }
obj.setup_actions(); obj.set_actions_enabled(false);
self.status_page self.status_page
.set_icon_name(Some(&format!("{}-symbolic", config::APP_ID))); .set_icon_name(Some(&format!("{}-symbolic", config::APP_ID)));
@ -142,50 +163,6 @@ impl IvWindow {
glib::Object::new(&[("application", app)]).expect("Failed to create IvWindow") glib::Object::new(&[("application", app)]).expect("Failed to create IvWindow")
} }
pub fn setup_actions(&self) {
action!(
self,
"toggle-fullscreen",
clone!(@weak self as win => move |_, _| {
win.set_fullscreened(!win.is_fullscreened());
})
);
action!(
self,
"open",
clone!(@weak self as win => move |_, _| {
win.pick_file();
})
);
action!(
self,
"open-with",
clone!(@weak self as win => move |_, _| {
win.open_with();
})
);
action!(
self,
"set-wallpaper",
clone!(@weak self as win => move |_, _| {
win.set_wallpaper();
})
);
action!(
self,
"print",
clone!(@weak self as win => move |_, _| {
win.print();
})
);
self.set_actions_enabled(false);
}
fn pick_file(&self) { fn pick_file(&self) {
let chooser = gtk::FileChooserNative::new( let chooser = gtk::FileChooserNative::new(
Some(&"Open Image".to_string()), Some(&"Open Image".to_string()),
@ -260,28 +237,10 @@ impl IvWindow {
} }
pub fn set_actions_enabled(&self, enabled: bool) { pub fn set_actions_enabled(&self, enabled: bool) {
// Here we need to downcast because lookup_action()` returns a plain self.action_set_enabled("win.open-with", enabled);
// GAction, not a GSimpleAction. self.action_set_enabled("win.set-wallpaper", enabled);
self.lookup_action("open-with") self.action_set_enabled("win.toggle-fullscreen", enabled);
.unwrap() self.action_set_enabled("win.print", enabled);
.downcast::<gio::SimpleAction>()
.unwrap()
.set_enabled(enabled);
self.lookup_action("set-wallpaper")
.unwrap()
.downcast::<gio::SimpleAction>()
.unwrap()
.set_enabled(enabled);
self.lookup_action("toggle-fullscreen")
.unwrap()
.downcast::<gio::SimpleAction>()
.unwrap()
.set_enabled(enabled);
self.lookup_action("print")
.unwrap()
.downcast::<gio::SimpleAction>()
.unwrap()
.set_enabled(enabled);
} }
// Adapted from https://gitlab.gnome.org/GNOME/eog/-/blob/master/src/eog-window.c:eog_window_obtain_desired_size // Adapted from https://gitlab.gnome.org/GNOME/eog/-/blob/master/src/eog-window.c:eog_window_obtain_desired_size