Skip to content

Commit

Permalink
expose step counter and undone op changes in hook
Browse files Browse the repository at this point in the history
  • Loading branch information
dae committed May 8, 2021
1 parent e9e1edc commit 3736e63
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 5 deletions.
12 changes: 8 additions & 4 deletions qt/aqt/operations/collection.py
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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:
Expand Down
8 changes: 7 additions & 1 deletion qt/tools/genhooks_gui.py
Expand Up @@ -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
"""
Expand Down Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions rslib/backend.proto
Expand Up @@ -1562,6 +1562,7 @@ message OpChangesAfterUndo {
string operation = 2;
int64 reverted_to_timestamp = 3;
UndoStatus new_status = 4;
uint32 counter = 5;
}

message DefaultsForAddingIn {
Expand Down
1 change: 1 addition & 0 deletions rslib/src/backend/ops.rs
Expand Up @@ -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,
}
}
}
3 changes: 3 additions & 0 deletions rslib/src/undo/mod.rs
Expand Up @@ -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)]
Expand Down Expand Up @@ -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() {
Expand All @@ -294,6 +296,7 @@ impl Collection {
undone_op,
reverted_to,
new_undo_status: col.undo_status(),
counter,
})
});
self.state.undo.mode = UndoMode::NormalOp;
Expand Down

0 comments on commit 3736e63

Please sign in to comment.