-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
125 lines (99 loc) · 3.88 KB
/
Copy pathmain.py
File metadata and controls
125 lines (99 loc) · 3.88 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
# main.py
import os
import sys
import webbrowser
from functools import partial
from PyQt5.QtCore import QThread
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QWidget, QMenu, QSystemTrayIcon
from rumps import rumps
from yaml import load
import config as cf
from shortcuts.load_plugin import open_plugin
from workers import config_watcher, keyboard_watcher
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
base_path = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))
return os.path.join(base_path, relative_path)
def iterate(parent, data):
for key, value in data.items():
if isinstance(value, dict):
new_menu = QMenu(parent)
new_menu.setTitle(key)
parent.addMenu(new_menu)
iterate(new_menu, value)
else:
parent.addAction(key, partial(open_plugin, key, value.lower()))
class Tray(QWidget):
# noinspection PyArgumentList
def __init__(self):
super().__init__()
self.menu = QMenu()
self.icon = QIcon(resource_path("assets/icon.png"))
self.tray = QSystemTrayIcon()
self.menu_active = False
self.obj = config_watcher.Worker()
self.thread = QThread()
self.obj.update_config.connect(self.update_config)
self.obj.moveToThread(self.thread)
self.obj.finished.connect(self.thread.quit)
self.thread.started.connect(self.obj.config_watcher)
self.obj2 = keyboard_watcher.Worker()
self.keyboard_watcher_thread = QThread()
self.obj2.command_status.connect(self.command_status)
self.obj2.moveToThread(self.keyboard_watcher_thread)
self.obj2.finished.connect(self.keyboard_watcher_thread.quit)
self.keyboard_watcher_thread.started.connect(self.obj2.keyboard_watcher)
self.keyboard_watcher_thread.finished.connect(app.exit)
self.thread.start()
self.keyboard_watcher_thread.start()
self.init_ui()
def init_ui(self):
self.tray.setIcon(self.icon)
self.tray.setVisible(True)
self.rebuild_menu()
# self.tray.activated.connect(self.on_systray_activated)
# def on_systray_activated(self, i_activation_reason):
# self.menu.popup(QPoint(20, 20))
def command_status(self, i):
pass
# globals.command_pressed = i
# command_pressed = i
# print(x, y)
# self.menu.hide()
# self.menu.popup(QPoint(x, y))
def update_config(self):
if self.rebuild_menu():
rumps.notification(title='Better LES', message='Configuration Loaded', subtitle='', )
def rebuild_menu(self):
config = None
success = True
try:
with open(cf.CONFIG_LOCATION_MENU) as f:
config = load(f)
except FileNotFoundError:
cf.create_config()
success = False
except Exception as e:
error_str = str(e).splitlines()
rumps.notification(title='An error occurred while loading the configuration', message=error_str[1],
subtitle=error_str[2])
success = False
self.menu.clear()
if success and config:
iterate(self.menu, config)
self.menu.addSeparator()
options = QMenu(self.menu)
options.setTitle('Options')
options.addAction('Configure Menu', partial(os.system, f'open {cf.CONFIG_LOCATION_MENU}'))
self.menu.addMenu(options)
self.menu.addSeparator()
# self.menu.addAction('New Update Available! (1.8.2)', partial(sys.exit))
self.menu.addAction('Quit Better LES', partial(sys.exit))
# Add the menu to the tray
self.tray.setContextMenu(self.menu)
return success
app = QApplication(sys.argv)
app.setQuitOnLastWindowClosed(False)
form = Tray()
sys.exit(app.exec_())