This commit is contained in:
kramo
2023-04-19 19:50:11 +02:00
parent 96a16c1adb
commit 9e7dc12a99
5 changed files with 89 additions and 107 deletions

View File

@@ -19,8 +19,9 @@
import json import json
import os 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 from .game_cover import GameCover
@@ -69,11 +70,11 @@ class Game(Gtk.Box):
self.event_contoller_motion = Gtk.EventControllerMotion.new() self.event_contoller_motion = Gtk.EventControllerMotion.new()
self.add_controller(self.event_contoller_motion) self.add_controller(self.event_contoller_motion)
self.event_contoller_motion.connect("enter", self.show_play) self.event_contoller_motion.connect("enter", self.toggle_play, False)
self.event_contoller_motion.connect("leave", self.hide_play) self.event_contoller_motion.connect("leave", self.toggle_play, None, None)
self.cover_button.connect("clicked", self.cover_button_clicked) self.cover_button.connect("clicked", self.main_button_clicked, False)
self.play_button.connect("clicked", self.play_button_clicked) self.play_button.connect("clicked", self.main_button_clicked, True)
self.win.schema.connect("changed", self.schema_changed) self.win.schema.connect("changed", self.schema_changed)
@@ -89,7 +90,9 @@ class Game(Gtk.Box):
self.title.set_label(self.name) 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( self.menu_button.get_popover().connect(
"notify::visible", self.win.set_active_game, self.game_id "notify::visible", self.win.set_active_game, self.game_id
) )
@@ -140,8 +143,28 @@ class Game(Gtk.Box):
self.update() 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): def launch(self):
# Generate launch arguments, either list (no shell) or a string (for shell). self.last_played = int(time())
self.save()
argv = ( argv = (
("flatpak-spawn", "--host", *self.executable) # Flatpak ("flatpak-spawn", "--host", *self.executable) # Flatpak
if os.getenv("FLATPAK_ID") == "hu.kramo.Cartridges" 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"): if Gio.Settings.new("hu.kramo.Cartridges").get_boolean("exit-after-launch"):
self.app.quit() 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.hidden = not self.hidden
self.save() 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): def get_cover_path(self):
cover_path = self.win.covers_dir / f"{self.game_id}.gif" cover_path = self.win.covers_dir / f"{self.game_id}.gif"
if cover_path.is_file(): if cover_path.is_file():
@@ -167,44 +221,24 @@ class Game(Gtk.Box):
return None return None
def show_play(self, _widget, *_unused): def toggle_play(self, _widget, _prop1, _prop2, state=True):
self.play_revealer.set_reveal_child(True)
self.title_revealer.set_reveal_child(False)
def hide_play(self, _widget, *_unused):
if not self.menu_button.get_active(): if not self.menu_button.get_active():
self.play_revealer.set_reveal_child(False) self.title_revealer.set_reveal_child(state)
self.title_revealer.set_reveal_child(True) self.play_revealer.set_reveal_child(not state)
def launch_game(self, _widget, *_unused): def main_button_clicked(self, _widget, button):
self.win.set_active_game(None, None, self.game_id) if self.win.schema.get_boolean("cover-launches-game") ^ button:
self.app.on_launch_game_action(None) self.launch()
def cover_button_clicked(self, _widget):
if self.win.schema.get_boolean("cover-launches-game"):
self.launch_game(None)
else: else:
self.win.show_details_view(None, self.game_id) 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): def set_play_label(self):
if self.win.schema.get_boolean("cover-launches-game"): self.play_button.set_label(
self.play_button.set_label(_("Details")) _("Details")
else: if self.win.schema.get_boolean("cover-launches-game")
self.play_button.set_label(_("Play")) else _("Play")
)
def schema_changed(self, _settings, key): def schema_changed(self, _settings, key):
if key == "cover-launches-game": if key == "cover-launches-game":
self.set_play_label() 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)

View File

