mirror of
https://github.com/ankitects/anki.git
synced 2025-09-18 22:12:21 -04:00
Ensure there's no duplicate shortcuts after running state_shortcuts_will_change (#2509)
* remove duplicate shortucts after running hook * normalize shortcuts by converting them to QKeySequence * no need to copy here * extract method
This commit is contained in:
parent
d2176ff712
commit
9c5edfa88c
1 changed files with 13 additions and 2 deletions
|
@ -1112,12 +1112,23 @@ title="{}" {}>{}</button>""".format(
|
||||||
self.applyShortcuts(globalShortcuts)
|
self.applyShortcuts(globalShortcuts)
|
||||||
self.stateShortcuts: list[QShortcut] = []
|
self.stateShortcuts: list[QShortcut] = []
|
||||||
|
|
||||||
|
def _normalize_shortcuts(
|
||||||
|
self, shortcuts: Sequence[tuple[str, Callable]]
|
||||||
|
) -> Sequence[tuple[QKeySequence, Callable]]:
|
||||||
|
"""
|
||||||
|
Remove duplicate shortcuts (possibly added by add-ons)
|
||||||
|
by normalizing them and filtering through a dictionary.
|
||||||
|
The last duplicate shortcut wins, so add-ons will override
|
||||||
|
standard shortcuts if they append to the shortcut list.
|
||||||
|
"""
|
||||||
|
return tuple({QKeySequence(key): fn for key, fn in shortcuts}.items())
|
||||||
|
|
||||||
def applyShortcuts(
|
def applyShortcuts(
|
||||||
self, shortcuts: Sequence[tuple[str, Callable]]
|
self, shortcuts: Sequence[tuple[str, Callable]]
|
||||||
) -> list[QShortcut]:
|
) -> list[QShortcut]:
|
||||||
qshortcuts = []
|
qshortcuts = []
|
||||||
for key, fn in shortcuts:
|
for key, fn in self._normalize_shortcuts(shortcuts):
|
||||||
scut = QShortcut(QKeySequence(key), self, activated=fn) # type: ignore
|
scut = QShortcut(key, self, activated=fn) # type: ignore
|
||||||
scut.setAutoRepeat(False)
|
scut.setAutoRepeat(False)
|
||||||
qshortcuts.append(scut)
|
qshortcuts.append(scut)
|
||||||
return qshortcuts
|
return qshortcuts
|
||||||
|
|
Loading…
Reference in a new issue