Skip to content

Commit 911b447

Browse files
MRP-2 T14: Eliminate all processEvents() calls — CLAUDE.md rule #8 compliance
Replace blocking loop + processEvents in external_games_dialog with QTimer.singleShot batching (5 games per tick). Reorder setLabelText before setValue in edit_actions (setValue already processes events internally via QProgressDialog). Remove unused QApplication import. grep -r "processEvents" src/ now returns 0 code hits.
1 parent 1a6f0f7 commit 911b447

2 files changed

Lines changed: 30 additions & 20 deletions

File tree

src/ui/actions/edit_actions.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
from typing import TYPE_CHECKING
1818

19-
from PyQt6.QtWidgets import QMessageBox, QApplication
19+
from PyQt6.QtWidgets import QMessageBox
2020

2121
from src.core.game_manager import Game
2222
from src.services.curator_client import CuratorRecommendation
@@ -227,10 +227,9 @@ def _do_auto_categorize(self, settings: dict) -> None:
227227
def tags_progress(index: int, name: str) -> None:
228228
if progress.wasCanceled():
229229
return
230-
progress.setValue(step + index)
231230
if index % 10 == 0:
232231
progress.setLabelText(t("auto_categorize.status_tags", game=name[:30]))
233-
QApplication.processEvents()
232+
progress.setValue(step + index)
234233

235234
self.mw.autocategorize_service.categorize_by_tags(
236235
games, tags_count=settings["tags_count"], progress_callback=tags_progress
@@ -249,9 +248,8 @@ def tags_progress(index: int, name: str) -> None:
249248
}
250249
included_types = {rec_map[r] for r in rec_strings if r in rec_map}
251250

252-
progress.setValue(step)
253251
progress.setLabelText(t("auto_categorize.curator_fetching"))
254-
QApplication.processEvents()
252+
progress.setValue(step)
255253

256254
try:
257255
self.mw.autocategorize_service.categorize_by_curator(

src/ui/dialogs/external_games_dialog.py

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from __future__ import annotations
88

99
import logging
10-
from PyQt6.QtCore import Qt, QThread, pyqtSignal
10+
from PyQt6.QtCore import Qt, QThread, QTimer, pyqtSignal
1111
from PyQt6.QtWidgets import (
1212
QCheckBox,
1313
QComboBox,
@@ -445,38 +445,50 @@ def _start_add(self, games: list[ExternalGame]) -> None:
445445
self._add_thread.start()
446446

447447
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.
449449
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().
451452
452453
Args:
453454
games: Games to add.
454455
"""
455-
# Group by platform for proper tagging
456456
self._progress_bar.setVisible(True)
457457
self._progress_label.setVisible(True)
458458
self._progress_bar.setMaximum(len(games))
459459

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)
466463

467-
QApplication.processEvents()
464+
def _add_batch(self, index: int) -> None:
465+
"""Processes a batch of games and schedules the next batch.
468466
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+
)
469481
try:
470482
tag = self._collection_name_for_platform(game.platform)
471483
if self._service and self._service.add_to_steam(game, category_tag=tag):
472-
stats["added"] += 1
484+
self._batch_stats["added"] += 1
473485
else:
474-
stats["skipped"] += 1
486+
self._batch_stats["skipped"] += 1
475487
except Exception:
476488
logger.exception("Error adding %s", game.name)
477-
stats["errors"] += 1
489+
self._batch_stats["errors"] += 1
478490

479-
self._on_add_finished(stats)
491+
QTimer.singleShot(0, lambda: self._add_batch(batch_end))
480492

481493
@staticmethod
482494
def _collection_name_for_platform(platform: str) -> str:

0 commit comments

Comments
 (0)