sidebar: Create ParentTagRows

With an expander button that shows or hides its children

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2021-07-02 11:46:01 -04:00
parent 43033b4e21
commit de155f6c88
4 changed files with 81 additions and 4 deletions

View File

@ -79,3 +79,6 @@ class ParentTagBox(TagBox):
TagBox.__init__(self, tagstore, icon, header)
self.childstore = childstore
self.childicon = childicon
def __alloc_tagrow__(self, tag):
return tagrow.ParentTagRow(tag, self.icon)

View File

@ -7,6 +7,8 @@ class TagRow(Gtk.ListBoxRow):
self.tag = tag
self.grid = Gtk.Grid()
self.image = Gtk.Image.new_from_icon_name(icon)
self.image.set_margin_start(5)
self.image.set_margin_end(5)
self.label = Gtk.Label()
self.label.set_halign(Gtk.Align.START)
@ -15,8 +17,6 @@ class TagRow(Gtk.ListBoxRow):
self.grid.attach(self.image, 0, 0, 1, 1)
self.grid.attach(self.label, 1, 0, 1, 1)
self.grid.set_column_spacing(5)
self.grid.set_margin_start(5)
self.grid.set_margin_end(5)
self.set_child(self.grid)
self.set_label()
@ -27,6 +27,29 @@ class TagRow(Gtk.ListBoxRow):
self.label.set_label(text)
class ParentTagRow(TagRow):
def __init__(self, tag, icon):
TagRow.__init__(self, tag, icon)
self.children = [ ]
self.expander = Gtk.ToggleButton()
self.expander.connect("toggled", self.on_toggle)
self.expander.set_icon_name("pan-end-symbolic")
self.expander.add_css_class("flat")
self.grid.insert_column(0)
self.grid.attach(self.expander, 0, 0, 1, 1)
def add_child(self, child):
self.children.append(child)
child.set_visible(self.expander.get_active())
def on_toggle(self, button):
direction = "down" if self.expander.get_active() else "end"
self.expander.set_icon_name(f"pan-{direction}-symbolic")
for child in self.children:
child.set_visible(self.expander.get_active())
class TagHeader(Gtk.Box):
def __init__(self, char):
Gtk.Box.__init__(self)

View File

@ -124,3 +124,17 @@ class TestParentTagBox(unittest.TestCase):
self.assertEqual(ptbox.icon, "missing-icon")
self.assertEqual(ptbox.childstore, superstore)
self.assertEqual(ptbox.childicon, "missing-icon")
def test_parent_tagbox_add_remove(self):
store = lib.tagstore.TagStore()
superstore = lib.tagstore.TagSuperStore()
ptbox = tagbox.ParentTagBox(store, "missing-icon",
superstore, "missing-icon")
tag = store.add("Test", fake.Track(1))
ptbox.bus.complete()
self.assertIsInstance(tag.widgets, tagrow.ParentTagRow)
store.remove(tag)
ptbox.bus.complete()
self.assertIsNone(tag.widgets)

View File

@ -24,8 +24,8 @@ class TestTagRow(unittest.TestCase):
self.assertIsInstance(row.label, Gtk.Label)
self.assertEqual(row.image.get_icon_name(), "missing-icon")
self.assertEqual(row.grid.get_column_spacing(), 5)
self.assertEqual(row.grid.get_margin_start(), 5)
self.assertEqual(row.image.get_margin_start(), 5)
self.assertEqual(row.image.get_margin_end(), 5)
self.assertEqual(row.grid.get_margin_end(), 5)
self.assertEqual(row.label.get_text(), "test\n0 Tracks")
@ -48,6 +48,43 @@ class TestTagRow(unittest.TestCase):
self.assertEqual(row.label.get_text(), "test\n4 Tracks")
class TestParentTagRow(unittest.TestCase):
def test_parent_tagrow_init(self):
tag = FakeTag("test")
row = tagrow.ParentTagRow(tag, "missing-icon")
self.assertIsInstance(row, tagrow.TagRow)
self.assertIsInstance(row.expander, Gtk.ToggleButton)
self.assertEqual(row.expander.get_icon_name(), "pan-end-symbolic")
self.assertEqual(row.children, [ ])
self.assertTrue(row.expander.has_css_class("flat"))
self.assertIn(row.expander, row.grid)
def test_parent_tagrow_children(self):
tag = FakeTag("test")
row = tagrow.ParentTagRow(tag, "missing-icon")
c1 = Gtk.Label.new(str="Child 1")
row.add_child(c1)
self.assertEqual(row.children, [ c1 ])
self.assertFalse(c1.is_visible())
row.expander.set_active(True)
self.assertEqual(row.expander.get_icon_name(), "pan-down-symbolic")
self.assertTrue(c1.is_visible())
c2 = Gtk.Label.new(str="Child 2")
row.add_child(c2)
self.assertEqual(row.children, [ c1, c2 ])
row.expander.set_active(False)
self.assertEqual(row.expander.get_icon_name(), "pan-end-symbolic")
self.assertFalse(c1.is_visible())
self.assertFalse(c2.is_visible())
class TestTagHeader(unittest.TestCase):
def test_tag_header(self):
header = tagrow.TagHeader("A")