From e33a2bcb1788f27ab08a267b9532145078be3f0a Mon Sep 17 00:00:00 2001 From: Abdo Date: Sun, 24 Dec 2023 08:22:59 +0300 Subject: [PATCH] Improve confirmation message in Add screen (#2903) --- ftl/core/adding.ftl | 3 ++- qt/aqt/addcards.py | 21 ++++++++++++++++----- qt/aqt/utils.py | 12 ++++++++++-- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/ftl/core/adding.ftl b/ftl/core/adding.ftl index 4a9756dc5..b95210ee9 100644 --- a/ftl/core/adding.ftl +++ b/ftl/core/adding.ftl @@ -1,6 +1,7 @@ adding-add-shortcut-ctrlandenter = Add (shortcut: ctrl+enter) adding-added = Added -adding-close-and-lose-current-input = Close and lose current input? +adding-discard-current-input = Discard current input? +adding-keep-editing = Keep Editing adding-edit = Edit "{ $val }" adding-history = History adding-note-deleted = (Note deleted) diff --git a/qt/aqt/addcards.py b/qt/aqt/addcards.py index cadb6fb6d..231287a29 100644 --- a/qt/aqt/addcards.py +++ b/qt/aqt/addcards.py @@ -22,6 +22,7 @@ from aqt.sound import av_player from aqt.utils import ( HelpPage, add_close_shortcut, + ask_user_dialog, askUser, downArrow, openHelp, @@ -342,13 +343,23 @@ class AddCards(QMainWindow): self.close() def ifCanClose(self, onOk: Callable) -> None: - def afterSave() -> None: - ok = self.editor.fieldsAreBlank(self._last_added_note) or askUser( - tr.adding_close_and_lose_current_input(), defaultno=True - ) - if ok: + def callback(choice: int) -> None: + if choice == 0: onOk() + def afterSave() -> None: + if self.editor.fieldsAreBlank(self._last_added_note): + return onOk() + + ask_user_dialog( + tr.adding_discard_current_input(), + callback=callback, + buttons=[ + QMessageBox.StandardButton.Discard, + (tr.adding_keep_editing(), QMessageBox.ButtonRole.RejectRole), + ], + ) + self.editor.call_after_note_saved(afterSave) def closeWithCallback(self, cb: Callable[[], None]) -> None: diff --git a/qt/aqt/utils.py b/qt/aqt/utils.py index 09020e0a2..27713a8e7 100644 --- a/qt/aqt/utils.py +++ b/qt/aqt/utils.py @@ -138,7 +138,10 @@ class MessageBox(QMessageBox): icon: QMessageBox.Icon = QMessageBox.Icon.NoIcon, help: HelpPageArgument | None = None, title: str = "Anki", - buttons: Sequence[str | QMessageBox.StandardButton] | None = None, + buttons: Sequence[ + str | QMessageBox.StandardButton | tuple[str, QMessageBox.ButtonRole] + ] + | None = None, default_button: int = 0, textFormat: Qt.TextFormat = Qt.TextFormat.PlainText, modality: Qt.WindowModality = Qt.WindowModality.WindowModal, @@ -161,6 +164,8 @@ class MessageBox(QMessageBox): b = self.addButton(button, QMessageBox.ButtonRole.ActionRole) elif isinstance(button, QMessageBox.StandardButton): b = self.addButton(button) + elif isinstance(button, tuple): + b = self.addButton(button[0], button[1]) else: continue if callback is not None: @@ -193,7 +198,10 @@ def ask_user( def ask_user_dialog( text: str, callback: Callable[[int], None], - buttons: Sequence[str | QMessageBox.StandardButton] | None = None, + buttons: Sequence[ + str | QMessageBox.StandardButton | tuple[str, QMessageBox.ButtonRole] + ] + | None = None, default_button: int = 1, **kwargs: Any, ) -> MessageBox: