Build and Release Barcode Reader Server (Multi-Platform) / build-linux (push) Failing after 39s
Build and Release Barcode Reader Server (Multi-Platform) / build-windows (push) Successful in 3m42s
Build and Release Barcode Reader Server (Multi-Platform) / build-macos (push) Successful in 1m5s
192 lines
7.0 KiB
Python
192 lines
7.0 KiB
Python
"""System Tray nativa basata su PySide6 (Wayland/X11 StatusNotifierItem, Windows, macOS)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Callable, Optional
|
|
|
|
from PySide6.QtCore import QObject, Qt
|
|
from PySide6.QtGui import QAction, QColor, QFont, QIcon, QPainter, QPainterPath, QPen, QPixmap
|
|
from PySide6.QtWidgets import QMenu, QSystemTrayIcon
|
|
|
|
from version import APP_NAME, APP_VERSION, get_resource_path
|
|
|
|
|
|
def create_status_pixmap(is_listening: bool, size: int = 64) -> QPixmap:
|
|
"""Genera un'icona nitida per la System Tray con indicatore stato attivo/pausa."""
|
|
pixmap = QPixmap(size, size)
|
|
pixmap.fill(Qt.GlobalColor.transparent)
|
|
|
|
painter = QPainter(pixmap)
|
|
painter.setRenderHint(QPainter.RenderHint.Antialiasing, True)
|
|
|
|
accent = QColor("#10B981") if is_listening else QColor("#F59E0B")
|
|
margin = max(2, round(size * 0.06))
|
|
rect_size = size - 2 * margin
|
|
radius = max(4, round(size * 0.22))
|
|
|
|
path = QPainterPath()
|
|
path.addRoundedRect(margin, margin, rect_size, rect_size, radius, radius)
|
|
painter.fillPath(path, QColor("#0F172A"))
|
|
painter.setPen(QPen(accent, max(2, round(size * 0.06))))
|
|
painter.drawPath(path)
|
|
|
|
# Barre barcode verticali ad alto contrasto
|
|
painter.setPen(Qt.PenStyle.NoPen)
|
|
painter.setBrush(QColor("#F8FAFC"))
|
|
top = round(size * 0.27)
|
|
height = round(size * 0.46)
|
|
bars = [
|
|
(0.20, 0.07),
|
|
(0.32, 0.05),
|
|
(0.42, 0.09),
|
|
(0.56, 0.05),
|
|
(0.66, 0.09),
|
|
]
|
|
for x_ratio, w_ratio in bars:
|
|
bx = round(size * x_ratio)
|
|
bw = max(2, round(size * w_ratio))
|
|
painter.drawRoundedRect(bx, top, bw, height, 1.5, 1.5)
|
|
|
|
# Linea laser di scansione / stato
|
|
painter.setBrush(accent)
|
|
laser_y = round(size * 0.48)
|
|
laser_h = max(3, round(size * 0.07))
|
|
painter.drawRoundedRect(round(size * 0.14), laser_y, round(size * 0.72), laser_h, 2, 2)
|
|
|
|
painter.end()
|
|
return pixmap
|
|
|
|
|
|
def load_app_icon() -> QIcon:
|
|
"""Carica l'icona principale dell'app da assets/icon.png (con fallback dinamico)."""
|
|
icon_file = get_resource_path("assets/icon.png")
|
|
if icon_file.exists():
|
|
icon = QIcon(str(icon_file))
|
|
if not icon.isNull():
|
|
return icon
|
|
return QIcon(create_status_pixmap(True, 128))
|
|
|
|
|
|
class ServerTrayIcon(QObject):
|
|
"""Gestisce l'icona e il menu contestuale nella System Tray tramite Qt6."""
|
|
|
|
def __init__(
|
|
self,
|
|
on_show_window: Callable[[], None],
|
|
on_toggle_listening: Callable[[], None],
|
|
on_open_web: Callable[[], None],
|
|
on_check_updates: Callable[[], None],
|
|
on_quit: Callable[[], None],
|
|
is_listening: bool = True,
|
|
parent: Optional[QObject] = None,
|
|
):
|
|
super().__init__(parent)
|
|
self.on_show_window = on_show_window
|
|
self.on_toggle_listening = on_toggle_listening
|
|
self.on_open_web = on_open_web
|
|
self.on_check_updates = on_check_updates
|
|
self.on_quit = on_quit
|
|
self.is_listening = is_listening
|
|
|
|
self._tray: Optional[QSystemTrayIcon] = None
|
|
self._menu: Optional[QMenu] = None
|
|
self._action_status: Optional[QAction] = None
|
|
self._action_show: Optional[QAction] = None
|
|
self._action_toggle: Optional[QAction] = None
|
|
self._action_web: Optional[QAction] = None
|
|
self._action_updates: Optional[QAction] = None
|
|
self._action_quit: Optional[QAction] = None
|
|
|
|
@staticmethod
|
|
def is_available() -> bool:
|
|
"""Verifica se il desktop corrente espone una System Tray attiva."""
|
|
return QSystemTrayIcon.isSystemTrayAvailable()
|
|
|
|
def start(self) -> bool:
|
|
"""Inizializza e mostra l'icona nella System Tray. Restituisce False se assente."""
|
|
if not self.is_available():
|
|
return False
|
|
|
|
if self._tray is None:
|
|
self._tray = QSystemTrayIcon(self)
|
|
self._build_menu()
|
|
self._tray.setContextMenu(self._menu)
|
|
self._tray.activated.connect(self._on_activated)
|
|
|
|
self.update_status(self.is_listening)
|
|
self._tray.show()
|
|
return self._tray.isVisible()
|
|
|
|
def _build_menu(self) -> None:
|
|
self._menu = QMenu()
|
|
|
|
self._action_status = QAction(self._menu)
|
|
self._action_status.setEnabled(False)
|
|
self._menu.addAction(self._action_status)
|
|
|
|
self._action_show = QAction("Mostra Finestra", self._menu)
|
|
show_font = QFont(self._action_show.font())
|
|
show_font.setBold(True)
|
|
self._action_show.setFont(show_font)
|
|
self._action_show.triggered.connect(self.on_show_window)
|
|
self._menu.addAction(self._action_show)
|
|
|
|
self._menu.addSeparator()
|
|
|
|
self._action_toggle = QAction(self._menu)
|
|
self._action_toggle.triggered.connect(self.on_toggle_listening)
|
|
self._menu.addAction(self._action_toggle)
|
|
|
|
self._action_web = QAction("Apri Dashboard Web", self._menu)
|
|
self._action_web.triggered.connect(self.on_open_web)
|
|
self._menu.addAction(self._action_web)
|
|
|
|
self._action_updates = QAction("Controlla Aggiornamenti...", self._menu)
|
|
self._action_updates.triggered.connect(self.on_check_updates)
|
|
self._menu.addAction(self._action_updates)
|
|
|
|
self._menu.addSeparator()
|
|
|
|
self._action_quit = QAction("Esci", self._menu)
|
|
self._action_quit.triggered.connect(self.on_quit)
|
|
self._menu.addAction(self._action_quit)
|
|
|
|
def _on_activated(self, reason: QSystemTrayIcon.ActivationReason) -> None:
|
|
if reason in (
|
|
QSystemTrayIcon.ActivationReason.Trigger,
|
|
QSystemTrayIcon.ActivationReason.DoubleClick,
|
|
):
|
|
self.on_show_window()
|
|
|
|
def update_status(self, is_listening: bool) -> None:
|
|
"""Aggiorna icona, tooltip e testi menu in base allo stato di ascolto."""
|
|
self.is_listening = is_listening
|
|
if self._tray is None:
|
|
return
|
|
|
|
icon = QIcon()
|
|
icon.addPixmap(create_status_pixmap(is_listening, 32))
|
|
icon.addPixmap(create_status_pixmap(is_listening, 64))
|
|
self._tray.setIcon(icon)
|
|
|
|
status_text = "IN ASCOLTO" if is_listening else "IN PAUSA"
|
|
self._tray.setToolTip(f"{APP_NAME} v{APP_VERSION} — {status_text}")
|
|
|
|
if self._action_status is not None:
|
|
marker = "●" if is_listening else "○"
|
|
self._action_status.setText(f"{marker} {status_text} (v{APP_VERSION})")
|
|
if self._action_toggle is not None:
|
|
self._action_toggle.setText("Metti in Pausa" if is_listening else "Riprendi Ascolto")
|
|
|
|
def show_message(self, title: str, message: str, duration_ms: int = 5000) -> None:
|
|
"""Mostra una notifica desktop nativa tramite la System Tray."""
|
|
if self._tray and self._tray.isVisible():
|
|
self._tray.showMessage(title, message, QSystemTrayIcon.MessageIcon.Information, duration_ms)
|
|
|
|
def stop(self) -> None:
|
|
"""Nasconde e rimuove l'icona della tray."""
|
|
if self._tray is not None:
|
|
self._tray.hide()
|
|
self._tray.deleteLater()
|
|
self._tray = None
|