expose step counter and undone op changes in hook

This commit is contained in:
Damien Elmes 2021-05-08 17:51:36 +10:00
parent e9e1edc64d
commit 3736e63a57
5 changed files with 20 additions and 5 deletions

View file

@ -3,7 +3,7 @@
from __future__ import annotations
from anki.collection import LegacyCheckpoint, LegacyReviewUndo
from anki.collection import LegacyCheckpoint, LegacyReviewUndo, OpChangesAfterUndo
from anki.errors import UndoEmpty
from anki.types import assert_exhaustive
from aqt import gui_hooks
@ -15,6 +15,10 @@ from aqt.utils import showInfo, showWarning, tooltip, tr
def undo(*, parent: QWidget) -> None:
"Undo the last operation, and refresh the UI."
def on_success(out: OpChangesAfterUndo) -> None:
gui_hooks.state_did_undo(out)
tooltip(tr.undo_action_undone(action=out.operation), parent=parent)
def on_failure(exc: Exception) -> None:
if isinstance(exc, UndoEmpty):
# backend has no undo, but there may be a checkpoint
@ -23,9 +27,9 @@ def undo(*, parent: QWidget) -> None:
else:
showWarning(str(exc), parent=parent)
CollectionOp(parent, lambda col: col.undo()).success(
lambda out: tooltip(tr.undo_action_undone(action=out.operation), parent=parent)
).failure(on_failure).run_in_background()
CollectionOp(parent, lambda col: col.undo()).success(on_success).failure(
on_failure
).run_in_background()
def _legacy_undo(*, parent: QWidget) -> None:

View file

@ -27,6 +27,7 @@ from anki.cards import Card
from anki.decks import DeckDict, DeckConfigDict
from anki.hooks import runFilter, runHook
from anki.models import NotetypeDict
from anki.collection import OpChangesAfterUndo
from aqt.qt import QDialog, QEvent, QMenu, QWidget
from aqt.tagedit import TagEdit
"""
@ -475,7 +476,12 @@ hooks = [
name="state_did_revert",
args=["action: str"],
legacy_hook="revertedState",
doc="Called when user used the undo option to restore to an earlier database state.",
doc="Legacy hook, called after undoing.",
),
Hook(
name="state_did_undo",
args=["changes: OpChangesAfterUndo"],
doc="Called after backend undoes a change.",
),
Hook(
name="state_did_reset",

View file

@ -1562,6 +1562,7 @@ message OpChangesAfterUndo {
string operation = 2;
int64 reverted_to_timestamp = 3;
UndoStatus new_status = 4;
uint32 counter = 5;
}
message DefaultsForAddingIn {

View file

@ -67,6 +67,7 @@ impl OpOutput<UndoOutput> {
operation: self.output.undone_op.describe(tr),
reverted_to_timestamp: self.output.reverted_to.0,
new_status: Some(self.output.new_undo_status.into_protobuf(tr)),
counter: self.output.counter as u32,
}
}
}

View file

@ -61,6 +61,7 @@ pub struct UndoOutput {
pub undone_op: Op,
pub reverted_to: TimestampSecs,
pub new_undo_status: UndoStatus,
pub counter: usize,
}
#[derive(Debug, Default)]
@ -285,6 +286,7 @@ impl Collection {
let undone_op = step.kind;
let reverted_to = step.timestamp;
let changes = step.changes;
let counter = step.counter;
self.state.undo.mode = mode;
let res = self.transact(undone_op.clone(), |col| {
for change in changes.into_iter().rev() {
@ -294,6 +296,7 @@ impl Collection {
undone_op,
reverted_to,
new_undo_status: col.undo_status(),
counter,
})
});
self.state.undo.mode = UndoMode::NormalOp;