Skip to content

Commit

Permalink
Create tags-added and tags-removed signals
Browse files Browse the repository at this point in the history
  • Loading branch information
kra-mo committed Dec 22, 2023
1 parent 230ec26 commit 9796930
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
12 changes: 7 additions & 5 deletions hyperplane/items_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ 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("tags-removed", self.__tags_changed, False)
shared.postmaster.connect("tags-added", self.__tags_changed, True)
shared.postmaster.connect("toggle-hidden", self.__toggle_hidden)

self.file_attrs = ",".join(
Expand Down Expand Up @@ -359,10 +360,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 __tags_changed(self, _obj: GObject.Object, added: bool) -> None:
# TODO: I could do less/more strict with adding/removing tags separately
self.item_filter.changed(
Gtk.FilterChange.MORE_STRICT if added else Gtk.FilterChange.LESS_STRICT
)

def __toggle_hidden(self, *_args: Any) -> None:
if shared.show_hidden:
Expand Down
23 changes: 23 additions & 0 deletions hyperplane/postmaster_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,36 @@ def zoom(self, zoom_level: int) -> None:
def toggle_hidden(self) -> None:
"""Emitted when the visibility of hidden files changes."""

@GObject.Signal(name="tags-added")
def tags_added(self) -> None:
"""
Emitted whenever items have been removed from the list of tags.
You can connect to this instead of `tags-changed` for performance
reasons in an object like a filter or if you don't care about removed items.
"""
self.emit("tags-changed")

@GObject.Signal(name="tags-removed")
def tags_removed(self) -> None:
"""
Emitted whenever items have been removed from the list of tags.
You can connect to this instead of `tags-changed` for performance
reasons in an object like a filter or if you don't care about added items.
"""
self.emit("tags-changed")

@GObject.Signal(name="tags-changed")
def tags_changed(self) -> None:
"""
Emitted whenever the list of tags changes.
All objects that keep an internal list of tags should connect to it
and update their list accordingly.
You can connect to `tags-removed` and `tags-added` if you need more specificity
like when updating a filter.
"""

@GObject.Signal(name="tag-location-created")
Expand Down
3 changes: 2 additions & 1 deletion hyperplane/utils/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def add_tags(*tags: str) -> None:
for tag in tags:
shared.tags.append(tag)
update_tags()
shared.postmaster.emit("tags-added")


def remove_tags(*tags: str) -> None:
Expand All @@ -54,12 +55,12 @@ def remove_tags(*tags: str) -> None:
if tag in shared.tags:
shared.tags.remove(tag)
update_tags()
shared.postmaster.emit("tags-removed")


def update_tags() -> None:
"""Updates the list of tags."""
(shared.home / ".hyperplane").write_text("\n".join(shared.tags), encoding="utf-8")
shared.postmaster.emit("tags-changed")


def move_tag(tag: str, up: bool) -> None:
Expand Down

0 comments on commit 9796930

Please sign in to comment.