[polari] mainWindow: Save geometry



commit 60f0cc18c8156f1b2caf0b6e691b360871be3ffe
Author: Florian Müllner <fmuellner gnome org>
Date:   Thu Aug 8 18:52:50 2013 +0200

    mainWindow: Save geometry
    
    We should respect when users change the window's size or position,
    so save the geometry and restore it on startup.

 data/org.gnome.polari.gschema.xml.in |   15 ++++++
 data/resources/main-window.ui        |    2 -
 src/mainWindow.js                    |   82 +++++++++++++++++++++++++++++++++-
 3 files changed, 96 insertions(+), 3 deletions(-)
---
diff --git a/data/org.gnome.polari.gschema.xml.in b/data/org.gnome.polari.gschema.xml.in
index 0cecb5d..7952296 100644
--- a/data/org.gnome.polari.gschema.xml.in
+++ b/data/org.gnome.polari.gschema.xml.in
@@ -6,5 +6,20 @@
       <summary>Saved channel list</summary>
       <description>List of channels to restore on startup</description>
     </key>
+    <key type="ai" name="window-size">
+      <default>[800,500]</default>
+      <summary>Window size</summary>
+      <description>Window size (width and height).</description>
+    </key>
+    <key type="ai" name="window-position">
+      <default>[]</default>
+      <summary>Window position</summary>
+      <description>Window position (x and y).</description>
+    </key>
+    <key type="b" name="window-maximized">
+      <default>false</default>
+      <summary>Window maximized</summary>
+      <description>Window maximized state</description>
+    </key>
   </schema>
 </schemalist>
diff --git a/data/resources/main-window.ui b/data/resources/main-window.ui
index 5ff61db..27d003f 100644
--- a/data/resources/main-window.ui
+++ b/data/resources/main-window.ui
@@ -16,8 +16,6 @@
   <object class="GtkApplicationWindow" id="main_window">
     <property name="can_focus">False</property>
     <property name="title" translatable="yes">Polari</property>
-    <property name="default_width">800</property>
-    <property name="default_height">500</property>
     <property name="icon_name">polari</property>
     <style>
       <class name="polari-main-window"/>
diff --git a/src/mainWindow.js b/src/mainWindow.js
index 7dc44a4..edbb20a 100644
--- a/src/mainWindow.js
+++ b/src/mainWindow.js
@@ -15,7 +15,8 @@ const Mainloop = imports.mainloop;
 const RoomList = imports.roomList;
 const UserList = imports.userList;
 
-const MAX_NICK_UPDATE_TIME = 5;
+const MAX_NICK_UPDATE_TIME = 5; /* s */
+const CONFIGURE_TIMEOUT = 100; /* ms */
 
 
 const MainWindow = new Lang.Class({
@@ -52,10 +53,12 @@ const MainWindow = new Lang.Class({
         this._rooms = {};
 
         this._room = null;
+        this._settings = new Gio.Settings({ schema: 'org.gnome.polari' });
 
         this._displayNameChangedId = 0;
         this._topicChangedId = 0;
         this._nicknameChangedId = 0;
+        this._configureId = 0;
 
         this._titlebarRight = builder.get_object('titlebar_right');
         this._titlebarLeft = builder.get_object('titlebar_left');
@@ -117,12 +120,89 @@ const MainWindow = new Lang.Class({
                     this._selectionModeAction.change_state(GLib.Variant.new('b', false));
                 }
             }));
+        this.window.connect('window-state-event',
+                            Lang.bind(this, this._onWindowStateEvent));
+        this.window.connect('configure-event',
+                            Lang.bind(this, this._onConfigureEvent));
+        this.window.connect('delete-event',
+                            Lang.bind(this, this._onDelete));
+
+        let size = this._settings.get_value('window-size');
+        if (size.n_children() == 2) {
+            let width = size.get_child_value(0);
+            let height = size.get_child_value(1);
+            this.window.set_default_size(width.get_int32(), height.get_int32());
+        }
+
+        let position = this._settings.get_value('window-position');
+        if (size.n_children() == 2) {
+            let x = position.get_child_value(0);
+            let y = position.get_child_value(1);
+            this.window.move(x.get_int32(), y.get_int32());
+        }
+
+        if (this._settings.get_boolean('window-maximized'))
+            this.window.maximize();
 
         this._updateSensitivity();
 
         this.window.show_all();
     },
 
+    _onWindowStateEvent: function(widget, event) {
+        let window = widget.get_window();
+        let state = window.get_state();
+
+        if (state & Gdk.WindowState.FULLSCREEN)
+            return;
+
+        let maximized = (state & Gdk.WindowState.MAXIMIZED);
+        this._settings.set_boolean('window-maximized', maximized);
+    },
+
+    _saveGeometry: function() {
+        let window = this.window.get_window();
+        let state = window.get_state();
+
+        if (state & Gdk.WindowState.MAXIMIZED)
+            return;
+
+        let size = this.window.get_size();
+        this._settings.set_value('window-size', GLib.Variant.new('ai', size));
+
+        let position = this.window.get_position();
+        this._settings.set_value('window-position',
+                                 GLib.Variant.new('ai', position));
+    },
+
+    _onConfigureEvent: function(widget, event) {
+        let window = widget.get_window();
+        let state = window.get_state();
+
+        if (state & Gdk.WindowState.FULLSCREEN)
+            return;
+
+        if (this._configureId != 0) {
+            Mainloop.source_remove(this._configureId);
+            this._configureId = 0;
+        }
+
+        this._configureId = Mainloop.timeout_add(CONFIGURE_TIMEOUT,
+            Lang.bind(this, function() {
+                this._saveGeometry();
+                return false;
+            }));
+    },
+
+    _onDelete: function(widget, event) {
+        if (this._configureId != 0) {
+            Mainloop.source_remove(this._configureId);
+            this._configureId = 0;
+        }
+
+        this._saveGeometry();
+    },
+
     _onSelectionModeChanged: function() {
         let enabled = this._selectionModeAction.state.get_boolean();
         this._selectionRevealer.reveal_child = enabled;


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