Commit 6cbffa8a authored by John Palmer's avatar John Palmer Committed by Commit Bot

Make assistive window show with autocorrect manager

Change-Id: I54079db9c196791727840498e5f5f6d83d583b6a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2483986
Commit-Queue: John Palmer <jopalmer@chromium.org>
Reviewed-by: default avatarDarren Shen <shend@chromium.org>
Reviewed-by: default avatarJing Wang <jiwan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#823769}
parent 5f1671f6
......@@ -5,12 +5,13 @@
#include "chrome/browser/chromeos/input_method/autocorrect_manager.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/chromeos/input_method/assistive_window_properties.h"
namespace chromeos {
const int kKeysUntilAutocorrectWindowHides = 4;
constexpr int kKeysUntilAutocorrectWindowHides = 4;
AutocorrectManager::AutocorrectManager(InputMethodEngineBase* engine)
AutocorrectManager::AutocorrectManager(InputMethodEngine* engine)
: engine_(engine) {}
void AutocorrectManager::MarkAutocorrectRange(const std::string& corrected_word,
......@@ -20,31 +21,50 @@ void AutocorrectManager::MarkAutocorrectRange(const std::string& corrected_word,
key_presses_until_underline_hide_ = kKeysUntilAutocorrectWindowHides;
ClearUnderline();
if (context_id_ != -1) {
std::string error;
// TODO(crbug/1111135): error handling
engine_->SetAutocorrectRange(context_id_, base::UTF8ToUTF16(corrected_word),
start_index, start_index, &error);
engine_->SetAutocorrectRange(base::UTF8ToUTF16(corrected_word), start_index,
start_index + corrected_word.length());
}
}
void AutocorrectManager::OnKeyEvent() {
if (key_presses_until_underline_hide_ < 0) {
void AutocorrectManager::OnKeyEvent(
const InputMethodEngineBase::KeyboardEvent& event) {
if (event.type != "keydown") {
return;
}
--key_presses_until_underline_hide_;
if (key_presses_until_underline_hide_ < 1) {
if (key_presses_until_underline_hide_ > 0) {
--key_presses_until_underline_hide_;
}
if (key_presses_until_underline_hide_ == 0) {
ClearUnderline();
}
}
void AutocorrectManager::ClearUnderline() {
// TODO(crbug/1111135): error handling
engine_->SetAutocorrectRange(/*autocorrect text=*/base::string16(),
/*start=*/0,
/*end=*/std::numeric_limits<uint32_t>::max());
// TODO(b/171924347): expose engine->clearAutocorrectRange() and use it here.
}
void AutocorrectManager::OnSurroundingTextChanged(const base::string16& text,
const int cursor_pos,
const int anchpr_pos) {
std::string error;
engine_->SetAutocorrectRange(
context_id_,
/*autocorrect_text=*/base::string16(),
/*start=*/0, /*end=*/std::numeric_limits<uint32_t>::max(), &error);
const gfx::Range range = engine_->GetAutocorrectRange();
if (!range.is_empty() && cursor_pos >= range.start() &&
cursor_pos <= range.end()) {
chromeos::AssistiveWindowProperties properties;
properties.type = ui::ime::AssistiveWindowType::kUndoWindow;
properties.visible = true;
engine_->SetAssistiveWindowProperties(context_id_, properties, &error);
key_presses_until_underline_hide_ = kKeysUntilAutocorrectWindowHides;
} else {
chromeos::AssistiveWindowProperties properties;
properties.type = ui::ime::AssistiveWindowType::kUndoWindow;
properties.visible = false;
engine_->SetAssistiveWindowProperties(context_id_, properties, &error);
}
}
void AutocorrectManager::OnFocus(int context_id) {
......
......@@ -24,7 +24,7 @@ class AutocorrectManager {
public:
// Engine is used to interact with the text field, and is assumed to be
// valid for the entire lifetime of the autocorrect manager.
explicit AutocorrectManager(InputMethodEngineBase* engine);
explicit AutocorrectManager(InputMethodEngine* engine);
AutocorrectManager(const AutocorrectManager&) = delete;
AutocorrectManager& operator=(const AutocorrectManager&) = delete;
......@@ -35,16 +35,21 @@ class AutocorrectManager {
void MarkAutocorrectRange(const std::string& corrected_word, int start_index);
// To hide the underline after enough keypresses, this class intercepts
// keystrokes.
void OnKeyEvent();
void OnKeyEvent(const InputMethodEngineBase::KeyboardEvent& event);
// Indicates a new text field is focused, used to save context ID.
void OnFocus(int context_id);
// To show the undo window when cursor is in an autocorrected word, this class
// is notified of surrounding text changes.
void OnSurroundingTextChanged(const base::string16& text,
int cursor_pos,
int anchor_pos);
private:
void ClearUnderline();
int key_presses_until_underline_hide_ = 0;
int context_id_ = -1;
InputMethodEngineBase* const engine_;
InputMethodEngine* const engine_;
};
} // namespace chromeos
......
......@@ -185,6 +185,8 @@ class InputMethodEngine : public InputMethodEngineBase,
uint32_t start,
uint32_t end) override;
gfx::Range GetAutocorrectRange() override;
private:
// InputMethodEngineBase:
void UpdateComposition(const ui::CompositionText& composition_text,
......@@ -199,7 +201,6 @@ class InputMethodEngine : public InputMethodEngineBase,
uint32_t end,
const std::vector<ui::ImeTextSpan>& text_spans) override;
gfx::Range GetAutocorrectRange() override;
gfx::Rect GetAutocorrectCharacterBounds() override;
......
......@@ -256,7 +256,7 @@ void NativeInputMethodEngine::ImeObserver::OnKeyEvent(
return;
}
}
autocorrect_manager_->OnKeyEvent();
autocorrect_manager_->OnKeyEvent(event);
auto key_event = ime::mojom::PhysicalKeyEvent::New(
event.type == "keydown" ? ime::mojom::KeyEventType::kKeyDown
: ime::mojom::KeyEventType::kKeyUp,
......@@ -313,6 +313,7 @@ void NativeInputMethodEngine::ImeObserver::OnSurroundingTextChanged(
assistive_suggester_->OnSurroundingTextChanged(text, cursor_pos,
anchor_pos);
}
autocorrect_manager_->OnSurroundingTextChanged(text, cursor_pos, anchor_pos);
if (ShouldUseFstMojoEngine(engine_id) && remote_to_engine_.is_bound()) {
auto selection = ime::mojom::SelectionRange::New();
selection->anchor = anchor_pos;
......
......@@ -12,6 +12,7 @@
#include "base/test/task_environment.h"
#include "base/values.h"
#include "chrome/browser/autofill/personal_data_manager_factory.h"
#include "chrome/browser/chromeos/input_method/assistive_window_controller.h"
#include "chrome/browser/chromeos/input_method/suggestion_enums.h"
#include "chrome/browser/chromeos/input_method/textinput_test_helper.h"
#include "chrome/browser/profiles/profile.h"
......@@ -214,6 +215,13 @@ class NativeInputMethodEngineTest : public InProcessBrowserTest,
waiterReleased.Wait();
}
void DispatchKeyPresses(const std::vector<ui::KeyboardCode>& codes,
bool need_flush) {
for (const ui::KeyboardCode& code : codes) {
DispatchKeyPress(code, need_flush);
}
}
void SetFocus(ui::TextInputClient* client) {
input_method_.SetFocusedTextInputClient(client);
}
......@@ -637,6 +645,72 @@ IN_PROC_BROWSER_TEST_F(NativeInputMethodEngineTest, DestroyProfile) {
EXPECT_EQ(engine_.GetPrefChangeRegistrarForTesting(), nullptr);
}
IN_PROC_BROWSER_TEST_F(NativeInputMethodEngineTest,
HighlightsOnAutocorrectThenDismissesHighlight) {
engine_.Enable(kEngineIdUs);
ui::DummyTextInputClient text_input_client(ui::TEXT_INPUT_TYPE_TEXT);
SetFocus(&text_input_client);
// Input the corrected word.
DispatchKeyPresses(
{
ui::VKEY_C,
ui::VKEY_O,
ui::VKEY_R,
ui::VKEY_R,
ui::VKEY_E,
ui::VKEY_C,
ui::VKEY_T,
ui::VKEY_E,
ui::VKEY_D,
},
false);
engine_.OnAutocorrect("typed", "corrected", 0);
EXPECT_FALSE(engine_.GetAutocorrectRange().is_empty());
DispatchKeyPress(ui::KeyboardCode::VKEY_A, false);
DispatchKeyPress(ui::KeyboardCode::VKEY_A, false);
DispatchKeyPress(ui::KeyboardCode::VKEY_A, false);
// Highlighting should only go away after 4 keypresses.
EXPECT_FALSE(engine_.GetAutocorrectRange().is_empty());
DispatchKeyPress(ui::KeyboardCode::VKEY_A, false);
EXPECT_TRUE(engine_.GetAutocorrectRange().is_empty());
SetFocus(nullptr);
}
IN_PROC_BROWSER_TEST_F(NativeInputMethodEngineTest,
ShowsAndHidesAutocorrectUndoWindow) {
engine_.Enable(kEngineIdUs);
chromeos::TextInputTestHelper helper(GetBrowserInputMethod());
SetUpTextInput(helper);
const base::string16 prefix_text = base::UTF8ToUTF16("corrected ");
helper.GetTextInputClient()->InsertText(prefix_text);
helper.WaitForSurroundingTextChanged(prefix_text);
engine_.OnAutocorrect("typed", "corrected", 0);
auto* controller =
((chromeos::input_method::
AssistiveWindowController*)(ui::IMEBridge::Get()
->GetAssistiveWindowHandler()));
EXPECT_FALSE(controller->GetUndoWindowForTesting());
// Move cursor back into the autocorrected word to show the window.
helper.GetTextInputClient()->ExtendSelectionAndDelete(1, 0);
helper.WaitForSurroundingTextChanged(base::UTF8ToUTF16("corrected"));
EXPECT_TRUE(controller->GetUndoWindowForTesting());
EXPECT_TRUE(controller->GetUndoWindowForTesting()->GetVisible());
SetFocus(nullptr);
}
class NativeInputMethodEngineAssistiveOff : public InProcessBrowserTest {
public:
NativeInputMethodEngineAssistiveOff() {
......
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