[gtk/switch-fallback] Add fallback glyphs for GtkSwitch states



commit 5c50e48156c6edbc2923bb3702f60349d8184057
Author: Emmanuele Bassi <ebassi gnome org>
Date:   Tue Jan 15 22:16:05 2019 +0000

    Add fallback glyphs for GtkSwitch states
    
    Even though the IEC power glyphs are part of Unicode 9.0 (released in
    2016) not all fonts have them.
    
    To avoid showing the hexbox of doom when the system font does not have
    the glyphs we'd like to use, add a fallback pair, using the old glyphs
    we suggested when the labels were translatable.

 gtk/gtkswitch.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 53 insertions(+), 2 deletions(-)
---
diff --git a/gtk/gtkswitch.c b/gtk/gtkswitch.c
index abc37ab863..cb361c1292 100644
--- a/gtk/gtkswitch.c
+++ b/gtk/gtkswitch.c
@@ -280,6 +280,54 @@ gtk_switch_activate (GtkSwitch *sw)
   gtk_switch_begin_toggle_animation (sw);
 }
 
+static void
+gtk_switch_update_state_labels (GtkSwitch *sw)
+{
+  /* Glyphs for the ON state, in descending order of preference */
+  const char *on_glyphs[] = {
+    "⏽", /* U+23FD POWER ON SYMBOL */
+    "❙", /* U+2759 MEDIUM VERTICAL BAR */
+  };
+
+  /* Glyphs for the OFF state, in descending order of preference */
+  const char *off_glyphs[] = {
+    "⭘", /* U+2B58 HEAVY CIRCLE */
+    "○", /* U+25CB WHITE CIRCLE */
+  };
+
+  GtkSwitchPrivate *priv = gtk_switch_get_instance_private (sw);
+  GtkWidget *widget = GTK_WIDGET (sw);
+  int i;
+
+  for (i = 0; i < G_N_ELEMENTS (on_glyphs); i++)
+    {
+      PangoLayout *layout = gtk_widget_create_pango_layout (widget, on_glyphs[i]);
+
+      if (pango_layout_get_unknown_glyphs_count (layout) == 0)
+        {
+          gtk_label_set_text (GTK_LABEL (priv->on_label), on_glyphs[i]);
+          g_object_unref (layout);
+          break;
+        }
+
+      g_object_unref (layout);
+    }
+
+  for (i = 0; i < G_N_ELEMENTS (off_glyphs); i++)
+    {
+      PangoLayout *layout = gtk_widget_create_pango_layout (widget, off_glyphs[i]);
+
+      if (pango_layout_get_unknown_glyphs_count (layout) == 0)
+        {
+          gtk_label_set_text (GTK_LABEL (priv->off_label), off_glyphs[i]);
+          g_object_unref (layout);
+          break;
+        }
+
+      g_object_unref (layout);
+    }
+}
+
 static void
 gtk_switch_measure (GtkWidget      *widget,
                     GtkOrientation  orientation,
@@ -639,14 +687,17 @@ gtk_switch_init (GtkSwitch *self)
   gtk_widget_add_controller (GTK_WIDGET (self), GTK_EVENT_CONTROLLER (gesture));
   priv->pan_gesture = gesture;
 
-  priv->on_label = gtk_label_new ("⏽");
+
+  priv->on_label = gtk_label_new ("");
   gtk_widget_set_parent (priv->on_label, GTK_WIDGET (self));
 
-  priv->off_label = gtk_label_new ("⭘");
+  priv->off_label = gtk_label_new ("");
   gtk_widget_set_parent (priv->off_label, GTK_WIDGET (self));
 
   priv->slider = gtk_gizmo_new ("slider", NULL, NULL, NULL);
   gtk_widget_set_parent (priv->slider, GTK_WIDGET (self));
+
+  gtk_switch_update_state_labels (self);
 }
 
 /**


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