[gnome-music/wip/mschraal/gtk4-v2: 62/69] Add CoverPaintable for art (FIXME)




commit e6e135c2934f94a50c8b49b16a006206cd66d3da
Author: Marinus Schraal <mschraal gnome org>
Date:   Mon Apr 12 16:12:34 2021 +0200

    Add CoverPaintable for art (FIXME)
    
    Very simple implementation.
    
    Anything Gsk related seems to fail, which limits abilities for now.

 gnomemusic/artcache.py         | 35 ++++++++++++++++--------------
 gnomemusic/coverpaintable.py   | 49 ++++++++++++++++++++++++++++++++++++++++++
 gnomemusic/widgets/artstack.py |  6 +++---
 3 files changed, 71 insertions(+), 19 deletions(-)
---
diff --git a/gnomemusic/artcache.py b/gnomemusic/artcache.py
index d512c4e1f..079597c89 100644
--- a/gnomemusic/artcache.py
+++ b/gnomemusic/artcache.py
@@ -32,6 +32,7 @@ from gi.repository import Gdk, GdkPixbuf, Gio, Gtk, GLib, GObject
 from gnomemusic.corealbum import CoreAlbum
 from gnomemusic.coreartist import CoreArtist
 from gnomemusic.coresong import CoreSong
+from gnomemusic.coverpaintable import CoverPaintable
 from gnomemusic.musiclogger import MusicLogger
 from gnomemusic.utils import ArtSize
 
@@ -113,14 +114,15 @@ class DefaultIcon(GObject.GObject):
         super().__init__()
 
     def _make_default_icon(self, icon_type, art_size, scale, round_shape):
-        icon_info = self._default_theme.lookup_icon(
-            icon_type.value, None, art_size.width / 3, scale, 0, 0)
-        icon = icon_info.load_surface()
+        # icon_info = self._default_theme.lookup_icon(
+        #     icon_type.value, None, art_size.width / 3, scale, 0, 0)
+        # icon = icon_info.load_surface()
 
-        icon_surface = _make_icon_frame(
-            icon, art_size, scale, True, round_shape=round_shape)
+        # icon_surface = _make_icon_frame(
+        #     icon, art_size, scale, True, round_shape=round_shape)
+        paintable = CoverPaintable(art_size)
 
-        return icon_surface
+        return paintable
 
     def get(self, icon_type, art_size, scale=1, round_shape=False):
         """Returns the requested symbolic icon
@@ -219,16 +221,17 @@ class ArtCache(GObject.GObject):
 
         stream.close_async(GLib.PRIORITY_LOW, None, self._close_stream, None)
 
-        surface = Gdk.cairo_surface_create_from_pixbuf(
-            pixbuf, self._scale, None)
-        if isinstance(self._coreobject, CoreArtist):
-            surface = _make_icon_frame(
-                surface, self._size, self._scale, round_shape=True)
-        elif (isinstance(self._coreobject, CoreAlbum)
-                or isinstance(self._coreobject, CoreSong)):
-            surface = _make_icon_frame(surface, self._size, self._scale)
-
-        self.emit("result", surface)
+        # surface = Gdk.cairo_surface_create_from_pixbuf(
+        #     pixbuf, self._scale, None)
+        # if isinstance(self._coreobject, CoreArtist):
+        #     surface = _make_icon_frame(
+        #         surface, self._size, self._scale, round_shape=True)
+        # elif (isinstance(self._coreobject, CoreAlbum)
+        #         or isinstance(self._coreobject, CoreSong)):
+        #     surface = _make_icon_frame(surface, self._size, self._scale)
+        paintable = CoverPaintable(self._size)
+
+        self.emit("result", paintable)
 
     def _close_stream(self, stream, result, data):
         try:
diff --git a/gnomemusic/coverpaintable.py b/gnomemusic/coverpaintable.py
new file mode 100644
index 000000000..2ee97b30f
--- /dev/null
+++ b/gnomemusic/coverpaintable.py
@@ -0,0 +1,49 @@
+import gi
+gi.require_versions({"Gdk": "4.0", "Gtk": "4.0"})
+# from gi.repository import Gsk, Gtk, GObject, Graphene, Gdk
+from gi.repository import Gtk, GObject, Graphene, Gdk
+
+
+class CoverPaintable(GObject.GObject, Gdk.Paintable):
+
+    __gtype_name__ = "CoverPaintable"
+
+    def __init__(self, art_size, texture=None):
+        super().__init__()
+
+        self._texture = texture
+        self._art_size = art_size
+
+    def do_snapshot(self, snapshot, width, height):
+        w = width
+        h = height
+
+        if self._texture is not None:
+            snapshot.translate(Graphene.Point().init(width / 2, height / 2))
+
+            rect = Graphene.Rect().init(-(w / 2), -(h / 2), w, h)
+            rect2 = Graphene.Rect().init(-50, -50, 100, 100)
+
+            # Anything Gsk related seems to be failing, no rounded
+            # clips for now. Related: pygobject#471
+            #
+            # gskrr = Gsk.RoundedRect().init_from_rect(rect2, 10)
+            # snapshot.push_rounded_clip(gskrr)
+
+            snapshot.push_clip(rect2)
+            snapshot.append_texture(self._texture, rect)
+            snapshot.pop()
+        else:
+            theme = Gtk.IconTheme.new()
+            icon_pt = theme.lookup_icon(
+                "folder-music-symbolic", None, w, 1, 0, 0)
+            icon_pt.snapshot(snapshot, w, h)
+
+    def do_get_flags(self):
+        return Gdk.PaintableFlags.SIZE | Gdk.PaintableFlags.CONTENTS
+
+    def do_get_intrinsic_height(self):
+        return self._art_size.height
+
+    def do_get_intrinsic_width(self):
+        return self._art_size.width
diff --git a/gnomemusic/widgets/artstack.py b/gnomemusic/widgets/artstack.py
index 772e359db..7235bde21 100644
--- a/gnomemusic/widgets/artstack.py
+++ b/gnomemusic/widgets/artstack.py
@@ -107,12 +107,12 @@ class ArtStack(Gtk.Stack):
 
         self._cache.query(coreobject)
 
-    def _on_cache_result(self, cache, surface):
+    def _on_cache_result(self, cache, paintable):
         if self.props.visible_child_name == "B":
-            self._cover_a.props.surface = surface
+            self._cover_a.props.paintable = paintable
             self.props.visible_child_name = "A"
         else:
-            self._cover_b.props.surface = surface
+            self._cover_b.props.paintable = paintable
             self.props.visible_child_name = "B"
 
     def _on_destroy(self, widget):


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]