Fix RemoveText issue in text controller
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / text-controller-text-updater.cpp
index 81adb8a..4d0ba8f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 // INTERNAL INCLUDES
 #include <dali-toolkit/internal/text/character-set-conversion.h>
+#include <dali-toolkit/internal/text/characters-helper-functions.h>
+#include <dali-toolkit/internal/text/emoji-helper.h>
 #include <dali-toolkit/internal/text/markup-processor.h>
 #include <dali-toolkit/internal/text/text-controller-impl.h>
+#include <dali-toolkit/internal/text/text-controller-placeholder-handler.h>
 #include <dali-toolkit/internal/text/text-editable-control-interface.h>
 
 namespace
@@ -55,7 +58,7 @@ void Controller::TextUpdater::SetText(Controller& controller, const std::string&
   ResetText(controller);
 
   // Remove the style.
-  controller.ClearStyleData();
+  impl.ClearStyleData();
 
   CharacterIndex lastCursorIndex = 0u;
 
@@ -69,6 +72,11 @@ void Controller::TextUpdater::SetText(Controller& controller, const std::string&
        (EventData::EDITING_WITH_GRAB_HANDLE == eventData->mState) ||
        (EventData::EDITING_WITH_PASTE_POPUP == eventData->mState))
     {
+      if((impl.mSelectableControlInterface != nullptr) && (EventData::SELECTING == eventData->mState))
+      {
+        impl.mSelectableControlInterface->SelectionChanged(eventData->mLeftSelectionPosition, eventData->mRightSelectionPosition, eventData->mPrimaryCursorPosition, eventData->mPrimaryCursorPosition);
+      }
+
       impl.ChangeState(EventData::EDITING);
     }
   }
@@ -82,7 +90,12 @@ void Controller::TextUpdater::SetText(Controller& controller, const std::string&
     MarkupProcessData markupProcessData(logicalModel->mColorRuns,
                                         logicalModel->mFontDescriptionRuns,
                                         logicalModel->mEmbeddedItems,
-                                        logicalModel->mAnchors);
+                                        logicalModel->mAnchors,
+                                        logicalModel->mUnderlinedCharacterRuns,
+                                        logicalModel->mBackgroundColorRuns,
+                                        logicalModel->mStrikethroughCharacterRuns,
+                                        logicalModel->mBoundedParagraphRuns,
+                                        logicalModel->mCharacterSpacingCharacterRuns);
 
     Length         textSize = 0u;
     const uint8_t* utf8     = NULL;
@@ -134,14 +147,16 @@ void Controller::TextUpdater::SetText(Controller& controller, const std::string&
   }
   else
   {
-    controller.ShowPlaceholderText();
+    PlaceholderHandler::ShowPlaceholderText(impl);
   }
 
+  unsigned int oldCursorPos = (nullptr != eventData ? eventData->mPrimaryCursorPosition : 0);
+
   // Resets the cursor position.
   controller.ResetCursorPosition(lastCursorIndex);
 
   // Scrolls the text to make the cursor visible.
-  controller.ResetScrollPosition();
+  impl.ResetScrollPosition();
 
   impl.RequestRelayout();
 
@@ -154,6 +169,7 @@ void Controller::TextUpdater::SetText(Controller& controller, const std::string&
   // Do this last since it provides callbacks into application code.
   if(NULL != impl.mEditableControlInterface)
   {
+    impl.mEditableControlInterface->CursorPositionChanged(oldCursorPos, lastCursorIndex);
     impl.mEditableControlInterface->TextChanged(true);
   }
 }
@@ -170,9 +186,10 @@ void Controller::TextUpdater::InsertText(Controller& controller, const std::stri
     return;
   }
 
-  bool removedPrevious  = false;
-  bool removedSelected  = false;
-  bool maxLengthReached = false;
+  bool         removedPrevious  = false;
+  bool         removedSelected  = false;
+  bool         maxLengthReached = false;
+  unsigned int oldCursorPos     = eventData->mPrimaryCursorPosition;
 
   DALI_LOG_INFO(gLogFilter, Debug::Verbose, "Controller::InsertText %p %s (%s) mPrimaryCursorPosition %d mPreEditFlag %d mPreEditStartPosition %d mPreEditLength %d\n", &controller, text.c_str(), (COMMIT == type ? "COMMIT" : "PRE_EDIT"), eventData->mPrimaryCursorPosition, eventData->mPreEditFlag, eventData->mPreEditStartPosition, eventData->mPreEditLength);
 
@@ -339,7 +356,7 @@ void Controller::TextUpdater::InsertText(Controller& controller, const std::stri
 
       if(addFontSizeRun)
       {
-        fontDescriptionRun.size        = static_cast<PointSize26Dot6>(inputStyle.size * impl.mFontSizeScale * 64.f);
+        fontDescriptionRun.size        = static_cast<PointSize26Dot6>(inputStyle.size * impl.GetFontSizeScale() * 64.f);
         fontDescriptionRun.sizeDefined = true;
       }
 
@@ -394,7 +411,7 @@ void Controller::TextUpdater::InsertText(Controller& controller, const std::stri
      impl.IsPlaceholderAvailable())
   {
     // Show place-holder if empty after removing the pre-edit text
-    controller.ShowPlaceholderText();
+    PlaceholderHandler::ShowPlaceholderText(impl);
     eventData->mUpdateCursorPosition = true;
     impl.ClearPreEditFlag();
   }
@@ -416,6 +433,11 @@ void Controller::TextUpdater::InsertText(Controller& controller, const std::stri
     }
   }
 
