Commit Graph

6899 Commits

Author SHA1 Message Date
Damien Elmes
6b0fe4b381 undoable ops now return changes directly; add new *_ops.py files
- 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
2021-03-19 19:45:21 +10:00
Damien Elmes
30c7cf1fdd fade out webview when pending updates; do some reviewer updates immediately
Issues that need fixing:
- when the editor saves the note with perform_op(), if it isn't modified,
no new undo entry is created, and perform_op then returns the changes
made by the previous operation instead
- the approach of fetching the last action in a subsequent backend
method is unsound, as another queued operation may sneak in first before
we have a chance to query the result - it would be better if it were
returned in a single atomic action
- redrawing the current card while editing is likely to make sound
autoplay annoyingly, and it has an unpleasant redraw. We may be better off
fading it out instead

Side note: the editor cursor moves to the start of the field when the
note is updated in another window - it might be nicer to have it move
the cursor to the end instead.
2021-03-19 19:45:21 +10:00
Damien Elmes
0a5be6543e experiment with replacing requireReset with updates on focus-in
- This avoids the need for a separate screen, though we may want to
slightly fade out the display when information is stale.
- Means the browser can delay updates just like the main window does.
2021-03-19 19:45:21 +10:00
Damien Elmes
1e849316be more reset refactoring
'card modified' covers the common case where we need to rebuild the
study queue, but is also set when changing the card flags. We want to
avoid a queue rebuild in that case, as it causes UI flicker, and may
result in a different card being shown. Note marking doesn't trigger
a queue build, but still causes flicker, and may return the user back
to the front side when they were looking at the answer.

I still think entity-based change tracking is the simplest in the
common case, but to solve the above, I've introduced an enum describing
the last operation that was taken. This currently is not trying to list
out all possible operations, and just describes the ones we want to
special-case.

Other changes:

- Fire the old 'state_did_reset' hook after an operation is performed,
so legacy code can refresh itself after an operation is performed.
- Fire the new `operation_did_execute` hook when mw.reset() is called,
so that as the UI is updated to the use the new hook, it will still
be able to refresh after legacy code calls mw.reset()
- Update the deck browser, overview and review screens to listen to
the new hook, instead of relying on the main window to call moveToState()
- Add a 'set flag' backend action, so we can distinguish it from a
normal card update.
- Drop the separate added/modified entries in the change list in
favour of a single entry per entity.
- Add typing to mw.state
- Tweak perform_op()
- Convert a few more actions to use perform_op()
2021-03-19 19:45:21 +10:00
Damien Elmes
112cbe8b59 experiment with finer-scoped reset in perform_op()
Basic proof of concept, where the 'delete note' operation in the
reviewer has been updated to use mw.perform_op(). Instead of manually
calling .reset() afterwards, a summary of the changes is returned as
part of the undo status query, and various parts of the GUI can listen
to gui_hooks.operation_did_execute and decide whether they want to
redraw based on the scope of the changes. This should allow the sidebar
to selectively redraw just the tags area in the future for example.

Currently we're just listing out all possible areas that might be changed;
in the future we could theoretically inspect the specific changes in the
undo log to provide a more accurate report (avoiding refreshing the tags
list when no tags were added for example).

You can test it out by opening the browse screen while studying, and
then deleting the current card - the browser should update to show (deleted)
on the cards due the earlier change.

If going ahead with this, aside from updating all the screens that currently
listen for resets, some thought will be required on how we can integrate
it with legacy code that expects to called when resets are made, and expects
to call .reset() when it makes changes.

Thoughts?
2021-03-19 19:45:21 +10:00
Damien Elmes
7fab319dad derive reset scope from last undoable operation 2021-03-19 19:45:21 +10:00
Damien Elmes
90526c61cd move ops.rs out of undo/ 2021-03-19 19:45:21 +10:00
Damien Elmes
8fc43956c2 move collection mtime bump into backend
Fixes the following issue:
- some code directly modifies the database, causing modified_in_python
to be set to true
- an undoable operation is run, which calls autosave() at the end
- autosave() notices there's an undoable operation, and commits immediately
- because modified_in_python was true, col.mtime was bumped in Python
- that invalidated the undo queue, preventing the operation from being
undone
2021-03-19 19:45:21 +10:00
Damien Elmes
e364b36dc4 experiment with preserving search when resetting
Up until now, we've been forcing a new search whenever reset is called.
The primary reason was that the card list display routines did not expect
a card or note to have been removed. By updating the model to show
"(deleted)" when a card or note is missing, we no longer have to repeat
the search.

