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




commit 94119b2e3e40ccd86fe118c9bb07f21a728d8552
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                 |  7 ++++++
 src/kgx-application.h                 |  1 +
 src/kgx-pages.c                       |  5 ----
 src/kgx-window.c                      | 45 +++++++++++++++++++++++++++++++++++
 5 files changed, 59 insertions(+), 5 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..4aa4c47 100644
--- a/src/kgx-application.c
+++ b/src/kgx-application.c
@@ -1196,3 +1196,10 @@ kgx_application_add_terminal (KgxApplication *self,
 
   return KGX_TAB (tab);
 }
+
+
+GSettings *
+kgx_application_get_settings (KgxApplication *self)
+{
+  return self->settings;
+}
diff --git a/src/kgx-application.h b/src/kgx-application.h
index fa401cd..ed12c6e 100644
--- a/src/kgx-application.h
+++ b/src/kgx-application.h
@@ -112,5 +112,6 @@ KgxTab *              kgx_application_add_terminal    (KgxApplication *self,
                                                        GFile          *working_directory,
                                                        GStrv           command,
                                                        const char     *title);
+GSettings *           kgx_application_get_settings    (KgxApplication *self);
 
 G_END_DECLS
diff --git a/src/kgx-pages.c b/src/kgx-pages.c
index d745314..bc9021e 100644
--- a/src/kgx-pages.c
+++ b/src/kgx-pages.c
@@ -436,17 +436,12 @@ 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_pages = kgx_window_get_pages (new_window);
diff --git a/src/kgx-window.c b/src/kgx-window.c
index edb3e37..21063fc 100644
--- a/src/kgx-window.c
+++ b/src/kgx-window.c
@@ -79,18 +79,60 @@ zoomed (GObject *object, GParamSpec *pspec, gpointer data)
 }
 
 
+static void
+save_window_size (KgxWindow *self)
+{
+  GtkApplication *application;
+  GSettings *settings;
+  int width, height;
+
+  if (!gtk_window_is_active (GTK_WINDOW (self))) {
+    return;
+  }
+
+  application = gtk_window_get_application (GTK_WINDOW (self));
+  settings = kgx_application_get_settings (KGX_APPLICATION (application));
+
+  gtk_window_get_default_size (GTK_WINDOW (self), &width, &height);
+
+  g_debug ("Saving window geometry: %ix%i", width, height);
+
+  g_settings_set_int (settings, "window-width", width);
+  g_settings_set_int (settings, "window-height", height);
+}
+
+
 static void
 kgx_window_constructed (GObject *object)
 {
   KgxWindow       *self = KGX_WINDOW (object);
   GtkApplication  *application = NULL;
+  GtkWindow       *active_window;
   AdwStyleManager *style_manager;
+  int width, height;
 
   G_OBJECT_CLASS (kgx_window_parent_class)->constructed (object);
 
   application = gtk_window_get_application (GTK_WINDOW (self));
+  active_window = gtk_application_get_active_window (application);
   style_manager = adw_style_manager_get_default ();
 
+  /* If there is an active window somewhere we use its size, otherwise we use
+   * the one stored as a gsetting. */
+  if (active_window) {
+    gtk_window_get_default_size (active_window, &width, &height);
+  } else {
+    GSettings *settings;
+
+    settings = kgx_application_get_settings (KGX_APPLICATION (application));
+
+    width = g_settings_get_int (settings, "window-width");
+    height = g_settings_get_int (settings, "window-height");
+  }
+
+  g_debug ("Loading window geometry: %ix%i", width, height);
+  gtk_window_set_default_size (GTK_WINDOW (self), width, height);
+
   g_object_bind_property (application, "theme",
                           self->pages, "theme",
                           G_BINDING_SYNC_CREATE);
@@ -179,6 +221,8 @@ close_response (KgxWindow *self)
 {
   self->close_anyway = TRUE;
 
+  save_window_size (self);
+
   gtk_window_destroy (GTK_WINDOW (self));
 }
 
@@ -193,6 +237,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]