Type hints for UI code

This commit is contained in:
kramo
2023-08-16 15:41:40 +02:00
parent 0d6432388c
commit 8dc96b53b2
7 changed files with 186 additions and 165 deletions

View File

@@ -17,9 +17,13 @@
#
# SPDX-License-Identifier: GPL-3.0-or-later
from gi.repository import Adw, Gtk
from typing import Any
from gi.repository import Adw, Gio, GLib, Gtk
from src import shared
from src.game import Game
from src.game_cover import GameCover
from src.utils.relative_date import relative_date
@@ -64,13 +68,13 @@ class CartridgesWindow(Adw.ApplicationWindow):
hidden_search_entry = Gtk.Template.Child()
hidden_search_button = Gtk.Template.Child()
game_covers = {}
toasts = {}
active_game = None
details_view_game_cover = None
sort_state = "a-z"
game_covers: dict = {}
toasts: dict = {}
active_game: Game
details_view_game_cover: GameCover | None = None
sort_state: str = "a-z"
def __init__(self, **kwargs):
def __init__(self, **kwargs: Any) -> None:
super().__init__(**kwargs)
self.previous_page = self.library_view
@@ -110,11 +114,11 @@ class CartridgesWindow(Adw.ApplicationWindow):
style_manager.connect("notify::dark", self.set_details_view_opacity)
style_manager.connect("notify::high-contrast", self.set_details_view_opacity)
def search_changed(self, _widget, hidden):
def search_changed(self, _widget: Any, hidden: bool) -> None:
# Refresh search filter on keystroke in search box
(self.hidden_library if hidden else self.library).invalidate_filter()
def set_library_child(self):
def set_library_child(self) -> None:
child, hidden_child = self.notice_empty, self.hidden_notice_empty
for game in shared.store:
@@ -134,7 +138,7 @@ class CartridgesWindow(Adw.ApplicationWindow):
self.library_bin.set_child(child)
self.hidden_library_bin.set_child(hidden_child)
def filter_func(self, child):
def filter_func(self, child: Gtk.Widget) -> bool:
game = child.get_child()
text = (
(
@@ -156,10 +160,10 @@ class CartridgesWindow(Adw.ApplicationWindow):
return not filtered
def set_active_game(self, _widget, _pspec, game):
def set_active_game(self, _widget: Any, _pspec: Any, game: Game) -> None:
self.active_game = game
def show_details_view(self, game):
def show_details_view(self, game: Game) -> None:
self.active_game = game
self.details_view_cover.set_opacity(int(not game.loading))
@@ -207,7 +211,7 @@ class CartridgesWindow(Adw.ApplicationWindow):
self.set_details_view_opacity()
def set_details_view_opacity(self, *_args):
def set_details_view_opacity(self, *_args: Any) -> None:
if self.stack.get_visible_child() != self.details_view:
return
@@ -218,12 +222,12 @@ class CartridgesWindow(Adw.ApplicationWindow):
return
self.details_view_blurred_cover.set_opacity(
1 - self.details_view_game_cover.luminance[0]
1 - self.details_view_game_cover.luminance[0] # type: ignore
if style_manager.get_dark()
else self.details_view_game_cover.luminance[1]
else self.details_view_game_cover.luminance[1] # type: ignore
)
def sort_func(self, child1, child2):
def sort_func(self, child1: Gtk.Widget, child2: Gtk.Widget) -> int:
var, order = "name", True
if self.sort_state in ("newest", "oldest"):
@@ -233,7 +237,7 @@ class CartridgesWindow(Adw.ApplicationWindow):
elif self.sort_state == "a-z":
order = False
def get_value(index):
def get_value(index: int) -> str:
return str(
getattr((child1.get_child(), child2.get_child())[index], var)
).lower()
@@ -243,7 +247,7 @@ class CartridgesWindow(Adw.ApplicationWindow):
return ((get_value(0) > get_value(1)) ^ order) * 2 - 1
def navigate(self, next_page):
def navigate(self, next_page: Gtk.Widget) -> None:
levels = (self.library_view, self.hidden_library_view, self.details_view)
self.stack.set_transition_type(
Gtk.StackTransitionType.UNDER_RIGHT
@@ -260,13 +264,13 @@ class CartridgesWindow(Adw.ApplicationWindow):
self.stack.set_visible_child(next_page)
def on_go_back_action(self, *_args):
def on_go_back_action(self, *_args: Any) -> None:
if self.stack.get_visible_child() == self.hidden_library_view:
self.navigate(self.library_view)
elif self.stack.get_visible_child() == self.details_view:
self.on_go_to_parent_action()
def on_go_to_parent_action(self, *_args):
def on_go_to_parent_action(self, *_args: Any) -> None:
if self.stack.get_visible_child() == self.details_view:
self.navigate(
self.hidden_library_view
@@ -274,20 +278,20 @@ class CartridgesWindow(Adw.ApplicationWindow):
else self.library_view
)
def on_go_home_action(self, *_args):
def on_go_home_action(self, *_args: Any) -> None:
self.navigate(self.library_view)
def on_show_hidden_action(self, *_args):
def on_show_hidden_action(self, *_args: Any) -> None:
self.navigate(self.hidden_library_view)
def on_sort_action(self, action, state):
def on_sort_action(self, action: Gio.SimpleAction, state: GLib.Variant) -> None:
action.set_state(state)
self.sort_state = str(state).strip("'")
self.library.invalidate_sort()
shared.state_schema.set_string("sort-mode", self.sort_state)
def on_toggle_search_action(self, *_args):
def on_toggle_search_action(self, *_args: Any) -> None:
if self.stack.get_visible_child() == self.library_view:
search_bar = self.search_bar
search_entry = self.search_entry
@@ -304,7 +308,7 @@ class CartridgesWindow(Adw.ApplicationWindow):
search_entry.set_text("")
def on_escape_action(self, *_args):
def on_escape_action(self, *_args: Any) -> None:
if (
self.get_focus() == self.search_entry.get_focus_child()
or self.hidden_search_entry.get_focus_child()
@@ -313,7 +317,7 @@ class CartridgesWindow(Adw.ApplicationWindow):
else:
self.on_go_back_action()
def show_details_view_search(self, widget):
def show_details_view_search(self, widget: Gtk.Widget) -> None:
library = (
self.hidden_library if widget == self.hidden_search_entry else self.library
)
@@ -329,39 +333,39 @@ class CartridgesWindow(Adw.ApplicationWindow):
index += 1
def on_undo_action(self, _widget, game=None, undo=None):
def on_undo_action(
self, _widget: Any, game: Game | None = None, undo: str | None = None
) -> None:
if not game: # If the action was activated via Ctrl + Z
if shared.importer and (
shared.importer.imported_game_ids or shared.importer.removed_game_ids
):
undo = "import"
else:
try:
game = tuple(self.toasts.keys())[-1][0]
undo = tuple(self.toasts.keys())[-1][1]
except IndexError:
return
shared.importer.undo_import()
return
if undo == "import":
shared.importer.undo_import()
elif undo == "hide":
game.toggle_hidden(False)
elif undo == "remove":
game.removed = False
game.save()
game.update()
try:
game = tuple(self.toasts.keys())[-1][0]
undo = tuple(self.toasts.keys())[-1][1]
except IndexError:
return
if game:
if undo == "hide":
game.toggle_hidden(False)
elif undo == "remove":
game.removed = False
game.save()
game.update()
self.toasts[(game, undo)].dismiss()
self.toasts.pop((game, undo))
def on_open_menu_action(self, *_args):
def on_open_menu_action(self, *_args: Any) -> None:
if self.stack.get_visible_child() == self.library_view:
self.primary_menu_button.popup()
elif self.stack.get_visible_child() == self.hidden_library_view:
self.hidden_primary_menu_button.popup()
def on_close_action(self, *_args):
def on_close_action(self, *_args: Any) -> None:
self.close()