Run in the background and open when MTGA starts

- Closing the window hides it to the system tray; tracking and history
  recording continue. Quit from the tray, the menu or Ctrl+Q.
- Show the window when MTGA starts a new Player.log, detected with OS
  file notifications on the log folder (QFileSystemWatcher), not polling.
- Single instance over a local socket: launching again shows the
  running window (or stays hidden with --background).
- Menu options for background mode, show-on-launch and start at login
  (XDG autostart on Linux, HKCU Run key on Windows); --background flag.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Aeth3randClaude Opus 5.5 committed 2026-10-19 09:54:08 -04:00
1 parent 5f3c7c13d0
commit c4533d12c0
4 files changed
+404 -4

No files matched your search

+17 -2
View File
@@ -1,4 +1,4 @@
"""Entry point: mtga-assistant [--log PATH] [--replay PATH [--rate N]]."""
"""Entry point: mtga-assistant [--background] [--log PATH] [--replay PATH [--rate N]]."""
from __future__ import annotations
@@ -10,6 +10,7 @@ from PySide6.QtGui import QIcon
from PySide6.QtWidgets import QApplication
from . import paths
from .background import SingleInstance
from .carddb import open_card_db
from .history import HistoryStore
from .images import ImageCache
@@ -23,6 +24,8 @@ def main() -> int:
ap.add_argument("--replay", type=Path, help="replay a saved log instead of following the live one")
ap.add_argument("--rate", type=float, default=40.0, help="replay speed in log events per second")
ap.add_argument("--mtga-dir", type=Path, help="MTGA install directory (default: auto-detect)")
ap.add_argument("--background", action="store_true",
help="start hidden (e.g. at login); the window opens when MTGA starts")
args, qt_args = ap.parse_known_args()
cfg = paths.load_config()
@@ -37,6 +40,15 @@ def main() -> int:
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID("mtga-assistant")
app = QApplication([sys.argv[0], *qt_args])
app.setQuitOnLastWindowClosed(False) # closing the window keeps recording in the background
# One instance per user: a second launch just asks the running one to show its window.
instance = None
if not args.replay:
instance = SingleInstance()
if instance.send("background" if args.background else "show"):
return 0
instance.listen()
app.setWindowIcon(QIcon(str(Path(__file__).with_name("icon.png"))))
app.setApplicationName("MTGA Assistant")
app.setDesktopFileName("mtga-assistant") # Wayland app_id, usable in Hyprland window rules
@@ -57,7 +69,10 @@ def main() -> int:
# Matches from the previous MTGA session are only in Player-prev.log.
win.import_history(log_path.with_name("Player-prev.log"))
app.aboutToQuit.connect(win.shutdown)
win.show()
if instance is not None:
instance.message.connect(lambda msg: win.bring_to_front() if msg == "show" else None)
if not args.background:
win.show()
return app.exec()