From f9ac6470adeef0b54b58c591afddef0cb247fdfc Mon Sep 17 00:00:00 2001 From: Christopher Davis Date: Wed, 22 Sep 2021 21:33:47 -0700 Subject: [PATCH] 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. --- src/window.rs | 95 +++++++++++++++------------------------------------ 1 file changed, 27 insertions(+), 68 deletions(-) diff --git a/src/window.rs b/src/window.rs index c558529..9a80f00 100644 --- a/src/window.rs +++ b/src/window.rs @@ -23,7 +23,6 @@ use glib::subclass::prelude::*; use gtk::prelude::*; use gtk::subclass::prelude::*; use gtk::CompositeTemplate; -use gtk_macros::*; use libadwaita::subclass::prelude::*; use crate::config; @@ -74,7 +73,29 @@ mod imp { // bind_template() is a function generated by the // CompositeTemplate macro to bind all children at once. 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) { obj.init_template(); } @@ -88,7 +109,7 @@ mod imp { obj.add_css_class("devel"); } - obj.setup_actions(); + obj.set_actions_enabled(false); self.status_page .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") } - 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) { let chooser = gtk::FileChooserNative::new( Some(&"Open Image".to_string()), @@ -260,28 +237,10 @@ impl IvWindow { } pub fn set_actions_enabled(&self, enabled: bool) { - // Here we need to downcast because lookup_action()` returns a plain - // GAction, not a GSimpleAction. - self.lookup_action("open-with") - .unwrap() - .downcast::() - .unwrap() - .set_enabled(enabled); - self.lookup_action("set-wallpaper") - .unwrap() - .downcast::() - .unwrap() - .set_enabled(enabled); - self.lookup_action("toggle-fullscreen") - .unwrap() - .downcast::() - .unwrap() - .set_enabled(enabled); - self.lookup_action("print") - .unwrap() - .downcast::() - .unwrap() - .set_enabled(enabled); + self.action_set_enabled("win.open-with", enabled); + self.action_set_enabled("win.set-wallpaper", enabled); + self.action_set_enabled("win.toggle-fullscreen", enabled); + self.action_set_enabled("win.print", enabled); } // Adapted from https://gitlab.gnome.org/GNOME/eog/-/blob/master/src/eog-window.c:eog_window_obtain_desired_size