emoji: Add keywords to the data

And use them for matching in the Emoji chooser.
This commit is contained in:
Matthias Clasen
2020-04-14 22:29:25 -04:00
committed by Marco Trevisan (Treviño)
parent cb32ff5b18
commit 78cc9113c1
3 changed files with 36 additions and 16 deletions

View File

@@ -23,9 +23,10 @@
* au - sequence of unicode codepoints. If the
* sequence contains a 0, it marks the point
* where skin tone modifiers should be inserted
* s - name, e.g. "man worker"
* s - shortname, for completion. This includes
* s - name, e.g. "man worker"
* s - shortname, for completion. This includes
* colons to mark the ends, e.g. ":guardsman:"
* as - keywords, e.g. "man", "worker"
*/
#include <json-glib/json-glib.h>
#include <string.h>
@@ -100,19 +101,16 @@ main (int argc, char *argv[])
ro = json_node_get_object (root);
json_object_iter_init (&iter, ro);
names = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
names = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify)json_object_unref);
name_key = g_string_new ("");
while (json_object_iter_next (&iter, &name, &node))
{
JsonObject *obj = json_node_get_object (node);
const char *unicode;
const char *shortname;
unicode = json_object_get_string_member (obj, "unicode");
shortname = json_object_get_string_member (obj, "shortname");
g_hash_table_insert (names, g_strdup (unicode), g_strdup (shortname));
g_hash_table_insert (names, g_strdup (unicode), json_object_ref (obj));
}
g_object_unref (parser);
@@ -129,19 +127,22 @@ main (int argc, char *argv[])
array = json_node_get_array (root);
length = json_array_get_length (array);
g_variant_builder_init (&builder, G_VARIANT_TYPE ("a(auss)"));
g_variant_builder_init (&builder, G_VARIANT_TYPE ("a(aussas)"));
i = 0;
while (i < length)
{
JsonNode *node = json_array_get_element (array, i);
JsonObject *obj = json_node_get_object (node);
GVariantBuilder b1;
GVariantBuilder b2;
const char *name;
const char *shortname;
char *code;
int j;
int j, k;
gboolean skip;
gboolean has_variations;
JsonObject *obj2;
JsonArray *kw;
i++;
@@ -177,9 +178,19 @@ main (int argc, char *argv[])
if (!parse_code (&b1, code, name_key))
return 1;
shortname = g_hash_table_lookup (names, name_key->str);
g_variant_builder_init (&b2, G_VARIANT_TYPE ("as"));
obj2 = g_hash_table_lookup (names, name_key->str);
if (obj2)
{
shortname = json_object_get_string_member (obj2, "shortname");
kw = json_object_get_array_member (obj2, "keywords");
for (k = 0; k < json_array_get_length (kw); k++)
g_variant_builder_add (&b2, "s", json_array_get_string_element (kw, k));
}
else
shortname = "";
g_variant_builder_add (&builder, "(auss)", &b1, name, shortname ? shortname : "");
g_variant_builder_add (&builder, "(aussas)", &b1, name, shortname, &b2);
}
v = g_variant_builder_end (&builder);

View File

@@ -184,8 +184,8 @@ add_recent_item (GtkEmojiChooser *chooser,
g_variant_ref (item);
g_variant_builder_init (&builder, G_VARIANT_TYPE ("a((auss)u)"));
g_variant_builder_add (&builder, "(@(auss)u)", item, modifier);
g_variant_builder_init (&builder, G_VARIANT_TYPE ("a((aussas)u)"));
g_variant_builder_add (&builder, "(@(aussas)u)", item, modifier);
children = gtk_container_get_children (GTK_CONTAINER (chooser->recent.box));
for (l = children, i = 1; l; l = l->next, i++)
@@ -205,7 +205,7 @@ add_recent_item (GtkEmojiChooser *chooser,
continue;
}
g_variant_builder_add (&builder, "(@(auss)u)", item2, modifier2);
g_variant_builder_add (&builder, "(@(aussas)u)", item2, modifier2);
}
g_list_free (children);
@@ -476,7 +476,7 @@ populate_emoji_chooser (gpointer data)
if (!chooser->data)
{
GBytes *bytes = g_resources_lookup_data ("/org/gtk/libgtk/emoji/emoji.data", 0, NULL);
chooser->data = g_variant_ref_sink (g_variant_new_from_bytes (G_VARIANT_TYPE ("a(auss)"), bytes, TRUE));
chooser->data = g_variant_ref_sink (g_variant_new_from_bytes (G_VARIANT_TYPE ("a(aussas)"), bytes, TRUE));
g_bytes_unref (bytes);
}
@@ -586,7 +586,9 @@ filter_func (GtkFlowBoxChild *child,
GVariant *emoji_data;
const char *text;
const char *name;
const char **keywords;
gboolean res;
int i;
res = TRUE;
@@ -603,6 +605,10 @@ filter_func (GtkFlowBoxChild *child,
g_variant_get_child (emoji_data, 1, "&s", &name);
res = g_str_match_string (text, name, TRUE);
g_variant_get_child (emoji_data, 3, "^a&s", &keywords);
for (i = 0; !res && keywords[i]; i++)
res = g_str_match_string (text, keywords[i], TRUE);
out:
if (res)
section->empty = FALSE;

View File

@@ -2,7 +2,7 @@
<schemalist>
<schema id='org.gtk.Settings.EmojiChooser' path='/org/gtk/settings/emoji-chooser/'>
<key name='recent-emoji' type='a((auss)u)'>
<key name='recent-emoji' type='a((aussas)u)'>
<default>[]</default>
<summary>Recently used Emoji</summary>
<description>
@@ -10,6 +10,9 @@
specified as an array of codepoints, name and shortname. The extra integer after this
pair is the code of the Fitzpatrick modifier to use in place of a 0 in the
codepoint array.
specified as an array of codepoints, name, shortname and keywords. The extra
integer after this pair is the code of the Fitzpatrick modifier to use in
place of a 0 in the codepoint array.
</description>
</key>
</schema>