-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
300 lines (206 loc) · 8.35 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
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
import sys
import pytz
from datetime import datetime, timedelta, tzinfo
from PyQt5.QtCore import Qt, QEvent, QTimer, QPoint
from PyQt5.QtGui import QIcon, QFont, QFontDatabase, QGuiApplication
from PyQt5.QtWidgets import QApplication, qApp, QWidget, QSystemTrayIcon, QLabel, QMenu, QHBoxLayout
#TODO: Save these in a settings file and expose them in the settings window
pre_window_length = 2
window_length = 30
timezone = pytz.timezone("Asia/Tokyo")
font = None
class EventWindow(object):
pre = None
def __init__(self, start):
self.start = datetime.strptime(start, "%H:%M")
self.end = self.start + timedelta(minutes=window_length)
if pre_window_length > 0:
self.pre = self.start - timedelta(minutes=pre_window_length)
def check(self, ct):
s = self.start.replace(year=ct.year, month=ct.month, day=ct.day)
e = self.end.replace(year=ct.year, month=ct.month, day=ct.day)
return ct.time() >= s.time() and ct.time() < e.time()
def check_pre(self, ct):
if self.pre is None:
return False
p = self.pre.replace(year=ct.year, month=ct.month, day=ct.day)
s = self.start.replace(year=ct.year, month=ct.month, day=ct.day)
return ct.time() >= p.time() and ct.time() < s.time()
windows = [
EventWindow("01:00"),
EventWindow("07:30"),
EventWindow("12:00"),
EventWindow("19:30"),
EventWindow("22:30")
]
class Notification(QWidget):
target_seconds = 0.5 # TODO: Expose this in settings window
framerate = 60
transition_complete = True
visible = False
frame = 0
target_frames = 0
transition_timer = None
def __init__(self, parent=None):
super().__init__(parent)
self.init()
def init(self):
self.setWindowFlags(Qt.FramelessWindowHint|Qt.WindowStaysOnTopHint)
self.setAttribute(Qt.WA_ShowWithoutActivating)
self.setWindowTitle("SINoALICE Event Window Status")
self.setWindowIcon(QIcon("icon.png"))
self.resize(300, 54)
self.setStyleSheet("background-color: #3b3325;")
layout = QHBoxLayout()
self.label = QLabel("Default notification text", font=font)
self.label.setStyleSheet("color: #ffffff;")
layout.addWidget(self.label)
self.setLayout(layout)
# TODO: Let the user to pick which screen to display the notification on
self.framerate = QGuiApplication.primaryScreen().refreshRate()
self.target_frames = self.framerate * self.target_seconds
# TODO: Let the user pick the corner that the notification shows up in
desktop = QApplication.desktop()
sg = desktop.availableGeometry().bottomRight()
p = QPoint(sg.x() - self.width() - 1, sg.y() - self.height())
self.move(p)
def start_show_transition(self):
self.fade_in = True
self.setWindowOpacity(0)
self.show()
self._start_transition()
def start_hide_transition(self):
self.fade_in = False
self.setWindowOpacity(1)
self._start_transition()
def _start_transition(self):
self.transition_complete = False
self.frame = 0
self.transition_timer = QTimer(self)
self.transition_timer.setSingleShot(False)
self.transition_timer.timeout.connect(self.update_transition)
self.transition_timer.start((1 / self.framerate) * 1000)
def update_transition(self):
self.frame += 1
if self.fade_in:
opacity = self.frame / self.target_frames
else:
opacity = (self.target_frames - self.frame) / self.target_frames
self.setWindowOpacity(opacity)
if self.frame == self.target_frames:
self.transition_timer.stop()
self.transition_complete = True
if self.fade_in:
self.visible = True
else:
self.visible = False
super().hide()
def show_regular(self):
self.label.setText("Upgrade Fodder window open!")
# TODO: Play sound?
if self.visible:
return
self.start_show_transition()
def show_pre(self):
self.label.setText("A window is about to open!")
#TODO: Play sound?
if self.visible:
return
self.start_show_transition()
def hide(self):
self.start_hide_transition()
class Window(QWidget):
notification = None
timer = None
def __init__(self):
super().__init__()
self.init()
self.notification = Notification()
self.tray_icon = TrayIcon(QIcon("icon.png"), self)
self.tray_icon.show()
self.update()
def init(self):
self.setWindowTitle("SINoALICE Upgrade Fodder Event Alerter")
self.setWindowIcon(QIcon("icon.png"))
self.resize(300, 75)
self.temp_label = QLabel("There'll be options here later", self, font=font)
frame = self.frameGeometry()
center_point = QApplication.desktop().availableGeometry().center()
frame.moveCenter(center_point)
self.move(center_point - self.rect().center())
self.timer = QTimer(self)
self.timer.setSingleShot(False)
self.timer.timeout.connect(self.update)
self.timer.start(1000 * 30)
self.show()
def update(self):
ct = datetime.now(timezone).replace(tzinfo=None)
in_pre_window = False
in_window = False
for t in windows:
if t.check_pre(ct):
in_pre_window = True
break
if t.check(ct):
in_window = True
break
if in_pre_window:
self.notification.show_pre()
elif in_window:
self.notification.show_regular()
else:
self.notification.hide()
def unhide(self):
self.setWindowState(Qt.WindowActive)
QTimer.singleShot(0, self.show)
def changeEvent(self, event):
if event.type() == QEvent.WindowStateChange:
if self.windowState() & Qt.WindowMinimized:
QTimer.singleShot(0, self.hide)
super().changeEvent(event)
def closeEvent(self, event):
self.tray_icon.hide()
if self.notification.isVisible():
self.notification.close()
event.accept()
class TrayIcon(QSystemTrayIcon):
def __init__(self, icon, parent=None):
super().__init__(icon, parent)
self.setToolTip("SINoALICE Upgrade Fodder Event Alerter")
menu = QMenu(parent)
show_action = menu.addAction("Show")
show_action.triggered.connect(parent.unhide)
menu.addSeparator()
next_window_action = menu.addAction("Next Window")
next_window_action.triggered.connect(self.next_window)
menu.addSeparator()
exit_action = menu.addAction("Exit")
exit_action.triggered.connect(parent.close)
self.setContextMenu(menu)
self.activated.connect(self.clicked)
def next_window(self):
ct = datetime.now(timezone).replace(tzinfo=None)
diff = None
for window in windows:
wt = window.start.replace(year=ct.year, month=ct.month, day=ct.day)
if wt.time() < ct.time():
continue
diff = wt - ct
break
if diff is None:
wt = windows[0].start.replace(year=ct.year, month=ct.month, day=ct.day + 1)
diff = wt - ct
hours, remainder = divmod(diff.seconds, 3600)
minutes, seconds = divmod(remainder, 60)
self.showMessage("Next Window", "The next window opens in {:02d}:{:02d}:{:02d}".format(hours, minutes, seconds))
def clicked(self, reason):
if reason == QSystemTrayIcon.DoubleClick or reason == QSystemTrayIcon.MiddleClick:
self.parent().unhide()
elif reason == QSystemTrayIcon.Trigger:
self.next_window()
if __name__ == "__main__":
app = QApplication(sys.argv)
QFontDatabase.addApplicationFont("./font.otf")
font = QFont("FOT-Pearl Std L", 16)
window = Window()
sys.exit(app.exec_())