From 969b1674f07475e15954ee1a26e682b0ed3e6366 Mon Sep 17 00:00:00 2001 From: Travis Abendshien <46939827+CyanVoxel@users.noreply.github.com> Date: Tue, 19 Aug 2025 12:58:09 -0700 Subject: [PATCH] fix(search): pass current `BrowsingState` to `from_tag_id()` (#1038) * fix(search): pass current BrowsingState to from_tag_id() * chore: fix docstring * fix: use existing query to build tag_id query Co-authored-by: Jann Stute <46534683+Computerdores@users.noreply.github.com> --------- Co-authored-by: Jann Stute <46534683+Computerdores@users.noreply.github.com> --- src/tagstudio/core/library/alchemy/enums.py | 15 ++++++++++++++- .../controller/components/tag_box_controller.py | 12 +++++++++--- src/tagstudio/qt/modals/tag_search.py | 4 +++- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/tagstudio/core/library/alchemy/enums.py b/src/tagstudio/core/library/alchemy/enums.py index f1d3106c..a51271e5 100644 --- a/src/tagstudio/core/library/alchemy/enums.py +++ b/src/tagstudio/core/library/alchemy/enums.py @@ -97,7 +97,20 @@ class BrowsingState: return cls(query=search_query) @classmethod - def from_tag_id(cls, tag_id: int | str) -> "BrowsingState": + def from_tag_id( + cls, tag_id: int | str, state: "BrowsingState | None" = None + ) -> "BrowsingState": + """Create and return a BrowsingState object given a tag ID. + + Args: + tag_id(int): The tag ID to search for. + state(BrowsingState|None): An optional BrowsingState object to use + existing options from, such as sorting options. + + """ + logger.warning(state) + if state: + return state.with_search_query(f"tag_id:{str(tag_id)}") return cls(query=f"tag_id:{str(tag_id)}") @classmethod diff --git a/src/tagstudio/qt/controller/components/tag_box_controller.py b/src/tagstudio/qt/controller/components/tag_box_controller.py index 974c62e2..9bb144d8 100644 --- a/src/tagstudio/qt/controller/components/tag_box_controller.py +++ b/src/tagstudio/qt/controller/components/tag_box_controller.py @@ -38,14 +38,18 @@ class TagBoxWidget(TagBoxWidgetView): case TagClickActionOption.OPEN_EDIT: self._on_edit(tag) case TagClickActionOption.SET_SEARCH: - self.__driver.update_browsing_state(BrowsingState.from_tag_id(tag.id)) + self.__driver.update_browsing_state( + BrowsingState.from_tag_id(tag.id, self.__driver.browsing_history.current) + ) case TagClickActionOption.ADD_TO_SEARCH: # NOTE: modifying the ast and then setting that would be nicer # than this string manipulation, but also much more complex, # due to needing to implement a visitor that turns an AST to a string # So if that exists when you read this, change the following accordingly. current = self.__driver.browsing_history.current - suffix = BrowsingState.from_tag_id(tag.id).query + suffix = BrowsingState.from_tag_id( + tag.id, self.__driver.browsing_history.current + ).query assert suffix is not None self.__driver.update_browsing_state( current.with_search_query( @@ -90,4 +94,6 @@ class TagBoxWidget(TagBoxWidgetView): @override def _on_search(self, tag: Tag) -> None: # type: ignore[misc] self.__driver.main_window.search_field.setText(f"tag_id:{tag.id}") - self.__driver.update_browsing_state(BrowsingState.from_tag_id(tag.id)) + self.__driver.update_browsing_state( + BrowsingState.from_tag_id(tag.id, self.__driver.browsing_history.current) + ) diff --git a/src/tagstudio/qt/modals/tag_search.py b/src/tagstudio/qt/modals/tag_search.py index efd81634..ab1d00ca 100644 --- a/src/tagstudio/qt/modals/tag_search.py +++ b/src/tagstudio/qt/modals/tag_search.py @@ -316,7 +316,9 @@ class TagSearchPanel(PanelWidget): tag_widget.search_for_tag_action.triggered.connect( lambda checked=False, tag_id=tag.id, driver=self.driver: ( driver.main_window.search_field.setText(f"tag_id:{tag_id}"), - driver.update_browsing_state(BrowsingState.from_tag_id(tag_id)), + driver.update_browsing_state( + BrowsingState.from_tag_id(tag_id, driver.browsing_history.current) + ), ) ) tag_widget.search_for_tag_action.setEnabled(True)