audio: Clean up the NowPlaying widget

It now uses the TrackTitle and TrackArtist labels. Additonally, I move
it out of the Player class and create a shortcut function for
instantiating one using the global Player instance

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2021-08-30 17:29:44 -04:00
parent ae7c2010e9
commit e884dc5a6e
8 changed files with 35 additions and 70 deletions

View File

@ -1,7 +1,12 @@
# Copyright 2021 (c) Anna Schumaker.
from . import player
from gi.repository import Gst
import sys
Gst.init(sys.argv)
from gi.repository import Gst
from . import nowplaying
from . import player
Gst.init(sys.argv)
Player = player.Player()
def NowPlaying():
return nowplaying.NowPlaying(Player)

View File

@ -27,37 +27,13 @@ class TrackArtist(Gtk.Label):
class NowPlaying(Gtk.ScrolledWindow):
def __init__(self):
def __init__(self, player):
Gtk.ScrolledWindow.__init__(self)
self.title = Gtk.Label()
self.title.add_css_class("title")
self.subtitle = Gtk.Label()
self.subtitle.add_css_class("subtitle")
self.box = Gtk.Box.new(Gtk.Orientation.VERTICAL, 0)
self.box.append(self.title)
self.box.append(self.subtitle)
self.box.append(TrackTitle(player))
self.box.append(TrackArtist(player))
self.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.NEVER)
self.set_valign(Gtk.Align.CENTER)
self.set_hexpand(True)
self.set_child(self.box)
self.set_title(None)
self.set_subtitle(None)
def set_artist(self, text):
self.set_subtitle(f"by {text}")
def set_title(self, text):
if text == None:
text = "Emmental"
text = GLib.markup_escape_text(text)
self.title.set_markup(f"<big>{text}</big>")
def set_subtitle(self, text):
if text == None:
text = "The Cheesy Music Player"
text = GLib.markup_escape_text(text)
self.subtitle.set_markup(f"<big>{text}</big>")

View File

@ -1,7 +1,6 @@
# Copyright 2021 (c) Anna Schumaker.
from . import artwork
from . import controls
from . import nowplaying
from . import seeker
from lib import publisher
from lib import settings
@ -29,7 +28,6 @@ class Player(GObject.GObject):
self.Controls.connect("next", self.next)
self.Controls.connect("volume-changed", self.volume_changed)
self.NowPlaying = nowplaying.NowPlaying()
self.Artwork = artwork.Artwork()
self.Seeker = seeker.Seeker()
@ -84,12 +82,6 @@ class Player(GObject.GObject):
def on_tag(self, bus, message):
taglist = message.parse_tag()
(res, title) = taglist.get_string("title")
if res == True:
self.NowPlaying.set_title(title)
(res, artist) = taglist.get_string("artist")
if res == True:
self.NowPlaying.set_artist(artist)
(res, sample) = taglist.get_sample("image")
if res == True:
self.Artwork.set_from_sample(sample)

11
audio/test_audio.py Normal file
View File

@ -0,0 +1,11 @@
# Copyright 2021 (c) Anna Schumaker.
import audio
import unittest
class TestAudio(unittest.TestCase):
def test_audio_init(self):
self.assertIsInstance(audio.Player, audio.player.Player)
def test_audio_widgets(self):
self.assertIsInstance(audio.NowPlaying(),
audio.nowplaying.NowPlaying)

View File

@ -46,41 +46,20 @@ class TestAudioTrackArtist(unittest.TestCase):
class TestNowPlaying(unittest.TestCase):
def test_now_playing_init(self):
now = nowplaying.NowPlaying()
def test_now_playing(self):
fake = FakePlayer()
now = nowplaying.NowPlaying(fake)
child = now.box.get_first_child()
viewport = now.get_child()
self.assertIsInstance(now, Gtk.ScrolledWindow)
self.assertIsInstance(now.box, Gtk.Box)
self.assertIsInstance(now.title, Gtk.Label)
self.assertIsInstance(now.subtitle, Gtk.Label)
self.assertIsInstance(child, nowplaying.TrackTitle)
self.assertIsInstance(child.get_next_sibling(), nowplaying.TrackArtist)
self.assertIn(now.title, now.box)
self.assertIn(now.subtitle, now.box)
viewport = now.get_child()
self.assertEqual(viewport.get_child(), now.box)
self.assertEqual(now.get_valign(), Gtk.Align.CENTER)
self.assertEqual(now.get_policy(), (Gtk.PolicyType.AUTOMATIC,
Gtk.PolicyType.NEVER))
self.assertTrue(now.get_hexpand())
self.assertTrue(now.title.has_css_class("title"))
self.assertTrue(now.subtitle.has_css_class("subtitle"))
def test_now_playing_text(self):
now = nowplaying.NowPlaying()
self.assertEqual(now.title.get_text(), "Emmental")
self.assertEqual(now.subtitle.get_text(), "The Cheesy Music Player")
now.set_title("Test Title")
now.set_artist("Test Artist")
self.assertEqual(now.title.get_text(), "Test Title")
self.assertEqual(now.subtitle.get_text(), "by Test Artist")
now.set_title(None)
now.set_subtitle(None)
self.assertEqual(now.title.get_text(), "Emmental")
self.assertEqual(now.subtitle.get_text(), "The Cheesy Music Player")

View File

@ -47,7 +47,6 @@ class TestPlayer(unittest.TestCase):
self.assertIsInstance(play.playbin, Gst.Element)
self.assertIsInstance(play.bus, Gst.Bus)
self.assertIsInstance(play.Controls, controls.Controls)
self.assertIsInstance(play.NowPlaying, nowplaying.NowPlaying)
self.assertIsInstance(play.Artwork, artwork.Artwork)
self.assertIsInstance(play.Seeker, seeker.Seeker)
self.assertIsNone(play.track)

View File

@ -4,5 +4,5 @@ import audio
Header = Gtk.HeaderBar()
Header.pack_start(audio.Player.Controls)
Header.set_title_widget(audio.Player.NowPlaying)
Header.set_title_widget(audio.NowPlaying())
Header.pack_end(audio.Player.Seeker)

View File

@ -1,8 +1,11 @@
# Copyright 2021 (c) Anna Schumaker.
from . import header
from gi.repository import Gtk
import audio
import unittest
from gi.repository import Gtk
from . import header
class TestHeader(unittest.TestCase):
def test_header_init(self):
self.assertIsInstance(header.Header, Gtk.HeaderBar)
self.assertIsInstance(header.Header.get_title_widget(),
audio.nowplaying.NowPlaying)