Create a progress bar for idle tasks

Whenever a task is queued, I increment a counter.  When it is run, I
increment a different counter.  This allows me to track the idle queue
completion percentage.  I also created a progress bar for Ocarina to
display in the toes of the footer.

Signed-off-by: Bryan Schumaker <bjschuma@gmail.com>
This commit is contained in:
Bryan Schumaker 2012-03-17 13:27:04 -04:00
parent 03d35fee77
commit 2c830af7fa
5 changed files with 52 additions and 3 deletions

View File

@ -12,6 +12,7 @@ namespace libsaria
void run_task();
void queue_task(IdleTask *, bool);
void enable();
float progress();
};
};

View File

@ -16,6 +16,10 @@ namespace ocarina
{
void init();
void set_idle_progress(float);
void hide_idle_progress();
GtkWidget *playlist_init();
GtkWidget *footer_init();
GtkWidget *now_playing_page();

View File

@ -1,11 +1,14 @@
// Copyright (c) 2011 Bryan Schumaker.
#include <libsaria/idle.h>
#include <libsaria/print.h>
#include <queue>
using namespace std;
static bool enabled = false;
static deque<IdleTask *> idle_queue;
static float queued = 0.0;
static float serviced = 0.0;
static void do_task(IdleTask *task)
{
@ -16,6 +19,13 @@ static void do_task(IdleTask *task)
namespace libsaria
{
float idle::progress()
{
if (queued == 0)
return 1.0;
return (serviced / queued);
}
int idle::size()
{
return idle_queue.size();
@ -29,6 +39,11 @@ namespace libsaria
task = idle_queue.front();
idle_queue.pop_front();
do_task(task);
if (size() == 0) {
queued = 0.0;
serviced = 0.0;
} else
serviced += 1.0;
}
/*
@ -47,6 +62,7 @@ namespace libsaria
idle_queue.push_front(task);
else
idle_queue.push_back(task);
queued += 1.0;
}
void idle::enable()

View File

@ -3,6 +3,7 @@
#include <ocarina/body.h>
static GtkWidget *notebook;
static GtkWidget *idle_progress;
static void add_page(string text, GtkWidget *page)
{
@ -14,20 +15,33 @@ static void add_page(string text, GtkWidget *page)
gtk_widget_show(label);
}
static GtkWidget *make_toes()
{
GtkWidget *box = gtk_hbox_new(FALSE, 0);
GtkWidget *label = gtk_label_new("Controls go here (buttons, progress bar, ...)");
idle_progress = gtk_progress_bar_new();
gtk_box_pack_start(GTK_BOX(box), idle_progress, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0);
gtk_widget_show(box);
gtk_widget_show(label);
return box;
}
namespace ocarina
{
GtkWidget *body::footer_init()
{
GtkWidget *footer = gtk_vbox_new(FALSE, 0);
GtkWidget *label = gtk_label_new("Controls go here (buttons, progress bar, ...)");
GtkWidget *toes = make_toes();
notebook = gtk_notebook_new();
gtk_notebook_set_tab_pos(GTK_NOTEBOOK(notebook), GTK_POS_TOP);
g_object_set(notebook, "tab-border", 0, NULL);
gtk_box_pack_start(GTK_BOX(footer), notebook, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(footer), label, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(footer), toes, FALSE, FALSE, 0);
add_page("Now Playing", now_playing_page());
add_page("Library", library_page());
@ -35,8 +49,18 @@ namespace ocarina
gtk_widget_show(footer);
gtk_widget_show(notebook);
gtk_widget_show(label);
return footer;
};
void body::set_idle_progress(float progress)
{
gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(idle_progress), progress);
gtk_widget_show(idle_progress);
}
void body::hide_idle_progress()
{
gtk_widget_hide(idle_progress);
}
}; /* Namespace: ocarina */

View File

@ -37,6 +37,10 @@ static gboolean idle(gpointer data)
{
libsaria::idle::run_task();
using_idle = (libsaria::idle::size() != 0);
if (using_idle)
ocarina::body::set_idle_progress(libsaria::idle::progress());
else
ocarina::body::hide_idle_progress();
return using_idle;
}