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

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