added minimize to system tray

This commit is contained in:
jknapp 2025-03-14 22:48:34 -07:00
parent c897195083
commit d74bedfa28
2 changed files with 100 additions and 50 deletions

View File

@ -8,6 +8,8 @@ import os
import pyautogui import pyautogui
import subprocess import subprocess
from PIL import Image, ImageTk from PIL import Image, ImageTk
import pystray # Add this import for system tray functionality
import io
class MacroServer: class MacroServer:
def __init__(self, root): def __init__(self, root):
@ -27,6 +29,47 @@ class MacroServer:
self.server_thread.daemon = True self.server_thread.daemon = True
self.server_thread.start() self.server_thread.start()
# Set up system tray icon
self.setup_tray_icon()
# Capture the window close event
self.root.protocol("WM_DELETE_WINDOW", self.on_closing)
# Capture the window minimize event
self.root.bind("<Unmap>", lambda e: self.minimize_to_tray() if self.root.state() == 'iconic' else None)
def setup_tray_icon(self):
# Create a simple icon for the tray
icon_image = Image.new("RGB", (64, 64), color="blue")
# Menu for the tray icon
menu = (
pystray.MenuItem('Show', self.show_window),
pystray.MenuItem('Exit', self.exit_app)
)
self.icon = pystray.Icon("macropad_server", icon_image, "MacroPad Server", menu)
def minimize_to_tray(self):
self.root.withdraw() # Hide window
if not self.icon.visible:
# Start the tray icon if it's not already running
self.icon_thread = threading.Thread(target=self.icon.run)
self.icon_thread.daemon = True
self.icon_thread.start()
def show_window(self, icon=None, item=None):
self.root.deiconify() # Show window
self.root.lift() # Bring window to front
self.root.focus_force() # Focus the window
if self.icon.visible:
self.icon.stop() # Remove icon
def exit_app(self, icon=None, item=None):
if self.icon.visible:
self.icon.stop()
self.server_running = False
self.save_macros()
self.root.destroy()
def create_widgets(self): def create_widgets(self):
# Toolbar # Toolbar
toolbar = tk.Frame(self.root) toolbar = tk.Frame(self.root)
@ -35,6 +78,7 @@ class MacroServer:
tk.Button(toolbar, text="Add Macro", command=self.add_macro).pack(side=tk.LEFT, padx=5, pady=5) tk.Button(toolbar, text="Add Macro", command=self.add_macro).pack(side=tk.LEFT, padx=5, pady=5)
tk.Button(toolbar, text="Edit Macro", command=self.edit_macro).pack(side=tk.LEFT, padx=5, pady=5) tk.Button(toolbar, text="Edit Macro", command=self.edit_macro).pack(side=tk.LEFT, padx=5, pady=5)
tk.Button(toolbar, text="Delete Macro", command=self.delete_macro).pack(side=tk.LEFT, padx=5, pady=5) tk.Button(toolbar, text="Delete Macro", command=self.delete_macro).pack(side=tk.LEFT, padx=5, pady=5)
tk.Button(toolbar, text="Minimize to Tray", command=self.minimize_to_tray).pack(side=tk.LEFT, padx=5, pady=5)
# Macro list frame # Macro list frame
list_frame = tk.Frame(self.root) list_frame = tk.Frame(self.root)
@ -248,13 +292,16 @@ class MacroServer:
client_socket.close() client_socket.close()
def on_closing(self): def on_closing(self):
self.server_running = False # Instead of directly closing, ask if user wants to minimize to tray
self.save_macros() if tk.messagebox.askyesno("Minimize to Tray", "Do you want to minimize to the system tray?"):
self.root.destroy() self.minimize_to_tray()
else:
self.exit_app()
if __name__ == "__main__": if __name__ == "__main__":
import io # Added for BytesIO import io # Added for BytesIO
import tkinter.messagebox # Add this for message boxes
root = tk.Tk() root = tk.Tk()
app = MacroServer(root) app = MacroServer(root)
root.protocol("WM_DELETE_WINDOW", app.on_closing)
root.mainloop() root.mainloop()

3
requirements.txt Normal file
View File

@ -0,0 +1,3 @@
pillow
pyautogui
pystray