Skip to content

Commit

Permalink
fix(ui): improve tagging ux (#784)
Browse files Browse the repository at this point in the history
* fix(ui): always reset tag search panel when opened

* feat: return parent tags in tag search

Known issue: this bypasses the tag_limit

* refactor: use consistant `datetime` imports

* refactor: sort by base tag name to improve performance

* fix: escape `&` when displaying tag names

* ui: show "create and add" tag with other results

* fix: optimize and fix tag result sorting

* feat(ui): allow tags in list to be selected and added by keyboard

* ui: use `esc` to reset search focus and/or close modal

* fix(ui): add pressed+focus styling to "create tag" button

* ui: use `esc` key to close `PanelWidget`

* ui: move disambiguation button to right side

* ui: expand clickable area of "-" tag button, improve styling

* ui: add "Ctrl+M" shortcut to open tag manager

* fix(ui): show "add tags" window title when accessing from home
  • Loading branch information
CyanVoxel authored Feb 4, 2025
1 parent 480328b commit dbf7353
Show file tree
Hide file tree
Showing 11 changed files with 188 additions and 120 deletions.
37 changes: 31 additions & 6 deletions tagstudio/src/core/library/alchemy/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
func,
or_,
select,
text,
update,
)
from sqlalchemy.exc import IntegrityError
Expand Down Expand Up @@ -70,6 +71,18 @@

logger = structlog.get_logger(__name__)

TAG_CHILDREN_QUERY = text("""
-- Note for this entire query that tag_parents.child_id is the parent id and tag_parents.parent_id is the child id due to bad naming
WITH RECURSIVE ChildTags AS (
SELECT :tag_id AS child_id
UNION ALL
SELECT tp.parent_id AS child_id
FROM tag_parents tp
INNER JOIN ChildTags c ON tp.child_id = c.child_id
)
SELECT * FROM ChildTags;
""") # noqa: E501


def slugify(input_string: str) -> str:
# Convert to lowercase and normalize unicode characters
Expand Down Expand Up @@ -752,10 +765,7 @@ def search_library(

return res

def search_tags(
self,
name: str | None,
) -> list[Tag]:
def search_tags(self, name: str | None) -> list[set[Tag]]:
"""Return a list of Tag records matching the query."""
tag_limit = 100

Expand All @@ -775,8 +785,23 @@ def search_tags(
)
)

tags = session.scalars(query)
res = list(set(tags))
direct_tags = set(session.scalars(query))
ancestor_tag_ids: list[Tag] = []
for tag in direct_tags:
ancestor_tag_ids.extend(
list(session.scalars(TAG_CHILDREN_QUERY, {"tag_id": tag.id}))
)

ancestor_tags = session.scalars(
select(Tag)
.where(Tag.id.in_(ancestor_tag_ids))
.options(selectinload(Tag.parent_tags), selectinload(Tag.aliases))
)

res = [
direct_tags,
{at for at in ancestor_tags if at not in direct_tags},
]

