From 3acb4caa0fb9d381be6cfbe1693ace389d90a16c Mon Sep 17 00:00:00 2001
From: Kitty Barnett <develop@catznip.com>
Date: Sun, 23 Oct 2022 16:18:22 +0200
Subject: [PATCH] Add emoji helper support to LLTextEditor

---
 indra/llui/lltexteditor.cpp | 21 ++++++++++++++++++---
 indra/llui/lltexteditor.h   |  2 ++
 2 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/indra/llui/lltexteditor.cpp b/indra/llui/lltexteditor.cpp
index 889940cf9ab..168c260c7d4 100644
--- a/indra/llui/lltexteditor.cpp
+++ b/indra/llui/lltexteditor.cpp
@@ -676,6 +676,21 @@ void LLTextEditor::selectByCursorPosition(S32 prev_cursor_pos, S32 next_cursor_p
 	endSelection();
 }
 
+void LLTextEditor::handleEmojiCommit(const LLWString& wstr)
+{
+	LLWString wtext(getWText()); S32 shortCodePos;
+	if (LLEmojiHelper::isCursorInEmojiCode(wtext, mCursorPos, &shortCodePos))
+	{
+		remove(shortCodePos, mCursorPos - shortCodePos, true);
+
+		auto styleParams = LLStyle::Params();
+		styleParams.font = LLFontGL::getFontEmoji();
+		insert(shortCodePos, wstr, false, new LLEmojiTextSegment(new LLStyle(styleParams), shortCodePos, shortCodePos + wstr.size(), *this));
+
+		setCursorPos(shortCodePos + 1);
+	}
+}
+
 BOOL LLTextEditor::handleMouseDown(S32 x, S32 y, MASK mask)
 {
 	BOOL	handled = FALSE;
@@ -1147,9 +1162,9 @@ void LLTextEditor::addChar(llwchar wc)
 		LLWString wtext(getWText()); S32 shortCodePos;
 		if (LLEmojiHelper::isCursorInEmojiCode(wtext, mCursorPos, &shortCodePos))
 		{
-			const LLRect cursorRect = getLocalRectFromDocIndex(mCursorPos);
-			const LLWString shortCode = wtext.substr(shortCodePos, mCursorPos);
-			LLEmojiHelper::instance().showHelper(this, cursorRect.mLeft, cursorRect.mTop, wstring_to_utf8str(shortCode));
+			const LLRect cursorRect = getLocalRectFromDocIndex(mCursorPos - 1);
+			const LLWString shortCode = wtext.substr(shortCodePos, mCursorPos - shortCodePos);
+			LLEmojiHelper::instance().showHelper(this, cursorRect.mLeft, cursorRect.mTop, wstring_to_utf8str(shortCode), std::bind(&LLTextEditor::handleEmojiCommit, this, std::placeholders::_1));
 		}
 	}
 
diff --git a/indra/llui/lltexteditor.h b/indra/llui/lltexteditor.h
index 04910b6f68a..4c8175a2865 100644
--- a/indra/llui/lltexteditor.h
+++ b/indra/llui/lltexteditor.h
@@ -92,6 +92,8 @@ class LLTextEditor :
 
 	static S32		spacesPerTab();
 
+	        void    handleEmojiCommit(const LLWString& wstr);
+
 	// mousehandler overrides
 	virtual BOOL	handleMouseDown(S32 x, S32 y, MASK mask);
 	virtual BOOL	handleMouseUp(S32 x, S32 y, MASK mask);
-- 
GitLab