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"
10 #include "base/command_line.h"
11 #include "content/public/browser/render_widget_host.h"
12 #include "content/public/browser/render_widget_host_iterator.h"
13 #include "content/public/browser/render_widget_host_view.h"
14 #include "ui/aura/window.h"
15 #include "ui/aura/window_delegate.h"
16 #include "ui/aura/window_observer.h"
17 #include "ui/base/cursor/cursor.h"
18 #include "ui/base/hit_test.h"
19 #include "ui/base/ime/input_method.h"
20 #include "ui/base/ime/text_input_client.h"
21 #include "ui/compositor/layer_animation_observer.h"
22 #include "ui/compositor/scoped_layer_animation_settings.h"
23 #include "ui/gfx/path.h"
24 #include "ui/gfx/rect.h"
25 #include "ui/gfx/skia_util.h"
26 #include "ui/keyboard/keyboard_controller_observer.h"
27 #include "ui/keyboard/keyboard_controller_proxy.h"
28 #include "ui/keyboard/keyboard_layout_manager.h"
29 #include "ui/keyboard/keyboard_switches.h"
30 #include "ui/keyboard/keyboard_util.h"
31 #include "ui/wm/core/masked_window_targeter.h"
33 #if defined(OS_CHROMEOS)
34 #include "base/process/launch.h"
35 #include "base/sys_info.h"
40 const int kHideKeyboardDelayMs
= 100;
42 // The virtual keyboard show/hide animation duration.
43 const int kShowAnimationDurationMs
= 350;
44 const int kHideAnimationDurationMs
= 100;
46 // The opacity of virtual keyboard container when show animation starts or
47 // hide animation finishes.
48 // TODO(rsadam@): Investigate why setting this to zero crashes.
49 const float kAnimationStartOrAfterHideOpacity
= 0.01f
;
51 // Event targeter for the keyboard container.
52 class KeyboardContainerTargeter
: public wm::MaskedWindowTargeter
{
54 KeyboardContainerTargeter(aura::Window
* container
,
55 keyboard::KeyboardControllerProxy
* proxy
)
56 : wm::MaskedWindowTargeter(container
),
60 virtual ~KeyboardContainerTargeter() {}
63 // wm::MaskedWindowTargeter:
64 virtual bool GetHitTestMask(aura::Window
* window
,
65 gfx::Path
* mask
) const OVERRIDE
{
66 if (proxy_
&& !proxy_
->HasKeyboardWindow())
68 gfx::Rect keyboard_bounds
= proxy_
? proxy_
->GetKeyboardWindow()->bounds() :
69 keyboard::DefaultKeyboardBoundsFromWindowBounds(window
->bounds());
70 mask
->addRect(RectToSkRect(keyboard_bounds
));
74 keyboard::KeyboardControllerProxy
* proxy_
;
76 DISALLOW_COPY_AND_ASSIGN(KeyboardContainerTargeter
);
79 // The KeyboardWindowDelegate makes sure the keyboard-window does not get focus.
80 // This is necessary to make sure that the synthetic key-events reach the target
82 // The delegate deletes itself when the window is destroyed.
83 class KeyboardWindowDelegate
: public aura::WindowDelegate
{
85 explicit KeyboardWindowDelegate(keyboard::KeyboardControllerProxy
* proxy
)
87 virtual ~KeyboardWindowDelegate() {}
90 // Overridden from aura::WindowDelegate:
91 virtual gfx::Size
GetMinimumSize() const OVERRIDE
{ return gfx::Size(); }
92 virtual gfx::Size
GetMaximumSize() const OVERRIDE
{ return gfx::Size(); }
93 virtual void OnBoundsChanged(const gfx::Rect
& old_bounds
,
94 const gfx::Rect
& new_bounds
) OVERRIDE
{
97 virtual gfx::NativeCursor
GetCursor(const gfx::Point
& point
) OVERRIDE
{
98 return gfx::kNullCursor
;
100 virtual int GetNonClientComponent(const gfx::Point
& point
) const OVERRIDE
{
103 virtual bool ShouldDescendIntoChildForEventHandling(
105 const gfx::Point
& location
) OVERRIDE
{
108 virtual bool CanFocus() OVERRIDE
{ return false; }
109 virtual void OnCaptureLost() OVERRIDE
{}
110 virtual void OnPaint(gfx::Canvas
* canvas
) OVERRIDE
{}
111 virtual void OnDeviceScaleFactorChanged(float device_scale_factor
) OVERRIDE
{}
112 virtual void OnWindowDestroying(aura::Window
* window
) OVERRIDE
{}
113 virtual void OnWindowDestroyed(aura::Window
* window
) OVERRIDE
{ delete this; }
114 virtual void OnWindowTargetVisibilityChanged(bool visible
) OVERRIDE
{}
115 virtual bool HasHitTestMask() const OVERRIDE
{
116 return !proxy_
|| proxy_
->HasKeyboardWindow();
118 virtual void GetHitTestMask(gfx::Path
* mask
) const OVERRIDE
{
119 if (proxy_
&& !proxy_
->HasKeyboardWindow())
121 gfx::Rect keyboard_bounds
= proxy_
? proxy_
->GetKeyboardWindow()->bounds() :
122 keyboard::DefaultKeyboardBoundsFromWindowBounds(bounds_
);
123 mask
->addRect(RectToSkRect(keyboard_bounds
));
127 keyboard::KeyboardControllerProxy
* proxy_
;
129 DISALLOW_COPY_AND_ASSIGN(KeyboardWindowDelegate
);
132 void ToggleTouchEventLogging(bool enable
) {
133 #if defined(OS_CHROMEOS)
134 if (!base::SysInfo::IsRunningOnChromeOS())
137 base::FilePath("/opt/google/touchscreen/toggle_touch_event_logging"));
139 command
.AppendArg("1");
141 command
.AppendArg("0");
142 VLOG(1) << "Running " << command
.GetCommandLineString();
143 base::LaunchOptions options
;
145 base::LaunchProcess(command
, options
, NULL
);
149 aura::Window
*GetFrameWindow(aura::Window
*window
) {
150 // Each container window has a non-negative id. Stop traversing at the child
151 // of a container window.
154 while (window
->parent() && window
->parent()->id() < 0) {
155 window
= window
->parent();
164 // Observer for both keyboard show and hide animations. It should be owned by
165 // KeyboardController.
166 class CallbackAnimationObserver
: public ui::LayerAnimationObserver
{
168 CallbackAnimationObserver(ui::LayerAnimator
* animator
,
169 base::Callback
<void(void)> callback
);
170 virtual ~CallbackAnimationObserver();
173 // Overridden from ui::LayerAnimationObserver:
174 virtual void OnLayerAnimationEnded(ui::LayerAnimationSequence
* seq
) OVERRIDE
;
175 virtual void OnLayerAnimationAborted(
176 ui::LayerAnimationSequence
* seq
) OVERRIDE
;
177 virtual void OnLayerAnimationScheduled(
178 ui::LayerAnimationSequence
* seq
) OVERRIDE
{}
180 ui::LayerAnimator
* animator_
;
181 base::Callback
<void(void)> callback_
;
183 DISALLOW_COPY_AND_ASSIGN(CallbackAnimationObserver
);
186 CallbackAnimationObserver::CallbackAnimationObserver(
187 ui::LayerAnimator
* animator
, base::Callback
<void(void)> callback
)
188 : animator_(animator
), callback_(callback
) {
191 CallbackAnimationObserver::~CallbackAnimationObserver() {
192 animator_
->RemoveObserver(this);
195 void CallbackAnimationObserver::OnLayerAnimationEnded(
196 ui::LayerAnimationSequence
* seq
) {
197 if (animator_
->is_animating())
199 animator_
->RemoveObserver(this);
203 void CallbackAnimationObserver::OnLayerAnimationAborted(
204 ui::LayerAnimationSequence
* seq
) {
205 animator_
->RemoveObserver(this);
208 class WindowBoundsChangeObserver
: public aura::WindowObserver
{
210 virtual void OnWindowBoundsChanged(aura::Window
* window
,
211 const gfx::Rect
& old_bounds
,
212 const gfx::Rect
& new_bounds
) OVERRIDE
;
213 virtual void OnWindowDestroyed(aura::Window
* window
) OVERRIDE
;
215 void AddObservedWindow(aura::Window
* window
);
216 void RemoveAllObservedWindows();
219 std::set
<aura::Window
*> observed_windows_
;
222 void WindowBoundsChangeObserver::OnWindowBoundsChanged(aura::Window
* window
,
223 const gfx::Rect
& old_bounds
, const gfx::Rect
& new_bounds
) {
224 KeyboardController
* controller
= KeyboardController::GetInstance();
226 controller
->UpdateWindowInsets(window
);
229 void WindowBoundsChangeObserver::OnWindowDestroyed(aura::Window
* window
) {
230 if (window
->HasObserver(this))
231 window
->RemoveObserver(this);
232 observed_windows_
.erase(window
);
235 void WindowBoundsChangeObserver::AddObservedWindow(aura::Window
* window
) {
236 if (!window
->HasObserver(this)) {
237 window
->AddObserver(this);
238 observed_windows_
.insert(window
);
242 void WindowBoundsChangeObserver::RemoveAllObservedWindows() {
243 for (std::set
<aura::Window
*>::iterator it
= observed_windows_
.begin();
244 it
!= observed_windows_
.end(); ++it
)
245 (*it
)->RemoveObserver(this);
246 observed_windows_
.clear();
250 KeyboardController
* KeyboardController::instance_
= NULL
;
252 KeyboardController::KeyboardController(KeyboardControllerProxy
* proxy
)
255 keyboard_visible_(false),
256 show_on_resize_(false),
257 lock_keyboard_(false),
258 type_(ui::TEXT_INPUT_TYPE_NONE
),
259 weak_factory_(this) {
261 input_method_
= proxy_
->GetInputMethod();
262 input_method_
->AddObserver(this);
263 window_bounds_observer_
.reset(new WindowBoundsChangeObserver());
266 KeyboardController::~KeyboardController() {
268 container_
->RemoveObserver(this);
270 input_method_
->RemoveObserver(this);
275 void KeyboardController::ResetInstance(KeyboardController
* controller
) {
276 if (instance_
&& instance_
!= controller
)
278 instance_
= controller
;
282 KeyboardController
* KeyboardController::GetInstance() {
286 aura::Window
* KeyboardController::GetContainerWindow() {
287 if (!container_
.get()) {
288 container_
.reset(new aura::Window(
289 new KeyboardWindowDelegate(proxy_
.get())));
290 container_
->SetEventTargeter(scoped_ptr
<ui::EventTargeter
>(
291 new KeyboardContainerTargeter(container_
.get(), proxy_
.get())));
292 container_
->SetName("KeyboardContainer");
293 container_
->set_owned_by_parent(false);
294 container_
->Init(aura::WINDOW_LAYER_NOT_DRAWN
);
295 container_
->AddObserver(this);
296 container_
->SetLayoutManager(new KeyboardLayoutManager(this));
298 return container_
.get();
301 void KeyboardController::NotifyKeyboardBoundsChanging(
302 const gfx::Rect
& new_bounds
) {
303 current_keyboard_bounds_
= new_bounds
;
304 if (proxy_
->HasKeyboardWindow() && proxy_
->GetKeyboardWindow()->IsVisible()) {
305 FOR_EACH_OBSERVER(KeyboardControllerObserver
,
307 OnKeyboardBoundsChanging(new_bounds
));
308 if (keyboard::IsKeyboardOverscrollEnabled()) {
309 // Adjust the height of the viewport for visible windows on the primary
311 // TODO(kevers): Add EnvObserver to properly initialize insets if a
312 // window is created while the keyboard is visible.
313 scoped_ptr
<content::RenderWidgetHostIterator
> widgets(
314 content::RenderWidgetHost::GetRenderWidgetHosts());
315 aura::Window
*keyboard_window
= proxy_
->GetKeyboardWindow();
316 aura::Window
*root_window
= keyboard_window
->GetRootWindow();
317 while (content::RenderWidgetHost
* widget
= widgets
->GetNextHost()) {
318 content::RenderWidgetHostView
* view
= widget
->GetView();
319 // Can be NULL, e.g. if the RenderWidget is being destroyed or
320 // the render process crashed.
322 aura::Window
*window
= view
->GetNativeView();
323 // If virtual keyboard failed to load, a widget that displays error
324 // message will be created and adds as a child of the virtual keyboard
325 // window. We want to avoid add BoundsChangedObserver to that window.
326 if (GetFrameWindow(window
) != keyboard_window
&&
327 window
->GetRootWindow() == root_window
) {
328 gfx::Rect window_bounds
= window
->GetBoundsInScreen();
329 gfx::Rect intersect
= gfx::IntersectRects(window_bounds
,
331 int overlap
= intersect
.height();
332 if (overlap
> 0 && overlap
< window_bounds
.height())
333 view
->SetInsets(gfx::Insets(0, 0, overlap
, 0));
335 view
->SetInsets(gfx::Insets());
336 AddBoundsChangedObserver(window
);
344 current_keyboard_bounds_
= gfx::Rect();
348 void KeyboardController::HideKeyboard(HideReason reason
) {
349 keyboard_visible_
= false;
350 ToggleTouchEventLogging(true);
352 keyboard::LogKeyboardControlEvent(
353 reason
== HIDE_REASON_AUTOMATIC
?
354 keyboard::KEYBOARD_CONTROL_HIDE_AUTO
:
355 keyboard::KEYBOARD_CONTROL_HIDE_USER
);
357 NotifyKeyboardBoundsChanging(gfx::Rect());
359 set_lock_keyboard(false);
361 ui::LayerAnimator
* container_animator
= container_
->layer()->GetAnimator();
362 animation_observer_
.reset(new CallbackAnimationObserver(
364 base::Bind(&KeyboardController::HideAnimationFinished
,
365 base::Unretained(this))));
366 container_animator
->AddObserver(animation_observer_
.get());
368 ui::ScopedLayerAnimationSettings
settings(container_animator
);
369 settings
.SetTweenType(gfx::Tween::FAST_OUT_LINEAR_IN
);
370 settings
.SetTransitionDuration(
371 base::TimeDelta::FromMilliseconds(kHideAnimationDurationMs
));
372 gfx::Transform transform
;
373 transform
.Translate(0, proxy_
->GetKeyboardWindow()->bounds().height());
374 container_
->SetTransform(transform
);
375 container_
->layer()->SetOpacity(kAnimationStartOrAfterHideOpacity
);
378 void KeyboardController::AddObserver(KeyboardControllerObserver
* observer
) {
379 observer_list_
.AddObserver(observer
);
382 void KeyboardController::RemoveObserver(KeyboardControllerObserver
* observer
) {
383 observer_list_
.RemoveObserver(observer
);
386 void KeyboardController::ShowKeyboard(bool lock
) {
387 set_lock_keyboard(lock
);
388 ShowKeyboardInternal();
391 void KeyboardController::OnWindowHierarchyChanged(
392 const HierarchyChangeParams
& params
) {
393 if (params
.new_parent
&& params
.target
== container_
.get())
394 OnTextInputStateChanged(proxy_
->GetInputMethod()->GetTextInputClient());
397 void KeyboardController::Reload() {
398 if (proxy_
->HasKeyboardWindow()) {
399 // A reload should never try to show virtual keyboard. If keyboard is not
400 // visible before reload, it should keep invisible after reload.
401 show_on_resize_
= false;
402 proxy_
->ReloadKeyboardIfNeeded();
406 void KeyboardController::OnTextInputStateChanged(
407 const ui::TextInputClient
* client
) {
408 if (!container_
.get())
411 if (IsKeyboardUsabilityExperimentEnabled()) {
412 ShowKeyboardInternal();
416 type_
= client
? client
->GetTextInputType() : ui::TEXT_INPUT_TYPE_NONE
;
418 if (type_
== ui::TEXT_INPUT_TYPE_NONE
&& !lock_keyboard_
) {
419 if (keyboard_visible_
) {
420 // Set the visibility state here so that any queries for visibility
421 // before the timer fires returns the correct future value.
422 keyboard_visible_
= false;
423 base::MessageLoop::current()->PostDelayedTask(
425 base::Bind(&KeyboardController::HideKeyboard
,
426 weak_factory_
.GetWeakPtr(), HIDE_REASON_AUTOMATIC
),
427 base::TimeDelta::FromMilliseconds(kHideKeyboardDelayMs
));
430 // Abort a pending keyboard hide.
431 if (WillHideKeyboard()) {
432 weak_factory_
.InvalidateWeakPtrs();
433 keyboard_visible_
= true;
435 proxy_
->SetUpdateInputType(type_
);
436 // Do not explicitly show the Virtual keyboard unless it is in the process
437 // of hiding. Instead, the virtual keyboard is shown in response to a user
438 // gesture (mouse or touch) that is received while an element has input
439 // focus. Showing the keyboard requires an explicit call to
440 // OnShowImeIfNeeded.
444 void KeyboardController::OnInputMethodDestroyed(
445 const ui::InputMethod
* input_method
) {
446 DCHECK_EQ(input_method_
, input_method
);
447 input_method_
= NULL
;
450 void KeyboardController::OnShowImeIfNeeded() {
451 ShowKeyboardInternal();
454 void KeyboardController::UpdateWindowInsets(aura::Window
* window
) {
455 aura::Window
*keyboard_window
= proxy_
->GetKeyboardWindow();
456 if (window
== keyboard_window
)
459 bool enableInsets
= (keyboard_window
->GetRootWindow() ==
460 window
->GetRootWindow()) && keyboard::IsKeyboardOverscrollEnabled() &&
461 proxy_
->GetKeyboardWindow()->IsVisible();
463 scoped_ptr
<content::RenderWidgetHostIterator
> widgets(
464 content::RenderWidgetHost::GetRenderWidgetHosts());
465 while (content::RenderWidgetHost
* widget
= widgets
->GetNextHost()) {
466 content::RenderWidgetHostView
* view
= widget
->GetView();
467 if (view
&& window
->Contains(view
->GetNativeView())) {
468 gfx::Rect window_bounds
= view
->GetNativeView()->GetBoundsInScreen();
469 gfx::Rect intersect
= gfx::IntersectRects(window_bounds
,
470 proxy_
->GetKeyboardWindow()->bounds());
471 int overlap
= enableInsets
? intersect
.height() : 0;
472 if (overlap
> 0 && overlap
< window_bounds
.height())
473 view
->SetInsets(gfx::Insets(0, 0, overlap
, 0));
475 view
->SetInsets(gfx::Insets());
481 void KeyboardController::ShowKeyboardInternal() {
482 if (!container_
.get())
485 if (container_
->children().empty()) {
486 keyboard::MarkKeyboardLoadStarted();
487 aura::Window
* keyboard
= proxy_
->GetKeyboardWindow();
489 container_
->AddChild(keyboard
);
490 keyboard
->set_owned_by_parent(false);
493 proxy_
->ReloadKeyboardIfNeeded();
495 if (keyboard_visible_
) {
497 } else if (proxy_
->GetKeyboardWindow()->bounds().height() == 0) {
498 show_on_resize_
= true;
502 keyboard_visible_
= true;
504 // If the controller is in the process of hiding the keyboard, do not log
505 // the stat here since the keyboard will not actually be shown.
506 if (!WillHideKeyboard())
507 keyboard::LogKeyboardControlEvent(keyboard::KEYBOARD_CONTROL_SHOW
);
509 weak_factory_
.InvalidateWeakPtrs();
511 // If |container_| has hide animation, its visibility is set to false when
512 // hide animation finished. So even if the container is visible at this
513 // point, it may in the process of hiding. We still need to show keyboard
514 // container in this case.
515 if (container_
->IsVisible() &&
516 !container_
->layer()->GetAnimator()->is_animating())
519 ToggleTouchEventLogging(false);
520 ui::LayerAnimator
* container_animator
= container_
->layer()->GetAnimator();
522 // If the container is not animating, makes sure the position and opacity
523 // are at begin states for animation.
524 if (!container_animator
->is_animating()) {
525 gfx::Transform transform
;
526 transform
.Translate(0, proxy_
->GetKeyboardWindow()->bounds().height());
527 container_
->SetTransform(transform
);
528 container_
->layer()->SetOpacity(kAnimationStartOrAfterHideOpacity
);
531 container_animator
->set_preemption_strategy(
532 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET
);
533 animation_observer_
.reset(new CallbackAnimationObserver(
535 base::Bind(&KeyboardController::ShowAnimationFinished
,
536 base::Unretained(this))));
537 container_animator
->AddObserver(animation_observer_
.get());
539 proxy_
->ShowKeyboardContainer(container_
.get());
542 // Scope the following animation settings as we don't want to animate
543 // visibility change that triggered by a call to the base class function
544 // ShowKeyboardContainer with these settings. The container should become
545 // visible immediately.
546 ui::ScopedLayerAnimationSettings
settings(container_animator
);
547 settings
.SetTweenType(gfx::Tween::LINEAR_OUT_SLOW_IN
);
548 settings
.SetTransitionDuration(
549 base::TimeDelta::FromMilliseconds(kShowAnimationDurationMs
));
550 container_
->SetTransform(gfx::Transform());
551 container_
->layer()->SetOpacity(1.0);
555 void KeyboardController::ResetWindowInsets() {
556 const gfx::Insets insets
;
557 scoped_ptr
<content::RenderWidgetHostIterator
> widgets(
558 content::RenderWidgetHost::GetRenderWidgetHosts());
559 while (content::RenderWidgetHost
* widget
= widgets
->GetNextHost()) {
560 content::RenderWidgetHostView
* view
= widget
->GetView();
562 view
->SetInsets(insets
);
564 window_bounds_observer_
->RemoveAllObservedWindows();
567 bool KeyboardController::WillHideKeyboard() const {
568 return weak_factory_
.HasWeakPtrs();
571 void KeyboardController::ShowAnimationFinished() {
572 // Notify observers after animation finished to prevent reveal desktop
573 // background during animation.
574 NotifyKeyboardBoundsChanging(proxy_
->GetKeyboardWindow()->bounds());
575 proxy_
->EnsureCaretInWorkArea();
578 void KeyboardController::HideAnimationFinished() {
579 proxy_
->HideKeyboardContainer(container_
.get());
582 void KeyboardController::AddBoundsChangedObserver(aura::Window
* window
) {
583 aura::Window
* target_window
= GetFrameWindow(window
);
585 window_bounds_observer_
->AddObservedWindow(target_window
);
588 } // namespace keyboard