Merge pull request #1367 from auouymous/idle-timeout

Add util.IdleTimeout class to reduce lag when updating progress tab.
This commit is contained in:
auouymous 2022-08-26 00:07:09 -07:00 committed by GitHub
commit 839c85a43e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 1 deletions

View File

@ -1124,7 +1124,7 @@ class gPodder(BuilderWidget, dbus.service.Object):
self.things_adding_tasks -= 1
if not self.download_list_update_enabled:
self.update_downloads_list()
GLib.timeout_add(1500, self.update_downloads_list)
util.IdleTimeout(1500, self.update_downloads_list)
self.download_list_update_enabled = True
def cleanup_downloads(self):

View File

@ -1324,6 +1324,34 @@ def idle_add(func, *args):
func(*args)
class IdleTimeout(object):
"""Run a function in the main GUI thread at regular intervals since the last run
A simple timeout_add() continuously calls the function if it exceeds the interval,
which lags the UI and prevents idle_add() calls from happening. This class restarts
the timer after the function finishes, allowing other callbacks to run.
"""
def __init__(self, milliseconds, func, *args):
if not gpodder.ui.gtk:
raise Exception('util.IdleTimeout() is only supported by Gtk+')
self.milliseconds = milliseconds
self.func = func
from gi.repository import GLib
self.id = GLib.timeout_add(milliseconds, self._callback, *args)
def _callback(self, *args):
self.cancel()
if self.func(args):
from gi.repository import GLib
self.id = GLib.timeout_add(self.milliseconds, self._callback, *args)
def cancel(self):
if self.id:
from gi.repository import GLib
GLib.source_remove(self.id)
self.id = 0
def bluetooth_available():
"""
Returns True or False depending on the availability