Skip to content

Commit

Permalink
Update to support Anki > 2.1.56
Browse files Browse the repository at this point in the history
  • Loading branch information
noobj2 committed May 11, 2023
1 parent 9abf4f8 commit 08517ff
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 240 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Release v2.0.0

- Compatible with Anki versions > 2.1.56

# Release v1.0.0

- Compatible with Anki versions 2.1.20 -> 2.1.56
246 changes: 6 additions & 240 deletions Change_Font.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
#// auth_ Mohamad Janati
#// Copyright (c) 2020-2023 Mohamad Janati (freaking stupid, right? :|)
#// auth_ Noobj2
#// Copyright (c) 2020-2023 Noobj2

from os.path import dirname
import webbrowser
from aqt.webview import AnkiWebView
from aqt.webview import WebContent
from typing import Optional, List, Any
from aqt import mw
from aqt import gui_hooks
from aqt import mw, colors
from aqt.qt import *
from aqt.theme import theme_manager
from aqt.utils import showInfo
Expand Down Expand Up @@ -108,232 +105,8 @@ def restore_defaults(self):
mw.addonManager.writeConfig(__name__, conf)
showInfo("Changes will take effect after you restart anki.", title="Anki - Change Font")

def stdHtml_new(
self,
body: str,
css: Optional[List[str]] = None,
js: Optional[List[str]] = None,
head: str = "",
context: Optional[Any] = None,
):

web_content = WebContent(
body=body,
head=head,
js=["webview.js"] + (["jquery.js"] if js is None else js),
css=["webview.css"] + ([] if css is None else css),
)

gui_hooks.webview_will_set_content(web_content, context)

palette = self.style().standardPalette()
color_hl = palette.color(QPalette.ColorRole.Highlight).name()

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"
# family = _('"Courier"')
widgetspec = "button { font-family:%s; }" % family
widgetspec += "\n:focus { outline: 1px solid %s; }" % color_hl
fontspec = "font-size:{}px; font-family:{};".format(font_size, family)
elif is_mac:
# family = "Helvetica"
fontspec = 'font-size:{}px; font-family:"{}";'.format(font_size, family)
widgetspec = """
button { -webkit-appearance: none; background: #fff; border: 1px solid #ccc;
border-radius:5px; font-family: %s }""" % family
else:
# family = self.font().family()
color_hl_txt = palette.color(QPalette.ColorRole.HighlightedText).name()
color_btn = palette.color(QPalette.Button).name()
fontspec = 'font-size:{}px;font-family:"{}";'.format(font_size, family)
widgetspec = """
/* Buttons */
button{
background-color: %(color_btn)s;
font-family:"%(family)s"; }
button:focus{ border-color: %(color_hl)s }
button:active, button:active:hover { background-color: %(color_hl)s; color: %(color_hl_txt)s;}
/* 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)s;
}""" % {
"family": family,
"color_btn": color_btn,
"color_hl": color_hl,
"color_hl_txt": color_hl_txt,
}

csstxt = "\n".join(self.bundledCSS(fname) for fname in web_content.css)
jstxt = "\n".join(self.bundledScript(fname) for fname in web_content.js)

from aqt import mw

head = mw.baseHTML() + csstxt + jstxt + web_content.head

body_class = theme_manager.body_class()

html = """
<!doctype html>
<html><head>
<title>{}</title>
<style>
body {{ zoom: {}; background: {}; {} }}
{}
</style>
{}
</head>
<body class="{}">{}</body>
</html>""".format(
self.title,
self.zoomFactor(),
self._getWindowColor().name(),
fontspec,
widgetspec,
head,
body_class,
web_content.body,
)
# print(html)
self.setHtml(html)

def stdHtml_old(self, body, css=None, js=None, head=""):
if css is None:
css = []
if js is None:
js = ["jquery.js"]

