Blame SOURCES/ibus-1897548-emoji-unicode.patch

1892be
From cddde2dbcbb4d78a32c342c7416aef9a5c5eb7cd Mon Sep 17 00:00:00 2001
1892be
From: Takao Fujiwara <fujiwara@redhat.com>
1892be
Date: Thu, 8 Jul 2021 03:58:09 -0400
1892be
Subject: [PATCH] Backport IBus Unicode feature
1892be
1892be
---
1892be
 ui/gtk3/emojier.vala      | 37 ++++++++++++++--------
1892be
 ui/gtk3/emojierapp.vala   | 66 +++++++++++++++++++++++++++------------
1892be
 ui/gtk3/panelbinding.vala | 13 ++++++--
1892be
 3 files changed, 80 insertions(+), 36 deletions(-)
1892be
1892be
diff --git a/ui/gtk3/emojier.vala b/ui/gtk3/emojier.vala
1892be
index 3eac2f2..9e6e926 100644
1892be
--- a/ui/gtk3/emojier.vala
1892be
+++ b/ui/gtk3/emojier.vala
1892be
@@ -2,7 +2,7 @@
1892be
  *
1892be
  * ibus - The Input Bus
1892be
  *
1892be
- * Copyright (c) 2017-2018 Takao Fujiwara <takao.fujiwara1@gmail.com>
1892be
+ * Copyright (c) 2017-2019 Takao Fujiwara <takao.fujiwara1@gmail.com>
1892be
  *
1892be
  * This library is free software; you can redistribute it and/or
1892be
  * modify it under the terms of the GNU Lesser General Public
