rind: Check notifications from the main thread
Gtk really doesn't like notifications coming from other threads, so we need to handle notifications from the main thread through an idle task. Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
parent
e38e6ae1b4
commit
332cd89fee
18
rind/gtk.py
18
rind/gtk.py
|
@ -1,7 +1,8 @@
|
|||
# Copyright 2019 (c) Anna Schumaker.
|
||||
import curds
|
||||
import gi
|
||||
gi.require_version("Gtk", "3.0")
|
||||
from gi.repository import Gtk
|
||||
from gi.repository import Gtk, GLib
|
||||
|
||||
Builder = Gtk.Builder()
|
||||
Builder.add_from_file("emmental.ui")
|
||||
|
@ -9,7 +10,8 @@ Builder.add_from_file("emmental.ui")
|
|||
class EmmentalApplication(Gtk.Application):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, application_id="org.gtk.emmental", **kwargs)
|
||||
self.window = None
|
||||
self.window = None
|
||||
self.idle_id = None
|
||||
|
||||
def do_activate(self):
|
||||
if self.window == None:
|
||||
|
@ -17,4 +19,16 @@ class EmmentalApplication(Gtk.Application):
|
|||
self.add_window(self.window)
|
||||
self.window.present()
|
||||
|
||||
def do_shutdown(self):
|
||||
Gtk.Application.do_shutdown(self)
|
||||
GLib.source_remove(self.idle_id)
|
||||
|
||||
def do_startup(self):
|
||||
Gtk.Application.do_startup(self)
|
||||
self.idle_id = GLib.idle_add(self.on_idle)
|
||||
|
||||
def on_idle(self):
|
||||
curds.Notify.run_queued()
|
||||
return GLib.SOURCE_CONTINUE
|
||||
|
||||
Application = EmmentalApplication()
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
from . import gtk
|
||||
from . import playlist
|
||||
import curds
|
||||
import threading
|
||||
from gi.repository import GObject, Gtk, GLib
|
||||
|
||||
toplevel = [ "Collection", "Previous", None, "Library" ]
|
||||
|
@ -10,11 +9,8 @@ toplevel = [ "Collection", "Previous", None, "Library" ]
|
|||
class ManagerModel(GObject.GObject, Gtk.TreeModel):
|
||||
def __init__(self, *args, **kwargs):
|
||||
GObject.GObject.__init__(self)
|
||||
curds.Notify.notify_me("add-track", self.on_add_track)
|
||||
curds.Notify.notify_me("new-library-playlist", self.on_new_library)
|
||||
|
||||
self.update_lock = threading.Lock()
|
||||
self.update_queue = [ ]
|
||||
curds.Notify.notify_me("add-track", self.on_add_track, queue=True)
|
||||
curds.Notify.notify_me("new-library-playlist", self.on_new_library, queue=True)
|
||||
|
||||
def can_select_path(self, selection, model, path, current):
|
||||
plist = self.iter_playlist(self.get_iter(path))
|
||||
|
@ -105,17 +101,9 @@ class ManagerModel(GObject.GObject, Gtk.TreeModel):
|
|||
return None
|
||||
|
||||
def on_add_track(self, plist, track):
|
||||
with self.update_lock:
|
||||
if not plist in self.update_queue:
|
||||
self.update_queue.append(plist)
|
||||
GLib.idle_add(self.on_idle, priority=GLib.PRIORITY_DEFAULT)
|
||||
|
||||
def on_idle(self, *args):
|
||||
with self.update_lock:
|
||||
plist = self.update_queue.pop(0)
|
||||
iter = self.find_playlist(plist)
|
||||
iter = self.find_playlist(plist)
|
||||
if iter:
|
||||
self.row_changed(self.get_path(iter), iter)
|
||||
return GLib.SOURCE_REMOVE
|
||||
|
||||
def on_new_library(self, plist):
|
||||
iter = self.find_playlist(plist)
|
||||
|
|
|
@ -10,7 +10,7 @@ class PlaylistModel(GObject.GObject, Gtk.TreeModel):
|
|||
def __init__(self, *args, **kwargs):
|
||||
GObject.GObject.__init__(self)
|
||||
self.playlist = curds.PlaylistManager["Collection"]
|
||||
curds.Notify.notify_me("add-track", self.on_add_track)
|
||||
curds.Notify.notify_me("add-track", self.on_add_track, queue=True)
|
||||
RandomButton.connect("toggled", self.on_random_toggled)
|
||||
|
||||
def do_get_column_type(self, col):
|
||||
|
|
|
@ -20,6 +20,7 @@ class TestGtk(unittest.TestCase):
|
|||
while gtk.Application.window == None:
|
||||
time.sleep(0.1)
|
||||
self.assertTrue(window.is_visible())
|
||||
self.assertGreater(gtk.Application.idle_id, 0)
|
||||
gtk.Application.quit()
|
||||
thread.stop()
|
||||
|
||||
|
|
Loading…
Reference in New Issue