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 <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2016-08-20 09:01:29 -04:00
parent f9238c34e4
commit 3383f9e32a
5 changed files with 31 additions and 44 deletions

View File

@ -1,38 +1,27 @@
/*
* Copyright 2016 (c) Anna Schumaker.
*/
#include <core/idle.h>
#include <gui/builder.h>
#include <gtk/gtk.h>
#include <gui/idle.h>
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()

View File

@ -3,6 +3,8 @@
*/
#ifndef OCARINA_GUI_IDLE_H
#define OCARINA_GUI_IDLE_H
#include <core/idle.h>
#include <gui/builder.h>
/* 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 */

View File

@ -1,9 +1,9 @@
builder
window
idle
model
view
queue
idle
sidebar
playlist
audio

View File

@ -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)

View File

@ -2,50 +2,41 @@
* Copyright 2016 (c) Anna Schumaker.
*/
#include <core/core.h>
#include <core/idle.h>
#include <core/settings.h>
#include <gui/builder.h>
#include <gui/idle.h>
#include <gui/view.h>
#include <gui/window.h>
#include <tests/test.h>
#include <tests/gui.h>
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()) {}