[gtk+/wip/dboles/scrolledwindow-measure: 2/4] ScrolledWindow: Optimise and clean up measure()



commit 2b49dd0eea16a6524c30517189e95202eb99e3fe
Author: Daniel Boles <dboles src gmail com>
Date:   Wed May 31 21:30:38 2017 +0100

    ScrolledWindow: Optimise and clean up measure()
    
     • Only calculate the specified dimension – rather than measuring both &
       discarding the other (which will often be recalculated right after)
     • Only measure a given child scrollbar if it may be visible, not always
     • Move variables into narrowest scopes & otherwise improve readability
    
    https://bugzilla.gnome.org/show_bug.cgi?id=778853

 gtk/gtkscrolledwindow.c |  104 ++++++++++++++++++++---------------------------
 1 files changed, 44 insertions(+), 60 deletions(-)
---
diff --git a/gtk/gtkscrolledwindow.c b/gtk/gtkscrolledwindow.c
index 51b9086..c2788cf 100644
--- a/gtk/gtkscrolledwindow.c
+++ b/gtk/gtkscrolledwindow.c
@@ -1790,28 +1790,13 @@ gtk_scrolled_window_measure (GtkCssGadget   *gadget,
   GtkWidget *widget = gtk_css_gadget_get_owner (gadget);
   GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
   GtkScrolledWindowPrivate *priv = scrolled_window->priv;
-  GtkBin *bin = GTK_BIN (scrolled_window);
-  gint scrollbar_spacing;
-  GtkRequisition hscrollbar_requisition;
-  GtkRequisition vscrollbar_requisition;
-  GtkRequisition minimum_req, natural_req;
-  GtkWidget *child;
-  gint min_child_size, nat_child_size;
-  GtkBorder sborder = { 0 };
-
-  scrollbar_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
 
-  minimum_req.width = 0;
-  minimum_req.height = 0;
-  natural_req.width = 0;
-  natural_req.height = 0;
-
-  gtk_widget_get_preferred_size (priv->hscrollbar,
-                                 &hscrollbar_requisition, NULL);
-  gtk_widget_get_preferred_size (priv->vscrollbar,
-                                 &vscrollbar_requisition, NULL);
+  GtkBin *bin = GTK_BIN (scrolled_window);
+  GtkWidget *child = gtk_bin_get_child (bin);
 
-  child = gtk_bin_get_child (bin);
+  int scrollbar_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
+  int minimum_req = 0, natural_req = 0;
+  GtkBorder sborder = { 0 };
 
   if (child)
     gtk_scrollable_get_border (GTK_SCROLLABLE (child), &sborder);
@@ -1823,53 +1808,50 @@ gtk_scrolled_window_measure (GtkCssGadget   *gadget,
     {
       if (orientation == GTK_ORIENTATION_HORIZONTAL)
        {
-         gtk_widget_get_preferred_width (child,
-                                          &min_child_size,
-                                          &nat_child_size);
+          int min_child_width, nat_child_width;
+          gtk_widget_get_preferred_width (child, &min_child_width, &nat_child_width);
 
          if (priv->propagate_natural_width)
-           natural_req.width += nat_child_size;
+            natural_req += nat_child_width;
 
          if (priv->hscrollbar_policy == GTK_POLICY_NEVER)
            {
-             minimum_req.width += min_child_size;
+              minimum_req += min_child_width;
            }
          else
            {
              gint min = priv->min_content_width >= 0 ? priv->min_content_width : 0;
              gint max = priv->max_content_width >= 0 ? priv->max_content_width : G_MAXINT;
 
-             minimum_req.width = CLAMP (minimum_req.width, min, max);
-             natural_req.width = CLAMP (natural_req.width, min, max);
+              minimum_req = CLAMP (minimum_req, min, max);
+              natural_req = CLAMP (natural_req, min, max);
            }
        }
       else /* GTK_ORIENTATION_VERTICAL */
        {
-         gtk_widget_get_preferred_height (child,
-                                           &min_child_size,
-                                           &nat_child_size);
+          int min_child_height, nat_child_height;
+          gtk_widget_get_preferred_height (child, &min_child_height, &nat_child_height);
 
          if (priv->propagate_natural_height)
-           natural_req.height += nat_child_size;
+            natural_req += nat_child_height;
 
          if (priv->vscrollbar_policy == GTK_POLICY_NEVER)
            {
-             minimum_req.height += min_child_size;
+              minimum_req += min_child_height;
            }
          else
            {
              gint min = priv->min_content_height >= 0 ? priv->min_content_height : 0;
              gint max = priv->max_content_height >= 0 ? priv->max_content_height : G_MAXINT;
 
-             minimum_req.height = CLAMP (minimum_req.height, min, max);
-             natural_req.height = CLAMP (natural_req.height, min, max);
+              minimum_req = CLAMP (minimum_req, min, max);
+              natural_req = CLAMP (natural_req, min, max);
            }
        }
     }
 
   /* Ensure we make requests with natural size >= minimum size */
-  natural_req.height = MAX (minimum_req.height, natural_req.height);
-  natural_req.width  = MAX (minimum_req.width,  natural_req.width);
+  natural_req = MAX (minimum_req, natural_req);
 
   /*
    * Now add to the requisition any additional space for surrounding scrollbars
@@ -1877,38 +1859,40 @@ gtk_scrolled_window_measure (GtkCssGadget   *gadget,
    */
   if (policy_may_be_visible (priv->hscrollbar_policy))
     {
-      minimum_req.width = MAX (minimum_req.width, hscrollbar_requisition.width + sborder.left + 
sborder.right);
-      natural_req.width = MAX (natural_req.width, hscrollbar_requisition.width + sborder.left + 
sborder.right);
+      GtkRequisition hscrollbar_requisition;
+      gtk_widget_get_preferred_size (priv->hscrollbar, &hscrollbar_requisition, NULL);
 
-      if (!priv->use_indicators)
-       {
-         minimum_req.height += scrollbar_spacing + hscrollbar_requisition.height;
-         natural_req.height += scrollbar_spacing + hscrollbar_requisition.height;
-       }
+      if (orientation == GTK_ORIENTATION_HORIZONTAL)
+        {
+          minimum_req = MAX (minimum_req, hscrollbar_requisition.width + sborder.left + sborder.right);
+          natural_req = MAX (natural_req, hscrollbar_requisition.width + sborder.left + sborder.right);
+        }
+      else if (!priv->use_indicators)
+        {
+          minimum_req += scrollbar_spacing + hscrollbar_requisition.height;
+          natural_req += scrollbar_spacing + hscrollbar_requisition.height;
+        }
     }
 
   if (policy_may_be_visible (priv->vscrollbar_policy))
     {
-      minimum_req.height = MAX (minimum_req.height, vscrollbar_requisition.height + sborder.top + 
sborder.bottom);
-      natural_req.height = MAX (natural_req.height, vscrollbar_requisition.height + sborder.top + 
sborder.bottom);
+      GtkRequisition vscrollbar_requisition;
+      gtk_widget_get_preferred_size (priv->vscrollbar, &vscrollbar_requisition, NULL);
 
-      if (!priv->use_indicators)
-       {
-         minimum_req.width += scrollbar_spacing + vscrollbar_requisition.width;
-         natural_req.width += scrollbar_spacing + vscrollbar_requisition.width;
-       }
+      if (orientation == GTK_ORIENTATION_VERTICAL)
+        {
+          minimum_req = MAX (minimum_req, vscrollbar_requisition.height + sborder.top + sborder.bottom);
+          natural_req = MAX (natural_req, vscrollbar_requisition.height + sborder.top + sborder.bottom);
+        }
+      else if (!priv->use_indicators)
+        {
+          minimum_req += scrollbar_spacing + vscrollbar_requisition.width;
+          natural_req += scrollbar_spacing + vscrollbar_requisition.width;
+        }
     }
 
-  if (orientation == GTK_ORIENTATION_HORIZONTAL)
-    {
-      *minimum_size = minimum_req.width;
-      *natural_size = natural_req.width;
-    }
-  else
-    {
-      *minimum_size = minimum_req.height;
-      *natural_size = natural_req.height;
-    }
+  *minimum_size = minimum_req;
+  *natural_size = natural_req;
 }
 
 static void


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