Skip to content

Commit

Permalink
fix: toggle button
Browse files Browse the repository at this point in the history
  • Loading branch information
psii committed Mar 20, 2022
1 parent a77600d commit bc1eda4
Showing 1 changed file with 38 additions and 11 deletions.
49 changes: 38 additions & 11 deletions chinese/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,58 +18,85 @@
# Chinese Support Redux. If not, see <https://www.gnu.org/licenses/>.

from aqt import mw, gui_hooks
from aqt.theme import theme_manager
from aqt.editor import Editor

from .behavior import update_fields
from .main import config


def webviewDidInit(web_content, context):
if isinstance(context, Editor):
web_content.head += """<script>
function chineseSupport_activateButton() {
jQuery('#chineseSupport').addClass('active');
}
function chineseSupport_deactivateButton() {
jQuery('#chineseSupport').removeClass('active');
}
</script>
"""

class EditManager:
def __init__(self):
gui_hooks.editor_did_init_buttons.append(self.setupButton)
gui_hooks.editor_did_load_note.append(self.updateButton)
gui_hooks.editor_did_unfocus_field.append(self.onFocusLost)
gui_hooks.webview_will_set_content.append(webviewDidInit)
self.editors = []

def setupButton(self, buttons, editor):
self.editors.append(editor)
self.buttonOn = False

# setting toggleable=False because this is currently broken in Anki 2.1.49.
# We implement our own toggleing mechanism here.
button = editor.addButton(
icon=None,
func=self.onToggle,
cmd='chineseSupport',
tip='Chinese Support',
label='<b>汉字</b>',
id='chineseSupport',
toggleable=True)
toggleable=False)
if theme_manager.night_mode:
btnclass = "btn-night"
else:
btnclass = "btn-day"
# this svelte-9lxpor class is required and was found by looking at the DOM
# for the other buttons in Anki 2.1.49. No idea how stable this class
# name is, though.
button = button.replace('class="', f'class="btn {btnclass} svelte-9lxpor ')

buttons.append(button)

def onToggle(self, editor):
self.buttonOn = not self.buttonOn

mid = str(editor.note.note_type()['id'])
enabled = mid in config['enabledModels']

if self.buttonOn and mid not in config['enabledModels']:
enabled = not enabled
if enabled:
config['enabledModels'].append(mid)
elif not self.buttonOn and mid in config['enabledModels']:
editor.web.eval("chineseSupport_activateButton()")
else:
config['enabledModels'].remove(mid)
editor.web.eval("chineseSupport_deactivateButton()")

config.save()

def updateButton(self, editor):
enabled = str(editor.note.note_type()['id']) in config['enabledModels']

if (enabled and not self.buttonOn) or (not enabled and self.buttonOn):
editor.web.eval('toggleEditorButton(chineseSupport);')
self.buttonOn = not self.buttonOn
if enabled:
editor.web.eval("chineseSupport_activateButton()")
else:
editor.web.eval("chineseSupport_deactivateButton()")

def _refreshAllEditors(self, focusTo):
for editor in self.editors:
editor.loadNote(focusTo=focusTo)

def onFocusLost(self, _, note, index):
if not self.buttonOn:
enabled = str(note.note_type()['id']) in config['enabledModels']
if not enabled:
return False

allFields = mw.col.models.field_names(note.note_type())
Expand Down

0 comments on commit bc1eda4

Please sign in to comment.