Commit 539f17b9 authored by Kenton Lam's avatar Kenton Lam Committed by Chromium LUCI CQ

Implement text insertion for emoji picker.

Gets and stores the current TextInputClient and selection range when the emoji dialog is opened. Inserting emoji will insert into that TextInputClient.
Because the emoji picker window closes when it loses focus, this *should* always be correct.

One edge case is if the TextInputClient disappears after the emoji picker is opened (one example of this is the launcher's search box).

Change-Id: I1fedd494a36603b252fcff3a5e9b0d8df63a004c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2626482Reviewed-by: default avatarJohn Palmer <jopalmer@chromium.org>
Reviewed-by: default avatarKeith Lee <keithlee@chromium.org>
Commit-Queue: Kenton Lam <kentonlam@google.com>
Cr-Commit-Position: refs/heads/master@{#845980}
parent 7084f3d9
......@@ -5,15 +5,26 @@
#include "chrome/browser/ui/webui/chromeos/emoji/emoji_dialog.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/browser_dialogs.h"
#include "chrome/browser/ui/webui/constrained_web_dialog_ui.h"
#include "chrome/common/url_constants.h"
#include "ui/base/ime/chromeos/ime_bridge.h"
namespace chromeos {
EmojiPickerDialog::EmojiPickerDialog() {}
ui::TextInputClient* EmojiPickerDialog::input_client = nullptr;
gfx::Range EmojiPickerDialog::selection_range = gfx::Range();
EmojiPickerDialog::EmojiPickerDialog() {
ui::InputMethod* input_method =
ui::IMEBridge::Get()->GetInputContextHandler()->GetInputMethod();
input_client = input_method->GetTextInputClient();
input_client->GetEditableSelectionRange(&selection_range);
}
void EmojiPickerDialog::Show() {
chrome::ShowWebDialog(nullptr, ProfileManager::GetActiveUserProfile(),
......@@ -50,6 +61,7 @@ void EmojiPickerDialog::OnDialogShown(content::WebUI* webui) {
}
void EmojiPickerDialog::OnDialogClosed(const std::string& json_retval) {
input_client = nullptr;
delete this;
}
......
......@@ -6,8 +6,11 @@
#define CHROME_BROWSER_UI_WEBUI_CHROMEOS_EMOJI_EMOJI_DIALOG_H_
#include "base/macros.h"
#include "ui/base/ime/text_input_client.h"
#include "ui/web_dialogs/web_dialog_delegate.h"
#include "chrome/browser/ui/webui/chromeos/emoji/emoji_handler.h"
namespace chromeos {
class EmojiPickerDialog : public ui::WebDialogDelegate {
......@@ -33,6 +36,12 @@ class EmojiPickerDialog : public ui::WebDialogDelegate {
content::WebUI* webui_ = nullptr;
// Input field which was focused before the EmojiDialog was opened.
static ui::TextInputClient* input_client;
// Selection range of input_client before the EmojiDialog was opened.
static gfx::Range selection_range;
friend class EmojiHandler;
DISALLOW_COPY_AND_ASSIGN(EmojiPickerDialog);
};
......
......@@ -5,10 +5,13 @@
#include "chrome/browser/ui/webui/chromeos/emoji/emoji_handler.h"
#include "base/logging.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/ui/webui/chromeos/emoji/emoji_dialog.h"
namespace chromeos {
EmojiHandler::EmojiHandler() = default;
EmojiHandler::EmojiHandler() : selection_range_set(false) {}
EmojiHandler::~EmojiHandler() = default;
void EmojiHandler::RegisterMessages() {
......@@ -24,8 +27,28 @@ void EmojiHandler::HandleInsertEmoji(const base::ListValue* args) {
return;
}
// Example usage:
// const std::string& emoji = args->GetList()[0].GetString();
const std::string& emoji = args->GetList()[0].GetString();
ui::TextInputClient* input_client = EmojiPickerDialog::input_client;
if (!input_client) {
LOG(WARNING) << "no input_client found";
return;
}
if (input_client->GetTextInputType() ==
ui::TextInputType::TEXT_INPUT_TYPE_NONE) {
LOG(WARNING) << "attempt to insert into input_client with type none";
}
if (!selection_range_set) {
input_client->SetEditableSelectionRange(EmojiPickerDialog::selection_range);
selection_range_set = true;
}
input_client->InsertText(
base::UTF8ToUTF16(emoji),
ui::TextInputClient::InsertTextCursorBehavior::kMoveCursorAfterText);
}
} // namespace chromeos
......@@ -22,6 +22,8 @@ class EmojiHandler : public content::WebUIMessageHandler {
void HandleInsertEmoji(const base::ListValue* args);
bool selection_range_set;
DISALLOW_COPY_AND_ASSIGN(EmojiHandler);
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment