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:
Ren Tatsumoto 2023-05-22 04:07:15 +00:00 committed by GitHub
parent d2176ff712
commit 9c5edfa88c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1112,12 +1112,23 @@ title="{}" {}>{}</button>""".format(
self.applyShortcuts(globalShortcuts)
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(
self, shortcuts: Sequence[tuple[str, Callable]]
) -> list[QShortcut]:
qshortcuts = []
for key, fn in shortcuts:
scut = QShortcut(QKeySequence(key), self, activated=fn) # type: ignore
for key, fn in self._normalize_shortcuts(shortcuts):
scut = QShortcut(key, self, activated=fn) # type: ignore
scut.setAutoRepeat(False)
qshortcuts.append(scut)
return qshortcuts