This source file includes following definitions.
- GetEngine
- weak_ptr_factory_
- OnFocus
- OnBlur
- OnUntranslatedIMEMessage
- ProcessKeyEventDone
- DispatchKeyEvent
- OnTextInputTypeChanged
- OnCaretBoundsChanged
- CancelComposition
- OnInputLocaleChanged
- GetInputLocale
- IsActive
- IsCandidatePopupOpen
- OnWillChangeFocusedClient
- OnDidChangeFocusedClient
- ConfirmCompositionText
- ResetContext
- UpdateContextFocusState
- ProcessKeyEventPostIME
- ProcessFilteredKeyPressEvent
- ProcessUnfilteredKeyPressEvent
- ProcessInputMethodResult
- NeedInsertChar
- HasInputMethodResult
- AbandonAllPendingKeyEvents
- CommitText
- UpdateCompositionText
- HidePreeditText
- DeleteSurroundingText
- ExecuteCharacterComposer
- ExtractCompositionText
#include "ui/base/ime/input_method_chromeos.h"
#include <algorithm>
#include <cstring>
#include <set>
#include <vector>
#include "base/basictypes.h"
#include "base/bind.h"
#include "base/i18n/char_iterator.h"
#include "base/logging.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/third_party/icu/icu_utf.h"
#include "chromeos/ime/composition_text.h"
#include "chromeos/ime/input_method_descriptor.h"
#include "chromeos/ime/input_method_manager.h"
#include "ui/base/ime/text_input_client.h"
#include "ui/events/event.h"
#include "ui/events/event_constants.h"
#include "ui/events/event_utils.h"
#include "ui/events/keycodes/keyboard_code_conversion.h"
#include "ui/events/keycodes/keyboard_code_conversion_x.h"
#include "ui/events/keycodes/keyboard_codes.h"
#include "ui/gfx/rect.h"
namespace {
chromeos::IMEEngineHandlerInterface* GetEngine() {
return chromeos::IMEBridge::Get()->GetCurrentEngineHandler();
}
}
namespace ui {
InputMethodChromeOS::InputMethodChromeOS(
internal::InputMethodDelegate* delegate)
: context_focused_(false),
composing_text_(false),
composition_changed_(false),
current_keyevent_id_(0),
previous_textinput_type_(TEXT_INPUT_TYPE_NONE),
weak_ptr_factory_(this) {
SetDelegate(delegate);
chromeos::IMEBridge::Get()->SetInputContextHandler(this);
UpdateContextFocusState();
OnInputMethodChanged();
}
InputMethodChromeOS::~InputMethodChromeOS() {
AbandonAllPendingKeyEvents();
context_focused_ = false;
ConfirmCompositionText();
OnInputMethodChanged();
chromeos::IMEBridge::Get()->SetInputContextHandler(NULL);
}
void InputMethodChromeOS::OnFocus() {
InputMethodBase::OnFocus();
UpdateContextFocusState();
}
void InputMethodChromeOS::OnBlur() {
ConfirmCompositionText();
InputMethodBase::OnBlur();
UpdateContextFocusState();
}
bool InputMethodChromeOS::OnUntranslatedIMEMessage(
const base::NativeEvent& event,
NativeEventResult* result) {
return false;
}
void InputMethodChromeOS::ProcessKeyEventDone(uint32 id,
ui::KeyEvent* event,
bool is_handled) {
if (pending_key_events_.find(id) == pending_key_events_.end())
return;
DCHECK(event);
if (event->type() == ET_KEY_PRESSED) {
if (is_handled) {
character_composer_.Reset();
} else {
is_handled = ExecuteCharacterComposer(*event);
}
}
if (event->type() == ET_KEY_PRESSED || event->type() == ET_KEY_RELEASED)
ProcessKeyEventPostIME(*event, is_handled);
pending_key_events_.erase(id);
}
bool InputMethodChromeOS::DispatchKeyEvent(const ui::KeyEvent& event) {
DCHECK(event.type() == ET_KEY_PRESSED || event.type() == ET_KEY_RELEASED);
DCHECK(system_toplevel_window_focused());
if (!context_focused_ || !GetEngine() ||
GetTextInputType() == TEXT_INPUT_TYPE_PASSWORD ) {
if (event.type() == ET_KEY_PRESSED) {
if (ExecuteCharacterComposer(event)) {
ProcessKeyEventPostIME(event, true);
return true;
}
ProcessUnfilteredKeyPressEvent(event);
} else {
DispatchKeyEventPostIME(event);
}
return true;
}
pending_key_events_.insert(current_keyevent_id_);
ui::KeyEvent* copied_event = new ui::KeyEvent(event);
GetEngine()->ProcessKeyEvent(
event,
base::Bind(&InputMethodChromeOS::ProcessKeyEventDone,
weak_ptr_factory_.GetWeakPtr(),
current_keyevent_id_,
base::Owned(copied_event)));
++current_keyevent_id_;
return true;
}
void InputMethodChromeOS::OnTextInputTypeChanged(
const TextInputClient* client) {
if (IsTextInputClientFocused(client)) {
ResetContext();
UpdateContextFocusState();
if (previous_textinput_type_ != client->GetTextInputType())
OnInputMethodChanged();
previous_textinput_type_ = client->GetTextInputType();
}
InputMethodBase::OnTextInputTypeChanged(client);
}
void InputMethodChromeOS::OnCaretBoundsChanged(const TextInputClient* client) {
if (!context_focused_ || !IsTextInputClientFocused(client))
return;
DCHECK(!IsTextInputTypeNone());
const gfx::Rect rect = GetTextInputClient()->GetCaretBounds();
gfx::Rect composition_head;
if (!GetTextInputClient()->GetCompositionCharacterBounds(0,
&composition_head)) {
composition_head = rect;
}
chromeos::IMECandidateWindowHandlerInterface* candidate_window =
chromeos::IMEBridge::Get()->GetCandidateWindowHandler();
if (!candidate_window)
return;
candidate_window->SetCursorBounds(rect, composition_head);
gfx::Range text_range;
gfx::Range selection_range;
base::string16 surrounding_text;
if (!GetTextInputClient()->GetTextRange(&text_range) ||
!GetTextInputClient()->GetTextFromRange(text_range, &surrounding_text) ||
!GetTextInputClient()->GetSelectionRange(&selection_range)) {
previous_surrounding_text_.clear();
previous_selection_range_ = gfx::Range::InvalidRange();
return;
}
if (previous_selection_range_ == selection_range &&
previous_surrounding_text_ == surrounding_text)
return;
previous_selection_range_ = selection_range;
previous_surrounding_text_ = surrounding_text;
if (!selection_range.IsValid()) {
return;
}
if (!GetEngine())
return;
GetEngine()->SetSurroundingText(base::UTF16ToUTF8(surrounding_text),
selection_range.start() - text_range.start(),
selection_range.end() - text_range.start());
}
void InputMethodChromeOS::CancelComposition(const TextInputClient* client) {
if (context_focused_ && IsTextInputClientFocused(client))
ResetContext();
}
void InputMethodChromeOS::OnInputLocaleChanged() {
}
std::string InputMethodChromeOS::GetInputLocale() {
return "";
}
bool InputMethodChromeOS::IsActive() {
return true;
}
bool InputMethodChromeOS::IsCandidatePopupOpen() const {
return false;
}
void InputMethodChromeOS::OnWillChangeFocusedClient(
TextInputClient* focused_before,
TextInputClient* focused) {
ConfirmCompositionText();
}
void InputMethodChromeOS::OnDidChangeFocusedClient(
TextInputClient* focused_before,
TextInputClient* focused) {
OnTextInputTypeChanged(focused);
UpdateContextFocusState();
OnCaretBoundsChanged(focused);
}
void InputMethodChromeOS::ConfirmCompositionText() {
TextInputClient* client = GetTextInputClient();
if (client && client->HasCompositionText())
client->ConfirmCompositionText();
ResetContext();
}
void InputMethodChromeOS::ResetContext() {
if (!context_focused_ || !GetTextInputClient())
return;
DCHECK(system_toplevel_window_focused());
composition_.Clear();
result_text_.clear();
composing_text_ = false;
composition_changed_ = false;
AbandonAllPendingKeyEvents();
if (GetEngine())
GetEngine()->Reset();
character_composer_.Reset();
}
void InputMethodChromeOS::UpdateContextFocusState() {
const bool old_context_focused = context_focused_;
const TextInputType current_text_input_type = GetTextInputType();
switch (current_text_input_type) {
case TEXT_INPUT_TYPE_NONE:
case TEXT_INPUT_TYPE_PASSWORD:
context_focused_ = false;
break;
default:
context_focused_ = true;
break;
}
chromeos::IMECandidateWindowHandlerInterface* candidate_window =
chromeos::IMEBridge::Get()->GetCandidateWindowHandler();
if (candidate_window)
candidate_window->FocusStateChanged(context_focused_);
if (!GetEngine())
return;
bool input_type_change =
(current_text_input_type != previous_textinput_type_);
if (old_context_focused && (!context_focused_ || input_type_change))
GetEngine()->FocusOut();
if (context_focused_ && (!old_context_focused || input_type_change)) {
chromeos::IMEEngineHandlerInterface::InputContext context(
current_text_input_type, GetTextInputMode());
GetEngine()->FocusIn(context);
OnCaretBoundsChanged(GetTextInputClient());
}
}
void InputMethodChromeOS::ProcessKeyEventPostIME(
const ui::KeyEvent& event,
bool handled) {
TextInputClient* client = GetTextInputClient();
if (!client) {
DispatchKeyEventPostIME(event);
return;
}
if (event.type() == ET_KEY_PRESSED && handled)
ProcessFilteredKeyPressEvent(event);
if (client != GetTextInputClient())
return;
if (HasInputMethodResult())
ProcessInputMethodResult(event, handled);
if (client != GetTextInputClient())
return;
if (event.type() == ET_KEY_PRESSED && !handled)
ProcessUnfilteredKeyPressEvent(event);
else if (event.type() == ET_KEY_RELEASED)
DispatchKeyEventPostIME(event);
}
void InputMethodChromeOS::ProcessFilteredKeyPressEvent(
const ui::KeyEvent& event) {
if (NeedInsertChar()) {
DispatchKeyEventPostIME(event);
} else {
const ui::KeyEvent fabricated_event(ET_KEY_PRESSED,
VKEY_PROCESSKEY,
event.flags(),
false);
DispatchKeyEventPostIME(fabricated_event);
}
}
void InputMethodChromeOS::ProcessUnfilteredKeyPressEvent(
const ui::KeyEvent& event) {
const TextInputClient* prev_client = GetTextInputClient();
DispatchKeyEventPostIME(event);
TextInputClient* client = GetTextInputClient();
if (!client || client != prev_client)
return;
const uint32 event_flags = event.flags();
uint16 ch = 0;
if (event.HasNativeEvent()) {
const base::NativeEvent& native_event = event.native_event();
if (!(event_flags & ui::EF_CONTROL_DOWN))
ch = ui::GetCharacterFromXEvent(native_event);
if (!ch) {
ch = ui::GetCharacterFromKeyCode(
ui::KeyboardCodeFromNative(native_event), event_flags);
}
} else {
ch = ui::GetCharacterFromKeyCode(event.key_code(), event_flags);
}
if (ch)
client->InsertChar(ch, event_flags);
}
void InputMethodChromeOS::ProcessInputMethodResult(const ui::KeyEvent& event,
bool handled) {
TextInputClient* client = GetTextInputClient();
DCHECK(client);
if (result_text_.length()) {
if (handled && NeedInsertChar()) {
for (base::string16::const_iterator i = result_text_.begin();
i != result_text_.end(); ++i) {
client->InsertChar(*i, event.flags());
}
} else {
client->InsertText(result_text_);
composing_text_ = false;
}
}
if (composition_changed_ && !IsTextInputTypeNone()) {
if (composition_.text.length()) {
composing_text_ = true;
client->SetCompositionText(composition_);
} else if (result_text_.empty()) {
client->ClearCompositionText();
}
}
result_text_.clear();
composition_changed_ = false;
}
bool InputMethodChromeOS::NeedInsertChar() const {
return GetTextInputClient() &&
(IsTextInputTypeNone() ||
(!composing_text_ && result_text_.length() == 1));
}
bool InputMethodChromeOS::HasInputMethodResult() const {
return result_text_.length() || composition_changed_;
}
void InputMethodChromeOS::AbandonAllPendingKeyEvents() {
pending_key_events_.clear();
}
void InputMethodChromeOS::CommitText(const std::string& text) {
if (text.empty())
return;
if (!GetTextInputClient())
return;
const base::string16 utf16_text = base::UTF8ToUTF16(text);
if (utf16_text.empty())
return;
result_text_.append(utf16_text);
if (pending_key_events_.empty() && !IsTextInputTypeNone()) {
GetTextInputClient()->InsertText(utf16_text);
result_text_.clear();
}
}
void InputMethodChromeOS::UpdateCompositionText(
const chromeos::CompositionText& text,
uint32 cursor_pos,
bool visible) {
if (IsTextInputTypeNone())
return;
if (!CanComposeInline()) {
chromeos::IMECandidateWindowHandlerInterface* candidate_window =
chromeos::IMEBridge::Get()->GetCandidateWindowHandler();
if (candidate_window)
candidate_window->UpdatePreeditText(text.text(), cursor_pos, visible);
}
if (!visible) {
HidePreeditText();
return;
}
ExtractCompositionText(text, cursor_pos, &composition_);
composition_changed_ = true;
if (composition_.text.length())
composing_text_ = true;
if (pending_key_events_.empty()) {
GetTextInputClient()->SetCompositionText(composition_);
composition_changed_ = false;
composition_.Clear();
}
}
void InputMethodChromeOS::HidePreeditText() {
if (composition_.text.empty() || IsTextInputTypeNone())
return;
composition_changed_ = true;
composition_.Clear();
if (pending_key_events_.empty()) {
TextInputClient* client = GetTextInputClient();
if (client && client->HasCompositionText())
client->ClearCompositionText();
composition_changed_ = false;
}
}
void InputMethodChromeOS::DeleteSurroundingText(int32 offset, uint32 length) {
if (!composition_.text.empty())
return;
if (offset < 0 && static_cast<uint32>(-1 * offset) != length)
return;
if (GetTextInputClient())
GetTextInputClient()->ExtendSelectionAndDelete(length, 0U);
}
bool InputMethodChromeOS::ExecuteCharacterComposer(const ui::KeyEvent& event) {
if (!character_composer_.FilterKeyPress(event))
return false;
chromeos::CompositionText preedit;
preedit.set_text(character_composer_.preedit_string());
UpdateCompositionText(preedit, preedit.text().size(),
!preedit.text().empty());
std::string commit_text =
base::UTF16ToUTF8(character_composer_.composed_character());
if (!commit_text.empty()) {
CommitText(commit_text);
}
return true;
}
void InputMethodChromeOS::ExtractCompositionText(
const chromeos::CompositionText& text,
uint32 cursor_position,
CompositionText* out_composition) const {
out_composition->Clear();
out_composition->text = text.text();
if (out_composition->text.empty())
return;
std::vector<size_t> char16_offsets;
size_t length = out_composition->text.length();
base::i18n::UTF16CharIterator char_iterator(&out_composition->text);
do {
char16_offsets.push_back(char_iterator.array_pos());
} while (char_iterator.Advance());
uint32 char_length = static_cast<uint32>(char16_offsets.size());
char16_offsets.push_back(length);
size_t cursor_offset =
char16_offsets[std::min(char_length, cursor_position)];
out_composition->selection = gfx::Range(cursor_offset);
const std::vector<chromeos::CompositionText::UnderlineAttribute>&
underline_attributes = text.underline_attributes();
if (!underline_attributes.empty()) {
for (size_t i = 0; i < underline_attributes.size(); ++i) {
const uint32 start = underline_attributes[i].start_index;
const uint32 end = underline_attributes[i].end_index;
if (start >= end)
continue;
CompositionUnderline underline(
char16_offsets[start], char16_offsets[end],
SK_ColorBLACK, false );
if (underline_attributes[i].type ==
chromeos::CompositionText::COMPOSITION_TEXT_UNDERLINE_DOUBLE)
underline.thick = true;
else if (underline_attributes[i].type ==
chromeos::CompositionText::COMPOSITION_TEXT_UNDERLINE_ERROR)
underline.color = SK_ColorRED;
out_composition->underlines.push_back(underline);
}
}
DCHECK(text.selection_start() <= text.selection_end());
if (text.selection_start() < text.selection_end()) {
const uint32 start = text.selection_start();
const uint32 end = text.selection_end();
CompositionUnderline underline(
char16_offsets[start], char16_offsets[end],
SK_ColorBLACK, true );
out_composition->underlines.push_back(underline);
if (underline.start_offset == cursor_offset) {
out_composition->selection.set_start(underline.end_offset);
out_composition->selection.set_end(cursor_offset);
} else if (underline.end_offset == cursor_offset) {
out_composition->selection.set_start(underline.start_offset);
out_composition->selection.set_end(cursor_offset);
}
}
if (out_composition->underlines.empty()) {
out_composition->underlines.push_back(CompositionUnderline(
0, length, SK_ColorBLACK, false ));
}
}
}