Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ui): sort tags in add tag panel by color/alphabetical (close #327) #329

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion tagstudio/src/qt/modals/tag_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
QFrame,
)

from src.core.constants import TAG_COLORS
from src.core.library import Library
from src.qt.widgets.panel import PanelWidget, PanelModal
from src.qt.widgets.tag import TagWidget
Expand Down Expand Up @@ -103,8 +104,28 @@ def update_tags(self, query: str):
# Get tag ids to keep this behaviorally identical
tags = [t.id for t in self.lib.tags]

if query:
# sort tags by whether the tag's name is the text that's matching the search, alphabetically, and then by color
sorted_tags = sorted(
tags,
key=lambda tag_id: (
not self.lib.get_tag(tag_id).name.lower().startswith(query.lower()),
self.lib.get_tag(tag_id).display_name(self.lib),
TAG_COLORS.index(self.lib.get_tag(tag_id).color.lower()),
),
)
else:
# sort tags by color and then alphabetically
sorted_tags = sorted(
tags,
key=lambda tag_id: (
TAG_COLORS.index(self.lib.get_tag(tag_id).color.lower()),
self.lib.get_tag(tag_id).display_name(self.lib),
),
)

first_id_set = False
for tag_id in tags:
for tag_id in sorted_tags:
if not first_id_set:
self.first_tag_id = tag_id
first_id_set = True
Expand Down
23 changes: 22 additions & 1 deletion tagstudio/src/qt/modals/tag_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
QFrame,
)

from src.core.constants import TAG_COLORS
from src.core.library import Library
from src.core.palette import ColorType, get_tag_color
from src.qt.widgets.panel import PanelWidget
Expand Down Expand Up @@ -110,7 +111,27 @@ def update_tags(self, query: str = ""):
found_tags = self.lib.search_tags(query, include_cluster=True)[: self.tag_limit]
self.first_tag_id = found_tags[0] if found_tags else None

for tag_id in found_tags:
if query:
# sort tags by whether the tag's name is the text that's matching the search, alphabetically, and then by color
sorted_tags = sorted(
found_tags,
key=lambda tag_id: (
not self.lib.get_tag(tag_id).name.lower().startswith(query.lower()),
self.lib.get_tag(tag_id).display_name(self.lib),
TAG_COLORS.index(self.lib.get_tag(tag_id).color.lower()),
),
)
else:
# sort tags by color and then alphabetically
sorted_tags = sorted(
found_tags,
key=lambda tag_id: (
TAG_COLORS.index(self.lib.get_tag(tag_id).color.lower()),
self.lib.get_tag(tag_id).display_name(self.lib),
),
)

for tag_id in sorted_tags:
c = QWidget()
l = QHBoxLayout(c)
l.setContentsMargins(0, 0, 0, 0)
Expand Down