GtkScale: Update the label's size request from set_digits ()
authorLuca Bacci <luca.bacci982@gmail.com>
Tue, 6 Sep 2022 17:27:38 +0000 (19:27 +0200)
committerLuca Bacci <luca.bacci982@gmail.com>
Tue, 6 Sep 2022 19:08:31 +0000 (21:08 +0200)
Update the label size request when setting the digits property
by calling the update_label_request () util function.

That util function works by measuring the size request of the
label with the lower and upper values of the adjustment, then
taking the max. That way the size requisition is constant
regardless of the actual displayed value.

Since the util function internally works by setting the text
of the label, let it also set the text at last by taking in
account the current adjustment's value. Most of its callers
do that anyway.

Fixes https://gitlab.gnome.org/GNOME/gtk/-/issues/5156

gtk/gtkscale.c

index 7d289d93634e3c354168d433fb7b7739813c488b..801d9dde109e8ae2aecccf899f4d5d21c3596ed7 100644 (file)
@@ -243,7 +243,6 @@ update_label_request (GtkScale *scale)
   GtkScalePrivate *priv = gtk_scale_get_instance_private (scale);
   GtkAdjustment *adjustment = gtk_range_get_adjustment (GTK_RANGE (scale));
   double lowest_value, highest_value;
-  char *old_text;
   char *text;
   int size = 0;
   int min;
@@ -253,7 +252,6 @@ update_label_request (GtkScale *scale)
   lowest_value = gtk_adjustment_get_lower (adjustment);
   highest_value = gtk_adjustment_get_upper (adjustment);
 
-  old_text = g_strdup (gtk_label_get_label (GTK_LABEL (priv->value_widget)));
   gtk_widget_set_size_request (priv->value_widget, -1, -1);
 
   text = gtk_scale_format_value (scale, lowest_value);
@@ -272,9 +270,10 @@ update_label_request (GtkScale *scale)
   size = MAX (size, min);
   g_free (text);
 
+  text = gtk_scale_format_value (scale, gtk_adjustment_get_value (adjustment));
   gtk_widget_set_size_request (priv->value_widget, size, -1);
-  gtk_label_set_label (GTK_LABEL (priv->value_widget), old_text);
-  g_free (old_text);
+  gtk_label_set_label (GTK_LABEL (priv->value_widget), text);
+  g_free (text);
 }
 
 static void
@@ -1028,6 +1027,9 @@ gtk_scale_set_digits (GtkScale *scale,
       if (priv->draw_value)
         gtk_range_set_round_digits (range, digits);
 
+      if (priv->value_widget)
+        update_label_request (scale);
+
       gtk_widget_queue_resize (GTK_WIDGET (scale));
 
       g_object_notify_by_pspec (G_OBJECT (scale), properties[PROP_DIGITS]);
@@ -1108,16 +1110,9 @@ gtk_scale_set_draw_value (GtkScale *scale,
       priv->draw_value = draw_value;
       if (draw_value)
         {
-          char *txt;
-
-          txt = gtk_scale_format_value (scale,
-                                        gtk_adjustment_get_value (gtk_range_get_adjustment (GTK_RANGE (scale))));
-
           priv->value_widget = g_object_new (GTK_TYPE_LABEL,
                                              "css-name", "value",
-                                             "label", txt,
                                              NULL);
-          g_free (txt);
 
           gtk_widget_insert_after (priv->value_widget, GTK_WIDGET (scale), NULL);
           gtk_range_set_round_digits (GTK_RANGE (scale), priv->digits);
@@ -2057,8 +2052,6 @@ gtk_scale_set_format_value_func (GtkScale                *scale,
                                  GDestroyNotify           destroy_notify)
 {
   GtkScalePrivate *priv = gtk_scale_get_instance_private (scale);
-  GtkAdjustment *adjustment;
-  char *text;
 
   g_return_if_fail (GTK_IS_SCALE (scale));
 
@@ -2069,15 +2062,6 @@ gtk_scale_set_format_value_func (GtkScale                *scale,
   priv->format_value_func_user_data = user_data;
   priv->format_value_func_destroy_notify = destroy_notify;
 
-  if (!priv->value_widget)
-    return;
-
-  update_label_request (scale);
-
-  adjustment = gtk_range_get_adjustment (GTK_RANGE (scale));
-  text = gtk_scale_format_value (scale,
-                                 gtk_adjustment_get_value (adjustment));
-  gtk_label_set_label (GTK_LABEL (priv->value_widget), text);
-
-  g_free (text);
+  if (priv->value_widget)
+    update_label_request (scale);
 }