This commit is contained in:
Evgeny Kulikov 2025-12-23 11:07:11 +00:00 committed by GitHub
commit 2b14f23293
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
22 changed files with 37 additions and 51 deletions

View file

@ -407,6 +407,22 @@ class AnkiApp(QApplication):
def eventFilter(self, src: Any, evt: QEvent | None) -> bool: def eventFilter(self, src: Any, evt: QEvent | None) -> bool:
assert evt is not None assert evt is not None
# Handle Close shortcut here because modal dialogs disable main-window shortcuts
if (is_mac or is_lin) and evt.type() == QEvent.Type.KeyPress:
key_event = cast(QKeyEvent, evt)
if not key_event.isAutoRepeat():
mods = cast(int, key_event.modifiers().value)
seq = QKeySequence(mods | key_event.key())
if any(
seq == binding
for binding in QKeySequence.keyBindings(
QKeySequence.StandardKey.Close
)
):
if mw is not None:
mw._close_active_window()
return True
pointer_classes = ( pointer_classes = (
QPushButton, QPushButton,
QCheckBox, QCheckBox,

View file

@ -21,7 +21,6 @@ from aqt.qt import *
from aqt.sound import av_player from aqt.sound import av_player
from aqt.utils import ( from aqt.utils import (
HelpPage, HelpPage,
add_close_shortcut,
ask_user_dialog, ask_user_dialog,
askUser, askUser,
downArrow, downArrow,
@ -49,7 +48,6 @@ class AddCards(QMainWindow):
self.setMinimumWidth(400) self.setMinimumWidth(400)
self.setup_choosers() self.setup_choosers()
self.setupEditor() self.setupEditor()
add_close_shortcut(self)
self._load_new_note() self._load_new_note()
self.setupButtons() self.setupButtons()
self.history: list[NoteId] = [] self.history: list[NoteId] = []

View file

@ -40,7 +40,6 @@ from aqt import gui_hooks
from aqt.log import ADDON_LOGGER_PREFIX, find_addon_logger, get_addon_logs_folder from aqt.log import ADDON_LOGGER_PREFIX, find_addon_logger, get_addon_logs_folder
from aqt.qt import * from aqt.qt import *
from aqt.utils import ( from aqt.utils import (
add_close_shortcut,
askUser, askUser,
disable_help_button, disable_help_button,
getFile, getFile,
@ -829,7 +828,6 @@ class AddonsDialog(QDialog):
self.setAcceptDrops(True) self.setAcceptDrops(True)
self.redrawAddons() self.redrawAddons()
restoreGeom(self, "addons") restoreGeom(self, "addons")
add_close_shortcut(self)
gui_hooks.addons_dialog_will_show(self) gui_hooks.addons_dialog_will_show(self)
self._onAddonSelectionChanged() self._onAddonSelectionChanged()
self.show() self.show()

View file

@ -60,7 +60,6 @@ from aqt.undo import UndoActionsInfo
from aqt.utils import ( from aqt.utils import (
HelpPage, HelpPage,
KeyboardModifiersPressed, KeyboardModifiersPressed,
add_close_shortcut,
add_ellipsis_to_action_label, add_ellipsis_to_action_label,
current_window, current_window,
ensure_editor_saved, ensure_editor_saved,
@ -1123,8 +1122,6 @@ class Browser(QMainWindow):
dialog.setWindowTitle(tr.actions_grade_now()) dialog.setWindowTitle(tr.actions_grade_now())
layout = QHBoxLayout() layout = QHBoxLayout()
dialog.setLayout(layout) dialog.setLayout(layout)
add_close_shortcut(dialog)
# Add grade buttons # Add grade buttons
for ease, label in [ for ease, label in [
(1, tr.studying_again()), (1, tr.studying_again()),

View file

@ -14,7 +14,6 @@ from anki.errors import NotFoundError
from anki.lang import without_unicode_isolation from anki.lang import without_unicode_isolation
from aqt.qt import * from aqt.qt import *
from aqt.utils import ( from aqt.utils import (
add_close_shortcut,
disable_help_button, disable_help_button,
qconnect, qconnect,
restoreGeom, restoreGeom,
@ -54,7 +53,6 @@ class CardInfoDialog(QDialog):
self.setMinimumSize(400, 300) self.setMinimumSize(400, 300)
disable_help_button(self) disable_help_button(self)
restoreGeom(self, self.GEOMETRY_KEY, default_size=(800, 800)) restoreGeom(self, self.GEOMETRY_KEY, default_size=(800, 800))
add_close_shortcut(self)
setWindowIcon(self) setWindowIcon(self)
self.web: AnkiWebView | None = AnkiWebView( self.web: AnkiWebView | None = AnkiWebView(

View file

@ -15,7 +15,6 @@ from anki.notes import NoteId
from aqt.operations.notetype import change_notetype_of_notes from aqt.operations.notetype import change_notetype_of_notes
from aqt.qt import * from aqt.qt import *
from aqt.utils import ( from aqt.utils import (
add_close_shortcut,
disable_help_button, disable_help_button,
restoreGeom, restoreGeom,
saveGeom, saveGeom,
@ -49,7 +48,6 @@ class ChangeNotetypeDialog(QDialog):
self.setMinimumSize(400, 300) self.setMinimumSize(400, 300)
disable_help_button(self) disable_help_button(self)
restoreGeom(self, self.TITLE, default_size=(800, 800)) restoreGeom(self, self.TITLE, default_size=(800, 800))
add_close_shortcut(self)
self.web = AnkiWebView(kind=AnkiWebViewKind.CHANGE_NOTETYPE) self.web = AnkiWebView(kind=AnkiWebViewKind.CHANGE_NOTETYPE)
self.web.setVisible(False) self.web.setVisible(False)

View file

@ -27,7 +27,6 @@ from aqt.sound import av_player, play_clicked_audio
from aqt.theme import theme_manager from aqt.theme import theme_manager
from aqt.utils import ( from aqt.utils import (
HelpPage, HelpPage,
add_close_shortcut,
ask_user_dialog, ask_user_dialog,
askUser, askUser,
disable_help_button, disable_help_button,
@ -91,7 +90,6 @@ class CardLayout(QDialog):
gui_hooks.card_layout_will_show(self) gui_hooks.card_layout_will_show(self)
self.redraw_everything() self.redraw_everything()
restoreGeom(self, "CardLayout") restoreGeom(self, "CardLayout")
add_close_shortcut(self)
restoreSplitter(self.mainArea, "CardLayoutMainArea") restoreSplitter(self.mainArea, "CardLayoutMainArea")
self.setWindowModality(Qt.WindowModality.ApplicationModal) self.setWindowModality(Qt.WindowModality.ApplicationModal)
self.show() self.show()

View file

@ -15,7 +15,7 @@ from aqt.operations import QueryOp
from aqt.operations.scheduling import custom_study from aqt.operations.scheduling import custom_study
from aqt.qt import * from aqt.qt import *
from aqt.taglimit import TagLimit from aqt.taglimit import TagLimit
from aqt.utils import add_close_shortcut, disable_help_button, tr from aqt.utils import disable_help_button, tr
RADIO_NEW = 1 RADIO_NEW = 1
RADIO_REV = 2 RADIO_REV = 2
@ -63,7 +63,6 @@ class CustomStudy(QDialog):
self.form.setupUi(self) self.form.setupUi(self)
disable_help_button(self) disable_help_button(self)
self.setupSignals() self.setupSignals()
add_close_shortcut(self)
self.form.radioNew.click() self.form.radioNew.click()
self.open() self.open()

View file

@ -10,7 +10,7 @@ from anki.decks import DeckDict
from aqt.operations import QueryOp from aqt.operations import QueryOp
from aqt.operations.deck import update_deck_dict from aqt.operations.deck import update_deck_dict
from aqt.qt import * from aqt.qt import *
from aqt.utils import add_close_shortcut, disable_help_button, restoreGeom, saveGeom, tr from aqt.utils import disable_help_button, restoreGeom, saveGeom, tr
class DeckDescriptionDialog(QDialog): class DeckDescriptionDialog(QDialog):
@ -45,7 +45,6 @@ class DeckDescriptionDialog(QDialog):
self.setMinimumWidth(400) self.setMinimumWidth(400)
disable_help_button(self) disable_help_button(self)
restoreGeom(self, self.TITLE) restoreGeom(self, self.TITLE)
add_close_shortcut(self)
box = QVBoxLayout() box = QVBoxLayout()

View file

@ -13,7 +13,6 @@ from aqt import gui_hooks
from aqt.qt import * from aqt.qt import *
from aqt.utils import ( from aqt.utils import (
KeyboardModifiersPressed, KeyboardModifiersPressed,
add_close_shortcut,
disable_help_button, disable_help_button,
restoreGeom, restoreGeom,
saveGeom, saveGeom,
@ -42,7 +41,6 @@ class DeckOptionsDialog(QDialog):
self.setMinimumWidth(400) self.setMinimumWidth(400)
disable_help_button(self) disable_help_button(self)
restoreGeom(self, self.TITLE, default_size=(800, 800)) restoreGeom(self, self.TITLE, default_size=(800, 800))
add_close_shortcut(self)
self.web = AnkiWebView(kind=AnkiWebViewKind.DECK_OPTIONS) self.web = AnkiWebView(kind=AnkiWebViewKind.DECK_OPTIONS)
self.web.load_sveltekit_page(f"deck-options/{self._deck['id']}") self.web.load_sveltekit_page(f"deck-options/{self._deck['id']}")

View file

@ -9,7 +9,7 @@ from anki.collection import OpChanges
from anki.errors import NotFoundError from anki.errors import NotFoundError
from aqt import gui_hooks from aqt import gui_hooks
from aqt.qt import * from aqt.qt import *
from aqt.utils import add_close_shortcut, restoreGeom, saveGeom, tr from aqt.utils import restoreGeom, saveGeom, tr
class EditCurrent(QMainWindow): class EditCurrent(QMainWindow):
@ -36,7 +36,6 @@ class EditCurrent(QMainWindow):
close_button = self.form.buttonBox.button(QDialogButtonBox.StandardButton.Close) close_button = self.form.buttonBox.button(QDialogButtonBox.StandardButton.Close)
assert close_button is not None assert close_button is not None
close_button.setShortcut(QKeySequence("Ctrl+Return")) close_button.setShortcut(QKeySequence("Ctrl+Return"))
add_close_shortcut(self)
# qt5.14+ doesn't handle numpad enter on Windows # qt5.14+ doesn't handle numpad enter on Windows
self.compat_add_shorcut = QShortcut(QKeySequence("Ctrl+Enter"), self) self.compat_add_shorcut = QShortcut(QKeySequence("Ctrl+Enter"), self)
qconnect(self.compat_add_shorcut.activated, close_button.click) qconnect(self.compat_add_shorcut.activated, close_button.click)

View file

@ -19,7 +19,6 @@ from aqt import gui_hooks
from aqt.errors import show_exception from aqt.errors import show_exception
from aqt.qt import * from aqt.qt import *
from aqt.utils import ( from aqt.utils import (
add_close_shortcut,
checkInvalidFilename, checkInvalidFilename,
disable_help_button, disable_help_button,
getSaveFile, getSaveFile,
@ -47,7 +46,6 @@ class ExportDialog(QDialog):
self.cids = cids self.cids = cids
disable_help_button(self) disable_help_button(self)
self.setup(did) self.setup(did)
add_close_shortcut(self)
self.exec() self.exec()
def setup(self, did: DeckId | None) -> None: def setup(self, did: DeckId | None) -> None:

View file

@ -15,7 +15,6 @@ from aqt.qt import *
from aqt.schema_change_tracker import ChangeTracker from aqt.schema_change_tracker import ChangeTracker
from aqt.utils import ( from aqt.utils import (
HelpPage, HelpPage,
add_close_shortcut,
askUser, askUser,
disable_help_button, disable_help_button,
getOnlyText, getOnlyText,
@ -51,7 +50,6 @@ class FieldDialog(QDialog):
without_unicode_isolation(tr.fields_fields_for(val=self.model["name"])) without_unicode_isolation(tr.fields_fields_for(val=self.model["name"]))
) )
add_close_shortcut(self)
disable_help_button(self) disable_help_button(self)
help_button = self.form.buttonBox.button(QDialogButtonBox.StandardButton.Help) help_button = self.form.buttonBox.button(QDialogButtonBox.StandardButton.Help)
assert help_button is not None assert help_button is not None

View file

@ -27,7 +27,6 @@ from aqt.operations import QueryOp
from aqt.progress import ProgressUpdate from aqt.progress import ProgressUpdate
from aqt.qt import * from aqt.qt import *
from aqt.utils import ( from aqt.utils import (
add_close_shortcut,
checkInvalidFilename, checkInvalidFilename,
disable_help_button, disable_help_button,
getSaveFile, getSaveFile,
@ -54,7 +53,6 @@ class ExportDialog(QDialog):
self.nids = nids self.nids = nids
disable_help_button(self) disable_help_button(self)
self.setup(did) self.setup(did)
add_close_shortcut(self)
self.open() self.open()
def setup(self, did: DeckId | None) -> None: def setup(self, did: DeckId | None) -> None:

View file

@ -12,7 +12,7 @@ import aqt.deckconf
import aqt.main import aqt.main
import aqt.operations import aqt.operations
from aqt.qt import * from aqt.qt import *
from aqt.utils import add_close_shortcut, disable_help_button, restoreGeom, saveGeom, tr from aqt.utils import disable_help_button, restoreGeom, saveGeom, tr
from aqt.webview import AnkiWebView, AnkiWebViewKind from aqt.webview import AnkiWebView, AnkiWebViewKind
@ -62,7 +62,6 @@ class ImportDialog(QDialog):
self.setMinimumSize(*self.MIN_SIZE) self.setMinimumSize(*self.MIN_SIZE)
disable_help_button(self) disable_help_button(self)
restoreGeom(self, self.args.title, default_size=self.DEFAULT_SIZE) restoreGeom(self, self.args.title, default_size=self.DEFAULT_SIZE)
add_close_shortcut(self)
self.web: AnkiWebView | None = AnkiWebView(kind=self.args.kind) self.web: AnkiWebView | None = AnkiWebView(kind=self.args.kind)
self.web.setVisible(False) self.web.setVisible(False)

View file

@ -1184,6 +1184,22 @@ title="{}" {}>{}</button>""".format(
self.applyShortcuts(globalShortcuts) self.applyShortcuts(globalShortcuts)
self.stateShortcuts: list[QShortcut] = [] self.stateShortcuts: list[QShortcut] = []
def _close_active_window(self) -> None:
window = (
QApplication.activeModalWidget()
or current_window()
or self.app.activeWindow()
)
if not window or window is self:
return
if window is getattr(self, "profileDiag", None):
# Do not allow closing of ProfileManager
return
if isinstance(window, QDialog):
window.reject()
else:
window.close()
def _normalize_shortcuts( def _normalize_shortcuts(
self, shortcuts: Sequence[tuple[str, Callable]] self, shortcuts: Sequence[tuple[str, Callable]]
) -> Sequence[tuple[QKeySequence, Callable]]: ) -> Sequence[tuple[QKeySequence, Callable]]:

View file

@ -24,7 +24,7 @@ from anki.scheduler.v3 import CardAnswer
from anki.scheduler.v3 import Scheduler as V3Scheduler from anki.scheduler.v3 import Scheduler as V3Scheduler
from aqt.operations import CollectionOp from aqt.operations import CollectionOp
from aqt.qt import * from aqt.qt import *
from aqt.utils import add_close_shortcut, disable_help_button, getText, tooltip, tr from aqt.utils import disable_help_button, getText, tooltip, tr
def set_due_date_dialog( def set_due_date_dialog(
@ -104,7 +104,6 @@ def forget_cards(
dialog = QDialog(parent) dialog = QDialog(parent)
disable_help_button(dialog) disable_help_button(dialog)
add_close_shortcut(dialog)
form = aqt.forms.forget.Ui_Dialog() form = aqt.forms.forget.Ui_Dialog()
form.setupUi(dialog) form.setupUi(dialog)
@ -154,7 +153,6 @@ def reposition_new_cards_dialog(
dialog = QDialog(parent) dialog = QDialog(parent)
disable_help_button(dialog) disable_help_button(dialog)
add_close_shortcut(dialog)
dialog.setWindowModality(Qt.WindowModality.WindowModal) dialog.setWindowModality(Qt.WindowModality.WindowModal)
form = aqt.forms.reposition.Ui_Dialog() form = aqt.forms.reposition.Ui_Dialog()
form.setupUi(dialog) form.setupUi(dialog)

View file

@ -23,7 +23,6 @@ from aqt.theme import Theme
from aqt.url_schemes import show_url_schemes_dialog from aqt.url_schemes import show_url_schemes_dialog
from aqt.utils import ( from aqt.utils import (
HelpPage, HelpPage,
add_close_shortcut,
add_ellipsis_to_action_label, add_ellipsis_to_action_label,
askUser, askUser,
disable_help_button, disable_help_button,
@ -67,7 +66,6 @@ class Preferences(QDialog):
self.setup_profile() self.setup_profile()
self.setup_global() self.setup_global()
self.setup_configurable_answer_keys() self.setup_configurable_answer_keys()
add_close_shortcut(self)
self.show() self.show()
def setup_configurable_answer_keys(self): def setup_configurable_answer_keys(self):

View file

@ -16,7 +16,6 @@ from aqt.operations.deck import set_current_deck
from aqt.qt import * from aqt.qt import *
from aqt.theme import theme_manager from aqt.theme import theme_manager
from aqt.utils import ( from aqt.utils import (
add_close_shortcut,
disable_help_button, disable_help_button,
getSaveFile, getSaveFile,
maybeHideClose, maybeHideClose,
@ -69,7 +68,6 @@ class NewDeckStats(QDialog):
assert b is not None assert b is not None
b.setAutoDefault(False) b.setAutoDefault(False)
maybeHideClose(self.form.buttonBox) maybeHideClose(self.form.buttonBox)
add_close_shortcut(self)
gui_hooks.stats_dialog_will_show(self) gui_hooks.stats_dialog_will_show(self)
self.form.web.hide_while_preserving_layout() self.form.web.hide_while_preserving_layout()
self.show() self.show()
@ -182,7 +180,6 @@ class DeckStats(QDialog):
qconnect(f.year.clicked, lambda: self.changePeriod(1)) qconnect(f.year.clicked, lambda: self.changePeriod(1))
qconnect(f.life.clicked, lambda: self.changePeriod(2)) qconnect(f.life.clicked, lambda: self.changePeriod(2))
maybeHideClose(self.form.buttonBox) maybeHideClose(self.form.buttonBox)
add_close_shortcut(self)
gui_hooks.stats_dialog_old_will_show(self) gui_hooks.stats_dialog_old_will_show(self)
self.show() self.show()
self.refresh() self.refresh()

View file

@ -16,7 +16,6 @@ from aqt.qt import *
from aqt.utils import ( from aqt.utils import (
HelpPage, HelpPage,
HelpPageArgument, HelpPageArgument,
add_close_shortcut,
disable_help_button, disable_help_button,
openHelp, openHelp,
restoreGeom, restoreGeom,
@ -53,7 +52,6 @@ class StudyDeck(QDialog):
gui_hooks.state_did_reset.append(self.onReset) gui_hooks.state_did_reset.append(self.onReset)
self.geomKey = f"studyDeck-{geomKey}" self.geomKey = f"studyDeck-{geomKey}"
restoreGeom(self, self.geomKey) restoreGeom(self, self.geomKey)
add_close_shortcut(self)
disable_help_button(self) disable_help_button(self)
if not cancel: if not cancel:
self.form.buttonBox.removeButton( self.form.buttonBox.removeButton(

View file

@ -28,7 +28,6 @@ from aqt.qt import (
qconnect, qconnect,
) )
from aqt.utils import ( from aqt.utils import (
add_close_shortcut,
ask_user_dialog, ask_user_dialog,
disable_help_button, disable_help_button,
show_warning, show_warning,
@ -391,7 +390,6 @@ def get_id_and_pass_from_user(
qconnect(bb.accepted, diag.accept) qconnect(bb.accepted, diag.accept)
qconnect(bb.rejected, diag.reject) qconnect(bb.rejected, diag.reject)
vbox.addWidget(bb) vbox.addWidget(bb)
add_close_shortcut(diag)
diag.setLayout(vbox) diag.setLayout(vbox)
diag.adjustSize() diag.adjustSize()
diag.show() diag.show()

View file

@ -45,7 +45,6 @@ from aqt.qt import (
QFrame, QFrame,
QHeaderView, QHeaderView,
QIcon, QIcon,
QKeySequence,
QLabel, QLabel,
QLineEdit, QLineEdit,
QListWidget, QListWidget,
@ -61,7 +60,6 @@ from aqt.qt import (
QPlainTextEdit, QPlainTextEdit,
QPoint, QPoint,
QPushButton, QPushButton,
QShortcut,
QSize, QSize,
QSplitter, QSplitter,
QStandardPaths, QStandardPaths,
@ -567,7 +565,6 @@ def getText(
d = GetTextDialog( d = GetTextDialog(
parent, prompt, help=help, edit=edit, default=default, title=title, **kwargs parent, prompt, help=help, edit=edit, default=default, title=title, **kwargs
) )
add_close_shortcut(d)
d.setWindowModality(Qt.WindowModality.WindowModal) d.setWindowModality(Qt.WindowModality.WindowModal)
if geomKey: if geomKey:
restoreGeom(d, geomKey) restoreGeom(d, geomKey)
@ -1021,13 +1018,6 @@ def maybeHideClose(bbox: QDialogButtonBox) -> None:
bbox.removeButton(b) bbox.removeButton(b)
def add_close_shortcut(widg: QWidget) -> None:
if not is_mac:
return
shortcut = QShortcut(QKeySequence("Ctrl+W"), widg)
qconnect(shortcut.activated, widg.close)
def downArrow() -> str: def downArrow() -> str:
if is_win: if is_win:
return "" return ""