This repository has been archived by the owner on Sep 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
69 lines (49 loc) · 1.75 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
import os
import pystray
import tomllib
import psutil
from PIL import Image
CURRENT_DIR = "/".join(__file__.split("/")[:-1])
BINDINGS_FOLDER = os.path.join(os.environ["HOME"], ".config", "mousekeyx")
CONFIG_FILE = tomllib.load(open(os.path.join(CURRENT_DIR, "resource", "config.toml"), "rb"))
if not os.path.exists(BINDINGS_FOLDER):
os.mkdir(BINDINGS_FOLDER)
# Config vars
theme_dark: bool = CONFIG_FILE["tray"]["theme_dark"]
def quit(tray: pystray.Icon):
open(os.path.join(os.environ["HOME"], ".xbindkeysrc"), "w")
for proc in psutil.process_iter():
if proc.name() == "xbindkeys":
proc.kill()
tray.stop()
def reload(tray: pystray.Icon):
tray.menu = refresh_menu()
tray.update_menu()
def refresh_menu():
configs_file = os.listdir(BINDINGS_FOLDER)
configs = []
for filename in configs_file:
configs.append(pystray.MenuItem(filename, set_config))
menu = pystray.Menu(
pystray.MenuItem("Configs", pystray.Menu(*configs)),
pystray.MenuItem("Reload", reload),
pystray.MenuItem("Quit", quit),
)
return menu
def set_config(_, item: pystray._base.MenuItem):
with open(os.path.join(BINDINGS_FOLDER, item.text), "r") as f:
src = f.read()
with open(os.path.join(os.environ["HOME"], ".xbindkeysrc"), "w") as f:
f.write(src)
for proc in psutil.process_iter():
if proc.name() == "xbindkeys":
proc.kill()
os.system("xbindkeys")
def main():
icon_path = os.path.join(CURRENT_DIR, "main_black.png")
if theme_dark:
icon_path = os.path.join(CURRENT_DIR, "main_white.png")
icon = pystray.Icon("MouseKeyX", Image.open(icon_path), "MouseKeyX", refresh_menu())
icon.run()
if __name__ == "__main__":
main()