sidebar: Create a TreeRow

The TreeRow is a factory.TreeRow configured for binding playlists to a
child widget. Individual sections are expected to inherit from this to
set up their section-specific widgets and bind any extra properties.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2023-03-16 14:39:12 -04:00
parent 70799fa50f
commit 976f465cec
2 changed files with 59 additions and 0 deletions

View File

@ -7,6 +7,7 @@ from .title import EditableTitle
from .icon import Icon from .icon import Icon
from .icon import Settable from .icon import Settable
from .. import db from .. import db
from .. import factory
class BaseRow(Gtk.Box): class BaseRow(Gtk.Box):
@ -155,3 +156,19 @@ class LibraryRow(BaseRow):
case "delete": self.playlist.delete() case "delete": self.playlist.delete()
case "scan": self.playlist.scan() case "scan": self.playlist.scan()
case "stop": self.playlist.stop() case "stop": self.playlist.stop()
class TreeRow(factory.TreeRow):
"""A factory Row used for displaying individual playlists."""
def do_bind(self) -> None:
"""Bind a Playlist to this Row."""
self.child.playlist = self.item
self.bind_and_set_property("name", "name")
self.bind_and_set_property("n-tracks", "count")
self.bind_n_children(self.item.children)
self.bind_active("active")
def do_unbind(self) -> None:
"""Unbind a Playlist from this Row."""
self.child.playlist = None

View File

@ -6,6 +6,7 @@ import emmental.sidebar.icon
import emmental.sidebar.row import emmental.sidebar.row
from gi.repository import Gio from gi.repository import Gio
from gi.repository import Gtk from gi.repository import Gtk
from gi.repository import Adw
class TestBaseRow(unittest.TestCase): class TestBaseRow(unittest.TestCase):
@ -310,3 +311,44 @@ class TestLibraryRow(unittest.TestCase):
self.assertTrue(self.row._progress.get_visible()) self.assertTrue(self.row._progress.get_visible())
self.assertTrue(self.row._stop.get_visible()) self.assertTrue(self.row._stop.get_visible())
self.assertFalse(self.row._scan.get_visible()) self.assertFalse(self.row._scan.get_visible())
class TestTreeRow(unittest.TestCase):
"""Test the TreeRow widget used by the section factories."""
def setUp(self):
"""Set up common variables."""
self.playlist = emmental.db.playlist.Playlist(Gio.ListStore(),
0, "Playlist Name")
self.treeitem = Gtk.TreeListRow()
self.treeitem.get_item = unittest.mock.Mock(return_value=self.playlist)
self.listitem = Gtk.ListItem()
self.listitem.get_item = unittest.mock.Mock(return_value=self.treeitem)
self.treerow = emmental.sidebar.row.TreeRow(self.listitem)
self.adw_bin = Adw.Bin(child=self.listitem.get_child())
def test_init(self):
"""Test that the TreeRow was set up properly."""
self.assertIsInstance(self.treerow, emmental.factory.TreeRow)
self.assertIsNone(self.treerow.child)
def test_bind(self):
"""Test binding a Playlist to the TreeRow."""
self.treerow.child = emmental.sidebar.row.BaseRow()
self.playlist.table.update = unittest.mock.Mock(return_value=True)
self.playlist.active = True
self.playlist.n_tracks = 42
self.playlist.children = Gtk.FilterListModel.new(Gio.ListStore(), None)
self.treerow.bind()
self.assertEqual(self.treerow.child.playlist, self.playlist)
self.assertEqual(self.treerow.child.name, "Playlist Name")
self.assertEqual(self.treerow.child.count, 42)
self.assertEqual(self.treerow.n_children, 0)
self.assertTrue(self.treerow.active)
self.playlist.children.get_model().append(Gtk.Label())
self.assertEqual(self.treerow.n_children, 1)
self.treerow.unbind()
self.assertIsNone(self.treerow.child.playlist)