[devhelp] Window: save GSettings only when needed



commit fff07126c457a84fcdc64da6f9a386c033ba5426
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Tue Jan 23 19:11:16 2018 +0100

    Window: save GSettings only when needed
    
    https://bugzilla.gnome.org/show_bug.cgi?id=786008
    https://bugzilla.gnome.org/show_bug.cgi?id=790902

 src/dh-app.c    |   18 ++++++++++--
 src/dh-window.c |   84 +------------------------------------------------------
 2 files changed, 16 insertions(+), 86 deletions(-)
---
diff --git a/src/dh-app.c b/src/dh-app.c
index 6a35126..12e9eda 100644
--- a/src/dh-app.c
+++ b/src/dh-app.c
@@ -25,6 +25,8 @@
 #include <glib/gi18n.h>
 #include "dh-assistant.h"
 #include "dh-preferences.h"
+#include "dh-settings.h"
+#include "dh-util.h"
 
 G_DEFINE_TYPE (DhApp, dh_app, GTK_TYPE_APPLICATION);
 
@@ -34,10 +36,20 @@ new_window_cb (GSimpleAction *action,
                gpointer       user_data)
 {
         DhApp *app = DH_APP (user_data);
-        GtkWidget *window;
+        DhWindow *active_window;
+        GtkWidget *new_window;
 
-        window = dh_window_new (GTK_APPLICATION (app));
-        gtk_widget_show_all (window);
+        active_window = dh_app_get_active_main_window (app, FALSE);
+        if (active_window != NULL) {
+                DhSettings *settings;
+
+                settings = dh_settings_get_singleton ();
+                dh_util_window_settings_save (GTK_WINDOW (active_window),
+                                              dh_settings_peek_window_settings (settings));
+        }
+
+        new_window = dh_window_new (GTK_APPLICATION (app));
+        gtk_widget_show_all (new_window);
 }
 
 static void
diff --git a/src/dh-window.c b/src/dh-window.c
index 2bf3762..4e61601 100644
--- a/src/dh-window.c
+++ b/src/dh-window.c
@@ -42,7 +42,6 @@ typedef struct {
         GtkButton      *go_down_button;
 
         DhLink         *selected_search_link;
-        guint           configure_id;
 } DhWindowPrivate;
 
 #define WINDOW_SETTINGS_SAVE_TIMEOUT_MSECS 100
@@ -540,75 +539,14 @@ settings_fonts_changed_cb (DhSettings  *settings,
         }
 }
 
-/* FIXME: this is a bit complicated just for saving the window state.
- * Implement something (hopefully) simpler, by saving the GSettings only when
- * needed:
- * - when closing a DhWindow.
- * - just before creating a second/third/... DhWindow, save the GSettings of the
- *   active DhWindow.
- *
- * There is also a HowDoI:
- * https://wiki.gnome.org/HowDoI/SaveWindowState
- */
-static gboolean
-window_settings_save_cb (gpointer user_data)
-{
-        DhWindow *window = DH_WINDOW (user_data);
-        DhWindowPrivate *priv;
-        DhSettings *settings;
-
-        priv = dh_window_get_instance_private (window);
-
-        priv->configure_id = 0;
-
-        settings = dh_settings_get_singleton ();
-        dh_util_window_settings_save (GTK_WINDOW (window),
-                                      dh_settings_peek_window_settings (settings));
-
-        return G_SOURCE_REMOVE;
-}
-
-static gboolean
-dh_window_configure_event (GtkWidget         *widget,
-                           GdkEventConfigure *event)
-{
-        DhWindow *window = DH_WINDOW (widget);
-        DhWindowPrivate *priv;
-
-        priv = dh_window_get_instance_private (window);
-
-        if (priv->configure_id != 0) {
-                g_source_remove (priv->configure_id);
-                priv->configure_id = 0;
-        }
-
-        priv->configure_id = g_timeout_add (WINDOW_SETTINGS_SAVE_TIMEOUT_MSECS,
-                                            window_settings_save_cb,
-                                            window);
-
-        if (GTK_WIDGET_CLASS (dh_window_parent_class)->configure_event == NULL)
-                return GDK_EVENT_PROPAGATE;
-
-        return GTK_WIDGET_CLASS (dh_window_parent_class)->configure_event (widget, event);
-}
-
 static gboolean
 dh_window_delete_event (GtkWidget   *widget,
                         GdkEventAny *event)
 {
-        DhWindow *window = DH_WINDOW (widget);
-        DhWindowPrivate *priv;
         DhSettings *settings;
 
-        priv = dh_window_get_instance_private (window);
-
-        if (priv->configure_id != 0) {
-                g_source_remove (priv->configure_id);
-                priv->configure_id = 0;
-        }
-
         settings = dh_settings_get_singleton ();
-        dh_util_window_settings_save (GTK_WINDOW (window),
+        dh_util_window_settings_save (GTK_WINDOW (widget),
                                       dh_settings_peek_window_settings (settings));
 
         if (GTK_WIDGET_CLASS (dh_window_parent_class)->delete_event == NULL)
@@ -664,30 +602,10 @@ dh_window_init (DhWindow *window)
 }
 
 static void
-dh_window_dispose (GObject *object)
-{
-        DhWindow *window = DH_WINDOW (object);
-        DhWindowPrivate *priv;
-
-        priv = dh_window_get_instance_private (window);
-
-        if (priv->configure_id != 0) {
-                g_source_remove (priv->configure_id);
-                priv->configure_id = 0;
-        }
-
-        G_OBJECT_CLASS (dh_window_parent_class)->dispose (object);
-}
-
-static void
 dh_window_class_init (DhWindowClass *klass)
 {
-        GObjectClass *object_class = G_OBJECT_CLASS (klass);
         GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
 
-        object_class->dispose = dh_window_dispose;
-
-        widget_class->configure_event = dh_window_configure_event;
         widget_class->delete_event = dh_window_delete_event;
 
         /* Bind class to template */


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