[gtk/wip/otte/for-main: 6/6] label: Don't set ellipsized size as natural size




commit f48bc5600853abe726d73420dad64a92175347cd
Author: Benjamin Otte <otte redhat com>
Date:   Tue Nov 30 14:05:22 2021 +0100

    label: Don't set ellipsized size as natural size
    
    Natural size should never ellipsize.
    
    Tests added.

 gtk/gtklabel.c                                     | 23 +++++++++++++++++++---
 .../reftests/hbox-with-ellipsizing-label.ref.ui    | 15 ++++++++++++++
 testsuite/reftests/hbox-with-ellipsizing-label.ui  | 16 +++++++++++++++
 .../hbox-with-ellipsizing-wrapping-label.ref.ui    | 17 ++++++++++++++++
 .../hbox-with-ellipsizing-wrapping-label.ui        | 18 +++++++++++++++++
 testsuite/reftests/meson.build                     |  4 ++++
 6 files changed, 90 insertions(+), 3 deletions(-)
---
diff --git a/gtk/gtklabel.c b/gtk/gtklabel.c
index 11ebad9c2d..fbb88b02cb 100644
--- a/gtk/gtklabel.c
+++ b/gtk/gtklabel.c
@@ -1185,10 +1185,17 @@ get_width_for_height (GtkLabel *self,
     {
       int min, max, mid, text_width, text_height;
 
+      /* Can't use a measuring layout here, because we need to force
+       * ellipsizing mode */
+      gtk_label_ensure_layout (self);
+      layout = pango_layout_copy (self->layout);
+      pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_NONE);
+
       /* binary search for the smallest width where the height doesn't
        * eclipse the given height */
       min = MAX (minimum_default, 0);
-      layout = gtk_label_get_measuring_layout (self, NULL, -1);
+
+      pango_layout_set_width (layout, -1);
       pango_layout_get_size (layout, &max, NULL);
 
       min = PANGO_PIXELS_CEIL (min);
@@ -1196,7 +1203,7 @@ get_width_for_height (GtkLabel *self,
       while (min < max)
         {
           mid = (min + max) / 2;
-          layout = gtk_label_get_measuring_layout (self, layout, mid * PANGO_SCALE);
+          pango_layout_set_width (layout, mid * PANGO_SCALE);
           pango_layout_get_size (layout, &text_width, &text_height);
           text_width = PANGO_PIXELS_CEIL (text_width);
           if (text_width > mid)
@@ -1207,8 +1214,18 @@ get_width_for_height (GtkLabel *self,
             max = mid;
         }
 
-      *minimum_width = min * PANGO_SCALE;
       *natural_width = min * PANGO_SCALE;
+
+      if (self->ellipsize != PANGO_ELLIPSIZE_NONE)
+        {
+          g_object_unref (layout);
+          layout = gtk_label_get_measuring_layout (self, NULL, 0);
+          pango_layout_get_size (layout, minimum_width, NULL);
+        }
+      else
+        {
+          *minimum_width = *natural_width;
+        }
     }
 
   g_object_unref (layout);
diff --git a/testsuite/reftests/hbox-with-ellipsizing-label.ref.ui 
b/testsuite/reftests/hbox-with-ellipsizing-label.ref.ui
new file mode 100644
index 0000000000..1e6871cb40
--- /dev/null
+++ b/testsuite/reftests/hbox-with-ellipsizing-label.ref.ui
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <object class="GtkWindow">
+    <property name="decorated">0</property>
+    <child>
+      <object class="GtkBox">
+        <child>
+          <object class="GtkLabel">
+            <property name="label">Hello World</property>
+          </object>
+        </child>
+      </object>
+    </child>
+  </object>
+</interface>
diff --git a/testsuite/reftests/hbox-with-ellipsizing-label.ui 
b/testsuite/reftests/hbox-with-ellipsizing-label.ui
new file mode 100644
index 0000000000..ff36ee32b3
--- /dev/null
+++ b/testsuite/reftests/hbox-with-ellipsizing-label.ui
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <object class="GtkWindow">
+    <property name="decorated">0</property>
+    <child>
+      <object class="GtkBox">
+        <child>
+          <object class="GtkLabel">
+            <property name="label">Hello World</property>
+            <property name="ellipsize">end</property>
+          </object>
+        </child>
+      </object>
+    </child>
+  </object>
+</interface>
diff --git a/testsuite/reftests/hbox-with-ellipsizing-wrapping-label.ref.ui 
b/testsuite/reftests/hbox-with-ellipsizing-wrapping-label.ref.ui
new file mode 100644
index 0000000000..f0151d113f
--- /dev/null
+++ b/testsuite/reftests/hbox-with-ellipsizing-wrapping-label.ref.ui
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <object class="GtkWindow">
+    <property name="decorated">0</property>
+    <child>
+      <object class="GtkBox">
+        <child>
+          <object class="GtkLabel">
+            <property name="label">Hello World</property>
+            <property name="wrap">True</property>
+            <property name="wrap-mode">char</property>
+          </object>
+        </child>
+      </object>
+    </child>
+  </object>
+</interface>
diff --git a/testsuite/reftests/hbox-with-ellipsizing-wrapping-label.ui 
b/testsuite/reftests/hbox-with-ellipsizing-wrapping-label.ui
new file mode 100644
index 0000000000..8649153e94
--- /dev/null
+++ b/testsuite/reftests/hbox-with-ellipsizing-wrapping-label.ui
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <object class="GtkWindow">
+    <property name="decorated">0</property>
+    <child>
+      <object class="GtkBox">
+        <child>
+          <object class="GtkLabel">
+            <property name="label">Hello World</property>
+            <property name="ellipsize">end</property>
+            <property name="wrap">True</property>
+            <property name="wrap-mode">char</property>
+          </object>
+        </child>
+      </object>
+    </child>
+  </object>
+</interface>
diff --git a/testsuite/reftests/meson.build b/testsuite/reftests/meson.build
index 5f36c6ddb4..0239f43df3 100644
--- a/testsuite/reftests/meson.build
+++ b/testsuite/reftests/meson.build
@@ -310,6 +310,10 @@ testdata = [
   'gtk-icontheme-sizing.css',
   'gtk-icontheme-sizing.ref.ui',
   'gtk-icontheme-sizing.ui',
+  'hbox-with-ellipsizing-label.ref.ui',
+  'hbox-with-ellipsizing-label.ui',
+  'hbox-with-ellipsizing-wrapping-label.ref.ui',
+  'hbox-with-ellipsizing-wrapping-label.ui',
   'icon-effect-missing.css',
   'icon-effect-missing.ref.ui',
   'icon-effect-missing.ui',


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