emojichooser: Only measure reference emoji once

Doing that once for every emoji is pretty slow and unnecessary as the
width does not change.
This commit is contained in:
Timm Bäder 2018-02-27 11:45:34 +01:00
parent 3c33e541cd
commit 813ccb6378

View File

@ -51,6 +51,8 @@ struct _GtkEmojiChooser
GtkWidget *stack; GtkWidget *stack;
GtkWidget *scrolled_window; GtkWidget *scrolled_window;
int emoji_max_width;
EmojiSection recent; EmojiSection recent;
EmojiSection people; EmojiSection people;
EmojiSection body; EmojiSection body;
@ -346,7 +348,6 @@ add_emoji (GtkWidget *box,
int i; int i;
PangoLayout *layout; PangoLayout *layout;
PangoRectangle rect; PangoRectangle rect;
int width;
codes = g_variant_get_child_value (item, 0); codes = g_variant_get_child_value (item, 0);
for (i = 0; i < g_variant_n_children (codes); i++) for (i = 0; i < g_variant_n_children (codes); i++)
@ -362,22 +363,17 @@ add_emoji (GtkWidget *box,
g_variant_unref (codes); g_variant_unref (codes);
p[0] = 0; p[0] = 0;
label = gtk_label_new ("🙂"); label = gtk_label_new (text);
attrs = pango_attr_list_new (); attrs = pango_attr_list_new ();
pango_attr_list_insert (attrs, pango_attr_scale_new (PANGO_SCALE_X_LARGE)); pango_attr_list_insert (attrs, pango_attr_scale_new (PANGO_SCALE_X_LARGE));
gtk_label_set_attributes (GTK_LABEL (label), attrs); gtk_label_set_attributes (GTK_LABEL (label), attrs);
pango_attr_list_unref (attrs); pango_attr_list_unref (attrs);
layout = gtk_label_get_layout (GTK_LABEL (label));
pango_layout_get_extents (layout, &rect, NULL);
width = rect.width;
gtk_label_set_text (GTK_LABEL (label), text);
layout = gtk_label_get_layout (GTK_LABEL (label)); layout = gtk_label_get_layout (GTK_LABEL (label));
pango_layout_get_extents (layout, &rect, NULL); pango_layout_get_extents (layout, &rect, NULL);
/* Check for fallback rendering that generates too wide items */ /* Check for fallback rendering that generates too wide items */
if (rect.width >= 2 * width) if (rect.width >= 2 * chooser->emoji_max_width)
{ {
gtk_widget_destroy (label); gtk_widget_destroy (label);
return; return;
@ -617,6 +613,26 @@ gtk_emoji_chooser_init (GtkEmojiChooser *chooser)
gtk_widget_init_template (GTK_WIDGET (chooser)); gtk_widget_init_template (GTK_WIDGET (chooser));
/* Get a reasonable maximum width for an emoji. We do this to
* skip overly wide fallback rendering for certain emojis the
* font does not contain and therefore end up being rendered
* as multiply glyphs. */
{
PangoLayout *layout = gtk_widget_create_pango_layout (GTK_WIDGET (chooser), "🙂");
PangoAttrList *attrs;
PangoRectangle rect;
attrs = pango_attr_list_new ();
pango_attr_list_insert (attrs, pango_attr_scale_new (PANGO_SCALE_X_LARGE));
pango_layout_set_attributes (layout, attrs);
pango_attr_list_unref (attrs);
pango_layout_get_extents (layout, &rect, NULL);
chooser->emoji_max_width = rect.width;
g_object_unref (layout);
}
chooser->recent_long_press = gtk_gesture_long_press_new (chooser->recent.box); chooser->recent_long_press = gtk_gesture_long_press_new (chooser->recent.box);
g_signal_connect (chooser->recent_long_press, "pressed", G_CALLBACK (long_pressed_cb), chooser); g_signal_connect (chooser->recent_long_press, "pressed", G_CALLBACK (long_pressed_cb), chooser);
chooser->recent_multi_press = gtk_gesture_multi_press_new (chooser->recent.box); chooser->recent_multi_press = gtk_gesture_multi_press_new (chooser->recent.box);