[meld] windowstate: New module for managing window state in the prefered way



commit c63c56327c9a217a28d98dd9b3296f7826883e3e
Author: Kai Willadsen <kai willadsen gmail com>
Date:   Sat Oct 1 09:14:45 2016 +1000

    windowstate: New module for managing window state in the prefered way
    
    This moves some logic out of meldwindow, and changes the way we store
    window state in the gsettings schema. No migration is provided because
    this is fairly low-value configuration data.

 data/org.gnome.meld.gschema.xml |   34 ++++++++++++--------
 data/ui/meldapp.ui              |    2 -
 meld/meldwindow.py              |   28 ++---------------
 meld/windowstate.py             |   65 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 89 insertions(+), 40 deletions(-)
---
diff --git a/data/org.gnome.meld.gschema.xml b/data/org.gnome.meld.gschema.xml
index f423956..50ab450 100644
--- a/data/org.gnome.meld.gschema.xml
+++ b/data/org.gnome.meld.gschema.xml
@@ -23,21 +23,29 @@
     <value nick="trailing" value="64"/>
   </flags>
 
-  <schema id="org.gnome.meld" path="/org/gnome/meld/">
-      <!-- Application settings -->
-      <key name="window-size" type="(ii)">
-          <default>(-1, -1)</default>
-          <summary>Default window size</summary>
-      </key>
+  <schema id="org.gnome.meld.WindowState" path="/org/gnome/meld/window-state/">
+      <!-- Saved window state -->
 
-      <key name="window-state" type="s">
-          <choices>
-            <choice value="normal"/>
-            <choice value="maximized"/>
-          </choices>
-          <default>"normal"</default>
-          <summary>Default window state</summary>
+      <key name="width" type="i">
+          <default>-1</default>
+          <summary>Default window width</summary>
+      </key>
+      <key name="height" type="i">
+          <default>-1</default>
+          <summary>Default window height</summary>
+      </key>
+      <key name="is-maximized" type="b">
+          <default>false</default>
+          <summary>Default window maximised state</summary>
       </key>
+      <key name="is-fullscreen" type="b">
+          <default>false</default>
+          <summary>Default window fullscreen state</summary>
+      </key>
+  </schema>
+
+  <schema id="org.gnome.meld" path="/org/gnome/meld/">
+      <!-- Application settings -->
 
       <key name="toolbar-visible" type="b">
           <default>true</default>
diff --git a/data/ui/meldapp.ui b/data/ui/meldapp.ui
index b33c20e..f112cff 100644
--- a/data/ui/meldapp.ui
+++ b/data/ui/meldapp.ui
@@ -7,8 +7,6 @@
     <property name="title" translatable="yes">Meld</property>
     <property name="icon_name">meld</property>
     <signal name="delete-event" handler="on_delete_event" swapped="no"/>
-    <signal name="size-allocate" handler="on_size_allocate" swapped="no"/>
-    <signal name="window-state-event" handler="on_window_state_event" swapped="no"/>
     <child>
       <object class="GtkVBox" id="appvbox">
         <property name="visible">True</property>
diff --git a/meld/meldwindow.py b/meld/meldwindow.py
index 9fd86f4..4078c69 100644
--- a/meld/meldwindow.py
+++ b/meld/meldwindow.py
@@ -36,6 +36,7 @@ from .ui import notebooklabel
 from meld.conf import _
 from meld.recent import recent_comparisons
 from meld.settings import interface_settings, settings
+from meld.windowstate import SavedWindowState
 
 
 class MeldWindow(gnomeglade.Component):
@@ -230,12 +231,8 @@ class MeldWindow(gnomeglade.Component):
         self.widget.connect("drag_data_received",
                             self.on_widget_drag_data_received)
 
-        # Handle saved window size and state
-        window_size = settings.get_value('window-size')
-        self.widget.set_default_size(window_size[0], window_size[1])
-        window_state = settings.get_string('window-state')
-        if window_state == 'maximized':
-            self.widget.maximize()
+        self.window_state = SavedWindowState()
+        self.window_state.bind(self.widget)
 
         self.should_close = False
         self.idle_hooked = 0
@@ -413,25 +410,6 @@ class MeldWindow(gnomeglade.Component):
         self.actiongroup.get_action("PrevChange").set_sensitive(have_prev)
         self.actiongroup.get_action("NextChange").set_sensitive(have_next)
 
-    def on_size_allocate(self, window, allocation):
-        gdk_window = window.get_window()
-        if not gdk_window:
-            return
-        nosave = Gdk.WindowState.FULLSCREEN | Gdk.WindowState.MAXIMIZED
-        if not (gdk_window.get_state() & nosave):
-            width, height = self.widget.get_size()
-            variant = GLib.Variant('(ii)', (width, height))
-            settings.set_value('window-size', variant)
-
-    def on_window_state_event(self, window, event):
-        state = event.window.get_state()
-
-        # TODO: Handle fullscreen
-        maximised = state & Gdk.WindowState.MAXIMIZED
-        # fullscreen = state & Gdk.WindowState.FULLSCREEN
-        window_state = 'maximized' if maximised else 'normal'
-        settings.set_string('window-state', window_state)
-
     def on_menu_file_new_activate(self, menuitem):
         self.append_new_comparison()
 
diff --git a/meld/windowstate.py b/meld/windowstate.py
new file mode 100644
index 0000000..baf7d98
--- /dev/null
+++ b/meld/windowstate.py
@@ -0,0 +1,65 @@
+# Copyright (C) 2016 Kai Willadsen <kai willadsen 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 2 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/>.
+
+
+from gi.repository import Gdk, Gio, GObject
+
+from meld.settings import load_settings_schema
+
+
+WINDOW_STATE_SCHEMA = 'org.gnome.meld.WindowState'
+
+
+class SavedWindowState(GObject.GObject):
+    '''Utility class for saving and restoring GtkWindow state'''
+
+    __gtype_name__ = 'SavedWindowState'
+
+    width = GObject.property(
+        type=int, nick='Current window width', default=-1)
+    height = GObject.property(
+        type=int, nick='Current window height', default=-1)
+    is_maximized = GObject.property(
+        type=bool, nick='Is window maximized', default=False)
+    is_fullscreen = GObject.property(
+        type=bool, nick='Is window fullscreen', default=False)
+
+    def bind(self, window):
+        window.connect('size-allocate', self.on_size_allocate)
+        window.connect('window-state-event', self.on_window_state_event)
+
+        bind_flags = Gio.SettingsBindFlags.DEFAULT
+        self.settings = load_settings_schema(WINDOW_STATE_SCHEMA)
+        self.settings.bind('width', self, 'width', bind_flags)
+        self.settings.bind('height', self, 'height', bind_flags)
+        self.settings.bind('is-maximized', self, 'is-maximized', bind_flags)
+        self.settings.bind('is-fullscreen', self, 'is-fullscreen', bind_flags)
+
+        window.set_default_size(self.props.width, self.props.height)
+        if self.props.is_maximized:
+            window.maximize()
+        if self.props.is_fullscreen:
+            window.fullscreen()
+
+    def on_size_allocate(self, window, allocation):
+        if not (self.props.is_maximized or self.props.is_fullscreen):
+            width, height = window.get_size()
+            self.props.width = width
+            self.props.height = height
+
+    def on_window_state_event(self, window, event):
+        state = event.window.get_state()
+        self.props.is_maximized = state & Gdk.WindowState.MAXIMIZED
+        self.props.is_fullscreen = state & Gdk.WindowState.FULLSCREEN


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