Update create_details_window to use Game class

This commit is contained in:
kramo
2023-04-19 21:54:25 +02:00
parent 259891fdc5
commit b92e316729
4 changed files with 48 additions and 60 deletions

View File

@@ -48,6 +48,7 @@ class Game(Gtk.Box):
added = None
executable = None
game_id = None
source = None
hidden = None
last_played = None
name = None
@@ -128,6 +129,7 @@ class Game(Gtk.Box):
"added",
"executable",
"game_id",
"source",
"hidden",
"last_played",
"name",
@@ -151,15 +153,13 @@ class Game(Gtk.Box):
if action:
toast.set_button_label(_("Undo"))
toast.connect(
"button-clicked", self.win.on_undo_action, self.game_id, action
)
toast.connect("button-clicked", self.win.on_undo_action, self, action)
if (self.game_id, action) in self.win.toasts.keys():
if (self, 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, action)].dismiss()
self.win.toasts[(self.game_id, action)] = toast
self.win.toasts[(self, action)] = toast
self.win.toast_overlay.add_toast(toast)
@@ -180,7 +180,7 @@ class Game(Gtk.Box):
# The variable is the title of the game
self.create_toast(_("{} launched"))
def toggle_hidden(self, toast):
def toggle_hidden(self, toast=True):
self.hidden = not self.hidden
self.save()

View File

@@ -139,15 +139,11 @@ class CartridgesApplication(Adw.Application):
def on_launch_game_action(self, *_args):
self.win.active_game.launch()
def on_hide_game_action(
self, _action=None, _parameter=None, game_id=None, toast=True
):
(self.win.games[game_id] if game_id else self.win.active_game).toggle_hidden(
toast
)
def on_hide_game_action(self, *_args):
self.win.active_game.toggle_hidden()
def on_edit_game_action(self, *_args):
create_details_window(self.win, self.win.active_game.game_id)
create_details_window(self.win, self.win.active_game)
def on_add_game_action(self, *_args):
create_details_window(self.win)

View File

@@ -31,12 +31,11 @@ from .save_cover import resize_cover, save_cover
from .steamgriddb import SGDBSave
def create_details_window(win, game_id=None):
def create_details_window(win, game=None):
window = Adw.Window(
modal=True, default_width=500, default_height=-1, transient_for=win
)
games = win.games
cover_changed = False
cover_button_edit = Gtk.Button(
@@ -77,18 +76,16 @@ def create_details_window(win, game_id=None):
cover = Gtk.Picture.new()
game_cover = GameCover({cover})
if game_id:
if game:
window.set_title(_("Edit Game Details"))
developer = Gtk.Entry.new_with_buffer(
Gtk.EntryBuffer.new(games[game_id].developer, -1)
)
name = Gtk.Entry.new_with_buffer(Gtk.EntryBuffer.new(games[game_id].name, -1))
developer = Gtk.Entry.new_with_buffer(Gtk.EntryBuffer.new(game.developer, -1))
name = Gtk.Entry.new_with_buffer(Gtk.EntryBuffer.new(game.name, -1))
executable = Gtk.Entry.new_with_buffer(
Gtk.EntryBuffer.new(shlex.join(games[game_id].executable), -1)
Gtk.EntryBuffer.new(shlex.join(game.executable), -1)
)
apply_button = Gtk.Button.new_with_label(_("Apply"))
game_cover.new_cover(win.games[game_id].get_cover_path())
game_cover.new_cover(game.get_cover_path())
if game_cover.pixbuf:
cover_button_delete_revealer.set_reveal_child(True)
else:
@@ -239,11 +236,9 @@ def create_details_window(win, game_id=None):
def apply_preferences(*_args):
nonlocal cover_changed
nonlocal game_id
nonlocal game
nonlocal cover
values = {}
final_name = name.get_buffer().get_text()
final_developer = developer.get_buffer().get_text()
final_executable = executable.get_buffer().get_text()
@@ -256,14 +251,12 @@ def create_details_window(win, game_id=None):
except ValueError as exception:
create_dialog(
window,
_("Couldn't Add Game")
if not game_id
else _("Couldn't Apply Preferences"),
_("Couldn't Add Game") if not game else _("Couldn't Apply Preferences"),
f'{_("Executable")}: {exception}.',
)
return
if not game_id:
if not game:
if final_name == "":
create_dialog(
window, _("Couldn't Add Game"), _("Game title cannot be empty.")
@@ -280,17 +273,20 @@ def create_details_window(win, game_id=None):
numbers = [0]
for game in games:
if "imported_" in game:
numbers.append(int(game.replace("imported_", "")))
for current_game in win.games:
if "imported_" in current_game:
numbers.append(int(current_game.replace("imported_", "")))
game_id = f"imported_{str(max(numbers) + 1)}"
values["game_id"] = game_id
values["hidden"] = False
values["source"] = "imported"
values["added"] = int(time())
values["last_played"] = 0
game = Game(
win,
{
"game_id": f"imported_{str(max(numbers) + 1)}",
"hidden": False,
"source": "imported",
"added": int(time()),
"last_played": 0,
},
)
else:
if final_name == "":
@@ -309,29 +305,25 @@ def create_details_window(win, game_id=None):
)
return
values["name"] = final_name
values["developer"] = final_developer or None
values["executable"] = final_executable_split
game.name = final_name
game.developer = final_developer or None
game.executable = final_executable_split
win.game_covers[game_id] = game_cover
win.game_covers[game.game_id] = game_cover
if cover_changed:
save_cover(
win,
game_id,
game.game_id,
game_cover.path,
)
if game_id in win.games:
game = win.games[game_id]
game.update_values(values)
else:
game = Game(win, values).save()
game.save()
if not game_cover.pixbuf:
SGDBSave(win, {(game_id, values["name"])})
SGDBSave(win, {(game.game_id, game.name)})
win.game_covers[game_id].pictures.remove(cover)
game.game_cover.pictures.remove(cover)
window.close()
win.show_details_view(game)

View File

@@ -394,23 +394,23 @@ class CartridgesWindow(Adw.ApplicationWindow):
search_button.set_active(False)
search_entry.set_text("")
def on_undo_action(self, _widget, game_id=None, undo=None):
if not game_id: # If the action was activated via Ctrl + Z
def on_undo_action(self, _widget, game=None, undo=None):
if not game: # If the action was activated via Ctrl + Z
try:
game_id = tuple(self.toasts.keys())[-1][0]
game = tuple(self.toasts.keys())[-1][0]
undo = tuple(self.toasts.keys())[-1][1]
except IndexError:
return
if undo == "hide":
self.get_application().on_hide_game_action(game_id=game_id, toast=False)
game.toggle_hidden(False)
elif undo == "remove":
self.games[game_id].removed = False
self.games[game_id].save()
game.removed = False
game.save()
self.toasts[(game_id, undo)].dismiss()
self.toasts.pop((game_id, undo))
self.toasts[(game, undo)].dismiss()
self.toasts.pop((game, undo))
def on_open_menu_action(self, *_args):
if self.stack.get_visible_child() != self.details_view: