|
7 | 7 | from __future__ import annotations |
8 | 8 |
|
9 | 9 | import logging |
10 | | -from PyQt6.QtCore import Qt, QThread, pyqtSignal |
| 10 | +from PyQt6.QtCore import Qt, QThread, QTimer, pyqtSignal |
11 | 11 | from PyQt6.QtWidgets import ( |
12 | 12 | QCheckBox, |
13 | 13 | QComboBox, |
@@ -445,38 +445,50 @@ def _start_add(self, games: list[ExternalGame]) -> None: |
445 | 445 | self._add_thread.start() |
446 | 446 |
|
447 | 447 | def _add_with_platform_tags(self, games: list[ExternalGame]) -> None: |
448 | | - """Adds games with per-platform category tags synchronously in a thread. |
| 448 | + """Adds games with per-platform category tags using non-blocking batching. |
449 | 449 |
|
450 | | - Groups games by platform and uses each platform name as the tag. |
| 450 | + Uses QTimer.singleShot to yield to the event loop between batches, |
| 451 | + keeping the dialog responsive without resorting to processEvents(). |
451 | 452 |
|
452 | 453 | Args: |
453 | 454 | games: Games to add. |
454 | 455 | """ |
455 | | - # Group by platform for proper tagging |
456 | 456 | self._progress_bar.setVisible(True) |
457 | 457 | self._progress_label.setVisible(True) |
458 | 458 | self._progress_bar.setMaximum(len(games)) |
459 | 459 |
|
460 | | - stats = {"added": 0, "skipped": 0, "errors": 0} |
461 | | - for i, game in enumerate(games): |
462 | | - self._progress_bar.setValue(i + 1) |
463 | | - self._progress_label.setText(t("ui.external.adding_game", name=game.name, current=i + 1, total=len(games))) |
464 | | - # Process events to keep UI responsive |
465 | | - from PyQt6.QtWidgets import QApplication |
| 460 | + self._batch_stats = {"added": 0, "skipped": 0, "errors": 0} |
| 461 | + self._batch_games = games |
| 462 | + self._add_batch(0) |
466 | 463 |
|
467 | | - QApplication.processEvents() |
| 464 | + def _add_batch(self, index: int) -> None: |
| 465 | + """Processes a batch of games and schedules the next batch. |
468 | 466 |
|
| 467 | + Args: |
| 468 | + index: Current index in the game list. |
| 469 | + """ |
| 470 | + if index >= len(self._batch_games): |
| 471 | + self._on_add_finished(self._batch_stats) |
| 472 | + return |
| 473 | + |
| 474 | + batch_end = min(index + 5, len(self._batch_games)) |
| 475 | + for i in range(index, batch_end): |
| 476 | + game = self._batch_games[i] |
| 477 | + self._progress_bar.setValue(i + 1) |
| 478 | + self._progress_label.setText( |
| 479 | + t("ui.external.adding_game", name=game.name, current=i + 1, total=len(self._batch_games)) |
| 480 | + ) |
469 | 481 | try: |
470 | 482 | tag = self._collection_name_for_platform(game.platform) |
471 | 483 | if self._service and self._service.add_to_steam(game, category_tag=tag): |
472 | | - stats["added"] += 1 |
| 484 | + self._batch_stats["added"] += 1 |
473 | 485 | else: |
474 | | - stats["skipped"] += 1 |
| 486 | + self._batch_stats["skipped"] += 1 |
475 | 487 | except Exception: |
476 | 488 | logger.exception("Error adding %s", game.name) |
477 | | - stats["errors"] += 1 |
| 489 | + self._batch_stats["errors"] += 1 |
478 | 490 |
|
479 | | - self._on_add_finished(stats) |
| 491 | + QTimer.singleShot(0, lambda: self._add_batch(batch_end)) |
480 | 492 |
|
481 | 493 | @staticmethod |
482 | 494 | def _collection_name_for_platform(platform: str) -> str: |
|
0 commit comments