-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSystemTrayManager.py
More file actions
222 lines (177 loc) · 8.28 KB
/
Copy pathSystemTrayManager.py
File metadata and controls
222 lines (177 loc) · 8.28 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/env python3
"""
System Tray Manager for Cosmos Collection
Provides system tray icon with mini weather forecast and quick actions
"""
import logging
from typing import List, Optional, Callable
from PySide6.QtCore import QObject, Signal, QSettings
from PySide6.QtGui import QIcon, QAction
from PySide6.QtWidgets import QSystemTrayIcon, QMenu, QApplication, QWidget
logger = logging.getLogger(__name__)
class SystemTrayManager(QObject):
"""Manages the system tray icon, menu, and tooltip for Cosmos Collection"""
# Signals for communicating with the main window
restore_requested = Signal()
quit_requested = Signal()
action_triggered = Signal(str) # Emits action name: "best_dso", "target_list", "weather", "gallery"
def __init__(self, parent=None):
super().__init__(parent)
self._tray_icon: Optional[QSystemTrayIcon] = None
self._menu: Optional[QMenu] = None
self._is_available = False
self._first_minimize = True
@property
def is_available(self) -> bool:
"""Check if system tray is available on this platform"""
return self._is_available
def setup(self, icon: QIcon) -> bool:
"""
Initialize the system tray icon and menu.
Args:
icon: The QIcon to use for the tray icon
Returns:
True if setup was successful, False otherwise
"""
# Check if system tray is available
if not QSystemTrayIcon.isSystemTrayAvailable():
logger.warning("System tray is not available on this platform")
self._is_available = False
return False
try:
# Create tray icon
self._tray_icon = QSystemTrayIcon(icon, self)
self._tray_icon.setToolTip("Cosmos Collection")
# Create context menu — must have a QWidget parent so Windows has a
# valid HWND when rendering hover highlights while the main window is hidden.
parent_widget = self.parent() if isinstance(self.parent(), QWidget) else None
self._menu = QMenu(parent_widget)
self._create_menu()
self._tray_icon.setContextMenu(self._menu)
# Connect signals
self._tray_icon.activated.connect(self._on_tray_activated)
self._is_available = True
logger.debug("System tray initialized successfully")
return True
except Exception as e:
logger.error(f"Failed to setup system tray: {e}", exc_info=True)
self._is_available = False
return False
def _create_menu(self):
"""Create the context menu for the tray icon"""
if not self._menu:
return
self._menu.clear()
# Show/Restore action
show_action = QAction("Show Cosmos Collection", self._menu)
show_action.triggered.connect(self._on_show_clicked)
self._menu.addAction(show_action)
self._menu.addSeparator()
# Quick access actions
best_dso_action = QAction("Best DSO Tonight", self._menu)
best_dso_action.triggered.connect(lambda: self._on_action_clicked("best_dso"))
self._menu.addAction(best_dso_action)
target_list_action = QAction("Target List", self._menu)
target_list_action.triggered.connect(lambda: self._on_action_clicked("target_list"))
self._menu.addAction(target_list_action)
weather_action = QAction("Weather Forecast", self._menu)
weather_action.triggered.connect(lambda: self._on_action_clicked("weather"))
self._menu.addAction(weather_action)
gallery_action = QAction("Image Gallery", self._menu)
gallery_action.triggered.connect(lambda: self._on_action_clicked("gallery"))
self._menu.addAction(gallery_action)
nina_dashboard_action = QAction("NINA Dashboard", self._menu)
nina_dashboard_action.triggered.connect(lambda: self._on_action_clicked("nina_dashboard"))
self._menu.addAction(nina_dashboard_action)
self._menu.addSeparator()
# Quit action
quit_action = QAction("Quit", self._menu)
quit_action.triggered.connect(self._on_quit_clicked)
self._menu.addAction(quit_action)
def show(self):
"""Show the tray icon"""
if self._tray_icon and self._is_available:
self._tray_icon.show()
logger.debug("System tray icon shown")
# Show balloon message on first minimize
if self._first_minimize:
self._first_minimize = False
settings = QSettings("CosmosCollection", "CosmosCollection")
if not settings.value("tray_notification_shown", False, type=bool):
self._tray_icon.showMessage(
"Cosmos Collection",
"The application is still running in the system tray.\n"
"Double-click the icon to restore, or right-click for quick actions.",
QSystemTrayIcon.Information,
3000
)
settings.setValue("tray_notification_shown", True)
def hide(self):
"""Hide the tray icon"""
if self._tray_icon:
self._tray_icon.hide()
logger.debug("System tray icon hidden")
def update_tooltip(self, weather_data: List) -> None:
"""
Update the tray icon tooltip with weather forecast summary.
Args:
weather_data: List of DailyWeatherSummary objects (first 3 days will be used)
"""
if not self._tray_icon:
return
try:
# Windows tooltip limit is ~127 chars, so keep it compact
# Format: "Cosmos Collection\nTue: ★78 ☁15% 🌔85%\nWed: ★45 ☁65%..."
tooltip_lines = ["Cosmos Collection"]
if weather_data and len(weather_data) > 0:
# Show up to 3 days in compact format
for summary in weather_data[:3]:
# Short day name
day_str = summary.date.strftime("%a")
# Astro score with star
score = summary.astro_score
# Cloud cover
cloud_pct = int(summary.tonight_avg_cloud_cover)
# Moon info (compact)
moon_str = ""
if summary.moon_phase:
moon_emoji = summary.moon_phase.phase_emoji
moon_pct = int(summary.moon_phase.illumination)
moon_str = f" {moon_emoji}{moon_pct}%"
# Compact format: "Tue: ★78 ☁15% 🌔85%"
line = f"{day_str}: ★{score} ☁{cloud_pct}%{moon_str}"
tooltip_lines.append(line)
else:
tooltip_lines.append("No weather data")
tooltip_lines.append("Right-click → Weather")
tooltip_text = "\n".join(tooltip_lines)
self._tray_icon.setToolTip(tooltip_text)
logger.debug("Tray tooltip updated with weather data")
except Exception as e:
logger.error(f"Failed to update tray tooltip: {e}", exc_info=True)
self._tray_icon.setToolTip("Cosmos Collection")
def cleanup(self):
"""Clean up resources before application exit"""
if self._tray_icon:
self._tray_icon.hide()
self._tray_icon = None
if self._menu:
self._menu.deleteLater()
self._menu = None
logger.debug("System tray cleaned up")
def _on_tray_activated(self, reason: QSystemTrayIcon.ActivationReason):
"""Handle tray icon activation (click, double-click, etc.)"""
logger.debug(f"Tray icon activated with reason: {reason}")
if reason == QSystemTrayIcon.DoubleClick:
self.restore_requested.emit()
def _on_show_clicked(self):
"""Handle Show action clicked"""
self.restore_requested.emit()
def _on_action_clicked(self, action_name: str):
"""Handle quick action clicked"""
# Just trigger the action - tools open independently without restoring main window
self.action_triggered.emit(action_name)
def _on_quit_clicked(self):
"""Handle Quit action clicked"""
logger.debug(f"Tray icon quit triggered.")
self.quit_requested.emit()