sidebar: Add a header to some TagBoxes

I enable it for Genre and Artist tagboxes right now

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2021-06-28 16:09:54 -04:00
parent 6a044b8d4b
commit 6059081d03
5 changed files with 54 additions and 3 deletions

View File

@ -43,8 +43,8 @@ def add_stack_page(name, page):
toggle.set_active(settings.get("sidebar.page") == name)
add_stack_page("Playlists", tagbox.TagBox(trackdb.tags.User, "audio-x-generic"))
add_stack_page("Artists", tagbox.TagBox(trackdb.tags.Artist, "avatar-default-symbolic"))
add_stack_page("Genres", tagbox.TagBox(trackdb.tags.Genre, "emblem-generic"))
add_stack_page("Artists", tagbox.TagBox(trackdb.tags.Artist, "avatar-default-symbolic", header=True))
add_stack_page("Genres", tagbox.TagBox(trackdb.tags.Genre, "emblem-generic", header=True))
add_stack_page("Decades", tagbox.TagBox(trackdb.tags.Decade, "x-office-calendar"))
add_stack_page("Libraries", library.Box)

View File

@ -4,7 +4,7 @@ from gi.repository import Gtk
import lib
class TagBox(Gtk.ScrolledWindow):
def __init__(self, tagstore, icon):
def __init__(self, tagstore, icon, header=False):
Gtk.ScrolledWindow.__init__(self)
self.listbox = Gtk.ListBox()
self.tagstore = tagstore
@ -18,6 +18,9 @@ class TagBox(Gtk.ScrolledWindow):
self.on_tag_added(tag)
self.listbox.set_sort_func(self.sort_func)
if header == True:
self.listbox.set_header_func(self.header_func)
tagstore.Added.register(self.tag_added)
tagstore.Removed.register(self.tag_removed)
@ -28,6 +31,13 @@ class TagBox(Gtk.ScrolledWindow):
while (row := self[0]) != None:
self.on_tag_removed(row.tag)
def header_func(self, cur, prev):
c = cur.tag.get_header()
if prev == None or prev.tag.get_header() != c:
cur.set_header(tagrow.TagHeader(c))
else:
cur.set_header(None)
def sort_func(self, lhs, rhs):
if lhs.tag < rhs.tag:
return -1

View File

@ -25,3 +25,17 @@ class TagRow(Gtk.ListBoxRow):
count = len(self.tag)
text = f"{self.tag.name}\n{count} Track{'s' if count != 1 else ''}"
self.label.set_label(text)
class TagHeader(Gtk.Box):
def __init__(self, char):
Gtk.Box.__init__(self)
self.set_orientation(Gtk.Orientation.VERTICAL)
self.set_margin_top(5)
self.label = Gtk.Label()
self.label.set_markup(f"<small><b>{char}</b></small>")
self.separator = Gtk.Separator.new(Gtk.Orientation.HORIZONTAL)
self.append(self.label)
self.append(self.separator)

View File

@ -84,6 +84,19 @@ class TestTagBox(unittest.TestCase):
self.assertEqual(tbox[0], tag1.widgets)
self.assertEqual(tbox[1], tag2.widgets)
def test_tag_box_header(self):
store = lib.tagstore.TagStore()
tbox = tagbox.TagBox(store, "missing-icon", header=True)
tagAB = store.add("AB")
tbox.bus.complete()
self.assertIsNotNone(tagAB.widgets.get_header())
tagAA = store.add("AA")
tbox.bus.complete()
self.assertIsNotNone(tagAA.widgets.get_header())
self.assertIsNone(tagAB.widgets.get_header())
def test_tag_box_sort(self):
store = lib.tagstore.TagStore()
tbox = tagbox.TagBox(store, "missing-icon")

View File

@ -46,3 +46,17 @@ class TestTagRow(unittest.TestCase):
tag.num = 4
row.set_label()
self.assertEqual(row.label.get_text(), "test\n4 Tracks")
class TestTagHeader(unittest.TestCase):
def test_tag_header(self):
header = tagrow.TagHeader("A")
self.assertIsInstance(header, Gtk.Box)
self.assertIsInstance(header.label, Gtk.Label)
self.assertIsInstance(header.separator, Gtk.Separator)
self.assertEqual(header.label.get_text(), "A")
self.assertEqual(header.get_margin_top(), 5)
self.assertIn(header.label, header)
self.assertIn(header.separator, header)