110 lines
3.6 KiB
Python
110 lines
3.6 KiB
Python
import threading
|
|
import webbrowser
|
|
from typing import Callable, Optional
|
|
from PIL import Image, ImageDraw
|
|
import pystray
|
|
from pystray import MenuItem as item, Menu
|
|
|
|
|
|
def create_tray_image(listening: bool = True) -> Image.Image:
|
|
"""Genera l'icona per la System Tray (64x64) con stato dinamico."""
|
|
img = Image.new("RGBA", (64, 64), (0, 0, 0, 0))
|
|
draw = ImageDraw.Draw(img)
|
|
|
|
# Base scura arrotondata
|
|
draw.rounded_rectangle((4, 4, 60, 60), radius=12, fill="#0F172A", outline="#334155", width=2)
|
|
|
|
# Barre del codice a barre stilizzato in azzurro
|
|
bar_color = "#38BDF8"
|
|
bars = [(14, 2), (19, 4), (26, 2), (31, 5), (39, 2), (44, 4)]
|
|
for x, w in bars:
|
|
draw.rectangle((x, 18, x + w - 1, 46), fill=bar_color)
|
|
|
|
# Badge circolare di stato (Verde per In Ascolto, Ambra per In Pausa)
|
|
dot_color = "#10B981" if listening else "#F59E0B"
|
|
draw.ellipse((42, 42, 58, 58), fill=dot_color, outline="#0F172A", width=2)
|
|
|
|
return img
|
|
|
|
|
|
class ServerTrayIcon:
|
|
def __init__(
|
|
self,
|
|
on_show_window: Callable[[], None],
|
|
on_toggle_listening: Callable[[], None],
|
|
on_open_web: Callable[[], None],
|
|
on_quit: Callable[[], None],
|
|
is_listening: bool = True,
|
|
):
|
|
self.on_show_window = on_show_window
|
|
self.on_toggle_listening = on_toggle_listening
|
|
self.on_open_web = on_open_web
|
|
self.on_quit = on_quit
|
|
self.is_listening = is_listening
|
|
|
|
self.icon: Optional[pystray.Icon] = None
|
|
self._thread: Optional[threading.Thread] = None
|
|
|
|
def _build_menu(self) -> Menu:
|
|
status_label = "● Stato: IN ASCOLTO" if self.is_listening else "○ Stato: IN PAUSA"
|
|
toggle_label = "Metti in Pausa Ricezione" if self.is_listening else "Riprendi Ricezione Codici"
|
|
|
|
return Menu(
|
|
item("Mostra Server Barcode", self._on_show, default=True),
|
|
Menu.SEPARATOR,
|
|
item(status_label, lambda icon, item: None, enabled=False),
|
|
item(toggle_label, self._on_toggle),
|
|
item("Apri Dashboard Web", self._on_web),
|
|
Menu.SEPARATOR,
|
|
item("Esci Definitivamente", self._on_exit),
|
|
)
|
|
|
|
def _on_show(self, icon, item):
|
|
if self.on_show_window:
|
|
self.on_show_window()
|
|
|
|
def _on_toggle(self, icon, item):
|
|
if self.on_toggle_listening:
|
|
self.on_toggle_listening()
|
|
|
|
def _on_web(self, icon, item):
|
|
if self.on_open_web:
|
|
self.on_open_web()
|
|
|
|
def _on_exit(self, icon, item):
|
|
if self.on_quit:
|
|
self.on_quit()
|
|
|
|
def update_status(self, is_listening: bool):
|
|
"""Aggiorna l'icona e il menu della tray dinamicamente."""
|
|
self.is_listening = is_listening
|
|
if self.icon:
|
|
self.icon.icon = create_tray_image(is_listening)
|
|
self.icon.menu = self._build_menu()
|
|
self.icon.title = f"Barcode Reader Server ({'In ascolto' if is_listening else 'In pausa'})"
|
|
|
|
def start(self):
|
|
"""Avvia l'icona di notifica nella tray in un thread in background."""
|
|
image = create_tray_image(self.is_listening)
|
|
self.icon = pystray.Icon(
|
|
name="barcode_reader_server",
|
|
icon=image,
|
|
title="Barcode Reader Server",
|
|
menu=self._build_menu(),
|
|
)
|
|
|
|
def run_tray():
|
|
try:
|
|
self.icon.run()
|
|
except Exception as e:
|
|
print(f"Avviso Tray: {e}")
|
|
|
|
self._thread = threading.Thread(target=run_tray, daemon=True)
|
|
self._thread.start()
|
|
|
|
def stop(self):
|
|
"""Rimuove l'icona dalla tray."""
|
|
if self.icon:
|
|
self.icon.stop()
|
|
|