Skip to content

Commit 6675c14

Browse files
Eadromaclaude
andcommitted
Fix single-instance: show window immediately on IPC connection
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent ea57c9e commit 6675c14

2 files changed

Lines changed: 503 additions & 210 deletions

File tree

main.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@
33
from pathlib import Path
44

55
from PyQt6.QtGui import QIcon
6+
from PyQt6.QtNetwork import QLocalServer, QLocalSocket
67
from PyQt6.QtWidgets import QApplication
78

89
from ui.main_window import AccountManagerWindow
910

1011
ICON_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

1416
def _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

Comments
 (0)