widget: Don't overadjust allocations
authorBenjamin Otte <otte@redhat.com>
Mon, 18 Oct 2021 23:08:06 +0000 (01:08 +0200)
committerBenjamin Otte <otte@redhat.com>
Mon, 18 Oct 2021 23:55:59 +0000 (01:55 +0200)
When adjusting allocations, don't query height for the current width,
but query it for the adjusted width.

And adjust width not to the width-for-any-height, but to
width-for-allocated-height.

gtk/gtkwidget.c

index 78a282829b8145d582b7d18a3c58207e70020992..6360fa16c7c7a8efbeb8c9433cda49a5d0196d2f 100644 (file)
@@ -3841,29 +3841,42 @@ gtk_widget_adjust_size_allocation (GtkWidget     *widget,
   if (priv->halign == GTK_ALIGN_FILL && priv->valign == GTK_ALIGN_FILL)
     return;
 
+  /* Note that adjust_for_align removes any margins from the
+   * allocated sizes and possibly limits them to the natural sizes */
+
   if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
     {
-      /* Go ahead and request the height for allocated width, note that the internals
-       * of get_height_for_width will internally limit the for_size to natural size
-       * when aligning implicitly.
-       */
-      gtk_widget_measure (widget, GTK_ORIENTATION_HORIZONTAL, -1,
+      gtk_widget_measure (widget, GTK_ORIENTATION_HORIZONTAL,
+                          allocation->height + priv->margin.top + priv->margin.bottom,
                           &min_width, &natural_width, NULL, NULL);
+      adjust_for_align (effective_align (priv->halign, _gtk_widget_get_direction (widget)),
+                        natural_width - priv->margin.left - priv->margin.right,
+                        &allocation->x,
+                        &allocation->width);
       gtk_widget_measure (widget, GTK_ORIENTATION_VERTICAL,
-                          allocation->width + priv->margin.left + priv->margin.right,
+                          allocation->width,
                           &min_height, &natural_height, NULL, NULL);
+      adjust_for_align (priv->valign,
+                        natural_height - priv->margin.top - priv->margin.bottom,
+                        &allocation->y,
+                        &allocation->height);
     }
   else
     {
-      /* Go ahead and request the width for allocated height, note that the internals
-       * of get_width_for_height will internally limit the for_size to natural size
-       * when aligning implicitly.
-       */
-      gtk_widget_measure (widget, GTK_ORIENTATION_VERTICAL, -1,
+      gtk_widget_measure (widget, GTK_ORIENTATION_VERTICAL,
+                          allocation->width + priv->margin.left + priv->margin.right,
                           &min_height, &natural_height, NULL, NULL);
+      adjust_for_align (priv->valign,
+                        natural_height - priv->margin.top - priv->margin.bottom,
+                        &allocation->y,
+                        &allocation->height);
       gtk_widget_measure (widget, GTK_ORIENTATION_HORIZONTAL,
-                          allocation->height + priv->margin.top + priv->margin.bottom,
+                          allocation->height,
                           &min_width, &natural_width, NULL, NULL);
+      adjust_for_align (effective_align (priv->halign, _gtk_widget_get_direction (widget)),
+                        natural_width - priv->margin.left - priv->margin.right,
+                        &allocation->x,
+                        &allocation->width);
     }
 
 #ifdef G_ENABLE_CONSISTENCY_CHECKS
@@ -3877,16 +3890,6 @@ gtk_widget_adjust_size_allocation (GtkWidget     *widget,
                allocation->width, allocation->height,
                min_width, min_height);
 #endif
-  /* Now that we have the right natural height and width, go ahead and remove any margins from the
-   * allocated sizes and possibly limit them to the natural sizes */
-  adjust_for_align (effective_align (priv->halign, _gtk_widget_get_direction (widget)),
-                    natural_width - priv->margin.left - priv->margin.right,
-                    &allocation->x,
-                    &allocation->width);
-  adjust_for_align (priv->valign,
-                    natural_height - priv->margin.top - priv->margin.bottom,
-                    &allocation->y,
-                    &allocation->height);
 }
 
 /**