@@ -18,7 +18,6 @@
# SPDX-License-Identifier: GPL-3.0-or-later # SPDX-License-Identifier: GPL-3.0-or-later
import sys import sys
import time
import gi import gi
@@ -146,46 +145,13 @@ class CartridgesApplication(Adw.Application):
getattr(win, expander_row).set_expanded(True) getattr(win, expander_row).set_expanded(True)
win.present() win.present()
def on_launch_game_action(self, _widget, _callback=None): def on_launch_game_action(self, _widget=None, _callback=None):
# Launch the game and update the last played value self.win.games[self.win.active_game_id].launch()
game = self.win.games[self.win.active_game_id]
game.last_played = int(time.time()) def on_hide_game_action(
game.save() self, _widget=None, _callback=None, game_id=None, toast=True
game.launch() ):
self.win.games[game_id or self.win.active_game_id].toggle_hidden(toast)
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_edit_game_action(self, _widget, _callback=None): def on_edit_game_action(self, _widget, _callback=None):
create_details_window(self.win, self.win.active_game_id) 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.queue = 1
self.win.importer.save_game() self.win.importer.save_game()
def on_remove_game_action(self, _widget, _callback=None): def on_remove_game_action(self, _widget=None, _callback=None):
# Add "removed=True" to the game properties so it can be deleted on next init self.win.games[self.win.active_game_id].remove_game()
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_details_view_action(self, _widget, _callback=None): def on_remove_game_details_view_action(self, _widget, _callback=None):
if self.win.stack.get_visible_child() == self.win.details_view: 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): def on_quit_action(self, _widget, _callback=None):
self.quit() self.quit()

View File

@@ -348,6 +348,6 @@ class PreferencesWindow(Adw.PreferencesWindow):
game.save() game.save()
if self.win.stack.get_visible_child() == self.win.details_view: 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) self.add_toast(self.toast)

View File

@@ -19,7 +19,7 @@
import os import os
import shlex import shlex
import time from time import time
from gi.repository import Adw, Gio, GLib, GObject, Gtk from gi.repository import Adw, Gio, GLib, GObject, Gtk
from PIL import Image from PIL import Image
@@ -289,7 +289,7 @@ def create_details_window(win, game_id=None):
values["game_id"] = game_id values["game_id"] = game_id
values["hidden"] = False values["hidden"] = False
values["source"] = "imported" values["source"] = "imported"
values["added"] = int(time.time()) values["added"] = int(time())
values["last_played"] = 0 values["last_played"] = 0
else: else:

View File

@@ -233,7 +233,7 @@ class CartridgesWindow(Adw.ApplicationWindow):
self.details_view_game_cover.add_picture(self.details_view_cover) self.details_view_game_cover.add_picture(self.details_view_cover)
self.scaled_pixbuf = ( self.scaled_pixbuf = (
self.details_view_game_cover.pixbuf self.details_view_game_cover.get_pixbuf()
or self.details_view_game_cover.placeholder_pixbuf or self.details_view_game_cover.placeholder_pixbuf
).scale_simple(2, 3, GdkPixbuf.InterpType.BILINEAR) ).scale_simple(2, 3, GdkPixbuf.InterpType.BILINEAR)
self.details_view_blurred_cover.set_pixbuf(self.scaled_pixbuf) 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 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: if self.stack.get_visible_child() == self.hidden_library_view:
self.on_show_library_action(None, None) self.on_show_library_action(None, None)
elif self.stack.get_visible_child() == self.details_view: elif self.stack.get_visible_child() == self.details_view:
@@ -380,7 +380,7 @@ class CartridgesWindow(Adw.ApplicationWindow):
def on_escape_action(self, _widget, _unused): def on_escape_action(self, _widget, _unused):
if self.stack.get_visible_child() == self.details_view: if self.stack.get_visible_child() == self.details_view:
self.on_go_back_action(None, None) self.on_go_back_action()
return return
if self.stack.get_visible_child() == self.library_view: if self.stack.get_visible_child() == self.library_view:
search_bar = self.search_bar search_bar = self.search_bar
@@ -407,9 +407,7 @@ class CartridgesWindow(Adw.ApplicationWindow):
return return
if undo == "hide": if undo == "hide":
self.get_application().on_hide_game_action( self.get_application().on_hide_game_action(game_id=game_id, toast=False)
None, game_id=game_id, toast=False
)
elif undo == "remove": elif undo == "remove":
self.games[game_id].removed = False self.games[game_id].removed = False