From df7f8689af43ec68c392aa3142b303e1e7a3dcbc Mon Sep 17 00:00:00 2001 From: Heliguy Date: Sat, 28 Sep 2024 14:29:27 -0400 Subject: [PATCH] Add ability to search --- src/snapshot_page/snapshot_page.blp | 17 ++++++++-------- src/snapshot_page/snapshot_page.py | 31 +++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/src/snapshot_page/snapshot_page.blp b/src/snapshot_page/snapshot_page.blp index 7910808..92b2ee1 100644 --- a/src/snapshot_page/snapshot_page.blp +++ b/src/snapshot_page/snapshot_page.blp @@ -50,16 +50,15 @@ template $SnapshotPage : Adw.BreakpointBin { tooltip-text: _("Select Packages"); } } + [top] + SearchBar search_bar { + search-mode-enabled: bind search_button.active bidirectional; + SearchEntry search_entry { + hexpand: true; + placeholder-text: _("Search Snapshots"); + } + } Stack stack { - // Adw.StatusPage loading_snapshots { - // title: _("Loading Snapshot"); - // description: _("This should only take a moment"); - // child: - // Spinner { - // spinning: true; - // } - // ; - // } Adw.StatusPage no_results { title: _("No Results Found"); description: _("Try a different search"); diff --git a/src/snapshot_page/snapshot_page.py b/src/snapshot_page/snapshot_page.py index 7465e1e..dae2047 100644 --- a/src/snapshot_page/snapshot_page.py +++ b/src/snapshot_page/snapshot_page.py @@ -34,6 +34,8 @@ class SnapshotPage(Adw.BreakpointBin): gtc = Gtk.Template.Child toast_overlay = gtc() + search_entry = gtc() + search_bar = gtc() active_box = gtc() active_listbox = gtc() leftover_box = gtc() @@ -194,6 +196,33 @@ class SnapshotPage(Adw.BreakpointBin): self.start_loading() self.end_loading() + def on_search(self, search_entry): + text = search_entry.get_text().lower() + i = 0 + total_active_visible = 0 + while row := self.active_listbox.get_row_at_index(i): + i += 1 + row.set_visible(False) + if text in row.get_title().lower(): + row.set_visible(True) + total_active_visible += 1 + self.active_box.set_visible(total_active_visible > 0) + + i = 0 + total_leftover_visible = 0 + while row := self.leftover_listbox.get_row_at_index(i): + i += 1 + row.set_visible(False) + if text in row.get_title().lower(): + row.set_visible(True) + total_leftover_visible += 1 + self.leftover_box.set_visible(total_leftover_visible > 0) + + if total_active_visible > 0 or total_leftover_visible > 0: + self.stack.set_visible_child(self.scrolled_window) + else: + self.stack.set_visible_child(self.no_results) + def __init__(self, main_window, **kwargs): super().__init__(**kwargs) @@ -214,8 +243,10 @@ class SnapshotPage(Adw.BreakpointBin): self.status_open_button.connect("clicked", self.open_snapshots_folder) self.status_new_button.connect("clicked", self.on_new) self.new_button.connect("clicked", self.on_new) + self.search_entry.connect("search-changed", self.on_search) # Apply + self.search_bar.set_key_capture_widget(HostInfo.main_window) self.loading_view.set_content(LoadingStatus(_("Loading Snapshots"), _("This should only take a moment"))) self.snapshotting_view.set_content(self.snapshotting_status) self.snapshotting_status.button.set_label(_("Cancel"))