From 3383f9e32aff1df68a2e2c58cb50dda089c42766 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Sat, 20 Aug 2016 09:01:29 -0400 Subject: [PATCH] gui/idle: Add a function for getting the progress bar And add in various cleanups while we're at it. Signed-off-by: Anna Schumaker --- gui/idle.c | 31 ++++++++++--------------------- include/gui/idle.h | 8 ++++++++ tests/gui/.gitignore | 2 +- tests/gui/CMakeLists.txt | 2 +- tests/gui/idle.c | 32 +++++++++++--------------------- 5 files changed, 31 insertions(+), 44 deletions(-) diff --git a/gui/idle.c b/gui/idle.c index 9af98a8a..e17e7cd1 100644 --- a/gui/idle.c +++ b/gui/idle.c @@ -1,38 +1,27 @@ /* * Copyright 2016 (c) Anna Schumaker. */ -#include -#include -#include +#include static guint idle_id = 0; static gboolean __on_idle(gpointer data) { - GtkProgressBar *progress = GTK_PROGRESS_BAR(data); - unsigned int percent = idle_progress() * 100; - gchar *text; + bool more = idle_run_task(); + gchar *text = g_strdup_printf("%f%%", idle_progress() * 100); - if (idle_run_task()) { - gtk_progress_bar_set_fraction(progress, idle_progress()); + gtk_widget_set_visible(GTK_WIDGET(gui_progress_bar()), more); + gtk_progress_bar_set_fraction(gui_progress_bar(), idle_progress()); + gtk_widget_set_tooltip_text(GTK_WIDGET(gui_progress_bar()), text); + g_free(text); - text = g_strdup_printf("%u%%", percent); - gtk_widget_set_tooltip_text(GTK_WIDGET(progress), text); - g_free(text); - return G_SOURCE_CONTINUE; - } else { - gtk_widget_hide(GTK_WIDGET(progress)); - idle_id = 0; - return G_SOURCE_REMOVE; - } + idle_id = more ? idle_id : 0; + return more ? G_SOURCE_CONTINUE : G_SOURCE_REMOVE; } void gui_idle_enable() { - GtkWidget *progress = gui_builder_widget("o_idle_progress"); - - gtk_widget_show(progress); - idle_id = g_idle_add(__on_idle, progress); + idle_id = g_idle_add(__on_idle, NULL); } void gui_idle_disable() diff --git a/include/gui/idle.h b/include/gui/idle.h index e579c268..2fc04e2e 100644 --- a/include/gui/idle.h +++ b/include/gui/idle.h @@ -3,6 +3,8 @@ */ #ifndef OCARINA_GUI_IDLE_H #define OCARINA_GUI_IDLE_H +#include +#include /* Called to enable processing idle queue tasks. */ void gui_idle_enable(); @@ -10,4 +12,10 @@ void gui_idle_enable(); /* Called to disable processing idle queue tasks. */ void gui_idle_disable(); +/* Called to get a pointer to the idle progress bar. */ +static inline GtkProgressBar *gui_progress_bar() +{ + return GTK_PROGRESS_BAR(gui_builder_widget("o_idle_progress")); +} + #endif /* OCARINA_GUI_IDLE_H */ diff --git a/tests/gui/.gitignore b/tests/gui/.gitignore index 662b236a..5a94e170 100644 --- a/tests/gui/.gitignore +++ b/tests/gui/.gitignore @@ -1,9 +1,9 @@ builder window +idle model view queue -idle sidebar playlist audio diff --git a/tests/gui/CMakeLists.txt b/tests/gui/CMakeLists.txt index 15149e33..b44bfea1 100644 --- a/tests/gui/CMakeLists.txt +++ b/tests/gui/CMakeLists.txt @@ -6,10 +6,10 @@ endfunction() gui_unit_test(Builder) gui_unit_test(Window) +gui_unit_test(Idle) gui_unit_test(Model) gui_unit_test(View) gui_unit_test(Queue) -gui_unit_test(Idle) gui_unit_test(Sidebar) gui_unit_test(Playlist) gui_unit_test(Audio) diff --git a/tests/gui/idle.c b/tests/gui/idle.c index 1c4705ff..08220def 100644 --- a/tests/gui/idle.c +++ b/tests/gui/idle.c @@ -2,50 +2,41 @@ * Copyright 2016 (c) Anna Schumaker. */ #include -#include -#include -#include #include -#include -#include #include #include -static const unsigned int N = 100; -static unsigned int cur = -1; +static const unsigned int N = 100; +static unsigned int cur = -1; struct core_init_data init_data; static bool inc_cur(void *data) { - cur++; - g_assert_cmpuint(cur, ==, GPOINTER_TO_UINT(data)); + g_assert_cmpuint(++cur, ==, GPOINTER_TO_UINT(data)); return true; } static void test_idle() { - GtkProgressBar *progress; + GtkProgressBar *progress = gui_progress_bar(); unsigned int i; - float fraction; - - progress = GTK_PROGRESS_BAR(gui_builder_widget("o_idle_progress")); - g_assert_false(gtk_widget_is_visible(GTK_WIDGET(progress))); for (i = 0; i < N; i++) idle_schedule(IDLE_SYNC, inc_cur, GINT_TO_POINTER(i)); + g_assert_false(gtk_widget_is_visible(GTK_WIDGET(progress))); gui_idle_enable(); + g_assert_false(gtk_widget_is_visible(GTK_WIDGET(progress))); - for (i = 0; i < N; i++) { - g_assert_true(gtk_widget_is_visible(GTK_WIDGET(progress))); + for (i = 0; i < (N - 1); i++) { gui_test_main_loop(); + g_assert_true(gtk_widget_is_visible(GTK_WIDGET(progress))); g_assert_cmpfloat(idle_progress(), ==, (float)(i + 1) / N); - if (i != (N - 1)) { - fraction = gtk_progress_bar_get_fraction(progress); - g_assert_cmpfloat(fraction, ==, idle_progress()); - } + g_assert_cmpfloat(gtk_progress_bar_get_fraction(progress), + ==, (float)(i + 1) / N); } + gui_test_main_loop(); g_assert_false(gtk_widget_is_visible(GTK_WIDGET(progress))); } @@ -56,7 +47,6 @@ int main(int argc, char **argv) gtk_init(&argc, NULL); core_init(&argc, NULL, &init_data); gui_builder_init("share/ocarina/ocarina.ui"); - gui_view_init(); gui_test_init(); while (idle_run_task()) {}