label: Don't set ellipsized size as natural size
authorBenjamin Otte <otte@redhat.com>
Tue, 30 Nov 2021 13:05:22 +0000 (14:05 +0100)
committerBenjamin Otte <otte@redhat.com>
Tue, 30 Nov 2021 14:10:02 +0000 (15:10 +0100)
Natural size should never ellipsize.

Tests added.

gtk/gtklabel.c
testsuite/reftests/hbox-with-ellipsizing-label.ref.ui [new file with mode: 0644]
testsuite/reftests/hbox-with-ellipsizing-label.ui [new file with mode: 0644]
testsuite/reftests/hbox-with-ellipsizing-wrapping-label.ref.ui [new file with mode: 0644]
testsuite/reftests/hbox-with-ellipsizing-wrapping-label.ui [new file with mode: 0644]
testsuite/reftests/label-sizing.ref.ui
testsuite/reftests/label-sizing.ui
testsuite/reftests/meson.build

index 11ebad9c2d976aeef63eb2b22c4867fc2d465df8..b3744ae8387912732db28cafef20fa9dbbd01129 100644 (file)
@@ -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,19 @@ 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, MAX (minimum_default, 0));
+          pango_layout_get_size (layout, minimum_width, NULL);
+          *minimum_width = MAX (*minimum_width, minimum_default);
+        }
+      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 (file)
index 0000000..1e6871c
--- /dev/null
@@ -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 (file)
index 0000000..ff36ee3
--- /dev/null
@@ -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 (file)
index 0000000..f0151d1
--- /dev/null
@@ -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 (file)
index 0000000..8649153
--- /dev/null
@@ -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>
index 4235424e800249fe2422a1a690b114b9ecfb0db3..390b1bcf84c7a76f2fa0b0ac9f9f617c0696821b 100644 (file)
@@ -2,7 +2,6 @@
 <interface>
   <object class="GtkWindow" id="window1">
     <property name="decorated">0</property>
-    <signal name="realize" handler="reftest:set_default_direction_ltr"/>
     <child>
       <object class="GtkGrid" id="grid1">
         <property name="row_spacing">1</property>
index ad3309b9976bfbfcbf57d38e0e503e709d6911ee..87e1c9eef4d5499090dc5c1543df92fb267a7eb0 100644 (file)
@@ -2,7 +2,6 @@
 <interface>
   <object class="GtkWindow" id="window1">
     <property name="decorated">0</property>
-    <signal name="realize" handler="reftest:set_default_direction_ltr"/>
     <child>
       <object class="GtkGrid" id="grid1">
         <property name="row-spacing">1</property>
index 5f36c6ddb4a8e46c1c552dd4b16956ad198ada1f..0239f43df323d24c3c1735f5e600919bd7a0030d 100644 (file)
@@ -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',