[console/wip/msandova/remember-window-size] window: Remember window size




commit 2660627d37d6925feaa704a2770e5ff506f133d6
Author: Maximiliano Sandoval R <msandova gnome org>
Date:   Fri Jul 29 21:46:59 2022 +0200

    window: Remember window size
    
    Fixes: https://gitlab.gnome.org/GNOME/console/-/issues/120
    
    window: Use active window size for new windows

 data/org.gnome.Console.gschema.xml.in |  6 ++++
 src/kgx-application.c                 | 67 +++++++++++++++++++++++++++++++++--
 src/kgx-application.h                 |  8 +++++
 src/kgx-pages.c                       | 10 ++----
 src/kgx-window.c                      | 21 +++++++++++
 5 files changed, 101 insertions(+), 11 deletions(-)
---
diff --git a/data/org.gnome.Console.gschema.xml.in b/data/org.gnome.Console.gschema.xml.in
index 8bf5d05..7e87e13 100644
--- a/data/org.gnome.Console.gschema.xml.in
+++ b/data/org.gnome.Console.gschema.xml.in
@@ -21,5 +21,11 @@
     <key name="scrollback-lines" type="x">
       <default>10000</default>
     </key>
+    <key name="window-width" type="i">
+      <default>-1</default>
+    </key>
+    <key name="window-height" type="i">
+      <default>-1</default>
+    </key>
   </schema>
 </schemalist>
diff --git a/src/kgx-application.c b/src/kgx-application.c
index d1b3c92..aba9005 100644
--- a/src/kgx-application.c
+++ b/src/kgx-application.c
@@ -1182,9 +1182,7 @@ kgx_application_add_terminal (KgxApplication *self,
   if (existing_window) {
     window = GTK_WINDOW (existing_window);
   } else {
-    window = g_object_new (KGX_TYPE_WINDOW,
-                           "application", self,
-                           NULL);
+    window = GTK_WINDOW (kgx_application_new_window (self));
   }
 
   pages = kgx_window_get_pages (KGX_WINDOW (window));
@@ -1196,3 +1194,66 @@ kgx_application_add_terminal (KgxApplication *self,
 
   return KGX_TAB (tab);
 }
+
+
+/**
+ * kgx_application_new_window:
+ * @self: an application
+ *
+ * Creates a new #KgxWindow.
+ *
+ * Returns: the newly created #KgxWindow
+ */
+KgxWindow *
+kgx_application_new_window (KgxApplication *self)
+{
+  GtkWindow *active_window;
+  int width, height;
+
+  g_return_val_if_fail (KGX_IS_APPLICATION (self), NULL);
+
+  active_window = gtk_application_get_active_window (GTK_APPLICATION (self));
+  if (active_window) {
+    gtk_window_get_default_size (active_window, &width, &height);
+  } else {
+    kgx_application_get_default_size (self, &width, &height);
+  }
+
+  g_debug ("Loading window geometry: %ix%i", width, height);
+
+  return g_object_new (KGX_TYPE_WINDOW,
+                       "application", self,
+                       "default-width", width,
+                       "default-height", height,
+                       NULL);
+}
+
+
+void
+kgx_application_get_default_size (KgxApplication *self,
+                                  int            *width,
+                                  int            *height)
+{
+  g_return_if_fail (KGX_IS_APPLICATION (self));
+
+  if (width) {
+    *width = g_settings_get_int (self->settings, "window-width");
+  }
+  if (height) {
+    *height = g_settings_get_int (self->settings, "window-height");
+  }
+}
+
+
+void
+kgx_application_save_window_size (KgxApplication *self,
+                                  int             width,
+                                  int             height)
+{
+  g_return_if_fail (KGX_IS_APPLICATION (self));
+
+  g_debug ("Saving window geometry: %ix%i", width, height);
+
+  g_settings_set_int (self->settings, "window-width", width);
+  g_settings_set_int (self->settings, "window-height", height);
+}
diff --git a/src/kgx-application.h b/src/kgx-application.h
index fa401cd..c4c1bef 100644
--- a/src/kgx-application.h
+++ b/src/kgx-application.h
@@ -112,5 +112,13 @@ KgxTab *              kgx_application_add_terminal    (KgxApplication *self,
                                                        GFile          *working_directory,
                                                        GStrv           command,
                                                        const char     *title);
+KgxWindow            *kgx_application_new_window      (KgxApplication *self);
+void                  kgx_application_get_default_size (KgxApplication *self,
+                                                        int            *width,
+                                                        int            *height);
+void                  kgx_application_save_window_size (KgxApplication *self,
+                                                        int             width,
+                                                        int             height);
+
 
 G_END_DECLS
diff --git a/src/kgx-pages.c b/src/kgx-pages.c
index d745314..7f85bb7 100644
--- a/src/kgx-pages.c
+++ b/src/kgx-pages.c
@@ -26,6 +26,7 @@
 
 #include <glib/gi18n.h>
 
+#include "kgx-application.h"
 #include "kgx-close-dialog.h"
 #include "kgx-config.h"
 #include "kgx-pages.h"
@@ -436,18 +437,11 @@ create_window (AdwTabView *view,
   GtkApplication *app;
   KgxPages *new_pages;
   KgxPagesPrivate *priv;
-  int width, height;
 
   window = GTK_WINDOW (gtk_widget_get_root (GTK_WIDGET (self)));
   app = gtk_window_get_application (window);
 
-  gtk_window_get_default_size (GTK_WINDOW (window), &width, &height);
-
-  new_window = g_object_new (KGX_TYPE_WINDOW,
-                             "application", app,
-                             "default-width", width,
-                             "default-height", height,
-                             NULL);
+  new_window = kgx_application_new_window (KGX_APPLICATION (app));
 
   new_pages = kgx_window_get_pages (new_window);
   priv = kgx_pages_get_instance_private (new_pages);
diff --git a/src/kgx-window.c b/src/kgx-window.c
index edb3e37..5a389c5 100644
--- a/src/kgx-window.c
+++ b/src/kgx-window.c
@@ -79,6 +79,24 @@ zoomed (GObject *object, GParamSpec *pspec, gpointer data)
 }
 
 
+static void
+save_window_size (KgxWindow *self)
+{
+  GtkApplication *application;
+  int width, height;
+
+  if (!gtk_window_is_active (GTK_WINDOW (self))) {
+    return;
+  }
+
+  application = gtk_window_get_application (GTK_WINDOW (self));
+
+  gtk_window_get_default_size (GTK_WINDOW (self), &width, &height);
+  kgx_application_save_window_size (KGX_APPLICATION (application), width,
+                                    height);
+}
+
+
 static void
 kgx_window_constructed (GObject *object)
 {
@@ -179,6 +197,8 @@ close_response (KgxWindow *self)
 {
   self->close_anyway = TRUE;
 
+  save_window_size (self);
+
   gtk_window_destroy (GTK_WINDOW (self));
 }
 
@@ -193,6 +213,7 @@ kgx_window_close_request (GtkWindow *window)
   children = kgx_pages_get_children (KGX_PAGES (self->pages));
 
   if (children->len < 1 || self->close_anyway) {
+    save_window_size (self);
     return FALSE; // Aka no, I don’t want to block closing
   }
 


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