sidebar: Create a PlaylistTitle widget

This widget sets the Title::subtitle property to a nicely formatted
string based on the number set to the 'count' property.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2022-08-11 15:47:28 -04:00
parent cd4caf7df8
commit cdae9541e9
2 changed files with 40 additions and 0 deletions

View File

@ -31,3 +31,18 @@ class Title(Gtk.Box):
self.append(self._title)
self.append(self._subtitle)
class PlaylistTitle(Title):
"""A title widget for displaying playlist size."""
count = GObject.Property(type=int)
def __init__(self, title: str = "", **kwargs):
"""Initialize a Playlist Title widget."""
super().__init__(title=title, subtitle="-- tracks", **kwargs)
self.connect("notify::count", self.__update_subtitle)
def __update_subtitle(self, title: Title, param) -> None:
s_count = "s" if self.count != 1 else ""
self.subtitle = f"{self.count} track{s_count}"

View File

@ -62,3 +62,28 @@ class TestTitle(unittest.TestCase):
title2 = emmental.sidebar.title.Title(subtitle="Other Subtitle")
self.assertEqual(title2._subtitle.get_text(), "Other Subtitle")
class TestPlaylistTitle(unittest.TestCase):
"""Test our title widget configured for displaying playlist size."""
def setUp(self):
"""Set up common variables."""
self.title = emmental.sidebar.title.PlaylistTitle()
def test_init(self):
"""Test that the PlaylistTitle is configured correctly."""
self.assertIsInstance(self.title, emmental.sidebar.title.Title)
self.assertEqual(self.title.subtitle, "-- tracks")
self.assertEqual(self.title.count, 0)
def test_count(self):
"""Test changing the count property."""
self.title.count = 0
self.assertEqual(self.title.subtitle, "0 tracks")
self.title.count = 1
self.assertEqual(self.title.subtitle, "1 track")
self.title.count = 2
self.assertEqual(self.title.subtitle, "2 tracks")