From f7f509c70dc4fcb553a8dbfd8e5e29f65bc8df6c Mon Sep 17 00:00:00 2001 From: abdo Date: Sat, 9 Jan 2021 16:55:08 +0300 Subject: [PATCH] Move tag collapse method to the backend --- pylib/anki/tags.py | 7 +++---- qt/aqt/browser.py | 22 ++++------------------ rslib/backend.proto | 6 ++++++ rslib/src/backend/mod.rs | 19 +++++++++++++++++++ rslib/src/tags.rs | 27 +++++++++++++-------------- 5 files changed, 45 insertions(+), 36 deletions(-) diff --git a/pylib/anki/tags.py b/pylib/anki/tags.py index 78adba0eb..2fd52375e 100644 --- a/pylib/anki/tags.py +++ b/pylib/anki/tags.py @@ -80,10 +80,9 @@ class TagManager: res = self.col.db.list(query) return list(set(self.split(" ".join(res)))) - def toggle_browser_collapse(self, name: str): - tag = self.col.backend.get_tag(name) - tag.config.browser_collapsed = not tag.config.browser_collapsed - self.col.backend.update_tag(tag) + def set_collapsed(self, tag: str, collapsed: bool): + "Set browser collapse state for tag, registering the tag if missing." + self.col.backend.set_tag_collapsed(name=tag, collapsed=collapsed) # Bulk addition/removal from notes ############################################################# diff --git a/qt/aqt/browser.py b/qt/aqt/browser.py index 3d7161a4c..dd008c657 100644 --- a/qt/aqt/browser.py +++ b/qt/aqt/browser.py @@ -21,13 +21,7 @@ from anki.consts import * from anki.lang import without_unicode_isolation from anki.models import NoteType from anki.notes import Note -from anki.rsbackend import ( - ConcatSeparator, - DeckTreeNode, - InvalidInput, - NotFoundError, - TagTreeNode, -) +from anki.rsbackend import ConcatSeparator, DeckTreeNode, InvalidInput, TagTreeNode from anki.stats import CardStats from anki.utils import htmlToTextLine, ids2str, isMac, isWin from aqt import AnkiQt, gui_hooks @@ -1152,18 +1146,10 @@ QTableView {{ gridline-color: {grid} }} return lambda: self.setFilter("tag", full_name) def toggle_expand(): - full_name = head + node.name # pylint: disable=cell-var-from-loop - - def toggle(_): - try: - self.mw.col.tags.toggle_browser_collapse(full_name) - except NotFoundError: - # tag is missing, register it first - self.mw.col.tags.register([full_name]) - self.mw.col.tags.toggle_browser_collapse(full_name) - - return toggle + return lambda _: self.mw.col.tags.set_collapsed( + full_name, not node.collapsed + ) item = SidebarItem( node.name, diff --git a/rslib/backend.proto b/rslib/backend.proto index 80aafe3ad..6a8165cdf 100644 --- a/rslib/backend.proto +++ b/rslib/backend.proto @@ -214,6 +214,7 @@ service BackendService { rpc AllTags (Empty) returns (AllTagsOut); rpc GetTag (String) returns (Tag); rpc UpdateTag (Tag) returns (Bool); + rpc SetTagCollapsed (SetTagCollapsedIn) returns (Bool); rpc ClearTag (String) returns (Bool); rpc TagTree (Empty) returns (TagTreeNode); @@ -796,6 +797,11 @@ message AllTagsOut { repeated Tag tags = 1; } +message SetTagCollapsedIn { + string name = 1; + bool collapsed = 2; +} + message TagConfig { bool browser_collapsed = 1; } diff --git a/rslib/src/backend/mod.rs b/rslib/src/backend/mod.rs index caa6e5318..e2da5d64e 100644 --- a/rslib/src/backend/mod.rs +++ b/rslib/src/backend/mod.rs @@ -44,6 +44,7 @@ use crate::{ get_remote_sync_meta, sync_abort, sync_login, FullSyncProgress, NormalSyncProgress, SyncActionRequired, SyncAuth, SyncMeta, SyncOutput, SyncStage, }, + tags::Tag, template::RenderedNode, text::{extract_av_tags, strip_av_tags, AVTag}, timestamp::TimestampSecs, @@ -1316,6 +1317,24 @@ impl BackendService for Backend { }) } + fn set_tag_collapsed(&self, input: pb::SetTagCollapsedIn) -> BackendResult { + self.with_col(|col| { + let name = &input.name; + let tag: Result = if let Some(tag) = col.storage.get_tag(name)? { + Ok(tag) + } else { + // tag is missing, register it + let t = Tag { + name: name.to_owned(), + ..Default::default() + }; + Ok(col.register_tag(t)?.0) + }; + tag?.config.browser_collapsed = input.collapsed; + Ok(pb::Bool { val: true }) + }) + } + fn register_tags(&self, input: pb::RegisterTagsIn) -> BackendResult { self.with_col(|col| { col.transact(None, |col| { diff --git a/rslib/src/tags.rs b/rslib/src/tags.rs index 7b2621561..26ff9b486 100644 --- a/rslib/src/tags.rs +++ b/rslib/src/tags.rs @@ -232,11 +232,11 @@ impl Collection { usn, ..Default::default() })?; - if t.0.is_empty() { + if t.0.name.is_empty() { continue; } added |= t.1; - seen.insert(UniCase::new(t.0)); + seen.insert(UniCase::new(t.0.name)); } // exit early if no non-empty tags @@ -247,28 +247,27 @@ impl Collection { // return the sorted, canonified tags let mut tags = seen.into_iter().collect::>(); tags.sort_unstable(); - let tags: Vec<_> = tags - .into_iter() - .map(|s| s.into_inner().to_string()) - .collect(); + let tags: Vec<_> = tags.into_iter().map(|s| s.into_inner()).collect(); Ok((tags, added)) } - pub(crate) fn register_tag<'a>(&self, tag: Tag) -> Result<(Cow<'a, str>, bool)> { + pub(crate) fn register_tag(&self, tag: Tag) -> Result<(Tag, bool)> { let native_name = human_tag_name_to_native(&tag.name); + let mut t = Tag { + name: native_name.clone(), + ..tag + }; if native_name.is_empty() { - return Ok(("".into(), false)); + return Ok((t, false)); } if let Some(preferred) = self.storage.preferred_tag_case(&native_name)? { - Ok((native_tag_name_to_human(&preferred).into(), false)) + t.name = native_tag_name_to_human(&preferred); + Ok((t, false)) } else { - let mut t = Tag { - name: native_name.clone(), - ..tag - }; self.storage.register_tag(&mut t)?; - Ok((native_tag_name_to_human(&native_name).into(), true)) + t.name = native_tag_name_to_human(&t.name); + Ok((t, true)) } }