sidebar: TagBoxes take a tagstore and icon as constructor arguments

I use the tagstore to register Added and Removed notifications. From
there, I can create and remove TagRow instances in the listbox.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2021-06-27 21:41:12 -04:00
parent 9ceb2d8a2f
commit bf0dbbe5dc
4 changed files with 58 additions and 7 deletions

View File

@ -3,6 +3,7 @@ from . import library
from . import tagbox
from lib import settings
from gi.repository import Gtk
import trackdb
Switcher = Gtk.Box.new(Gtk.Orientation.VERTICAL, 0)
Stack = Gtk.Stack()
@ -41,10 +42,10 @@ def add_stack_page(name, page):
Stack.add_named(page, name)
toggle.set_active(settings.get("sidebar.page") == name)
add_stack_page("Playlists", tagbox.TagBox("audio-x-generic"))
add_stack_page("Artists", tagbox.TagBox("avatar-default-symbolic"))
add_stack_page("Genres", tagbox.TagBox("emblem-generic"))
add_stack_page("Decades", tagbox.TagBox("x-office-calendar"))
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("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 pathlib
import trackdb
TagBox = tagbox.TagBox("folder-music")
TagBox = tagbox.TagBox(trackdb.Library, "folder-music")
#

View File

@ -1,11 +1,35 @@
# Copyright 2021 (c) Anna Schumaker.
from . import tagrow
from gi.repository import Gtk
import lib
class TagBox(Gtk.ScrolledWindow):
def __init__(self, icon):
def __init__(self, tagstore, icon):
Gtk.ScrolledWindow.__init__(self)
self.listbox = Gtk.ListBox()
self.tagstore = tagstore
self.icon = icon
self.bus = lib.bus.Bus(50)
self.set_child(self.listbox)
self.set_vexpand(True)
tagstore.Added.register(self.tag_added)
tagstore.Removed.register(self.tag_removed)
def __getitem__(self, i):
return self.listbox.get_row_at_index(i)
def on_tag_added(self, tag):
tag.widgets = tagrow.TagRow(tag, self.icon)
self.listbox.append(tag.widgets)
def tag_added(self, tag):
self.bus.board(self.on_tag_added, tag)
def on_tag_removed(self, tag):
self.listbox.remove(tag.widgets)
tag.widgets = None
def tag_removed(self, tag):
self.bus.board(self.on_tag_removed, tag)

View File

@ -1,17 +1,43 @@
# Copyright 2021 (c) Anna Schumaker.
from . import tagbox
from . import tagrow
from lib import fake
from gi.repository import Gtk
import lib
import unittest
class TestTagBox(unittest.TestCase):
def test_tag_box(self):
tbox = tagbox.TagBox("image-missing")
store = lib.tagstore.TagStore()
tbox = tagbox.TagBox(store, "image-missing")
self.assertEqual(tbox.tagstore, store)
self.assertEqual(tbox.icon, "image-missing")
self.assertEqual(tbox.bus.timeout, 50)
self.assertIsInstance(tbox, Gtk.ScrolledWindow)
self.assertIsInstance(tbox.listbox, Gtk.ListBox)
self.assertIsInstance(tbox.bus, lib.bus.Bus)
viewport = tbox.get_child()
self.assertIsInstance(viewport, Gtk.Viewport)
self.assertEqual(viewport.get_child(), tbox.listbox)
self.assertTrue(tbox.get_vexpand())
self.assertIn(tbox.tag_added, store.Added.subscribers)
self.assertIn(tbox.tag_removed, store.Removed.subscribers)
def test_tag_box_add_remove(self):
store = lib.tagstore.TagStore()
tbox = tagbox.TagBox(store, "missing-icon")
tag = store.add("Test", fake.Track(1))
self.assertIn((tbox.on_tag_added, (tag,)), tbox.bus.passengers)
tbox.bus.complete()
self.assertIsInstance(tag.widgets, tagrow.TagRow)
self.assertEqual(tbox[0], tag.widgets)
store.remove(tag)
self.assertIn((tbox.on_tag_removed, (tag,)), tbox.bus.passengers)
tbox.bus.complete()
self.assertIsNone(tag.widgets)
self.assertIsNone(tbox[0])