diff --git a/src/game.py b/src/game.py index 9db8506..aabcfb0 100644 --- a/src/game.py +++ b/src/game.py @@ -19,8 +19,9 @@ import json import os +from time import time -from gi.repository import Gio, GLib, Gtk +from gi.repository import Adw, Gio, GLib, Gtk from .game_cover import GameCover @@ -69,11 +70,11 @@ class Game(Gtk.Box): self.event_contoller_motion = Gtk.EventControllerMotion.new() self.add_controller(self.event_contoller_motion) - self.event_contoller_motion.connect("enter", self.show_play) - self.event_contoller_motion.connect("leave", self.hide_play) + self.event_contoller_motion.connect("enter", self.toggle_play, False) + self.event_contoller_motion.connect("leave", self.toggle_play, None, None) - self.cover_button.connect("clicked", self.cover_button_clicked) - self.play_button.connect("clicked", self.play_button_clicked) + self.cover_button.connect("clicked", self.main_button_clicked, False) + self.play_button.connect("clicked", self.main_button_clicked, True) self.win.schema.connect("changed", self.schema_changed) @@ -89,7 +90,9 @@ class Game(Gtk.Box): self.title.set_label(self.name) - self.menu_button.get_popover().connect("notify::visible", self.hide_play) + self.menu_button.get_popover().connect( + "notify::visible", self.toggle_play, None + ) self.menu_button.get_popover().connect( "notify::visible", self.win.set_active_game, self.game_id ) @@ -140,8 +143,28 @@ class Game(Gtk.Box): self.update() + def create_toast(self, title, action=None): + toast = Adw.Toast.new(title.format(self.name)) + toast.set_priority(Adw.ToastPriority.HIGH) + + if action: + toast.set_button_label(_("Undo")) + toast.connect( + "button-clicked", self.win.on_undo_action, self.game_id, action + ) + + if (self.game_id, action) in self.win.toasts.keys(): + # Dismiss the toast if there already is one + self.win.toasts[(self.game_id, action)].dismiss() + + self.win.toasts[(self.game_id, action)] = toast + + self.win.toast_overlay.add_toast(toast) + def launch(self): - # Generate launch arguments, either list (no shell) or a string (for shell). + self.last_played = int(time()) + self.save() + argv = ( ("flatpak-spawn", "--host", *self.executable) # Flatpak if os.getenv("FLATPAK_ID") == "hu.kramo.Cartridges" @@ -152,10 +175,41 @@ class Game(Gtk.Box): if Gio.Settings.new("hu.kramo.Cartridges").get_boolean("exit-after-launch"): self.app.quit() - def toggle_hidden(self): + # The variable is the title of the game + self.create_toast(_("{} launched")) + + def toggle_hidden(self, toast): self.hidden = not self.hidden self.save() + if self.win.stack.get_visible_child() == self.win.details_view: + self.win.on_go_back_action() + + if toast: + self.create_toast( + # The variable is the title of the game + (_("{} hidden") if self.hidden else _("{} unhidden")).format(self.name), + "hide", + ) + + def remove_game(self): + # Add "removed=True" to the game properties so it can be deleted on next init + self.removed = True + self.save() + + if self.win.stack.get_visible_child() == self.win.details_view: + self.win.on_go_back_action() + + # The variable is the title of the game + self.create_toast(_("{} removed").format(self.name), "remove") + + def set_loading(self, state): + self.loading += state + loading = self.loading > 0 + + self.cover.set_opacity(int(not loading)) + self.spinner.set_spinning(loading) + def get_cover_path(self): cover_path = self.win.covers_dir / f"{self.game_id}.gif" if cover_path.is_file(): @@ -167,44 +221,24 @@ class Game(Gtk.Box): return None - def show_play(self, _widget, *_unused): - self.play_revealer.set_reveal_child(True) - self.title_revealer.set_reveal_child(False) - - def hide_play(self, _widget, *_unused): + def toggle_play(self, _widget, _prop1, _prop2, state=True): if not self.menu_button.get_active(): - self.play_revealer.set_reveal_child(False) - self.title_revealer.set_reveal_child(True) + self.title_revealer.set_reveal_child(state) + self.play_revealer.set_reveal_child(not state) - def launch_game(self, _widget, *_unused): - self.win.set_active_game(None, None, self.game_id) - self.app.on_launch_game_action(None) - - def cover_button_clicked(self, _widget): - if self.win.schema.get_boolean("cover-launches-game"): - self.launch_game(None) + def main_button_clicked(self, _widget, button): + if self.win.schema.get_boolean("cover-launches-game") ^ button: + self.launch() else: self.win.show_details_view(None, self.game_id) - def play_button_clicked(self, _widget): - if self.win.schema.get_boolean("cover-launches-game"): - self.win.show_details_view(None, self.game_id) - else: - self.launch_game(None) - def set_play_label(self): - if self.win.schema.get_boolean("cover-launches-game"): - self.play_button.set_label(_("Details")) - else: - self.play_button.set_label(_("Play")) + self.play_button.set_label( + _("Details") + if self.win.schema.get_boolean("cover-launches-game") + else _("Play") + ) def schema_changed(self, _settings, key): if key == "cover-launches-game": self.set_play_label() - - def set_loading(self, state): - self.loading += state - loading = self.loading > 0 - - self.cover.set_opacity(int(not loading)) - self.spinner.set_spinning(loading) diff --git a/src/main.py b/src/main.py index 730dd56..4a6d963 100644 --- a/src/main.py +++ b/src/main.py @@ -18,7 +18,6 @@ # SPDX-License-Identifier: GPL-3.0-or-later import sys -import time import gi @@ -146,46 +145,13 @@ class CartridgesApplication(Adw.Application): getattr(win, expander_row).set_expanded(True) win.present() - def on_launch_game_action(self, _widget, _callback=None): - # Launch the game and update the last played value - game = self.win.games[self.win.active_game_id] + def on_launch_game_action(self, _widget=None, _callback=None): + self.win.games[self.win.active_game_id].launch() - game.last_played = int(time.time()) - game.save() - game.launch() - - title = game.name - # The variable is the title of the game - toast = Adw.Toast.new(_("{} launched").format(title)) - toast.set_priority(Adw.ToastPriority.HIGH) - self.win.toast_overlay.add_toast(toast) - - def on_hide_game_action(self, _widget, _callback=None, game_id=None, toast=True): - if not game_id: - game_id = self.win.active_game_id - - if self.win.stack.get_visible_child() == self.win.details_view: - self.win.on_go_back_action(None, None) - self.win.games[game_id].toggle_hidden() - - if not toast: - return - - title = self.win.games[game_id].name - if self.win.games[game_id].hidden: - # The variable is the title of the game - toast = Adw.Toast.new(_("{} hidden").format(title)) - else: - # The variable is the title of the game - toast = Adw.Toast.new(_("{} unhidden").format(title)) - toast.set_button_label(_("Undo")) - toast.connect("button-clicked", self.win.on_undo_action, game_id, "hide") - toast.set_priority(Adw.ToastPriority.HIGH) - if (game_id, "hide") in self.win.toasts.keys(): - # Dismiss the toast if there already is one - self.win.toasts[(game_id, "hide")].dismiss() - self.win.toasts[(game_id, "hide")] = toast - self.win.toast_overlay.add_toast(toast) + def on_hide_game_action( + self, _widget=None, _callback=None, game_id=None, toast=True + ): + self.win.games[game_id or self.win.active_game_id].toggle_hidden(toast) def on_edit_game_action(self, _widget, _callback=None): create_details_window(self.win, self.win.active_game_id) @@ -219,28 +185,12 @@ class CartridgesApplication(Adw.Application): self.win.importer.queue = 1 self.win.importer.save_game() - def on_remove_game_action(self, _widget, _callback=None): - # Add "removed=True" to the game properties so it can be deleted on next init - game = self.win.games[self.win.active_game_id] - - game.removed = True - game.save() - - if self.win.stack.get_visible_child() == self.win.details_view: - self.win.on_go_back_action(None, None) - - title = game.name - # The variable is the title of the game - toast = Adw.Toast.new(_("{} removed").format(title)) - toast.set_button_label(_("Undo")) - toast.connect("button-clicked", self.win.on_undo_action, game.game_id, "remove") - toast.set_priority(Adw.ToastPriority.HIGH) - self.win.toasts[(game.game_id, "remove")] = toast - self.win.toast_overlay.add_toast(toast) + def on_remove_game_action(self, _widget=None, _callback=None): + self.win.games[self.win.active_game_id].remove_game() def on_remove_game_details_view_action(self, _widget, _callback=None): if self.win.stack.get_visible_child() == self.win.details_view: - self.on_remove_game_action(None) + self.on_remove_game_action() def on_quit_action(self, _widget, _callback=None): self.quit() diff --git a/src/preferences.py b/src/preferences.py index 9aa6a45..fda314e 100644 --- a/src/preferences.py +++ b/src/preferences.py @@ -348,6 +348,6 @@ class PreferencesWindow(Adw.PreferencesWindow): game.save() if self.win.stack.get_visible_child() == self.win.details_view: - self.win.on_go_back_action(None, None) + self.win.on_go_back_action() self.add_toast(self.toast) diff --git a/src/utils/create_details_window.py b/src/utils/create_details_window.py index 59f251a..dd43132 100644 --- a/src/utils/create_details_window.py +++ b/src/utils/create_details_window.py @@ -19,7 +19,7 @@ import os import shlex -import time +from time import time from gi.repository import Adw, Gio, GLib, GObject, Gtk from PIL import Image @@ -289,7 +289,7 @@ def create_details_window(win, game_id=None): values["game_id"] = game_id values["hidden"] = False values["source"] = "imported" - values["added"] = int(time.time()) + values["added"] = int(time()) values["last_played"] = 0 else: diff --git a/src/window.py b/src/window.py index 9344209..53a465d 100644 --- a/src/window.py +++ b/src/window.py @@ -233,7 +233,7 @@ class CartridgesWindow(Adw.ApplicationWindow): self.details_view_game_cover.add_picture(self.details_view_cover) self.scaled_pixbuf = ( - self.details_view_game_cover.pixbuf + self.details_view_game_cover.get_pixbuf() or self.details_view_game_cover.placeholder_pixbuf ).scale_simple(2, 3, GdkPixbuf.InterpType.BILINEAR) self.details_view_blurred_cover.set_pixbuf(self.scaled_pixbuf) @@ -320,7 +320,7 @@ class CartridgesWindow(Adw.ApplicationWindow): return ((get_value(0) > get_value(1)) ^ order) * 2 - 1 - def on_go_back_action(self, _widget, _unused, _x=None, _y=None): + def on_go_back_action(self, *_unused): if self.stack.get_visible_child() == self.hidden_library_view: self.on_show_library_action(None, None) elif self.stack.get_visible_child() == self.details_view: @@ -380,7 +380,7 @@ class CartridgesWindow(Adw.ApplicationWindow): def on_escape_action(self, _widget, _unused): if self.stack.get_visible_child() == self.details_view: - self.on_go_back_action(None, None) + self.on_go_back_action() return if self.stack.get_visible_child() == self.library_view: search_bar = self.search_bar @@ -407,9 +407,7 @@ class CartridgesWindow(Adw.ApplicationWindow): return if undo == "hide": - self.get_application().on_hide_game_action( - None, game_id=game_id, toast=False - ) + self.get_application().on_hide_game_action(game_id=game_id, toast=False) elif undo == "remove": self.games[game_id].removed = False