Avoid extra initial loadNote()

This commit is contained in:
Abdo 2025-10-19 08:33:26 +03:00
parent 0ed12f5c63
commit 783a6d1fa2
3 changed files with 17 additions and 13 deletions

View file

@ -23,12 +23,7 @@ from aqt.utils import (
class NewAddCards(QMainWindow):
def __init__(
self,
mw: AnkiQt,
deck_id: DeckId | None = None,
notetype_id: NotetypeId | None = None,
) -> None:
def __init__(self, mw: AnkiQt) -> None:
super().__init__(None, Qt.WindowType.Window)
self._close_event_has_cleaned_up = False
self._close_callback: Callable[[], None] = self._close
@ -42,7 +37,6 @@ class NewAddCards(QMainWindow):
self.setMinimumWidth(400)
self.setupEditor()
add_close_shortcut(self)
self._load_new_note(deck_id, notetype_id)
restoreGeom(self, "add")
gui_hooks.add_cards_did_init(self)
if not is_mac:
@ -79,7 +73,7 @@ class NewAddCards(QMainWindow):
def helpRequested(self) -> None:
openHelp(HelpPage.ADDING_CARD_AND_NOTE)
def _load_new_note(
def load_new_note(
self, deck_id: DeckId | None = None, notetype_id: NotetypeId | None = None
) -> None:
self.editor.load_note(

View file

@ -30,6 +30,8 @@ from anki.scheduler.base import ScheduleCardsAsNew
from anki.tags import MARKED_TAG
from anki.utils import is_mac
from aqt import AnkiQt, gui_hooks
from aqt.addcards import NewAddCards
from aqt.addcards_legacy import AddCards
from aqt.errors import show_exception
from aqt.exporting import ExportDialog as LegacyExportDialog
from aqt.import_export.exporting import ExportDialog
@ -262,10 +264,14 @@ class Browser(QMainWindow):
return None
def add_card(self, deck_id: DeckId):
args: list[Any] = [deck_id]
if note_type_id := self.get_active_note_type_id():
args.append(note_type_id)
self.mw._open_new_or_legacy_dialog("AddCards", *args)
add_cards = self.mw._open_new_or_legacy_dialog("AddCards")
if isinstance(add_cards, AddCards):
add_cards.set_deck(deck_id)
if note_type_id := self.get_active_note_type_id():
add_cards.set_note_type(note_type_id)
else:
assert isinstance(add_cards, NewAddCards)
add_cards.load_new_note(deck_id, self.get_active_note_type_id())
# If in the Browser we open Preview and press Ctrl+W there,
# both Preview and Browser windows get closed by Qt out of the box.

View file

@ -1287,7 +1287,11 @@ title="{}" {}>{}</button>""".format(
return aqt.dialogs.open(name, self, *args, **kwargs)
def onAddCard(self) -> None:
self._open_new_or_legacy_dialog("AddCards")
from aqt.addcards import NewAddCards
add_cards = self._open_new_or_legacy_dialog("AddCards")
if isinstance(add_cards, NewAddCards):
add_cards.load_new_note()
def onBrowse(self) -> None:
aqt.dialogs.open("Browser", self, card=self.reviewer.card)