nowplaying: Add a TagInfo window to the Now Playing card

And bind the Player tag properties to the Now Playing card.

Implements: #45 (Create a new NowPlaying widget)
Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2022-06-24 17:30:57 -04:00
parent dbc2ec03f2
commit 4bced82b1f
5 changed files with 71 additions and 1 deletions

View File

@ -66,7 +66,13 @@ class Application(Adw.Application):
def build_now_playing(self) -> nowplaying.Card: def build_now_playing(self) -> nowplaying.Card:
"""Build a new now playing card.""" """Build a new now playing card."""
return nowplaying.Card() playing = nowplaying.Card()
for prop in ["title", "album", "artist", "album-artist"]:
self.player.bind_property(prop, playing, prop)
self.db.settings.bind_setting("now-playing.prefer-artist",
playing, "prefer-artist")
return playing
def build_window(self) -> window.Window: def build_window(self) -> window.Window:
"""Build a new window instance.""" """Build a new window instance."""

View File

@ -1,15 +1,31 @@
# Copyright 2022 (c) Anna Schumaker. # Copyright 2022 (c) Anna Schumaker.
"""A card for displaying information about the currently playing track.""" """A card for displaying information about the currently playing track."""
from gi.repository import GObject
from gi.repository import Gtk from gi.repository import Gtk
from . import tags
class Card(Gtk.Box): class Card(Gtk.Box):
"""The Now Playing information card.""" """The Now Playing information card."""
title = GObject.Property(type=str)
album = GObject.Property(type=str)
artist = GObject.Property(type=str)
album_artist = GObject.Property(type=str)
prefer_artist = GObject.Property(type=bool, default=True)
def __init__(self): def __init__(self):
"""Initialize a Now Playing Card.""" """Initialize a Now Playing Card."""
super().__init__() super().__init__()
self._grid = Gtk.Grid() self._grid = Gtk.Grid()
self._tags = tags.TagInfo()
for prop in ["title", "album", "artist", "album-artist"]:
self.bind_property(prop, self._tags, prop)
self.bind_property("prefer-artist", self._tags, "prefer-artist",
GObject.BindingFlags.BIDIRECTIONAL)
self._grid.attach(self._tags, 0, 0, 1, 1)
self.append(self._grid) self.append(self._grid)
self.add_css_class("card") self.add_css_class("card")

View File

@ -19,3 +19,28 @@ class TestNowPlaying(unittest.TestCase):
self.assertEqual(self.card.get_last_child(), self.card._grid) self.assertEqual(self.card.get_last_child(), self.card._grid)
self.assertTrue(self.card.has_css_class("card")) self.assertTrue(self.card.has_css_class("card"))
def test_prefer_artist(self):
"""Test the 'prefer-artist' property."""
self.assertTrue(self.card.prefer_artist)
self.card.prefer_artist = False
self.assertFalse(self.card.prefer_artist)
self.assertFalse(self.card._tags.prefer_artist)
self.card._tags.prefer_artist = True
self.assertTrue(self.card.prefer_artist)
self.assertTrue(self.card._tags.prefer_artist)
def test_tags(self):
"""Test tag properties."""
self.assertIsInstance(self.card._tags,
emmental.nowplaying.tags.TagInfo)
self.assertEqual(self.card._grid.get_child_at(0, 0), self.card._tags)
for tag in ["title", "album", "artist", "album-artist"]:
with self.subTest(tag=tag):
self.card.set_property(tag, f"test {tag}")
self.assertEqual(self.card.get_property(tag), f"test {tag}")
self.assertEqual(self.card._tags.get_property(tag),
f"test {tag}")

View File

@ -103,6 +103,19 @@ class TestEmmental(unittest.TestCase):
self.assertEqual(win.header.title, emmental.VERSION_STRING) self.assertEqual(win.header.title, emmental.VERSION_STRING)
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
def test_nowplaying(self, mock_stdout: io.StringIO):
"""Check that the nowplaying widget is wired up properly."""
self.application.db = emmental.db.Connection()
self.application.player = emmental.audio.Player()
win = self.application.build_window()
for tag in ["title", "album", "artist", "album-artist"]:
with self.subTest(tag=tag):
self.assertEqual(win.now_playing.get_property(tag), "")
self.application.player.set_property(tag, "Test Tag")
self.assertEqual(win.now_playing.get_property(tag), "Test Tag")
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO) @unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
def test_replaygain(self, mock_stdout: io.StringIO): def test_replaygain(self, mock_stdout: io.StringIO):
"""Test setting replaygain modes.""" """Test setting replaygain modes."""

View File

@ -88,3 +88,13 @@ class TestSettings(unittest.TestCase):
self.assertEqual(self.settings["now-playing.size"], 400) self.assertEqual(self.settings["now-playing.size"], 400)
self.assertEqual(self.app.build_window().now_playing_size, 400) self.assertEqual(self.app.build_window().now_playing_size, 400)
def test_save_nowplaying_prefer_artist(self, mock_stdout: io.StringIO):
"""Check saving and loading the prefer-artist setting."""
self.assertTrue(self.win.now_playing.prefer_artist)
self.assertTrue(self.settings["now-playing.prefer-artist"])
self.win.now_playing.prefer_artist = False
self.assertFalse(self.settings["now-playing.prefer-artist"])
self.assertFalse(self.app.build_window().now_playing.prefer_artist)