From c1715aa328b25b33a47cedd207a7bcd8b398554b Mon Sep 17 00:00:00 2001 From: kramo <93832451+kra-mo@users.noreply.github.com> Date: Mon, 10 Apr 2023 20:54:24 +0200 Subject: [PATCH] Reimplement pixbuf caching --- src/game.py | 8 +++++++- src/preferences.py | 28 ++++++++++++++++++++++++++++ src/utils/create_details_window.py | 8 +++++++- src/window.py | 22 +++++++++++++--------- 4 files changed, 55 insertions(+), 11 deletions(-) diff --git a/src/game.py b/src/game.py index 29df76b..c75bfff 100644 --- a/src/game.py +++ b/src/game.py @@ -117,6 +117,10 @@ class game(Gtk.Box): # pylint: disable=invalid-name save_game(self.parent_widget, data) def get_cover(self): + # If the cover is already in memory, return + if self.game_id in self.parent_widget.pixbufs: + return self.parent_widget.pixbufs[self.game_id] + # Create a new pixbuf cover_path = ( self.parent_widget.data_dir @@ -126,7 +130,9 @@ class game(Gtk.Box): # pylint: disable=invalid-name ) if cover_path.is_file(): - return GdkPixbuf.Pixbuf.new_from_file(str(cover_path)) + pixbuf = GdkPixbuf.Pixbuf.new_from_file(str(cover_path)) + self.parent_widget.pixbufs[self.game_id] = pixbuf + return pixbuf # Return the placeholder pixbuf return self.parent_widget.placeholder_pixbuf diff --git a/src/preferences.py b/src/preferences.py index 9961348..1138197 100644 --- a/src/preferences.py +++ b/src/preferences.py @@ -19,6 +19,7 @@ import os from pathlib import Path +from shutil import move from gi.repository import Adw, Gio, GLib, Gtk @@ -342,22 +343,49 @@ class PreferencesWindow(Adw.PreferencesWindow): self.file_chooser.select_folder(self.parent_widget, None, function, None) def undo_remove_all(self, _widget, _unused): + deleted_covers_dir = ( + self.parent_widget.cache_dir / "cartridges" / "deleted_covers" + ) + for game_id in self.removed_games: data = get_games(self.parent_widget, [game_id])[game_id] if "removed" in data: data.pop("removed") save_game(self.parent_widget, data) + if (deleted_covers_dir / f"{game_id}.tiff").is_file(): + move( + deleted_covers_dir / f"{game_id}.tiff", + self.parent_widget.data_dir + / "cartridges" + / "covers" + / f"{game_id}.tiff", + ) self.parent_widget.update_games(self.removed_games) self.removed_games = [] self.toast.dismiss() def remove_all_games(self, _widget): + deleted_covers_dir = ( + self.parent_widget.cache_dir / "cartridges" / "deleted_covers" + ) + deleted_covers_dir.mkdir(parents=True, exist_ok=True) + for game in get_games(self.parent_widget).values(): if "removed" not in game: self.removed_games.append(game["game_id"]) game["removed"] = True save_game(self.parent_widget, game) + if game["game_id"] in self.parent_widget.pixbufs: + move( + self.parent_widget.data_dir + / "cartridges" + / "covers" + / f'{game["game_id"]}.tiff', + deleted_covers_dir / f'{game["game_id"]}.tiff', + ) + self.parent_widget.pixbufs.pop(game["game_id"]) + self.parent_widget.update_games(self.parent_widget.games) if self.parent_widget.stack.get_visible_child() == self.parent_widget.overview: self.parent_widget.on_go_back_action(None, None) diff --git a/src/utils/create_details_window.py b/src/utils/create_details_window.py index eabb348..2f9cae3 100644 --- a/src/utils/create_details_window.py +++ b/src/utils/create_details_window.py @@ -227,7 +227,9 @@ def create_details_window(parent_widget, game_id=None): pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(path, 200, 300, False) except GLib.GError: animated_pixbuf = GdkPixbuf.PixbufAnimation.new_from_file(path) - pixbuf = animated_pixbuf.get_static_image() + pixbuf = animated_pixbuf.get_static_image().scale_simple( + 200, 300, GdkPixbuf.InterpType.BILINEAR + ) cover_button_delete_revealer.set_reveal_child(True) cover.set_pixbuf(pixbuf) @@ -315,8 +317,12 @@ def create_details_window(parent_widget, game_id=None): ( parent_widget.data_dir / "cartridges" / "covers" / f"{game_id}.tiff" ).unlink(missing_ok=True) + parent_widget.pixbufs.pop(game_id) if pixbuf: + if game_id in parent_widget.pixbufs: + parent_widget.pixbufs.pop(game_id) + save_cover(parent_widget, game_id, None, pixbuf) elif not ( parent_widget.data_dir / "cartridges" / "covers" / f"{game_id}.tiff" diff --git a/src/window.py b/src/window.py index 2ce9b57..ddcbfb6 100644 --- a/src/window.py +++ b/src/window.py @@ -21,6 +21,7 @@ import datetime import os import struct from pathlib import Path +from shutil import rmtree from gi.repository import Adw, GdkPixbuf, Gio, GLib, Gtk @@ -94,6 +95,7 @@ class CartridgesWindow(Adw.ApplicationWindow): self.hidden_filtered = {} self.previous_page = self.library_view self.toasts = {} + self.pixbufs = {} self.active_game_id = None self.loading = None self.scaled_pixbuf = None @@ -105,15 +107,17 @@ class CartridgesWindow(Adw.ApplicationWindow): self.placeholder_pixbuf = GdkPixbuf.Pixbuf.new_from_resource_at_scale( "/hu/kramo/Cartridges/library_placeholder.svg", 400, 600, False ) - current_games = get_games(self) - for current_game in current_games: - if "removed" in current_games[current_game]: - ( - self.data_dir / "cartridges" / "games" / f"{current_game}.json" - ).unlink(missing_ok=True) - ( - self.data_dir / "cartridges" / "covers" / f"{current_game}.tiff" - ).unlink(missing_ok=True) + games = get_games(self) + for game_id in games: + if "removed" in games[game_id]: + (self.data_dir / "cartridges" / "games" / f"{game_id}.json").unlink( + missing_ok=True + ) + (self.data_dir / "cartridges" / "covers" / f"{game_id}.tiff").unlink( + missing_ok=True + ) + + rmtree(self.cache_dir / "cartridges" / "deleted_covers", True) self.library.set_filter_func(self.search_filter) self.hidden_library.set_filter_func(self.hidden_search_filter)