-
Notifications
You must be signed in to change notification settings - Fork 0
/
__main__.py
247 lines (199 loc) · 7.46 KB
/
__main__.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import sys
if getattr(sys, "frozen", False):
import psutil
import os
current_pid = os.getpid()
for process in psutil.process_iter(["exe"]):
if process.info["exe"] == sys.executable and process.pid != current_pid:
sys.exit(0)
import keyboard
import pystray
import json
import platformdirs
from winutils.fn_lock import core as fn_core
from winutils.toggle_rainmeter import core as rain_core
from winutils.toggle_click import core as click_core
from winutils.mechvibes_volume import core as mech_core
from winutils.monitor_brightness import core as monitor_core
from winutils.clear_ram import core as clear_ram_core
from winutils._helpers import path, overlay
from PIL import Image
ICON_PATH = path.ICON_DIR / "settings.ico"
CLICK_HOTKEY = "ctrl+alt+shift+c"
RAIN_HOTKEY = "ctrl+alt+shift+r"
RAM_HOTKEY = "ctrl+alt+shift+t"
CONFIG_PATH = platformdirs.user_config_path("Winutils", appauthor=False)
SETTINGS_PATH = CONFIG_PATH / "settings.json"
ram_next_action_is_quit = True
def invoke_ram_toggle() -> None:
"""Run the clear ram tool."""
global ram_next_action_is_quit
if ram_next_action_is_quit:
clear_ram_core.quit_apps()
else:
clear_ram_core.start_apps()
ram_next_action_is_quit = not ram_next_action_is_quit
def invoke_click() -> None:
"""Run the click tool."""
new_state = click_core.swap_mouse_buttons()
click_core.notify_state(new_state)
def invoke_rainmeter() -> None:
"""Run the Rainmeter tool."""
is_white = rain_core.toggle_colours()
rain_core.refresh_skins()
rain_core.notify_state(is_white)
def toggle_enabled() -> None:
"""
Invoke the original toggle_enabled function, and update the menu item.
The current state is also saved into the settings.
"""
original_toggle_enabled()
tray_icon.update_menu()
settings["fn_lock_enabled"] = fn_core.State.enabled
SETTINGS_PATH.write_text(json.dumps(settings))
def mech_start_hook() -> None:
settings["mechvibes_enabled"] = True
SETTINGS_PATH.write_text(json.dumps(settings))
if settings["suppressive_key_events"]:
Hooks.increase_mech_hotkey = keyboard.add_hotkey(
mech_core.INCREASE_HOTKEY, mech_core.Handler.increment_scaling, suppress=True
)
Hooks.decrease_mech_hotkey = keyboard.add_hotkey(
mech_core.DECREASE_HOTKEY, mech_core.Handler.decrement_scaling, suppress=True
)
else:
Hooks.increase_mech_hotkey = keyboard.add_hotkey(
mech_core.INCREASE_HOTKEY, mech_core.Handler.increment_scaling
)
Hooks.decrease_mech_hotkey = keyboard.add_hotkey(
mech_core.DECREASE_HOTKEY, mech_core.Handler.decrement_scaling
)
def mech_stop_hook() -> None:
settings["mechvibes_enabled"] = False
SETTINGS_PATH.write_text(json.dumps(settings))
keyboard.remove_hotkey(Hooks.increase_mech_hotkey)
Hooks.increase_mech_hotkey = None
keyboard.remove_hotkey(Hooks.decrease_mech_hotkey)
Hooks.decrease_mech_hotkey = None
def toggle_monitor_brightness() -> None:
"""Toggle the monitor brightness tool."""
monitor_core.Handler.toggle(suppress=settings["suppressive_key_events"])
settings["monitor_brightness_enabled"] = monitor_core.Handler.running
SETTINGS_PATH.write_text(json.dumps(settings))
class Hooks:
"""Store keyboard hooks used by this tool."""
fn_hook = None
click_hotkey = None
rain_hotkey = None
ram_hotkey = None
increase_mech_hotkey = None
decrease_mech_hotkey = None
def initialize_hooks() -> None:
"""
Start all keyboard hooks, keeping in mind the suppression configuration.
"""
if settings["suppressive_key_events"]:
Hooks.fn_hook = keyboard.hook(fn_core.handle_events, suppress=True)
Hooks.click_hotkey = keyboard.add_hotkey(CLICK_HOTKEY, invoke_click, suppress=True)
Hooks.rain_hotkey = keyboard.add_hotkey(RAIN_HOTKEY, invoke_rainmeter, suppress=True)
Hooks.ram_hotkey = keyboard.add_hotkey(RAM_HOTKEY, invoke_ram_toggle, suppress=True)
else:
Hooks.click_hotkey = keyboard.add_hotkey(CLICK_HOTKEY, invoke_click)
Hooks.rain_hotkey = keyboard.add_hotkey(RAIN_HOTKEY, invoke_rainmeter)
Hooks.ram_hotkey = keyboard.add_hotkey(RAM_HOTKEY, invoke_ram_toggle)
def change_key_supression() -> None:
"""
Switch between using suppressive and non-suppressive keyboard hooks.
When using non-suppressive keyboard hooks, fn lock is disabled completely.
"""
settings["suppressive_key_events"] = not settings["suppressive_key_events"]
SETTINGS_PATH.write_text(json.dumps(settings))
if Hooks.fn_hook:
keyboard.unhook(Hooks.fn_hook)
Hooks.fn_hook = None
keyboard.remove_hotkey(Hooks.click_hotkey)
Hooks.click_hotkey = None
keyboard.remove_hotkey(Hooks.rain_hotkey)
Hooks.rain_hotkey = None
keyboard.remove_hotkey(Hooks.ram_hotkey)
Hooks.ram_hotkey = None
if settings["mechvibes_enabled"]:
mech_stop_hook()
mech_start_hook()
if settings["monitor_brightness_enabled"]:
monitor_core.Handler.sync_hooks(suppress=settings["suppressive_key_events"])
initialize_hooks()
def teardown_app() -> None:
"""Teardown the application."""
tray_icon.stop()
overlay.root.after(1, overlay.root.destroy)
CONFIG_PATH.mkdir(parents=True, exist_ok=True)
SETTINGS_PATH.touch(exist_ok=True)
original_toggle_enabled = fn_core.toggle_enabled
fn_core.toggle_enabled = toggle_enabled
mech_core.Handler.start_hook = mech_start_hook
mech_core.Handler.stop_hook = mech_stop_hook
settings_string = SETTINGS_PATH.read_text()
if settings_string:
settings = json.loads(settings_string)
else:
settings = {
"suppressive_key_events": False,
"fn_lock_enabled": False,
"mechvibes_enabled": False,
"monitor_brightness_enabled": False,
}
if settings["mechvibes_enabled"]:
mech_core.Handler.start()
if settings["monitor_brightness_enabled"]:
monitor_core.Handler.start(suppress=settings["suppressive_key_events"])
fn_core.State.enabled = settings["fn_lock_enabled"]
initialize_hooks()
icon = Image.open(ICON_PATH)
icon.size = (64, 64)
click_item = pystray.MenuItem("Swap mouse buttons", invoke_click)
rain_item = pystray.MenuItem("Toggle Rainmeter colours", invoke_rainmeter)
fn_lock_item = pystray.MenuItem(
"Fn Lock",
original_toggle_enabled,
checked=lambda item: fn_core.State.enabled,
enabled=lambda item: settings["suppressive_key_events"],
)
fn_lock_item
mechvibes_item = pystray.MenuItem(
"Mechvibes stable audio",
mech_core.Handler.toggle,
checked=lambda item: mech_core.Handler.running,
)
monitor_item = pystray.MenuItem(
"Monitor brightness",
toggle_monitor_brightness,
checked=lambda item: monitor_core.Handler.running,
)
quit_apps_item = pystray.MenuItem("Quit target apps", clear_ram_core.quit_apps)
start_apps_item = pystray.MenuItem("Start target apps", clear_ram_core.start_apps)
suppress_item = pystray.MenuItem(
"Use suppressive events",
change_key_supression,
checked=lambda item: settings["suppressive_key_events"],
)
quit_item = pystray.MenuItem("Quit", teardown_app)
menu = pystray.Menu(
click_item,
rain_item,
fn_lock_item,
mechvibes_item,
monitor_item,
pystray.Menu.SEPARATOR,
start_apps_item,
quit_apps_item,
pystray.Menu.SEPARATOR,
suppress_item,
quit_item,
)
tray_icon = pystray.Icon("Winutils", icon=icon, menu=menu)
tray_icon.run_detached()
try:
overlay.root.mainloop()
except KeyboardInterrupt:
teardown_app()