Skip to content

Commit

Permalink
Add the ability to clear the history of recents
Browse files Browse the repository at this point in the history
  • Loading branch information
kra-mo committed Dec 21, 2023
1 parent a152de8 commit 406ce3b
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 4 deletions.
5 changes: 5 additions & 0 deletions hyperplane/gtk/items-page.blp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ Adw.StatusPage empty_trash {
icon-name: "user-trash-symbolic";
}

Adw.StatusPage no_recents {
title: _("No Recent Files");
icon-name: "document-open-recent-symbolic";
}

Adw.StatusPage no_results {
title: _("No Results Found");
icon-name: "system-search-symbolic";
Expand Down
12 changes: 12 additions & 0 deletions hyperplane/gtk/window.blp
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,11 @@ menu right_click {
action: "win.empty-trash";
hidden-when: "action-disabled";
}
item {
label: _("Clear History");
action: "win.clear-recents";
hidden-when: "action-disabled";
}
}
section {
item (_("Properties"), "win.properties")
Expand Down Expand Up @@ -458,6 +463,13 @@ menu file_right_click {
hidden-when: "action-disabled";
}
}
section {
item {
label: _("Clear History");
action: "win.clear-recents";
hidden-when: "action-disabled";
}
}
section {
item (_("Properties"), "win.properties-sidebar")
}
Expand Down
16 changes: 14 additions & 2 deletions hyperplane/items_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class HypItemsPage(Adw.NavigationPage):
empty_folder: Adw.StatusPage = Gtk.Template.Child()
no_matching_items: Adw.StatusPage = Gtk.Template.Child()
empty_trash: Adw.StatusPage = Gtk.Template.Child()
no_recents: Adw.StatusPage = Gtk.Template.Child()
no_results: Adw.StatusPage = Gtk.Template.Child()
loading: Gtk.Viewport = Gtk.Template.Child()

Expand Down Expand Up @@ -105,7 +106,7 @@ def __init__(
)
self.grid_view.add_controller(texture_drop_target)

text_drop_target = Gtk.DropTarget.new(GObject.TYPE_STRING, Gdk.DragAction.COPY)
text_drop_target = Gtk.DropTarget.new(str, Gdk.DragAction.COPY)
text_drop_target.connect("drop", self.__drop_text)
self.grid_view.add_controller(text_drop_target)

Expand Down Expand Up @@ -328,6 +329,10 @@ def __items_changed(
self.scrolled_window.set_child(self.empty_trash)
return

elif self.gfile.get_uri() == "recent:///":
self.scrolled_window.set_child(self.no_recents)
return

self.scrolled_window.set_child(self.empty_folder)

if self.tags:
Expand Down Expand Up @@ -403,12 +408,19 @@ def __popup_menu(self) -> None:
"select-all",
"open-with",
}

if self.gfile.get_uri() == "trash:///":
if shared.trash_list.get_n_items():
items.add("empty-trash")
items.remove("paste")
items.remove("new-folder")

if self.gfile.get_uri() == "recent:///":
if bool(shared.recent_manager.get_items()):
items.add("clear-recents")
items.remove("paste")
items.remove("new-folder")

self.get_root().set_menu_items(items)

self.get_root().right_click_menu.popup()
Expand Down Expand Up @@ -497,7 +509,7 @@ def __drop_text(
self, _drop_target: Gtk.DropTarget, text: GObject.Value, _x, _y
) -> None:
# TODO: Agian again, copy-paste from __paste()
if not text: # If text is an empty string
if not text: # If text is an empty string
return

if self.tags:
Expand Down
20 changes: 19 additions & 1 deletion hyperplane/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from gi.repository import Adw, Gio, GLib, Gtk, Pango

from hyperplane import shared
from hyperplane.utils.files import empty_trash, get_gfile_path
from hyperplane.utils.files import clear_recent_files, empty_trash, get_gfile_path
from hyperplane.utils.get_color_for_content_type import get_color_for_content_type


Expand Down Expand Up @@ -286,6 +286,24 @@ def empty(*_args: Any) -> None:
recent_items_row.add_css_class("property")
recent_items_row.set_subtitle_selectable(True)

recent_group.add(
clear_recents_button := Gtk.Button(
margin_top=24,
label=_("Clear History"),
halign="center",
)
)

def clear(*_args: Any) -> None:
clear_recent_files()
self.close()

clear_recents_button.add_css_class("pill")
clear_recents_button.connect("clicked", clear)
clear_recents_button.set_sensitive(
bool(shared.recent_manager.get_items())
)

if access or modified or created:
page.add(history_group := Adw.PreferencesGroup())

Expand Down
5 changes: 5 additions & 0 deletions hyperplane/utils/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,11 @@ def empty_trash() -> None:
shared.undo_queue.pop(key)


def clear_recent_files() -> None:
"""Clears the list of recently used files."""
shared.recent_manager.purge_items()


def trash_rm(gfile: Gio.File) -> None:
"""
Tries to asynchronously remove a file or directory that is in the trash.
Expand Down
16 changes: 15 additions & 1 deletion hyperplane/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@
from hyperplane.navigation_bin import HypNavigationBin
from hyperplane.properties import HypPropertiesWindow
from hyperplane.tag_row import HypTagRow
from hyperplane.utils.files import empty_trash, get_gfile_path, validate_name
from hyperplane.utils.files import (
clear_recent_files,
empty_trash,
get_gfile_path,
validate_name,
)
from hyperplane.utils.tags import add_tags, move_tag, remove_tags


Expand Down Expand Up @@ -162,6 +167,7 @@ def __init__(self, **kwargs: Any) -> None:
self.create_action("new-tab", self.__new_tab, ("<primary>t",))
self.create_action("tab-overview", self.__tab_overview, ("<primary><shift>o",))
self.create_action("empty-trash", self.__emptry_trash)
self.create_action("clear-recents", self.__clear_recents)

# Connect signals

Expand Down Expand Up @@ -338,6 +344,7 @@ def set_menu_items(self, menu_items: Iterable[str]) -> None:
"trash-delete": page,
"trash-restore": page,
"empty-trash": self,
"clear-recents": self,
"new-folder": page,
"select-all": page,
"open": page,
Expand Down Expand Up @@ -712,6 +719,7 @@ def __set_actions(self, *_args: Any) -> None:
"trash-delete",
"trash-restore",
"empty-trash",
"clear-recents",
"new-folder",
"select-all",
"open",
Expand Down Expand Up @@ -811,6 +819,9 @@ def __sidebar_right_click(
self.lookup_action("empty-trash").set_enabled(
gfile.get_uri() == "trash:///" and shared.trash_list.get_n_items()
)
self.lookup_action("clear-recents").set_enabled(
gfile.get_uri() == "recent:///" and bool(shared.recent_manager.get_items())
)

self.file_right_click_menu.unparent()
self.file_right_click_menu.set_parent(gesture.get_widget())
Expand Down Expand Up @@ -843,3 +854,6 @@ def handle_response(_dialog: Adw.MessageDialog, response: str) -> None:

dialog.connect("response", handle_response)
dialog.present()

def __clear_recents(self, *_args: Any) -> None:
clear_recent_files()

0 comments on commit 406ce3b

Please sign in to comment.