[gnome-builder/ebassi/gnome-app-template: 2/3] meson-templates: Improve the Python GTK4 template




commit c56b6a9ae3cc77a43d9cc6fd66d34c08837bf6a8
Author: Emmanuele Bassi <ebassi gnome org>
Date:   Tue Feb 8 22:27:21 2022 +0000

    meson-templates: Improve the Python GTK4 template
    
    Add more docstrings, and modify create_action() to also handle
    accelerators, just like examples in every other language.

 .../meson-templates/resources/src/main-gtk4.py     | 24 ++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)
---
diff --git a/src/plugins/meson-templates/resources/src/main-gtk4.py 
b/src/plugins/meson-templates/resources/src/main-gtk4.py
index eac683f18..554bf3e81 100755
--- a/src/plugins/meson-templates/resources/src/main-gtk4.py
+++ b/src/plugins/meson-templates/resources/src/main-gtk4.py
@@ -11,32 +11,52 @@ from .window import {{PreFix}}Window, AboutDialog
 
 
 class Application(Gtk.Application):
+    """The main application singleton class."""
+
     def __init__(self):
         super().__init__(application_id='{{appid}}',
                          flags=Gio.ApplicationFlags.FLAGS_NONE)
 
     def do_activate(self):
+        """Called when the application is activated.
+
+        We raise the application's main window, creating it if
+        necessary.
+        """
         win = self.props.active_window
         if not win:
             win = {{PreFix}}Window(application=self)
+        self.create_action('quit', self.quit, ['<primary>q'])
         self.create_action('about', self.on_about_action)
         self.create_action('preferences', self.on_preferences_action)
         win.present()
 
     def on_about_action(self, widget, _):
+        """Callback for the app.about action."""
         about = AboutDialog(self.props.active_window)
         about.present()
 
     def on_preferences_action(self, widget, _):
+        """Callback for the app.preferences action."""
         print('app.preferences action activated')
 
-    def create_action(self, name, callback):
-        """ Add an Action and connect to a callback """
+    def create_action(self, name, callback, shortcuts=None):
+        """Add an application action.
+
+        Args:
+            name: the name of the action
+            callback: the function to be called when the action is
+              activated
+            shortcuts: an optional list of accelerators
+        """
         action = Gio.SimpleAction.new(name, None)
         action.connect("activate", callback)
         self.add_action(action)
+        if shortcuts:
+            self.set_accels_for_action(f"app.{name}", shorcuts)
 
 
 def main(version):
+    """The application's entry point."""
     app = Application()
     return app.run(sys.argv)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]