[gnome-builder/wip/gtk4-port] libide/gui: store window settings in workspace base class



commit b1789a573b671f7246cd51d0e0a4104b28b5c015
Author: Christian Hergert <chergert redhat com>
Date:   Tue Mar 29 01:37:43 2022 -0700

    libide/gui: store window settings in workspace base class
    
    We probably want to modify this in the future to keep the window settings
    for the particular workspace-kind rather than all workspaces.

 data/gsettings/org.gnome.builder.gschema.xml |   5 -
 src/libide/gui/ide-primary-workspace.c       |   2 -
 src/libide/gui/ide-window-settings-private.h |  29 -----
 src/libide/gui/ide-window-settings.c         | 165 ---------------------------
 src/libide/gui/ide-workspace.c               |  79 ++++++++++++-
 src/libide/gui/meson.build                   |   2 -
 6 files changed, 78 insertions(+), 204 deletions(-)
---
diff --git a/data/gsettings/org.gnome.builder.gschema.xml b/data/gsettings/org.gnome.builder.gschema.xml
index 58a8453df..fcc44bc7d 100644
--- a/data/gsettings/org.gnome.builder.gschema.xml
+++ b/data/gsettings/org.gnome.builder.gschema.xml
@@ -5,11 +5,6 @@
       <summary>Window size</summary>
       <description>Window size (width and height).</description>
     </key>
-    <key name="window-position" type="(ii)">
-      <default>(-1,-1)</default>
-      <summary>Window position</summary>
-      <description>Window position (x and y).</description>
-    </key>
     <key name="window-maximized" type="b">
       <default>true</default>
       <summary>Window maximized</summary>
diff --git a/src/libide/gui/ide-primary-workspace.c b/src/libide/gui/ide-primary-workspace.c
index 024b8ba3d..38f77eef0 100644
--- a/src/libide/gui/ide-primary-workspace.c
+++ b/src/libide/gui/ide-primary-workspace.c
@@ -28,7 +28,6 @@
 #include "ide-omni-bar.h"
 #include "ide-primary-workspace-private.h"
 #include "ide-run-button.h"
-#include "ide-window-settings-private.h"
 
 /**
  * SECTION:ide-primary-workspace
@@ -105,5 +104,4 @@ ide_primary_workspace_init (IdePrimaryWorkspace *self)
   gtk_widget_init_template (GTK_WIDGET (self));
 
   _ide_primary_workspace_init_actions (self);
-  _ide_window_settings_register (GTK_WINDOW (self));
 }
diff --git a/src/libide/gui/ide-workspace.c b/src/libide/gui/ide-workspace.c
index b7199c771..a57928d9b 100644
--- a/src/libide/gui/ide-workspace.c
+++ b/src/libide/gui/ide-workspace.c
@@ -60,7 +60,8 @@ typedef struct
    */
   GQueue page_mru;
 
-  guint in_key_press : 1;
+  /* Queued source to save window size/etc */
+  guint queued_window_save;
 } IdeWorkspacePrivate;
 
 typedef struct
