-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathtools.py
354 lines (307 loc) · 12.9 KB
/
tools.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
from fabric.widgets.box import Box
from fabric.widgets.label import Label
from fabric.widgets.button import Button
from fabric.utils.helpers import exec_shell_command_async, get_relative_path
import modules.icons as icons
from gi.repository import Gdk, GLib
import os
import config.data as data
import subprocess
from loguru import logger
SCREENSHOT_SCRIPT = get_relative_path("../scripts/screenshot.sh")
POMODORO_SCRIPT = get_relative_path("../scripts/pomodoro.sh")
OCR_SCRIPT = get_relative_path("../scripts/ocr.sh")
GAMEMODE_SCRIPT = get_relative_path("../scripts/gamemode.sh")
SCREENRECORD_SCRIPT = get_relative_path("../scripts/screenrecord.sh")
class Toolbox(Box):
def __init__(self, **kwargs):
super().__init__(
name="toolbox",
orientation="h",
spacing=4,
v_align="center",
h_align="center",
visible=True,
**kwargs,
)
self.notch = kwargs["notch"]
self.btn_ssregion = Button(
name="toolbox-button",
child=Label(name="button-label", markup=icons.ssregion),
on_clicked=self.ssregion,
h_expand=False,
v_expand=False,
h_align="center",
v_align="center",
)
# Enable keyboard focus and connect events
self.btn_ssregion.set_can_focus(True)
self.btn_ssregion.connect("button-press-event", self.on_ssregion_click)
self.btn_ssregion.connect("key-press-event", self.on_ssregion_key)
self.btn_ssfull = Button(
name="toolbox-button",
child=Label(name="button-label", markup=icons.ssfull),
on_clicked=self.ssfull,
h_expand=False,
v_expand=False,
h_align="center",
v_align="center",
)
# Enable keyboard focus and connect events
self.btn_ssfull.set_can_focus(True)
self.btn_ssfull.connect("button-press-event", self.on_ssfull_click)
self.btn_ssfull.connect("key-press-event", self.on_ssfull_key)
self.btn_screenrecord = Button(
name="toolbox-button",
child=Label(name="button-label", markup=icons.screenrecord),
on_clicked=self.screenrecord,
h_expand=False,
v_expand=False,
h_align="center",
v_align="center",
)
self.btn_ocr = Button(
name="toolbox-button",
child=Label(name="button-label", markup=icons.ocr),
on_clicked=self.ocr,
h_expand=False,
v_expand=False,
h_align="center",
v_align="center",
)
self.btn_color = Button(
name="toolbox-button",
tooltip_text="Color Picker\nLeft Click: HEX\nMiddle Click: HSV\nRight Click: RGB\n\nKeyboard:\nEnter: HEX\nShift+Enter: RGB\nCtrl+Enter: HSV",
child=Label(
name="button-bar-label",
markup=icons.colorpicker
),
h_expand=False,
v_expand=False,
h_align="center",
v_align="center",
)
self.btn_gamemode = Button(
name="toolbox-button",
child=Label(name="button-label", markup=icons.gamemode),
on_clicked=self.gamemode,
h_expand=False,
v_expand=False,
h_align="center",
v_align="center",
)
self.btn_pomodoro = Button(
name="toolbox-button",
child=Label(name="button-label", markup=icons.timer_off),
on_clicked=self.pomodoro,
h_expand=False,
v_expand=False,
h_align="center",
v_align="center",
)
# Enable keyboard focus for the colorpicker button.
self.btn_color.set_can_focus(True)
# Connect both mouse and keyboard events.
self.btn_color.connect("button-press-event", self.colorpicker)
self.btn_color.connect("key_press_event", self.colorpicker_key)
self.btn_emoji = Button(
name="toolbox-button",
child=Label(name="button-label", markup=icons.emoji),
on_clicked=self.emoji,
h_expand=False,
v_expand=False,
h_align="center",
v_align="center",
)
self.btn_screenshots_folder = Button(
name="toolbox-button",
child=Label(name="button-label", markup=icons.screenshots),
on_clicked=self.open_screenshots_folder,
h_expand=False,
v_expand=False,
h_align="center",
v_align="center",
)
self.btn_recordings_folder = Button(
name="toolbox-button",
child=Label(name="button-label", markup=icons.recordings),
on_clicked=self.open_recordings_folder,
h_expand=False,
v_expand=False,
h_align="center",
v_align="center",
)
self.buttons = [
self.btn_ssregion,
self.btn_ssfull,
self.btn_screenshots_folder,
Box(name="tool-sep", h_expand=False, v_expand=False, h_align="center", v_align="center"),
self.btn_screenrecord,
self.btn_recordings_folder,
Box(name="tool-sep", h_expand=False, v_expand=False, h_align="center", v_align="center"),
self.btn_ocr,
self.btn_color,
Box(name="tool-sep", h_expand=False, v_expand=False, h_align="center", v_align="center"),
self.btn_gamemode,
self.btn_pomodoro,
self.btn_emoji,
]
for button in self.buttons:
self.add(button)
self.show_all()
# Start polling for process state every second.
self.recorder_timer_id = GLib.timeout_add_seconds(1, self.update_screenrecord_state)
self.gamemode_updater = GLib.timeout_add_seconds(1, self.gamemode_check)
self.pomodoro_updater = GLib.timeout_add_seconds(1, self.pomodoro_check)
def close_menu(self):
self.notch.close_notch()
# Action methods
def ssfull(self, *args):
exec_shell_command_async(f"bash {SCREENSHOT_SCRIPT} p")
self.close_menu()
def on_ssfull_click(self, button, event):
if event.type == Gdk.EventType.BUTTON_PRESS:
if event.button == 1: # Left click
self.ssfull()
elif event.button == 3: # Right click
exec_shell_command_async(f"bash {SCREENSHOT_SCRIPT} p mockup")
self.close_menu()
return True
return False
def on_ssfull_key(self, widget, event):
if event.keyval in {Gdk.KEY_Return, Gdk.KEY_KP_Enter}:
modifiers = event.get_state()
if modifiers & Gdk.ModifierType.SHIFT_MASK:
exec_shell_command_async(f"bash {SCREENSHOT_SCRIPT} p mockup")
self.close_menu()
else:
self.ssfull()
return True
return False
def ssregion(self, *args):
exec_shell_command_async(f"bash {SCREENSHOT_SCRIPT} sf")
self.close_menu()
def on_ssregion_click(self, button, event):
if event.type == Gdk.EventType.BUTTON_PRESS:
if event.button == 1: # Left click
self.ssregion()
elif event.button == 3: # Right click
exec_shell_command_async(f"bash {SCREENSHOT_SCRIPT} sf mockup")
self.close_menu()
return True
return False
def on_ssregion_key(self, widget, event):
if event.keyval in {Gdk.KEY_Return, Gdk.KEY_KP_Enter}:
modifiers = event.get_state()
if modifiers & Gdk.ModifierType.SHIFT_MASK:
exec_shell_command_async(f"bash {SCREENSHOT_SCRIPT} sf mockup")
self.close_menu()
else:
self.ssregion()
return True
return False
def screenrecord(self, *args):
# Launch screenrecord script in detached mode so that it remains running independently of this program.
exec_shell_command_async(f"bash -c 'nohup bash {SCREENRECORD_SCRIPT} > /dev/null 2>&1 & disown'")
self.close_menu()
# Function to run the pomodoro script
def pomodoro(self, *args):
exec_shell_command_async(f"bash -c 'nohup bash {POMODORO_SCRIPT} > /dev/null 2>&1 & disown'")
self.close_menu()
# Function to check if the pomodoro script is running
def pomodoro_check(self):
try:
result = subprocess.run("pgrep -f pomodoro.sh", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
running = result.returncode == 0
except Exception:
running = False
if running:
self.btn_pomodoro.get_child().set_markup(icons.timer_on)
self.btn_pomodoro.add_style_class("pomodoro")
else:
self.btn_pomodoro.get_child().set_markup(icons.timer_off)
self.btn_pomodoro.remove_style_class("pomodoro")
return True
def ocr(self, *args):
exec_shell_command_async(f"bash {OCR_SCRIPT} sf")
self.close_menu()
def gamemode(self, *args):
exec_shell_command_async(f"bash {GAMEMODE_SCRIPT}")
self.gamemode_check()
self.close_menu()
def gamemode_check(self):
try:
result = subprocess.run(f"bash {GAMEMODE_SCRIPT} check", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
enabled = result.stdout == b't\n'
except Exception:
enabled = False
if enabled:
self.btn_gamemode.get_child().set_markup(icons.gamemode_off)
else:
self.btn_gamemode.get_child().set_markup(icons.gamemode)
return True
def ssregion(self, *args):
exec_shell_command_async(f"bash {SCREENSHOT_SCRIPT} sf")
self.close_menu()
def colorpicker(self, button, event):
if event.type == Gdk.EventType.BUTTON_PRESS:
cmd = {
1: "-hex", # Left click
2: "-hsv", # Middle click
3: "-rgb" # Right click
}.get(event.button)
if cmd:
exec_shell_command_async(f"bash {get_relative_path('../scripts/hyprpicker.sh')} {cmd}")
self.close_menu()
def colorpicker_key(self, widget, event):
if event.keyval in {Gdk.KEY_Return, Gdk.KEY_KP_Enter}:
modifiers = event.get_state()
cmd = "-hex" # Default
match modifiers & (Gdk.ModifierType.SHIFT_MASK | Gdk.ModifierType.CONTROL_MASK):
case Gdk.ModifierType.SHIFT_MASK:
cmd = "-rgb"
case Gdk.ModifierType.CONTROL_MASK:
cmd = "-hsv"
exec_shell_command_async(f"bash {get_relative_path('../scripts/hyprpicker.sh')} {cmd}")
self.close_menu()
return True
return False
def update_screenrecord_state(self):
"""
Checks if the 'gpu-screen-recorder' process is running.
If it is, updates the btn_screenrecord icon to icons.stop and adds the 'recording' style class.
Otherwise, sets the icon back to icons.screenrecord and removes the 'recording' style class.
This function is called periodically every second.
"""
try:
# Use pgrep with -f to check for the process name anywhere in the command line
result = subprocess.run("pgrep -f gpu-screen-recorder", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
running = result.returncode == 0
except Exception:
running = False
if running:
self.btn_screenrecord.get_child().set_markup(icons.stop)
self.btn_screenrecord.add_style_class("recording")
else:
self.btn_screenrecord.get_child().set_markup(icons.screenrecord)
self.btn_screenrecord.remove_style_class("recording")
# Return True to keep this callback active.
return True
def open_screenshots_folder(self, *args):
screenshots_dir = os.path.join(os.environ.get('XDG_PICTURES_DIR',
os.path.expanduser('~/Pictures')),
'Screenshots')
# Create directory if it doesn't exist
os.makedirs(screenshots_dir, exist_ok=True)
exec_shell_command_async(f"xdg-open {screenshots_dir}")
self.close_menu()
def open_recordings_folder(self, *args):
recordings_dir = os.path.join(os.environ.get('XDG_VIDEOS_DIR',
os.path.expanduser('~/Videos')),
'Recordings')
# Create directory if it doesn't exist
os.makedirs(recordings_dir, exist_ok=True)
exec_shell_command_async(f"xdg-open {recordings_dir}")
self.close_menu()
def emoji(self, *args):
self.notch.open_notch("emoji")