fix(server): risolve fallback involontario su CLI correggendo pack width ed integrando NSStatusBar su macOS
This commit is contained in:
1 parent
33cac8fd29
commit
a248933516
3 files changed
+191
-72
No files matched your search
+2
-2
@@ -379,8 +379,8 @@ class ServerAppGUI:
|
|||||||
f_port = tk.Frame(container, bg="#1E293B")
|
f_port = tk.Frame(container, bg="#1E293B")
|
||||||
f_port.pack(fill="x", padx=pad_x, pady=pad_y)
|
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")
|
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)
|
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", width=120)
|
entry_port.pack(side="left")
|
||||||
|
|
||||||
# Sezione Emulazione Tastiera
|
# Sezione Emulazione Tastiera
|
||||||
lbl_sec2 = tk.Label(container, text="COMPORTAMENTO EMULAZIONE TASTIERA", font=("Segoe UI", 11, "bold"), fg="#38BDF8", bg="#1E293B")
|
lbl_sec2 = tk.Label(container, text="COMPORTAMENTO EMULAZIONE TASTIERA", font=("Segoe UI", 11, "bold"), fg="#38BDF8", bg="#1E293B")
|
||||||
|
|||||||
+3
-1
@@ -117,7 +117,9 @@ def main():
|
|||||||
try:
|
try:
|
||||||
run_gui_mode(server, start_minimized=args.minimized)
|
run_gui_mode(server, start_minimized=args.minimized)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Avviso: Impossibile avviare interfaccia grafica ({e}). Fallback su modalità terminale.")
|
import traceback
|
||||||
|
traceback.print_exc()
|
||||||
|
print(f"\n[!] Avviso: Impossibile avviare interfaccia grafica ({e}). Fallback su modalità terminale.")
|
||||||
run_cli_mode(server)
|
run_cli_mode(server)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+186
-69
@@ -1,30 +1,41 @@
|
|||||||
|
import sys
|
||||||
import threading
|
import threading
|
||||||
import webbrowser
|
|
||||||
from typing import Callable, Optional
|
from typing import Callable, Optional
|
||||||
from PIL import Image, ImageDraw
|
|
||||||
import pystray
|
|
||||||
from pystray import MenuItem as item, Menu
|
|
||||||
|
|
||||||
|
# Su Windows e Linux usiamo pystray
|
||||||
|
# Su macOS usiamo direttamente Cocoa NSStatusBar per compatibilità perfetta con il main thread di Tkinter
|
||||||
|
IS_MAC = sys.platform == "darwin"
|
||||||
|
|
||||||
def create_tray_image(listening: bool = True) -> Image.Image:
|
if IS_MAC:
|
||||||
"""Genera l'icona per la System Tray (64x64) con stato dinamico."""
|
try:
|
||||||
img = Image.new("RGBA", (64, 64), (0, 0, 0, 0))
|
from AppKit import NSApplication, NSStatusBar, NSVariableStatusItemLength, NSMenu, NSMenuItem
|
||||||
draw = ImageDraw.Draw(img)
|
from Foundation import NSObject
|
||||||
|
|
||||||
# Base scura arrotondata
|
class MacMenuHandler(NSObject):
|
||||||
draw.rounded_rectangle((4, 4, 60, 60), radius=12, fill="#0F172A", outline="#334155", width=2)
|
on_show = None
|
||||||
|
on_toggle = None
|
||||||
|
on_web = None
|
||||||
|
on_quit = None
|
||||||
|
|
||||||
# Barre del codice a barre stilizzato in azzurro
|
def onShow_(self, sender):
|
||||||
bar_color = "#38BDF8"
|
if MacMenuHandler.on_show:
|
||||||
bars = [(14, 2), (19, 4), (26, 2), (31, 5), (39, 2), (44, 4)]
|
MacMenuHandler.on_show()
|
||||||
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)
|
def onToggle_(self, sender):
|
||||||
dot_color = "#10B981" if listening else "#F59E0B"
|
if MacMenuHandler.on_toggle:
|
||||||
draw.ellipse((42, 42, 58, 58), fill=dot_color, outline="#0F172A", width=2)
|
MacMenuHandler.on_toggle()
|
||||||
|
|
||||||
return img
|
def onWeb_(self, sender):
|
||||||
|
if MacMenuHandler.on_web:
|
||||||
|
MacMenuHandler.on_web()
|
||||||
|
|
||||||
|
def onQuit_(self, sender):
|
||||||
|
if MacMenuHandler.on_quit:
|
||||||
|
MacMenuHandler.on_quit()
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Avviso: PyObjC non disponibile per macOS menu bar: {e}")
|
||||||
|
IS_MAC = False
|
||||||
|
|
||||||
|
|
||||||
class ServerTrayIcon:
|
class ServerTrayIcon:
|
||||||
@@ -42,68 +53,174 @@ class ServerTrayIcon:
|
|||||||
self.on_quit = on_quit
|
self.on_quit = on_quit
|
||||||
self.is_listening = is_listening
|
self.is_listening = is_listening
|
||||||
|
|
||||||
self.icon: Optional[pystray.Icon] = None
|
# macOS specific
|
||||||
|
self._mac_status_item = None
|
||||||
|
self._mac_handler = None
|
||||||
|
self._mac_status_menu_item = None
|
||||||
|
self._mac_toggle_menu_item = None
|
||||||
|
|
||||||
|
# Windows / Linux specific
|
||||||
|
self._pystray_icon = None
|
||||||
self._thread: Optional[threading.Thread] = None
|
self._thread: Optional[threading.Thread] = None
|
||||||
|
|
||||||
def _build_menu(self) -> Menu:
|
def start(self):
|
||||||
status_label = "● Stato: IN ASCOLTO" if self.is_listening else "○ Stato: IN PAUSA"
|
"""Avvia la System Tray Icon in base alla piattaforma."""
|
||||||
toggle_label = "Metti in Pausa Ricezione" if self.is_listening else "Riprendi Ricezione Codici"
|
if IS_MAC:
|
||||||
|
self._start_mac()
|
||||||
|
else:
|
||||||
|
self._start_pystray()
|
||||||
|
|
||||||
return Menu(
|
def _start_mac(self):
|
||||||
item("Mostra Server Barcode", self._on_show, default=True),
|
try:
|
||||||
Menu.SEPARATOR,
|
status_bar = NSStatusBar.systemStatusBar()
|
||||||
item(status_label, lambda icon, item: None, enabled=False),
|
self._mac_status_item = status_bar.statusItemWithLength_(NSVariableStatusItemLength)
|
||||||
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):
|
MacMenuHandler.on_show = self.on_show_window
|
||||||
if self.on_show_window:
|
MacMenuHandler.on_toggle = self.on_toggle_listening
|
||||||
self.on_show_window()
|
MacMenuHandler.on_web = self.on_open_web
|
||||||
|
MacMenuHandler.on_quit = self.on_quit
|
||||||
|
self._mac_handler = MacMenuHandler.alloc().init()
|
||||||
|
|
||||||
def _on_toggle(self, icon, item):
|
menu = NSMenu.alloc().init()
|
||||||
if self.on_toggle_listening:
|
|
||||||
self.on_toggle_listening()
|
|
||||||
|
|
||||||
def _on_web(self, icon, item):
|
item_show = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(
|
||||||
if self.on_open_web:
|
"Mostra Server Barcode", "onShow:", ""
|
||||||
self.on_open_web()
|
)
|
||||||
|
item_show.setTarget_(self._mac_handler)
|
||||||
|
menu.addItem_(item_show)
|
||||||
|
|
||||||
def _on_exit(self, icon, item):
|
menu.addItem_(NSMenuItem.separatorItem())
|
||||||
if self.on_quit:
|
|
||||||
self.on_quit()
|
status_text = "● Stato: IN ASCOLTO" if self.is_listening else "○ Stato: IN PAUSA"
|
||||||
|
self._mac_status_menu_item = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(
|
||||||
|
status_text, "", ""
|
||||||
|
)
|
||||||
|
self._mac_status_menu_item.setEnabled_(False)
|
||||||
|
menu.addItem_(self._mac_status_menu_item)
|
||||||
|
|
||||||
|
toggle_text = "Metti in Pausa Ricezione" if self.is_listening else "Riprendi Ricezione Codici"
|
||||||
|
self._mac_toggle_menu_item = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(
|
||||||
|
toggle_text, "onToggle:", ""
|
||||||
|
)
|
||||||
|
self._mac_toggle_menu_item.setTarget_(self._mac_handler)
|
||||||
|
menu.addItem_(self._mac_toggle_menu_item)
|
||||||
|
|
||||||
|
item_web = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(
|
||||||
|
"Apri Dashboard Web", "onWeb:", ""
|
||||||
|
)
|
||||||
|
item_web.setTarget_(self._mac_handler)
|
||||||
|
menu.addItem_(item_web)
|
||||||
|
|
||||||
|
menu.addItem_(NSMenuItem.separatorItem())
|
||||||
|
|
||||||
|
item_quit = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(
|
||||||
|
"Esci Definitivamente", "onQuit:", ""
|
||||||
|
)
|
||||||
|
item_quit.setTarget_(self._mac_handler)
|
||||||
|
menu.addItem_(item_quit)
|
||||||
|
|
||||||
|
self._mac_status_item.setMenu_(menu)
|
||||||
|
self._update_mac_title()
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Avviso: Impossibile creare status item su macOS: {e}")
|
||||||
|
|
||||||
|
def _update_mac_title(self):
|
||||||
|
if self._mac_status_item:
|
||||||
|
title = "● 📦 Barcode" if self.is_listening else "○ 📦 Barcode"
|
||||||
|
self._mac_status_item.button().setTitle_(title)
|
||||||
|
|
||||||
|
def _start_pystray(self):
|
||||||
|
try:
|
||||||
|
from PIL import Image, ImageDraw
|
||||||
|
import pystray
|
||||||
|
from pystray import MenuItem as item, Menu
|
||||||
|
|
||||||
|
def create_image(listening: bool):
|
||||||
|
img = Image.new("RGBA", (64, 64), (0, 0, 0, 0))
|
||||||
|
draw = ImageDraw.Draw(img)
|
||||||
|
draw.rounded_rectangle((4, 4, 60, 60), radius=12, fill="#0F172A", outline="#334155", width=2)
|
||||||
|
bar_color = "#38BDF8"
|
||||||
|
for x, w in [(14, 2), (19, 4), (26, 2), (31, 5), (39, 2), (44, 4)]:
|
||||||
|
draw.rectangle((x, 18, x + w - 1, 46), fill=bar_color)
|
||||||
|
dot_color = "#10B981" if listening else "#F59E0B"
|
||||||
|
draw.ellipse((42, 42, 58, 58), fill=dot_color, outline="#0F172A", width=2)
|
||||||
|
return img
|
||||||
|
|
||||||
|
def build_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", lambda icon, item: self.on_show_window(), default=True),
|
||||||
|
Menu.SEPARATOR,
|
||||||
|
item(status_label, lambda icon, item: None, enabled=False),
|
||||||
|
item(toggle_label, lambda icon, item: self.on_toggle_listening()),
|
||||||
|
item("Apri Dashboard Web", lambda icon, item: self.on_open_web()),
|
||||||
|
Menu.SEPARATOR,
|
||||||
|
item("Esci Definitivamente", lambda icon, item: self.on_quit()),
|
||||||
|
)
|
||||||
|
|
||||||
|
image = create_image(self.is_listening)
|
||||||
|
self._pystray_icon = pystray.Icon(
|
||||||
|
name="barcode_reader_server",
|
||||||
|
icon=image,
|
||||||
|
title="Barcode Reader Server",
|
||||||
|
menu=build_menu(),
|
||||||
|
)
|
||||||
|
|
||||||
|
def run():
|
||||||
|
try:
|
||||||
|
self._pystray_icon.run()
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Avviso pystray: {e}")
|
||||||
|
|
||||||
|
self._thread = threading.Thread(target=run, daemon=True)
|
||||||
|
self._thread.start()
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Avviso: Impossibile avviare pystray: {e}")
|
||||||
|
|
||||||
def update_status(self, is_listening: bool):
|
def update_status(self, is_listening: bool):
|
||||||
"""Aggiorna l'icona e il menu della tray dinamicamente."""
|
"""Aggiorna lo stato visivo della tray icon."""
|
||||||
self.is_listening = is_listening
|
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):
|
if IS_MAC and self._mac_status_item:
|
||||||
"""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:
|
try:
|
||||||
self.icon.run()
|
self._update_mac_title()
|
||||||
except Exception as e:
|
if self._mac_status_menu_item:
|
||||||
print(f"Avviso Tray: {e}")
|
status_text = "● Stato: IN ASCOLTO" if is_listening else "○ Stato: IN PAUSA"
|
||||||
|
self._mac_status_menu_item.setTitle_(status_text)
|
||||||
|
if self._mac_toggle_menu_item:
|
||||||
|
toggle_text = "Metti in Pausa Ricezione" if is_listening else "Riprendi Ricezione Codici"
|
||||||
|
self._mac_toggle_menu_item.setTitle_(toggle_text)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
elif self._pystray_icon:
|
||||||
|
try:
|
||||||
|
from PIL import Image, ImageDraw
|
||||||
|
img = Image.new("RGBA", (64, 64), (0, 0, 0, 0))
|
||||||
|
draw = ImageDraw.Draw(img)
|
||||||
|
draw.rounded_rectangle((4, 4, 60, 60), radius=12, fill="#0F172A", outline="#334155", width=2)
|
||||||
|
for x, w in [(14, 2), (19, 4), (26, 2), (31, 5), (39, 2), (44, 4)]:
|
||||||
|
draw.rectangle((x, 18, x + w - 1, 46), fill="#38BDF8")
|
||||||
|
dot_color = "#10B981" if is_listening else "#F59E0B"
|
||||||
|
draw.ellipse((42, 42, 58, 58), fill=dot_color, outline="#0F172A", width=2)
|
||||||
|
|
||||||
self._thread = threading.Thread(target=run_tray, daemon=True)
|
self._pystray_icon.icon = img
|
||||||
self._thread.start()
|
self._pystray_icon.title = f"Barcode Reader Server ({'In ascolto' if is_listening else 'In pausa'})"
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
"""Rimuove l'icona dalla tray."""
|
"""Rimuove l'icona della tray."""
|
||||||
if self.icon:
|
if IS_MAC and self._mac_status_item:
|
||||||
self.icon.stop()
|
try:
|
||||||
|
NSStatusBar.systemStatusBar().removeStatusItem_(self._mac_status_item)
|
||||||
|
self._mac_status_item = None
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
elif self._pystray_icon:
|
||||||
|
try:
|
||||||
|
self._pystray_icon.stop()
|
||||||
|
self._pystray_icon = None
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
Reference in new issue
Block a user