1892be
@@ -320,6 +320,7 @@ public class IBusEmojier : Gtk.ApplicationWindow {
1892be
 
1892be
     public signal void candidate_clicked(uint index, uint button, uint state);
1892be
     public signal void commit_text(string text);
1892be
+    public signal void cancel();
1892be
 
1892be
     public IBusEmojier() {
1892be
         GLib.Object(
1892be
@@ -864,7 +865,7 @@ public class IBusEmojier : Gtk.ApplicationWindow {
1892be
         row.get_allocation(out alloc);
1892be
         var adjustment = m_scrolled_window.get_vadjustment();
1892be
         adjustment.clamp_page(alloc.y, alloc.y + alloc.height);
1892be
-        return_val_if_fail(m_category_active_index >= 0, false);
1892be
+        return_if_fail(m_category_active_index >= 0);
1892be
         m_lookup_table.set_cursor_pos((uint)m_category_active_index);
1892be
     }
1892be
 
1892be
@@ -936,8 +937,13 @@ public class IBusEmojier : Gtk.ApplicationWindow {
1892be
             update_unicode_blocks();
1892be
             return;
1892be
         } else {
1892be
-            unowned GLib.SList<unowned string> emojis =
1892be
-                    m_category_to_emojis_dict.lookup(category);
1892be
+            // Use copy_deep() since vala 0.43.4 does not allow to assign
1892be
+            // a weak pointer to the full one in SList:
1892be
+            // emojier.vala:885.48-886.62: error: Assignment: Cannot convert
1892be
+            // from `GLib.SList<string>' to `GLib.SList<weak string>?'
1892be
+            GLib.SList<string> emojis =
1892be
+                    m_category_to_emojis_dict.lookup(category).copy_deep(
1892be
+                            GLib.strdup);
1892be
             m_lookup_table.clear();
1892be
             m_candidate_panel_mode = true;
1892be
             foreach (unowned string emoji in emojis) {
1892be
@@ -1601,8 +1607,8 @@ public class IBusEmojier : Gtk.ApplicationWindow {
1892be
             m_vbox.add(widget);
1892be
             widget.show_all();
1892be
         }
1892be
-        unowned GLib.SList<unowned string>? annotations =
1892be
-                data.get_annotations();
1892be
+        GLib.SList<string> annotations =
1892be
+                data.get_annotations().copy_deep(GLib.strdup);
1892be
         var buff = new GLib.StringBuilder();
1892be
         int i = 0;
1892be
         foreach (unowned string annotation in annotations) {
1892be
@@ -1784,8 +1790,7 @@ public class IBusEmojier : Gtk.ApplicationWindow {
1892be
             show_emoji_variants(emojis);
1892be
             return true;
1892be
         }
1892be
-        if (m_input_context_path != "")
1892be
-            m_result = text;
1892be
+        m_result = text;
1892be
         if (need_commit_signal)
1892be
             commit_text(text);
1892be
         return false;
1892be
@@ -1892,6 +1897,7 @@ public class IBusEmojier : Gtk.ApplicationWindow {
1892be
             // PageUp/PageDown.
1892be
             remove_all_children();
1892be
         }
1892be
+        cancel();
1892be
         return false;
1892be
     }
1892be
 
1892be
@@ -2055,17 +2061,20 @@ public class IBusEmojier : Gtk.ApplicationWindow {
1892be
                     ) as IBus.EmojiData;
1892be
                 m_emoji_to_data_dict.insert(favorite, new_data);
1892be
             } else {
1892be
-                unowned GLib.SList<string> annotations = data.get_annotations();
1892be
+                GLib.SList<string> annotations =
1892be
+                        data.get_annotations().copy_deep(GLib.strdup);
1892be
                 if (annotations.find_custom(annotation, GLib.strcmp) == null) {
1892be
                     annotations.append(annotation);
1892be
-                    data.set_annotations(annotations.copy());
1892be
+                    data.set_annotations(annotations.copy_deep(GLib.strdup));
1892be
                 }
1892be
             }
1892be
             unowned GLib.SList<string> emojis =
1892be
                     m_annotation_to_emojis_dict.lookup(annotation);
1892be
             if (emojis.find_custom(favorite, GLib.strcmp) == null) {
1892be
                 emojis.append(favorite);
1892be
-                m_annotation_to_emojis_dict.replace(annotation, emojis.copy());
1892be
+                m_annotation_to_emojis_dict.replace(
1892be
+                        annotation,
1892be
+                        emojis.copy_deep(GLib.strdup));
1892be
             }
1892be
         }
1892be
     }
1892be
@@ -2117,7 +2126,7 @@ public class IBusEmojier : Gtk.ApplicationWindow {
1892be
     public string get_current_candidate() {
1892be
         // If category_list mode, do not show the category name on preedit.
1892be
         // If candidate_panel mode, the first space key does not show the
1892be
-        // lookup table but the first candidate is avaiable on preedit.
1892be
+        // lookup table but the first candidate is available on preedit.
1892be
         if (!m_candidate_panel_mode)
1892be
             return "";
1892be
         uint cursor = m_lookup_table.get_cursor_pos();
1892be
@@ -2139,11 +2148,13 @@ public class IBusEmojier : Gtk.ApplicationWindow {
1892be
                         ncandidates));
1892be
         int char_count = text.text.char_count();
1892be
         int start_index = -1;
1892be
+        unowned string title = text.text;
1892be
         for (int i = 0; i < char_count; i++) {
1892be
-            if (text.text.utf8_offset(i).has_prefix(language)) {
1892be
+            if (title.has_prefix(language)) {
1892be
                 start_index = i;
1892be
                 break;
1892be
             }
1892be
+            title = title.next_char();
1892be
         }
1892be
         if (start_index >= 0) {
1892be
             var attr = new IBus.Attribute(
1892be
diff --git a/ui/gtk3/emojierapp.vala b/ui/gtk3/emojierapp.vala
1892be
index fab99d9..783c611 100644
1892be
--- a/ui/gtk3/emojierapp.vala
1892be
+++ b/ui/gtk3/emojierapp.vala
1892be
@@ -3,6 +3,7 @@
1892be
  * ibus - The Input Bus
1892be
  *
1892be
  * Copyright (c) 2017 Peng Wu <alexepico@gmail.com>
1892be
+ * Copyright (c) 2017-2019 Takao Fujiwara <takao.fujiwara1@gmail.com>
1892be
  *
1892be
  * This library is free software; you can redistribute it and/or
1892be
  * modify it under the terms of the GNU Lesser General Public
1892be
@@ -40,6 +41,33 @@ public class EmojiApplication : Gtk.Application {
1892be
     }
1892be
 
1892be
 
1892be
+    private void save_selected_string(string? selected_string,
1892be
+                                      bool    cancelled) {
1892be
+        if (cancelled) {
1892be
+            m_command_line.print("%s\n", _("Canceled to choose an emoji."));
1892be
+            return;
1892be
+        }
1892be
+        GLib.return_if_fail(selected_string != null);
1892be
+        Gtk.Clipboard clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD);
1892be
+        clipboard.set_text(selected_string, -1);
1892be
+        clipboard.store();
1892be
+
1892be
+        var emojier_favorites = m_settings_emoji.get_strv("favorites");
1892be
+        bool has_favorite = false;
1892be
+        foreach (unowned string favorite in emojier_favorites) {
1892be
+            if (favorite == selected_string) {
1892be
+                has_favorite = true;
1892be
+                break;
1892be
+            }
1892be
+        }
1892be
+        if (!has_favorite) {
1892be
+            emojier_favorites += selected_string;
1892be
+            m_settings_emoji.set_strv("favorites", emojier_favorites);
1892be
+        }
1892be
+        m_command_line.print("%s\n", _("Copied an emoji to your clipboard."));
1892be
+    }
1892be
+
1892be
+
1892be
     private void show_dialog(ApplicationCommandLine command_line) {
1892be
         m_command_line = command_line;
1892be
         m_emojier.reset();
1892be
@@ -55,7 +83,7 @@ public class EmojiApplication : Gtk.Application {
1892be
             return;
1892be
         if (button == IBusEmojier.BUTTON_CLOSE_BUTTON) {
1892be
             m_emojier.hide();
1892be
-            m_command_line.print("%s\n", _("Canceled to choose an emoji."));
1892be
+            save_selected_string(null, true);
1892be
             m_command_line = null;
1892be
             return;
1892be
         }
1892be
@@ -74,23 +102,7 @@ public class EmojiApplication : Gtk.Application {
1892be
         }
1892be
         string emoji = m_emojier.get_current_candidate();
1892be
         m_emojier.hide();
1892be
-        Gtk.Clipboard clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD);
1892be
-        clipboard.set_text(emoji, -1);
1892be
-        clipboard.store();
1892be
-
1892be
-        var emojier_favorites = m_settings_emoji.get_strv("favorites");
1892be
-        bool has_favorite = false;
1892be
-        foreach (unowned string favorite in emojier_favorites) {
1892be
-            if (favorite == emoji) {
1892be
-                has_favorite = true;
1892be
-                break;
1892be
-            }
1892be
-        }
1892be
-        if (!has_favorite) {
1892be
-            emojier_favorites += emoji;
1892be
-            m_settings_emoji.set_strv("favorites", emojier_favorites);
1892be
-        }
1892be
-        m_command_line.print("%s\n", _("Copied an emoji to your clipboard."));
1892be
+        save_selected_string(emoji, false);
1892be
         m_command_line = null;
1892be
     }
1892be
 
1892be
@@ -202,6 +214,21 @@ public class EmojiApplication : Gtk.Application {
1892be
             m_emojier.candidate_clicked.connect((i, b, s) => {
1892be
                 candidate_clicked_lookup_table(i, b, s);
1892be
             });
1892be
+            m_emojier.cancel.connect(() => {
1892be
+                if (m_command_line == null)
1892be
+                    return;
1892be
+                m_emojier.hide();
1892be
+                save_selected_string(null, true);
1892be
+                m_command_line = null;
1892be
+            });
1892be
+            m_emojier.commit_text.connect(() => {
1892be
+                if (m_command_line == null)
1892be
+                    return;
1892be
+                m_emojier.hide();
1892be
+                string selected_string = m_emojier.get_selected_string();
1892be
+                save_selected_string(selected_string, false);
1892be
+                m_command_line = null;
1892be
+            });
1892be
         }
1892be
 
1892be
         activate_dialog(command_line);
1892be
diff --git a/ui/gtk3/panelbinding.vala b/ui/gtk3/panelbinding.vala
1892be
index cfedb2d..861255b 100644
1892be
--- a/ui/gtk3/panelbinding.vala
1892be
+++ b/ui/gtk3/panelbinding.vala
1892be
@@ -3,7 +3,7 @@
1892be
  * ibus - The Input Bus
1892be
  *
1892be
  * Copyright(c) 2018 Peng Huang <shawn.p.huang@gmail.com>
1892be
- * Copyright(c) 2018 Takao Fujwiara <takao.fujiwara1@gmail.com>
1892be
+ * Copyright(c) 2018-2020 Takao Fujwiara <takao.fujiwara1@gmail.com>
1892be
  *
1892be
  * This library is free software; you can redistribute it and/or
1892be
  * modify it under the terms of the GNU Lesser General Public
1892be
@@ -190,7 +190,7 @@ class Preedit : Gtk.Window {
1892be
 
1892be
     public IBus.Text get_commit_text() {
1892be
         string extension_text = m_extension_preedit_emoji.get_text();
1892be
-        if (extension_text.length == 0)
1892be
+        if (extension_text.length == 0 && m_prefix != "u")
1892be
             extension_text = m_extension_preedit_text.get_text();
1892be
         return new IBus.Text.from_string(extension_text);
1892be
     }
1892be
@@ -237,9 +237,14 @@ class PanelBinding : IBus.PanelService {
1892be
         GLib.Object(connection : bus.get_connection(),
1892be
                     object_path : IBus.PATH_PANEL_EXTENSION_EMOJI);
1892be
 
1892be
+#if USE_GDK_WAYLAND
1892be
         Type instance_type = Gdk.Display.get_default().get_type();
1892be
         Type wayland_type = typeof(GdkWayland.Display);
1892be
         m_is_wayland = instance_type.is_a(wayland_type);
1892be
+#else
1892be
+        m_is_wayland = false;
1892be
+        warning("Checking Wayland is disabled");
1892be
+#endif
1892be
 
1892be
         m_bus = bus;
1892be
         m_application = application;
1892be
@@ -551,8 +556,10 @@ class PanelBinding : IBus.PanelService {
1892be
 
1892be
     private bool key_press_keyval(uint keyval) {
1892be
         unichar ch = IBus.keyval_to_unicode(keyval);
1892be
+        if (m_extension_name == "unicode" && !ch.isxdigit())
1892be
+            return false;
1892be
         if (ch.iscntrl())
1892be
-                return false;
1892be
+            return false;
1892be
         string str = ch.to_string();
1892be
         m_preedit.append_text(str);
1892be
         string annotation = m_preedit.get_text();
1892be
-- 
1892be
2.18.2
1892be