Skip to content

Commit

Permalink
Add match counter to text search entry
Browse files Browse the repository at this point in the history
  • Loading branch information
hugolabe committed Feb 3, 2024
1 parent 6a4fcbc commit 30664e8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
39 changes: 19 additions & 20 deletions data/gtk/page.ui
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,28 @@ SPDX-License-Identifier: GPL-3.0-or-later
</object>
</child>

<!-- Searchbar buttons box -->
<!-- Searchbar prev button -->
<child>
<object class="GtkBox">
<style>
<class name="linked"/>
</style>

<!-- Searchbar prev button -->
<child>
<object class="GtkButton" id="search_prev_button">
<property name="icon-name">go-up-symbolic</property>
<property name="sensitive">False</property>
</object>
</child>
<object class="GtkButton" id="search_prev_button">
<property name="icon-name">go-up-symbolic</property>
<property name="sensitive">False</property>
</object>
</child>

<!-- Searchbar next button -->
<child>
<object class="GtkButton" id="search_next_button">
<property name="icon-name">go-down-symbolic</property>
<property name="sensitive">False</property>
</object>
</child>
<!-- Searchbar next button -->
<child>
<object class="GtkButton" id="search_next_button">
<property name="icon-name">go-down-symbolic</property>
<property name="sensitive">False</property>
</object>
</child>

<!-- Matches label -->
<child>
<object class="GtkLabel" id="search_matches_label">
<property name="width-chars">4</property>
<property name="margin-start">9</property>
<property name="xalign">0</property>
</object>
</child>

Expand Down
13 changes: 12 additions & 1 deletion src/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class PageBox(Gtk.Box):
search_entry = Gtk.Template.Child()
search_prev_button = Gtk.Template.Child()
search_next_button = Gtk.Template.Child()
search_matches_label = Gtk.Template.Child()
view_stack = Gtk.Template.Child()
status_page = Gtk.Template.Child()
main_page_button = Gtk.Template.Child()
Expand Down Expand Up @@ -53,6 +54,7 @@ def __init__(self, window, lazy_load):
self.search_next_button.connect('clicked', self._search_next_button_cb, find_controller)
find_controller.connect('found-text', self._find_controller_found_cb)
find_controller.connect('failed-to-find-text', self._find_controller_not_found_cb)
find_controller.connect('counted-matches', self._find_controller_matches_cb)
nav_list.connect('changed', self._nav_list_changed_cb)

# Load a non-loaded page, for example when the user selects the tab
Expand Down Expand Up @@ -148,8 +150,10 @@ def _wikiview_add_bookmark_cb(self, wikiview, uri, title, lang):
def _search_entry_changed_cb(self, search_entry, find_controller):
text = search_entry.get_text()
if len(text) > 2:
find_controller.count_matches(text, WebKit.FindOptions.WRAP_AROUND | WebKit.FindOptions.CASE_INSENSITIVE, 9999)
find_controller.search(text, WebKit.FindOptions.WRAP_AROUND | WebKit.FindOptions.CASE_INSENSITIVE, 9999)
else:
self.search_matches_label.set_label('')
self.search_entry.remove_css_class('error')
self.search_prev_button.set_sensitive(False)
self.search_next_button.set_sensitive(False)
Expand All @@ -175,18 +179,25 @@ def _search_next_button_cb(self, search_next_button, find_controller):
# Found text in article

def _find_controller_found_cb(self, find_controller, match_count):
self.search_entry.remove_css_class('error')
self.search_prev_button.set_sensitive(True)
self.search_next_button.set_sensitive(True)

# Not found text in article

def _find_controller_not_found_cb(self, find_controller):
self.search_matches_label.set_label('')
self.search_entry.add_css_class('error')
self.search_prev_button.set_sensitive(False)
self.search_next_button.set_sensitive(False)
find_controller.search_finish()

# Show text search matches found

def _find_controller_matches_cb(self, find_controller, match_count):
if match_count > 0:
self.search_matches_label.set_label(str(match_count))
self.search_entry.remove_css_class('error')

# Refresh navbox state on navlist changed

def _nav_list_changed_cb(self, nav_list, item_added, item_removed):
Expand Down
2 changes: 1 addition & 1 deletion src/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ def _search_text_cb(self, action, parameter):
if not self.page.search_bar.get_search_mode():
self.page.search_bar.set_search_mode(True)
else:
self.page.textsearch_entry.grab_focus()
self.page.search_entry.grab_focus()

# Print current page

Expand Down

0 comments on commit 30664e8

Please sign in to comment.