-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.pyw
269 lines (219 loc) · 11 KB
/
main.pyw
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
import time
import sys
import multiprocess
import webbrowser
import PySimpleGUI as sg
import pyautogui as pag
import keyboard
import logging
from app.settings.helpers.functions import (get_latest_version, create_process, countdown, graceful_exit, get_hotkey, correct_key,
is_capslock_on, terminate)
from threading import Thread, Event
from app.settings.helpers.mouse_jiggler import jiggler
from app.settings.helpers.configurator import Configurator
logging.basicConfig(filename='app/settings/helpers/log.log', encoding='utf-8', level=logging.INFO,
format='%(asctime)s | %(message)s', datefmt='%Y/%m/%d %I:%M:%S %p')
def about_window():
layout = [[sg.T(s=40)],
[sg.Push(), sg.T(str(WINDOW_TITLE), font=(FONT_FAMILY, 12, "bold")), sg.Push()],
[sg.Push(), sg.T("Prevent your PC from sleeping with - 'Don't sleep' \nor X-Sleep for short.",
font=(FONT_FAMILY, 9, "italic"), justification='c', text_color='grey'), sg.Push()],
[sg.T()],
[sg.Push(), sg.T(github_url['name'], enable_events=True, font=(FONT_FAMILY, 10, "underline"),
justification='l', text_color='#0066CC',
auto_size_text=True, key='-LINK-'), sg.Push()],
[sg.Push(), sg.T("License: GPL-3.0", justification='c'), sg.Push()],
[sg.T()],
[sg.Push(), sg.T("Copyright © 2023 Kaloian Kozlev", text_color='light grey'), sg.Push()]]
window = sg.Window("About", layout, icon=ICON)
while True:
event, values = window.read()
match event:
case sg.WIN_CLOSED:
break
case '-LINK-':
webbrowser.open(github_url['url'])
window.close()
def new_version_check(c_release, c_release_name, l_release, l_release_name, down_url):
global update_check
layout = [[sg.T(s=40)],
[sg.T(font=(FONT_FAMILY, 10), justification='l', key="-INFO-")],
[sg.T()],
[sg.T('Current Version is :', justification='l', font=(FONT_FAMILY, 10)),
sg.T(f'{c_release_name}', font=(FONT_FAMILY, 10))],
[sg.T('Available Version is:', justification='l', font=(FONT_FAMILY, 10)),
sg.T(f'{l_release_name}', font=(FONT_FAMILY, 10))],
[sg.T()],
[sg.Push(),
sg.B('Yes', key='-DOWNLOAD-', s=8, button_color='#93b7a6'),
sg.B(key='-EXIT-', s=8, button_color='#db5656'),
sg.Push()]]
window = sg.Window("Update Available", layout, icon=ICON, keep_on_top=True, finalize=True)
if l_release is None:
message = "Cannot fetch version data! \nPlease check your network connection."
title = "Error"
download_enabled = False
exit_text = "Exit"
elif l_release <= c_release:
message = "You have the latest version!"
title = "No update available"
download_enabled = False
exit_text = "Exit"
else:
message = "An update is available, do you want to download it?"
title = "Update available"
download_enabled = True
exit_text = "No"
window['-INFO-'].update(message)
window.set_title(title)
window['-DOWNLOAD-'].update(visible=download_enabled)
window['-EXIT-'].update(exit_text)
while True:
event, values = window.read()
match event:
case sg.WIN_CLOSED:
break
case '-DOWNLOAD-':
webbrowser.open(down_url)
window.close()
case '-EXIT-':
window.close()
update_check = False
def main_window():
global hot_key, update_check, bgp
app_menu = [['Help', ['About', 'Check for Updates']]]
layout = [[sg.Menubar(app_menu)],
[sg.Frame('Hotkey',
[[sg.I(disabled=True, default_text=hot_key, justification='c',
disabled_readonly_text_color='grey', disabled_readonly_background_color='#dae0e6',
key='-HT_KEY-', tooltip="ALT, CTRL, SHIFT, WINDOWS, A-Z, 0-9, F1-F12")],
[sg.Checkbox('Change', key='-CHANGE-', enable_events=True), sg.Push(),
sg.B('Reset', size=8, key='-RESET-'),
sg.B('Apply', size=8, disabled=True, disabled_button_color='light grey', key='-APPLY-')]
], expand_x=True)],
[sg.Frame('Timer',
[[sg.T('Hours:'), sg.DropDown(HOURS, default_value=' 00', key='-H-', disabled=True,
readonly=True, button_background_color='#93b7a6', s=(3, 1)),
sg.T('Minutes:'), sg.DropDown(MINUTES, default_value=' 00', key='-M-', disabled=True,
readonly=True, button_background_color='#93b7a6', s=(3, 1)),
sg.T('Seconds:'), sg.DropDown(SECONDS, default_value=' 00', key='-S-', disabled=True,
readonly=True, button_background_color='#93b7a6', s=(3, 1))
],
[sg.Radio('Off', 'timer', default=True, enable_events=True, key='-OFF-'),
sg.Radio('On', 'timer', enable_events=True, key='-ON-'), sg.Push(),
sg.I(background_color='#dae0e6', size=8, key='-LOG_TIME-', justification='c',
default_text='00:00:00', disabled=True, disabled_readonly_text_color='grey',
disabled_readonly_background_color='#dae0e6', readonly=True)]
], expand_x=True)],
[sg.Frame('Log',
[[sg.Input(background_color='#dae0e6', size=45, key='-LOG-', justification='c',
text_color='white')]], expand_x=True)],
[sg.Button('Start', size=8, button_color='#93b7a6', disabled_button_color='light grey',
key='-START-'),
sg.Button('Stop', size=8, button_color='#ffcf61', disabled=True,
disabled_button_color='light grey', key='-STOP-'),
sg.Button('Exit', size=8, button_color='#db5656')]
]
window = sg.Window(WINDOW_TITLE, layout, keep_on_top=False)
while True:
event, values = window.read(timeout=10)
if update_check:
new_version_check(RELEASE, RELEASE_NAME, latest_release, latest_release_name, download_url)
keyboard.add_hotkey(hot_key, lambda: graceful_exit(thread_event, window, pag))
if event in ('Exit', sg.WIN_CLOSED):
break
if event == '-START-':
window['-STOP-'].update(disabled=False)
window['-STOP-'].update(button_color='#ffcf61')
window['-START-'].update(disabled=True)
bgp = create_process(jiggler, pag)
bgp.daemon = True
if values['-ON-'] and values['-H-'] == ' 00' and values['-M-'] == ' 00' and values['-S-'] == ' 00':
window.write_event_value('-OFF-', True)
window['-OFF-'].update(True)
elif values['-ON-'] and (values['-H-'] != ' 00' or values['-M-'] != ' 00' or values['-S-'] != ' 00'):
Thread(target=countdown,
args=(values['-H-'], values['-M-'], values['-S-'], window, thread_event, bgp),
daemon=True).start()
bgp.start()
window['-LOG-'].update('Application running', background_color='#5fad65')
elif event == '-STOP-':
is_capslock_on(pag)
if values['-ON-'] and values['-LOG_TIME-'] != '00:00:00':
thread_event.set()
else:
terminate(window, bgp)
# Events for Frame - Hotkey
if values['-CHANGE-']:
window['-HT_KEY-'].update(disabled=False)
window['-HT_KEY-'].update(text_color='black')
window['-APPLY-'].update(disabled=False)
window['-APPLY-'].update(button_color='#93b7a6')
else:
window['-HT_KEY-'].update(hot_key)
window['-HT_KEY-'].update(disabled=True)
window['-HT_KEY-'].update(text_color='grey')
window['-APPLY-'].update(disabled=True)
if event == '-APPLY-':
wrong_values, format_str = correct_key(text=values['-HT_KEY-'])
if not wrong_values:
conf.hot_key_state = True
hot_key = conf.cust_hot_key = format_str
conf.save_config_file()
window['-CHANGE-'].update(False)
else:
window['-LOG-'].update(
f'{wrong_values} {"key is" if len(wrong_values) == 1 else "keys are"} not valid!',
background_color='#db5656')
window.refresh()
time.sleep(2)
window['-LOG-'].update('', background_color='#dae0e6')
if event == '-RESET-':
conf.hot_key_state = False
conf.cust_hot_key = ''
conf.save_config_file()
hot_key = get_hotkey(conf)
window['-HT_KEY-'].update(hot_key)
window['-CHANGE-'].update(False)
# Events for Frame - Timer
if event == '-ON-':
window['-H-'].update(disabled=False)
window['-M-'].update(disabled=False)
window['-S-'].update(disabled=False)
window['-LOG_TIME-'].update(disabled=False, text_color='black')
elif event == '-OFF-':
window['-H-'].update(disabled=True, value=' 00')
window['-M-'].update(disabled=True, value=' 00')
window['-S-'].update(disabled=True, value=' 00')
window['-LOG_TIME-'].update(disabled=True, text_color='grey')
# Events for menubar
if event == 'About':
about_window()
if event == 'Check for Updates':
new_version_check(RELEASE, RELEASE_NAME, latest_release, latest_release_name, download_url)
graceful_exit(thread_event, window, pag)
if __name__ == '__main__':
if sys.platform.startswith('win'):
multiprocess.freeze_support()
RELEASE_NAME = '2.0.2'
RELEASE = int(''.join(filter(lambda x: x.isdigit(), RELEASE_NAME)))
WINDOW_TITLE = "X-Sleep"
FONT_FAMILY = "Arial"
HOURS = [f" {i:02}" for i in range(24)]
MINUTES = [f" {i:02}" for i in range(60)]
SECONDS = [f" {i:02}" for i in range(60)]
ICON = 'app/media/x-sleep.ico'
github_url = {'name': 'Official GitHub Page',
'url': 'https://github.com/kbkozlev/x-sleepGUI'}
sg.theme("Reddit")
sg.set_options(force_modal_windows=True, dpi_awareness=True, use_ttk_buttons=True, icon=ICON)
thread_event = Event()
conf = Configurator()
conf.create_on_start()
hot_key = get_hotkey(conf)
update_check = False
bgp = None
latest_release, latest_release_name, download_url = get_latest_version()
if latest_release is not None and latest_release > RELEASE:
update_check = True
main_window()