33from pathlib import Path
44
55from PyQt6 .QtGui import QIcon
6+ from PyQt6 .QtNetwork import QLocalServer , QLocalSocket
67from PyQt6 .QtWidgets import QApplication
78
89from ui .main_window import AccountManagerWindow
910
1011ICON_PATH = Path (__file__ ).parent / "icon.ico"
11- APP_ID = "RiotAccountManager.1.0"
12+ APP_ID = "RiotAccountManager.1.0"
13+ IPC_NAME = "RiotAccountManager-Instance"
1214
1315
1416def _set_app_id () -> None :
15- """Tell Windows this is its own app, not pythonw.exe — fixes taskbar icon."""
1617 try :
1718 ctypes .windll .shell32 .SetCurrentProcessExplicitAppUserModelID (APP_ID )
1819 except AttributeError :
@@ -23,11 +24,34 @@ def main() -> None:
2324 _set_app_id ()
2425 app = QApplication (sys .argv )
2526
27+ # If another instance is running, tell it to raise and exit.
28+ sock = QLocalSocket ()
29+ sock .connectToServer (IPC_NAME )
30+ if sock .waitForConnected (300 ):
31+ sock .write (b"raise" )
32+ sock .waitForBytesWritten (300 )
33+ sock .disconnectFromServer ()
34+ sys .exit (0 )
35+
36+ # First instance: start the IPC server.
37+ server = QLocalServer ()
38+ QLocalServer .removeServer (IPC_NAME )
39+ server .listen (IPC_NAME )
40+
2641 icon = QIcon (str (ICON_PATH )) if ICON_PATH .exists () else QIcon ()
2742 app .setWindowIcon (icon )
2843
2944 window = AccountManagerWindow ()
3045 window .setWindowIcon (icon )
46+
47+ def _on_new_connection () -> None :
48+ client = server .nextPendingConnection ()
49+ if client :
50+ window ._show_window ()
51+ client .disconnectFromServer ()
52+
53+ server .newConnection .connect (_on_new_connection )
54+
3155 window .show ()
3256 sys .exit (app .exec ())
3357
0 commit comments