logger.info(
"searching tags",
Expand Down
14 changes: 7 additions & 7 deletions tagstudio/src/core/library/alchemy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Licensed under the GPL-3.0 License.
# Created for TagStudio: https://github.com/CyanVoxel/TagStudio

import datetime as dt
from datetime import datetime as dt
from pathlib import Path

from sqlalchemy import JSON, ForeignKey, ForeignKeyConstraint, Integer, event
Expand Down Expand Up @@ -185,9 +185,9 @@ class Entry(Base):

path: Mapped[Path] = mapped_column(PathType, unique=True)
suffix: Mapped[str] = mapped_column()
date_created: Mapped[dt.datetime | None]
date_modified: Mapped[dt.datetime | None]
date_added: Mapped[dt.datetime | None]
date_created: Mapped[dt | None]
date_modified: Mapped[dt | None]
date_added: Mapped[dt | None]

tags: Mapped[set[Tag]] = relationship(secondary="tag_entries")

Expand Down Expand Up @@ -222,9 +222,9 @@ def __init__(
folder: Folder,
fields: list[BaseField],
id: int | None = None,
date_created: dt.datetime | None = None,
date_modified: dt.datetime | None = None,
date_added: dt.datetime | None = None,
date_created: dt | None = None,
date_modified: dt | None = None,
date_added: dt | None = None,
) -> None:
self.path = path
self.folder = folder
Expand Down
4 changes: 2 additions & 2 deletions tagstudio/src/core/library/alchemy/visitors.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
logger = structlog.get_logger(__name__)

# TODO: Reevaluate after subtags -> parent tags name change
CHILDREN_QUERY = text("""
TAG_CHILDREN_ID_QUERY = text("""
-- Note for this entire query that tag_parents.child_id is the parent id and tag_parents.parent_id is the child id due to bad naming
WITH RECURSIVE ChildTags AS (
SELECT :tag_id AS child_id
Expand Down Expand Up @@ -151,7 +151,7 @@ def __get_tag_ids(self, tag_name: str, include_children: bool = True) -> list[in
return tag_ids
outp = []
for tag_id in tag_ids:
outp.extend(list(session.scalars(CHILDREN_QUERY, {"tag_id": tag_id})))
outp.extend(list(session.scalars(TAG_CHILDREN_ID_QUERY, {"tag_id": tag_id})))
return outp

def __entry_has_all_tags(self, tag_ids: list[int]) -> ColumnElement[bool]:
Expand Down
4 changes: 2 additions & 2 deletions tagstudio/src/core/utils/refresh_dir.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import datetime as dt
from collections.abc import Iterator
from dataclasses import dataclass, field
from datetime import datetime as dt
from pathlib import Path
from time import time

Expand Down Expand Up @@ -42,7 +42,7 @@ def save_new_files(self):
path=entry_path,
folder=self.library.folder,
fields=[],
date_added=dt.datetime.now(),
date_added=dt.now(),
)
for entry_path in self.files_not_in_library
]
Expand Down
8 changes: 8 additions & 0 deletions tagstudio/src/qt/helpers/escape_text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (C) 2025 Travis Abendshien (CyanVoxel).
# Licensed under the GPL-3.0 License.
# Created for TagStudio: https://github.com/CyanVoxel/TagStudio


def escape_text(text: str):
"""Escapes characters that are problematic in Qt widgets."""
return text.replace("&", "&&")
32 changes: 20 additions & 12 deletions tagstudio/src/qt/modals/build_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,16 @@ def __build_row_item_widget(self, tag: Tag, parent_id: int, is_disambiguation: b
else:
text_color = get_text_color(primary_color, highlight_color)

# Add Tag Widget
tag_widget = TagWidget(
tag,
library=self.lib,
has_edit=False,
has_remove=True,
)
tag_widget.on_remove.connect(lambda t=parent_id: self.remove_parent_tag_callback(t))
row.addWidget(tag_widget)

# Add Disambiguation Tag Button
disam_button = QRadioButton()
disam_button.setObjectName(f"disambiguationButton.{parent_id}")
Expand All @@ -412,6 +422,15 @@ def __build_row_item_widget(self, tag: Tag, parent_id: int, is_disambiguation: b
f"QRadioButton::hover{{"
f"border-color: rgba{highlight_color.toTuple()};"
f"}}"
f"QRadioButton::pressed{{"
f"background: rgba{border_color.toTuple()};"
f"color: rgba{primary_color.toTuple()};"
f"border-color: rgba{primary_color.toTuple()};"
f"}}"
f"QRadioButton::focus{{"
f"border-color: rgba{highlight_color.toTuple()};"
f"outline:none;"
f"}}"
)

self.disam_button_group.addButton(disam_button)
Expand All @@ -421,18 +440,7 @@ def __build_row_item_widget(self, tag: Tag, parent_id: int, is_disambiguation: b
disam_button.clicked.connect(lambda checked=False: self.toggle_disam_id(parent_id))
row.addWidget(disam_button)

# Add Tag Widget
tag_widget = TagWidget(
tag,
library=self.lib,
has_edit=False,
has_remove=True,
)

tag_widget.on_remove.connect(lambda t=parent_id: self.remove_parent_tag_callback(t))
row.addWidget(tag_widget)

return disam_button, tag_widget.bg_button, container
return tag_widget.bg_button, disam_button, container

def toggle_disam_id(self, disambiguation_id: int | None):
if self.disambiguation_id == disambiguation_id:
Expand Down
Loading

0 comments on commit dbf7353

Please sign in to comment.