51 lines
1.7 KiB
Python
51 lines
1.7 KiB
Python
# Copyright 2022 (c) Anna Schumaker.
|
|
"""Our custom Album Art widget."""
|
|
import pathlib
|
|
from gi.repository import GObject
|
|
from gi.repository import Gtk
|
|
from .. import gsetup
|
|
|
|
FALLBACK_RESOURCE = f"{gsetup.RESOURCE_ICONS}/emmental.svg"
|
|
|
|
|
|
class Artwork(Gtk.Picture):
|
|
"""Our custom Album Art widget takes a pathlib.Path."""
|
|
|
|
def __init__(self):
|
|
"""Initialize the Album Art widget."""
|
|
super().__init__(content_fit=Gtk.ContentFit.CONTAIN,
|
|
margin_top=6, margin_bottom=6,
|
|
margin_start=6, margin_end=6,
|
|
halign=Gtk.Align.CENTER, valign=Gtk.Align.CENTER)
|
|
self._fullsize = Gtk.Picture(content_fit=Gtk.ContentFit.FILL)
|
|
|
|
self._popover = Gtk.Popover(child=self._fullsize)
|
|
self._popover.set_parent(self)
|
|
|
|
self._clicked = Gtk.GestureClick()
|
|
self._clicked.connect("released", self.clicked)
|
|
self.add_controller(self._clicked)
|
|
|
|
self.add_css_class("card")
|
|
self.filepath = None
|
|
|
|
@GObject.Property(type=GObject.TYPE_PYOBJECT)
|
|
def filepath(self) -> pathlib.Path:
|
|
"""Get the current artwork path."""
|
|
name = self.get_file().get_parse_name()
|
|
return None if name.startswith("resource:") else pathlib.Path(name)
|
|
|
|
@filepath.setter
|
|
def filepath(self, path: pathlib.Path) -> None:
|
|
if path is not None:
|
|
self.set_filename(str(path))
|
|
self._fullsize.set_filename(str(path))
|
|
else:
|
|
self.set_resource(FALLBACK_RESOURCE)
|
|
self._fullsize.set_resource(FALLBACK_RESOURCE)
|
|
|
|
def clicked(self, gesture: Gtk.GestureClick, n_press: int,
|
|
x: float, y: float) -> None:
|
|
"""Handle a click event."""
|
|
self._popover.popup()
|