[gnome-robots] extract helper to store window size



commit 6a257d63abad7004487142ecb05c1deba013e5d6
Author: Andrey Kutejko <andy128k gmail com>
Date:   Mon Sep 28 01:36:03 2020 +0200

    extract helper to store window size

 src/meson.build      |  1 +
 src/robots.vala      | 36 ++---------------------
 src/window-size.vala | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 84 insertions(+), 34 deletions(-)
---
diff --git a/src/meson.build b/src/meson.build
index 3e3c459..771de4c 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -27,6 +27,7 @@ sources = files(
     'game.vala',
     'game-area.vala',
     'bubble.vala',
+    'window-size.vala',
     'robots.vala',
 )
 
diff --git a/src/robots.vala b/src/robots.vala
index b1e5f20..87d9a33 100644
--- a/src/robots.vala
+++ b/src/robots.vala
@@ -21,12 +21,7 @@ using Gtk;
 using Cairo;
 using Games;
 
-int window_width = 0;
-int window_height = 0;
-bool window_is_maximized = false;
-
 Games.Scores.Context highscores;
-GLib.Settings settings;
 uint control_keys[12];
 
 public class RobotsWindow : ApplicationWindow {
@@ -46,6 +41,7 @@ public class RobotsWindow : ApplicationWindow {
                          SoundPlayer sound_player
     ) throws Error {
         Object (application: app);
+        remember_window_size (this, new WindowSizeSettings ("org.gnome.Robots"));
 
         headerbar = new HeaderBar ();
         headerbar.set_title (_("Robots"));
@@ -60,13 +56,6 @@ public class RobotsWindow : ApplicationWindow {
         menu_button.show ();
         headerbar.pack_end (menu_button);
 
-        configure_event.connect (window_configure_event_cb);
-        window_state_event.connect (window_state_event_cb);
-        set_default_size (settings.get_int ("window-width"), settings.get_int ("window-height"));
-        if (settings.get_boolean ("window-is-maximized")) {
-            maximize ();
-        }
-
         GLib.ActionEntry[] win_entries = {
             { "random-teleport",  random_teleport_cb },
             { "safe-teleport",    safe_teleport_cb   },
@@ -221,18 +210,6 @@ public class RobotsWindow : ApplicationWindow {
         return false;
     }
 
-    private bool window_configure_event_cb () {
-        if (!window_is_maximized)
-            get_size (out window_width, out window_height);
-        return false;
-    }
-
-    private bool window_state_event_cb (Gdk.EventWindowState event) {
-        if ((event.changed_mask & Gdk.WindowState.MAXIMIZED) != 0)
-            window_is_maximized = (event.new_window_state & Gdk.WindowState.MAXIMIZED) != 0;
-        return false;
-    }
-
     public void start_new_game () {
         game_area.start_new_game ();
     }
@@ -310,7 +287,7 @@ class RobotsApplication : Gtk.Application {
 
         Environment.set_application_name (_("Robots"));
 
-        settings = new GLib.Settings ("org.gnome.Robots");
+        var settings = new GLib.Settings ("org.gnome.Robots");
         properties = new Properties (settings);
 
         Window.set_default_icon_name ("org.gnome.Robots");
@@ -355,13 +332,6 @@ class RobotsApplication : Gtk.Application {
         }
     }
 
-    protected override void shutdown () {
-        base.shutdown ();
-        settings.set_int ("window-width", window_width);
-        settings.set_int ("window-height", window_height);
-        settings.set_boolean ("window-is-maximized", window_is_maximized);
-    }
-
     protected override void activate () {
         var window = get_active_window () as RobotsWindow;
         if (window != null) {
@@ -395,8 +365,6 @@ class RobotsApplication : Gtk.Application {
                                                                            "org.gnome.Robots");
 
         window.show_all ();
-
-        GLib.Settings.sync ();
     }
 
     private void new_game_cb () {
diff --git a/src/window-size.vala b/src/window-size.vala
new file mode 100644
index 0000000..8e259c6
--- /dev/null
+++ b/src/window-size.vala
@@ -0,0 +1,81 @@
+/*
+ * Copyright 2020 Andrey Kutejko <andy128k gmail com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * For more details see the file COPYING.
+ */
+
+using Gtk;
+
+public interface WindowSize : Object {
+    public abstract int width { get; set; }
+    public abstract int height { get; set; }
+    public abstract bool is_maximized { get; set; }
+}
+
+public class WindowSizeSettings : Object, WindowSize {
+    public override int width {
+        get { return settings.get_int ("window-width"); }
+        set { settings.set_int ("window-width", value); }
+    }
+
+    public override int height {
+        get { return settings.get_int ("window-height"); }
+        set { settings.set_int ("window-height", value); }
+    }
+
+    public override bool is_maximized {
+        get { return settings.get_boolean ("window-is-maximized"); }
+        set { settings.set_boolean ("window-is-maximized", value); }
+    }
+
+    private GLib.Settings settings;
+
+    public WindowSizeSettings (string schema_id) {
+        this.settings = new GLib.Settings (schema_id);
+    }
+}
+
+private bool window_configure_event_cb (Gtk.Window window,
+                                        WindowSize size
+) {
+    if (!size.is_maximized) {
+        int width, height;
+        window.get_size (out width, out height);
+        size.width = width;
+        size.height = height;
+    }
+    return false;
+}
+
+private bool window_state_event_cb (Gtk.Window window,
+                                    WindowSize size,
+                                    Gdk.EventWindowState event
+) {
+    if ((event.changed_mask & Gdk.WindowState.MAXIMIZED) != 0) {
+        size.is_maximized = (event.new_window_state & Gdk.WindowState.MAXIMIZED) != 0;
+    }
+    return false;
+}
+
+public void remember_window_size (Gtk.Window window, WindowSize size) {
+    window.configure_event.connect (() => window_configure_event_cb (window, size));
+    window.window_state_event.connect (event => window_state_event_cb (window, size, event));
+    window.set_default_size (size.width, size.height);
+    if (size.is_maximized) {
+        window.maximize ();
+    }
+}
+


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