From 510a7a49bb127666c75e495ce2df48c96dbd185c Mon Sep 17 00:00:00 2001 From: Diego Garcia Gangl Date: Mon, 15 Mar 2021 20:38:04 -0300 Subject: [PATCH] Use a timer for live search --- GTG/gtk/browser/main_window.py | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/GTG/gtk/browser/main_window.py b/GTG/gtk/browser/main_window.py index ed8d42176a..30db664aa4 100644 --- a/GTG/gtk/browser/main_window.py +++ b/GTG/gtk/browser/main_window.py @@ -22,7 +22,7 @@ import datetime import logging -from gi.repository import GObject, Gtk, Gdk, Gio +from gi.repository import GObject, Gtk, Gdk, Gio, GLib from GTG.core import info from GTG.backends.backend_signals import BackendSignals @@ -70,6 +70,9 @@ def __init__(self, requester, app): self.config = self.req.get_config('browser') self.tag_active = False + # Timeout handler for search + self.search_timeout = None + # Treeviews handlers self.vtree_panes = {} self.tv_factory = TreeviewFactory(self.req, self.config) @@ -442,8 +445,26 @@ def _try_filter_by_query(self, query, refresh: bool = True): log.debug("Invalid query %r: %r", query, error) vtree.unapply_filter(SEARCH_TAG) - def on_search(self, data): + + def do_search(self): + """Perform the actual search and cancel the timeout.""" + self._try_filter_by_query(self.search_entry.get_text()) + GLib.source_remove(self.search_timeout) + self.search_timeout = None + + + def on_search(self, data): + """Callback everytime a character is inserted in the search field.""" + + TIMEOUT = 500 + + if self.search_timeout: + GLib.source_remove(self.search_timeout) + self.search_timeout = None + + self.search_timeout = GLib.timeout_add(TIMEOUT, self.do_search) + def on_save_search(self, action, param): query = self.search_entry.get_text()