From 9c5edfa88c952f5fff2d614bb15222ad3a3bf29c Mon Sep 17 00:00:00 2001 From: Ren Tatsumoto Date: Mon, 22 May 2023 04:07:15 +0000 Subject: [PATCH] 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 --- qt/aqt/main.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/qt/aqt/main.py b/qt/aqt/main.py index 6d48d863d..a9f984997 100644 --- a/qt/aqt/main.py +++ b/qt/aqt/main.py @@ -1112,12 +1112,23 @@ title="{}" {}>{}""".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