emmental/emmental/nowplaying/artwork.py

50 lines
1.8 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.Frame):
"""Our custom Album Art widget that draws a border around a picture."""
def __init__(self):
"""Initialize the Album Art widget."""
super().__init__(margin_top=6, margin_bottom=6, margin_start=6,
margin_end=6, halign=Gtk.Align.CENTER,
valign=Gtk.Align.CENTER)
self._picture = Gtk.Picture(content_fit=Gtk.ContentFit.CONTAIN)
self._fullsize = Gtk.Picture(content_fit=Gtk.ContentFit.FILL)
self._popover = Gtk.Popover(child=self._fullsize)
self._clicked = Gtk.GestureClick()
self._clicked.connect("released", self.clicked)
self.add_controller(self._clicked)
self._popover.set_parent(self)
self.set_child(self._picture)
self.filepath = None
@GObject.Property(type=GObject.TYPE_PYOBJECT)
def filepath(self) -> pathlib.Path:
"""Get the current artwork path."""
name = self._picture.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._picture.set_filename(str(path))
self._fullsize.set_filename(str(path))
else:
self._picture.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()