Blob Blame History Raw
From cddde2dbcbb4d78a32c342c7416aef9a5c5eb7cd Mon Sep 17 00:00:00 2001
From: Takao Fujiwara <fujiwara@redhat.com>
Date: Thu, 8 Jul 2021 03:58:09 -0400
Subject: [PATCH] Backport IBus Unicode feature

---
 ui/gtk3/emojier.vala      | 37 ++++++++++++++--------
 ui/gtk3/emojierapp.vala   | 66 +++++++++++++++++++++++++++------------
 ui/gtk3/panelbinding.vala | 13 ++++++--
 3 files changed, 80 insertions(+), 36 deletions(-)

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