# game_cover.py # # Copyright 2022-2023 kramo # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # # SPDX-License-Identifier: GPL-3.0-or-later from gi.repository import GdkPixbuf, Gio, GLib class GameCover: pixbuf = None path = None animation = None anim_iter = None placeholder_pixbuf = GdkPixbuf.Pixbuf.new_from_resource_at_scale( "/hu/kramo/Cartridges/library_placeholder.svg", 400, 600, False ) def __init__(self, pictures, path=None): self.pictures = pictures self.new_pixbuf(path) # Wrap the function in another one as Gio.Task.run_in_thread does not allow for passing args def create_func(self, path): self.animation = GdkPixbuf.PixbufAnimation.new_from_file(str(path)) self.anim_iter = self.animation.get_iter() def wrapper(task, *_unused): self.update_animation((task, self.animation)) return wrapper def new_pixbuf(self, path=None): self.animation = None self.pixbuf = None self.path = path if path: if str(path).rsplit(".", maxsplit=1)[-1] == "gif": task = Gio.Task.new() task.run_in_thread(self.create_func(self.path)) else: self.pixbuf = GdkPixbuf.Pixbuf.new_from_file(str(path)) if not self.animation: self.set_pixbuf(self.pixbuf) def get_pixbuf(self): return self.animation.get_static_image() if self.animation else self.pixbuf def get_animation(self): return self.path if self.animation else None def add_picture(self, picture): self.pictures.add(picture) if not self.animation: self.set_pixbuf(self.pixbuf) def set_pixbuf(self, pixbuf): self.pictures.discard( picture for picture in self.pictures if not picture.is_visible() ) if not self.pictures: self.animation = None else: for picture in self.pictures: if not pixbuf: pixbuf = self.placeholder_pixbuf picture.set_pixbuf(pixbuf) def update_animation(self, data): if self.animation == data[1]: 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, data, ) else: data[0].return_value(False)