From 37ae2afafd76d27b6aedab75269aca7234e03b54 Mon Sep 17 00:00:00 2001
From: fujiwarat <takao.fujiwara1@gmail.com>
Date: Tue, 17 Jul 2018 16:29:53 +0900
Subject: [PATCH] Do not commit text when reset signal is received

ibus-hangul uses ibus_engine_update_preedit_text_with_mode() to
commit the preedit text by ibus-daemon and now focus-out and
mouse click are handled by ibus-daemon and ibus clients.

BUG=https://github.com/ibus/ibus/issues/1980
---
 src/engine.c | 29 ++++++++++++++++++-----------
 1 file changed, 18 insertions(+), 11 deletions(-)

diff --git a/src/engine.c b/src/engine.c
index f37ff97..1fce067 100644
--- a/src/engine.c
+++ b/src/engine.c
@@ -129,7 +129,8 @@ static void ibus_hangul_engine_candidate
                                              guint                   button,
                                              guint                   state);
 
-static void ibus_hangul_engine_flush        (IBusHangulEngine       *hangul);
+static void ibus_hangul_engine_flush        (IBusHangulEngine       *hangul,
+                                             gboolean                is_commit);
 static void ibus_hangul_engine_clear_preedit_text
                                             (IBusHangulEngine       *hangul);
 static void ibus_hangul_engine_update_preedit_text
@@ -496,6 +497,7 @@ ibus_hangul_engine_update_preedit_text (
                 0x00000000, preedit_len, -1);
         ibus_engine_update_preedit_text_with_mode ((IBusEngine *)hangul,
                                                    text,
+                                                   //0,
                                                    ibus_text_get_length (text),
                                                    TRUE,
                                                    preedit_option);
@@ -974,7 +976,7 @@ ibus_hangul_engine_process_key_event (IB
 
     if (hotkey_list_match(&hangul_keys, keyval, modifiers)) {
         if (hangul->hangul_mode)
-            ibus_hangul_engine_flush (hangul);
+            ibus_hangul_engine_flush (hangul, TRUE);
 
         hangul->hangul_mode = !hangul->hangul_mode;
         if (hangul->hangul_mode) {
@@ -1022,7 +1024,7 @@ ibus_hangul_engine_process_key_event (IB
     mask = IBUS_CONTROL_MASK |
 	    IBUS_MOD1_MASK | IBUS_MOD3_MASK | IBUS_MOD4_MASK | IBUS_MOD5_MASK;
     if (modifiers & mask) {
-        ibus_hangul_engine_flush (hangul);
+        ibus_hangul_engine_flush (hangul, TRUE);
         return FALSE;
     }
 
@@ -1112,17 +1114,17 @@ ibus_hangul_engine_process_key_event (IB
         }
 
         if (!retval)
-            ibus_hangul_engine_flush (hangul);
+            ibus_hangul_engine_flush (hangul, TRUE);
     }
 
     return retval;
 }
 
 static void
-ibus_hangul_engine_flush (IBusHangulEngine *hangul)
+ibus_hangul_engine_flush (IBusHangulEngine *hangul,
+                          gboolean          is_commit)
 {
     const gunichar *str;
-    IBusText *text;
 
     ibus_hangul_engine_hide_lookup_table (hangul);
 
@@ -1133,11 +1135,12 @@ ibus_hangul_engine_flush (IBusHangulEngi
     if (ustring_length (hangul->preedit) != 0) {
         /* clear preedit text before commit */
         ibus_hangul_engine_clear_preedit_text (hangul);
-
 	str = ustring_begin (hangul->preedit);
-	text = ibus_text_new_from_ucs4 (str);
 
-	ibus_engine_commit_text ((IBusEngine *) hangul, text);
+       if (is_commit) {
+	    IBusText *text = ibus_text_new_from_ucs4 (str);
+	    ibus_engine_commit_text ((IBusEngine *) hangul, text);
+       }
 
 	ustring_clear(hangul->preedit);
     }
@@ -1197,7 +1200,11 @@ ibus_hangul_engine_reset (IBusEngine *en
 {
     IBusHangulEngine *hangul = (IBusHangulEngine *) engine;
 
-    ibus_hangul_engine_flush (hangul);
+    // ibus-hangul uses
+    // ibus_engine_update_preedit_text_with_mode() function which makes
+    // the preedit string committed automatically when the reset is received
+    // So we don't need to commit the preedit here.
+    ibus_hangul_engine_flush (hangul, FALSE);
     parent_class->reset (engine);
 }
 
@@ -1288,7 +1295,7 @@ ibus_hangul_engine_property_activate (IB
         }
 
         ibus_engine_update_property (engine, hangul->prop_hangul_mode);
-        ibus_hangul_engine_flush (hangul);
+        ibus_hangul_engine_flush (hangul, TRUE);
     } else if (strcmp(prop_name, "hanja_mode") == 0) {
         IBusHangulEngine *hangul = (IBusHangulEngine *) engine;
 
@@ -1302,7 +1309,7 @@ ibus_hangul_engine_property_activate (IB
         }
 
         ibus_engine_update_property (engine, hangul->prop_hanja_mode);
-        ibus_hangul_engine_flush (hangul);
+        ibus_hangul_engine_flush (hangul, TRUE);
     }
 }
 
-- 
2.17.1