+  if(nullptr != impl.mEditableControlInterface)
+  {
+    impl.mEditableControlInterface->CursorPositionChanged(oldCursorPos, eventData->mPrimaryCursorPosition);
+  }
+
   if(maxLengthReached)
   {
     DALI_LOG_INFO(gLogFilter, Debug::Verbose, "MaxLengthReached (%d)\n", logicalModel->mText.Count());
@@ -451,6 +473,7 @@ bool Controller::TextUpdater::RemoveText(
   UpdateInputStyleType type)
 {
   bool removed = false;
+  bool removeAll = false;
 
   Controller::Impl& impl      = *controller.mImpl;
   EventData*&       eventData = impl.mEventData;
@@ -462,6 +485,7 @@ bool Controller::TextUpdater::RemoveText(
 
   ModelPtr&        model        = impl.mModel;
   LogicalModelPtr& logicalModel = model->mLogicalModel;
+  VisualModelPtr&  visualModel  = model->mVisualModel;
 
   DALI_LOG_INFO(gLogFilter, Debug::General, "Controller::RemoveText %p mText.Count() %d cursor %d cursorOffset %d numberOfCharacters %d\n", &controller, logicalModel->mText.Count(), eventData->mPrimaryCursorPosition, cursorOffset, numberOfCharacters);
 
@@ -479,14 +503,38 @@ bool Controller::TextUpdater::RemoveText(
       cursorIndex = eventData->mPrimaryCursorPosition + cursorOffset;
     }
 
+    //Handle Emoji clustering for cursor handling
+    // Deletion case: this is handling the deletion cases when the cursor is before or after Emoji
+    //  - Before: when use delete key and cursor is before Emoji (cursorOffset = -1)
+    //  - After: when use backspace key and cursor is after Emoji (cursorOffset = 0)
+
+    const Script script = logicalModel->GetScript(cursorIndex);
+    if((numberOfCharacters == 1u) &&
+       (IsOneOfEmojiScripts(script)))
+    {
+      //TODO: Use this clustering for Emoji cases only. This needs more testing to generalize to all scripts.
+      CharacterRun emojiClusteredCharacters = RetrieveClusteredCharactersOfCharacterIndex(visualModel, logicalModel, cursorIndex);
+      Length       actualNumberOfCharacters = emojiClusteredCharacters.numberOfCharacters;
+
+      //Set cursorIndex at the first characterIndex of clustred Emoji
+      cursorIndex = emojiClusteredCharacters.characterIndex;
+
+      numberOfCharacters = actualNumberOfCharacters;
+    }
+
     if((cursorIndex + numberOfCharacters) > currentText.Count())
     {
       numberOfCharacters = currentText.Count() - cursorIndex;
     }
 
+    if((cursorIndex == 0) && (currentText.Count() - numberOfCharacters == 0))
+    {
+      removeAll = true;
+    }
+
     TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
 
-    if(eventData->mPreEditFlag || // If the preedit flag is enabled, it means two (or more) of them came together i.e. when two keys have been pressed at the same time.
+    if(eventData->mPreEditFlag || removeAll || // If the preedit flag is enabled, it means two (or more) of them came together i.e. when two keys have been pressed at the same time.
        ((cursorIndex + numberOfCharacters) <= textUpdateInfo.mPreviousNumberOfCharacters))
     {
       // Mark the paragraphs to be updated.
@@ -532,7 +580,7 @@ bool Controller::TextUpdater::RemoveText(
 
       // If the number of current text and the number of characters to be deleted are same,
       // it means all texts should be removed and all Preedit variables should be initialized.
-      if((currentText.Count() - numberOfCharacters == 0) && (cursorIndex == 0))
+      if(removeAll)
       {
         impl.ClearPreEditFlag();
         textUpdateInfo.mNumberOfCharactersToAdd = 0;
@@ -559,6 +607,11 @@ bool Controller::TextUpdater::RemoveText(
         RemoveTextAnchor(controller, cursorOffset, numberOfCharacters, previousCursorIndex);
       }
 
+      if(nullptr != impl.mEditableControlInterface)
+      {
+        impl.mEditableControlInterface->CursorPositionChanged(previousCursorIndex, cursorIndex);
+      }
+
       // Cursor position retreat
       previousCursorIndex = cursorIndex;
 
@@ -570,6 +623,7 @@ bool Controller::TextUpdater::RemoveText(
       }
 
       DALI_LOG_INFO(gLogFilter, Debug::General, "Controller::RemoveText %p removed %d\n", &controller, numberOfCharacters);
+      removeAll = false;
       removed = true;
     }
   }
@@ -586,6 +640,9 @@ bool Controller::TextUpdater::RemoveSelectedText(Controller& controller)
   if(EventData::SELECTING == impl.mEventData->mState)
   {
     std::string removedString;
+    uint32_t    oldSelStart = impl.mEventData->mLeftSelectionPosition;
+    uint32_t    oldSelEnd   = impl.mEventData->mRightSelectionPosition;
+
     impl.RetrieveSelection(removedString, true);
 
     if(!removedString.empty())
@@ -602,6 +659,11 @@ bool Controller::TextUpdater::RemoveSelectedText(Controller& controller)
 
         RemoveTextAnchor(controller, cursorOffset, numberOfCharacters, previousCursorIndex);
       }
+
+      if(impl.mSelectableControlInterface != nullptr)
+      {
+        impl.mSelectableControlInterface->SelectionChanged(oldSelStart, oldSelEnd, impl.mEventData->mPrimaryCursorPosition, impl.mEventData->mPrimaryCursorPosition);
+      }
     }
   }