OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "components/html_viewer/ime_controller.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "components/html_viewer/blink_text_input_type_converters.h" | |
10 #include "components/mus/public/cpp/window.h" | |
11 #include "mojo/converters/blink/blink_input_events_type_converters.h" | |
12 #include "third_party/WebKit/public/web/WebInputEvent.h" | |
13 #include "third_party/WebKit/public/web/WebWidget.h" | |
14 | |
15 namespace html_viewer { | |
16 | |
17 ImeController::ImeController(mus::Window* window, blink::WebWidget* widget) | |
18 : window_(window), widget_(widget) {} | |
19 | |
20 ImeController::~ImeController() {} | |
21 | |
22 void ImeController::ResetInputMethod() { | |
23 // TODO(penghuang): Reset IME. | |
24 } | |
25 | |
26 void ImeController::DidHandleGestureEvent(const blink::WebGestureEvent& event, | |
27 bool event_cancelled) { | |
28 // Called when a gesture event is handled. | |
29 if (event_cancelled) | |
30 return; | |
31 | |
32 if (event.type == blink::WebInputEvent::GestureTap) { | |
33 const bool show_ime = true; | |
34 UpdateTextInputState(show_ime); | |
35 } else if (event.type == blink::WebInputEvent::GestureLongPress) { | |
36 // Only show IME if the textfield contains text. | |
37 const bool show_ime = !widget_->textInputInfo().value.isEmpty(); | |
38 UpdateTextInputState(show_ime); | |
39 } | |
40 } | |
41 | |
42 void ImeController::DidUpdateTextOfFocusedElementByNonUserInput() { | |
43 // Called when value of focused textfield gets dirty, e.g. value is | |
44 // modified by script, not by user input. | |
45 const bool show_ime = false; | |
46 UpdateTextInputState(show_ime); | |
47 } | |
48 | |
49 void ImeController::ShowImeIfNeeded() { | |
50 // Request the browser to show the IME for current input type. | |
51 const bool show_ime = true; | |
52 UpdateTextInputState(show_ime); | |
53 } | |
54 | |
55 void ImeController::UpdateTextInputState(bool show_ime) { | |
56 blink::WebTextInputInfo new_info = widget_->textInputInfo(); | |
57 // Only show IME if the focused element is editable. | |
58 show_ime = show_ime && new_info.type != blink::WebTextInputTypeNone; | |
59 if (show_ime || text_input_info_ != new_info) { | |
60 text_input_info_ = new_info; | |
61 mojo::TextInputStatePtr state = mojo::TextInputState::New(); | |
62 state->type = mojo::ConvertTo<mojo::TextInputType>(new_info.type); | |
63 state->flags = new_info.flags; | |
64 state->text = mojo::String::From(new_info.value.utf8()); | |
65 state->selection_start = new_info.selectionStart; | |
66 state->selection_end = new_info.selectionEnd; | |
67 state->composition_start = new_info.compositionStart; | |
68 state->composition_end = new_info.compositionEnd; | |
69 if (show_ime) | |
70 window_->SetImeVisibility(true, std::move(state)); | |
71 else | |
72 window_->SetTextInputState(std::move(state)); | |
73 } | |
74 } | |
75 | |
76 } // namespace html_viewer | |
OLD | NEW |