572 lines
22 KiB
Python
572 lines
22 KiB
Python
import io
|
|
import json
|
|
import os
|
|
import socket
|
|
import sys
|
|
import tkinter as tk
|
|
from tkinter import ttk, messagebox
|
|
import webbrowser
|
|
from datetime import datetime
|
|
from typing import Optional
|
|
|
|
from PIL import Image, ImageTk
|
|
|
|
from autostart import is_autostart_enabled, set_autostart
|
|
from config import load_config, save_config
|
|
from server_core import BarcodeServerCore, generate_qr_image
|
|
from tray import ServerTrayIcon
|
|
|
|
|
|
class ServerAppGUI:
|
|
def __init__(self, root: tk.Tk, server: BarcodeServerCore, start_minimized: bool = False):
|
|
self.root = root
|
|
self.server = server
|
|
self.config = server.config
|
|
|
|
self.root.title("Barcode Reader PC Server - Catelli Srl")
|
|
self.root.geometry("820x680")
|
|
self.root.minsize(760, 600)
|
|
self.root.configure(bg="#0F172A")
|
|
|
|
self.qr_photo: Optional[ImageTk.PhotoImage] = None
|
|
|
|
# Setup stili moderni scuri
|
|
self._setup_styles()
|
|
|
|
# Inizializza Tray Icon
|
|
self.tray = ServerTrayIcon(
|
|
on_show_window=self.show_window,
|
|
on_toggle_listening=self.toggle_listening,
|
|
on_open_web=self.open_web_dashboard,
|
|
on_quit=self.quit_app,
|
|
is_listening=self.server.is_listening,
|
|
)
|
|
self.tray.start()
|
|
|
|
# Intercetta evento chiusura finestra (X)
|
|
self.root.protocol("WM_DELETE_WINDOW", self.on_close_clicked)
|
|
|
|
# Costruisci l'interfaccia grafica
|
|
self._build_ui()
|
|
|
|
# Collega callback del server
|
|
self.server.on_scan = self._threadsafe_on_scan
|
|
self.server.on_devices_change = self._threadsafe_on_devices_change
|
|
self.server.on_status_change = self._threadsafe_on_status_change
|
|
|
|
# Avvia il server core in background
|
|
self.server.start_background()
|
|
|
|
# Se richiesto l'avvio minimizzato (es. autostart all'avvio del PC)
|
|
if start_minimized or self.config.get("start_minimized", False):
|
|
self.root.withdraw()
|
|
else:
|
|
self.root.deiconify()
|
|
|
|
def _setup_styles(self):
|
|
style = ttk.Style()
|
|
try:
|
|
style.theme_use("clam")
|
|
except Exception:
|
|
pass
|
|
|
|
# Colori Palette
|
|
bg_dark = "#0F172A"
|
|
card_bg = "#1E293B"
|
|
border_color = "#334155"
|
|
primary_color = "#0284C7"
|
|
text_primary = "#F8FAFC"
|
|
text_secondary = "#94A3B8"
|
|
|
|
style.configure(".", background=bg_dark, foreground=text_primary, font=("Segoe UI", 10))
|
|
style.configure("TNotebook", background=bg_dark, borderwidth=0)
|
|
style.configure(
|
|
"TNotebook.Tab",
|
|
background=card_bg,
|
|
foreground=text_secondary,
|
|
padding=[18, 8],
|
|
font=("Segoe UI", 10, "bold"),
|
|
borderwidth=0,
|
|
)
|
|
style.map(
|
|
"TNotebook.Tab",
|
|
background=[("selected", primary_color)],
|
|
foreground=[("selected", "#FFFFFF")],
|
|
)
|
|
|
|
style.configure("Card.TFrame", background=card_bg, relief="solid", borderwidth=1)
|
|
style.configure("Inner.TFrame", background=card_bg)
|
|
|
|
style.configure(
|
|
"Primary.TButton",
|
|
background=primary_color,
|
|
foreground="#FFFFFF",
|
|
font=("Segoe UI", 10, "bold"),
|
|
borderwidth=0,
|
|
padding=[14, 8],
|
|
)
|
|
style.map(
|
|
"Primary.TButton",
|
|
background=[("active", "#0369A1"), ("disabled", "#475569")],
|
|
)
|
|
|
|
style.configure(
|
|
"Secondary.TButton",
|
|
background=card_bg,
|
|
foreground=text_primary,
|
|
font=("Segoe UI", 10),
|
|
borderwidth=1,
|
|
padding=[12, 6],
|
|
)
|
|
style.map(
|
|
"Secondary.TButton",
|
|
background=[("active", "#334155")],
|
|
)
|
|
|
|
style.configure(
|
|
"Treeview",
|
|
background=card_bg,
|
|
foreground=text_primary,
|
|
fieldbackground=card_bg,
|
|
rowheight=26,
|
|
font=("Segoe UI", 9),
|
|
)
|
|
style.configure(
|
|
"Treeview.Heading",
|
|
background="#0F172A",
|
|
foreground="#38BDF8",
|
|
font=("Segoe UI", 9, "bold"),
|
|
)
|
|
|
|
def _build_ui(self):
|
|
# 1. Header superiore
|
|
header = tk.Frame(self.root, bg="#1E293B", height=70, bd=1, relief="solid", highlightbackground="#334155")
|
|
header.pack(fill="x", side="top", padx=16, pady=12)
|
|
|
|
# Titolo e sottotitolo
|
|
title_box = tk.Frame(header, bg="#1E293B")
|
|
title_box.pack(side="left", padx=16, pady=10)
|
|
|
|
title_lbl = tk.Label(
|
|
title_box,
|
|
text="Barcode Reader PC Server",
|
|
font=("Segoe UI", 16, "bold"),
|
|
fg="#38BDF8",
|
|
bg="#1E293B",
|
|
)
|
|
title_lbl.pack(anchor="w")
|
|
|
|
sub_lbl = tk.Label(
|
|
title_box,
|
|
text="Catelli Srl • Ricezione e digitazione automatica codici a barre",
|
|
font=("Segoe UI", 9),
|
|
fg="#94A3B8",
|
|
bg="#1E293B",
|
|
)
|
|
sub_lbl.pack(anchor="w")
|
|
|
|
# Controlli di stato a destra
|
|
status_box = tk.Frame(header, bg="#1E293B")
|
|
status_box.pack(side="right", padx=16, pady=10)
|
|
|
|
self.status_badge = tk.Label(
|
|
status_box,
|
|
text="● IN ASCOLTO",
|
|
font=("Segoe UI", 11, "bold"),
|
|
fg="#10B981",
|
|
bg="#064E3B",
|
|
padx=12,
|
|
pady=4,
|
|
)
|
|
self.status_badge.pack(side="left", padx=(0, 10))
|
|
|
|
self.btn_toggle = tk.Button(
|
|
status_box,
|
|
text="Metti in Pausa",
|
|
font=("Segoe UI", 9, "bold"),
|
|
bg="#334155",
|
|
fg="white",
|
|
activebackground="#475569",
|
|
activeforeground="white",
|
|
relief="flat",
|
|
padx=12,
|
|
pady=4,
|
|
command=self.toggle_listening,
|
|
cursor="hand2",
|
|
)
|
|
self.btn_toggle.pack(side="left")
|
|
|
|
# 2. Notebook a schede
|
|
self.notebook = ttk.Notebook(self.root)
|
|
self.notebook.pack(fill="both", expand=True, padx=16, pady=(0, 12))
|
|
|
|
# Scheda 1: Dashboard
|
|
self.tab_dashboard = tk.Frame(self.notebook, bg="#0F172A")
|
|
self.notebook.add(self.tab_dashboard, text=" Dashboard & Dispositivi ")
|
|
self._build_dashboard_tab(self.tab_dashboard)
|
|
|
|
# Scheda 2: Impostazioni
|
|
self.tab_settings = tk.Frame(self.notebook, bg="#0F172A")
|
|
self.notebook.add(self.tab_settings, text=" Impostazioni Server ")
|
|
self._build_settings_tab(self.tab_settings)
|
|
|
|
def _build_dashboard_tab(self, parent: tk.Frame):
|
|
# Frame superiore a due colonne
|
|
top_frame = tk.Frame(parent, bg="#0F172A")
|
|
top_frame.pack(fill="x", pady=12)
|
|
|
|
# Colonna Sinistra: QR Code per Pairing rapido
|
|
qr_card = tk.Frame(top_frame, bg="#1E293B", bd=1, relief="solid", highlightbackground="#334155")
|
|
qr_card.pack(side="left", fill="both", expand=True, padx=(0, 8))
|
|
|
|
qr_title = tk.Label(qr_card, text="PAIRING RAPIDO CON APP", font=("Segoe UI", 10, "bold"), fg="#38BDF8", bg="#1E293B")
|
|
qr_title.pack(anchor="w", padx=14, pady=(12, 6))
|
|
|
|
self.qr_label = tk.Label(qr_card, bg="#FFFFFF", padx=8, pady=8)
|
|
self.qr_label.pack(pady=6)
|
|
self._refresh_qr_display()
|
|
|
|
qr_help = tk.Label(
|
|
qr_card,
|
|
text="Inquadra il QR con l'app per collegarti istantaneamente",
|
|
font=("Segoe UI", 9),
|
|
fg="#94A3B8",
|
|
bg="#1E293B",
|
|
)
|
|
qr_help.pack(pady=(0, 10))
|
|
|
|
# Colonna Destra: Informazioni di Rete & Dispositivi
|
|
info_card = tk.Frame(top_frame, bg="#1E293B", bd=1, relief="solid", highlightbackground="#334155")
|
|
info_card.pack(side="right", fill="both", expand=True, padx=(8, 0))
|
|
|
|
info_title = tk.Label(info_card, text="STATO SERVER & RETE", font=("Segoe UI", 10, "bold"), fg="#38BDF8", bg="#1E293B")
|
|
info_title.pack(anchor="w", padx=14, pady=(12, 10))
|
|
|
|
grid_box = tk.Frame(info_card, bg="#1E293B")
|
|
grid_box.pack(fill="x", padx=14)
|
|
|
|
self.lbl_ip = self._create_info_row(grid_box, 0, "Indirizzo IP PC:", self.server.local_ip)
|
|
self.lbl_port = self._create_info_row(grid_box, 1, "Porta Server:", str(self.server.port))
|
|
self.lbl_name = self._create_info_row(grid_box, 2, "Nome Server:", self.server.server_name)
|
|
pin_val = self.config.get("pin") or "Nessuno (Libero)"
|
|
self.lbl_pin = self._create_info_row(grid_box, 3, "PIN Sicurezza:", pin_val)
|
|
|
|
# Azioni rapide
|
|
btn_box = tk.Frame(info_card, bg="#1E293B")
|
|
btn_box.pack(fill="x", padx=14, pady=16)
|
|
|
|
btn_copy_ip = tk.Button(
|
|
btn_box,
|
|
text="Copia IP",
|
|
bg="#334155",
|
|
fg="white",
|
|
relief="flat",
|
|
padx=10,
|
|
pady=4,
|
|
command=self._copy_ip_to_clipboard,
|
|
cursor="hand2",
|
|
)
|
|
btn_copy_ip.pack(side="left", padx=(0, 8))
|
|
|
|
btn_web = tk.Button(
|
|
btn_box,
|
|
text="Apri Dashboard Web",
|
|
bg="#0284C7",
|
|
fg="white",
|
|
font=("Segoe UI", 9, "bold"),
|
|
relief="flat",
|
|
padx=10,
|
|
pady=4,
|
|
command=self.open_web_dashboard,
|
|
cursor="hand2",
|
|
)
|
|
btn_web.pack(side="left")
|
|
|
|
# Dispositivi connessi
|
|
dev_lbl = tk.Label(info_card, text="DISPOSITIVI CONNESSI AL MOMENTO:", font=("Segoe UI", 9, "bold"), fg="#94A3B8", bg="#1E293B")
|
|
dev_lbl.pack(anchor="w", padx=14, pady=(6, 4))
|
|
|
|
self.lbl_devices = tk.Label(
|
|
info_card,
|
|
text="Nessun dispositivo mobile collegato.",
|
|
font=("Segoe UI", 9),
|
|
fg="#64748B",
|
|
bg="#1E293B",
|
|
)
|
|
self.lbl_devices.pack(anchor="w", padx=14, pady=(0, 12))
|
|
|
|
# Sezione Inferiore: Registro Scansioni Live
|
|
log_card = tk.Frame(parent, bg="#1E293B", bd=1, relief="solid", highlightbackground="#334155")
|
|
log_card.pack(fill="both", expand=True, pady=(4, 0))
|
|
|
|
log_header = tk.Frame(log_card, bg="#1E293B")
|
|
log_header.pack(fill="x", padx=14, pady=8)
|
|
|
|
log_title = tk.Label(log_header, text="REGISTRO SCANSIONI DIGITATE IN TEMPO REALE", font=("Segoe UI", 10, "bold"), fg="#38BDF8", bg="#1E293B")
|
|
log_title.pack(side="left")
|
|
|
|
btn_clear_log = tk.Button(
|
|
log_header,
|
|
text="Pulisci Elenco",
|
|
bg="#334155",
|
|
fg="#94A3B8",
|
|
relief="flat",
|
|
padx=8,
|
|
pady=2,
|
|
command=self._clear_scan_log,
|
|
cursor="hand2",
|
|
)
|
|
btn_clear_log.pack(side="right")
|
|
|
|
# Tabella Treeview
|
|
columns = ("time", "source", "code", "status")
|
|
self.tree = ttk.Treeview(log_card, columns=columns, show="headings", height=7)
|
|
self.tree.heading("time", text="Orario")
|
|
self.tree.heading("source", text="Dispositivo")
|
|
self.tree.heading("code", text="Codice Barcode")
|
|
self.tree.heading("status", text="Stato")
|
|
|
|
self.tree.column("time", width=80, anchor="center")
|
|
self.tree.column("source", width=140, anchor="w")
|
|
self.tree.column("code", width=360, anchor="w")
|
|
self.tree.column("status", width=100, anchor="center")
|
|
|
|
scroll = ttk.Scrollbar(log_card, orient="vertical", command=self.tree.yview)
|
|
self.tree.configure(yscrollcommand=scroll.set)
|
|
|
|
self.tree.pack(side="left", fill="both", expand=True, padx=(14, 0), pady=(0, 10))
|
|
scroll.pack(side="right", fill="y", padx=(0, 14), pady=(0, 10))
|
|
|
|
def _create_info_row(self, parent: tk.Frame, row: int, label: str, value: str) -> tk.Label:
|
|
lbl = tk.Label(parent, text=label, font=("Segoe UI", 9), fg="#94A3B8", bg="#1E293B")
|
|
lbl.grid(row=row, column=0, sticky="w", pady=2)
|
|
val_lbl = tk.Label(parent, text=value, font=("Segoe UI", 9, "bold"), fg="#F8FAFC", bg="#1E293B")
|
|
val_lbl.grid(row=row, column=1, sticky="w", padx=10, pady=2)
|
|
return val_lbl
|
|
|
|
def _build_settings_tab(self, parent: tk.Frame):
|
|
container = tk.Frame(parent, bg="#1E293B", bd=1, relief="solid", highlightbackground="#334155")
|
|
container.pack(fill="both", expand=True, padx=8, pady=12)
|
|
|
|
# Variabili Tkinter
|
|
self.var_name = tk.StringVar(value=self.config.get("server_name", socket.gethostname()))
|
|
self.var_pin = tk.StringVar(value=self.config.get("pin", ""))
|
|
self.var_port = tk.StringVar(value=str(self.config.get("port", 8765)))
|
|
self.var_suffix = tk.StringVar(value=self.config.get("suffix", "enter"))
|
|
self.var_mode = tk.StringVar(value=self.config.get("mode", "type"))
|
|
self.var_autostart = tk.BooleanVar(value=is_autostart_enabled())
|
|
self.var_tray = tk.BooleanVar(value=self.config.get("minimize_to_tray", True))
|
|
|
|
pad_x = 24
|
|
pad_y = 6
|
|
|
|
# Sezione Identità Server
|
|
lbl_sec1 = tk.Label(container, text="IDENTITÀ E SICUREZZA", font=("Segoe UI", 11, "bold"), fg="#38BDF8", bg="#1E293B")
|
|
lbl_sec1.pack(anchor="w", padx=pad_x, pady=(16, 8))
|
|
|
|
f_name = tk.Frame(container, bg="#1E293B")
|
|
f_name.pack(fill="x", padx=pad_x, pady=pad_y)
|
|
tk.Label(f_name, text="Nome Computer (visibile sull'app mobile):", width=36, anchor="w", fg="#F8FAFC", bg="#1E293B").pack(side="left")
|
|
entry_name = tk.Entry(f_name, textvariable=self.var_name, font=("Segoe UI", 10), bg="#0F172A", fg="white", insertbackground="white", relief="solid", bd=1)
|
|
entry_name.pack(side="left", fill="x", expand=True)
|
|
|
|
f_pin = tk.Frame(container, bg="#1E293B")
|
|
f_pin.pack(fill="x", padx=pad_x, pady=pad_y)
|
|
tk.Label(f_pin, text="Codice PIN di sicurezza (lascia vuoto per disattivare):", width=36, anchor="w", fg="#F8FAFC", bg="#1E293B").pack(side="left")
|
|
entry_pin = tk.Entry(f_pin, textvariable=self.var_pin, font=("Segoe UI", 10), bg="#0F172A", fg="white", insertbackground="white", relief="solid", bd=1)
|
|
entry_pin.pack(side="left", fill="x", expand=True)
|
|
|
|
f_port = tk.Frame(container, bg="#1E293B")
|
|
f_port.pack(fill="x", padx=pad_x, pady=pad_y)
|
|
tk.Label(f_port, text="Porta di ascolto (default 8765):", width=36, anchor="w", fg="#F8FAFC", bg="#1E293B").pack(side="left")
|
|
entry_port = tk.Entry(f_port, textvariable=self.var_port, font=("Segoe UI", 10), bg="#0F172A", fg="white", insertbackground="white", relief="solid", bd=1, width=12)
|
|
entry_port.pack(side="left")
|
|
|
|
# Sezione Emulazione Tastiera
|
|
lbl_sec2 = tk.Label(container, text="COMPORTAMENTO EMULAZIONE TASTIERA", font=("Segoe UI", 11, "bold"), fg="#38BDF8", bg="#1E293B")
|
|
lbl_sec2.pack(anchor="w", padx=pad_x, pady=(16, 8))
|
|
|
|
f_suffix = tk.Frame(container, bg="#1E293B")
|
|
f_suffix.pack(fill="x", padx=pad_x, pady=pad_y)
|
|
tk.Label(f_suffix, text="Tasto finale dopo ogni scansione:", width=36, anchor="w", fg="#F8FAFC", bg="#1E293B").pack(side="left")
|
|
r1 = tk.Radiobutton(f_suffix, text="Tasto Invio (Enter)", variable=self.var_suffix, value="enter", bg="#1E293B", fg="#F8FAFC", selectcolor="#0F172A", activebackground="#1E293B")
|
|
r2 = tk.Radiobutton(f_suffix, text="Tasto Tabulatore (Tab)", variable=self.var_suffix, value="tab", bg="#1E293B", fg="#F8FAFC", selectcolor="#0F172A", activebackground="#1E293B")
|
|
r3 = tk.Radiobutton(f_suffix, text="Nessun tasto", variable=self.var_suffix, value="none", bg="#1E293B", fg="#F8FAFC", selectcolor="#0F172A", activebackground="#1E293B")
|
|
r1.pack(side="left", padx=4)
|
|
r2.pack(side="left", padx=4)
|
|
r3.pack(side="left", padx=4)
|
|
|
|
f_mode = tk.Frame(container, bg="#1E293B")
|
|
f_mode.pack(fill="x", padx=pad_x, pady=pad_y)
|
|
tk.Label(f_mode, text="Modalità di scrittura:", width=36, anchor="w", fg="#F8FAFC", bg="#1E293B").pack(side="left")
|
|
m1 = tk.Radiobutton(f_mode, text="Digitazione tastiera reale (Consigliato)", variable=self.var_mode, value="type", bg="#1E293B", fg="#F8FAFC", selectcolor="#0F172A", activebackground="#1E293B")
|
|
m2 = tk.Radiobutton(f_mode, text="Incolla veloce da appunti (Ctrl+V / Cmd+V)", variable=self.var_mode, value="paste", bg="#1E293B", fg="#F8FAFC", selectcolor="#0F172A", activebackground="#1E293B")
|
|
m1.pack(side="left", padx=4)
|
|
m2.pack(side="left", padx=4)
|
|
|
|
# Sezione Sistema & Tray
|
|
lbl_sec3 = tk.Label(container, text="AVVIO AUTOMATICO E TRAY", font=("Segoe UI", 11, "bold"), fg="#38BDF8", bg="#1E293B")
|
|
lbl_sec3.pack(anchor="w", padx=pad_x, pady=(16, 8))
|
|
|
|
chk_autostart = tk.Checkbutton(
|
|
container,
|
|
text="Avvia automaticamente all'avvio del sistema operativo (Login / Boot)",
|
|
variable=self.var_autostart,
|
|
bg="#1E293B",
|
|
fg="#F8FAFC",
|
|
selectcolor="#0F172A",
|
|
activebackground="#1E293B",
|
|
font=("Segoe UI", 10),
|
|
)
|
|
chk_autostart.pack(anchor="w", padx=pad_x, pady=3)
|
|
|
|
chk_tray = tk.Checkbutton(
|
|
container,
|
|
text="Alla chiusura (X) riduci nel vassoio di sistema (System Tray Icon) invece di terminare",
|
|
variable=self.var_tray,
|
|
bg="#1E293B",
|
|
fg="#F8FAFC",
|
|
selectcolor="#0F172A",
|
|
activebackground="#1E293B",
|
|
font=("Segoe UI", 10),
|
|
)
|
|
chk_tray.pack(anchor="w", padx=pad_x, pady=3)
|
|
|
|
# Pulsante Salva
|
|
btn_save = tk.Button(
|
|
container,
|
|
text="Salva ed Applica Impostazioni",
|
|
font=("Segoe UI", 11, "bold"),
|
|
bg="#0284C7",
|
|
fg="white",
|
|
relief="flat",
|
|
padx=20,
|
|
pady=10,
|
|
command=self._save_settings_action,
|
|
cursor="hand2",
|
|
)
|
|
btn_save.pack(anchor="w", padx=pad_x, pady=(24, 16))
|
|
|
|
def _refresh_qr_display(self):
|
|
try:
|
|
buffer = generate_qr_image(self.server.pairing_payload)
|
|
pil_img = Image.open(buffer).resize((180, 180), Image.Resampling.LANCZOS)
|
|
self.qr_photo = ImageTk.PhotoImage(pil_img)
|
|
self.qr_label.configure(image=self.qr_photo)
|
|
except Exception as e:
|
|
print(f"Errore generazione QR per interfaccia: {e}")
|
|
|
|
def _copy_ip_to_clipboard(self):
|
|
self.root.clipboard_clear()
|
|
self.root.clipboard_append(self.server.local_ip)
|
|
messagebox.showinfo("Copia", f"Indirizzo IP ({self.server.local_ip}) copiato negli appunti!")
|
|
|
|
def _clear_scan_log(self):
|
|
for item in self.tree.get_children():
|
|
self.tree.delete(item)
|
|
|
|
def _save_settings_action(self):
|
|
try:
|
|
port = int(self.var_port.get().strip())
|
|
except ValueError:
|
|
messagebox.showerror("Errore", "La porta deve essere un numero valido (es. 8765)")
|
|
return
|
|
|
|
new_config = {
|
|
"server_name": self.var_name.get().strip() or socket.gethostname(),
|
|
"pin": self.var_pin.get().strip(),
|
|
"port": port,
|
|
"suffix": self.var_suffix.get(),
|
|
"mode": self.var_mode.get(),
|
|
"minimize_to_tray": self.var_tray.get(),
|
|
}
|
|
|
|
# Gestione Autostart
|
|
autostart_desired = self.var_autostart.get()
|
|
if autostart_desired != is_autostart_enabled():
|
|
set_autostart(autostart_desired)
|
|
|
|
# Applica modifiche al server
|
|
self.server.update_settings(new_config)
|
|
|
|
# Aggiorna label interfaccia
|
|
self.lbl_name.configure(text=self.server.server_name)
|
|
pin_text = new_config["pin"] if new_config["pin"] else "Nessuno (Libero)"
|
|
self.lbl_pin.configure(text=pin_text)
|
|
self.lbl_port.configure(text=str(port))
|
|
self._refresh_qr_display()
|
|
|
|
messagebox.showinfo("Impostazioni", "Impostazioni salvate ed applicate con successo!")
|
|
|
|
def toggle_listening(self):
|
|
new_state = not self.server.is_listening
|
|
self.server.set_listening(new_state)
|
|
self.tray.update_status(new_state)
|
|
self._update_status_ui(new_state)
|
|
|
|
def _update_status_ui(self, is_listening: bool):
|
|
if is_listening:
|
|
self.status_badge.configure(text="● IN ASCOLTO", fg="#10B981", bg="#064E3B")
|
|
self.btn_toggle.configure(text="Metti in Pausa")
|
|
else:
|
|
self.status_badge.configure(text="○ IN PAUSA", fg="#F59E0B", bg="#451A03")
|
|
self.btn_toggle.configure(text="Riprendi Ricezione")
|
|
|
|
def _threadsafe_on_status_change(self, is_listening: bool):
|
|
self.root.after(0, lambda: self._update_status_ui(is_listening))
|
|
self.root.after(0, lambda: self.tray.update_status(is_listening))
|
|
|
|
def _threadsafe_on_scan(self, code: str, source: str, success: bool):
|
|
def _add():
|
|
timestamp = datetime.now().strftime("%H:%M:%S")
|
|
status_str = "Digitato ✓" if success else "Fallito ✗"
|
|
self.tree.insert("", 0, values=(timestamp, source, code, status_str))
|
|
# Mantieni massimo 100 righe
|
|
children = self.tree.get_children()
|
|
if len(children) > 100:
|
|
self.tree.delete(children[-1])
|
|
|
|
self.root.after(0, _add)
|
|
|
|
def _threadsafe_on_devices_change(self, devices: list[str]):
|
|
def _update():
|
|
if not devices:
|
|
self.lbl_devices.configure(text="Nessun dispositivo mobile collegato.", fg="#64748B")
|
|
else:
|
|
text = f"{len(devices)} collegati: " + ", ".join(devices)
|
|
self.lbl_devices.configure(text=text, fg="#10B981")
|
|
|
|
self.root.after(0, _update)
|
|
|
|
def open_web_dashboard(self):
|
|
webbrowser.open(self.server.web_url)
|
|
|
|
def show_window(self):
|
|
self.root.after(0, self._deiconify)
|
|
|
|
def _deiconify(self):
|
|
self.root.deiconify()
|
|
self.root.lift()
|
|
self.root.focus_force()
|
|
|
|
def on_close_clicked(self):
|
|
"""Gestisce il click sul pulsante di chiusura (X)."""
|
|
if self.config.get("minimize_to_tray", True):
|
|
self.root.withdraw()
|
|
else:
|
|
self.quit_app()
|
|
|
|
def quit_app(self):
|
|
"""Arresta completamente server, tray ed esce."""
|
|
try:
|
|
self.tray.stop()
|
|
except Exception:
|
|
pass
|
|
try:
|
|
self.server.stop()
|
|
except Exception:
|
|
pass
|
|
self.root.after(100, self.root.destroy)
|
|
self.root.after(200, sys.exit)
|
|
|