Port UI to GTK 4

This commit is contained in:
coderkun 2023-01-08 18:20:30 +01:00
commit 8359143663

View file

@ -2,7 +2,8 @@
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
@ -11,7 +12,7 @@ except:
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 +44,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'
@ -70,27 +71,27 @@ class Window(Gtk.ApplicationWindow):
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())
# Login screen
self._connection_panel = ConnectionPanel()
self._panels.append(self._connection_panel)
# Server panel
self._server_panel = ServerPanel()
self._panels.append(self._server_panel)
@ -108,19 +109,19 @@ 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.content_stack.add_child(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_with_icon(self._cover_panel, 'cover-panel', locale.gettext("Cover"), "image-x-generic-symbolic")
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)
# 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 +135,9 @@ 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::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)
@ -176,7 +180,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()
@ -228,7 +231,7 @@ class Window(Gtk.ApplicationWindow):
def on_menu_toggle_fullscreen(self, action, value):
self.panel_stack.set_visible_child(self._cover_panel)
self.panel_stack.set_visible_child(self.cover_panel_page)
if not self._state.get_property(WindowState.IS_FULLSCREENED):
self.fullscreen()
else:
@ -236,52 +239,36 @@ 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)
@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(fullscreened is True)
# 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 +277,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
@ -310,13 +291,13 @@ class Window(Gtk.ApplicationWindow):
self._save_visible_panel()
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
)
panel.set_selected(panel == self.panel_stack.get_visible_child())
def on_panel_open_standalone(self, panel):
self.set_titlebar(panel.get_headerbar_standalone())
@ -450,9 +431,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)
@ -593,58 +572,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 +615,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))