sidebar: Add the Playlist section to the Sidebar

I created a section Group with this section as the only member for now, and
bind the "selected-playlist" property to the sidebar.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2022-08-11 12:22:30 -04:00
parent d49b033b0d
commit 711c04cb29
2 changed files with 69 additions and 4 deletions

View File

@ -2,6 +2,8 @@
"""A card for displaying the list of playlists."""
from gi.repository import GObject
from gi.repository import Gtk
from . import playlist
from . import section
from .. import db
from .. import entry
@ -10,15 +12,25 @@ class Card(Gtk.Box):
"""Our playlist Sidebar."""
sql = GObject.Property(type=db.Connection)
selected_playlist = GObject.Property(type=db.playlist.Playlist)
def __init__(self, sql: db.Connection, **kwargs):
"""Set up the Sidebar widget."""
super().__init__(sql=sql, orientation=Gtk.Orientation.VERTICAL,
sensitive=False, **kwargs)
self._filter = entry.Filter("playlists")
self._playlists = playlist.Section(self.sql.playlists)
self._group = section.Group(sql)
self.append(self._filter)
for sect in [self._playlists]:
self.append(sect)
self._group.add(sect)
self._group.bind_property("selected-playlist",
self, "selected-playlist")
self._filter.connect("search-changed", self.__search_changed)
self.sql.connect("table-loaded", self.__table_loaded)
@ -30,5 +42,17 @@ class Card(Gtk.Box):
self.sql.filter(entry.get_query())
def __table_loaded(self, sql: db.Connection, table: db.table.Table):
loaded = {tbl.loaded for tbl in sql.playlist_tables()}
self.set_sensitive(False not in loaded)
if self.get_sensitive() is False:
if False not in {tbl.loaded for tbl in sql.playlist_tables()}:
self.set_sensitive(True)
self.select_playlist(sql.active_playlist)
def select_playlist(self, playlist: db.playlist.Playlist) -> None:
"""Set the current active playlist."""
if playlist is not None:
match playlist.table:
case self.sql.playlists:
section = self._playlists
section.active = True
section.select_playlist(playlist)

View File

@ -42,8 +42,49 @@ class TestSidebar(tests.util.TestCase):
def test_sensitivity(self):
"""Test setting the sidebar sensitivity when all tables have loaded."""
tables = [t for t in self.sql.playlist_tables()]
self.assertFalse(self.sidebar.get_sensitive())
self.sidebar.select_playlist = unittest.mock.Mock()
for table in tables:
self.assertFalse(self.sidebar.get_sensitive())
self.sidebar.select_playlist.assert_not_called()
self.sql.emit("table-loaded", table)
self.assertEqual(self.sidebar.get_sensitive(), table == tables[-1])
self.assertTrue(self.sidebar.get_sensitive())
self.sidebar.select_playlist.assert_called()
self.sidebar.select_playlist.reset_mock()
self.sql.emit("table-loaded", tables[0])
self.sidebar.select_playlist.assert_not_called()
def test_selected_playlist(self):
"""Test the selected-playlist property."""
self.assertIsNone(self.sidebar.selected_playlist)
playlist1 = self.sql.playlists.create("Playlist 1")
self.sidebar._group.selected_playlist = playlist1
self.assertEqual(self.sidebar.selected_playlist, playlist1)
def test_group(self):
"""Test that sidebar sections are part of the same Group."""
self.assertIsInstance(self.sidebar._group,
emmental.sidebar.section.Group)
self.assertListEqual(self.sidebar._group._sections,
[self.sidebar._playlists])
def test_sections(self):
"""Tests sidebar section models."""
self.assertIsInstance(self.sidebar._playlists,
emmental.sidebar.playlist.Section)
self.assertEqual(self.sidebar._filter.get_next_sibling(),
self.sidebar._playlists)
self.assertEqual(self.sidebar._playlists.table, self.sql.playlists)
def test_select_playlist(self):
"""Test setting the active playlist."""
playlist = self.sql.playlists.create("Test Playlist")
self.sidebar.select_playlist(playlist)
self.assertTrue(self.sidebar._playlists.active)
self.assertEqual(self.sidebar.selected_playlist, playlist)