[devhelp] App: better function to get active DhWindow



commit 5a27bdf9bb139e96a0f7d09e9bdd94d85ca68b18
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Wed Dec 20 15:44:02 2017 +0100

    App: better function to get active DhWindow
    
    peek_first_window() was not a good name, since by reading the function
    prototype we didn't know that it created a new DhWindow if there was
    none. And we didn't know that it returned a DhWindow.
    
    get_active_main_window() has a name similar to
    gtk_application_get_active_window(), and with the create_if_none
    parameter things are done explicitly. create_if_none is set to FALSE in
    several cases, for example to set the transient-for of secondary
    windows.

 src/dh-app.c       |   45 +++++++++++++++++++++++++--------------------
 src/dh-app.h       |    4 +++-
 src/dh-assistant.c |    6 +++---
 3 files changed, 31 insertions(+), 24 deletions(-)
---
diff --git a/src/dh-app.c b/src/dh-app.c
index 13172c8..12c4612 100644
--- a/src/dh-app.c
+++ b/src/dh-app.c
@@ -25,7 +25,6 @@
 #include <glib/gi18n-lib.h>
 #include "dh-assistant.h"
 #include "dh-preferences.h"
-#include "dh-window.h"
 
 G_DEFINE_TYPE (DhApp, dh_app, GTK_TYPE_APPLICATION);
 
@@ -47,11 +46,10 @@ preferences_cb (GSimpleAction *action,
                 gpointer       user_data)
 {
         DhApp *app = DH_APP (user_data);
-        GtkWindow *window;
-
-        window = dh_app_peek_first_window (app);
+        GtkWindow *parent_window;
 
-        dh_preferences_show_dialog (window);
+        parent_window = (GtkWindow *) dh_app_get_active_main_window (app, FALSE);
+        dh_preferences_show_dialog (parent_window);
 }
 
 static void
@@ -60,7 +58,7 @@ about_cb (GSimpleAction *action,
           gpointer       user_data)
 {
         DhApp *app = DH_APP (user_data);
-        GtkWindow *parent;
+        GtkWindow *parent_window;
 
         const gchar *authors[] = {
                 "Mikael Hallendal <micke imendio com>",
@@ -74,9 +72,9 @@ about_cb (GSimpleAction *action,
                 NULL
         };
 
-        parent = dh_app_peek_first_window (app);
+        parent_window = (GtkWindow *) dh_app_get_active_main_window (app, FALSE);
 
-        gtk_show_about_dialog (parent,
+        gtk_show_about_dialog (parent_window,
                                /* Translators: please don't translate "Devhelp" (it's marked as
                                 * translatable for transliteration only).
                                 */
@@ -110,7 +108,7 @@ search_cb (GSimpleAction *action,
 {
         DhApp *app = DH_APP (user_data);
         const gchar *keyword;
-        GtkWindow *window;
+        DhWindow *window;
 
         keyword = g_variant_get_string (parameter, NULL);
         if (keyword == NULL || keyword[0] == '\0') {
@@ -118,9 +116,9 @@ search_cb (GSimpleAction *action,
                 return;
         }
 
-        window = dh_app_peek_first_window (app);
-        dh_window_search (DH_WINDOW (window), keyword);
-        gtk_window_present (window);
+        window = dh_app_get_active_main_window (app, TRUE);
+        dh_window_search (window, keyword);
+        gtk_window_present (GTK_WINDOW (window));
 }
 
 static DhAssistant *
@@ -174,7 +172,7 @@ raise_cb (GSimpleAction *action,
 
         window = gtk_application_get_active_window (GTK_APPLICATION (app));
         if (window == NULL)
-                window = dh_app_peek_first_window (app);
+                window = (GtkWindow *) dh_app_get_active_main_window (app, TRUE);
 
         gtk_window_present (window);
 }
@@ -409,8 +407,10 @@ dh_app_new (void)
                              NULL);
 }
 
-GtkWindow *
-dh_app_peek_first_window (DhApp *app)
+/* Returns: (transfer none) (nullable). */
+DhWindow *
+dh_app_get_active_main_window (DhApp    *app,
+                               gboolean  create_if_none)
 {
         GList *windows;
         GList *l;
@@ -423,12 +423,17 @@ dh_app_peek_first_window (DhApp *app)
                 GtkWindow *cur_window = GTK_WINDOW (l->data);
 
                 if (DH_IS_WINDOW (cur_window))
-                        return cur_window;
+                        return DH_WINDOW (cur_window);
         }
 
-        /* Create a new window */
-        g_action_group_activate_action (G_ACTION_GROUP (app), "new-window", NULL);
+        if (create_if_none) {
+                g_action_group_activate_action (G_ACTION_GROUP (app), "new-window", NULL);
+
+                /* Look again, but with create_if_none = FALSE to avoid an
+                 * infinite recursion in case creating a new main window fails.
+                 */
+                return dh_app_get_active_main_window (app, FALSE);
+        }
 
-        /* And look for the newly created window again */
-        return dh_app_peek_first_window (app);
+        return NULL;
 }
diff --git a/src/dh-app.h b/src/dh-app.h
index 90d03d5..b02de44 100644
--- a/src/dh-app.h
+++ b/src/dh-app.h
@@ -21,6 +21,7 @@
 #define DH_APP_H
 
 #include <gtk/gtk.h>
+#include "dh-window.h"
 
 G_BEGIN_DECLS
 
@@ -46,7 +47,8 @@ GType           dh_app_get_type                 (void) G_GNUC_CONST;
 
 DhApp *         dh_app_new                      (void);
 
-GtkWindow *     dh_app_peek_first_window        (DhApp *app);
+DhWindow *      dh_app_get_active_main_window   (DhApp    *app,
+                                                 gboolean  create_if_none);
 
 G_END_DECLS
 
diff --git a/src/dh-assistant.c b/src/dh-assistant.c
index 5319c6e..231ef25 100644
--- a/src/dh-assistant.c
+++ b/src/dh-assistant.c
@@ -35,12 +35,12 @@ assistant_view_open_uri_cb (DhAssistantView *view,
                             DhAssistant     *assistant)
 {
         DhApp *app;
-        GtkWindow *window;
+        DhWindow *window;
 
         app = DH_APP (gtk_window_get_application (GTK_WINDOW (assistant)));
 
-        window = dh_app_peek_first_window (app);
-        _dh_window_display_uri (DH_WINDOW (window), uri);
+        window = dh_app_get_active_main_window (app, TRUE);
+        _dh_window_display_uri (window, uri);
 }
 
 static gboolean


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