This has a few advantages:

- Searches, especially complex ones, can be slow to execute. When we
perform them after every operation like a delete, it can make Anki feel
sluggish.
- The fact that notes have been deleted becomes more obvious - some users
found it easy to miss the "deleted" pop-up in the past.

This change does not just affect deletions, as many other operations
trigger a reset as well. In the past, when using 'set due date' in the
review screen for example, it caused an ugly flicker in the browser screen,
and could be slow when the current search couldn't be quickly redone.

The disadvantage of this approach is that the displayed content may
not reflect the specified search, which has the potential to be confusing.
But if that turns out to be a problem, it could be (partly) alleviated by
displaying a refresh button next to the search bar when the search may
need to be refreshed.

Feedback welcome!
2021-03-19 19:45:21 +10:00
Damien Elmes
6f9e1d62ec deck deletion in deck list was not resetting state 2021-03-19 19:45:20 +10:00
Damien Elmes
a90a6ab3cd normalize first field before comparing with local DB
https://forums.ankiweb.net/t/python-checksum-rust-checksum/8195/8
2021-03-17 22:22:58 +10:00
Damien Elmes
7472181aeb Revert "ensure fields normalized before checksumming"
This reverts commit f4bd867b3b54b172125d2f2021c8c6a6e69c4c4d.
2021-03-17 22:21:13 +10:00
Damien Elmes
f8b5210df9 fix schema not being modified
https://forums.ankiweb.net/t/python-checksum-rust-checksum/8195/8
2021-03-17 22:18:31 +10:00
Damien Elmes
19c217d8a4 add note about bumping Rust deps 2021-03-15 13:29:36 +10:00
Damien Elmes
6a119d92fb
Merge pull request #1071 from RumovZ/sidebar-del
Only delete by key when selected items have same type
2021-03-15 13:27:31 +10:00
Damien Elmes
36fc8bf90a
Merge pull request #1070 from Arthur-Milchior/update_is_executable
update is executable
2021-03-15 13:23:29 +10:00
Damien Elmes
5ff5a6f2e6
Merge pull request #1069 from Arthur-Milchior/Emptying_field_lead_to_proper_message
Emptying field lead to proper message
2021-03-15 13:17:05 +10:00
Arthur Milchior
9661684ca3 If a template name contains only quote, show relevant error message
This is for the sake of the consistency with the last commit
2021-03-14 02:10:48 +01:00
Arthur Milchior
dd48b2dff0 Return meaningful message if a field is empty after normalizing
This correct the probably rare bug as follow:

I got a note type with a field whose name is "\".

