Fix the progressbar spinner to actually work

We need to use the private GMainContext we created.
This commit is contained in:
Richard Hughes 2021-07-13 10:57:06 +01:00
parent 9a4997e42b
commit a7409d3117
3 changed files with 36 additions and 11 deletions

View File

@ -19,6 +19,7 @@ static void fu_progressbar_finalize (GObject *obj);
struct _FuProgressbar struct _FuProgressbar
{ {
GObject parent_instance; GObject parent_instance;
GMainContext *main_ctx;
FwupdStatus status; FwupdStatus status;
gboolean spinner_count_up; /* chars */ gboolean spinner_count_up; /* chars */
guint spinner_idx; /* chars */ guint spinner_idx; /* chars */
@ -26,7 +27,7 @@ struct _FuProgressbar
guint length_status; /* chars */ guint length_status; /* chars */
guint percentage; guint percentage;
guint to_erase; /* chars */ guint to_erase; /* chars */
guint timer_id; GSource *timer_source;
gint64 last_animated; /* monotonic */ gint64 last_animated; /* monotonic */
GTimer *time_elapsed; GTimer *time_elapsed;
gdouble last_estimate; gdouble last_estimate;
@ -223,6 +224,21 @@ fu_progressbar_set_title (FuProgressbar *self, const gchar *title)
fu_progressbar_refresh (self, self->status, self->percentage); fu_progressbar_refresh (self, self->status, self->percentage);
} }
/**
* fu_progressbar_set_main_context:
* @self: A #FuProgressbar
* @main_ctx: (nullable): main context
*
* Sets progressbar main context to use for animations.
*
* Since: 1.6.2
**/
void
fu_progressbar_set_main_context (FuProgressbar *self, GMainContext *main_ctx)
{
self->main_ctx = g_main_context_ref (main_ctx);
}
static void static void
fu_progressbar_spin_inc (FuProgressbar *self) fu_progressbar_spin_inc (FuProgressbar *self)
{ {
@ -261,9 +277,9 @@ fu_progressbar_spin_cb (gpointer user_data)
static void static void
fu_progressbar_spin_end (FuProgressbar *self) fu_progressbar_spin_end (FuProgressbar *self)
{ {
if (self->timer_id != 0) { if (self->timer_source != NULL) {
g_source_remove (self->timer_id); g_source_destroy (self->timer_source);
self->timer_id = 0; self->timer_source = NULL;
/* reset when the spinner has been stopped */ /* reset when the spinner has been stopped */
g_timer_start (self->time_elapsed); g_timer_start (self->time_elapsed);
@ -277,9 +293,13 @@ fu_progressbar_spin_end (FuProgressbar *self)
static void static void
fu_progressbar_spin_start (FuProgressbar *self) fu_progressbar_spin_start (FuProgressbar *self)
{ {
if (self->timer_id != 0) if (self->timer_source != NULL)
g_source_remove (self->timer_id); g_source_destroy (self->timer_source);
self->timer_id = g_timeout_add (40, fu_progressbar_spin_cb, self); self->timer_source = g_timeout_source_new (40);
g_source_set_callback (self->timer_source,
fu_progressbar_spin_cb,
self, NULL);
g_source_attach (self->timer_source, self->main_ctx);
} }
/** /**
@ -419,8 +439,10 @@ fu_progressbar_finalize (GObject *obj)
{ {
FuProgressbar *self = FU_PROGRESSBAR (obj); FuProgressbar *self = FU_PROGRESSBAR (obj);
if (self->timer_id != 0) if (self->timer_source != 0)
g_source_remove (self->timer_id); g_source_destroy (self->timer_source);
if (self->main_ctx != NULL)
g_main_context_unref (self->main_ctx);
g_timer_destroy (self->time_elapsed); g_timer_destroy (self->time_elapsed);
G_OBJECT_CLASS (fu_progressbar_parent_class)->finalize (obj); G_OBJECT_CLASS (fu_progressbar_parent_class)->finalize (obj);

View File

@ -23,5 +23,7 @@ void fu_progressbar_set_length_percentage (FuProgressbar *self,
guint len); guint len);
void fu_progressbar_set_title (FuProgressbar *self, void fu_progressbar_set_title (FuProgressbar *self,
const gchar *title); const gchar *title);
void fu_progressbar_set_interactive (FuProgressbar *self, void fu_progressbar_set_interactive (FuProgressbar *self,
gboolean interactive); gboolean interactive);
void fu_progressbar_set_main_context (FuProgressbar *self,
GMainContext *main_ctx);

View File

@ -2852,6 +2852,7 @@ main (int argc, char *argv[])
/* create helper object */ /* create helper object */
priv->main_ctx = g_main_context_new (); priv->main_ctx = g_main_context_new ();
priv->progressbar = fu_progressbar_new (); priv->progressbar = fu_progressbar_new ();
fu_progressbar_set_main_context (priv->progressbar, priv->main_ctx);
/* add commands */ /* add commands */
fu_util_cmd_array_add (cmd_array, fu_util_cmd_array_add (cmd_array,