Reimplement animated covers with Pillow
This commit is contained in:
@@ -17,28 +17,68 @@
|
||||
#
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
from gi.repository import GdkPixbuf
|
||||
from gi.repository import GdkPixbuf, GLib
|
||||
|
||||
from .save_cover import resize_animation
|
||||
|
||||
|
||||
class GameCover:
|
||||
pixbuf = None
|
||||
path = None
|
||||
animation = None
|
||||
|
||||
placeholder_pixbuf = GdkPixbuf.Pixbuf.new_from_resource_at_scale(
|
||||
"/hu/kramo/Cartridges/library_placeholder.svg", 400, 600, False
|
||||
)
|
||||
|
||||
def __init__(self, picture, pixbuf=None, path=None):
|
||||
self.picture = picture
|
||||
self.pixbuf = pixbuf
|
||||
self.new_pixbuf(pixbuf, path)
|
||||
|
||||
def new_pixbuf(self, pixbuf=None, path=None):
|
||||
self.animation = None
|
||||
self.pixbuf = None
|
||||
self.path = None
|
||||
|
||||
if pixbuf:
|
||||
self.pixbuf = pixbuf
|
||||
|
||||
if path:
|
||||
self.pixbuf = GdkPixbuf.Pixbuf.new_from_file(path)
|
||||
if str(path).rsplit(".", maxsplit=1)[-1] == "gif":
|
||||
self.path = resize_animation(path)
|
||||
self.animation = GdkPixbuf.PixbufAnimation.new_from_file(str(self.path))
|
||||
self.anim_iter = self.animation.get_iter()
|
||||
self.update_animation(self.animation)
|
||||
else:
|
||||
self.path = path
|
||||
self.pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(
|
||||
str(path), 200, 300, False
|
||||
)
|
||||
|
||||
if not self.pixbuf:
|
||||
self.pixbuf = self.placeholder_pixbuf
|
||||
|
||||
self.update_pixbuf()
|
||||
if not self.animation:
|
||||
self.set_pixbuf(self.pixbuf)
|
||||
|
||||
def get_pixbuf(self):
|
||||
return self.pixbuf
|
||||
return self.animation.get_static_image() if self.animation else self.pixbuf
|
||||
|
||||
def update_pixbuf(self):
|
||||
self.picture.set_pixbuf(self.pixbuf)
|
||||
def get_animation(self):
|
||||
return self.path if self.animation else None
|
||||
|
||||
def set_pixbuf(self, pixbuf):
|
||||
self.picture.set_pixbuf(pixbuf)
|
||||
|
||||
def update_animation(self, animation):
|
||||
if self.animation == animation:
|
||||
self.anim_iter.advance()
|
||||
|
||||
self.set_pixbuf(self.anim_iter.get_pixbuf())
|
||||
|
||||
delay_time = self.anim_iter.get_delay_time()
|
||||
GLib.timeout_add(
|
||||
20 if delay_time < 20 else delay_time,
|
||||
self.update_animation,
|
||||
animation,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user