[gnome-builder/wip/gtk4-port: 1405/1774] plugins/meson-templates: style cleanup




commit 0e37cbfef1a3ea1cd10139fc20d7066237be9762
Author: Christian Hergert <chergert redhat com>
Date:   Tue Jun 7 17:16:17 2022 -0700

    plugins/meson-templates: style cleanup
    
    I'm very opinionated about this stuff, so maintainers choice.

 .../meson-templates/resources/src/application.c    | 74 +++++++++-------------
 1 file changed, 31 insertions(+), 43 deletions(-)
---
diff --git a/src/plugins/meson-templates/resources/src/application.c 
b/src/plugins/meson-templates/resources/src/application.c
index c327dfc8c..27b290107 100644
--- a/src/plugins/meson-templates/resources/src/application.c
+++ b/src/plugins/meson-templates/resources/src/application.c
@@ -1,6 +1,7 @@
 {{include "license.c"}}
 
 #include "{{prefix}}-application.h"
+#include "{{prefix}}-config.h"
 #include "{{prefix}}-window.h"
 
 struct _{{PreFix}}Application
@@ -18,70 +19,48 @@ G_DEFINE_TYPE ({{PreFix}}Application, {{prefix_}}_application, {{if is_adwaita}}
 {{prefix_}}_application_new (const char        *application_id,
 {{spaces}}                  GApplicationFlags  flags)
 {
+  g_return_val_if_fail (application_id != NULL, NULL);
+
   return g_object_new ({{PREFIX}}_TYPE_APPLICATION,
                        "application-id", application_id,
                        "flags", flags,
                        NULL);
 }
 
-static void
-{{prefix_}}_application_finalize (GObject *object)
-{
-  {{PreFix}}Application *self = ({{PreFix}}Application *)object;
-
-  G_OBJECT_CLASS ({{prefix_}}_application_parent_class)->finalize (object);
-}
-
 static void
 {{prefix_}}_application_activate (GApplication *app)
 {
   GtkWindow *window;
 
-  /* It's good practice to check your parameters at the beginning of the
-   * function. It helps catch errors early and in development instead of
-   * by your users.
-   */
-  g_assert (GTK_IS_APPLICATION (app));
+  g_assert ({{PREFIX}}_IS_APPLICATION (app));
 
-  /* Get the current window or create one if necessary. */
   window = gtk_application_get_active_window (GTK_APPLICATION (app));
   if (window == NULL)
     window = g_object_new ({{PREFIX}}_TYPE_WINDOW,
                            "application", app,
                            NULL);
 
-  /* Ask the window manager/compositor to present the window. */
   gtk_window_present (window);
 }
 
-
 static void
 {{prefix_}}_application_class_init ({{PreFix}}ApplicationClass *klass)
 {
-  GObjectClass *object_class = G_OBJECT_CLASS (klass);
   GApplicationClass *app_class = G_APPLICATION_CLASS (klass);
 
-  object_class->finalize = {{prefix_}}_application_finalize;
-
-  /*
-   * We connect to the activate callback to create a window when the application
-   * has been launched. Additionally, this callback notifies us when the user
-   * tries to launch a "second instance" of the application. When they try
-   * to do that, we'll just present any existing window.
-   */
   app_class->activate = {{prefix_}}_application_activate;
 }
 
 static void
-{{prefix_}}_application_show_about (GSimpleAction *action,
-{{spaces}}                         GVariant      *parameter,
-{{spaces}}                         gpointer       user_data)
+{{prefix_}}_application_about_action (GSimpleAction *action,
+{{spaces}}                           GVariant      *parameter,
+{{spaces}}                           gpointer       user_data)
 {
-  {{PreFix}}Application *self = {{PREFIX}}_APPLICATION (user_data);
+  static const char *authors[] = {"{{author}}", NULL};
+  {{PreFix}}Application *self = user_data;
   GtkWindow *window = NULL;
-  const gchar *authors[] = {"{{author}}", NULL};
 
-  g_return_if_fail ({{PREFIX}}_IS_APPLICATION (self));
+  g_assert ({{PREFIX}}_IS_APPLICATION (self));
 
   window = gtk_application_get_active_window (GTK_APPLICATION (self));
 
@@ -92,22 +71,31 @@ static void
                          NULL);
 }
 
-
 static void
-{{prefix_}}_application_init ({{PreFix}}Application *self)
+{{prefix_}}_application_quit_action (GSimpleAction *action,
+{{spaces}}                          GVariant      *parameter,
+{{spaces}}                          gpointer       user_data)
 {
-  g_autoptr (GSimpleAction) quit_action = g_simple_action_new ("quit", NULL);
-  g_signal_connect_swapped (quit_action, "activate", G_CALLBACK (g_application_quit), self);
-  g_action_map_add_action (G_ACTION_MAP (self), G_ACTION (quit_action));
+  {{PreFix}}Application *self = user_data;
+
+  g_assert ({{PREFIX}}_IS_APPLICATION (self));
+
+  g_application_quit (G_APPLICATION (self));
+}
 
-  g_autoptr (GSimpleAction) about_action = g_simple_action_new ("about", NULL);
-  g_signal_connect (about_action, "activate", G_CALLBACK ({{prefix_}}_application_show_about), self);
-  g_action_map_add_action (G_ACTION_MAP (self), G_ACTION (about_action));
+static const GActionEntry app_actions[] = {
+  { "quit", {{prefix_}}_application_quit_action },
+  { "about", {{prefix_}}_application_about_action },
+};
 
+static void
+{{prefix_}}_application_init ({{PreFix}}Application *self)
+{
+  g_action_map_add_action_entries (G_ACTION_MAP (self),
+                                   app_actions,
+                                   G_N_ELEMENTS (app_actions),
+                                   self);
   gtk_application_set_accels_for_action (GTK_APPLICATION (self),
                                          "app.quit",
-                                         (const char *[]) {
-                                           "<primary>q",
-                                           NULL,
-                                         });
+                                         (const char *[]) { "<primary>q", NULL });
 }


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