[gtk/label-sizing: 5/5] widget: Don't overadjust allocations




commit 3802f79d70d77a4b0c60e686951c8a116c0a7278
Author: Benjamin Otte <otte redhat com>
Date:   Tue Oct 19 01:08:06 2021 +0200

    widget: Don't overadjust allocations
    
    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 | 51 +++++++++++++++++++++++++++++----------------------
 1 file changed, 29 insertions(+), 22 deletions(-)
---
diff --git a/gtk/gtkwidget.c b/gtk/gtkwidget.c
index 78a282829b..93664155dd 100644
--- a/gtk/gtkwidget.c
+++ b/gtk/gtkwidget.c
@@ -3841,29 +3841,46 @@ 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);
+      g_print ("adjusting width from %d", allocation->width);
+      adjust_for_align (effective_align (priv->halign, _gtk_widget_get_direction (widget)),
+                        natural_width - priv->margin.left - priv->margin.right,
+                        &allocation->x,
+                        &allocation->width);
+      g_print (" to %d (min %d nat %d)\n", allocation->width, min_width, natural_width);
       gtk_widget_measure (widget, GTK_ORIENTATION_VERTICAL,
-                          allocation->width + priv->margin.left + priv->margin.right,
+                          allocation->width,
                           &min_height, &natural_height, NULL, NULL);
+      g_print ("adjusting height from %d", allocation->height);
+      adjust_for_align (priv->valign,
+                        natural_height - priv->margin.top - priv->margin.bottom,
+                        &allocation->y,
+                        &allocation->height);
+      g_print (" to %d (min %d nat %d)\n", allocation->height, min_height, natural_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 +3894,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);
 }
 
 /**


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