Port UI to GTK 4 (close #85)
This commit is contained in:
parent
6ba8bc550f
commit
75b99e5820
36 changed files with 1730 additions and 3285 deletions
205
src/window.py
205
src/window.py
|
|
@ -2,16 +2,18 @@
|
|||
|
||||
|
||||
import gi
|
||||
gi.require_version('Gtk', '3.0')
|
||||
gi.require_version('Gtk', '4.0')
|
||||
gi.require_version('Adw', '1')
|
||||
try:
|
||||
import keyring
|
||||
use_keyring = True
|
||||
except:
|
||||
use_keyring = False
|
||||
use_keyring = False
|
||||
import locale
|
||||
import logging
|
||||
|
||||
from gi.repository import Gtk, Gdk, GObject, GLib, Gio
|
||||
from gi.repository import Gtk, Adw, Gdk, GObject, GLib, Gio
|
||||
|
||||
from . import client
|
||||
from .shortcutsdialog import ShortcutsDialog
|
||||
|
|
@ -43,7 +45,7 @@ class WindowState(GObject.Object):
|
|||
|
||||
|
||||
@Gtk.Template(resource_path='/xyz/suruatoel/mcg/ui/window.ui')
|
||||
class Window(Gtk.ApplicationWindow):
|
||||
class Window(Adw.ApplicationWindow):
|
||||
__gtype_name__ = 'McgAppWindow'
|
||||
SETTING_HOST = 'host'
|
||||
SETTING_PORT = 'port'
|
||||
|
|
@ -55,36 +57,33 @@ class Window(Gtk.ApplicationWindow):
|
|||
SETTING_ITEM_SIZE = 'item-size'
|
||||
SETTING_SORT_ORDER = 'sort-order'
|
||||
SETTING_SORT_TYPE = 'sort-type'
|
||||
_CUSTOM_STARTUP_COMPLETE = 'startup-complete'
|
||||
|
||||
# Widgets
|
||||
toolbar_view = Gtk.Template.Child()
|
||||
content_stack = Gtk.Template.Child()
|
||||
panel_stack = Gtk.Template.Child()
|
||||
toolbar_stack = Gtk.Template.Child()
|
||||
# Headerbar
|
||||
headerbar = Gtk.Template.Child()
|
||||
headerbar_title_stack = Gtk.Template.Child()
|
||||
headerbar_panel_switcher = Gtk.Template.Child()
|
||||
headerbar_connection_label = Gtk.Template.Child()
|
||||
headerbar_button_connect = Gtk.Template.Child()
|
||||
headerbar_button_playpause = Gtk.Template.Child()
|
||||
headerbar_button_volume = Gtk.Template.Child()
|
||||
# Infobar
|
||||
info_revealer = Gtk.Template.Child()
|
||||
info_bar = Gtk.Template.Child()
|
||||
info_label = Gtk.Template.Child()
|
||||
info_toast = Gtk.Template.Child()
|
||||
|
||||
|
||||
def __init__(self, app, title, settings):
|
||||
super().__init__()
|
||||
def __init__(self, app, title, settings, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
self.set_application(app)
|
||||
self.set_title(title)
|
||||
self._settings = settings
|
||||
self._panels = []
|
||||
self._mcg = client.Client()
|
||||
self._state = WindowState()
|
||||
self._changing_volume = False
|
||||
self._setting_volume = False
|
||||
self._headerbar_connection_button_active = True
|
||||
self._headerbar_playpause_button_active = True
|
||||
|
||||
# Help/Shortcuts dialog
|
||||
self.set_help_overlay(ShortcutsDialog())
|
||||
|
|
@ -108,19 +107,16 @@ class Window(Gtk.ApplicationWindow):
|
|||
self._library_panel.connect('close-standalone', self.on_panel_close_standalone)
|
||||
self._panels.append(self._library_panel)
|
||||
# Stack
|
||||
self.content_stack.add(self._connection_panel)
|
||||
self.panel_stack.add_titled(self._server_panel, 'server-panel', locale.gettext("Server"))
|
||||
self.panel_stack.add_titled(self._cover_panel, 'cover-panel', locale.gettext("Cover"))
|
||||
self.panel_stack.add_titled(self._playlist_panel, 'playlist-panel', locale.gettext("Playlist"))
|
||||
self.panel_stack.add_titled(self._library_panel, 'library-panel', locale.gettext("Library"))
|
||||
# Header
|
||||
self._playlist_panel.get_headerbar_standalone().connect('close', self.on_panel_close_standalone)
|
||||
self._library_panel.get_headerbar_standalone().connect('close', self.on_panel_close_standalone)
|
||||
self.content_stack.add_child(self._connection_panel)
|
||||
self.panel_stack.add_titled_with_icon(self._server_panel, 'server-panel', locale.gettext("Server"), "network-wired-symbolic")
|
||||
self.panel_stack.add_titled_with_icon(self._cover_panel, 'cover-panel', locale.gettext("Cover"), "image-x-generic-symbolic")
|
||||
self.panel_stack.add_titled_with_icon(self._playlist_panel, 'playlist-panel', locale.gettext("Playlist"), "view-list-symbolic")
|
||||
self.panel_stack.add_titled_with_icon(self._library_panel, 'library-panel', locale.gettext("Library"), "emblem-music-symbolic")
|
||||
# Toolbar stack
|
||||
self.toolbar_stack.add(self._server_panel.get_toolbar())
|
||||
self.toolbar_stack.add(self._cover_panel.get_toolbar())
|
||||
self.toolbar_stack.add(self._playlist_panel.get_toolbar())
|
||||
self.toolbar_stack.add(self._library_panel.get_toolbar())
|
||||
self.toolbar_stack.add_child(self._server_panel.get_toolbar())
|
||||
self.toolbar_stack.add_child(self._cover_panel.get_toolbar())
|
||||
self.toolbar_stack.add_child(self._playlist_panel.get_toolbar())
|
||||
self.toolbar_stack.add_child(self._library_panel.get_toolbar())
|
||||
|
||||
# Properties
|
||||
self._set_headerbar_sensitive(False, False)
|
||||
|
|
@ -134,6 +130,10 @@ class Window(Gtk.ApplicationWindow):
|
|||
self._library_panel.set_sort_type(self._settings.get_boolean(Window.SETTING_SORT_TYPE))
|
||||
|
||||
# Signals
|
||||
self.connect("notify::default-width", self.on_resize)
|
||||
self.connect("notify::default-height", self.on_resize)
|
||||
self.connect("notify::maximized", self.on_maximized)
|
||||
self.connect("notify::fullscreened", self.on_fullscreened)
|
||||
self._connection_panel.connect('connection-changed', self.on_connection_panel_connection_changed)
|
||||
self.panel_stack.connect('notify::visible-child', self.on_stack_switched)
|
||||
self._server_panel.connect('change-output-device', self.on_server_panel_output_device_changed)
|
||||
|
|
@ -162,7 +162,6 @@ class Window(Gtk.ApplicationWindow):
|
|||
self._mcg.connect_signal(client.Client.SIGNAL_INIT_ALBUMS, self.on_mcg_init_albums)
|
||||
self._mcg.connect_signal(client.Client.SIGNAL_LOAD_ALBUMS, self.on_mcg_load_albums)
|
||||
self._mcg.connect_signal(client.Client.SIGNAL_LOAD_ALBUMART, self.on_mcg_load_albumart)
|
||||
self._mcg.connect_signal(client.Client.SIGNAL_CUSTOM, self.on_mcg_custom)
|
||||
self._mcg.connect_signal(client.Client.SIGNAL_ERROR, self.on_mcg_error)
|
||||
self._settings.connect('changed::'+Window.SETTING_PANEL, self.on_settings_panel_changed)
|
||||
self._settings.connect('changed::'+Window.SETTING_ITEM_SIZE, self.on_settings_item_size_changed)
|
||||
|
|
@ -176,7 +175,6 @@ class Window(Gtk.ApplicationWindow):
|
|||
self.set_default_size(self._state.width, self._state.height)
|
||||
if self._state.get_property(WindowState.IS_MAXIMIZED):
|
||||
self.maximize()
|
||||
self.show_all()
|
||||
self.content_stack.set_visible_child(self._connection_panel)
|
||||
if self._settings.get_boolean(Window.SETTING_CONNECTED):
|
||||
self._connect()
|
||||
|
|
@ -236,52 +234,38 @@ class Window(Gtk.ApplicationWindow):
|
|||
|
||||
|
||||
def on_menu_search_library(self, action, value):
|
||||
self.panel_stack.set_visible_child(self._library_panel)
|
||||
self.panel_stack.set_visible_child(self.library_panel_page)
|
||||
self._library_panel.show_search()
|
||||
|
||||
|
||||
# Window callbacks
|
||||
|
||||
@Gtk.Template.Callback()
|
||||
def on_resize(self, widget, event):
|
||||
if not self._state.get_property(WindowState.IS_MAXIMIZED):
|
||||
size = self.get_size()
|
||||
self._state.set_property(WindowState.WIDTH, size.width)
|
||||
self._state.set_property(WindowState.HEIGHT, size.height)
|
||||
width = self.get_size(Gtk.Orientation.HORIZONTAL)
|
||||
height = self.get_size(Gtk.Orientation.VERTICAL)
|
||||
if width > 0:
|
||||
self._cover_panel.set_width(width)
|
||||
if not self._state.get_property(WindowState.IS_MAXIMIZED):
|
||||
self._state.set_property(WindowState.WIDTH, width)
|
||||
self._state.set_property(WindowState.HEIGHT, height)
|
||||
GObject.idle_add(self._playlist_panel.set_size, width, height)
|
||||
GObject.idle_add(self._library_panel.set_size, width, height)
|
||||
|
||||
|
||||
@Gtk.Template.Callback()
|
||||
def on_state(self, widget, state):
|
||||
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))
|
||||
def on_maximized(self, widget, maximized):
|
||||
self._state.set_property(WindowState.IS_MAXIMIZED, maximized is True)
|
||||
|
||||
|
||||
def on_fullscreened(self, widget, fullscreened):
|
||||
self._fullscreen(self.is_fullscreen())
|
||||
|
||||
|
||||
# HeaderBar callbacks
|
||||
|
||||
@Gtk.Template.Callback()
|
||||
def on_headerbar_connection_active_notify(self, widget, status):
|
||||
self._connect()
|
||||
|
||||
|
||||
@Gtk.Template.Callback()
|
||||
def on_headerbar_connection_state_set(self, widget, state):
|
||||
return True
|
||||
|
||||
|
||||
@Gtk.Template.Callback()
|
||||
def on_headerbar_volume_press(self, widget, active):
|
||||
self._changing_volume = active
|
||||
|
||||
|
||||
@Gtk.Template.Callback()
|
||||
def on_headerbar_volume_release(self, widget, active):
|
||||
self._changing_volume = active
|
||||
|
||||
|
||||
@Gtk.Template.Callback()
|
||||
def on_headerbar_playpause_toggled(self, widget):
|
||||
self._mcg.playpause()
|
||||
self._mcg.get_status()
|
||||
if self._headerbar_connection_button_active:
|
||||
self._connect()
|
||||
|
||||
|
||||
@Gtk.Template.Callback()
|
||||
|
|
@ -290,17 +274,11 @@ class Window(Gtk.ApplicationWindow):
|
|||
self._mcg.set_volume(int(value*100))
|
||||
|
||||
|
||||
# Infobar callback
|
||||
|
||||
@Gtk.Template.Callback()
|
||||
def on_info_bar_close(self, *args):
|
||||
self.info_revealer.set_reveal_child(False)
|
||||
|
||||
|
||||
@Gtk.Template.Callback()
|
||||
def on_info_bar_response(self, widget, response):
|
||||
self.info_revealer.set_reveal_child(False)
|
||||
|
||||
def on_headerbar_playpause_toggled(self, widget):
|
||||
if self._headerbar_playpause_button_active:
|
||||
self._mcg.playpause()
|
||||
self._mcg.get_status()
|
||||
|
||||
|
||||
# Panel callbacks
|
||||
|
|
@ -311,19 +289,17 @@ class Window(Gtk.ApplicationWindow):
|
|||
self._set_menu_visible_panel()
|
||||
for panel in self._panels:
|
||||
panel.set_selected(panel == self.panel_stack.get_visible_child())
|
||||
GObject.idle_add(
|
||||
self.panel_stack.child_set_property,
|
||||
self.panel_stack.get_visible_child(),
|
||||
'needs-attention',
|
||||
False
|
||||
)
|
||||
|
||||
|
||||
def on_panel_open_standalone(self, panel):
|
||||
self.set_titlebar(panel.get_headerbar_standalone())
|
||||
self.toolbar_view.add_top_bar(panel.get_headerbar_standalone())
|
||||
self.toolbar_view.remove(self.headerbar)
|
||||
|
||||
|
||||
def on_panel_close_standalone(self, headerbar):
|
||||
self.set_titlebar(self.headerbar)
|
||||
def on_panel_close_standalone(self, panel):
|
||||
self.toolbar_view.add_top_bar(self.headerbar)
|
||||
self.toolbar_view.remove(panel.get_headerbar_standalone())
|
||||
|
||||
|
||||
def on_connection_panel_connection_changed(self, widget, host, port, password):
|
||||
self._settings.set_string(Window.SETTING_HOST, host)
|
||||
|
|
@ -397,11 +373,11 @@ class Window(Gtk.ApplicationWindow):
|
|||
|
||||
|
||||
def on_library_panel_sort_order_changed(self, widget, sort_order):
|
||||
self._settings.set_enum(Window.SETTING_SORT_ORDER, self._library_panel.get_sort_order())
|
||||
self._settings.set_enum(Window.SETTING_SORT_ORDER, sort_order)
|
||||
|
||||
|
||||
def on_library_panel_sort_type_changed(self, widget, sort_type):
|
||||
self._settings.set_boolean(Window.SETTING_SORT_TYPE, self._library_panel.get_sort_type())
|
||||
self._settings.set_boolean(Window.SETTING_SORT_TYPE, sort_type)
|
||||
|
||||
|
||||
def on_library_panel_albumart(self, widget, album):
|
||||
|
|
@ -415,7 +391,6 @@ class Window(Gtk.ApplicationWindow):
|
|||
GObject.idle_add(self._connect_connected)
|
||||
self._mcg.load_playlist()
|
||||
self._mcg.load_albums()
|
||||
self._mcg.get_custom(Window._CUSTOM_STARTUP_COMPLETE)
|
||||
self._mcg.get_status()
|
||||
self._mcg.get_stats()
|
||||
self._mcg.get_output_devices()
|
||||
|
|
@ -450,9 +425,7 @@ class Window(Gtk.ApplicationWindow):
|
|||
# Status
|
||||
self._server_panel.set_status(file, audio, bitrate, error)
|
||||
# Error
|
||||
if error is None:
|
||||
self.info_revealer.set_reveal_child(False)
|
||||
else:
|
||||
if error:
|
||||
self._show_error(error)
|
||||
|
||||
|
||||
|
|
@ -486,17 +459,6 @@ class Window(Gtk.ApplicationWindow):
|
|||
self._library_panel.set_albumart(album, data)
|
||||
|
||||
|
||||
def on_mcg_custom(self, name):
|
||||
if name == Window._CUSTOM_STARTUP_COMPLETE:
|
||||
for panel in self._panels:
|
||||
GObject.idle_add(
|
||||
self.panel_stack.child_set_property,
|
||||
panel,
|
||||
'needs-attention',
|
||||
False
|
||||
)
|
||||
|
||||
|
||||
def on_mcg_error(self, error):
|
||||
GObject.idle_add(self._show_error, str(error))
|
||||
|
||||
|
|
@ -563,17 +525,11 @@ class Window(Gtk.ApplicationWindow):
|
|||
if self._state.get_property(WindowState.IS_FULLSCREENED):
|
||||
self.headerbar.hide()
|
||||
self._cover_panel.set_fullscreen(True)
|
||||
# Hide cursor
|
||||
self.get_window().set_cursor(
|
||||
Gdk.Cursor.new_from_name(Gdk.Display.get_default(), "none")
|
||||
)
|
||||
self.set_cursor(Gdk.Cursor.new_from_name("none", None))
|
||||
else:
|
||||
self.headerbar.show()
|
||||
self._cover_panel.set_fullscreen(False)
|
||||
# Reset cursor
|
||||
self.get_window().set_cursor(
|
||||
Gdk.Cursor.new_from_name(Gdk.Display.get_default(), "default")
|
||||
)
|
||||
self.set_cursor(Gdk.Cursor.new_from_name("default", None))
|
||||
|
||||
|
||||
def _save_visible_panel(self):
|
||||
|
|
@ -593,58 +549,39 @@ class Window(Gtk.ApplicationWindow):
|
|||
|
||||
|
||||
def _set_play(self):
|
||||
self.headerbar_button_playpause.handler_block_by_func(
|
||||
self.on_headerbar_playpause_toggled
|
||||
)
|
||||
self._headerbar_playpause_button_active = False
|
||||
self.headerbar_button_playpause.set_active(True)
|
||||
self.headerbar_button_playpause.handler_unblock_by_func(
|
||||
self.on_headerbar_playpause_toggled
|
||||
)
|
||||
self._headerbar_playpause_button_active = True
|
||||
|
||||
|
||||
def _set_pause(self):
|
||||
self.headerbar_button_playpause.handler_block_by_func(
|
||||
self.on_headerbar_playpause_toggled
|
||||
)
|
||||
self._headerbar_playpause_button_active = False
|
||||
self.headerbar_button_playpause.set_active(False)
|
||||
self.headerbar_button_playpause.handler_unblock_by_func(
|
||||
self.on_headerbar_playpause_toggled
|
||||
)
|
||||
self._headerbar_playpause_button_active = True
|
||||
|
||||
|
||||
def _set_volume(self, volume):
|
||||
if volume >= 0:
|
||||
self.headerbar_button_volume.set_visible(True)
|
||||
if not self._changing_volume:
|
||||
self._setting_volume = True
|
||||
self.headerbar_button_volume.set_value(volume / 100)
|
||||
self._setting_volume = False
|
||||
self._setting_volume = True
|
||||
self.headerbar_button_volume.set_value(volume / 100)
|
||||
self._setting_volume = False
|
||||
else:
|
||||
self.headerbar_button_volume.set_visible(False)
|
||||
|
||||
|
||||
def _headerbar_connected(self):
|
||||
self.headerbar_button_connect.handler_block_by_func(
|
||||
self.on_headerbar_connection_active_notify
|
||||
)
|
||||
self._headerbar_connection_button_active = False
|
||||
self.headerbar_button_connect.set_active(True)
|
||||
self.headerbar_button_connect.set_state(True)
|
||||
self.headerbar_button_connect.handler_unblock_by_func(
|
||||
self.on_headerbar_connection_active_notify
|
||||
)
|
||||
self.headerbar_title_stack.set_visible_child(self.headerbar_panel_switcher)
|
||||
self._headerbar_connection_button_active = True
|
||||
|
||||
|
||||
def _headerbar_disconnected(self):
|
||||
self.headerbar_button_connect.handler_block_by_func(
|
||||
self.on_headerbar_connection_active_notify
|
||||
)
|
||||
self._headerbar_connection_button_active = False
|
||||
self.headerbar_button_connect.set_active(False)
|
||||
self.headerbar_button_connect.set_state(False)
|
||||
self.headerbar_button_connect.handler_unblock_by_func(
|
||||
self.on_headerbar_connection_active_notify
|
||||
)
|
||||
self.headerbar_title_stack.set_visible_child(self.headerbar_connection_label)
|
||||
self._headerbar_connection_button_active = True
|
||||
|
||||
|
||||
def _set_headerbar_sensitive(self, sensitive, connecting):
|
||||
|
|
@ -655,6 +592,4 @@ class Window(Gtk.ApplicationWindow):
|
|||
|
||||
|
||||
def _show_error(self, message):
|
||||
self.info_bar.set_message_type(Gtk.MessageType.ERROR)
|
||||
self.info_label.set_text(message)
|
||||
self.info_revealer.set_reveal_child(True)
|
||||
self.info_toast.add_toast(Adw.Toast.new(message))
|
||||
|
|
|
|||
Loading…
Reference in a new issue