From 18bd821a1f671c8189ea09add393ffdd9fd8af81 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Mon, 13 Sep 2021 15:31:24 +1000 Subject: [PATCH] preserve focus on notetype change Closes #1360 --- qt/aqt/addcards.py | 4 +++- qt/aqt/editor.py | 5 ++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/qt/aqt/addcards.py b/qt/aqt/addcards.py index 7022829db..ac0f65319 100644 --- a/qt/aqt/addcards.py +++ b/qt/aqt/addcards.py @@ -145,7 +145,9 @@ class AddCards(QDialog): # and update editor state self.editor.note = new - self.editor.loadNote() + self.editor.loadNote( + focusTo=min(self.editor.last_field_index or 0, len(new.fields) - 1) + ) def _load_new_note(self, sticky_fields_from: Optional[Note] = None) -> None: note = self._new_note() diff --git a/qt/aqt/editor.py b/qt/aqt/editor.py index d4019ab77..6c847b300 100644 --- a/qt/aqt/editor.py +++ b/qt/aqt/editor.py @@ -106,6 +106,9 @@ class Editor: self.note: Optional[Note] = None self.addMode = addMode self.currentField: Optional[int] = None + # Similar to currentField, but not set to None on a blur. May be + # outside the bounds of the current notetype. + self.last_field_index: Optional[int] = None # current card, for card layout self.card: Optional[Card] = None self.setupOuter() @@ -387,7 +390,7 @@ $editorToolbar.then(({{ toolbar }}) => toolbar.appendGroup({{ # focused into field? elif cmd.startswith("focus"): (type, num) = cmd.split(":", 1) - self.currentField = int(num) + self.last_field_index = self.currentField = int(num) gui_hooks.editor_did_focus_field(self.note, self.currentField) elif cmd.startswith("toggleStickyAll"):