[gtk/wip/otte/listview: 16/139] gtk-demo: Make fishbowl info text use bindings



commit 7aa2efc3df865a843f5a800d38a5aeab279df356
Author: Benjamin Otte <otte redhat com>
Date:   Mon Nov 25 07:28:19 2019 +0100

    gtk-demo: Make fishbowl info text use bindings
    
    It's a good demo for how bindings can format multiple properties into an
    informative string with 1 line of code (and 5 lines of XML).

 demos/gtk-demo/fishbowl.c    | 13 ++++++++++++-
 demos/gtk-demo/fishbowl.ui   | 22 ++++++----------------
 demos/gtk-demo/gtkfishbowl.c | 17 -----------------
 3 files changed, 18 insertions(+), 34 deletions(-)
---
diff --git a/demos/gtk-demo/fishbowl.c b/demos/gtk-demo/fishbowl.c
index 1539b3d336..49e908781a 100644
--- a/demos/gtk-demo/fishbowl.c
+++ b/demos/gtk-demo/fishbowl.c
@@ -265,6 +265,14 @@ changes_toggled_cb (GtkToggleButton *button,
     gtk_button_set_icon_name (GTK_BUTTON (button), "changes-allow");
 }
 
+char *
+format_header_cb (GObject *object,
+                  guint    count,
+                  double   fps)
+{
+  return g_strdup_printf ("%u Icons, %.2f fps", count, fps);
+}
+
 GtkWidget *
 do_fishbowl (GtkWidget *do_widget)
 {
@@ -283,6 +291,7 @@ do_fishbowl (GtkWidget *do_widget)
   if (!window)
     {
       GtkBuilder *builder;
+      GError *error = NULL;
       GtkWidget *bowl;
 
       g_type_ensure (GTK_TYPE_FISHBOWL);
@@ -292,8 +301,10 @@ do_fishbowl (GtkWidget *do_widget)
                                         "next_button_clicked_cb", G_CALLBACK (next_button_clicked_cb),
                                         "prev_button_clicked_cb", G_CALLBACK (prev_button_clicked_cb),
                                         "changes_toggled_cb", G_CALLBACK (changes_toggled_cb),
+                                        "format_header_cb", G_CALLBACK (format_header_cb),
                                         NULL);
-      gtk_builder_add_from_resource (builder, "/fishbowl/fishbowl.ui", NULL);
+      gtk_builder_add_from_resource (builder, "/fishbowl/fishbowl.ui", &error);
+      g_assert_no_error (error);
       window = GTK_WIDGET (gtk_builder_get_object (builder, "window"));
       g_signal_connect (window, "destroy",
                         G_CALLBACK (gtk_widget_destroyed), &window);
diff --git a/demos/gtk-demo/fishbowl.ui b/demos/gtk-demo/fishbowl.ui
index 5d09b3477a..2f8d3a69d0 100644
--- a/demos/gtk-demo/fishbowl.ui
+++ b/demos/gtk-demo/fishbowl.ui
@@ -28,22 +28,12 @@
         </child>
         <child type="end">
           <object class="GtkLabel">
-            <property name="label">fps</property>
-          </object>
-        </child>
-        <child type="end">
-          <object class="GtkLabel">
-            <property name="label" bind-source="bowl" bind-property="framerate-string"/>
-          </object>
-        </child>
-        <child type="end">
-          <object class="GtkLabel">
-            <property name="label">Icons, </property>
-          </object>
-        </child>
-        <child type="end">
-          <object class="GtkLabel">
-            <property name="label" bind-source="bowl" bind-property="count"/>
+            <binding name="label">
+              <closure type="gchararray" function="format_header_cb">
+                <lookup name="count">bowl</lookup>
+                <lookup name="framerate">bowl</lookup>
+              </closure>
+            </binding>
           </object>
         </child>
         <child type="end">
diff --git a/demos/gtk-demo/gtkfishbowl.c b/demos/gtk-demo/gtkfishbowl.c
index 0d9f85f124..58c2c5a182 100644
--- a/demos/gtk-demo/gtkfishbowl.c
+++ b/demos/gtk-demo/gtkfishbowl.c
@@ -53,7 +53,6 @@ enum {
    PROP_BENCHMARK,
    PROP_COUNT,
    PROP_FRAMERATE,
-   PROP_FRAMERATE_STRING,
    PROP_UPDATE_DELAY,
    NUM_PROPERTIES
 };
@@ -289,14 +288,6 @@ gtk_fishbowl_get_property (GObject         *object,
       g_value_set_double (value, gtk_fishbowl_get_framerate (fishbowl));
       break;
 
-    case PROP_FRAMERATE_STRING:
-      {
-        char *s = g_strdup_printf ("%.2f", gtk_fishbowl_get_framerate (fishbowl));
-        g_value_set_string (value, s);
-        g_free (s);
-      }
-      break;
-
     case PROP_UPDATE_DELAY:
       g_value_set_int64 (value, gtk_fishbowl_get_update_delay (fishbowl));
       break;
@@ -350,13 +341,6 @@ gtk_fishbowl_class_init (GtkFishbowlClass *klass)
                            0,
                            G_PARAM_READABLE);
 
-  props[PROP_FRAMERATE_STRING] =
-      g_param_spec_string ("framerate-string",
-                           "Framerate as string",
-                           "Framerate as string, with 2 decimals",
-                           NULL,
-                           G_PARAM_READABLE);
-
   props[PROP_UPDATE_DELAY] =
       g_param_spec_int64 ("update-delay",
                           "Update delay",
@@ -508,7 +492,6 @@ gtk_fishbowl_do_update (GtkFishbowl *fishbowl)
   priv->framerate = ((int)(priv->framerate * 100))/100.0;
 
   g_object_notify_by_pspec (G_OBJECT (fishbowl), props[PROP_FRAMERATE]);
-  g_object_notify_by_pspec (G_OBJECT (fishbowl), props[PROP_FRAMERATE_STRING]);
 
   if (!priv->benchmark)
     return;


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