When I made any change to this note type, even unrelated, I get a message stating that there is an empty field. This is
strange because I can see it to be false. Investigating show that "\" is normalized to empty field. This ensure that
it's shown
2021-03-14 02:10:32 +01:00
RumovZ
35fa58730c Improve grouping of sidebar methods 2021-03-13 09:45:06 +01:00
RumovZ
53665f0cf4 Use same constraints for deleting by key press ...
... as for deleting via context menu, i.e., delete key does nothing if
not all selected items are of the same type.
2021-03-13 09:31:56 +01:00
Arthur Milchior
80db5dc7d5 update is executable
Without this change, I can't just do `./update.py` and need to do `python3 update.py`
2021-03-13 07:35:36 +01:00
Damien Elmes
1ab085dfab ensure fields normalized before checksumming
https://forums.ankiweb.net/t/python-checksum-rust-checksum/8195
2021-03-13 10:23:32 +10:00
Damien Elmes
bd959731d7
Merge pull request #1067 from RumovZ/regex-err
Fix regex error formatting and search error escaping
2021-03-13 10:16:10 +10:00
RumovZ
e033f21767 Fix markdown escaping in search errors 2021-03-12 20:32:38 +01:00
RumovZ
2078a094f4 Fix formatting of invalid regex error
Preserve whitespace, special characters and use monospace font.
2021-03-12 20:31:23 +01:00
Damien Elmes
28994458e9 add indexes to graves table to speed up undo 2021-03-12 18:59:24 +10:00
Damien Elmes
44dc3f494c avoid hanging UI when undoing in browse screen 2021-03-12 18:54:08 +10:00
Damien Elmes
57a05a2ae3 undo in background, and show progress window 2021-03-12 17:54:56 +10:00
Damien Elmes
d92f1499ff experiment with perform_op() wrapper
Fixes #1065, and gives us similar functionality to #1066
2021-03-12 17:54:13 +10:00
Damien Elmes
2ffc055487 'change deck' now undoable 2021-03-12 16:27:57 +10:00
Damien Elmes
7b7d7e8330 consume original card when updating 2021-03-12 16:20:58 +10:00
Damien Elmes
24762261d9 make 'forget card' undoable; remove checkpoint() in set_due_date 2021-03-12 16:13:50 +10:00
Damien Elmes
9ba02b5ca6 fix set due date not remembering default in browse screen 2021-03-12 15:47:11 +10:00
Damien Elmes
6e0e17b2b9 Revert "Merge pull request #1066 from RumovZ/editor-save-dec"
This reverts commit a0c47243b6, reversing
changes made to 0ab87b7339.

@RumoVZ this broke a bunch of operations like 'select notes' and
'set due date'. When the triggered signal is connected to a function,
PyQt looks at the function signature to decide what arguments to pass
it. The wrapper was using *args, so PyQt passes in an extra argument,
which the underlying function didn't expect.

I tried settting __signature__ on the wrapper, but PyQT seems to
ignore it, so we may either need to check all of the existing calls
and add the ignored extra arguments, or create a separate wrapper for
such cases.
2021-03-12 15:44:19 +10:00
Damien Elmes
3b067c7a66 limit initial sort selection to new cards
https://github.com/ankidroid/Anki-Android/issues/8172
2021-03-12 14:58:19 +10:00
Damien Elmes
c1316bb65f 'set due date' now undoable 2021-03-12 14:50:31 +10:00
Damien Elmes
ec8adf7371 move old scheduler files into scheduler/
Includes a hack that should allow existing imports to continue to work;
if this breaks things for you, please let me know.
2021-03-12 14:43:45 +10:00
Damien Elmes
ad973bb701 split out common scheduler code into base.py, use scheduler/ dir
Also move the legacy aliases into a separate file
2021-03-12 14:07:52 +10:00
Damien Elmes
a0c47243b6
Merge pull request #1066 from RumovZ/editor-save-dec
Add decorators for calling editor.saveNow()
2021-03-12 09:23:18 +10:00
RumovZ
538afd94bc Add decorator to save editor in sidebar 2021-03-11 22:25:18 +01:00
RumovZ
9bf93573da Add decorator to save editor in browser 2021-03-11 22:24:24 +01:00
Damien Elmes
0ab87b7339 enable deck removal undo again 2021-03-11 22:24:12 +10:00
Damien Elmes
71789eb51a
Merge pull request #1044 from RumovZ/sidebar-tools
Add sidebar modes for different click behaviour
2021-03-11 21:57:53 +10:00
RumovZ
f4c2fe6485 Merge branch 'master' into sidebar-tools 2021-03-11 12:08:32 +01:00
RumovZ
dad92e1e22 Annotate decks.rem as deprecated 2021-03-11 11:26:35 +01:00
Damien Elmes
e20c5ed9c5 deck drag&drop undo 2021-03-11 20:02:16 +10:00
RumovZ
c11a394753 Remove prompt when deleting from deckbrowser 2021-03-11 10:28:23 +01:00
Damien Elmes
984e2c2666 add a separate 'rename deck' method 2021-03-11 19:24:54 +10:00
RumovZ
5d93832713 Run background tasks with progress 2021-03-11 10:04:58 +01:00