deck drag&drop undo

This commit is contained in:
Damien Elmes 2021-03-11 19:54:29 +10:00
parent 984e2c2666
commit e20c5ed9c5
3 changed files with 26 additions and 12 deletions

View File

@ -287,13 +287,21 @@ class DeckBrowser:
self._renderPage(reuse=True) self._renderPage(reuse=True)
def _handle_drag_and_drop(self, source: int, target: int) -> None: def _handle_drag_and_drop(self, source: int, target: int) -> None:
try: def process() -> None:
self.mw.col.decks.drag_drop_decks([source], target) self.mw.col.decks.drag_drop_decks([source], target)
except Exception as e:
showWarning(str(e)) def on_done(fut: Future) -> None:
return try:
gui_hooks.sidebar_should_refresh_decks() fut.result()
self.show() except Exception as e:
showWarning(str(e))
return
self.mw.update_undo_actions()
gui_hooks.sidebar_should_refresh_decks()
self.show()
self.mw.taskman.with_progress(process, on_done)
def ask_delete_deck(self, did: int) -> bool: def ask_delete_deck(self, did: int) -> bool:
deck = self.mw.col.decks.get(did) deck = self.mw.col.decks.get(did)

View File

@ -480,9 +480,9 @@ class SidebarTreeView(QTreeView):
return return
self.refresh() self.refresh()
self.mw.deckBrowser.refresh() self.mw.deckBrowser.refresh()
self.mw.update_undo_actions()
def on_save() -> None: def on_save() -> None:
self.mw.checkpoint(tr(TR.ACTIONS_RENAME_DECK))
self.browser.model.beginReset() self.browser.model.beginReset()
self.mw.taskman.with_progress( self.mw.taskman.with_progress(
lambda: self.col.decks.drag_drop_decks(source_ids, target.id), on_done lambda: self.col.decks.drag_drop_decks(source_ids, target.id), on_done

View File

@ -617,9 +617,8 @@ impl Collection {
source_decks: &[DeckID], source_decks: &[DeckID],
target: Option<DeckID>, target: Option<DeckID>,
) -> Result<()> { ) -> Result<()> {
self.state.deck_cache.clear();
let usn = self.usn()?; let usn = self.usn()?;
self.transact(None, |col| { self.transact(Some(UndoableOpKind::RenameDeck), |col| {
let target_deck; let target_deck;
let mut target_name = None; let mut target_name = None;
if let Some(target) = target { if let Some(target) = target {
@ -634,18 +633,25 @@ impl Collection {
for source in source_decks { for source in source_decks {
if let Some(mut source) = col.storage.get_deck(*source)? { if let Some(mut source) = col.storage.get_deck(*source)? {
let orig = source.clone();
let new_name = drag_drop_deck_name(&source.name, target_name); let new_name = drag_drop_deck_name(&source.name, target_name);
if new_name == source.name { if new_name == source.name {
continue; continue;
} }
let orig = source.clone();
// this is basically update_deck_inner(), except:
// - we skip the normalization in prepare_for_update()
// - we skip the match_or_create_parents() step
source.set_modified(usn);
source.name = new_name; source.name = new_name;
col.ensure_deck_name_unique(&mut source, usn)?; col.ensure_deck_name_unique(&mut source, usn)?;
col.rename_child_decks(&orig, &source.name, usn)?; col.rename_child_decks(&orig, &source.name, usn)?;
source.set_modified(usn); col.update_single_deck_undoable(&mut source, orig)?;
col.storage.update_deck(&source)?;
// after updating, we need to ensure all grandparents exist, which may not be the case // after updating, we need to ensure all grandparents exist, which may not be the case
// in the parent->child case // in the parent->child case
// FIXME: maybe we only need to do this once at the end of the loop?
col.create_missing_parents(&source.name, usn)?; col.create_missing_parents(&source.name, usn)?;
} }
} }