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:
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 = (
QPushButton,
QCheckBox,

View file

@ -21,7 +21,6 @@ from aqt.qt import *
from aqt.sound import av_player
from aqt.utils import (
HelpPage,
add_close_shortcut,
ask_user_dialog,
askUser,
downArrow,
@ -49,7 +48,6 @@ class AddCards(QMainWindow):
self.setMinimumWidth(400)
self.setup_choosers()
self.setupEditor()
add_close_shortcut(self)
self._load_new_note()
self.setupButtons()
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.qt import *
from aqt.utils import (
add_close_shortcut,
askUser,
disable_help_button,
getFile,
@ -829,7 +828,6 @@ class AddonsDialog(QDialog):
self.setAcceptDrops(True)
self.redrawAddons()
restoreGeom(self, "addons")
add_close_shortcut(self)
gui_hooks.addons_dialog_will_show(self)
self._onAddonSelectionChanged()
self.show()

View file

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

View file

@ -14,7 +14,6 @@ from anki.errors import NotFoundError
from anki.lang import without_unicode_isolation
from aqt.qt import *
from aqt.utils import (
add_close_shortcut,
disable_help_button,
qconnect,
restoreGeom,
@ -54,7 +53,6 @@ class CardInfoDialog(QDialog):
self.setMinimumSize(400, 300)
disable_help_button(self)
restoreGeom(self, self.GEOMETRY_KEY, default_size=(800, 800))
add_close_shortcut(self)
setWindowIcon(self)
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.qt import *
from aqt.utils import (
add_close_shortcut,
disable_help_button,
restoreGeom,
saveGeom,
@ -49,7 +48,6 @@ class ChangeNotetypeDialog(QDialog):
self.setMinimumSize(400, 300)
disable_help_button(self)
restoreGeom(self, self.TITLE, default_size=(800, 800))
add_close_shortcut(self)
self.web = AnkiWebView(kind=AnkiWebViewKind.CHANGE_NOTETYPE)
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.utils import (
HelpPage,
add_close_shortcut,
ask_user_dialog,
askUser,
disable_help_button,
@ -91,7 +90,6 @@ class CardLayout(QDialog):
gui_hooks.card_layout_will_show(self)
self.redraw_everything()
restoreGeom(self, "CardLayout")
add_close_shortcut(self)
restoreSplitter(self.mainArea, "CardLayoutMainArea")
self.setWindowModality(Qt.WindowModality.ApplicationModal)
self.show()

View file

@ -15,7 +15,7 @@ from aqt.operations import QueryOp
from aqt.operations.scheduling import custom_study
from aqt.qt import *
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_REV = 2
@ -63,7 +63,6 @@ class CustomStudy(QDialog):
self.form.setupUi(self)
disable_help_button(self)
self.setupSignals()
add_close_shortcut(self)
self.form.radioNew.click()
self.open()

View file

@ -10,7 +10,7 @@ from anki.decks import DeckDict
from aqt.operations import QueryOp
from aqt.operations.deck import update_deck_dict
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):
@ -45,7 +45,6 @@ class DeckDescriptionDialog(QDialog):
self.setMinimumWidth(400)
disable_help_button(self)
restoreGeom(self, self.TITLE)
add_close_shortcut(self)
box = QVBoxLayout()

View file

@ -13,7 +13,6 @@ from aqt import gui_hooks
from aqt.qt import *
from aqt.utils import (
KeyboardModifiersPressed,
add_close_shortcut,
disable_help_button,
restoreGeom,
saveGeom,
@ -42,7 +41,6 @@ class DeckOptionsDialog(QDialog):
self.setMinimumWidth(400)
disable_help_button(self)
restoreGeom(self, self.TITLE, default_size=(800, 800))
add_close_shortcut(self)
self.web = AnkiWebView(kind=AnkiWebViewKind.DECK_OPTIONS)
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 aqt import gui_hooks
from aqt.qt import *
from aqt.utils import add_close_shortcut, restoreGeom, saveGeom, tr
from aqt.utils import restoreGeom, saveGeom, tr
class EditCurrent(QMainWindow):
@ -36,7 +36,6 @@ class EditCurrent(QMainWindow):
close_button = self.form.buttonBox.button(QDialogButtonBox.StandardButton.Close)
assert close_button is not None
close_button.setShortcut(QKeySequence("Ctrl+Return"))
add_close_shortcut(self)
# qt5.14+ doesn't handle numpad enter on Windows
self.compat_add_shorcut = QShortcut(QKeySequence("Ctrl+Enter"), self)
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.qt import *
from aqt.utils import (
add_close_shortcut,
checkInvalidFilename,
disable_help_button,
getSaveFile,
@ -47,7 +46,6 @@ class ExportDialog(QDialog):
self.cids = cids
disable_help_button(self)
self.setup(did)
add_close_shortcut(self)
self.exec()
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.utils import (
HelpPage,
add_close_shortcut,
askUser,
disable_help_button,
getOnlyText,
@ -51,7 +50,6 @@ class FieldDialog(QDialog):
without_unicode_isolation(tr.fields_fields_for(val=self.model["name"]))
)
add_close_shortcut(self)
disable_help_button(self)
help_button = self.form.buttonBox.button(QDialogButtonBox.StandardButton.Help)
assert help_button is not None

View file

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

View file

@ -12,7 +12,7 @@ import aqt.deckconf
import aqt.main
import aqt.operations
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
@ -62,7 +62,6 @@ class ImportDialog(QDialog):
self.setMinimumSize(*self.MIN_SIZE)
disable_help_button(self)
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.setVisible(False)

View file

@ -1184,6 +1184,22 @@ title="{}" {}>{}</button>""".format(
self.applyShortcuts(globalShortcuts)
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(
self, shortcuts: Sequence[tuple[str, 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 aqt.operations import CollectionOp
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(
@ -104,7 +104,6 @@ def forget_cards(
dialog = QDialog(parent)
disable_help_button(dialog)
add_close_shortcut(dialog)
form = aqt.forms.forget.Ui_Dialog()
form.setupUi(dialog)
@ -154,7 +153,6 @@ def reposition_new_cards_dialog(
dialog = QDialog(parent)
disable_help_button(dialog)
add_close_shortcut(dialog)
dialog.setWindowModality(Qt.WindowModality.WindowModal)
form = aqt.forms.reposition.Ui_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.utils import (
HelpPage,
add_close_shortcut,
add_ellipsis_to_action_label,
askUser,
disable_help_button,
@ -67,7 +66,6 @@ class Preferences(QDialog):
self.setup_profile()
self.setup_global()
self.setup_configurable_answer_keys()
add_close_shortcut(self)
self.show()
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.theme import theme_manager
from aqt.utils import (
add_close_shortcut,
disable_help_button,
getSaveFile,
maybeHideClose,
@ -69,7 +68,6 @@ class NewDeckStats(QDialog):
assert b is not None
b.setAutoDefault(False)
maybeHideClose(self.form.buttonBox)
add_close_shortcut(self)
gui_hooks.stats_dialog_will_show(self)
self.form.web.hide_while_preserving_layout()
self.show()
@ -182,7 +180,6 @@ class DeckStats(QDialog):
qconnect(f.year.clicked, lambda: self.changePeriod(1))
qconnect(f.life.clicked, lambda: self.changePeriod(2))
maybeHideClose(self.form.buttonBox)
add_close_shortcut(self)
gui_hooks.stats_dialog_old_will_show(self)
self.show()
self.refresh()

View file

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

View file

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

View file

@ -45,7 +45,6 @@ from aqt.qt import (
QFrame,
QHeaderView,
QIcon,
QKeySequence,
QLabel,
QLineEdit,
QListWidget,
@ -61,7 +60,6 @@ from aqt.qt import (
QPlainTextEdit,
QPoint,
QPushButton,
QShortcut,
QSize,
QSplitter,
QStandardPaths,
@ -567,7 +565,6 @@ def getText(
d = GetTextDialog(
parent, prompt, help=help, edit=edit, default=default, title=title, **kwargs
)
add_close_shortcut(d)
d.setWindowModality(Qt.WindowModality.WindowModal)
if geomKey:
restoreGeom(d, geomKey)
@ -1021,13 +1018,6 @@ def maybeHideClose(bbox: QDialogButtonBox) -> None:
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:
if is_win:
return ""