From 407e2dc43b719f499d1d28f734e26620ce6a5fde Mon Sep 17 00:00:00 2001 From: "Hikaru Y." Date: Sun, 6 Oct 2024 13:21:00 +0900 Subject: [PATCH] Fix issues with 'Discard changes' confirmation dialog (#3478) * Prevent memory leak * Fix deck option changes not detected until focus is lost * Accurately determine if there are any pending changes This makes it so that the confirmation dialog appears when it should, and not when it shouldn't. --- qt/aqt/deckoptions.py | 42 ++++++------- ts/routes/deck-options/SaveButton.svelte | 9 +-- ts/routes/deck-options/[deckId]/+page.svelte | 13 +++- ts/routes/deck-options/lib.ts | 65 +++++++++++++++++++- 4 files changed, 94 insertions(+), 35 deletions(-) diff --git a/qt/aqt/deckoptions.py b/qt/aqt/deckoptions.py index 0a1e778d18a..e4802f98ad4 100644 --- a/qt/aqt/deckoptions.py +++ b/qt/aqt/deckoptions.py @@ -62,41 +62,38 @@ def _on_bridge_cmd(self, cmd: str) -> None: if cmd == "deckOptionsReady": self._ready = True gui_hooks.deck_options_did_load(self) + elif cmd == "confirmDiscardChanges": + self.confirm_discard_changes() + elif cmd == "_close": + self._close() def closeEvent(self, evt: QCloseEvent) -> None: if self._close_event_has_cleaned_up: evt.accept() return evt.ignore() - self.if_can_close() + self.check_pending_changes() def _close(self): """Close. Ensure the closeEvent is not ignored.""" self._close_event_has_cleaned_up = True self.close() - def if_can_close(self): - """Close if there was no modification. Otherwise ask for confirmation first.""" - - def callbackWithUserChoice(choice: int): + def confirm_discard_changes(self) -> None: + def callbackWithUserChoice(choice: int) -> None: if choice == 0: # The user accepted to discard current input. self._close() - def if_can_close_callback_with_data_information(has_modified_dataData: bool): - if has_modified_dataData: - ask_user_dialog( - tr.card_templates_discard_changes(), - callback=callbackWithUserChoice, - buttons=[ - QMessageBox.StandardButton.Discard, - (tr.adding_keep_editing(), QMessageBox.ButtonRole.RejectRole), - ], - ) - else: - self._close() - - self.has_modified_data(if_can_close_callback_with_data_information) + ask_user_dialog( + tr.card_templates_discard_changes(), + callback=callbackWithUserChoice, + buttons=[ + QMessageBox.StandardButton.Discard, + (tr.adding_keep_editing(), QMessageBox.ButtonRole.RejectRole), + ], + parent=self, + ) def reject(self) -> None: self.mw.col.set_wants_abort() @@ -105,12 +102,11 @@ def reject(self) -> None: saveGeom(self, self.TITLE) QDialog.reject(self) - def has_modified_data(self, callback: Callable[[bool], None]): - """Calls `callback` with the information of whether any deck options are modified.""" + def check_pending_changes(self): if self._ready: - self.web.evalWithCallback("anki.deckOptionsPendingChanges()", callback) + self.web.eval("anki.deckOptionsPendingChanges();") else: - callback(False) + self._close() def confirm_deck_then_display_options(active_card: Card | None = None) -> None: diff --git a/ts/routes/deck-options/SaveButton.svelte b/ts/routes/deck-options/SaveButton.svelte index 247f8c66b0b..166ed003fdf 100644 --- a/ts/routes/deck-options/SaveButton.svelte +++ b/ts/routes/deck-options/SaveButton.svelte @@ -21,6 +21,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html import WithFloating from "$lib/components/WithFloating.svelte"; import type { DeckOptionsState } from "./lib"; + import { commitEditing } from "./lib"; const rtl: boolean = window.getComputedStyle(document.body).direction == "rtl"; @@ -28,14 +29,6 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html export let state: DeckOptionsState; - /** Ensure blur handler has fired so changes get committed. */ - async function commitEditing(): Promise { - if (document.activeElement instanceof HTMLElement) { - document.activeElement.blur(); - } - await tick(); - } - async function removeConfig(): Promise { // show pop-up after dropdown has gone away await tick(); diff --git a/ts/routes/deck-options/[deckId]/+page.svelte b/ts/routes/deck-options/[deckId]/+page.svelte index e3a5f6432a7..ec99facc152 100644 --- a/ts/routes/deck-options/[deckId]/+page.svelte +++ b/ts/routes/deck-options/[deckId]/+page.svelte @@ -5,6 +5,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html