[gnome-music/wip/mschraal/window-cleanup: 2/5] window: Add seperate class to handle placement



commit c5ed52f4d85b51efe8d7b16ea6bde6f4416bf2d0
Author: Marinus Schraal <mschraal gnome org>
Date:   Tue Sep 4 22:38:40 2018 +0200

    window: Add seperate class to handle placement
    
    Add WindowPlacement class to save and restore the main window size and
    location.

 gnomemusic/window.py          |  38 +---------------
 gnomemusic/windowplacement.py | 101 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 103 insertions(+), 36 deletions(-)
---
diff --git a/gnomemusic/window.py b/gnomemusic/window.py
index f86b0beb..b4a4f314 100644
--- a/gnomemusic/window.py
+++ b/gnomemusic/window.py
@@ -49,6 +49,7 @@ from gnomemusic.widgets.playertoolbar import PlayerToolbar
 from gnomemusic.widgets.playlistdialog import PlaylistDialog
 from gnomemusic.widgets.searchbar import Searchbar
 from gnomemusic.widgets.selectiontoolbar import SelectionToolbar
+from gnomemusic.windowplacement import WindowPlacement
 from gnomemusic.playlists import Playlists
 from gnomemusic.grilo import grilo
 
@@ -80,32 +81,17 @@ class Window(Gtk.ApplicationWindow):
         self.add_action(select_none)
 
         self.set_size_request(200, 100)
+        WindowPlacement(self, self._settings)
 
         self.prev_view = None
         self.curr_view = None
 
-        size_setting = self._settings.get_value('window-size')
-        if isinstance(size_setting[0], int) and isinstance(size_setting[1], int):
-            self.resize(size_setting[0], size_setting[1])
-
-        position_setting = self._settings.get_value('window-position')
-        if len(position_setting) == 2 \
-           and isinstance(position_setting[0], int) \
-           and isinstance(position_setting[1], int):
-            self.move(position_setting[0], position_setting[1])
-
-        if self._settings.get_value('window-maximized'):
-            self.maximize()
-
         self._setup_view()
         self.notifications_popup = NotificationsPopup()
         self._overlay.add_overlay(self.notifications_popup)
 
         MediaKeys(self.player, self)
 
-        self.window_size_update_timeout = None
-        self.connect("notify::is-maximized", self._on_maximized)
-        self.connect("configure-event", self._on_configure_event)
         grilo.connect('changes-pending', self._on_changes_pending)
 
     @log
@@ -130,26 +116,6 @@ class Window(Gtk.ApplicationWindow):
 
         grilo.songs_available(songs_available_cb)
 
-    @log
-    def _on_configure_event(self, widget, event):
-        if self.window_size_update_timeout is None:
-            self.window_size_update_timeout = GLib.timeout_add(500, self.store_window_size_and_position, 
widget)
-
-    @log
-    def store_window_size_and_position(self, widget):
-        size = widget.get_size()
-        self._settings.set_value('window-size', GLib.Variant('ai', [size[0], size[1]]))
-
-        position = widget.get_position()
-        self._settings.set_value('window-position', GLib.Variant('ai', [position[0], position[1]]))
-        GLib.source_remove(self.window_size_update_timeout)
-        self.window_size_update_timeout = None
-        return False
-
-    @log
-    def _on_maximized(self, klass, value, data=None):
-        self._settings.set_boolean('window-maximized', self.is_maximized())
-
     @log
     def _setup_view(self):
         self._box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
diff --git a/gnomemusic/windowplacement.py b/gnomemusic/windowplacement.py
new file mode 100644
index 00000000..83273a54
--- /dev/null
+++ b/gnomemusic/windowplacement.py
@@ -0,0 +1,101 @@
+# Copyright 2018 The GNOME Music developers
+#
+# GNOME Music 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.
+#
+# GNOME Music 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 GNOME Music; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+# The GNOME Music authors hereby grant permission for non-GPL compatible
+# GStreamer plugins to be used and distributed together with GStreamer
+# and GNOME Music.  This permission is above and beyond the permissions
+# granted by the GPL license by which GNOME Music is covered.  If you
+# modify this code, you may extend this exception to your version of the
+# code, but you are not obligated to do so.  If you do not wish to do so,
+# delete this exception statement from your version.
+
+from gi.repository import GLib, GObject
+
+from gnomemusic import log
+
+
+class WindowPlacement(GObject.GObject):
+    """Main window placement
+
+    Restores and saves the main window placement, success may vary
+    depending on the underlying window manager.
+    """
+
+    __gtype_name__ = 'WindowPlacement'
+
+    def __repr__(self):
+        return '<WindowPlacement>'
+
+    @log
+    def __init__(self, window, gsettings):
+        """Initialize WindowPlacement
+
+        :param Gtk.Window window: Main window
+        :param Gio.Settings gsettings: GSettings instance to use
+        """
+        super().__init__()
+
+        self._window = window
+        self._settings = gsettings
+
+        self._restore_window_state()
+
+        self._window_placement_update_timeout = None
+        self._window.connect('notify::is-maximized', self._on_maximized)
+        self._window.connect('configure-event', self._on_configure_event)
+
+    @log
+    def _restore_window_state(self):
+        size_setting = self._settings.get_value('window-size')
+        if (len(size_setting) == 2
+                and isinstance(size_setting[0], int)
+                and isinstance(size_setting[1], int)):
+            self._window.resize(size_setting[0], size_setting[1])
+
+        position_setting = self._settings.get_value('window-position')
+        if (len(position_setting) == 2
+                and isinstance(position_setting[0], int)
+                and isinstance(position_setting[1], int)):
+            self._window.move(position_setting[0], position_setting[1])
+
+        if self._settings.get_value('window-maximized'):
+            self._window.maximize()
+
+    @log
+    def _on_configure_event(self, widget, event):
+        if self._window_placement_update_timeout is None:
+            self._window_placement_update_timeout = GLib.timeout_add(
+                500, self._store_size_and_position, widget)
+
+    @log
+    def _store_size_and_position(self, widget):
+        size = widget.get_size()
+        self._settings.set_value(
+            'window-size', GLib.Variant('ai', [size[0], size[1]]))
+
+        position = widget.get_position()
+        self._settings.set_value(
+            'window-position', GLib.Variant('ai', [position[0], position[1]]))
+
+        GLib.source_remove(self._window_placement_update_timeout)
+        self._window_placement_update_timeout = None
+
+        return False
+
+    @log
+    def _on_maximized(self, klass, value, data=None):
+        self._settings.set_boolean(
+            'window-maximized', self._window.is_maximized())


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