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:
101
server/src/wled_controller/__main__.py
Normal file
101
server/src/wled_controller/__main__.py
Normal 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()
|
||||
@@ -32,6 +32,7 @@ from wled_controller.storage.automation_store import AutomationStore
|
||||
from wled_controller.storage.scene_preset_store import ScenePresetStore
|
||||
from wled_controller.storage.sync_clock_store import SyncClockStore
|
||||
from wled_controller.storage.color_strip_processing_template_store import ColorStripProcessingTemplateStore
|
||||
from wled_controller.storage.gradient_store import GradientStore
|
||||
from wled_controller.core.processing.sync_clock_manager import SyncClockManager
|
||||
from wled_controller.core.automations.automation_engine import AutomationEngine
|
||||
from wled_controller.core.mqtt.mqtt_service import MQTTService
|
||||
@@ -69,6 +70,8 @@ automation_store = AutomationStore(config.storage.automations_file)
|
||||
scene_preset_store = ScenePresetStore(config.storage.scene_presets_file)
|
||||
sync_clock_store = SyncClockStore(config.storage.sync_clocks_file)
|
||||
cspt_store = ColorStripProcessingTemplateStore(config.storage.color_strip_processing_templates_file)
|
||||
gradient_store = GradientStore(config.storage.gradients_file)
|
||||
gradient_store.migrate_palette_references(color_strip_store)
|
||||
sync_clock_manager = SyncClockManager(sync_clock_store)
|
||||
|
||||
processor_manager = ProcessorManager(
|
||||
@@ -84,6 +87,7 @@ processor_manager = ProcessorManager(
|
||||
audio_template_store=audio_template_store,
|
||||
sync_clock_manager=sync_clock_manager,
|
||||
cspt_store=cspt_store,
|
||||
gradient_store=gradient_store,
|
||||
)
|
||||
)
|
||||
|
||||
@@ -167,6 +171,7 @@ async def lifespan(app: FastAPI):
|
||||
sync_clock_store=sync_clock_store,
|
||||
sync_clock_manager=sync_clock_manager,
|
||||
cspt_store=cspt_store,
|
||||
gradient_store=gradient_store,
|
||||
)
|
||||
|
||||
# Register devices in processor manager for health monitoring
|
||||
|
||||
71
server/src/wled_controller/tray.py
Normal file
71
server/src/wled_controller/tray.py
Normal file
@@ -0,0 +1,71 @@
|
||||
"""System tray icon for LED Grab (Windows)."""
|
||||
|
||||
import webbrowser
|
||||
from pathlib import Path
|
||||
from typing import Callable
|
||||
|
||||
from PIL import Image
|
||||
|
||||
try:
|
||||
import pystray
|
||||
|
||||
PYSTRAY_AVAILABLE = True
|
||||
except ImportError:
|
||||
PYSTRAY_AVAILABLE = False
|
||||
|
||||
|
||||
def _load_icon(icon_path: Path) -> Image.Image:
|
||||
"""Load tray icon from PNG, with a solid-color fallback."""
|
||||
if icon_path.exists():
|
||||
return Image.open(icon_path)
|
||||
# Fallback: blue square
|
||||
return Image.new("RGB", (64, 64), (0, 120, 255))
|
||||
|
||||
|
||||
class TrayManager:
|
||||
"""Manages the system tray icon and its context menu.
|
||||
|
||||
Call ``run()`` on the **main thread** — it blocks until ``stop()``
|
||||
is called (from any thread) or the user picks *Exit* from the menu.
|
||||
"""
|
||||
|
||||
def __init__(self, icon_path: Path, port: int, on_exit: Callable[[], None]) -> None:
|
||||
if not PYSTRAY_AVAILABLE:
|
||||
raise ImportError("pystray is required for system tray support")
|
||||
|
||||
self._port = port
|
||||
self._on_exit = on_exit
|
||||
|
||||
image = _load_icon(icon_path)
|
||||
|
||||
menu = pystray.Menu(
|
||||
pystray.MenuItem("Show UI", self._show_ui, default=True),
|
||||
pystray.Menu.SEPARATOR,
|
||||
pystray.MenuItem("Exit", self._exit),
|
||||
)
|
||||
|
||||
self._icon = pystray.Icon(
|
||||
name="ledgrab",
|
||||
icon=image,
|
||||
title="LED Grab",
|
||||
menu=menu,
|
||||
)
|
||||
|
||||
# -- menu callbacks ------------------------------------------------
|
||||
|
||||
def _show_ui(self, icon: "pystray.Icon", item: "pystray.MenuItem") -> None:
|
||||
webbrowser.open(f"http://localhost:{self._port}")
|
||||
|
||||
def _exit(self, icon: "pystray.Icon", item: "pystray.MenuItem") -> None:
|
||||
self._on_exit()
|
||||
self._icon.stop()
|
||||
|
||||
# -- public API ----------------------------------------------------
|
||||
|
||||
def run(self) -> None:
|
||||
"""Block the calling thread running the tray message loop."""
|
||||
self._icon.run()
|
||||
|
||||
def stop(self) -> None:
|
||||
"""Stop the tray icon from any thread."""
|
||||
self._icon.stop()
|
||||
Reference in New Issue
Block a user