[gtk/wip/otte/whatever: 9/9] gtk-demo: Use a progressbar in the words demo



commit 45fe3258ec16679263bbff28b76a63071a9befcf
Author: Matthias Clasen <mclasen redhat com>
Date:   Tue Jun 30 10:38:59 2020 -0400

    gtk-demo: Use a progressbar in the words demo
    
    This looks better and a bit more polished.

 demos/gtk-demo/listview_words.c | 33 +++++++++++++++++++++------------
 1 file changed, 21 insertions(+), 12 deletions(-)
---
diff --git a/demos/gtk-demo/listview_words.c b/demos/gtk-demo/listview_words.c
index de124b3383..38dbf19b1e 100644
--- a/demos/gtk-demo/listview_words.c
+++ b/demos/gtk-demo/listview_words.c
@@ -9,6 +9,7 @@
 #include <gtk/gtk.h>
 
 static GtkWidget *window = NULL;
+static GtkWidget *progress;
 
 const char *factory_text =
 "<?xml version='1.0' encoding='UTF-8'?>\n"
@@ -31,18 +32,17 @@ const char *factory_text =
 static void
 update_title_cb (GtkFilterListModel *model)
 {
+  guint total;
   char *title;
   guint pending;
 
+  total = g_list_model_get_n_items (gtk_filter_list_model_get_model (model));
   pending = gtk_filter_list_model_get_pending (model);
-  if (pending)
-    {
-      guint total = g_list_model_get_n_items (gtk_filter_list_model_get_model (model));
-      title = g_strdup_printf ("%u Words (%u%% done)", g_list_model_get_n_items (G_LIST_MODEL (model)),
-                               (total - pending) * 100 / total);
-    }
-  else
-    title = g_strdup_printf ("%u Words", g_list_model_get_n_items (G_LIST_MODEL (model)));
+
+  title = g_strdup_printf ("%u Words", g_list_model_get_n_items (G_LIST_MODEL (model)));
+
+  gtk_widget_set_visible (progress, pending != 0);
+  gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (progress), (total - pending) / (double) total);
   gtk_window_set_title (GTK_WINDOW (window), title);
   g_free (title);
 }
@@ -155,7 +155,7 @@ do_listview_words (GtkWidget *do_widget)
 {
   if (window == NULL)
     {
-      GtkWidget *header, *listview, *sw, *vbox, *search_entry, *open_button;
+      GtkWidget *header, *listview, *sw, *vbox, *search_entry, *open_button, *overlay;
       GtkFilterListModel *filter_model;
       GtkNoSelection *selection;
       GtkStringList *stringlist;
@@ -205,8 +205,17 @@ do_listview_words (GtkWidget *do_widget)
       g_object_bind_property (search_entry, "text", filter, "search", 0);
       gtk_box_append (GTK_BOX (vbox), search_entry);
 
+      overlay = gtk_overlay_new ();
+      gtk_box_append (GTK_BOX (vbox), overlay);
+
+      progress = gtk_progress_bar_new ();
+      gtk_widget_set_halign (progress, GTK_ALIGN_FILL);
+      gtk_widget_set_valign (progress, GTK_ALIGN_START);
+      gtk_widget_set_hexpand (progress, TRUE);
+      gtk_overlay_add_overlay (GTK_OVERLAY (overlay), progress);
+
       sw = gtk_scrolled_window_new ();
-      gtk_box_append (GTK_BOX (vbox), sw);
+      gtk_overlay_set_child (GTK_OVERLAY (overlay), sw);
 
       listview = gtk_list_view_new_with_factory (
           gtk_builder_list_item_factory_new_from_bytes (NULL,
@@ -216,8 +225,8 @@ do_listview_words (GtkWidget *do_widget)
       gtk_list_view_set_model (GTK_LIST_VIEW (listview), G_LIST_MODEL (selection));
       g_object_unref (selection);
 
-      g_signal_connect (filter_model, "items-changed", G_CALLBACK (update_title_cb), NULL);
-      g_signal_connect (filter_model, "notify::pending", G_CALLBACK (update_title_cb), NULL);
+      g_signal_connect (filter_model, "items-changed", G_CALLBACK (update_title_cb), progress);
+      g_signal_connect (filter_model, "notify::pending", G_CALLBACK (update_title_cb), progress);
       update_title_cb (filter_model);
 
       g_object_unref (filter_model);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]