From c38e925eba2b1f7af39696e2f64ec1eaea94b00b Mon Sep 17 00:00:00 2001 From: Takao Fujiwara Date: Thu, 30 Sep 2021 09:36:12 -0400 Subject: [PATCH] Backport IBus Unicode feature --- configure.ac | 10 +++++++ ui/gtk3/Makefile.am | 5 +++- ui/gtk3/emojier.vala | 37 +++++++++++++++-------- ui/gtk3/emojierapp.vala | 63 ++++++++++++++++++++++++++++----------- ui/gtk3/panelbinding.vala | 13 ++++++-- 5 files changed, 93 insertions(+), 35 deletions(-) diff --git a/configure.ac b/configure.ac index 46ab7a9..26bb357 100644 --- a/configure.ac +++ b/configure.ac @@ -237,12 +237,21 @@ if test x"$enable_gtk3" = x"yes"; then PKG_CHECK_MODULES(GTK3, [ gtk+-3.0 ]) + PKG_CHECK_EXISTS([gdk-wayland-3.0], + [enable_gdk3_wayland=yes], + [enable_gdk3_wayland=no] + ) gtk3_binary_version=`$PKG_CONFIG --variable=gtk_binary_version gtk+-3.0` GTK3_IM_MODULEDIR="$libdir"/gtk-3.0/$gtk3_binary_version/immodules else enable_gtk3="no (disabled, use --enable-gtk3 to enable)" + enable_gdk3_wayland=no +fi +if test x"$enable_gdk3_wayland" != x"yes"; then + enable_gdk3_wayland="no (disabled, need to install gdk-wayland-3.0.pc)" fi +AM_CONDITIONAL([ENABLE_GDK3_WAYLAND], [test x"$enable_gdk3_wayland" = x"yes"]) if test x"$enable_xim" = x"yes"; then # Check for x11 @@ -796,6 +805,7 @@ Build options: Build gtk3 immodule $enable_gtk3 Build XIM agent server $enable_xim Build wayland support $enable_wayland + Build gdk3 wayland support $enable_gdk3_wayland Build appindicator support $enable_appindicator Build appindicator engine icon $enable_appindicator_engine_icon Build python library $enable_python_library diff --git a/ui/gtk3/Makefile.am b/ui/gtk3/Makefile.am index aaba7a4..6ebc96c 100644 --- a/ui/gtk3/Makefile.am +++ b/ui/gtk3/Makefile.am @@ -78,7 +78,6 @@ AM_VALAFLAGS = \ --pkg=ibus-1.0 \ --pkg=config \ --pkg=xi \ - --pkg=gdk-wayland \ --target-glib="$(VALA_TARGET_GLIB_VERSION)" \ $(NULL) @@ -105,6 +104,10 @@ if ENABLE_APPINDICATOR_ENGINE_ICON AM_VALAFLAGS += --define=INDICATOR_ENGINE_ICON endif +if ENABLE_GDK3_WAYLAND +AM_VALAFLAGS += --pkg=gdk-wayland --define=USE_GDK_WAYLAND +endif + libexec_PROGRAMS = ibus-ui-gtk3 ibus_ui_gtk3_SOURCES = \ 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 + * Copyright (c) 2017-2019 Takao Fujiwara * * 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 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' to `GLib.SList?' + GLib.SList 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? annotations = - data.get_annotations(); + GLib.SList 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 annotations = data.get_annotations(); + GLib.SList 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 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..7bc7b42 100644 --- a/ui/gtk3/emojierapp.vala +++ b/ui/gtk3/emojierapp.vala @@ -3,6 +3,7 @@ * ibus - The Input Bus * * Copyright (c) 2017 Peng Wu + * Copyright (c) 2017-2019 Takao Fujiwara * * 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 - * Copyright(c) 2018 Takao Fujwiara + * Copyright(c) 2018-2020 Takao Fujwiara * * 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.27.0