@@ -79,6 +80,7 @@ enum {
 G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (IdeWorkspace, ide_workspace, ADW_TYPE_APPLICATION_WINDOW)
 
 static GParamSpec *properties [N_PROPS];
+static GSettings *settings;
 
 static void
 ide_workspace_addin_added_cb (IdeExtensionSetAdapter *set,
@@ -256,6 +258,76 @@ ide_workspace_agree_to_close_finish (IdeWorkspace *self,
   return g_task_propagate_boolean (G_TASK (result), error);
 }
 
+static gboolean
+ide_workspace_save_settings (gpointer data)
+{
+  IdeWorkspace *self = data;
+  IdeWorkspacePrivate *priv = ide_workspace_get_instance_private (self);
+
+  g_assert (IDE_IS_WORKSPACE (self));
+
+  priv->queued_window_save = 0;
+
+  if (gtk_widget_get_realized (GTK_WIDGET (self)) &&
+      gtk_widget_get_visible (GTK_WIDGET (self)))
+    {
+      GdkRectangle geom = {0};
+      gboolean maximized;
+
+      if (settings == NULL)
+        settings = g_settings_new ("org.gnome.builder");
+
+      gtk_window_get_default_size (GTK_WINDOW (self), &geom.width, &geom.height);
+
+      maximized = gtk_window_is_maximized (GTK_WINDOW (self));
+
+      g_settings_set (settings, "window-size", "(ii)", geom.width, geom.height);
+      g_settings_set_boolean (settings, "window-maximized", maximized);
+    }
+
+  return G_SOURCE_REMOVE;
+}
+
+static void
+ide_workspace_size_allocate (GtkWidget *widget,
+                             int        width,
+                             int        height,
+                             int        baseline)
+{
+  IdeWorkspace *self = (IdeWorkspace *)widget;
+  IdeWorkspacePrivate *priv = ide_workspace_get_instance_private (self);
+
+  g_assert (IDE_IS_WORKSPACE (self));
+
+  GTK_WIDGET_CLASS (ide_workspace_parent_class)->size_allocate (widget, width, height, baseline);
+
+  if (priv->queued_window_save == 0)
+    priv->queued_window_save = g_timeout_add_seconds (1, ide_workspace_save_settings, self);
+}
+
+static void
+ide_workspace_realize (GtkWidget *widget)
+{
+  IdeWorkspace *self = (IdeWorkspace *)widget;
+  GdkRectangle geom = {0};
+  gboolean maximized = FALSE;
+
+  g_assert (IDE_IS_WORKSPACE (self));
+
+  if (settings == NULL)
+    settings = g_settings_new ("org.gnome.builder");
+
+  g_settings_get (settings, "window-size", "(ii)", &geom.width, &geom.height);
+  g_settings_get (settings, "window-maximized", "b", &maximized);
+
+  gtk_window_set_default_size (GTK_WINDOW (self), geom.width, geom.height);
+
+  GTK_WIDGET_CLASS (ide_workspace_parent_class)->realize (widget);
+
+  if (maximized)
+    gtk_window_maximize (GTK_WINDOW (self));
+}
+
 static void
 ide_workspace_finalize (GObject *object)
 {
@@ -264,6 +336,7 @@ ide_workspace_finalize (GObject *object)
 
   g_clear_object (&priv->context);
   g_clear_object (&priv->cancellable);
+  g_clear_handle_id (&priv->queued_window_save, g_source_remove);
 
   G_OBJECT_CLASS (ide_workspace_parent_class)->finalize (object);
 }
@@ -304,6 +377,7 @@ static void
 ide_workspace_class_init (IdeWorkspaceClass *klass)
 {
   GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
   GtkWindowClass *window_class = GTK_WINDOW_CLASS (klass);
 
   object_class->dispose = ide_workspace_dispose;
@@ -311,6 +385,9 @@ ide_workspace_class_init (IdeWorkspaceClass *klass)
   object_class->get_property = ide_workspace_get_property;
   object_class->set_property = ide_workspace_set_property;
 
+  widget_class->realize = ide_workspace_realize;
+  widget_class->size_allocate = ide_workspace_size_allocate;
+
   window_class->close_request = ide_workspace_close_request;
 
   klass->foreach_page = ide_workspace_real_foreach_page;
diff --git a/src/libide/gui/meson.build b/src/libide/gui/meson.build
index 732484aa3..d55772e02 100644
--- a/src/libide/gui/meson.build
+++ b/src/libide/gui/meson.build
@@ -51,7 +51,6 @@ libide_gui_private_headers = [
   'ide-primary-workspace-private.h',
   'ide-run-button.h',
   'ide-session-private.h',
-  'ide-window-settings-private.h',
 ]
 
 libide_gui_private_sources = [
@@ -67,7 +66,6 @@ libide_gui_private_sources = [
   'ide-primary-workspace-actions.c',
   'ide-run-button.c',
   'ide-session.c',
-  'ide-window-settings.c',
   'ide-workspace-actions.c',
 ]
 


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