- Introduced a new transact() method that wraps the return value
in a separate struct that describes the changes that were made.
- Changes are now gathered from the undo log, so we don't need to
guess at what was changed - eg if update_note() is called with identical
note contents, no changes are returned. Card changes will only be set
if cards were actually generated by the update_note() call, and tag
will only be set if a new tag was added.
- mw.perform_op() has been updated to expect the op to return the changes,
or a structure with the changes in it, and it will use them to fire the
change hook, instead of fetching the changes from undo_status(), so there
is no risk of race conditions.
- the various calls to mw.perform_op() have been split into separate
files like card_ops.py. Aside from making the code cleaner, this works
around a rather annoying issue with mypy. Because we run it with
no_strict_optional, mypy is happy to accept an operation that returns None,
despite the type signature saying it requires changes to be returned.
Turning no_strict_optional on for the whole codebase is not practical
at the moment, but we can enable it for individual files.
Still todo:
- The cursor keeps moving back to the start of a field when typing -
we need to ignore the refresh hook when we are the initiator.
- The busy cursor icon should probably be delayed a few hundreds ms.
- Still need to think about a nicer way of handling saveNow()
- op_made_changes(), op_affects_study_queue() might be better embedded
as properties in the object instead
The existing code was really difficult to reason about:
- The default notetype depended on the selected deck, and vice versa,
and this logic was buried in the deck and notetype choosing screens,
and models.py.
- Changes to the notetype were not passed back directly, but were fired
via a hook, which changed any screen in the app that had a notetype
selector.
It also wasn't great for performance, as the most recent deck and tags
were embedded in the notetype, which can be expensive to save and sync
for large notetypes.
To address these points:
- The current deck for a notetype, and notetype for a deck, are now
stored in separate config variables, instead of directly in the deck
or notetype. These are cheap to read and write, and we'll be able to
sync them individually in the future once config syncing is updated in
the future. I seem to recall some users not wanting the tag saving
behaviour, so I've dropped that for now, but if people end up missing
it, it would be simple to add as an extra auxiliary config variable.
- The logic for getting the starting deck and notetype has been moved
into the backend. It should be the same as the older Python code, with
one exception: when "change deck depending on notetype" is enabled in
the preferences, it will start with the current notetype ("curModel"),
instead of first trying to get a deck-specific notetype.
- ModelChooser has been duplicated into notetypechooser.py, and it
has been updated to solely be concerned with keeping track of a selected
notetype - it no longer alters global state.
- note.flush() behaves like before, as otherwise actions or add-ons
that perform bulk flushing would end up creating an undo entry for
each note
- added col.update_note() to opt in to the new behaviour
- tidy up the names of some related routines
Reviews and operations on the backend that support undoing can now be
committed immediately, so they will not be lost in the event of a crash.
This required tweaks to a few places:
- don't set collection mtime on save() unless changes were made in
Python, as otherwise we end up accidentally clearing the backend undo
queue
- autosave() is now run on every reset()
- garbage collection now runs in a timer, instead of relying on
autosave() to be run periodically
- use dataclasses for the review/checkpoint undo cases, instead of the
nasty ad-hoc list structure
- expose backend review undo to Python, and hook it into GUI
- redo is not currently exposed on the GUI, and the backend can only
cope with reviews done by the new scheduler at the moment
- the initial undo prototype code was bumping mtime/usn on undo, but
that was not ideal, as it was breaking the queue handling which expected
the mtime to match. The original rationale for bumping mtime/usn was
to avoid problems with syncing, but various operations like removing
a revlog can't be synced anyway - so we just need to ensure we clear the
undo queue prior to syncing
- SearchTerm -> SearchNode
- Operator -> Joiner; share between messages
- build_search_string() supports specifying AND/OR as a convenience
- group_searches() makes it easier to negate
That way, the caller doesn't have to hold a reference to the browser and
explicitly call it again, if it wants to search for something specific.
Also, if the browser was closed and opened for a single-card-search, it
now won't perform a redundant current-deck-search first.
- IdList could be re-used for a cids: search in the future if required.
- Embedding the message means it's easy to access from Python as
an attribute of SearchTerm.
Manually hide the editor's completer popup before accepting a short cut which changes the focus widget. Thus, work around the bug in PyQt 5.15 where a QLineEdit with open QCompleter popup won't get notified about focus changes handled by the Main Window, leading to inconsistent focus.
Initially, I wanted to solve the bug reported on
https://github.com/Arthur-Milchior/anki-html-src-in-field/issues/1
After some research, I finally discovered that the trouble was that,
when we change the note type in add card, the method
`aqt.editor.Editor.loadNote` is called twice. In itself, it would not
be a problem, but given the way callback works, its call back is
called twice on the last version of the webview. Which means that
`gui_hooks.editor_did_load_note` is called twice, which breaks this
add-on.
The reason why loadNote is called twice is because `setNote` is called
twice in `aqt.modelchooser.onModelChange`. The first time through
`gui_hooks.current_note_type_did_change` which calls
`addcards.AddCards.onModelChange` which calls `loadNote`, the second
time through `self.mw.reset()` which calls
`gui_hooks.state_did_reset()` which calls `addcards.AddCards.onReset`
which calls `setAndFocusNote` which calls `setNote`.
I should note furthermore that currently,
`gui_hooks.current_note_type_did_change` is called only when the model
chooser change a model. And `addCards.onModelChange` is never called,
only added to the hook `gui_hooks.current_note_type_did_change`. So
removing the line of code removed in this commit will have no side
effect in Anki itself. It will only affect the fact that this method
is called twice.
I do not know of any add-on calling `onModelChange` or
`gui_hooks.current_note_type_did_change`, but it means little, so of
course, it may always mean an add-on will break because of this
change. No way of being sure.
The type hints allow mypy to check the gui_hook calls, revealing a
bunch of places that are broken as they expect no arguments like the
legacy hooks.
To make mypy happy about PyQt's signal.connect(func), a qconnect()
helper has been added.