Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ui / keyboard / keyboard_controller.cc
blobceef5941f7e424a9e912a7ad8ee0f8fe9ff73751
1 // Copyright (c) 2013 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 "ui/keyboard/keyboard_controller.h"
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "content/public/browser/render_widget_host.h"
10 #include "content/public/browser/render_widget_host_iterator.h"
11 #include "content/public/browser/render_widget_host_view.h"
12 #include "ui/aura/window.h"
13 #include "ui/aura/window_delegate.h"
14 #include "ui/aura/window_observer.h"
15 #include "ui/base/cursor/cursor.h"
16 #include "ui/base/hit_test.h"
17 #include "ui/base/ime/input_method.h"
18 #include "ui/base/ime/text_input_client.h"
19 #include "ui/compositor/layer_animation_observer.h"
20 #include "ui/compositor/scoped_layer_animation_settings.h"
21 #include "ui/gfx/path.h"
22 #include "ui/gfx/rect.h"
23 #include "ui/gfx/skia_util.h"
24 #include "ui/keyboard/keyboard_controller_observer.h"
25 #include "ui/keyboard/keyboard_controller_proxy.h"
26 #include "ui/keyboard/keyboard_layout_manager.h"
27 #include "ui/keyboard/keyboard_switches.h"
28 #include "ui/keyboard/keyboard_util.h"
29 #include "ui/wm/core/masked_window_targeter.h"
31 #if defined(OS_CHROMEOS)
32 #include "base/process/launch.h"
33 #include "base/sys_info.h"
34 #endif
36 namespace {
38 const int kHideKeyboardDelayMs = 100;
40 // The virtual keyboard show/hide animation duration.
41 const int kAnimationDurationMs = 200;
43 // The opacity of virtual keyboard container when show animation starts or
44 // hide animation finishes.
45 const float kAnimationStartOrAfterHideOpacity = 0.2f;
47 // Event targeter for the keyboard container.
48 class KeyboardContainerTargeter : public wm::MaskedWindowTargeter {
49 public:
50 KeyboardContainerTargeter(aura::Window* container,
51 keyboard::KeyboardControllerProxy* proxy)
52 : wm::MaskedWindowTargeter(container),
53 proxy_(proxy) {
56 virtual ~KeyboardContainerTargeter() {}
58 private:
59 // wm::MaskedWindowTargeter:
60 virtual bool GetHitTestMask(aura::Window* window,
61 gfx::Path* mask) const OVERRIDE {
62 if (proxy_ && !proxy_->HasKeyboardWindow())
63 return true;
64 gfx::Rect keyboard_bounds = proxy_ ? proxy_->GetKeyboardWindow()->bounds() :
65 keyboard::DefaultKeyboardBoundsFromWindowBounds(window->bounds());
66 mask->addRect(RectToSkRect(keyboard_bounds));
67 return true;
70 keyboard::KeyboardControllerProxy* proxy_;
72 DISALLOW_COPY_AND_ASSIGN(KeyboardContainerTargeter);
75 // The KeyboardWindowDelegate makes sure the keyboard-window does not get focus.
76 // This is necessary to make sure that the synthetic key-events reach the target
77 // window.
78 // The delegate deletes itself when the window is destroyed.
79 class KeyboardWindowDelegate : public aura::WindowDelegate {
80 public:
81 explicit KeyboardWindowDelegate(keyboard::KeyboardControllerProxy* proxy)
82 : proxy_(proxy) {}
83 virtual ~KeyboardWindowDelegate() {}
85 private:
86 // Overridden from aura::WindowDelegate:
87 virtual gfx::Size GetMinimumSize() const OVERRIDE { return gfx::Size(); }
88 virtual gfx::Size GetMaximumSize() const OVERRIDE { return gfx::Size(); }
89 virtual void OnBoundsChanged(const gfx::Rect& old_bounds,
90 const gfx::Rect& new_bounds) OVERRIDE {
91 bounds_ = new_bounds;
93 virtual gfx::NativeCursor GetCursor(const gfx::Point& point) OVERRIDE {
94 return gfx::kNullCursor;
96 virtual int GetNonClientComponent(const gfx::Point& point) const OVERRIDE {
97 return HTNOWHERE;
99 virtual bool ShouldDescendIntoChildForEventHandling(
100 aura::Window* child,
101 const gfx::Point& location) OVERRIDE {
102 return true;
104 virtual bool CanFocus() OVERRIDE { return false; }
105 virtual void OnCaptureLost() OVERRIDE {}
106 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {}
107 virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE {}
108 virtual void OnWindowDestroying(aura::Window* window) OVERRIDE {}
109 virtual void OnWindowDestroyed(aura::Window* window) OVERRIDE { delete this; }
110 virtual void OnWindowTargetVisibilityChanged(bool visible) OVERRIDE {}
111 virtual bool HasHitTestMask() const OVERRIDE {
112 return !proxy_ || proxy_->HasKeyboardWindow();
114 virtual void GetHitTestMask(gfx::Path* mask) const OVERRIDE {
115 if (proxy_ && !proxy_->HasKeyboardWindow())
116 return;
117 gfx::Rect keyboard_bounds = proxy_ ? proxy_->GetKeyboardWindow()->bounds() :
118 keyboard::DefaultKeyboardBoundsFromWindowBounds(bounds_);
119 mask->addRect(RectToSkRect(keyboard_bounds));
122 gfx::Rect bounds_;
123 keyboard::KeyboardControllerProxy* proxy_;
125 DISALLOW_COPY_AND_ASSIGN(KeyboardWindowDelegate);
128 void ToggleTouchEventLogging(bool enable) {
129 #if defined(OS_CHROMEOS)
130 if (!base::SysInfo::IsRunningOnChromeOS())
131 return;
132 CommandLine command(
133 base::FilePath("/opt/google/touchscreen/toggle_touch_event_logging"));
134 if (enable)
135 command.AppendArg("1");
136 else
137 command.AppendArg("0");
138 VLOG(1) << "Running " << command.GetCommandLineString();
139 base::LaunchOptions options;
140 options.wait = true;
141 base::LaunchProcess(command, options, NULL);
142 #endif
145 aura::Window *GetFrameWindow(aura::Window *window) {
146 // Each container window has a non-negative id. Stop traversing at the child
147 // of a container window.
148 if (!window)
149 return NULL;
150 while(window->parent() && window->parent()->id() < 0) {
151 window = window->parent();
153 return window;
156 } // namespace
158 namespace keyboard {
160 // Observer for both keyboard show and hide animations. It should be owned by
161 // KeyboardController.
162 class CallbackAnimationObserver : public ui::LayerAnimationObserver {
163 public:
164 CallbackAnimationObserver(ui::LayerAnimator* animator,
165 base::Callback<void(void)> callback);
166 virtual ~CallbackAnimationObserver();
168 private:
169 // Overridden from ui::LayerAnimationObserver:
170 virtual void OnLayerAnimationEnded(ui::LayerAnimationSequence* seq) OVERRIDE;
171 virtual void OnLayerAnimationAborted(
172 ui::LayerAnimationSequence* seq) OVERRIDE;
173 virtual void OnLayerAnimationScheduled(
174 ui::LayerAnimationSequence* seq) OVERRIDE {}
176 ui::LayerAnimator* animator_;
177 base::Callback<void(void)> callback_;
179 DISALLOW_COPY_AND_ASSIGN(CallbackAnimationObserver);
182 CallbackAnimationObserver::CallbackAnimationObserver(
183 ui::LayerAnimator* animator, base::Callback<void(void)> callback)
184 : animator_(animator), callback_(callback) {
187 CallbackAnimationObserver::~CallbackAnimationObserver() {
188 animator_->RemoveObserver(this);
191 void CallbackAnimationObserver::OnLayerAnimationEnded(
192 ui::LayerAnimationSequence* seq) {
193 if (animator_->is_animating())
194 return;
195 animator_->RemoveObserver(this);
196 callback_.Run();
199 void CallbackAnimationObserver::OnLayerAnimationAborted(
200 ui::LayerAnimationSequence* seq) {
201 animator_->RemoveObserver(this);
204 class WindowBoundsChangeObserver : public aura::WindowObserver {
205 public:
206 virtual void OnWindowBoundsChanged(aura::Window* window,
207 const gfx::Rect& old_bounds,
208 const gfx::Rect& new_bounds) OVERRIDE;
209 virtual void OnWindowDestroyed(aura::Window* window) OVERRIDE;
212 void WindowBoundsChangeObserver::OnWindowBoundsChanged(aura::Window* window,
213 const gfx::Rect& old_bounds, const gfx::Rect& new_bounds) {
214 KeyboardController* controller = KeyboardController::GetInstance();
215 if (controller)
216 controller->UpdateWindowInsets(window);
219 void WindowBoundsChangeObserver::OnWindowDestroyed(aura::Window* window) {
220 if (window->HasObserver(this))
221 window->RemoveObserver(this);
224 // static
225 KeyboardController* KeyboardController::instance_ = NULL;
227 KeyboardController::KeyboardController(KeyboardControllerProxy* proxy)
228 : proxy_(proxy),
229 input_method_(NULL),
230 keyboard_visible_(false),
231 show_on_resize_(false),
232 lock_keyboard_(false),
233 type_(ui::TEXT_INPUT_TYPE_NONE),
234 weak_factory_(this) {
235 CHECK(proxy);
236 input_method_ = proxy_->GetInputMethod();
237 input_method_->AddObserver(this);
238 window_bounds_observer_.reset(new WindowBoundsChangeObserver());
241 KeyboardController::~KeyboardController() {
242 if (container_)
243 container_->RemoveObserver(this);
244 if (input_method_)
245 input_method_->RemoveObserver(this);
246 ResetWindowInsets();
249 // static
250 void KeyboardController::ResetInstance(KeyboardController* controller) {
251 if (instance_ && instance_ != controller)
252 delete instance_;
253 instance_ = controller;
256 // static
257 KeyboardController* KeyboardController::GetInstance() {
258 return instance_;
261 aura::Window* KeyboardController::GetContainerWindow() {
262 if (!container_.get()) {
263 container_.reset(new aura::Window(
264 new KeyboardWindowDelegate(proxy_.get())));
265 container_->SetEventTargeter(scoped_ptr<ui::EventTargeter>(
266 new KeyboardContainerTargeter(container_.get(), proxy_.get())));
267 container_->SetName("KeyboardContainer");
268 container_->set_owned_by_parent(false);
269 container_->Init(aura::WINDOW_LAYER_NOT_DRAWN);
270 container_->AddObserver(this);
271 container_->SetLayoutManager(new KeyboardLayoutManager(this));
273 return container_.get();
276 void KeyboardController::NotifyKeyboardBoundsChanging(
277 const gfx::Rect& new_bounds) {
278 current_keyboard_bounds_ = new_bounds;
279 if (proxy_->HasKeyboardWindow() && proxy_->GetKeyboardWindow()->IsVisible()) {
280 FOR_EACH_OBSERVER(KeyboardControllerObserver,
281 observer_list_,
282 OnKeyboardBoundsChanging(new_bounds));
283 if (keyboard::IsKeyboardOverscrollEnabled()) {
284 // Adjust the height of the viewport for visible windows on the primary
285 // display.
286 // TODO(kevers): Add EnvObserver to properly initialize insets if a
287 // window is created while the keyboard is visible.
288 scoped_ptr<content::RenderWidgetHostIterator> widgets(
289 content::RenderWidgetHost::GetRenderWidgetHosts());
290 aura::Window *keyboard_window = proxy_->GetKeyboardWindow();
291 aura::Window *root_window = keyboard_window->GetRootWindow();
292 while (content::RenderWidgetHost* widget = widgets->GetNextHost()) {
293 content::RenderWidgetHostView* view = widget->GetView();
294 // Can be NULL, e.g. if the RenderWidget is being destroyed or
295 // the render process crashed.
296 if (view) {
297 aura::Window *window = view->GetNativeView();
298 // If virtual keyboard failed to load, a widget that displays error
299 // message will be created and adds as a child of the virtual keyboard
300 // window. We want to avoid add BoundsChangedObserver to that window.
301 if (GetFrameWindow(window) != keyboard_window &&
302 window->GetRootWindow() == root_window) {
303 gfx::Rect window_bounds = window->GetBoundsInScreen();
304 gfx::Rect intersect = gfx::IntersectRects(window_bounds,
305 new_bounds);
306 int overlap = intersect.height();
307 if (overlap > 0 && overlap < window_bounds.height())
308 view->SetInsets(gfx::Insets(0, 0, overlap, 0));
309 else
310 view->SetInsets(gfx::Insets());
311 AddBoundsChangedObserver(window);
315 } else {
316 ResetWindowInsets();
318 } else {
319 current_keyboard_bounds_ = gfx::Rect();
323 void KeyboardController::HideKeyboard(HideReason reason) {
324 keyboard_visible_ = false;
325 ToggleTouchEventLogging(true);
327 keyboard::LogKeyboardControlEvent(
328 reason == HIDE_REASON_AUTOMATIC ?
329 keyboard::KEYBOARD_CONTROL_HIDE_AUTO :
330 keyboard::KEYBOARD_CONTROL_HIDE_USER);
332 NotifyKeyboardBoundsChanging(gfx::Rect());
334 set_lock_keyboard(false);
336 ui::LayerAnimator* container_animator = container_->layer()->GetAnimator();
337 animation_observer_.reset(new CallbackAnimationObserver(
338 container_animator,
339 base::Bind(&KeyboardController::HideAnimationFinished,
340 base::Unretained(this))));
341 container_animator->AddObserver(animation_observer_.get());
343 ui::ScopedLayerAnimationSettings settings(container_animator);
344 settings.SetTweenType(gfx::Tween::EASE_OUT);
345 settings.SetTransitionDuration(
346 base::TimeDelta::FromMilliseconds(kAnimationDurationMs));
347 gfx::Transform transform;
348 transform.Translate(0, proxy_->GetKeyboardWindow()->bounds().height());
349 container_->SetTransform(transform);
350 container_->layer()->SetOpacity(kAnimationStartOrAfterHideOpacity);
353 void KeyboardController::AddObserver(KeyboardControllerObserver* observer) {
354 observer_list_.AddObserver(observer);
357 void KeyboardController::RemoveObserver(KeyboardControllerObserver* observer) {
358 observer_list_.RemoveObserver(observer);
361 void KeyboardController::ShowKeyboard(bool lock) {
362 set_lock_keyboard(lock);
363 ShowKeyboardInternal();
366 void KeyboardController::OnWindowHierarchyChanged(
367 const HierarchyChangeParams& params) {
368 if (params.new_parent && params.target == container_.get())
369 OnTextInputStateChanged(proxy_->GetInputMethod()->GetTextInputClient());
372 void KeyboardController::Reload() {
373 if (proxy_->HasKeyboardWindow()) {
374 // A reload should never try to show virtual keyboard. If keyboard is not
375 // visible before reload, it should keep invisible after reload.
376 show_on_resize_ = false;
377 proxy_->ReloadKeyboardIfNeeded();
381 void KeyboardController::OnTextInputStateChanged(
382 const ui::TextInputClient* client) {
383 if (!container_.get())
384 return;
386 if (IsKeyboardUsabilityExperimentEnabled()) {
387 ShowKeyboardInternal();
388 return;
391 type_ = client ? client->GetTextInputType() : ui::TEXT_INPUT_TYPE_NONE;
393 if (type_ == ui::TEXT_INPUT_TYPE_NONE && !lock_keyboard_) {
394 if (keyboard_visible_) {
395 // Set the visibility state here so that any queries for visibility
396 // before the timer fires returns the correct future value.
397 keyboard_visible_ = false;
398 base::MessageLoop::current()->PostDelayedTask(
399 FROM_HERE,
400 base::Bind(&KeyboardController::HideKeyboard,
401 weak_factory_.GetWeakPtr(), HIDE_REASON_AUTOMATIC),
402 base::TimeDelta::FromMilliseconds(kHideKeyboardDelayMs));
404 } else {
405 // Abort a pending keyboard hide.
406 if (WillHideKeyboard()) {
407 weak_factory_.InvalidateWeakPtrs();
408 keyboard_visible_ = true;
410 proxy_->SetUpdateInputType(type_);
411 // Do not explicitly show the Virtual keyboard unless it is in the process
412 // of hiding. Instead, the virtual keyboard is shown in response to a user
413 // gesture (mouse or touch) that is received while an element has input
414 // focus. Showing the keyboard requires an explicit call to
415 // OnShowImeIfNeeded.
419 void KeyboardController::OnInputMethodDestroyed(
420 const ui::InputMethod* input_method) {
421 DCHECK_EQ(input_method_, input_method);
422 input_method_ = NULL;
425 void KeyboardController::OnShowImeIfNeeded() {
426 ShowKeyboardInternal();
429 void KeyboardController::UpdateWindowInsets(aura::Window* window) {
430 aura::Window *keyboard_window = proxy_->GetKeyboardWindow();
431 if (window == keyboard_window)
432 return;
434 bool enableInsets = (keyboard_window->GetRootWindow() ==
435 window->GetRootWindow()) && keyboard::IsKeyboardOverscrollEnabled() &&
436 proxy_->GetKeyboardWindow()->IsVisible();
438 scoped_ptr<content::RenderWidgetHostIterator> widgets(
439 content::RenderWidgetHost::GetRenderWidgetHosts());
440 while (content::RenderWidgetHost* widget = widgets->GetNextHost()) {
441 content::RenderWidgetHostView* view = widget->GetView();
442 if (view && window->Contains(view->GetNativeView())) {
443 gfx::Rect window_bounds = view->GetNativeView()->GetBoundsInScreen();
444 gfx::Rect intersect = gfx::IntersectRects(window_bounds,
445 proxy_->GetKeyboardWindow()->bounds());
446 int overlap = enableInsets ? intersect.height() : 0;
447 if (overlap > 0 && overlap < window_bounds.height())
448 view->SetInsets(gfx::Insets(0, 0, overlap, 0));
449 else
450 view->SetInsets(gfx::Insets());
451 return;
456 void KeyboardController::ShowKeyboardInternal() {
457 if (!container_.get())
458 return;
460 if (container_->children().empty()) {
461 keyboard::MarkKeyboardLoadStarted();
462 aura::Window* keyboard = proxy_->GetKeyboardWindow();
463 keyboard->Show();
464 container_->AddChild(keyboard);
465 keyboard->set_owned_by_parent(false);
468 proxy_->ReloadKeyboardIfNeeded();
470 if (keyboard_visible_) {
471 return;
472 } else if (proxy_->GetKeyboardWindow()->bounds().height() == 0) {
473 show_on_resize_ = true;
474 return;
477 keyboard_visible_ = true;
479 // If the controller is in the process of hiding the keyboard, do not log
480 // the stat here since the keyboard will not actually be shown.
481 if (!WillHideKeyboard())
482 keyboard::LogKeyboardControlEvent(keyboard::KEYBOARD_CONTROL_SHOW);
484 weak_factory_.InvalidateWeakPtrs();
486 // If |container_| has hide animation, its visibility is set to false when
487 // hide animation finished. So even if the container is visible at this
488 // point, it may in the process of hiding. We still need to show keyboard
489 // container in this case.
490 if (container_->IsVisible() &&
491 !container_->layer()->GetAnimator()->is_animating())
492 return;
494 ToggleTouchEventLogging(false);
495 ui::LayerAnimator* container_animator = container_->layer()->GetAnimator();
497 // If the container is not animating, makes sure the position and opacity
498 // are at begin states for animation.
499 if (!container_animator->is_animating()) {
500 gfx::Transform transform;
501 transform.Translate(0, proxy_->GetKeyboardWindow()->bounds().height());
502 container_->SetTransform(transform);
503 container_->layer()->SetOpacity(kAnimationStartOrAfterHideOpacity);
506 container_animator->set_preemption_strategy(
507 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
508 animation_observer_.reset(new CallbackAnimationObserver(
509 container_animator,
510 base::Bind(&KeyboardController::ShowAnimationFinished,
511 base::Unretained(this))));
512 container_animator->AddObserver(animation_observer_.get());
514 proxy_->ShowKeyboardContainer(container_.get());
517 // Scope the following animation settings as we don't want to animate
518 // visibility change that triggered by a call to the base class function
519 // ShowKeyboardContainer with these settings. The container should become
520 // visible immediately.
521 ui::ScopedLayerAnimationSettings settings(container_animator);
522 settings.SetTweenType(gfx::Tween::EASE_IN);
523 settings.SetTransitionDuration(
524 base::TimeDelta::FromMilliseconds(kAnimationDurationMs));
525 container_->SetTransform(gfx::Transform());
526 container_->layer()->SetOpacity(1.0);
530 void KeyboardController::ResetWindowInsets() {
531 const gfx::Insets insets;
532 scoped_ptr<content::RenderWidgetHostIterator> widgets(
533 content::RenderWidgetHost::GetRenderWidgetHosts());
534 while (content::RenderWidgetHost* widget = widgets->GetNextHost()) {
535 content::RenderWidgetHostView* view = widget->GetView();
536 if (view) {
537 view->SetInsets(insets);
538 aura::Window *window = view->GetNativeView();
539 RemoveBoundsChangedObserver(window);
544 bool KeyboardController::WillHideKeyboard() const {
545 return weak_factory_.HasWeakPtrs();
548 void KeyboardController::ShowAnimationFinished() {
549 // Notify observers after animation finished to prevent reveal desktop
550 // background during animation.
551 NotifyKeyboardBoundsChanging(proxy_->GetKeyboardWindow()->bounds());
552 proxy_->EnsureCaretInWorkArea();
555 void KeyboardController::HideAnimationFinished() {
556 proxy_->HideKeyboardContainer(container_.get());
559 void KeyboardController::AddBoundsChangedObserver(aura::Window* window) {
560 aura::Window* target_window = GetFrameWindow(window);
561 if (target_window &&
562 !target_window->HasObserver(window_bounds_observer_.get())) {
563 target_window->AddObserver(window_bounds_observer_.get());
567 void KeyboardController::RemoveBoundsChangedObserver(aura::Window* window) {
568 aura::Window* target_window = GetFrameWindow(window);
569 if (target_window &&
570 target_window->HasObserver(window_bounds_observer_.get())) {
571 target_window->RemoveObserver(window_bounds_observer_.get());
575 } // namespace keyboard