playlist: Make the current track bold

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2021-11-22 10:57:26 -05:00
parent 78647884d6
commit da708a9810
2 changed files with 47 additions and 7 deletions

View File

@ -1,9 +1,25 @@
# Copyright 2021 (c) Anna Schumaker. # Copyright 2021 (c) Anna Schumaker.
import audio import audio
import lib import lib
from gi.repository import Gtk, GLib from gi.repository import Gtk, GLib, Pango
from lib import settings from lib import settings
BoldAttr = Pango.AttrList.new()
BoldAttr.insert(Pango.attr_weight_new(Pango.Weight.BOLD))
class TrackLabel(Gtk.Label):
def set_item(self, item, text):
audio.Player.connect("track-changed", self.track_changed, item)
self.track_changed(audio.Player, None, audio.Player.track, item)
self.set_text(text)
def unset_item(self, item):
self.set_text("")
audio.Player.disconnect_by_func(self.track_changed)
def track_changed(self, player, old, new, this):
self.set_attributes(BoldAttr if this == new else None)
class LabelFactory(Gtk.SignalListItemFactory): class LabelFactory(Gtk.SignalListItemFactory):
def __init__(self, xalign): def __init__(self, xalign):
@ -18,14 +34,15 @@ class LabelFactory(Gtk.SignalListItemFactory):
raise NotImplementedError raise NotImplementedError
def on_setup(self, factory, listitem): def on_setup(self, factory, listitem):
listitem.set_child(Gtk.Label(xalign=self.xalign)) listitem.set_child(TrackLabel(xalign=self.xalign))
def on_bind(self, factory, listitem): def on_bind(self, factory, listitem):
text = self.get_track_text(listitem.get_item()) item = listitem.get_item()
listitem.get_child().set_text(text) text = self.get_track_text(item)
listitem.get_child().set_item(item, text)
def on_unbind(self, factory, listitem): def on_unbind(self, factory, listitem):
listitem.get_child().set_text("") listitem.get_child().unset_item(listitem.get_item())
def on_teardown(self, factory, listitem): def on_teardown(self, factory, listitem):
listitem.set_child(None) listitem.set_child(None)

View File

@ -1,4 +1,5 @@
# Copyright 2021 (c) Anna Schumaker. # Copyright 2021 (c) Anna Schumaker.
import audio
import db import db
import lib import lib
import unittest import unittest
@ -13,23 +14,45 @@ class FakeListItem:
class TestLabelFactory(unittest.TestCase): class TestLabelFactory(unittest.TestCase):
def setUp(self): db.reset()
def test_init(self): def test_init(self):
factory = column.LabelFactory(0.5) factory = column.LabelFactory(0.5)
self.assertIsInstance(factory, Gtk.SignalListItemFactory) self.assertIsInstance(factory, Gtk.SignalListItemFactory)
self.assertEqual(factory.xalign, 0.5) self.assertEqual(factory.xalign, 0.5)
def test_label(self):
audio.Player.track = None
track = db.make_fake_track(1, 1, "Test Track", "/a/b/c/1.ogg")
label = column.TrackLabel()
self.assertIsInstance(label, Gtk.Label)
self.assertIsNone(label.get_attributes())
label.set_attributes(column.BoldAttr)
label.set_item(track, "Test Track")
self.assertEqual(label.get_text(), "Test Track")
self.assertIsNone(label.get_attributes())
audio.Player.change_track(track)
self.assertIsNotNone(label.get_attributes())
label.unset_item(track)
self.assertEqual(label.get_text(), "")
def test_setup_teardown(self): def test_setup_teardown(self):
fake = FakeListItem() fake = FakeListItem()
factory = column.LabelFactory(0.5) factory = column.LabelFactory(0.5)
factory.on_setup(factory, fake) factory.on_setup(factory, fake)
self.assertIsInstance(fake.child, Gtk.Label) self.assertIsInstance(fake.child, column.TrackLabel)
self.assertEqual(fake.child.get_xalign(), 0.5) self.assertEqual(fake.child.get_xalign(), 0.5)
factory.on_teardown(factory, fake) factory.on_teardown(factory, fake)
self.assertIsNone(fake.child) self.assertIsNone(fake.child)
def test_bind_unbind(self): def test_bind_unbind(self):
track = db.make_fake_track(1, 1, "Test Track", "/a/b/c/1.ogg")
fake = FakeListItem() fake = FakeListItem()
factory = column.LabelFactory(0.5) factory = column.LabelFactory(0.5)
factory.on_setup(factory, fake) factory.on_setup(factory, fake)
@ -37,7 +60,7 @@ class TestLabelFactory(unittest.TestCase):
with self.assertRaises(NotImplementedError): with self.assertRaises(NotImplementedError):
factory.on_bind(factory, fake) factory.on_bind(factory, fake)
fake.child.set_text("abcde") fake.child.set_item(track, "abcde")
factory.on_unbind(factory, fake) factory.on_unbind(factory, fake)
self.assertEqual(fake.child.get_text(), "") self.assertEqual(fake.child.get_text(), "")