-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtray.py
More file actions
91 lines (77 loc) · 3.1 KB
/
tray.py
File metadata and controls
91 lines (77 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
"""System tray/taskbar widget code."""
import threading
import os
import sys
import pystray
from PIL import Image
from state import settings, state
class TrayApp:
def __init__(self, on_exit=None, on_settings=None):
self.icon = None
self.on_exit = on_exit
self.on_settings = on_settings
self.running = True
def run(self):
# Get icon color from settings (default to yellow)
icon_color = settings.get_tray_icon()
icon_filename = f'ssmsplus_{icon_color}.ico'
icon_path = os.path.join(os.path.dirname(sys.argv[0]), icon_filename)
try:
image = Image.open(icon_path)
except Exception as e:
# Try fallback to yellow if specified color doesn't exist
if icon_color != 'yellow':
fallback_path = os.path.join(os.path.dirname(sys.argv[0]), 'ssmsplus_yellow.ico')
try:
image = Image.open(fallback_path)
except:
# Final fallback to blank image
image = Image.new('RGB', (64, 64), color='white')
else:
# Final fallback to blank image
image = Image.new('RGB', (64, 64), color='white')
# Get tray name from settings
tray_name = settings.get_tray_name()
# Create menu - add a separator and make the first item the default
menu = pystray.Menu(
pystray.MenuItem('Open Settings', self.show_settings, default=True),
pystray.Menu.SEPARATOR,
pystray.MenuItem('Reset Tab Colors', self.reset_tab_colors),
pystray.MenuItem('Exit', self.exit_app)
)
self.icon = pystray.Icon("SSMS Plus", image, tray_name, menu)
self.icon.run()
def show_settings(self):
if self.on_settings:
self.on_settings()
def reset_tab_colors(self):
"""Reset tab color tracking so colors will be applied again"""
state.clear_tab_color_tracking()
print("[tray] Tab color tracking reset - colors will be applied to new files")
def exit_app(self):
self.running = False
if self.icon:
self.icon.stop()
if self.on_exit:
self.on_exit()
def update_icon_and_name(self):
"""Update the tray icon and name from current settings"""
if self.icon:
# Get current settings
icon_color = settings.get_tray_icon()
tray_name = settings.get_tray_name()
# Load new icon
icon_filename = f'ssmsplus_{icon_color}.ico'
icon_path = os.path.join(os.path.dirname(sys.argv[0]), icon_filename)
try:
new_image = Image.open(icon_path)
# Update the icon image and title
self.icon.icon = new_image
self.icon.title = tray_name
except Exception as e:
pass # Keep current icon if loading fails
def start_tray_app():
tray = TrayApp()
thread = threading.Thread(target=tray.run, daemon=True)
thread.start()
return tray