Reimplement pixbuf caching

This commit is contained in:
kramo
2023-04-10 20:54:24 +02:00
parent d136897c8c
commit c1715aa328
4 changed files with 55 additions and 11 deletions

View File

@@ -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

View File

@@ -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)

View File

@@ -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"

View File

@@ -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)