Skip to content

Commit fccd5ca

Browse files
fix(trayicon): wait for system tray availability on startup (#116)
* fix(trayicon): wait for system tray availability on startup On some desktop environments (e.g. KDE Plasma), autostart programs launch before the system tray/panel is loaded. aw-qt would immediately exit with an error in this case. Now polls for up to 30 seconds with Qt event processing, giving the desktop environment time to initialize its system tray. Falls back to the original error if the tray never becomes available. Fixes #97 * fix(trayicon): reduce wait timeout from 30s to 10s 10 seconds is sufficient for KDE Plasma to load the system tray. Feedback from @ErikBjare.
1 parent 2ed852e commit fccd5ca

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

aw_qt/trayicon.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import signal
44
import subprocess
55
import sys
6+
import time
67
import webbrowser
78
from pathlib import Path
89
from typing import Any, Dict, Optional
@@ -241,13 +242,28 @@ def run(manager: Manager, testing: bool = False) -> Any:
241242
# root widget
242243
widget = QWidget()
243244

245+
# Wait for system tray to become available (up to 10 s).
246+
# On some desktop environments (e.g. KDE Plasma), autostart programs
247+
# launch before the panel/system tray is loaded. Qt docs note that
248+
# "if the system tray is currently unavailable but becomes available
249+
# later, QSystemTrayIcon will automatically add an entry."
250+
# See: https://github.com/ActivityWatch/aw-qt/issues/97
244251
if not QSystemTrayIcon.isSystemTrayAvailable():
245-
QMessageBox.critical(
246-
widget,
247-
"Systray",
248-
"I couldn't detect any system tray on this system. Either get one or run the ActivityWatch modules from the console.",
249-
)
250-
sys.exit(1)
252+
logger.info("System tray not yet available, waiting up to 10 s...")
253+
for i in range(10):
254+
time.sleep(1)
255+
# Process events so Qt can detect tray availability changes
256+
app.processEvents()
257+
if QSystemTrayIcon.isSystemTrayAvailable():
258+
logger.info(f"System tray became available after {i + 1}s")
259+
break
260+
else:
261+
QMessageBox.critical(
262+
widget,
263+
"Systray",
264+
"I couldn't detect any system tray on this system. Either get one or run the ActivityWatch modules from the console.",
265+
)
266+
sys.exit(1)
251267

252268
if sys.platform == "darwin":
253269
icon = QIcon("icons:black-monochrome-logo.png")

0 commit comments

Comments
 (0)