palette = self.style().standardPalette()
color_hl = palette.color(QPalette.ColorRole.Highlight).name()

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"
# family = _('"Segoe UI"')
widgetspec = "button { font-size: 12px; font-family:%s; }" % family
widgetspec += "\n:focus { outline: 1px solid %s; }" % color_hl
fontspec = 'font-size:{}px; font-family:"{}";'.format(font_size, family)
elif is_mac:
# family="Helvetica"
fontspec = 'font-size:{}px; font-family:"{}";'.format(font_size, family)
widgetspec = """
button { font-size: 13px; -webkit-appearance: none; background: #fff; border: 1px solid #ccc;
border-radius:5px; font-family: %s }""" % family
else:
# family = self.font().family()
color_hl_txt = palette.color(QPalette.ColorRole.HighlightedText).name()
color_btn = palette.color(QPalette.Button).name()
fontspec = 'font-size:{}px; font-family:"{}";'.format(font_size, family)
widgetspec = """
/* Buttons */
button{ font-size:14px; -webkit-appearance:none; outline:0;
background-color: %(color_btn)s; border:1px solid rgba(0,0,0,.2);
border-radius:2px; height:24px; font-family:"%(family)s"; }
button:focus{ border-color: %(color_hl)s }
button:hover{ background-color:#fff }
button:active, button:active:hover { background-color: %(color_hl)s; color: %(color_hl_txt)s;}
/* 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)s;
}""" % {"family": family, "color_btn": color_btn,
"color_hl": color_hl, "color_hl_txt": color_hl_txt}

csstxt = "\n".join([self.bundledCSS("webview.css")]+
[self.bundledCSS(fname) for fname in css])
jstxt = "\n".join([self.bundledScript("webview.js")]+
[self.bundledScript(fname) for fname in js])
from aqt import mw
head = mw.baseHTML() + head + csstxt + jstxt

html = """
<!doctype html>
<html><head>
<title>{}</title>
<style>
body {{ zoom: {}; background: {}; {} }}
{}
</style>
{}
</head>
<body>{}</body>
</html>""".format(self.title, self.zoomFactor(), self._getWindowColor().name(),
fontspec, widgetspec, head, body)
#print(html)
self.setHtml(html)


def standard_css_old(self) -> str:
palette = self.style().standardPalette()
color_hl = palette.color(QPalette.ColorRole.Highlight).name()

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 = "button { font-size: 12px; font-family:%s; }" % family
button_style += "\n:focus { outline: 1px solid %s; }" % color_hl
font = "font-size:{}px;font-family:{};".format(font_size, family)
elif is_mac:
font = 'font-size:{}px;font-family:"{}";'.format(font_size, family)
button_style = """
button { -webkit-appearance: none; background: #fff; border: 1px solid #ccc;
border-radius:5px; font-family: %s }""" % family
else:
color_hl_txt = palette.color(QPalette.ColorRole.HighlightedText).name()
color_btn = palette.color(QPalette.Button).name()
font = 'font-size:{}px;font-family:"{}";'.format(font_size, family)
button_style = """
/* Buttons */
button{
background-color: %(color_btn)s;
font-family:"%(family)s"; }
button:focus{ border-color: %(color_hl)s }
button:active, button:active:hover { background-color: %(color_hl)s; color: %(color_hl_txt)s;}
/* 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)s;
}""" % {
"family": family,
"color_btn": color_btn,
"color_hl": color_hl,
"color_hl_txt": color_hl_txt,
}

zoom = self.zoomFactor()
background = self._getWindowColor().name()

if is_rtl(anki.lang.currentLang):
lang_dir = "rtl"
else:
lang_dir = "ltr"

return f"""
body {{ zoom: {zoom}; background: {background}; direction: {lang_dir}; {font} }}
{button_style}
:root {{ --window-bg: {background} }}
:root[class*=night-mode] {{ --window-bg: {background} }}
"""

def standard_css_new(self) -> str:
palette = theme_manager.default_palette
color_hl = palette.color(QPalette.ColorRole.Highlight).name()
color_hl = theme_manager.var(colors.BORDER_FOCUS)

family = config["Interface Font"]
font_size = config["Font Size"]
Expand All @@ -349,7 +122,7 @@ def standard_css_new(self) -> str:
button { -webkit-appearance: none; background: #fff; border: 1px solid #ccc;
border-radius:5px; font-family: %s }""" % family
else:
color_hl_txt = palette.color(QPalette.ColorRole.HighlightedText).name()
color_hl_txt = palette.color(QPalette.HighlightedText).name()
color_btn = palette.color(QPalette.Button).name()
font = f'font-size:{font_size}px;font-family:"{family}";'
button_style = """
Expand Down Expand Up @@ -398,11 +171,4 @@ def open_window():
action.triggered.connect(open_window)
mw.form.menuTools.addAction(action)
mw.addonManager.setConfigAction(__name__, open_window)
if anki_version < 2122:
AnkiWebView.stdHtml = stdHtml_old
elif 2121 < anki_version < 2136:
AnkiWebView.stdHtml = stdHtml_new
elif 2135 < anki_version < 2145:
AnkiWebView.standard_css = standard_css_old
else:
AnkiWebView.standard_css = standard_css_new
AnkiWebView.standard_css = standard_css_new

0 comments on commit 08517ff

Please sign in to comment.