From 3736e63a57f0c7770b5f1d79bd557685eddfb7b8 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Sat, 8 May 2021 17:51:36 +1000 Subject: [PATCH] expose step counter and undone op changes in hook --- qt/aqt/operations/collection.py | 12 ++++++++---- qt/tools/genhooks_gui.py | 8 +++++++- rslib/backend.proto | 1 + rslib/src/backend/ops.rs | 1 + rslib/src/undo/mod.rs | 3 +++ 5 files changed, 20 insertions(+), 5 deletions(-) diff --git a/qt/aqt/operations/collection.py b/qt/aqt/operations/collection.py index 1eb130b8c4..373921b5bc 100644 --- a/qt/aqt/operations/collection.py +++ b/qt/aqt/operations/collection.py @@ -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 @@ 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 on_failure(exc: Exception) -> 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: diff --git a/qt/tools/genhooks_gui.py b/qt/tools/genhooks_gui.py index 8d194dbce5..42e7fb085f 100644 --- a/qt/tools/genhooks_gui.py +++ b/qt/tools/genhooks_gui.py @@ -27,6 +27,7 @@ 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 @@ def on_browser_will_build_tree(handled, root, stage, browser): 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", diff --git a/rslib/backend.proto b/rslib/backend.proto index 32d72d873a..0ca84af7bb 100644 --- a/rslib/backend.proto +++ b/rslib/backend.proto @@ -1562,6 +1562,7 @@ message OpChangesAfterUndo { string operation = 2; int64 reverted_to_timestamp = 3; UndoStatus new_status = 4; + uint32 counter = 5; } message DefaultsForAddingIn { diff --git a/rslib/src/backend/ops.rs b/rslib/src/backend/ops.rs index 0f9c436360..2d6c7fa2b8 100644 --- a/rslib/src/backend/ops.rs +++ b/rslib/src/backend/ops.rs @@ -67,6 +67,7 @@ impl OpOutput { 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, } } } diff --git a/rslib/src/undo/mod.rs b/rslib/src/undo/mod.rs index 1cf1ab94d2..5f79e3ae6d 100644 --- a/rslib/src/undo/mod.rs +++ b/rslib/src/undo/mod.rs @@ -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;