diff --git a/playlist/column.py b/playlist/column.py index ae3ca93..d5c64f4 100644 --- a/playlist/column.py +++ b/playlist/column.py @@ -1,9 +1,25 @@ # Copyright 2021 (c) Anna Schumaker. import audio import lib -from gi.repository import Gtk, GLib +from gi.repository import Gtk, GLib, Pango 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): def __init__(self, xalign): @@ -18,14 +34,15 @@ class LabelFactory(Gtk.SignalListItemFactory): raise NotImplementedError 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): - text = self.get_track_text(listitem.get_item()) - listitem.get_child().set_text(text) + item = listitem.get_item() + text = self.get_track_text(item) + listitem.get_child().set_item(item, text) 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): listitem.set_child(None) diff --git a/playlist/test_column.py b/playlist/test_column.py index b91f5a4..7906f73 100644 --- a/playlist/test_column.py +++ b/playlist/test_column.py @@ -1,4 +1,5 @@ # Copyright 2021 (c) Anna Schumaker. +import audio import db import lib import unittest @@ -13,23 +14,45 @@ class FakeListItem: class TestLabelFactory(unittest.TestCase): + def setUp(self): db.reset() + def test_init(self): factory = column.LabelFactory(0.5) self.assertIsInstance(factory, Gtk.SignalListItemFactory) 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): fake = FakeListItem() factory = column.LabelFactory(0.5) 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) factory.on_teardown(factory, fake) self.assertIsNone(fake.child) def test_bind_unbind(self): + track = db.make_fake_track(1, 1, "Test Track", "/a/b/c/1.ogg") fake = FakeListItem() factory = column.LabelFactory(0.5) factory.on_setup(factory, fake) @@ -37,7 +60,7 @@ class TestLabelFactory(unittest.TestCase): with self.assertRaises(NotImplementedError): factory.on_bind(factory, fake) - fake.child.set_text("abcde") + fake.child.set_item(track, "abcde") factory.on_unbind(factory, fake) self.assertEqual(fake.child.get_text(), "")