Handle window state properly (implements #52)

Handle the window state as recommended by the Gnome HowDoI guide by
using the wrapper class WindowState with GObject properties and
bindings. Additionally use separate settings for width and height
instead of an array.
This commit is contained in:
coderkun 2018-07-10 22:45:29 +02:00
parent 45b088bfa2
commit cb585cda1e
3 changed files with 49 additions and 30 deletions

View file

@ -26,15 +26,20 @@
<summary>Connection state</summary> <summary>Connection state</summary>
<description>State of last connection</description> <description>State of last connection</description>
</key> </key>
<key type="ai" name="window-size"> <key type="i" name="width">
<default>[800, 600]</default> <default>800</default>
<summary>Window size</summary> <summary>Window width</summary>
<description>Window size (width and height).</description> <description>The window width in pixels.</description>
</key> </key>
<key type="b" name="window-maximized"> <key type="i" name="height">
<default>600</default>
<summary>Window height</summary>
<description>The window height in pixels.</description>
</key>
<key type="b" name="is-maximized">
<default>false</default> <default>false</default>
<summary>Window maximized</summary> <summary>Window maximized</summary>
<description>Window maximized state.</description> <description>Whether or not the window is in maximized state.</description>
</key> </key>
<key type="i" name="panel"> <key type="i" name="panel">
<range min="0" max="3"/> <range min="0" max="3"/>

View file

@ -277,7 +277,6 @@
</object> </object>
<object class="GtkApplicationWindow" id="appwindow"> <object class="GtkApplicationWindow" id="appwindow">
<property name="can_focus">False</property> <property name="can_focus">False</property>
<signal name="destroy" handler="on_appwindow_destroy" swapped="no"/>
<signal name="size-allocate" handler="on_appwindow_size_allocate" swapped="no"/> <signal name="size-allocate" handler="on_appwindow_size_allocate" swapped="no"/>
<signal name="window-state-event" handler="on_appwindow_window_state_event" swapped="no"/> <signal name="window-state-event" handler="on_appwindow_window_state_event" swapped="no"/>
<child type="titlebar"> <child type="titlebar">

View file

@ -74,13 +74,31 @@ class InfoDialog():
class WindowState(GObject.Object):
WIDTH = 'width'
HEIGHT = 'height'
IS_MAXIMIZED = 'is_maximized'
IS_FULLSCREENED = 'is_fullscreened'
width = GObject.Property(type=int, default=800)
height = GObject.Property(type=int, default=600)
is_maximized = GObject.Property(type=bool, default=False)
is_fullscreened = GObject.Property(type=bool, default=False)
def __init__(self):
GObject.Object.__init__(self)
class Window(): class Window():
SETTING_HOST = 'host' SETTING_HOST = 'host'
SETTING_PORT = 'port' SETTING_PORT = 'port'
SETTING_CONNECTED = 'connected' SETTING_CONNECTED = 'connected'
SETTING_IMAGE_DIR = 'image-dir' SETTING_IMAGE_DIR = 'image-dir'
SETTING_WINDOW_SIZE = 'window-size' SETTING_WINDOW_WIDTH = 'width'
SETTING_WINDOW_MAXIMIZED = 'window-maximized' SETTING_WINDOW_HEIGHT = 'height'
SETTING_WINDOW_MAXIMIZED = 'is-maximized'
SETTING_PANEL = 'panel' SETTING_PANEL = 'panel'
SETTING_ITEM_SIZE = 'item-size' SETTING_ITEM_SIZE = 'item-size'
SETTING_SORT_ORDER = 'sort-order' SETTING_SORT_ORDER = 'sort-order'
@ -101,9 +119,7 @@ class Window():
self._panels = [] self._panels = []
self._mcg = client.Client() self._mcg = client.Client()
self._logger = logging.getLogger(__name__) self._logger = logging.getLogger(__name__)
self._size = self._settings.get_value(Window.SETTING_WINDOW_SIZE) self._state = WindowState()
self._maximized = self._settings.get_boolean(Window.SETTING_WINDOW_MAXIMIZED)
self._fullscreened = False
# Login screen # Login screen
self._connection_panel = ConnectionPanel(builder) self._connection_panel = ConnectionPanel(builder)
@ -166,10 +182,13 @@ class Window():
self._settings.connect('changed::'+Window.SETTING_ITEM_SIZE, self.on_settings_item_size_changed) self._settings.connect('changed::'+Window.SETTING_ITEM_SIZE, self.on_settings_item_size_changed)
self._settings.connect('changed::'+Window.SETTING_SORT_ORDER, self.on_settings_sort_order_changed) self._settings.connect('changed::'+Window.SETTING_SORT_ORDER, self.on_settings_sort_order_changed)
self._settings.connect('changed::'+Window.SETTING_SORT_TYPE, self.on_settings_sort_type_changed) self._settings.connect('changed::'+Window.SETTING_SORT_TYPE, self.on_settings_sort_type_changed)
self._settings.bind(Window.SETTING_WINDOW_WIDTH, self._state, WindowState.WIDTH, Gio.SettingsBindFlags.DEFAULT)
self._settings.bind(Window.SETTING_WINDOW_HEIGHT, self._state, WindowState.HEIGHT, Gio.SettingsBindFlags.DEFAULT)
self._settings.bind(Window.SETTING_WINDOW_MAXIMIZED, self._state, WindowState.IS_MAXIMIZED, Gio.SettingsBindFlags.DEFAULT)
handlers = { handlers = {
'on_appwindow_size_allocate': self.on_resize, 'on_appwindow_size_allocate': self.on_resize,
'on_appwindow_window_state_event': self.on_state, 'on_appwindow_window_state_event': self.on_state
'on_appwindow_destroy': self.on_destroy
} }
handlers.update(self._header_bar.get_signal_handlers()) handlers.update(self._header_bar.get_signal_handlers())
handlers.update(self._infobar.get_signal_handlers()) handlers.update(self._infobar.get_signal_handlers())
@ -180,8 +199,8 @@ class Window():
builder.connect_signals(handlers) builder.connect_signals(handlers)
# Actions # Actions
self._appwindow.resize(int(self._size[0]), int(self._size[1])) self._appwindow.set_default_size(self._state.width, self._state.height)
if self._maximized: if self._state.get_property(WindowState.IS_MAXIMIZED):
self._appwindow.maximize() self._appwindow.maximize()
self._appwindow.show_all() self._appwindow.show_all()
self._content_stack.set_visible_child(self._connection_panel.get()) self._content_stack.set_visible_child(self._connection_panel.get())
@ -213,22 +232,18 @@ class Window():
def present(self): def present(self):
self._appwindow.present() self._appwindow.present()
self._appwindow.resize(800, 600)
def on_resize(self, widget, event): def on_resize(self, widget, event):
if not self._maximized: if not self._state.get_property(WindowState.IS_MAXIMIZED):
self._size = (self._appwindow.get_allocation().width, self._appwindow.get_allocation().height) size = self._appwindow.get_size()
self._state.set_property(WindowState.WIDTH, size.width)
self._state.set_property(WindowState.HEIGHT, size.height)
def on_state(self, widget, state): def on_state(self, widget, state):
self._maximized = (state.new_window_state & Gdk.WindowState.MAXIMIZED > 0) self._state.set_property(WindowState.IS_MAXIMIZED, (state.new_window_state & Gdk.WindowState.MAXIMIZED > 0))
self._fullscreen((state.new_window_state & Gdk.WindowState.FULLSCREEN > 0)) self._fullscreen((state.new_window_state & Gdk.WindowState.FULLSCREEN > 0))
self._settings.set_boolean(Window.SETTING_WINDOW_MAXIMIZED, self._maximized)
def on_destroy(self, window):
self._settings.set_value(Window.SETTING_WINDOW_SIZE, GLib.Variant('ai', list(self._size)))
def on_menu_connect(self, action, value): def on_menu_connect(self, action, value):
@ -304,7 +319,7 @@ class Window():
def on_cover_panel_toggle_fullscreen(self, widget): def on_cover_panel_toggle_fullscreen(self, widget):
if not self._fullscreened: if not self._state.get_property(WindowState.IS_FULLSCREENED):
self._appwindow.fullscreen() self._appwindow.fullscreen()
else: else:
self._appwindow.unfullscreen() self._appwindow.unfullscreen()
@ -364,7 +379,7 @@ class Window():
def on_mcg_status(self, state, album, pos, time, volume, file, audio, bitrate, error): def on_mcg_status(self, state, album, pos, time, volume, file, audio, bitrate, error):
# Album # Album
GObject.idle_add(self._panels[Window._PANEL_INDEX_COVER].set_album, album) GObject.idle_add(self._panels[Window._PANEL_INDEX_COVER].set_album, album)
if not album and self._fullscreened: if not album and self._state.get_property(WindowState.IS_FULLSCREENED):
self._fullscreen(False) self._fullscreen(False)
# State # State
if state == 'play': if state == 'play':
@ -464,9 +479,9 @@ class Window():
def _fullscreen(self, fullscreened_new): def _fullscreen(self, fullscreened_new):
if fullscreened_new != self._fullscreened: if fullscreened_new != self._state.get_property(WindowState.IS_FULLSCREENED):
self._fullscreened = fullscreened_new self._state.set_property(WindowState.IS_FULLSCREENED, fullscreened_new)
if self._fullscreened: if self._state.get_property(WindowState.IS_FULLSCREENED):
self._header_bar.get().hide() self._header_bar.get().hide()
self._panels[Window._PANEL_INDEX_COVER].set_fullscreen(True) self._panels[Window._PANEL_INDEX_COVER].set_fullscreen(True)
else: else: