Update create_details_window to use Game class
This commit is contained in:
14
src/game.py
14
src/game.py
@@ -48,6 +48,7 @@ class Game(Gtk.Box):
|
|||||||
added = None
|
added = None
|
||||||
executable = None
|
executable = None
|
||||||
game_id = None
|
game_id = None
|
||||||
|
source = None
|
||||||
hidden = None
|
hidden = None
|
||||||
last_played = None
|
last_played = None
|
||||||
name = None
|
name = None
|
||||||
@@ -128,6 +129,7 @@ class Game(Gtk.Box):
|
|||||||
"added",
|
"added",
|
||||||
"executable",
|
"executable",
|
||||||
"game_id",
|
"game_id",
|
||||||
|
"source",
|
||||||
"hidden",
|
"hidden",
|
||||||
"last_played",
|
"last_played",
|
||||||
"name",
|
"name",
|
||||||
@@ -151,15 +153,13 @@ class Game(Gtk.Box):
|
|||||||
|
|
||||||
if action:
|
if action:
|
||||||
toast.set_button_label(_("Undo"))
|
toast.set_button_label(_("Undo"))
|
||||||
toast.connect(
|
toast.connect("button-clicked", self.win.on_undo_action, self, action)
|
||||||
"button-clicked", self.win.on_undo_action, self.game_id, 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
|
# 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)
|
self.win.toast_overlay.add_toast(toast)
|
||||||
|
|
||||||
@@ -180,7 +180,7 @@ class Game(Gtk.Box):
|
|||||||
# The variable is the title of the game
|
# The variable is the title of the game
|
||||||
self.create_toast(_("{} launched"))
|
self.create_toast(_("{} launched"))
|
||||||
|
|
||||||
def toggle_hidden(self, toast):
|
def toggle_hidden(self, toast=True):
|
||||||
self.hidden = not self.hidden
|
self.hidden = not self.hidden
|
||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
|
|||||||
10
src/main.py
10
src/main.py
@@ -139,15 +139,11 @@ class CartridgesApplication(Adw.Application):
|
|||||||
def on_launch_game_action(self, *_args):
|
def on_launch_game_action(self, *_args):
|
||||||
self.win.active_game.launch()
|
self.win.active_game.launch()
|
||||||
|
|
||||||
def on_hide_game_action(
|
def on_hide_game_action(self, *_args):
|
||||||
self, _action=None, _parameter=None, game_id=None, toast=True
|
self.win.active_game.toggle_hidden()
|
||||||
):
|
|
||||||
(self.win.games[game_id] if game_id else self.win.active_game).toggle_hidden(
|
|
||||||
toast
|
|
||||||
)
|
|
||||||
|
|
||||||
def on_edit_game_action(self, *_args):
|
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):
|
def on_add_game_action(self, *_args):
|
||||||
create_details_window(self.win)
|
create_details_window(self.win)
|
||||||
|
|||||||
@@ -31,12 +31,11 @@ from .save_cover import resize_cover, save_cover
|
|||||||
from .steamgriddb import SGDBSave
|
from .steamgriddb import SGDBSave
|
||||||
|
|
||||||
|
|
||||||
def create_details_window(win, game_id=None):
|
def create_details_window(win, game=None):
|
||||||
window = Adw.Window(
|
window = Adw.Window(
|
||||||
modal=True, default_width=500, default_height=-1, transient_for=win
|
modal=True, default_width=500, default_height=-1, transient_for=win
|
||||||
)
|
)
|
||||||
|
|
||||||
games = win.games
|
|
||||||
cover_changed = False
|
cover_changed = False
|
||||||
|
|
||||||
cover_button_edit = Gtk.Button(
|
cover_button_edit = Gtk.Button(
|
||||||
@@ -77,18 +76,16 @@ def create_details_window(win, game_id=None):
|
|||||||
cover = Gtk.Picture.new()
|
cover = Gtk.Picture.new()
|
||||||
game_cover = GameCover({cover})
|
game_cover = GameCover({cover})
|
||||||
|
|
||||||
if game_id:
|
if game:
|
||||||
window.set_title(_("Edit Game Details"))
|
window.set_title(_("Edit Game Details"))
|
||||||
developer = Gtk.Entry.new_with_buffer(
|
developer = Gtk.Entry.new_with_buffer(Gtk.EntryBuffer.new(game.developer, -1))
|
||||||
Gtk.EntryBuffer.new(games[game_id].developer, -1)
|
name = Gtk.Entry.new_with_buffer(Gtk.EntryBuffer.new(game.name, -1))
|
||||||
)
|
|
||||||
name = Gtk.Entry.new_with_buffer(Gtk.EntryBuffer.new(games[game_id].name, -1))
|
|
||||||
executable = Gtk.Entry.new_with_buffer(
|
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"))
|
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:
|
if game_cover.pixbuf:
|
||||||
cover_button_delete_revealer.set_reveal_child(True)
|
cover_button_delete_revealer.set_reveal_child(True)
|
||||||
else:
|
else:
|
||||||
@@ -239,11 +236,9 @@ def create_details_window(win, game_id=None):
|
|||||||
|
|
||||||
def apply_preferences(*_args):
|
def apply_preferences(*_args):
|
||||||
nonlocal cover_changed
|
nonlocal cover_changed
|
||||||
nonlocal game_id
|
nonlocal game
|
||||||
nonlocal cover
|
nonlocal cover
|
||||||
|
|
||||||
values = {}
|
|
||||||
|
|
||||||
final_name = name.get_buffer().get_text()
|
final_name = name.get_buffer().get_text()
|
||||||
final_developer = developer.get_buffer().get_text()
|
final_developer = developer.get_buffer().get_text()
|
||||||
final_executable = executable.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:
|
except ValueError as exception:
|
||||||
create_dialog(
|
create_dialog(
|
||||||
window,
|
window,
|
||||||
_("Couldn't Add Game")
|
_("Couldn't Add Game") if not game else _("Couldn't Apply Preferences"),
|
||||||
if not game_id
|
|
||||||
else _("Couldn't Apply Preferences"),
|
|
||||||
f'{_("Executable")}: {exception}.',
|
f'{_("Executable")}: {exception}.',
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
if not game_id:
|
if not game:
|
||||||
if final_name == "":
|
if final_name == "":
|
||||||
create_dialog(
|
create_dialog(
|
||||||
window, _("Couldn't Add Game"), _("Game title cannot be empty.")
|
window, _("Couldn't Add Game"), _("Game title cannot be empty.")
|
||||||
@@ -280,17 +273,20 @@ def create_details_window(win, game_id=None):
|
|||||||
|
|
||||||
numbers = [0]
|
numbers = [0]
|
||||||
|
|
||||||
for game in games:
|
for current_game in win.games:
|
||||||
if "imported_" in game:
|
if "imported_" in current_game:
|
||||||
numbers.append(int(game.replace("imported_", "")))
|
numbers.append(int(current_game.replace("imported_", "")))
|
||||||
|
|
||||||
game_id = f"imported_{str(max(numbers) + 1)}"
|
game = Game(
|
||||||
|
win,
|
||||||
values["game_id"] = game_id
|
{
|
||||||
values["hidden"] = False
|
"game_id": f"imported_{str(max(numbers) + 1)}",
|
||||||
values["source"] = "imported"
|
"hidden": False,
|
||||||
values["added"] = int(time())
|
"source": "imported",
|
||||||
values["last_played"] = 0
|
"added": int(time()),
|
||||||
|
"last_played": 0,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
if final_name == "":
|
if final_name == "":
|
||||||
@@ -309,29 +305,25 @@ def create_details_window(win, game_id=None):
|
|||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
values["name"] = final_name
|
game.name = final_name
|
||||||
values["developer"] = final_developer or None
|
game.developer = final_developer or None
|
||||||
values["executable"] = final_executable_split
|
game.executable = final_executable_split
|
||||||
|
|
||||||
win.game_covers[game_id] = game_cover
|
win.game_covers[game.game_id] = game_cover
|
||||||
|
|
||||||
if cover_changed:
|
if cover_changed:
|
||||||
save_cover(
|
save_cover(
|
||||||
win,
|
win,
|
||||||
game_id,
|
game.game_id,
|
||||||
game_cover.path,
|
game_cover.path,
|
||||||
)
|
)
|
||||||
|
|
||||||
if game_id in win.games:
|
game.save()
|
||||||
game = win.games[game_id]
|
|
||||||
game.update_values(values)
|
|
||||||
else:
|
|
||||||
game = Game(win, values).save()
|
|
||||||
|
|
||||||
if not game_cover.pixbuf:
|
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()
|
window.close()
|
||||||
win.show_details_view(game)
|
win.show_details_view(game)
|
||||||
|
|||||||
@@ -394,23 +394,23 @@ class CartridgesWindow(Adw.ApplicationWindow):
|
|||||||
search_button.set_active(False)
|
search_button.set_active(False)
|
||||||
search_entry.set_text("")
|
search_entry.set_text("")
|
||||||
|
|
||||||
def on_undo_action(self, _widget, game_id=None, undo=None):
|
def on_undo_action(self, _widget, game=None, undo=None):
|
||||||
if not game_id: # If the action was activated via Ctrl + Z
|
if not game: # If the action was activated via Ctrl + Z
|
||||||
try:
|
try:
|
||||||
game_id = tuple(self.toasts.keys())[-1][0]
|
game = tuple(self.toasts.keys())[-1][0]
|
||||||
undo = tuple(self.toasts.keys())[-1][1]
|
undo = tuple(self.toasts.keys())[-1][1]
|
||||||
except IndexError:
|
except IndexError:
|
||||||
return
|
return
|
||||||
|
|
||||||
if undo == "hide":
|
if undo == "hide":
|
||||||
self.get_application().on_hide_game_action(game_id=game_id, toast=False)
|
game.toggle_hidden(False)
|
||||||
|
|
||||||
elif undo == "remove":
|
elif undo == "remove":
|
||||||
self.games[game_id].removed = False
|
game.removed = False
|
||||||
self.games[game_id].save()
|
game.save()
|
||||||
|
|
||||||
self.toasts[(game_id, undo)].dismiss()
|
self.toasts[(game, undo)].dismiss()
|
||||||
self.toasts.pop((game_id, undo))
|
self.toasts.pop((game, undo))
|
||||||
|
|
||||||
def on_open_menu_action(self, *_args):
|
def on_open_menu_action(self, *_args):
|
||||||
if self.stack.get_visible_child() != self.details_view:
|
if self.stack.get_visible_child() != self.details_view:
|
||||||
|
|||||||
Reference in New Issue
Block a user