tracklist: Use the Texture Cache for album art

Replacing our tracklist-specific one that caches textures, but not to
local disk.

Implements #53 ("Convert the TrackList to use the Texture Cache")
Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2023-09-15 15:37:41 -04:00
parent a4f30d87e6
commit 6ebf29a632
2 changed files with 8 additions and 20 deletions

View File

@ -4,10 +4,10 @@ import datetime
import dateutil.tz import dateutil.tz
import pathlib import pathlib
from gi.repository import GObject from gi.repository import GObject
from gi.repository import Gdk
from gi.repository import Gtk from gi.repository import Gtk
from .. import buttons from .. import buttons
from .. import factory from .. import factory
from .. import texture
class TrackRow(factory.ListRow): class TrackRow(factory.ListRow):
@ -278,8 +278,6 @@ class MediumString(InscriptionRow):
class AlbumCover(TrackRow): class AlbumCover(TrackRow):
"""A Track Row to display Album art.""" """A Track Row to display Album art."""
Cache = dict()
filepath = GObject.Property(type=GObject.TYPE_PYOBJECT) filepath = GObject.Property(type=GObject.TYPE_PYOBJECT)
def __init__(self, listitem: Gtk.ListItem, property: str): def __init__(self, listitem: Gtk.ListItem, property: str):
@ -293,19 +291,14 @@ class AlbumCover(TrackRow):
match param.name: match param.name:
case "mediumid": self.rebind_album("filepath", to_self=True) case "mediumid": self.rebind_album("filepath", to_self=True)
case "filepath": case "filepath":
if self.filepath is None: tex = texture.CACHE[self.filepath]
texture = None self.child.set_paintable(tex)
elif (texture := AlbumCover.Cache.get(self.filepath)) is None: self.child.set_has_tooltip(tex is not None)
texture = Gdk.Texture.new_from_filename(str(self.filepath))
AlbumCover.Cache[self.filepath] = texture
self.child.set_paintable(texture)
self.child.set_has_tooltip(texture is not None)
def __query_tooltip(self, child: Gtk.Picture, x: int, y: int, def __query_tooltip(self, child: Gtk.Picture, x: int, y: int,
keyboard_mode: bool, tooltip: Gtk.Tooltip) -> bool: keyboard_mode: bool, tooltip: Gtk.Tooltip) -> bool:
texture = AlbumCover.Cache.get(self.filepath) tex = texture.CACHE[self.filepath]
tooltip.set_custom(Gtk.Picture.new_for_paintable(texture)) tooltip.set_custom(Gtk.Picture.new_for_paintable(tex))
return True return True
def do_bind(self) -> None: def do_bind(self) -> None:

View File

@ -8,7 +8,6 @@ import emmental.tracklist.row
import tests.util import tests.util
import unittest.mock import unittest.mock
from gi.repository import GObject from gi.repository import GObject
from gi.repository import Gdk
from gi.repository import Gtk from gi.repository import Gtk
from gi.repository import Adw from gi.repository import Adw
@ -193,9 +192,6 @@ class TestTrackRowWidgets(tests.util.TestCase):
def test_album_cover(self): def test_album_cover(self):
"""Test the Album Cover widget.""" """Test the Album Cover widget."""
self.assertDictEqual(emmental.tracklist.row.AlbumCover.Cache, {})
cache = emmental.tracklist.row.AlbumCover.Cache
row = emmental.tracklist.row.AlbumCover(self.listitem, "cover") row = emmental.tracklist.row.AlbumCover(self.listitem, "cover")
self.assertIsInstance(row, emmental.tracklist.row.TrackRow) self.assertIsInstance(row, emmental.tracklist.row.TrackRow)
self.assertIsInstance(row.child, Gtk.Picture) self.assertIsInstance(row.child, Gtk.Picture)
@ -206,10 +202,9 @@ class TestTrackRowWidgets(tests.util.TestCase):
row.bind() row.bind()
self.assertEqual(row.filepath, tests.util.COVER_JPG) self.assertEqual(row.filepath, tests.util.COVER_JPG)
self.assertEqual(len(cache), 1) self.assertEqual(len(emmental.texture.CACHE), 1)
self.assertIsInstance(cache[tests.util.COVER_JPG], Gdk.Texture)
self.assertEqual(row.child.get_paintable(), self.assertEqual(row.child.get_paintable(),
cache[tests.util.COVER_JPG]) emmental.texture.CACHE[tests.util.COVER_JPG])
self.assertTrue(row.child.get_has_tooltip()) self.assertTrue(row.child.get_has_tooltip())
self.album.cover = None self.album.cover = None