-
Notifications
You must be signed in to change notification settings - Fork 0
/
systemtray.py
96 lines (77 loc) · 3.98 KB
/
systemtray.py
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
import sys
from PySide6.QtWidgets import QApplication, QSystemTrayIcon, QMenu, QLabel, QWidgetAction, QPushButton
from PySide6.QtGui import QIcon, QCursor
from PySide6.QtCore import Slot, Signal
from configmanager import ConfigManager
from settingsinterface import SettingsInterface
from whisperqueue import WhisperQueue
class SystemTrayApp(QSystemTrayIcon):
status_updated = Signal(str, str)
def __init__(self, message_queue: WhisperQueue, config_manager: ConfigManager = None, restart_callback = None, start_stop_callback = None, close_callback = None, switch_ui_callback = None):
super().__init__(QIcon())
self.restart_callback = restart_callback
self.start_stop_callback = start_stop_callback
self.config_manager = config_manager or ConfigManager()
self.message_queue = message_queue
self.close_callback = close_callback
self.switch_ui_callback = switch_ui_callback
self.status_updated.connect(self.update_status)
# Set up the system tray application
self.setIcon(QIcon("assets/logo/16/wsp_wait.png"))
self.menu = QMenu()
self.hotkey_label = QLabel(f"Hold {self.config_manager.get_setting('hotkey')} to type with voice")
self.hotkey_label_action = QWidgetAction(self)
self.hotkey_label_action.setDefaultWidget(self.hotkey_label)
self.menu.addAction(self.hotkey_label_action)
self.settings_action = SettingsInterface(self.config_manager, self.menu, self.restart)
# Advanced Settings Menu
self.advanced_menu = QMenu("Advanced Settings")
# Moving settings_action to the advanced menu
self.advanced_menu.addAction(self.settings_action)
# Creating a button for Advanced Settings
self.advanced_menu_button = QPushButton("Advanced Settings")
# Adding button as a widget action in the main menu
self.advanced_menu_button_action = QWidgetAction(self)
self.advanced_menu_button_action.setDefaultWidget(self.advanced_menu_button)
self.menu.addAction(self.advanced_menu_button_action)
self.advanced_menu_button.setMenu(self.advanced_menu)
# Add start and stop actions to the system tray menu
self.start_stop_action = self.menu.addAction("Disable Typing")
self.start_stop_action.triggered.connect(self.start_stop_callback)
self.switch_ui_action = self.menu.addAction("Open Floating Window")
self.switch_ui_action.triggered.connect(self.switch_ui_callback)
self.exit_action = self.menu.addAction("Close")
self.exit_action.triggered.connect(self.close_application)
self.setContextMenu(self.menu)
self.activated.connect(self.icon_activated)
# self.show()
self.showMessage("Whisper Speech Typing Started",
f"Hold {self.config_manager.get_setting('hotkey')} to type. \nClick green icon below for more.",
QSystemTrayIcon.Information, 5000)
def restart(self):
if self.restart_callback:
self.restart_callback()
self.menu.hide()
@Slot(str, str)
def update_status(self, icon_name, status_text):
icon_path = f"assets/logo/16/{icon_name}.png"
self.setIcon(QIcon(icon_path))
self.setToolTip(status_text)
if status_text == "disabled":
self.start_stop_action.setText("Enable Typing")
if status_text == "ready":
self.start_stop_action.setText("Disable Typing")
@Slot(QSystemTrayIcon.ActivationReason)
def icon_activated(self, reason):
if reason == QSystemTrayIcon.Trigger: # Left click
# Get the current cursor position
cursor_position = QCursor.pos()
# Popup the menu at the cursor's current position
self.menu.popup(cursor_position)
def close_application(self):
self.hide()
if self.close_callback:
self.close_callback()
if __name__ == "__main__":
app = SystemTrayApp(WhisperQueue())
sys.exit(app.exec_())