Skip to content

Commit

Permalink
Improve confirmation message in Add screen (#2903)
Browse files Browse the repository at this point in the history
  • Loading branch information
abdnh authored Dec 24, 2023
1 parent 3378e47 commit e33a2bc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
3 changes: 2 additions & 1 deletion ftl/core/adding.ftl
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
19 changes: 15 additions & 4 deletions qt/aqt/addcards.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from aqt.utils import (
HelpPage,
add_close_shortcut,
ask_user_dialog,
askUser,
downArrow,
openHelp,
Expand Down Expand Up @@ -342,12 +343,22 @@ def _close(self) -> None:
self.close()

def ifCanClose(self, onOk: Callable) -> None:
def callback(choice: int) -> None:
if choice == 0:
onOk()

def afterSave() -> None:
ok = self.editor.fieldsAreBlank(self._last_added_note) or askUser(
tr.adding_close_and_lose_current_input(), defaultno=True
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),
],
)
if ok:
onOk()

self.editor.call_after_note_saved(afterSave)

Expand Down
12 changes: 10 additions & 2 deletions qt/aqt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,10 @@ def __init__(
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,
Expand All @@ -161,6 +164,8 @@ def __init__(
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:
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit e33a2bc

Please sign in to comment.