[gedit/wip/tab-refactoring] tab: improve timer handling for showing progress info bar



commit 834e936695f2252036ee2bf764dd1c4c8ed1ba5a
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Fri Apr 3 19:30:23 2015 +0200

    tab: improve timer handling for showing progress info bar

 gedit/gedit-tab.c |  140 ++++++++++++++++++++++++++++++----------------------
 1 files changed, 81 insertions(+), 59 deletions(-)
---
diff --git a/gedit/gedit-tab.c b/gedit/gedit-tab.c
index 5c1e207..e8904bb 100644
--- a/gedit/gedit-tab.c
+++ b/gedit/gedit-tab.c
@@ -910,33 +910,85 @@ info_bar_set_progress (GeditTab *tab,
                       goffset   size,
                       goffset   total_size)
 {
+       GeditProgressInfoBar *progress_info_bar;
+
        if (tab->priv->info_bar == NULL)
+       {
                return;
+       }
 
        gedit_debug_message (DEBUG_TAB, "%" G_GOFFSET_FORMAT "/%" G_GOFFSET_FORMAT, size, total_size);
 
        g_return_if_fail (GEDIT_IS_PROGRESS_INFO_BAR (tab->priv->info_bar));
 
-       if (total_size == 0)
+       progress_info_bar = GEDIT_PROGRESS_INFO_BAR (tab->priv->info_bar);
+
+       if (total_size != 0)
        {
-               if (size != 0)
-                       gedit_progress_info_bar_pulse (
-                                       GEDIT_PROGRESS_INFO_BAR (tab->priv->info_bar));
-               else
-                       gedit_progress_info_bar_set_fraction (
-                               GEDIT_PROGRESS_INFO_BAR (tab->priv->info_bar),
-                               0);
+               gdouble frac = (gdouble)size / (gdouble)total_size;
+
+               gedit_progress_info_bar_set_fraction (progress_info_bar, frac);
+       }
+       else if (size != 0)
+       {
+               gedit_progress_info_bar_pulse (progress_info_bar);
        }
        else
        {
-               gdouble frac;
+               gedit_progress_info_bar_set_fraction (progress_info_bar, 0);
+       }
+}
+
+static void
+handle_progress_info (GeditTab *tab,
+                     goffset   size,
+                     goffset   total_size)
+{
+       g_return_if_fail (tab->priv->state == GEDIT_TAB_STATE_SAVING ||
+                         tab->priv->state == GEDIT_TAB_STATE_LOADING ||
+                         tab->priv->state == GEDIT_TAB_STATE_REVERTING);
+
+       if (tab->priv->timer != NULL)
+       {
+               gdouble elapsed_time;
+               gdouble total_time;
+               gdouble remaining_time;
 
-               frac = (gdouble)size / (gdouble)total_size;
+               elapsed_time = g_timer_elapsed (tab->priv->timer, NULL);
 
-               gedit_progress_info_bar_set_fraction (
-                               GEDIT_PROGRESS_INFO_BAR (tab->priv->info_bar),
-                               frac);
+               /* elapsed_time / total_time = size / total_size */
+               total_time = (elapsed_time * total_size) / size;
+
+               remaining_time = total_time - elapsed_time;
+
+               /* Approximately more than 3 seconds remaining. */
+               if (remaining_time > 3.0)
+               {
+                       switch (tab->priv->state)
+                       {
+                               case GEDIT_TAB_STATE_SAVING:
+                                       show_saving_info_bar (tab);
+                                       break;
+
+                               case GEDIT_TAB_STATE_LOADING:
+                               case GEDIT_TAB_STATE_REVERTING:
+                                       show_loading_info_bar (tab);
+                                       break;
+
+                               default:
+                                       g_assert_not_reached ();
+                       }
+
+                       /* Once the progress info bar is shown, it must remain
+                        * shown until the end, so we don't need the timer
+                        * anymore.
+                        */
+                       g_timer_destroy (tab->priv->timer);
+                       tab->priv->timer = NULL;
+               }
        }
+
+       info_bar_set_progress (tab, size, total_size);
 }
 
 static gboolean
@@ -1636,32 +1688,10 @@ loader_progress_cb (goffset   size,
                    goffset   total_size,
                    GeditTab *tab)
 {
-       gdouble elapsed_time;
-       gdouble total_time;
-       gdouble remaining_time;
-
        g_return_if_fail (tab->priv->state == GEDIT_TAB_STATE_LOADING ||
                          tab->priv->state == GEDIT_TAB_STATE_REVERTING);
 
-       if (tab->priv->timer == NULL)
-       {
-               tab->priv->timer = g_timer_new ();
-       }
-
-       elapsed_time = g_timer_elapsed (tab->priv->timer, NULL);
-
-       /* elapsed_time / total_time = size / total_size */
-       total_time = (elapsed_time * total_size) / size;
-
-       remaining_time = total_time - elapsed_time;
-
-       /* Approximately more than 3 seconds remaining. */
-       if (remaining_time > 3.0)
-       {
-               show_loading_info_bar (tab);
-       }
-
-       info_bar_set_progress (tab, size, total_size);
+       handle_progress_info (tab, size, total_size);
 }
 
 static void
@@ -2018,6 +2048,13 @@ load (GeditTab                *tab,
        doc = gedit_tab_get_document (tab);
        g_signal_emit_by_name (doc, "load");
 
+       if (tab->priv->timer != NULL)
+       {
+               g_timer_destroy (tab->priv->timer);
+       }
+
+       tab->priv->timer = g_timer_new ();
+
        /* Keep the tab alive during the async operation. */
        g_object_ref (tab);
 
@@ -2158,31 +2195,9 @@ saver_progress_cb (goffset   size,
                   goffset   total_size,
                   GeditTab *tab)
 {
-       gdouble elapsed_time;
-       gdouble total_time;
-       gdouble remaining_time;
-
        g_return_if_fail (tab->priv->state == GEDIT_TAB_STATE_SAVING);
 
-       if (tab->priv->timer == NULL)
-       {
-               tab->priv->timer = g_timer_new ();
-       }
-
-       elapsed_time = g_timer_elapsed (tab->priv->timer, NULL);
-
-       /* elapsed_time / total_time = size / total_size */
-       total_time = (elapsed_time * total_size) / size;
-
-       remaining_time = total_time - elapsed_time;
-
-       /* Approximately more than 3 seconds remaining. */
-       if (remaining_time > 3.0)
-       {
-               show_saving_info_bar (tab);
-       }
-
-       info_bar_set_progress (tab, size, total_size);
+       handle_progress_info (tab, size, total_size);
 }
 
 static void
@@ -2326,6 +2341,13 @@ save (GeditTab *tab)
 
        data = g_task_get_task_data (tab->priv->task_saver);
 
+       if (tab->priv->timer != NULL)
+       {
+               g_timer_destroy (tab->priv->timer);
+       }
+
+       tab->priv->timer = g_timer_new ();
+
        gtk_source_file_saver_save_async (data->saver,
                                          G_PRIORITY_DEFAULT,
                                          g_task_get_cancellable (tab->priv->task_saver),


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