From bb7031e26c63076820f7863bbf6fb105f66bca73 Mon Sep 17 00:00:00 2001 From: psii Date: Mon, 8 Mar 2021 10:53:47 +0100 Subject: [PATCH 1/3] edit.py: Converted legacy GUI hooks --- chinese/edit.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/chinese/edit.py b/chinese/edit.py index b9c5e39..c23ab86 100644 --- a/chinese/edit.py +++ b/chinese/edit.py @@ -17,8 +17,7 @@ # You should have received a copy of the GNU General Public License along with # Chinese Support Redux. If not, see . -from anki.hooks import addHook -from aqt import mw +from aqt import mw, gui_hooks from .behavior import update_fields from .main import config @@ -26,9 +25,9 @@ class EditManager: def __init__(self): - addHook('setupEditorButtons', self.setupButton) - addHook('loadNote', self.updateButton) - addHook('editFocusLost', self.onFocusLost) + 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) def setupButton(self, buttons, editor): self.editor = editor @@ -43,7 +42,7 @@ def setupButton(self, buttons, editor): id='chineseSupport', toggleable=True) - return buttons + [button] + buttons.append(button) def onToggle(self, editor): self.buttonOn = not self.buttonOn From 8db468cc2bc7f930c09c5110f88369d806e3b39a Mon Sep 17 00:00:00 2001 From: psii Date: Mon, 8 Mar 2021 11:00:31 +0100 Subject: [PATCH 2/3] main.py: Converted legacy GUI hooks --- chinese/main.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/chinese/main.py b/chinese/main.py index 04ba325..4c51cc6 100644 --- a/chinese/main.py +++ b/chinese/main.py @@ -15,11 +15,10 @@ # You should have received a copy of the GNU General Public License along with # Chinese Support Redux. If not, see . -from anki.hooks import addHook, wrap -from anki.lang import _ +from anki.hooks import wrap from anki.stats import CollectionStats from anki.stdmodels import models -from aqt import mw +from aqt import mw, gui_hooks from .config import ConfigManager from .database import Dictionary @@ -42,12 +41,12 @@ def load(): ruby.install() chinese.install() - addHook('profileLoaded', load_menu) - addHook('profileLoaded', add_models) - addHook('loadNote', append_tone_styling) - addHook('unloadProfile', config.save) - addHook('unloadProfile', dictionary.conn.close) - addHook('unloadProfile', unload_menu) + gui_hooks.profile_did_open.append(load_menu) + gui_hooks.profile_did_open.append(add_models) + gui_hooks.editor_did_load_note.append(append_tone_styling) + gui_hooks.profile_will_close.append(config.save) + gui_hooks.profile_will_close.append(dictionary.conn.close) + gui_hooks.profile_will_close.append(unload_menu) CollectionStats.todayStats = wrap( CollectionStats.todayStats, todayStats, 'around' ) From b13e1ec316a199425ba46decb3fb98fecd44cb25 Mon Sep 17 00:00:00 2001 From: psii Date: Mon, 8 Mar 2021 12:11:57 +0100 Subject: [PATCH 3/3] Bugfix: Allow adding cards and browsing cards Fixes the bug where simultaneous adding new cards and having the card browser open for editing stops the auto-fill feature from working correctly. `edit.EditManager` wrongly assumes that there is only one `Editor` instance. The window for adding cards and the browsing window each instantiate their own editor. --- chinese/edit.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/chinese/edit.py b/chinese/edit.py index c23ab86..588f5c7 100644 --- a/chinese/edit.py +++ b/chinese/edit.py @@ -28,14 +28,15 @@ 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) + self.editors = [] def setupButton(self, buttons, editor): - self.editor = editor + self.editors.append(editor) self.buttonOn = False - editor._links['chineseSupport'] = self.onToggle - button = editor._addButton( + button = editor.addButton( icon=None, + func=self.onToggle, cmd='chineseSupport', tip='Chinese Support', label='汉字', @@ -63,6 +64,10 @@ def updateButton(self, editor): editor.web.eval('toggleEditorButton(chineseSupport);') self.buttonOn = not self.buttonOn + def _refreshAllEditors(self, focusTo): + for editor in self.editors: + editor.loadNote(focusTo=focusTo) + def onFocusLost(self, _, note, index): if not self.buttonOn: return False @@ -71,10 +76,8 @@ def onFocusLost(self, _, note, index): field = allFields[index] if update_fields(note, field, allFields): - if index == len(allFields) - 1: - self.editor.loadNote(focusTo=index) - else: - self.editor.loadNote(focusTo=index+1) + focusTo = (index + 1) % len(allFields) + self._refreshAllEditors(focusTo) return False