Merge pull request #534 from Arthur-Milchior/tags_hook

hook tag_editor_received_a_key
This commit is contained in:
Damien Elmes 2020-04-01 17:10:31 +10:00 committed by GitHub
commit 28380d4183
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 1 deletions

View file

@ -13,7 +13,8 @@ import anki
import aqt
from anki.cards import Card
from anki.hooks import runFilter, runHook
from aqt.qt import QDialog, QMenu
from aqt.qt import QDialog, QEvent, QMenu
from aqt.tagedit import TagEdit
# New hook/filter handling
##############################################################################
@ -1927,6 +1928,30 @@ class _StyleDidInitFilter:
style_did_init = _StyleDidInitFilter()
class _TagEditorReceivedAKeyHook:
_hooks: List[Callable[[TagEdit, QEvent], None]] = []
def append(self, cb: Callable[[TagEdit, QEvent], None]) -> None:
"""(tag_edit: TagEdit, evt: QEvent)"""
self._hooks.append(cb)
def remove(self, cb: Callable[[TagEdit, QEvent], None]) -> None:
if cb in self._hooks:
self._hooks.remove(cb)
def __call__(self, tag_edit: TagEdit, evt: QEvent) -> None:
for hook in self._hooks:
try:
hook(tag_edit, evt)
except:
# if the hook fails, remove it
self._hooks.remove(hook)
raise
tag_editor_received_a_key = _TagEditorReceivedAKeyHook()
class _TopToolbarDidInitLinksHook:
"""Used to modify or add links in the top toolbar of Anki's main window

View file

@ -3,6 +3,7 @@
import re
from aqt import gui_hooks
from aqt.qt import *
@ -76,6 +77,7 @@ class TagEdit(QLineEdit):
Qt.Key_Delete,
):
self.showCompleter()
gui_hooks.tag_editor_received_a_key(self, evt)
def showCompleter(self):
self.completer.setCompletionPrefix(self.text())

View file

@ -509,6 +509,9 @@ def emptyNewCard():
doc="""Allows changing the javascript commands to load note before
executing it and do change in the QT editor.""",
),
# Tag
###################
Hook(name="tag_editor_received_a_key", args=["tag_edit: TagEdit", "evt: QEvent"]),
# Sound/video
###################
Hook(name="av_player_will_play", args=["tag: anki.sound.AVTag"]),