updating main app to resolve previous issues
Tabs are working correctly, and the app that minimizes to the tray
This commit is contained in:
86
main.py
86
main.py
@@ -9,7 +9,7 @@ import socket
|
||||
import qrcode
|
||||
import webbrowser
|
||||
import pystray
|
||||
from PIL import Image, ImageTk
|
||||
from PIL import Image, ImageTk, ImageDraw, ImageFont
|
||||
|
||||
from config import VERSION, DEFAULT_PORT, THEME
|
||||
from macro_manager import MacroManager
|
||||
@@ -46,12 +46,20 @@ class MacroPadServer:
|
||||
# Server state
|
||||
self.server_running = False
|
||||
self.flask_thread = None
|
||||
|
||||
# Tray state
|
||||
self.tray_icon = None
|
||||
self.is_closing = False
|
||||
|
||||
# Create UI
|
||||
self.create_ui()
|
||||
|
||||
# Set up window close handler
|
||||
# Set up window event handlers
|
||||
self.root.protocol("WM_DELETE_WINDOW", self.on_closing)
|
||||
self.root.bind('<Unmap>', self.on_minimize)
|
||||
|
||||
# Initialize tray icon
|
||||
self.create_tray_icon()
|
||||
|
||||
def configure_styles(self):
|
||||
"""Configure the dark theme styles"""
|
||||
@@ -406,15 +414,53 @@ class MacroPadServer:
|
||||
if self.server_running:
|
||||
webbrowser.open(f"http://localhost:{DEFAULT_PORT}")
|
||||
|
||||
def on_minimize(self, event):
|
||||
"""Handle window minimize event"""
|
||||
# Only minimize to tray if the window is being iconified, not just unmapped
|
||||
if event.widget == self.root and self.root.state() == 'iconic':
|
||||
self.root.withdraw() # Hide window
|
||||
|
||||
def create_tray_icon(self):
|
||||
"""Create system tray icon"""
|
||||
icon_image = Image.new("RGB", (64, 64), THEME['accent_color'])
|
||||
menu = (
|
||||
pystray.MenuItem('Show', self.show_window),
|
||||
pystray.MenuItem('Exit', self.exit_app)
|
||||
)
|
||||
self.tray_icon = pystray.Icon("macropad", icon_image, "MacroPad Server", menu)
|
||||
self.tray_icon.run_detached()
|
||||
try:
|
||||
# Create a simple icon image with M letter
|
||||
icon_image = Image.new("RGB", (64, 64), THEME['accent_color'])
|
||||
draw = ImageDraw.Draw(icon_image)
|
||||
|
||||
try:
|
||||
# Try to use a system font
|
||||
font = ImageFont.truetype("arial.ttf", 40)
|
||||
except:
|
||||
try:
|
||||
# Try other common fonts
|
||||
font = ImageFont.truetype("calibri.ttf", 40)
|
||||
except:
|
||||
# Fall back to default font
|
||||
font = ImageFont.load_default()
|
||||
|
||||
# Draw "M" in the center
|
||||
text = "M"
|
||||
bbox = draw.textbbox((0, 0), text, font=font)
|
||||
text_width = bbox[2] - bbox[0]
|
||||
text_height = bbox[3] - bbox[1]
|
||||
x = (64 - text_width) // 2
|
||||
y = (64 - text_height) // 2
|
||||
draw.text((x, y), text, fill="white", font=font)
|
||||
|
||||
menu = (
|
||||
pystray.MenuItem('Show', self.show_window),
|
||||
pystray.MenuItem('Exit', self.exit_app)
|
||||
)
|
||||
|
||||
self.tray_icon = pystray.Icon("macropad", icon_image, "MacroPad Server", menu)
|
||||
|
||||
# Run tray icon in a separate thread
|
||||
tray_thread = threading.Thread(target=self.tray_icon.run, daemon=True)
|
||||
tray_thread.start()
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error creating tray icon: {e}")
|
||||
# Tray icon is optional, continue without it
|
||||
|
||||
def show_window(self, icon=None, item=None):
|
||||
"""Show window from tray"""
|
||||
@@ -425,14 +471,26 @@ class MacroPadServer:
|
||||
|
||||
def exit_app(self, icon=None, item=None):
|
||||
"""Exit the application"""
|
||||
self.is_closing = True
|
||||
self.stop_server()
|
||||
if hasattr(self, 'tray_icon'):
|
||||
self.tray_icon.stop()
|
||||
self.root.quit()
|
||||
|
||||
if self.tray_icon:
|
||||
try:
|
||||
self.tray_icon.stop()
|
||||
except:
|
||||
pass
|
||||
|
||||
try:
|
||||
self.root.quit()
|
||||
except:
|
||||
pass
|
||||
|
||||
# Force exit if needed
|
||||
import os
|
||||
os._exit(0)
|
||||
|
||||
def on_closing(self):
|
||||
"""Handle window close event"""
|
||||
# For now just exit, but could minimize to tray
|
||||
"""Handle window close event - exit the application"""
|
||||
self.exit_app()
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user