Commit 84c821b5 authored by John Palmer's avatar John Palmer Committed by Commit Bot

Add support for keyboard interaction with undo window

Change-Id: I1bedf8383e3cd46d5e37d8a5778e47a060521f82
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2518526
Commit-Queue: John Palmer <jopalmer@chromium.org>
Reviewed-by: default avatarKeith Lee <keithlee@chromium.org>
Cr-Commit-Position: refs/heads/master@{#825588}
parent 6f7283f8
...@@ -31,10 +31,23 @@ void AutocorrectManager::MarkAutocorrectRange(const std::string& corrected_word, ...@@ -31,10 +31,23 @@ void AutocorrectManager::MarkAutocorrectRange(const std::string& corrected_word,
} }
} }
void AutocorrectManager::OnKeyEvent( bool AutocorrectManager::OnKeyEvent(
const InputMethodEngineBase::KeyboardEvent& event) { const InputMethodEngineBase::KeyboardEvent& event) {
if (event.type != "keydown") { if (event.type != "keydown") {
return; return false;
}
if (event.key == "Up" && window_visible) {
std::string error;
auto button = ui::ime::AssistiveWindowButton();
button.id = ui::ime::ButtonId::kUndo;
button.window_type = ui::ime::AssistiveWindowType::kUndoWindow;
engine_->SetButtonHighlighted(context_id_, button, true, &error);
button_highlighted = true;
return true;
}
if (event.key == "Enter" && window_visible && button_highlighted) {
UndoAutocorrect();
return true;
} }
if (key_presses_until_underline_hide_ > 0) { if (key_presses_until_underline_hide_ > 0) {
--key_presses_until_underline_hide_; --key_presses_until_underline_hide_;
...@@ -42,6 +55,7 @@ void AutocorrectManager::OnKeyEvent( ...@@ -42,6 +55,7 @@ void AutocorrectManager::OnKeyEvent(
if (key_presses_until_underline_hide_ == 0) { if (key_presses_until_underline_hide_ == 0) {
ClearUnderline(); ClearUnderline();
} }
return false;
} }
void AutocorrectManager::ClearUnderline() { void AutocorrectManager::ClearUnderline() {
...@@ -60,6 +74,8 @@ void AutocorrectManager::OnSurroundingTextChanged(const base::string16& text, ...@@ -60,6 +74,8 @@ void AutocorrectManager::OnSurroundingTextChanged(const base::string16& text,
chromeos::AssistiveWindowProperties properties; chromeos::AssistiveWindowProperties properties;
properties.type = ui::ime::AssistiveWindowType::kUndoWindow; properties.type = ui::ime::AssistiveWindowType::kUndoWindow;
properties.visible = true; properties.visible = true;
window_visible = true;
button_highlighted = false;
engine_->SetAssistiveWindowProperties(context_id_, properties, &error); engine_->SetAssistiveWindowProperties(context_id_, properties, &error);
key_presses_until_underline_hide_ = kKeysUntilAutocorrectWindowHides; key_presses_until_underline_hide_ = kKeysUntilAutocorrectWindowHides;
...@@ -67,7 +83,8 @@ void AutocorrectManager::OnSurroundingTextChanged(const base::string16& text, ...@@ -67,7 +83,8 @@ void AutocorrectManager::OnSurroundingTextChanged(const base::string16& text,
chromeos::AssistiveWindowProperties properties; chromeos::AssistiveWindowProperties properties;
properties.type = ui::ime::AssistiveWindowType::kUndoWindow; properties.type = ui::ime::AssistiveWindowType::kUndoWindow;
properties.visible = false; properties.visible = false;
window_visible = false;
button_highlighted = false;
engine_->SetAssistiveWindowProperties(context_id_, properties, &error); engine_->SetAssistiveWindowProperties(context_id_, properties, &error);
} }
} }
...@@ -82,6 +99,8 @@ void AutocorrectManager::UndoAutocorrect() { ...@@ -82,6 +99,8 @@ void AutocorrectManager::UndoAutocorrect() {
chromeos::AssistiveWindowProperties properties; chromeos::AssistiveWindowProperties properties;
properties.type = ui::ime::AssistiveWindowType::kUndoWindow; properties.type = ui::ime::AssistiveWindowType::kUndoWindow;
properties.visible = false; properties.visible = false;
window_visible = false;
button_highlighted = false;
engine_->SetAssistiveWindowProperties(context_id_, properties, &error); engine_->SetAssistiveWindowProperties(context_id_, properties, &error);
const gfx::Range range = engine_->GetAutocorrectRange(); const gfx::Range range = engine_->GetAutocorrectRange();
const ui::SurroundingTextInfo surrounding_text = const ui::SurroundingTextInfo surrounding_text =
......
...@@ -36,8 +36,8 @@ class AutocorrectManager { ...@@ -36,8 +36,8 @@ class AutocorrectManager {
const std::string& typed_word, const std::string& typed_word,
int start_index); int start_index);
// To hide the underline after enough keypresses, this class intercepts // To hide the underline after enough keypresses, this class intercepts
// keystrokes. // keystrokes. Returns whether the keypress has now been handled.
void OnKeyEvent(const InputMethodEngineBase::KeyboardEvent& event); bool OnKeyEvent(const InputMethodEngineBase::KeyboardEvent& event);
// Indicates a new text field is focused, used to save context ID. // Indicates a new text field is focused, used to save context ID.
void OnFocus(int context_id); void OnFocus(int context_id);
// To show the undo window when cursor is in an autocorrected word, this class // To show the undo window when cursor is in an autocorrected word, this class
...@@ -54,6 +54,8 @@ class AutocorrectManager { ...@@ -54,6 +54,8 @@ class AutocorrectManager {
int context_id_ = -1; int context_id_ = -1;
InputMethodEngine* const engine_; InputMethodEngine* const engine_;
std::string last_typed_word_; std::string last_typed_word_;
bool window_visible = false;
bool button_highlighted = false;
}; };
} // namespace chromeos } // namespace chromeos
......
...@@ -258,7 +258,10 @@ void NativeInputMethodEngine::ImeObserver::OnKeyEvent( ...@@ -258,7 +258,10 @@ void NativeInputMethodEngine::ImeObserver::OnKeyEvent(
return; return;
} }
} }
autocorrect_manager_->OnKeyEvent(event); if (autocorrect_manager_->OnKeyEvent(event)) {
std::move(callback).Run(true);
return;
}
auto key_event = ime::mojom::PhysicalKeyEvent::New( auto key_event = ime::mojom::PhysicalKeyEvent::New(
event.type == "keydown" ? ime::mojom::KeyEventType::kKeyDown event.type == "keydown" ? ime::mojom::KeyEventType::kKeyDown
: ime::mojom::KeyEventType::kKeyUp, : ime::mojom::KeyEventType::kKeyUp,
......
...@@ -748,6 +748,45 @@ IN_PROC_BROWSER_TEST_F(NativeInputMethodEngineTest, RevertsAutocorrect) { ...@@ -748,6 +748,45 @@ IN_PROC_BROWSER_TEST_F(NativeInputMethodEngineTest, RevertsAutocorrect) {
SetFocus(nullptr); SetFocus(nullptr);
} }
IN_PROC_BROWSER_TEST_F(NativeInputMethodEngineTest,
RevertsAutocorrectWithKeyboard) {
engine_.Enable(kEngineIdUs);
chromeos::TextInputTestHelper helper(GetBrowserInputMethod());
SetUpTextInput(helper);
const base::string16 corrected_text = base::UTF8ToUTF16("corrected");
const base::string16 typed_text = base::UTF8ToUTF16("typed");
helper.GetTextInputClient()->InsertText(corrected_text);
helper.WaitForSurroundingTextChanged(corrected_text);
EXPECT_EQ(ui::IMEBridge::Get()
->GetInputContextHandler()
->GetSurroundingTextInfo()
.surrounding_text,
corrected_text);
engine_.OnAutocorrect("typed", "corrected", 0);
// Move cursor into the corrected word, sending VKEY_LEFT fails, so use JS.
content::WebContents* tab =
browser()->tab_strip_model()->GetActiveWebContents();
ASSERT_TRUE(content::ExecuteScript(
tab, "document.getElementById('text_id').setSelectionRange(2,2)"));
helper.WaitForSurroundingTextChanged(corrected_text, gfx::Range(2, 2));
DispatchKeyPress(ui::VKEY_UP, false);
DispatchKeyPress(ui::VKEY_RETURN, false);
helper.WaitForSurroundingTextChanged(typed_text);
EXPECT_EQ(ui::IMEBridge::Get()
->GetInputContextHandler()
->GetSurroundingTextInfo()
.surrounding_text,
typed_text);
SetFocus(nullptr);
}
class NativeInputMethodEngineAssistiveOff : public InProcessBrowserTest { class NativeInputMethodEngineAssistiveOff : public InProcessBrowserTest {
public: public:
NativeInputMethodEngineAssistiveOff() { 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