From bc0d33f96631e9a7f26e08c23d220d3dc475ff8d Mon Sep 17 00:00:00 2001 From: kramo <93832451+kra-mo@users.noreply.github.com> Date: Mon, 17 Apr 2023 19:20:29 +0200 Subject: [PATCH] More reliance on GLib --- src/game.py | 32 +++++++++++--------------------- src/importers/itch_importer.py | 3 ++- src/utils/save_cover.py | 4 +++- src/window.py | 29 +++++++++++++++-------------- 4 files changed, 31 insertions(+), 37 deletions(-) diff --git a/src/game.py b/src/game.py index 5b0e0c2..72c38c1 100644 --- a/src/game.py +++ b/src/game.py @@ -19,11 +19,8 @@ import json import os -import shlex # pylint: disable=unused-import -import subprocess -import sys -from gi.repository import Gio, Gtk +from gi.repository import Gio, GLib, Gtk from .game_cover import GameCover from .save_game import save_game @@ -45,10 +42,14 @@ class Game(Gtk.Box): game_options = Gtk.Template.Child() hidden_game_options = Gtk.Template.Child() + loading = 0 + def __init__(self, win, data, **kwargs): super().__init__(**kwargs) self.win = win + self.app = win.get_application() + self.added = data["added"] self.executable = data["executable"] self.game_id = data["game_id"] @@ -59,7 +60,6 @@ class Game(Gtk.Box): self.removed = "removed" in data self.blacklisted = "blacklisted" in data - self.loading = 0 self.title.set_label(self.name) if self.game_id in self.win.game_covers: @@ -90,25 +90,15 @@ class Game(Gtk.Box): def launch(self): # Generate launch arguments, either list (no shell) or a string (for shell). - args = ( - ["flatpak-spawn", "--host", *self.executable] # Flatpak + argv = ( + ("flatpak-spawn", "--host", *self.executable) # Flatpak if os.getenv("FLATPAK_ID") == "hu.kramo.Cartridges" - else shlex.join( - self.executable - ) # Windows (We need shell to support its "open" built-in). - if os.name == "nt" - else self.executable # Linux/Others + else self.executable # Others ) - # The host environment vars are automatically passed through by Popen. - subprocess.Popen( - args, - shell=isinstance(args, str), - start_new_session=True, - creationflags=subprocess.CREATE_NEW_PROCESS_GROUP if os.name == "nt" else 0, - ) + GLib.spawn_async(argv, flags=GLib.SpawnFlags.SEARCH_PATH) if Gio.Settings.new("hu.kramo.Cartridges").get_boolean("exit-after-launch"): - sys.exit() + self.app.quit() def toggle_hidden(self): data = json.loads( @@ -139,7 +129,7 @@ class Game(Gtk.Box): def launch_game(self, _widget, *_unused): self.win.set_active_game(None, None, self.game_id) - self.win.get_application().on_launch_game_action(None) + self.app.on_launch_game_action(None) def cover_button_clicked(self, _widget): if self.win.schema.get_boolean("cover-launches-game"): diff --git a/src/importers/itch_importer.py b/src/importers/itch_importer.py index 1cba187..9f92c37 100644 --- a/src/importers/itch_importer.py +++ b/src/importers/itch_importer.py @@ -83,6 +83,7 @@ def get_game(task, current_time, win, row): GdkPixbuf.InterpType.BILINEAR, 255, ) + else: game_cover = None @@ -109,7 +110,7 @@ def get_games_async(win, rows, importer): # No need for an if statement as final_value would be None for games we don't want to save importer.save_game( final_values[0], - resize_cover(win, pixbuf=final_values[1]) if final_values[1] else None, + resize_cover(win, pixbuf=final_values[1]), ) for row in rows: diff --git a/src/utils/save_cover.py b/src/utils/save_cover.py index e8f28ac..e2636b1 100644 --- a/src/utils/save_cover.py +++ b/src/utils/save_cover.py @@ -26,6 +26,9 @@ from PIL import Image, ImageSequence def resize_cover(win, cover_path=None, pixbuf=None): + if not cover_path and not pixbuf: + return + if pixbuf: cover_path = Path(Gio.File.new_tmp("XXXXXX.tiff")[0].get_path()) pixbuf.savev(str(cover_path), "tiff", ["compression"], ["1"]) @@ -48,7 +51,6 @@ def resize_cover(win, cover_path=None, pixbuf=None): # This might not be necessary in the future # https://github.com/python-pillow/Pillow/issues/2663 if image.mode not in ("RGB", "RGBA"): - print(image.mode) image = image.convert("RGBA") tmp_path = Path(Gio.File.new_tmp("XXXXXX.tiff")[0].get_path()) diff --git a/src/window.py b/src/window.py index d1cbe06..8b4fd4d 100644 --- a/src/window.py +++ b/src/window.py @@ -19,7 +19,7 @@ import datetime import os -import struct +from struct import unpack_from from pathlib import Path from shutil import rmtree @@ -69,9 +69,22 @@ class CartridgesWindow(Adw.ApplicationWindow): hidden_search_entry = Gtk.Template.Child() hidden_search_button = Gtk.Template.Child() + games = {} + game_covers = {} + visible_widgets = {} + hidden_widgets = {} + filtered = {} + hidden_filtered = {} + toasts = {} + active_game_id = None + scaled_pixbuf = None + details_view_game_cover = None + def __init__(self, **kwargs): super().__init__(**kwargs) + self.previous_page = self.library_view + self.data_dir = ( Path(os.getenv("XDG_DATA_HOME")) if "XDG_DATA_HOME" in os.environ @@ -91,18 +104,6 @@ class CartridgesWindow(Adw.ApplicationWindow): self.games_dir = self.data_dir / "cartridges" / "games" self.covers_dir = self.data_dir / "cartridges" / "covers" - self.games = {} - self.game_covers = {} - self.visible_widgets = {} - self.hidden_widgets = {} - self.filtered = {} - self.hidden_filtered = {} - self.previous_page = self.library_view - self.toasts = {} - self.active_game_id = None - self.scaled_pixbuf = None - self.details_view_game_cover = None - self.schema = Gio.Settings.new("hu.kramo.Cartridges") scale_factor = max( monitor.get_scale_factor() @@ -333,7 +334,7 @@ class CartridgesWindow(Adw.ApplicationWindow): colors = set() for index in range(6): - colors.add(struct.unpack_from("BBBB", pixels, offset=index * channels)) + colors.add(unpack_from("BBBB", pixels, offset=index * channels)) dark_theme = style_manager.get_dark()