[gnome-control-center] window: Save application window state



commit f89a48a980a59dce863647c9adbd780c116a859e
Author: 1pav <40791-1pav users noreply gitlab gnome org>
Date:   Sat Aug 7 12:11:51 2021 +0200

    window: Save application window state
    
    Currently the window state (width, height, maximized) is not preserved
    across application restarts.
    
    These changes implement window state saving/restoring by leveraging the
    GSettings API.
    
    Fixes https://gitlab.gnome.org/GNOME/gnome-control-center/-/issues/1293

 shell/cc-window.c                         | 80 +++++++++++++++++++++++++++++++
 shell/org.gnome.ControlCenter.gschema.xml |  5 ++
 2 files changed, 85 insertions(+)
---
diff --git a/shell/cc-window.c b/shell/cc-window.c
index de4eccc04..6e72ef7ca 100644
--- a/shell/cc-window.c
+++ b/shell/cc-window.c
@@ -82,6 +82,10 @@ struct _CcWindow
   GSettings *settings;
 
   CcPanelListView previous_list_view;
+
+  gint current_width;
+  gint current_height;
+  gboolean is_maximized;
 };
 
 static void     cc_shell_iface_init         (CcShellInterface      *iface);
@@ -97,6 +101,33 @@ enum
 };
 
 /* Auxiliary methods */
+static void
+store_window_state (CcWindow *self)
+{
+  g_settings_set (self->settings,
+                 "window-state",
+                 "(iib)",
+                 self->current_width,
+                 self->current_height,
+                 self->is_maximized);
+}
+
+static void
+load_window_state (CcWindow *self)
+{
+  g_settings_get (self->settings,
+                 "window-state",
+                 "(iib)",
+                 &self->current_width,
+                 &self->current_height,
+                 &self->is_maximized);
+
+  if (self->current_width != -1 && self->current_height != -1)
+    gtk_window_set_default_size (GTK_WINDOW (self), self->current_width, self->current_height);
+  if (self->is_maximized)
+    gtk_window_maximize (GTK_WINDOW (self));
+}
+
 static gboolean
 in_flatpak_sandbox (void)
 {
@@ -676,6 +707,39 @@ on_development_warning_dialog_responded_cb (CcWindow *self)
   gtk_widget_hide (GTK_WIDGET (self->development_warning_dialog));
 }
 
+static void
+on_window_size_allocate (GtkWidget     *widget,
+                         GtkAllocation *allocation)
+{
+  CcWindow *self = CC_WINDOW (widget);
+
+  GTK_WIDGET_CLASS (cc_window_parent_class)->size_allocate (widget, allocation);
+
+  if (!self->is_maximized)
+    gtk_window_get_size (GTK_WINDOW (widget), &self->current_width, &self->current_height);
+}
+
+static gboolean
+on_window_state_event (GtkWidget           *widget,
+                       GdkEventWindowState *event)
+{
+  CcWindow *self = CC_WINDOW (widget);
+
+  self->is_maximized = (event->new_window_state & GDK_WINDOW_STATE_MAXIMIZED) != 0;
+
+  return GTK_WIDGET_CLASS (cc_window_parent_class)->window_state_event (widget, event);
+}
+
+static void
+on_window_destroy (GtkWidget *widget)
+{
+  CcWindow *self = CC_WINDOW (widget);
+
+  store_window_state (self);
+
+  GTK_WIDGET_CLASS (cc_window_parent_class)->destroy (widget);
+}
+
 /* CcShell implementation */
 static gboolean
 cc_window_set_active_panel_from_id (CcShell      *shell,
@@ -801,6 +865,8 @@ cc_window_constructed (GObject *object)
   CcWindow *self = CC_WINDOW (object);
   g_autofree char *id = NULL;
 
+  load_window_state (self);
+
   /* Add the panels */
   setup_model (self);
 
@@ -817,6 +883,20 @@ cc_window_constructed (GObject *object)
                             G_CALLBACK (update_headerbar_buttons),
                             self);
 
+  g_signal_connect (self,
+                    "size-allocate",
+                    G_CALLBACK (on_window_size_allocate),
+                    NULL);
+
+  g_signal_connect (self,
+                    "window-state-event",
+                    G_CALLBACK (on_window_state_event),
+                    NULL);
+  g_signal_connect (self,
+                    "destroy",
+                    G_CALLBACK (on_window_destroy),
+                    NULL);
+
   update_headerbar_buttons (self);
   show_sidebar (self);
 
diff --git a/shell/org.gnome.ControlCenter.gschema.xml b/shell/org.gnome.ControlCenter.gschema.xml
index 40350bca4..ccf376b00 100644
--- a/shell/org.gnome.ControlCenter.gschema.xml
+++ b/shell/org.gnome.ControlCenter.gschema.xml
@@ -15,5 +15,10 @@
         Whether Settings should show a warning when running a development build.
       </description>
     </key>
+    <key type="(iib)" name="window-state">
+      <default>(-1, -1, false)</default>
+      <summary>Initial state of the window</summary>
+      <description>A tuple containing the initial width, height and maximized state of the application 
window.</description>
+    </key>
   </schema>
 </schemalist>


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