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/view_manager/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(mojo::View
* view
, blink::WebWidget
* widget
)
16 : view_(view
), widget_(widget
) {
19 ImeController::~ImeController() {}
21 void ImeController::ResetInputMethod() {
22 // TODO(penghuang): Reset IME.
25 void ImeController::DidHandleGestureEvent(const blink::WebGestureEvent
& event
,
26 bool event_cancelled
) {
27 // Called when a gesture event is handled.
31 if (event
.type
== blink::WebInputEvent::GestureTap
) {
32 const bool show_ime
= true;
33 UpdateTextInputState(show_ime
);
34 } else if (event
.type
== blink::WebInputEvent::GestureLongPress
) {
35 // Only show IME if the textfield contains text.
36 const bool show_ime
= !widget_
->textInputInfo().value
.isEmpty();
37 UpdateTextInputState(show_ime
);
41 void ImeController::DidUpdateTextOfFocusedElementByNonUserInput() {
42 // Called when value of focused textfield gets dirty, e.g. value is
43 // modified by script, not by user input.
44 const bool show_ime
= false;
45 UpdateTextInputState(show_ime
);
48 void ImeController::ShowImeIfNeeded() {
49 // Request the browser to show the IME for current input type.
50 const bool show_ime
= true;
51 UpdateTextInputState(show_ime
);
54 void ImeController::UpdateTextInputState(bool show_ime
) {
55 blink::WebTextInputInfo new_info
= widget_
->textInputInfo();
56 // Only show IME if the focused element is editable.
57 show_ime
= show_ime
&& new_info
.type
!= blink::WebTextInputTypeNone
;
58 if (show_ime
|| text_input_info_
!= new_info
) {
59 text_input_info_
= new_info
;
60 mojo::TextInputStatePtr state
= mojo::TextInputState::New();
61 state
->type
= mojo::ConvertTo
<mojo::TextInputType
>(new_info
.type
);
62 state
->flags
= new_info
.flags
;
63 state
->text
= mojo::String::From(new_info
.value
.utf8());
64 state
->selection_start
= new_info
.selectionStart
;
65 state
->selection_end
= new_info
.selectionEnd
;
66 state
->composition_start
= new_info
.compositionStart
;
67 state
->composition_end
= new_info
.compositionEnd
;
69 view_
->SetImeVisibility(true, state
.Pass());
71 view_
->SetTextInputState(state
.Pass());
75 } // namespace html_viewer