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




commit c41383165644eefbdc85b00190440c4c439b3d70
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 09ecee1..42e9cf9 100644
--- a/src/kgx-application.c
+++ b/src/kgx-application.c
@@ -952,9 +952,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));
@@ -966,3 +964,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_window_size (self, &width, &height);
+  }
+
+  g_debug ("Loading window geometry: %i×%i", width, height);
+
+  return g_object_new (KGX_TYPE_WINDOW,
+                       "application", self,
+                       "default-width", width,
+                       "default-height", height,
+                       NULL);
+}
+
+
+void
+kgx_application_get_window_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_set_window_size (KgxApplication *self,
+                                 int             width,
+                                 int             height)
+{
+  g_return_if_fail (KGX_IS_APPLICATION (self));
+
+  g_debug ("Saving window geometry: %i×%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 4aa879b..84a4736 100644
--- a/src/kgx-application.h
+++ b/src/kgx-application.h
@@ -84,5 +84,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_window_size (KgxApplication *self,
+                                                       int            *width,
+                                                       int            *height);
+void                  kgx_application_set_window_size (KgxApplication *self,
+                                                       int             width,
+                                                       int             height);
+
 
 G_END_DECLS
diff --git a/src/kgx-pages.c b/src/kgx-pages.c
index 5dc2df1..5ec9665 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"
@@ -428,18 +429,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 05bf557..0171838 100644
--- a/src/kgx-window.c
+++ b/src/kgx-window.c
@@ -80,6 +80,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_set_window_size (KGX_APPLICATION (application), width,
+                                   height);
+}
+
+
 static void
 kgx_window_constructed (GObject *object)
 {
@@ -180,6 +198,8 @@ close_response (KgxWindow *self)
 {
   self->close_anyway = TRUE;
 
+  save_window_size (self);
+
   gtk_window_destroy (GTK_WINDOW (self));
 }
 
@@ -194,6 +214,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]