1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/html_viewer/ime_controller.h"
7 #include "components/html_viewer/blink_input_events_type_converters.h"
8 #include "components/html_viewer/blink_text_input_type_converters.h"
9 #include "components/mus/public/cpp/view.h"
10 #include "third_party/WebKit/public/web/WebInputEvent.h"
11 #include "third_party/WebKit/public/web/WebWidget.h"
13 namespace html_viewer
{
15 ImeController::ImeController(mus::View
* view
, blink::WebWidget
* widget
)
16 : view_(view
), widget_(widget
) {}
18 ImeController::~ImeController() {}
20 void ImeController::ResetInputMethod() {
21 // TODO(penghuang): Reset IME.
24 void ImeController::DidHandleGestureEvent(const blink::WebGestureEvent
& event
,
25 bool event_cancelled
) {
26 // Called when a gesture event is handled.
30 if (event
.type
== blink::WebInputEvent::GestureTap
) {
31 const bool show_ime
= true;
32 UpdateTextInputState(show_ime
);
33 } else if (event
.type
== blink::WebInputEvent::GestureLongPress
) {
34 // Only show IME if the textfield contains text.
35 const bool show_ime
= !widget_
->textInputInfo().value
.isEmpty();
36 UpdateTextInputState(show_ime
);
40 void ImeController::DidUpdateTextOfFocusedElementByNonUserInput() {
41 // Called when value of focused textfield gets dirty, e.g. value is
42 // modified by script, not by user input.
43 const bool show_ime
= false;
44 UpdateTextInputState(show_ime
);
47 void ImeController::ShowImeIfNeeded() {
48 // Request the browser to show the IME for current input type.
49 const bool show_ime
= true;
50 UpdateTextInputState(show_ime
);
53 void ImeController::UpdateTextInputState(bool show_ime
) {
54 blink::WebTextInputInfo new_info
= widget_
->textInputInfo();
55 // Only show IME if the focused element is editable.
56 show_ime
= show_ime
&& new_info
.type
!= blink::WebTextInputTypeNone
;
57 if (show_ime
|| text_input_info_
!= new_info
) {
58 text_input_info_
= new_info
;
59 mojo::TextInputStatePtr state
= mojo::TextInputState::New();
60 state
->type
= mojo::ConvertTo
<mojo::TextInputType
>(new_info
.type
);
61 state
->flags
= new_info
.flags
;
62 state
->text
= mojo::String::From(new_info
.value
.utf8());
63 state
->selection_start
= new_info
.selectionStart
;
64 state
->selection_end
= new_info
.selectionEnd
;
65 state
->composition_start
= new_info
.compositionStart
;
66 state
->composition_end
= new_info
.compositionEnd
;
68 view_
->SetImeVisibility(true, state
.Pass());
70 view_
->SetTextInputState(state
.Pass());
74 } // namespace html_viewer