[hitori: 2/3] Save window state(maximization, position and size)



commit b619d74f3eea39fb390d05b641f8777c954e23cc
Author: Jonathan Kang <jonathankang gnome org>
Date:   Sun Jan 6 21:39:51 2019 +0800

    Save window state(maximization, position and size)
    
    Save window state including maximization, position and size. Restore
    them when the application is launched.
    
    https://gitlab.gnome.org/GNOME/hitori/issues/4

 data/org.gnome.hitori.gschema.xml | 15 +++++++++++++++
 src/interface.c                   | 30 ++++++++++++++++++++++++++++++
 src/main.c                        | 26 ++++++++++++++++++++++++++
 3 files changed, 71 insertions(+)
---
diff --git a/data/org.gnome.hitori.gschema.xml b/data/org.gnome.hitori.gschema.xml
index 206bf87..ab14a40 100644
--- a/data/org.gnome.hitori.gschema.xml
+++ b/data/org.gnome.hitori.gschema.xml
@@ -6,5 +6,20 @@
                        <summary>Board size</summary>
                        <description>The size of the board, in cells.</description>
                </key>
+               <key name="window-maximized" type="b">
+                       <default>false</default>
+                       <summary>Window maximized state</summary>
+                       <description>Whether the window is maximized.</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-size" type="(ii)">
+                       <default>(-1, -1)</default>
+                       <summary>Window size</summary>
+                       <description>Window size (width and height).</description>
+               </key>
        </schema>
 </schemalist>
diff --git a/src/interface.c b/src/interface.c
index 492018b..f390912 100644
--- a/src/interface.c
+++ b/src/interface.c
@@ -62,6 +62,33 @@ static GActionEntry win_entries[] = {
        { "redo", redo_cb, NULL, NULL, NULL },
 };
 
+static void
+hitori_window_unmap_cb (GtkWidget *window,
+                        gpointer user_data)
+{
+       gboolean window_maximized;
+       GdkRectangle geometry;
+       HitoriApplication *hitori;
+
+       hitori = HITORI_APPLICATION (user_data);
+
+       window_maximized = gtk_window_is_maximized (GTK_WINDOW (window));
+       g_settings_set_boolean (hitori->settings,
+                               "window-maximized", window_maximized);
+
+       if (window_maximized)
+               return;
+
+       gtk_window_get_position (GTK_WINDOW (window), &geometry.x, &geometry.y);
+       gtk_window_get_size (GTK_WINDOW (window),
+                            &geometry.width, &geometry.height);
+
+       g_settings_set (hitori->settings, "window-position", "(ii)",
+                       geometry.x, geometry.y);
+       g_settings_set (hitori->settings, "window-size", "(ii)",
+                       geometry.width, geometry.height);
+}
+
 GtkWidget *
 hitori_create_interface (Hitori *hitori)
 {
@@ -81,6 +108,9 @@ hitori_create_interface (Hitori *hitori)
        hitori->drawing_area = GTK_WIDGET (gtk_builder_get_object (builder, "hitori_drawing_area"));
        hitori->timer_label = GTK_LABEL (gtk_builder_get_object (builder, "hitori_timer"));
 
+       g_signal_connect (hitori->window, "unmap",
+                         G_CALLBACK (hitori_window_unmap_cb), hitori);
+
        g_object_unref (builder);
 
        /* Set up actions */
diff --git a/src/main.c b/src/main.c
index 602cb40..4ad4b00 100644
--- a/src/main.c
+++ b/src/main.c
@@ -180,7 +180,9 @@ activate (GApplication *application)
 
        /* Create the interface. */
        if (self->window == NULL) {
+               GdkRectangle geometry;
                HitoriUndo *undo;
+               gboolean window_maximized;
                gchar *size_str;
 
                /* Setup */
@@ -207,6 +209,30 @@ activate (GApplication *application)
                hitori_create_interface (self);
                hitori_generate_board (self, self->board_size, priv->seed);
 
+               /* Restore window position and size */
+               window_maximized = g_settings_get_boolean (self->settings,
+                                                                                                             
                                                                           "window-maximized");
+               g_settings_get (self->settings,
+                                                                               "window-position", "(ii)",
+                                                                               &geometry.x, &geometry.y);
+               g_settings_get (self->settings,
+                                                                               "window-size", "(ii)",
+                                                                               &geometry.width, 
&geometry.height);
+
+               if (window_maximized) {
+                       gtk_window_maximize (GTK_WINDOW (self->window));
+               } else {
+                       if (geometry.x > -1 && geometry.y > -1) {
+                               gtk_window_move (GTK_WINDOW (self->window),
+                                                                                                geometry.x, 
geometry.y);
+                       }
+
+                       if (geometry.width >= 0 && geometry.height >= 0) {
+                               gtk_window_resize (GTK_WINDOW (self->window),
+                                                                                                        
geometry.width, geometry.height);
+                       }
+               }
+
                gtk_window_set_application (GTK_WINDOW (self->window), GTK_APPLICATION (self));
                gtk_widget_show_all (self->window);
        }


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