progress.c (1546B)
1// SPDX-License-Identifier: GPL-2.0 2#include <inttypes.h> 3 4#include "gtk.h" 5#include "../progress.h" 6 7static GtkWidget *dialog; 8static GtkWidget *progress; 9 10static void gtk_ui_progress__update(struct ui_progress *p) 11{ 12 double fraction = p->total ? 1.0 * p->curr / p->total : 0.0; 13 char buf[1024]; 14 15 if (dialog == NULL) { 16 GtkWidget *vbox = gtk_vbox_new(TRUE, 5); 17 GtkWidget *label = gtk_label_new(p->title); 18 19 dialog = gtk_window_new(GTK_WINDOW_TOPLEVEL); 20 progress = gtk_progress_bar_new(); 21 22 gtk_box_pack_start(GTK_BOX(vbox), label, TRUE, FALSE, 3); 23 gtk_box_pack_start(GTK_BOX(vbox), progress, TRUE, TRUE, 3); 24 25 gtk_container_add(GTK_CONTAINER(dialog), vbox); 26 27 gtk_window_set_title(GTK_WINDOW(dialog), "perf"); 28 gtk_window_resize(GTK_WINDOW(dialog), 300, 80); 29 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER); 30 31 gtk_widget_show_all(dialog); 32 } 33 34 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress), fraction); 35 snprintf(buf, sizeof(buf), "%"PRIu64" / %"PRIu64, p->curr, p->total); 36 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress), buf); 37 38 /* we didn't call gtk_main yet, so do it manually */ 39 while (gtk_events_pending()) 40 gtk_main_iteration(); 41} 42 43static void gtk_ui_progress__finish(void) 44{ 45 /* this will also destroy all of its children */ 46 gtk_widget_destroy(dialog); 47 48 dialog = NULL; 49} 50 51static struct ui_progress_ops gtk_ui_progress__ops = { 52 .update = gtk_ui_progress__update, 53 .finish = gtk_ui_progress__finish, 54}; 55 56void gtk_ui_progress__init(void) 57{ 58 ui_progress__ops = >k_ui_progress__ops; 59}