From 35185205e3bd1dc90f7ca27c6d5afb14c1b4710e Mon Sep 17 00:00:00 2001 From: kramo <93832451+kra-mo@users.noreply.github.com> Date: Mon, 13 Mar 2023 23:09:51 +0100 Subject: [PATCH] Async image saving --- src/utils/save_cover.py | 12 ++++++++---- src/window.py | 4 ++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/utils/save_cover.py b/src/utils/save_cover.py index 63e4b2d..7fc1352 100644 --- a/src/utils/save_cover.py +++ b/src/utils/save_cover.py @@ -18,7 +18,7 @@ # SPDX-License-Identifier: GPL-3.0-or-later def save_cover(game, parent_widget, file_path, pixbuf = None, game_id = None): - from gi.repository import GdkPixbuf + from gi.repository import GdkPixbuf, Gio import os covers_dir = os.path.join(os.environ.get("XDG_DATA_HOME"), "cartridges", "covers") @@ -28,9 +28,13 @@ def save_cover(game, parent_widget, file_path, pixbuf = None, game_id = None): if game_id == None: game_id = game["game_id"] - cover_path = os.path.join(covers_dir, game_id + ".png") - if pixbuf == None: pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(file_path, 400, 600, False) - pixbuf.savev(cover_path, "png") + def cover_callback(*args): + parent_widget.busy_games.remove(game_id) + parent_widget.update_games([game_id]) + + file = Gio.File.new_for_path(os.path.join(covers_dir, game_id + ".png")) + parent_widget.busy_games.append(game_id) + pixbuf.save_to_streamv_async(file.replace(None, False, Gio.FileCreateFlags.NONE), "png", None, None, None, cover_callback) diff --git a/src/window.py b/src/window.py index a173836..04eb21d 100644 --- a/src/window.py +++ b/src/window.py @@ -74,6 +74,7 @@ class CartridgesWindow(Adw.ApplicationWindow): self.hidden_filtered = {} self.previous_page = self.library_view self.toasts = {} + self.busy_games = [] self.overview.set_measure_overlay(self.overview_box, True) self.overview.set_clip_overlay(self.overview_box, False) @@ -107,6 +108,9 @@ class CartridgesWindow(Adw.ApplicationWindow): self.games = get_games() for game_id in games: + if game_id in self.busy_games: + continue + if game_id in self.visible_widgets: self.library.remove(self.visible_widgets[game_id]) self.filtered.pop(self.visible_widgets[game_id])