generated from songquanpeng/gofile-launcher
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrest_thread.py
103 lines (91 loc) · 3.58 KB
/
rest_thread.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
import ctypes
import math
import os
import random
import time
from threading import Thread, Event
import pyautogui
from sentences import rest_sentences, work_sentences
is_windows = os.name == "nt"
class RestThread(Thread):
def __init__(self, main, debug=False):
super().__init__()
self.main = main
self.working = True
self.paused = False
self.count = 0
self.work_interval = int(self.main.config['workInterval'].split(' ')[0])
self.rest_interval = int(self.main.config['restInterval'].split(' ')[0])
self._stop_event = Event()
self._pause_event = Event()
self.method = self.main.config['remindMethod']
self.remind_work_text = self.main.config['remindWorkText']
self.remind_rest_text = self.main.config['remindRestText']
self.seconds_per_minute = 60
self.title = self.main.config["titleText"]
if debug:
self.seconds_per_minute = 1
def remind_user(self, message):
if self.method == "消息提醒":
self.main.tray_message_signal.emit(self.title, message)
elif self.method == "弹窗提醒":
self.main.notify_message_box_signal.emit(message)
elif self.method == "显示桌面":
try:
pyautogui.hotkey('win', 'd')
except Exception as e:
print(e)
self.main.statusbar.showMessage(str(e))
elif self.method == "强制锁屏":
if self.working:
self.main.tray_message_signal.emit(self.title, message)
else:
if is_windows:
ctypes.windll.user32.LockWorkStation()
else:
self.main.tray_message_signal.emit(self.title, message)
def run(self):
while not self.should_stop():
while self.should_pause():
time.sleep(1)
sleep_seconds = 1
if self.working:
sleep_seconds *= self.seconds_per_minute
time.sleep(sleep_seconds)
if self.should_stop():
return
while self.should_pause():
time.sleep(1)
self.count += 1
if self.working:
self.main.update_work_progress_signal.emit(math.ceil(100 * self.count / self.work_interval))
if self.count == self.work_interval:
self.working = False
self.count = 0
if self.remind_rest_text == "":
prompt = random.choice(rest_sentences)
else:
prompt = self.remind_rest_text
self.remind_user(prompt)
else:
self.main.update_rest_progress_signal.emit(math.ceil(100 * self.count / self.rest_interval))
if self.count == self.rest_interval:
self.working = True
self.count = 0
self.main.update_work_progress_signal.emit(0)
self.main.update_rest_progress_signal.emit(0)
if self.remind_work_text == "":
prompt = random.choice(work_sentences)
else:
prompt = self.remind_work_text
self.remind_user(prompt)
def stop(self):
self._stop_event.set()
def should_stop(self):
return self._stop_event.is_set()
def pause(self):
self._pause_event.set()
def resume(self):
self._pause_event.clear()
def should_pause(self):
return self._pause_event.is_set()