feat: add system tray and __main__ entry point

Add pystray-based system tray icon with Open UI / Restart / Quit
actions. Add __main__.py for `python -m wled_controller` support.
Update start-hidden.vbs with embedded Python fallback for both
installed and dev environments.
This commit is contained in:
2026-03-24 13:58:19 +03:00
parent c26aec916e
commit 6a881f8fdd
9 changed files with 204 additions and 6 deletions
+101
View File
@@ -0,0 +1,101 @@
"""Entry point for ``python -m wled_controller``.
Starts the uvicorn server and, on Windows when *pystray* is installed,
shows a system-tray icon with **Show UI** / **Exit** actions.
"""
import asyncio
import sys
import threading
import time
import webbrowser
from pathlib import Path
import uvicorn
from wled_controller.config import get_config
from wled_controller.tray import PYSTRAY_AVAILABLE, TrayManager
from wled_controller.utils import setup_logging, get_logger
setup_logging()
logger = get_logger(__name__)
_ICON_PATH = Path(__file__).parent / "static" / "icons" / "icon-192.png"
def _run_server(server: uvicorn.Server) -> None:
"""Run uvicorn in a dedicated asyncio event loop (background thread)."""
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(server.serve())
def _open_browser(port: int, delay: float = 2.0) -> None:
"""Open the UI in the default browser after a short delay."""
time.sleep(delay)
webbrowser.open(f"http://localhost:{port}")
def main() -> None:
config = get_config()
uv_config = uvicorn.Config(
"wled_controller.main:app",
host=config.server.host,
port=config.server.port,
log_level=config.server.log_level.lower(),
)
server = uvicorn.Server(uv_config)
use_tray = PYSTRAY_AVAILABLE and (
sys.platform == "win32" or _force_tray()
)
if use_tray:
logger.info("Starting with system tray icon")
# Uvicorn in a background thread
server_thread = threading.Thread(
target=_run_server, args=(server,), daemon=True,
)
server_thread.start()
# Browser after a short delay
threading.Thread(
target=_open_browser,
args=(config.server.port,),
daemon=True,
).start()
# Tray on main thread (blocking)
tray = TrayManager(
icon_path=_ICON_PATH,
port=config.server.port,
on_exit=lambda: _request_shutdown(server),
)
tray.run()
# Tray exited — wait for server to finish its graceful shutdown
server_thread.join(timeout=10)
else:
if not PYSTRAY_AVAILABLE:
logger.info(
"System tray not available (install pystray for tray support)"
)
server.run()
def _request_shutdown(server: uvicorn.Server) -> None:
"""Signal uvicorn to perform a graceful shutdown."""
server.should_exit = True
def _force_tray() -> bool:
"""Allow forcing tray on non-Windows via WLED_TRAY=1."""
import os
return os.environ.get("WLED_TRAY", "").strip() in ("1", "true", "yes")
if __name__ == "__main__":
main()