[gnome-calculator] Remember window position



commit bbb3f2afc64ffa45d90d73571cedd51df5c2d27b
Author: Bahodir Mansurov <bahodir mansurov org>
Date:   Wed Oct 26 20:20:59 2016 -0400

    Remember window position
    
    Save the last window position before quitting the application.
    Then restore the first window to this position the next time
    the application starts. When quitting the application (via
    the appmenu), if there are multiple open windows, the active
    window position will be saved.
    
    This will allow users to always start the application in a
    predefined position on the screen. The feature is especially
    useful when multiple application are configured to open
    at set locations so that they can be viewed without
    overlapping each other.
    
    Note that only the first window position is restored. Subsequent
    windows will not be positioned by the application.

 data/org.gnome.calculator.gschema.xml |    5 ++++
 src/gnome-calculator.vala             |   36 +++++++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+), 0 deletions(-)
---
diff --git a/data/org.gnome.calculator.gschema.xml b/data/org.gnome.calculator.gschema.xml
index 45f95ab..c626826 100644
--- a/data/org.gnome.calculator.gschema.xml
+++ b/data/org.gnome.calculator.gschema.xml
@@ -87,5 +87,10 @@
       <summary>Internal precision</summary>
       <description>The internal precision used with the MPFR library</description>
     </key>
+    <key type="(ii)" name="window-position">
+      <default>(-1, -1)</default>
+      <summary>Window position</summary>
+      <description>Window position (x and y) of the last closed window.</description>
+    </key>
   </schema>
 </schemalist>
diff --git a/src/gnome-calculator.vala b/src/gnome-calculator.vala
index 2f494f2..d0bec78 100644
--- a/src/gnome-calculator.vala
+++ b/src/gnome-calculator.vala
@@ -13,6 +13,7 @@ public class Calculator : Gtk.Application
 {
     private Settings settings;
     private MathWindow last_opened_window;
+    int n_math_windows = 0;
     private MathPreferencesDialog preferences_dialog;
     private Gtk.ShortcutsWindow shortcuts_window;
     private static string program_name = null;
@@ -77,6 +78,16 @@ public class Calculator : Gtk.Application
 
         var current_window = new MathWindow (this, equation);
         current_window.set_title (_("Calculator"));
+        // when closing the last window save its position to the settings
+        current_window.delete_event.connect((sender, event) => {
+            if (n_math_windows == 1) {
+                save_window_position ((sender as MathWindow));
+            }
+            n_math_windows -= 1;
+            return false;
+        });
+        n_math_windows += 1;
+
         var buttons = current_window.buttons;
         buttons.programming_base = number_base;
         buttons.mode = button_mode; // FIXME: We load the basic buttons even if we immediately switch to the 
next type
@@ -113,6 +124,8 @@ public class Calculator : Gtk.Application
 
         settings = new Settings ("org.gnome.calculator");
         last_opened_window = create_new_window (settings);
+        // restore the first window position from the settings
+        load_window_position (last_opened_window);
     }
 
     private MathWindow get_active_math_window ()
@@ -331,6 +344,7 @@ public class Calculator : Gtk.Application
 
     private void quit_cb ()
     {
+        save_window_position (get_active_math_window ());
         get_active_window ().destroy ();
     }
 
@@ -340,6 +354,28 @@ public class Calculator : Gtk.Application
         window.present ();
     }
 
+    /**
+     * Load `window-position` from the settings and move the window to that
+     * position
+     */
+    private void load_window_position (MathWindow window) {
+        int32 x, y;
+        settings.get("window-position", "(ii)", out x, out y);
+        // (-1, -1) is the default value
+        if (x != -1 && y != -1) {
+            window.move (x, y);
+        }
+    }
+
+    /**
+     * Save window position to the settings
+     */
+    private void save_window_position (MathWindow window) {
+        int32 x, y;
+        window.get_position (out x, out y);
+        settings.set_value("window-position", new Variant("(ii)", x, y));
+    }
+
     public static int main (string[] args)
     {
         Intl.setlocale (LocaleCategory.ALL, "");


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