[gtk/matthiasc/for-master: 40/40] Create the trash monitor async



commit a3ff337d4a0604c30995716eb9017b5cb8f541a0
Author: Matthias Clasen <mclasen redhat com>
Date:   Tue Feb 4 17:55:31 2020 +0100

    Create the trash monitor async
    
    This speeds up the initial setup of GtkPlacesSidebar.

 gtk/gtkplacessidebar.c | 31 +++++++++++++++++-----------
 gtk/gtktrashmonitor.c  | 56 ++++++++++++++++++++++++++++++++++++--------------
 gtk/gtktrashmonitor.h  |  6 +++++-
 3 files changed, 65 insertions(+), 28 deletions(-)
---
diff --git a/gtk/gtkplacessidebar.c b/gtk/gtkplacessidebar.c
index 0f2071b937..435ead46b8 100644
--- a/gtk/gtkplacessidebar.c
+++ b/gtk/gtkplacessidebar.c
@@ -68,6 +68,7 @@
 #include "gtkselectionprivate.h"
 #include "gtkstylecontext.h"
 #include "gtkvolumemonitor.h"
+#include "gdk/gdkprofilerprivate.h"
 
 /*< private >
  * SECTION:gtkplacessidebar
@@ -4016,15 +4017,23 @@ got_volume_monitor (GObject *source,
 }
 
 static void
-create_volume_monitor (GtkPlacesSidebar *sidebar)
+got_trash_monitor (GObject *source,
+                   GAsyncResult *result,
+                   gpointer data)
 {
-  GTask *task;
+  GtkPlacesSidebar *sidebar = GTK_PLACES_SIDEBAR (data);
+  GTask *task = G_TASK (result);
 
-  g_assert (sidebar->volume_monitor == NULL);
+  sidebar->trash_monitor = g_task_propagate_pointer (task, NULL);
+  if (!sidebar->trash_monitor)
+    return;
 
-  sidebar->init_cancellable = g_cancellable_new ();
-  
-  gtk_volume_monitor_get (got_volume_monitor, sidebar, sidebar->init_cancellable);
+  g_object_ref (sidebar->trash_monitor);
+
+  sidebar->trash_monitor_changed_id = g_signal_connect_swapped (sidebar->trash_monitor, 
"trash-state-changed",
+                                                                G_CALLBACK (update_trash_icon), sidebar);
+
+  update_trash_icon (sidebar);
 }
 
 static void
@@ -4070,15 +4079,13 @@ gtk_places_sidebar_init (GtkPlacesSidebar *sidebar)
   sidebar->show_recent = TRUE;
   sidebar->show_desktop = TRUE;
 
-  create_volume_monitor (sidebar);
-
   sidebar->open_flags = GTK_PLACES_OPEN_NORMAL;
 
-  sidebar->bookmarks_manager = _gtk_bookmarks_manager_new ((GtkBookmarksChangedFunc)update_places, sidebar);
+  sidebar->init_cancellable = g_cancellable_new ();
 
-  sidebar->trash_monitor = _gtk_trash_monitor_get ();
-  sidebar->trash_monitor_changed_id = g_signal_connect_swapped (sidebar->trash_monitor, 
"trash-state-changed",
-                                                                G_CALLBACK (update_trash_icon), sidebar);
+  gtk_volume_monitor_get (got_volume_monitor, sidebar, sidebar->init_cancellable);
+  gtk_trash_monitor_get (got_trash_monitor, sidebar, sidebar->init_cancellable);
+  sidebar->bookmarks_manager = _gtk_bookmarks_manager_new ((GtkBookmarksChangedFunc)update_places, sidebar);
 
   sidebar->swin = gtk_scrolled_window_new (NULL, NULL);
   gtk_widget_set_parent (sidebar->swin, GTK_WIDGET (sidebar));
diff --git a/gtk/gtktrashmonitor.c b/gtk/gtktrashmonitor.c
index d52006432e..63e81cea43 100644
--- a/gtk/gtktrashmonitor.c
+++ b/gtk/gtktrashmonitor.c
@@ -51,6 +51,7 @@ static guint signals[LAST_SIGNAL];
 G_DEFINE_TYPE (GtkTrashMonitor, _gtk_trash_monitor, G_TYPE_OBJECT)
 
 static GtkTrashMonitor *the_trash_monitor;
+static GList *pending_tasks;
 
 #define ICON_NAME_TRASH_EMPTY "user-trash-symbolic"
 #define ICON_NAME_TRASH_FULL  "user-trash-full-symbolic"
@@ -183,27 +184,52 @@ _gtk_trash_monitor_init (GtkTrashMonitor *monitor)
   recompute_trash_state (monitor);
 }
 
-/*
- * _gtk_trash_monitor_get:
- *
- * Returns: (transfer full): a new reference to the singleton
- * #GtkTrashMonitor object.  Be sure to call g_object_unref() on it when you are
- * done with the trash monitor.
- */
-GtkTrashMonitor *
-_gtk_trash_monitor_get (void)
+static void
+get_trash_monitor_thread (GTask        *running_task,
+                          gpointer      source_object,
+                          gpointer      task_data,
+                          GCancellable *cancellable)
 {
-  if (the_trash_monitor != NULL)
+  GList *l;
+
+  the_trash_monitor = g_object_new (GTK_TYPE_TRASH_MONITOR, NULL);
+  g_object_add_weak_pointer (G_OBJECT (the_trash_monitor), (gpointer *)&the_trash_monitor);
+
+  for (l = pending_tasks; l; l = l->next)
     {
-      g_object_ref (the_trash_monitor);
+      GTask *task = l->data;
+
+      if (!g_task_return_error_if_cancelled (task))
+        g_task_return_pointer (task, g_object_ref (the_trash_monitor), g_object_unref);
+    }
+
+  g_list_free_full (pending_tasks, g_object_unref);
+  pending_tasks = NULL;
+
+  g_object_unref (the_trash_monitor);
+}
+
+void
+gtk_trash_monitor_get (GAsyncReadyCallback  callback,
+                       gpointer             data,
+                       GCancellable        *cancellable)
+{
+  GTask *task;
+
+  task = g_task_new (NULL, cancellable, callback, data);
+  g_task_set_return_on_cancel (task, TRUE);
+
+  if (the_trash_monitor)
+    {
+      g_task_return_pointer (task, g_object_ref (the_trash_monitor), g_object_unref);
+      g_object_unref (task);
     }
   else
     {
-      the_trash_monitor = g_object_new (GTK_TYPE_TRASH_MONITOR, NULL);
-      g_object_add_weak_pointer (G_OBJECT (the_trash_monitor), (gpointer *) &the_trash_monitor);
+      pending_tasks = g_list_prepend (pending_tasks, task);
+      if (pending_tasks->next == NULL)
+        g_task_run_in_thread (task, get_trash_monitor_thread);
     }
-
-  return the_trash_monitor;
 }
 
 /*
diff --git a/gtk/gtktrashmonitor.h b/gtk/gtktrashmonitor.h
index 619d5c3304..2dc214383b 100644
--- a/gtk/gtktrashmonitor.h
+++ b/gtk/gtktrashmonitor.h
@@ -36,7 +36,11 @@ typedef struct _GtkTrashMonitor GtkTrashMonitor;
 typedef struct _GtkTrashMonitorClass GtkTrashMonitorClass;
 
 GType _gtk_trash_monitor_get_type (void);
-GtkTrashMonitor *_gtk_trash_monitor_get (void);
+
+void gtk_trash_monitor_get (GAsyncReadyCallback  callback,
+                            gpointer             data,
+                            GCancellable        *cancellable);
+
 
 GIcon *_gtk_trash_monitor_get_icon (GtkTrashMonitor *monitor);
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]