Skip to content

Commit

Permalink
Improve the UX of creating tags
Browse files Browse the repository at this point in the history
  • Loading branch information
kra-mo committed Dec 22, 2023
1 parent 5bd6c85 commit 230ec26
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 18 deletions.
2 changes: 1 addition & 1 deletion hyperplane/item_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from gi.repository import Gio, GLib, Gtk

from hyperplane import shared
from hyperplane.utils.files import path_represents_tags
from hyperplane.utils.tags import path_represents_tags


class HypItemFilter(Gtk.Filter):
Expand Down
13 changes: 10 additions & 3 deletions hyperplane/items_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ def __init__(
self,
gfile: Optional[Gio.File] = None,
tags: Optional[list[str]] = None,
**kwargs) -> None:
**kwargs,
) -> None:
super().__init__(**kwargs)
self.gfile = gfile
self.tags = tags
Expand Down Expand Up @@ -110,6 +111,7 @@ def __init__(
text_drop_target.connect("drop", self.__drop_text)
self.grid_view.add_controller(text_drop_target)

shared.postmaster.connect("tags-changed", self.__tags_changed)
shared.postmaster.connect("toggle-hidden", self.__toggle_hidden)

self.file_attrs = ",".join(
Expand Down Expand Up @@ -145,7 +147,7 @@ def __init__(
self.filter_list.connect("items-changed", self.__items_changed)
self.__items_changed()

shared.postmaster.connect("tag-location-created", self.__tag_location_added)
shared.postmaster.connect("tag-location-created", self.__tag_location_created)

# Set up the "page" action group
self.shortcut_controller = Gtk.ShortcutController.new()
Expand Down Expand Up @@ -278,7 +280,7 @@ def __get_list(

return Gtk.FlattenListModel.new(list_store)

def __tag_location_added(
def __tag_location_created(
self, _obj: Any, string_list: Gtk.StringList, new_location: Gio.File
):
if not self.tags:
Expand Down Expand Up @@ -357,6 +359,11 @@ def __bind(self, _factory: Gtk.SignalListItemFactory, item: Gtk.ListItem) -> Non
def __unbind(self, _factory: Gtk.SignalListItemFactory, item: Gtk.ListItem) -> None:
item.get_child().unbind()

def __tags_changed(self, *_args: Any) -> None:
# There may be a way to check whether I actually need to reload here
# But it may actually be slower
self.reload()

def __toggle_hidden(self, *_args: Any) -> None:
if shared.show_hidden:
self.item_filter.changed(Gtk.FilterChange.LESS_STRICT)
Expand Down
16 changes: 2 additions & 14 deletions hyperplane/utils/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,11 @@

from hyperplane import shared
from hyperplane.file_properties import DOT_IS_NOT_EXTENSION
from hyperplane.utils.tags import path_represents_tags

# TODO: Handle errors better


def path_represents_tags(path: PathLike | str) -> bool:
"""Checks whether a given `path` represents tags or not."""
path = Path(path)

if path == shared.home:
return False

if not path.is_relative_to(shared.home):
return False

return all(part in shared.tags for part in path.relative_to(shared.home).parts)


def copy(src: Gio.File, dst: Gio.File) -> None:
"""
Asynchronously copies a file or directory from `src` to `dst`.
Expand Down Expand Up @@ -122,7 +110,7 @@ def move(src: Gio.File, dst: Gio.File) -> None:
None,
lambda *_: __emit_tags_changed(dst) if tags_changed else None,
None,
None
None,
)
task.run_in_thread(lambda *_: shutil.move(src, dst))

Expand Down
16 changes: 16 additions & 0 deletions hyperplane/utils/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,25 @@
# SPDX-License-Identifier: GPL-3.0-or-later

"""Miscellaneous utilities for working with tags."""
from os import PathLike
from pathlib import Path

from hyperplane import shared


def path_represents_tags(path: PathLike | str) -> bool:
"""Checks whether a given `path` represents tags or not."""
path = Path(path)

if path == shared.home:
return False

if not path.is_relative_to(shared.home):
return False

return all(part in shared.tags for part in path.relative_to(shared.home).parts)


def add_tags(*tags: str) -> None:
"""
Adds new tags and updates the list of tags.
Expand Down
3 changes: 3 additions & 0 deletions hyperplane/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,9 @@ def add_tag(*_args: Any) -> None:
_("New Category"),
(_("Cancel"), None, None, None, False),
(_("Add"), None, Adw.ResponseAppearance.SUGGESTED, add_tag, True),
body=_(
"Existing folders with the same name will be added to the category."
),
extra_child=preferences_group,
)

Expand Down

0 comments on commit 230ec26

Please sign in to comment.