Inscription: Derive row alignment from xalign
authorGeorges Basile Stavracas Neto <georges.stavracas@gmail.com>
Sun, 12 Jun 2022 17:27:33 +0000 (14:27 -0300)
committerBenjamin Otte <otte@redhat.com>
Mon, 13 Jun 2022 03:35:47 +0000 (05:35 +0200)
Texts usually want the alignment of each row to match the xalign of
the text itself.

Derive the alignment of the PangoLayout from the xalign property of
the inscription. Because Pango doesn't provide float row alignment,
map left, center and right from the xalign in 1 / 3 steps.

gtk/gtkinscription.c

index c8937df9e9e417e51ff597bb17bf2c2d3fb09bb1..5f13063fb601ef72a9e44e544e29b7a454cc266f 100644 (file)
@@ -233,6 +233,24 @@ gtk_inscription_set_property (GObject      *object,
     }
 }
 
+static void
+update_pango_alignment (GtkInscription *self)
+{
+  PangoAlignment align;
+  gboolean ltr;
+
+  ltr = _gtk_widget_get_direction (GTK_WIDGET (self)) != GTK_TEXT_DIR_RTL;
+
+  if (self->xalign < 0.33)
+      align = ltr ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
+  else if (self->xalign < 0.67)
+      align = PANGO_ALIGN_CENTER;
+  else
+      align = ltr ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
+
+  pango_layout_set_alignment (self->layout, align);
+}
+
 static void
 gtk_inscription_update_layout_attributes (GtkInscription *self,
                                           PangoAttrList  *css_attrs)
@@ -267,6 +285,17 @@ gtk_inscription_css_changed (GtkWidget         *widget,
     }
 }
 
+static void
+gtk_inscription_direction_changed (GtkWidget        *widget,
+                                   GtkTextDirection  previous_direction)
+{
+  GtkInscription *self = GTK_INSCRIPTION (widget);
+
+  GTK_WIDGET_CLASS (gtk_inscription_parent_class)->direction_changed (widget, previous_direction);
+
+  update_pango_alignment (self);
+}
+
 static PangoFontMetrics *
 gtk_inscription_get_font_metrics (GtkInscription *self)
 {
@@ -491,6 +520,7 @@ gtk_inscription_class_init (GtkInscriptionClass *klass)
   gobject_class->set_property = gtk_inscription_set_property;
 
   widget_class->css_changed = gtk_inscription_css_changed;
+  widget_class->direction_changed = gtk_inscription_direction_changed;
   widget_class->measure = gtk_inscription_measure;
   widget_class->size_allocate = gtk_inscription_allocate;
   widget_class->snapshot = gtk_inscription_snapshot;
@@ -700,6 +730,7 @@ gtk_inscription_init (GtkInscription *self)
 
   self->layout = gtk_widget_create_pango_layout (GTK_WIDGET (self), NULL);
   pango_layout_set_wrap (self->layout, PANGO_WRAP_WORD_CHAR);
+  update_pango_alignment (self);
 }
 
 /* for a11y */
@@ -988,6 +1019,8 @@ gtk_inscription_set_xalign (GtkInscription *self,
 
   self->xalign = xalign;
 
+  update_pango_alignment (self);
+
   gtk_widget_queue_draw (GTK_WIDGET (self));
 
   g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_XALIGN]);