-
Notifications
You must be signed in to change notification settings - Fork 0
/
lifepart.pyw
217 lines (165 loc) · 6.8 KB
/
lifepart.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
from PyQt5.QtWidgets import QLabel, QMainWindow, QApplication, QTextEdit, QSizePolicy
from PyQt5.QtCore import QTimer, QThread, QObject, pyqtSignal
from PyQt5.QtGui import QPixmap, QTextCursor
from PyQt5 import QtCore, QtWidgets
from pystray import MenuItem as item
import pystray
from PIL import Image
import sys
import configparser
import threading
import subprocess
import os
import time
from math import floor
import lockTest
os.chdir(os.path.dirname(os.path.realpath(__file__)))
def nt_posix_run(program):
if os.name == 'posix':
subprocess.run(["python", program])
elif os.name == 'nt':
subprocess.run(["pythonw", program])
class Worker(QObject):
finished = pyqtSignal()
def __init__(self):
super().__init__()
def do_work(self):
self.timer = QTimer()
self.timer.timeout.connect(self.loopCheck)
self.timer.start(1000)
# self.finished.emit()
def loopCheck(self):
global big_timer
global small_timer
global big_timer_start
global small_timer_start
global locked
if lockTest.test():
if not locked:
self.report('blocked')
big_timer_start = floor(time.time())
small_timer_start = big_timer_start
locked = True
else:
if locked:
self.report('unblocked')
big_timer_start = floor(time.time())
small_timer_start = big_timer_start
locked = False
timestamp = floor(time.time())
big_timer = timestamp - big_timer_start
small_timer = timestamp - small_timer_start
if small_timer > 300:
self.report('passed', floor(big_timer/60))
small_timer = 0
small_timer_start = timestamp
if big_timer >= 2700:
nt_posix_run("blinker45.pyw")
if big_timer < 3000:
self.report('recommend')
if os.name == 'posix':
self.report('posix hint')
elif os.name == 'nt':
self.report('nt hint')
else:
self.report('recommend at least')
else:
nt_posix_run("blinker5.pyw")
def report(self, message='', data=None):
match message:
case 'blocked':
text = '\nСессия заблокирована, сбрасываем время.'
case 'unblocked':
text = '\nСессия разблокирована, время пошло.'
case 'passed':
text = '\nПрошло ' + str(data) + ' минут.'
case 'recommend':
text = '\nПора сделать 15-минутный перерыв.'
case 'recommend at least':
text = '\nПора сделать хотя бы 15-минутный перерыв.'
case 'posix hint':
text = '\nЗаблокируйте сессию (через i3lock), это сбросит таймер в течение 5 минут)'
case 'nt hint':
text = '\n(Windows+L заблокирует сессию и сбросит таймер в течение 5 минут)'
case _:
text = '\nEmpty message!'
window.textEdit.moveCursor(QTextCursor.End)
window.textEdit.insertPlainText(text)
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("LifePart")
self.setStyleSheet('background-color: ' + config['settings']['background_color'] + '; color: ' + config['settings']['foreground_color'] + ';')
self.setGeometry(0, 0, 460, 170)
x = int((size.width() - self.width()) / 2)
y = int((size.height() - self.height()) / 2)
self.move(x, y)
self.textEdit = QTextEdit(self)
self.textEdit.setGeometry(0, 0, 460, 170) # Set the position and size of the input field
self.textEdit.setReadOnly(True)
self.textEdit.setFontPointSize(10);
self.textEdit.setAlignment(QtCore.Qt.AlignCenter)
self.textEdit.insertPlainText("~ Мигалка ~")
self.textEdit.insertPlainText("\nЭта программа напоминает делать 15-минутный перерыв")
self.textEdit.insertPlainText("\nпосле 45 минут работы и смотреть вдаль каждые 5 минут.")
self.textEdit.insertPlainText("\n(Программа работает в фоне; окно можно просто закрыть)")
self.textEdit.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
wid = QtWidgets.QWidget(self)
self.setCentralWidget(wid)
layout = QtWidgets.QVBoxLayout()
layout.setContentsMargins(0,0,0,0)
layout.addWidget(self.textEdit)
wid.setLayout(layout)
# self.layout = QGridLayout()
# self.layout.addWidget(self.textEdit)
# self.setLayout(self.layout)
# self.closeEvent = quit_window
self.worker = Worker()
self.worker_thread = QThread()
self.worker.moveToThread(self.worker_thread)
self.worker.finished.connect(self.worker_thread.quit)
self.worker_thread.started.connect(self.worker.do_work)
self.worker_thread.start()
self.show()
def closeEvent(self, event):
event.ignore()
toggle_window()
config = configparser.ConfigParser()
config.read('config.ini')
def quit_window():
config['settings']['show_cmd'] = str(not sh)
with open('config.ini', 'w') as configfile:
config.write(configfile)
os._exit(0)
def toggle_window():
global sh
if sh:
sh = False
window.show()
else:
sh = True
window.hide()
def showIcon():
image=Image.open("flower.png")
menu=(item('Показать/скрыть', toggle_window), item('Выход', quit_window))
icon=pystray.Icon('name', image, 'LifePart', menu)
icon.run()
x = threading.Thread(target=showIcon, args=())
x.start()
sh = config['settings']['show_cmd']
if sh == "False":
sh = False
else:
sh = True
# toggle_window()
small_timer = 0
big_timer = 0
big_timer_start = floor(time.time())
small_timer_start = big_timer_start
locked = False
App = QApplication(sys.argv)
screen = App.primaryScreen()
size = screen.size()
nt_posix_run("blinker45.pyw")
window = Window()
sys.exit(App.exec())