[gnome-calendar] search-model: Wait for results before returning



commit a0403b0b3e1fd8eff6fb7b9b1956d80adae39eac
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Fri Oct 4 01:55:08 2019 -0300

    search-model: Wait for results before returning
    
    Dazzle is very sensitive to returning empty search results,
    and ends up flickering when we pass a search model that is
    not yet populated.
    
    Wait for a few results, or for 150ms, before returning the
    model.
    
    https://gitlab.gnome.org/GNOME/gnome-calendar/issues/468

 src/search/gcal-search-engine.c |  7 ++++++-
 src/search/gcal-search-model.c  | 31 +++++++++++++++++++++++++++++++
 src/search/gcal-search-model.h  |  3 +++
 3 files changed, 40 insertions(+), 1 deletion(-)
---
diff --git a/src/search/gcal-search-engine.c b/src/search/gcal-search-engine.c
index 7cf44699..8ad8cec7 100644
--- a/src/search/gcal-search-engine.c
+++ b/src/search/gcal-search-engine.c
@@ -107,7 +107,12 @@ search_func (GTask        *task,
                               start,
                               end);
 
-  g_task_return_pointer (task, g_steal_pointer (&model), g_object_unref);
+  gcal_search_model_wait_for_hits (model, cancellable);
+
+  if (g_cancellable_is_cancelled (cancellable))
+    g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_CANCELLED, "Cancelled");
+  else
+    g_task_return_pointer (task, g_steal_pointer (&model), g_object_unref);
 }
 
 static void
diff --git a/src/search/gcal-search-model.c b/src/search/gcal-search-model.c
index b8442068..e31102f5 100644
--- a/src/search/gcal-search-model.c
+++ b/src/search/gcal-search-model.c
@@ -31,6 +31,9 @@
 
 #include <dazzle.h>
 
+#define MIN_RESULTS         5
+#define WAIT_FOR_RESULTS_MS 0.150
+
 struct _GcalSearchModel
 {
   GObject             parent;
@@ -245,3 +248,31 @@ gcal_search_model_new (GCancellable *cancellable,
 
   return model;
 }
+
+void
+gcal_search_model_wait_for_hits (GcalSearchModel *self,
+                                 GCancellable    *cancellable)
+{
+  g_autoptr (GMainContext) thread_context = NULL;
+  g_autoptr (GTimer) timer = NULL;
+
+  GCAL_ENTRY;
+
+  g_return_if_fail (GCAL_IS_SEARCH_MODEL (self));
+
+  thread_context = g_main_context_ref_thread_default ();
+  timer = g_timer_new ();
+
+  g_timer_start (timer);
+
+  while (g_list_model_get_n_items (self->model) < MIN_RESULTS &&
+         g_timer_elapsed (timer, NULL) < WAIT_FOR_RESULTS_MS)
+    {
+      if (g_cancellable_is_cancelled (cancellable))
+        break;
+
+      g_main_context_iteration (thread_context, FALSE);
+    }
+
+  GCAL_EXIT;
+}
diff --git a/src/search/gcal-search-model.h b/src/search/gcal-search-model.h
index 9b7c1fc8..3a0f7eb8 100644
--- a/src/search/gcal-search-model.h
+++ b/src/search/gcal-search-model.h
@@ -30,4 +30,7 @@ G_DECLARE_FINAL_TYPE (GcalSearchModel, gcal_search_model, GCAL, SEARCH_MODEL, GO
 GcalSearchModel*     gcal_search_model_new                       (GCancellable       *cancellable,
                                                                   gint                max_results);
 
+void                 gcal_search_model_wait_for_hits             (GcalSearchModel    *self,
+                                                                  GCancellable       *cancellable);
+
 G_END_DECLS


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