-
Notifications
You must be signed in to change notification settings - Fork 1
/
Change_Font.py
170 lines (156 loc) · 5.97 KB
/
Change_Font.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
#// auth_ Noobj2
#// Copyright (c) 2020-2023 Noobj2
from os.path import dirname
import webbrowser
from aqt.webview import AnkiWebView
from aqt import mw, colors
from aqt.qt import *
from aqt.theme import theme_manager
from aqt.utils import showInfo
from anki import version
import anki
from anki.utils import is_mac, is_win
anki_version = int(version.replace('.', ''))
config = mw.addonManager.getConfig(__name__)
def refreshConfig():
global C_font, C_fontSize
C_font = config["Interface Font"]
C_fontSize = config["Font Size"]
class FontDialog(QDialog):
def __init__(self, parent=None):
super(FontDialog, self).__init__(parent)
self.mainWindow()
def mainWindow(self):
addon_path = dirname(__file__)
self.choose_font()
self.setWindowFlags(Qt.WindowType.MSWindowsFixedSizeDialogHint)
self.setLayout(self.layout)
self.setWindowTitle("Anki - Change Font")
self.setWindowIcon(QIcon(addon_path + "/icon.png"))
def choose_font(self):
addon_path = dirname(__file__)
refreshConfig()
font_label = QLabel("Font: ")
font_label.setFixedWidth(100)
self.interface_font = QFontComboBox()
self.interface_font.setFixedWidth(200)
self.interface_font.setCurrentFont(QFont(C_font))
size_label = QLabel("Font Size: ")
size_label.setFixedWidth(100)
self.font_size = QSpinBox()
self.font_size.setFixedWidth(200)
self.font_size.setValue(C_fontSize)
self.font_size.setSuffix("px")
apply_button = QPushButton("&Apply")
apply_button.clicked.connect(lambda: self.onApply())
apply_button.clicked.connect(lambda: self.hide())
restore_button = QPushButton("&Default")
restore_button.clicked.connect(lambda: self.restore_defaults())
restore_button.clicked.connect(lambda: self.hide())
cancel_button = QPushButton("&Cancel")
cancel_button.clicked.connect(lambda: self.hide())
buyMeACoffee_button = QPushButton()
buyMeACoffee_button.setIcon(QIcon(addon_path + "/bmac.png"))
buyMeACoffee_button.setIconSize(QSize(130,40))
buyMeACoffee_button.setGeometry(QRect(1030, 500, 161, 61))
buyMeACoffee_button.clicked.connect(lambda: webbrowser.open('https://www.buymeacoffee.com/noobj2'))
font_line = QHBoxLayout()
font_line.addWidget(font_label)
font_line.addStretch()
font_line.addWidget(self.interface_font)
size_line = QHBoxLayout()
size_line.addWidget(size_label)
size_line.addStretch()
size_line.addWidget(self.font_size)
button_line = QHBoxLayout()
button_line.addWidget(apply_button)
button_line.addWidget(restore_button)
button_line.addWidget(cancel_button)
buyMeACoffee_line = QHBoxLayout()
buyMeACoffee_line.addWidget(buyMeACoffee_button)
self.layout = QVBoxLayout()
self.layout.addLayout(font_line)
self.layout.addLayout(size_line)
self.layout.addLayout(button_line)
self.layout.addLayout(buyMeACoffee_line)
def onApply(self):
conf = {
"Interface Font": self.interface_font.currentFont().family(),
"Font Size": self.font_size.value()
}
mw.addonManager.writeConfig(__name__, conf)
showInfo("Changes will take effect after you restart anki.", title="Anki - Change Font")
def restore_defaults(self):
if is_win:
font = "Segoe UI"
font_size = 12
elif is_mac:
font = "Helvetica"
font_size = 15
else:
font = "Segoe UI"
font_size = 14
conf = {
"Interface Font": font,
"Font Size": font_size
}
mw.addonManager.writeConfig(__name__, conf)
showInfo("Changes will take effect after you restart anki.", title="Anki - Change Font")
def standard_css_new(self) -> str:
color_hl = theme_manager.var(colors.BORDER_FOCUS)
family = config["Interface Font"]
font_size = config["Font Size"]
if is_win:
# T: include a font for your language on Windows, eg: "Segoe UI", "MS Mincho"
button_style = f"""
button {{ font-family: {family}; }}
"""
font = f"font-family: {family};"
elif is_mac:
font = f'font-family:"{family}";'
button_style = """
button {
--canvas: #fff;
-webkit-appearance: none;
background: var(--canvas);
border-radius: var(--border-radius);
padding: 3px 12px;
border: 0.5px solid var(--border);
box-shadow: 0px 1px 3px var(--border-subtle);
font-family: %s
}
.night-mode button { --canvas: #606060; --fg: #eee; }
""" % family
else:
font = f'font-family:"{family}", sans-serif;'
button_style = """
/* Buttons */
button{{
font-family: "{family}", sans-serif;
}}
/* Input field focus outline */
textarea:focus, input:focus, input[type]:focus, .uneditable-input:focus,
div[contenteditable="true"]:focus {{
outline: 0 none;
border-color: {color_hl};
}}
""".format(
family=family,
color_hl=color_hl
)
zoom = self.app_zoom_factor()
return f"""
body {{ zoom: {zoom}; background-color: var(--canvas); font-size: {font_size}px }}
html {{ {font} }}
{button_style}
:root {{ --canvas: {colors.CANVAS["light"]} }}
:root[class*=night-mode] {{ --canvas: {colors.CANVAS["dark"]} }}
"""
def open_window():
font_dialog = FontDialog()
font_dialog.exec()
action = QAction("Change Interface &Font", mw)
action.triggered.connect(open_window)
mw.form.menuTools.addAction(action)
mw.addonManager.setConfigAction(__name__, open_window)
AnkiWebView.standard_css = standard_css_new