diff --git a/app/commons.py b/app/commons.py index aeba2b84..89e93833 100644 --- a/app/commons.py +++ b/app/commons.py @@ -1,6 +1,6 @@ import logging from functools import wraps -from threading import Thread +from threading import Thread, Timer from gi.repository import GLib @@ -43,5 +43,31 @@ def run_task(func): return wrapper +def run_with_delay(timeout=5): + """ Starts the function with a delay. + + If the previous timer still works, it will canceled! + """ + + def run_with(func): + timer = None + + @wraps(func) + def wrapper(*args, **kwargs): + nonlocal timer + if timer and timer.is_alive(): + timer.cancel() + + def run(): + GLib.idle_add(func, *args, **kwargs, priority=GLib.PRIORITY_LOW) + + timer = Timer(interval=timeout, function=run) + timer.start() + + return wrapper + + return run_with + + if __name__ == "__main__": pass diff --git a/app/ui/main_app_window.py b/app/ui/main_app_window.py index c586aa40..b0e86559 100644 --- a/app/ui/main_app_window.py +++ b/app/ui/main_app_window.py @@ -6,7 +6,7 @@ from functools import lru_cache from gi.repository import GLib -from app.commons import run_idle, log, run_task +from app.commons import run_idle, log, run_task, run_with_delay from app.eparser import get_blacklist, write_blacklist, parse_m3u from app.eparser import get_services, get_bouquets, write_bouquets, write_services, Bouquets, Bouquet, Service from app.eparser.ecommons import CAS, Flag @@ -1053,7 +1053,7 @@ class MainAppWindow: self._filter_bar.set_search_mode(active) self._filter_bar.set_visible(active) - @run_idle + @run_with_delay(1) def on_filter_changed(self, entry): self._services_model_filter.refilter() @@ -1072,7 +1072,7 @@ class MainAppWindow: def on_search_up(self, item): self._search_provider.on_search_up() - @run_idle + @run_with_delay(1) def on_search(self, entry): self._search_provider.search(entry.get_text())