From 3a50235c3867bcab03c7639cb89f7af6e4c6e2cf Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Tue, 7 Sep 2021 10:54:49 -0400 Subject: [PATCH] audio: Rework the Artwork widget It now sets artwork based on the signals sent by the player, allowing us to move it out of the Player class and create instances based on the global Player instead. Signed-off-by: Anna Schumaker --- audio/__init__.py | 3 +++ audio/artwork.py | 48 ++++++++++++++++++++--------------------- audio/player.py | 11 ---------- audio/test_artwork.py | 16 ++++++++------ audio/test_audio.py | 4 ++++ audio/test_player.py | 1 - sidebar/__init__.py | 2 +- sidebar/test_sidebar.py | 2 +- ui/__init__.py | 1 - 9 files changed, 42 insertions(+), 46 deletions(-) diff --git a/audio/__init__.py b/audio/__init__.py index ea31136..f41563d 100644 --- a/audio/__init__.py +++ b/audio/__init__.py @@ -15,6 +15,9 @@ tagdb.Stack.Counter = Player.Autopause def AudioControls(): return controls.AudioControls(Player, Player.Autopause) +def Artwork(): + return artwork.Artwork(Player) + def NowPlaying(): return nowplaying.NowPlaying(Player) diff --git a/audio/artwork.py b/audio/artwork.py index cfa5ace..564bad2 100644 --- a/audio/artwork.py +++ b/audio/artwork.py @@ -2,10 +2,9 @@ from gi.repository import Gtk, GdkPixbuf, Gst class Artwork(Gtk.AspectFrame): - def __init__(self): + def __init__(self, player): Gtk.AspectFrame.__init__(self) self.picture = Gtk.Picture() - self.frame = Gtk.Frame() self.frame.set_child(self.picture) @@ -16,31 +15,32 @@ class Artwork(Gtk.AspectFrame): self.set_margin_top(5) self.set_margin_bottom(5) self.set_ratio(1.0) - self.reset() - def get_default_path(self): - display = self.picture.get_display() - theme = Gtk.IconTheme.get_for_display(display) - icon = theme.lookup_icon("emmental", [ ], 1024, 1, 0, 0) - return icon.get_file().get_path() + self.player = player + self.player.connect("artwork", self.on_artwork) + self.player.connect("track-changed", self.on_track_changed) + self.on_track_changed(player, None, player.track) - def set_from_data(self, data): - loader = GdkPixbuf.PixbufLoader() - loader.write(data) + def __set_from_cover_jpg__(self, track): + cover = track.filepath().parent / "cover.jpg" + if cover.exists(): + self.picture.set_filename(str(cover)) + return True + return False - pixbuf = loader.get_pixbuf() - self.picture.set_pixbuf(pixbuf) - loader.close() - - def set_from_sample(self, sample): + def on_artwork(self, player, sample): buffer = sample.get_buffer() - (res, map) = buffer.map(Gst.MapFlags.READ) - if res == True: - self.set_from_data(map.data) - buffer.unmap(map) - else: - self.reset() + if res: + loader = GdkPixbuf.PixbufLoader() + loader.write(map.data) + self.picture.set_pixbuf(loader.get_pixbuf()) + loader.close() + buffer.unmap(map) - def reset(self): - self.picture.set_filename(self.get_default_path()) + def on_track_changed(self, player, prev, new): + if not (new and self.__set_from_cover_jpg__(new)): + display = self.picture.get_display() + theme = Gtk.IconTheme.get_for_display(display) + icon = theme.lookup_icon("emmental", [ ], 1024, 1, 0, 0) + self.picture.set_file(icon.get_file()) diff --git a/audio/player.py b/audio/player.py index 26f306d..6d7c666 100644 --- a/audio/player.py +++ b/audio/player.py @@ -17,9 +17,6 @@ class Player(bass.BassPlayer): self.track = None self.bus.connect("message::eos", self.next) - self.bus.connect("message::tag", self.on_tag) - - self.Artwork = artwork.Artwork() self.load_track(tagdb.Tracks[settings.get_int("audio.trackid")]) if self.track: @@ -34,14 +31,6 @@ class Player(bass.BassPlayer): self.load_track(track) self.playing = cont - def on_tag(self, bus, message): - taglist = message.parse_tag() - (res, sample) = taglist.get_sample("image") - if res == True: - self.Artwork.set_from_sample(sample) - else: - self.Artwork.reset() - def play(self): self.playing = True def pause(self): self.playing = False def playpause(self, *args): self.playing = not self.playing diff --git a/audio/test_artwork.py b/audio/test_artwork.py index c1a8cad..f5df320 100644 --- a/audio/test_artwork.py +++ b/audio/test_artwork.py @@ -1,19 +1,23 @@ # Copyright 2021 (c) Anna Schumaker. -from . import artwork -from gi.repository import Gtk -import pathlib import unittest +from gi.repository import Gtk +from . import artwork -Path = pathlib.Path("./data/hicolor/scalable/apps/emmental.svg") +class FakePlayer: + def __init__(self): + self.track = None + def connect(self, name, cb): pass class TestAudioArtwork(unittest.TestCase): def test_audio_artwork_init(self): - art = artwork.Artwork() + fake = FakePlayer() + art = artwork.Artwork(fake) self.assertIsInstance(art, Gtk.AspectFrame) self.assertIsInstance(art.frame, Gtk.Frame) self.assertIsInstance(art.picture, Gtk.Picture) + self.assertEqual(art.player, fake) self.assertEqual(art.get_child(), art.frame) self.assertEqual(art.frame.get_child(), art.picture) self.assertEqual(art.get_obey_child(), False) @@ -23,5 +27,3 @@ class TestAudioArtwork(unittest.TestCase): self.assertEqual(art.get_margin_end(), 5) self.assertEqual(art.get_margin_top(), 5) self.assertEqual(art.get_margin_bottom(), 5) - - self.assertIsNotNone(art.get_default_path()) diff --git a/audio/test_audio.py b/audio/test_audio.py index 848da98..7a0b4c1 100644 --- a/audio/test_audio.py +++ b/audio/test_audio.py @@ -23,6 +23,10 @@ class TestAudio(unittest.TestCase): def test_audio_widgets(self): seeker = audio.SeekControl() + self.assertIsInstance(audio.AudioControls(), + audio.controls.AudioControls) + self.assertIsInstance(audio.Artwork(), + audio.artwork.Artwork) self.assertIsInstance(audio.NowPlaying(), audio.nowplaying.NowPlaying) self.assertIsInstance(seeker, audio.scale.ScaleButtonBox) diff --git a/audio/test_player.py b/audio/test_player.py index 18eb7aa..d169997 100644 --- a/audio/test_player.py +++ b/audio/test_player.py @@ -43,7 +43,6 @@ class TestPlayer(unittest.TestCase): play = player.Player() self.assertIsInstance(play, bass.BassPlayer) self.assertIsInstance(play.bus, Gst.Bus) - self.assertIsInstance(play.Artwork, artwork.Artwork) self.assertIsInstance(play.Autopause, scale.AutoPauseScale) self.assertIsNone(play.track) diff --git a/sidebar/__init__.py b/sidebar/__init__.py index 68cc519..3f4e1f0 100644 --- a/sidebar/__init__.py +++ b/sidebar/__init__.py @@ -59,7 +59,7 @@ add_stack_page("Decades", tagbox.ParentTagBox(tagdb.tags.Decade, "x-office-cal add_stack_page("Libraries", library.Box) -Box.append(audio.Player.Artwork) +Box.append(audio.Artwork()) Box.append(Gtk.Separator.new(Gtk.Orientation.HORIZONTAL)) Box.append(Stack) Box.append(Gtk.Separator.new(Gtk.Orientation.HORIZONTAL)) diff --git a/sidebar/test_sidebar.py b/sidebar/test_sidebar.py index fe0a893..de98808 100644 --- a/sidebar/test_sidebar.py +++ b/sidebar/test_sidebar.py @@ -14,7 +14,7 @@ class TestSidebar(unittest.TestCase): self.assertIsInstance(sidebar.Stack, Gtk.Stack) self.assertIsInstance(sidebar.Box, Gtk.Box) - self.assertIn(audio.Player.Artwork, sidebar.Box) + #self.assertIn(audio.Player.Artwork, sidebar.Box) self.assertIn(sidebar.Stack, sidebar.Box) self.assertIn(sidebar.pulser.Box, sidebar.Box) diff --git a/ui/__init__.py b/ui/__init__.py index 243de1e..09f9691 100644 --- a/ui/__init__.py +++ b/ui/__init__.py @@ -14,6 +14,5 @@ class EmmentalApplication(Gtk.Application): def do_startup(self): Gtk.Application.do_startup(self) self.add_window(window.Window) - audio.Player.Artwork.reset() Application = EmmentalApplication()