Faster deduplication; use natural sorting for category names in the dialog, and also in the sidebar. Also, natural sorting for games in the category-games dialog.

Some more commenting too.
This commit is contained in:
Daniel Johnson 2024-03-23 09:19:09 -04:00 committed by Mathieu Comandon
parent 2742514f92
commit 308c9ba404
3 changed files with 17 additions and 9 deletions

View file

@ -7,6 +7,7 @@ from lutris.database import categories as categories_db
from lutris.database import games as games_db
from lutris.game import Game
from lutris.gui.dialogs import QuestionDialog, SavableModelessDialog
from lutris.util.strings import get_natural_sort_key
class EditCategoryGamesDialog(SavableModelessDialog):
@ -17,9 +18,9 @@ class EditCategoryGamesDialog(SavableModelessDialog):
self.category = category["name"]
self.category_id = category["id"]
self.available_games = [
Game(x["id"]) for x in games_db.get_games(sorts=[("installed", "DESC"), ("name", "COLLATE NOCASE ASC")])
]
self.available_games = sorted(
[Game(x["id"]) for x in games_db.get_games()], key=lambda g: (g.is_installed, get_natural_sort_key(g.name))
)
self.category_games = [Game(x) for x in categories_db.get_game_ids_for_categories([self.category])]
self.grid = Gtk.Grid()

View file

@ -1,5 +1,4 @@
# pylint: disable=no-member
import locale
from gettext import gettext as _
from typing import Sequence
@ -9,6 +8,7 @@ from lutris.database import categories as categories_db
from lutris.database.categories import is_reserved_category
from lutris.game import Game
from lutris.gui.dialogs import QuestionDialog, SavableModelessDialog
from lutris.util.strings import get_natural_sort_key
class EditGameCategoriesDialog(SavableModelessDialog):
@ -22,10 +22,9 @@ class EditGameCategoriesDialog(SavableModelessDialog):
self.category_checkboxes = {}
self.games = []
self.game_ids = []
self.categories = sorted(
[c["name"] for c in categories_db.get_categories() if not is_reserved_category(c["name"])],
key=locale.strxfrm,
key=lambda c: get_natural_sort_key(c),
)
self.checkbox_grid = Gtk.Grid()
@ -69,10 +68,10 @@ class EditGameCategoriesDialog(SavableModelessDialog):
mark_category_checkbox(category_checkbox, included=False)
self.games.append(game)
self.game_ids.append(game.id)
existing_game_ids = set(game.id for game in self.games)
for g in games:
if g.id not in self.game_ids:
if g.id not in existing_game_ids:
add_game(g)
if len(self.games) > 1:
@ -98,6 +97,8 @@ class EditGameCategoriesDialog(SavableModelessDialog):
return frame
def _create_add_category(self):
"""Creates a box that carries the controls to add a new category."""
def on_add_category(*_args):
category = categories_db.strip_category_name(category_entry.get_text())
if not categories_db.is_reserved_category(category) and category not in self.category_checkboxes:
@ -122,10 +123,11 @@ class EditGameCategoriesDialog(SavableModelessDialog):
@staticmethod
def on_checkbutton_toggled(checkbutton):
# If the user toggles a checkbox, it is no longer inconsistent.
checkbutton.set_inconsistent(False)
def on_save(self, _button):
"""Save game info and destroy widget."""
"""Save category changes and destroy widget."""
changes = []

View file

@ -22,6 +22,7 @@ from lutris.runners import InvalidRunnerError
from lutris.services import SERVICES
from lutris.services.base import AuthTokenExpiredError, BaseService
from lutris.util.library_sync import LOCAL_LIBRARY_SYNCED, LOCAL_LIBRARY_SYNCING
from lutris.util.strings import get_natural_sort_key
TYPE = 0
SLUG = 1
@ -254,6 +255,10 @@ class CategorySidebarRow(SidebarRow):
self._sort_name = locale.strxfrm(category["name"])
@property
def sort_key(self):
return get_natural_sort_key(self.name)
def get_actions(self):
"""Return the definition of buttons to be added to the row"""
return [("applications-system-symbolic", _("Edit Games"), self.on_category_clicked, "manage-category-games")]