-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
148 lines (113 loc) · 4.05 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
import json
from aqt.qt import (
QSplitter,
QUrl,
Qt,
QVariantAnimation,
QEasingCurve,
QVariant
)
from aqt import gui_hooks, mw, pyqtSlot
from aqt.webview import AnkiWebView
from .settings import SettingsDialog
from . import consts
mw.addonManager.setWebExports(__name__, r"web/.*")
class tetrisWidget:
webview = None
ankiWebWidget = None
splitter = None
injected = False
counter = 0
threshold = 10
def inject(self):
if self.injected == False:
self.ankiWebWidget = mw.web
self.splitter = QSplitter()
self.splitter.setOrientation(Qt.Orientation.Horizontal)
self.splitter.setHandleWidth(0)
self.splitter.setStyleSheet(
"QSplitter::handle { image: url('data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=='); }"
)
self.webview = AnkiWebView()
self.webview.set_open_links_externally(False)
centralWidget = mw.web
# If no widgets are wrapping the main webview
if mw.web.parentWidget().objectName() != "centralwidget":
centralWidget = mw.web.parentWidget()
mw.mainLayout.removeWidget(centralWidget)
self.splitter.insertWidget(1, centralWidget)
self.splitter.insertWidget(2, self.webview)
mw.mainLayout.insertWidget(1, self.splitter)
self.injected = True
self.closeSidebar()
self.threshold = int(consts.CONFIG["cardsToPlay"])
def navigate(self, path):
url = f"""http://{consts.HOST}:{consts.MEDIA_SERVER.getPort()}/_addons/{consts.ADDON_NAME}/{path}"""
self.webview.load(QUrl(url))
def openSidebar(self):
consts.LINES_CLEARED = 0
self.webview.setFixedWidth(consts.PANEL_WIDTH)
self.webview.setFocus()
def closeSidebar(self):
self.webview.setFixedWidth(0)
self.ankiWebWidget.setFocus()
def saveData(self):
js = f"""
window.tetris.save()
"""
if(self.webview):
self.webview.eval(js)
def loadData(self):
with open(consts.PATH_TO_DATAFILE) as f:
data = f.read()
js = f"""
window.tetris.loadSettings({json.dumps(consts.CONFIG)})
window.tetris.load({data})
"""
self.webview.eval(js)
def uninject(self):
if self.injected == True:
# self.ankiWebWidget = mw.web
mw.mainLayout.removeWidget(self.splitter)
mw.mainLayout.insertWidget(1, self.ankiWebWidget)
self.injected = False
# Show Answer
def showAnswer(card) -> None:
if tetris.injected == False:
tetris.inject()
tetris.navigate(consts.PATH_TO_HTML)
else:
tetris.counter = tetris.counter + 1
if tetris.counter >= tetris.threshold:
tetris.counter = 0
tetris.openSidebar()
tetris = tetrisWidget()
# Close / Collapse
def collapseAddon():
tetris.saveData()
tetris.closeSidebar()
def on_js_message(handled, msg, context):
if msg.startswith("tetris::linesCleared"):
consts.LINES_CLEARED += int(msg.replace("tetris::linesCleared::", ""))
if consts.LINES_CLEARED >= int(consts.CONFIG["linesPerPlay"]):
tetris.closeSidebar()
return True, None
if msg.startswith("tetris::save"):
data = json.loads(msg.replace("tetris::save::", ""))
with open(consts.PATH_TO_DATAFILE, "w") as f:
json.dump(data, f)
return True, None
if msg.startswith("tetris::load"):
tetris.loadData()
return True, None
return handled
def init():
gui_hooks.reviewer_did_show_answer.append(showAnswer)
# Save before ending review
gui_hooks.reviewer_will_end.append(collapseAddon)
# Save before closing
gui_hooks.profile_will_close.append(collapseAddon)
gui_hooks.webview_did_receive_js_message.append(on_js_message)
mw.addonManager.setConfigAction(__name__, SettingsDialog().open)
def unload():
tetris.uninject()