Skip to content

Commit

Permalink
Use a timer for live search
Browse files Browse the repository at this point in the history
  • Loading branch information
diegogangl committed Mar 16, 2021
1 parent 3454d28 commit 510a7a4
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions GTG/gtk/browser/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 510a7a4

Please sign in to comment.