From 58cdcd5dd23778c6fad9fa0da31b670ceff8eeeb Mon Sep 17 00:00:00 2001
From: Kitty Barnett <develop@catznip.com>
Date: Sun, 23 Oct 2022 17:47:51 +0200
Subject: [PATCH] Handle return and escape in the mini emoji helper

---
 indra/llui/llemojihelper.cpp           |  5 +++
 indra/llui/llemojihelper.h             |  2 +-
 indra/newview/llpanelemojicomplete.cpp | 48 +++++++++++++++++++++++---
 indra/newview/llpanelemojicomplete.h   |  1 +
 4 files changed, 51 insertions(+), 5 deletions(-)

diff --git a/indra/llui/llemojihelper.cpp b/indra/llui/llemojihelper.cpp
index d4c31ee9867..54c801ab7bd 100644
--- a/indra/llui/llemojihelper.cpp
+++ b/indra/llui/llemojihelper.cpp
@@ -98,6 +98,11 @@ void LLEmojiHelper::showHelper(LLUICtrl* hostctrl_p, S32 local_x, S32 local_y, c
 
 void LLEmojiHelper::hideHelper(const LLUICtrl* ctrl_p)
 {
+	if (ctrl_p && !isActive(ctrl_p))
+	{
+		return;
+	}
+
 	setHostCtrl(nullptr);
 }
 
diff --git a/indra/llui/llemojihelper.h b/indra/llui/llemojihelper.h
index 7ed042fc5ff..63f5c640c99 100644
--- a/indra/llui/llemojihelper.h
+++ b/indra/llui/llemojihelper.h
@@ -45,7 +45,7 @@ class LLEmojiHelper : public LLSingleton<LLEmojiHelper>
 	bool        isActive(const LLUICtrl* ctrl_p) const;
 	static bool isCursorInEmojiCode(const LLWString& wtext, S32 cursor_pos, S32* short_code_pos_p = nullptr);
 	void        showHelper(LLUICtrl* hostctrl_p, S32 local_x, S32 local_y, const std::string& short_code, std::function<void(LLWString)> commit_cb);
-	void        hideHelper(const LLUICtrl* ctrl_p);
+	void        hideHelper(const LLUICtrl* ctrl_p = nullptr);
 
 	// Eventing
 	bool handleKey(const LLUICtrl* ctrl_p, KEY key, MASK mask);
diff --git a/indra/newview/llpanelemojicomplete.cpp b/indra/newview/llpanelemojicomplete.cpp
index f6823befac5..61b08ad48dc 100644
--- a/indra/newview/llpanelemojicomplete.cpp
+++ b/indra/newview/llpanelemojicomplete.cpp
@@ -27,6 +27,7 @@
 #include "llviewerprecompiledheaders.h"
 
 #include "llemojidictionary.h"
+#include "llemojihelper.h"
 #include "llpanelemojicomplete.h"
 #include "lluictrlfactory.h"
 
@@ -101,9 +102,9 @@ BOOL LLPanelEmojiComplete::handleHover(S32 x, S32 y, MASK mask)
 
 BOOL LLPanelEmojiComplete::handleKey(KEY key, MASK mask, BOOL called_from_parent)
 {
+	bool handled = false;
 	if (MASK_NONE == mask)
 	{
-		bool handled = false;
 		switch (key)
 		{
 			case KEY_LEFT:
@@ -116,11 +117,24 @@ BOOL LLPanelEmojiComplete::handleKey(KEY key, MASK mask, BOOL called_from_parent
 				selectNext();
 				handled = true;
 				break;
+			case KEY_RETURN:
+				if (!mEmojis.empty())
+				{
+					LLWString wstr;
+					wstr.push_back(mEmojis.at(mCurSelected));
+					setValue(wstring_to_utf8str(wstr));
+					onCommit();
+					handled = true;
+				}
+				break;
 		}
-		return handled;
 	}
 
-	return false;
+	if (handled)
+	{
+		return TRUE;
+	}
+	return LLUICtrl::handleKey(key, mask, called_from_parent);
 }
 
 void LLPanelEmojiComplete::reshape(S32 width, S32 height, BOOL called_from_parent)
@@ -223,6 +237,26 @@ LLFloaterEmojiComplete::LLFloaterEmojiComplete(const LLSD& sdKey)
 	setIsChrome(true);
 }
 
+BOOL LLFloaterEmojiComplete::handleKey(KEY key, MASK mask, BOOL called_from_parent)
+{
+	bool handled = false;
+	if (MASK_NONE == mask)
+	{
+		switch (key)
+		{
+			case KEY_ESCAPE:
+				LLEmojiHelper::instance().hideHelper();
+				handled = true;
+				break;
+		}
+
+	}
+
+	if (handled)
+		return TRUE;
+	return LLFloater::handleKey(key, mask, called_from_parent);
+}
+
 void LLFloaterEmojiComplete::onOpen(const LLSD& key)
 {
 	mEmojiCtrl->setEmojiHint(key["hint"].asString());
@@ -231,9 +265,15 @@ void LLFloaterEmojiComplete::onOpen(const LLSD& key)
 BOOL LLFloaterEmojiComplete::postBuild()
 {
 	mEmojiCtrl = findChild<LLPanelEmojiComplete>("emoji_complete_ctrl");
+	mEmojiCtrl->setCommitCallback(
+		std::bind([&](const LLSD& sdValue)
+		{
+			setValue(sdValue);
+			onCommit();
+		}, std::placeholders::_2));
 	mEmojiCtrlHorz = getRect().getWidth() - mEmojiCtrl->getRect().getWidth();
 
-	return TRUE;
+	return LLFloater::postBuild();
 }
 
 void LLFloaterEmojiComplete::reshape(S32 width, S32 height, BOOL called_from_parent)
diff --git a/indra/newview/llpanelemojicomplete.h b/indra/newview/llpanelemojicomplete.h
index b389ac9d535..361a2dc9b7e 100644
--- a/indra/newview/llpanelemojicomplete.h
+++ b/indra/newview/llpanelemojicomplete.h
@@ -98,6 +98,7 @@ class LLFloaterEmojiComplete : public LLFloater
 	LLFloaterEmojiComplete(const LLSD& sdKey);
 
 public:
+	BOOL handleKey(KEY key, MASK mask, BOOL called_from_parent) override;
 	void onOpen(const LLSD& key) override;
 	BOOL postBuild() override;
 	void reshape(S32 width, S32 height, BOOL called_from_parent) override;
-- 
GitLab