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/geometry/rect.h"
24 #include "ui/gfx/path.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_util.h"
30 #include "ui/wm/core/masked_window_targeter.h"
32 #if defined(OS_CHROMEOS)
33 #include "base/process/launch.h"
34 #include "base/sys_info.h"
39 const int kHideKeyboardDelayMs
= 100;
41 // The virtual keyboard show/hide animation duration.
42 const int kShowAnimationDurationMs
= 350;
43 const int kHideAnimationDurationMs
= 100;
45 // The opacity of virtual keyboard container when show animation starts or
46 // hide animation finishes. This cannot be zero because we call Show() on the
47 // keyboard window before setting the opacity back to 1.0. Since windows are not
48 // allowed to be shown with zero opacity, we always animate to 0.01 instead.
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 ~KeyboardContainerTargeter() override
{}
63 // wm::MaskedWindowTargeter:
64 bool GetHitTestMask(aura::Window
* window
, gfx::Path
* mask
) const override
{
65 if (proxy_
&& !proxy_
->HasKeyboardWindow())
67 gfx::Rect keyboard_bounds
= proxy_
? proxy_
->GetKeyboardWindow()->bounds() :
68 keyboard::DefaultKeyboardBoundsFromWindowBounds(window
->bounds());
69 mask
->addRect(RectToSkRect(keyboard_bounds
));
73 keyboard::KeyboardControllerProxy
* proxy_
;
75 DISALLOW_COPY_AND_ASSIGN(KeyboardContainerTargeter
);
78 // The KeyboardWindowDelegate makes sure the keyboard-window does not get focus.
79 // This is necessary to make sure that the synthetic key-events reach the target
81 // The delegate deletes itself when the window is destroyed.
82 class KeyboardWindowDelegate
: public aura::WindowDelegate
{
84 explicit KeyboardWindowDelegate(keyboard::KeyboardControllerProxy
* proxy
)
86 ~KeyboardWindowDelegate() override
{}
89 // Overridden from aura::WindowDelegate:
90 gfx::Size
GetMinimumSize() const override
{ return gfx::Size(); }
91 gfx::Size
GetMaximumSize() const override
{ return gfx::Size(); }
92 void OnBoundsChanged(const gfx::Rect
& old_bounds
,
93 const gfx::Rect
& new_bounds
) override
{
96 ui::TextInputClient
* GetFocusedTextInputClient() override
{
99 gfx::NativeCursor
GetCursor(const gfx::Point
& point
) override
{
100 return gfx::kNullCursor
;
102 int GetNonClientComponent(const gfx::Point
& point
) const override
{
105 bool ShouldDescendIntoChildForEventHandling(
107 const gfx::Point
& location
) override
{
110 bool CanFocus() override
{ return false; }
111 void OnCaptureLost() override
{}
112 void OnPaint(const ui::PaintContext
& context
) override
{}
113 void OnDeviceScaleFactorChanged(float device_scale_factor
) override
{}
114 void OnWindowDestroying(aura::Window
* window
) override
{}
115 void OnWindowDestroyed(aura::Window
* window
) override
{ delete this; }
116 void OnWindowTargetVisibilityChanged(bool visible
) override
{}
117 bool HasHitTestMask() const override
{
118 return !proxy_
|| proxy_
->HasKeyboardWindow();
120 void GetHitTestMask(gfx::Path
* mask
) const override
{
121 if (proxy_
&& !proxy_
->HasKeyboardWindow())
123 gfx::Rect keyboard_bounds
= proxy_
? proxy_
->GetKeyboardWindow()->bounds() :
124 keyboard::DefaultKeyboardBoundsFromWindowBounds(bounds_
);
125 mask
->addRect(RectToSkRect(keyboard_bounds
));
129 keyboard::KeyboardControllerProxy
* proxy_
;
131 DISALLOW_COPY_AND_ASSIGN(KeyboardWindowDelegate
);
134 void ToggleTouchEventLogging(bool enable
) {
135 #if defined(OS_CHROMEOS)
136 if (!base::SysInfo::IsRunningOnChromeOS())
138 base::CommandLine
command(
139 base::FilePath("/opt/google/touchscreen/toggle_touch_event_logging"));
141 command
.AppendArg("1");
143 command
.AppendArg("0");
144 VLOG(1) << "Running " << command
.GetCommandLineString();
145 base::LaunchOptions options
;
147 base::LaunchProcess(command
, options
);
155 // Observer for both keyboard show and hide animations. It should be owned by
156 // KeyboardController.
157 class CallbackAnimationObserver
: public ui::LayerAnimationObserver
{
159 CallbackAnimationObserver(ui::LayerAnimator
* animator
,
160 base::Callback
<void(void)> callback
);
161 ~CallbackAnimationObserver() override
;
164 // Overridden from ui::LayerAnimationObserver:
165 void OnLayerAnimationEnded(ui::LayerAnimationSequence
* seq
) override
;
166 void OnLayerAnimationAborted(ui::LayerAnimationSequence
* seq
) override
;
167 void OnLayerAnimationScheduled(ui::LayerAnimationSequence
* seq
) override
{}
169 ui::LayerAnimator
* animator_
;
170 base::Callback
<void(void)> callback_
;
172 DISALLOW_COPY_AND_ASSIGN(CallbackAnimationObserver
);
175 CallbackAnimationObserver::CallbackAnimationObserver(
176 ui::LayerAnimator
* animator
, base::Callback
<void(void)> callback
)
177 : animator_(animator
), callback_(callback
) {
180 CallbackAnimationObserver::~CallbackAnimationObserver() {
181 animator_
->RemoveObserver(this);
184 void CallbackAnimationObserver::OnLayerAnimationEnded(
185 ui::LayerAnimationSequence
* seq
) {
186 if (animator_
->is_animating())
188 animator_
->RemoveObserver(this);
192 void CallbackAnimationObserver::OnLayerAnimationAborted(
193 ui::LayerAnimationSequence
* seq
) {
194 animator_
->RemoveObserver(this);
197 class WindowBoundsChangeObserver
: public aura::WindowObserver
{
199 void OnWindowBoundsChanged(aura::Window
* window
,
200 const gfx::Rect
& old_bounds
,
201 const gfx::Rect
& new_bounds
) override
;
202 void OnWindowDestroyed(aura::Window
* window
) override
;
204 void AddObservedWindow(aura::Window
* window
);
205 void RemoveAllObservedWindows();
208 std::set
<aura::Window
*> observed_windows_
;
211 void WindowBoundsChangeObserver::OnWindowBoundsChanged(aura::Window
* window
,
212 const gfx::Rect
& old_bounds
, const gfx::Rect
& new_bounds
) {
213 KeyboardController
* controller
= KeyboardController::GetInstance();
215 controller
->UpdateWindowInsets(window
);
218 void WindowBoundsChangeObserver::OnWindowDestroyed(aura::Window
* window
) {
219 if (window
->HasObserver(this))
220 window
->RemoveObserver(this);
221 observed_windows_
.erase(window
);
224 void WindowBoundsChangeObserver::AddObservedWindow(aura::Window
* window
) {
225 if (!window
->HasObserver(this)) {
226 window
->AddObserver(this);
227 observed_windows_
.insert(window
);
231 void WindowBoundsChangeObserver::RemoveAllObservedWindows() {
232 for (std::set
<aura::Window
*>::iterator it
= observed_windows_
.begin();
233 it
!= observed_windows_
.end(); ++it
)
234 (*it
)->RemoveObserver(this);
235 observed_windows_
.clear();
239 KeyboardController
* KeyboardController::instance_
= NULL
;
241 KeyboardController::KeyboardController(KeyboardControllerProxy
* proxy
)
244 keyboard_visible_(false),
245 show_on_resize_(false),
246 lock_keyboard_(false),
247 keyboard_mode_(FULL_WIDTH
),
248 type_(ui::TEXT_INPUT_TYPE_NONE
),
249 weak_factory_(this) {
251 input_method_
= proxy_
->GetInputMethod();
252 input_method_
->AddObserver(this);
253 window_bounds_observer_
.reset(new WindowBoundsChangeObserver());
256 KeyboardController::~KeyboardController() {
258 container_
->RemoveObserver(this);
260 input_method_
->RemoveObserver(this);
265 void KeyboardController::ResetInstance(KeyboardController
* controller
) {
266 if (instance_
&& instance_
!= controller
)
268 instance_
= controller
;
272 KeyboardController
* KeyboardController::GetInstance() {
276 aura::Window
* KeyboardController::GetContainerWindow() {
277 if (!container_
.get()) {
278 container_
.reset(new aura::Window(
279 new KeyboardWindowDelegate(proxy_
.get())));
280 container_
->SetEventTargeter(scoped_ptr
<ui::EventTargeter
>(
281 new KeyboardContainerTargeter(container_
.get(), proxy_
.get())));
282 container_
->SetName("KeyboardContainer");
283 container_
->set_owned_by_parent(false);
284 container_
->Init(ui::LAYER_NOT_DRAWN
);
285 container_
->AddObserver(this);
286 container_
->SetLayoutManager(new KeyboardLayoutManager(this));
288 return container_
.get();
291 void KeyboardController::NotifyKeyboardBoundsChanging(
292 const gfx::Rect
& new_bounds
) {
293 current_keyboard_bounds_
= new_bounds
;
294 if (proxy_
->HasKeyboardWindow() && proxy_
->GetKeyboardWindow()->IsVisible()) {
295 FOR_EACH_OBSERVER(KeyboardControllerObserver
,
297 OnKeyboardBoundsChanging(new_bounds
));
298 if (keyboard::IsKeyboardOverscrollEnabled()) {
299 // Adjust the height of the viewport for visible windows on the primary
301 // TODO(kevers): Add EnvObserver to properly initialize insets if a
302 // window is created while the keyboard is visible.
303 scoped_ptr
<content::RenderWidgetHostIterator
> widgets(
304 content::RenderWidgetHost::GetRenderWidgetHosts());
305 aura::Window
* keyboard_window
= proxy_
->GetKeyboardWindow();
306 aura::Window
* root_window
= keyboard_window
->GetRootWindow();
307 while (content::RenderWidgetHost
* widget
= widgets
->GetNextHost()) {
308 content::RenderWidgetHostView
* view
= widget
->GetView();
309 // Can be NULL, e.g. if the RenderWidget is being destroyed or
310 // the render process crashed.
312 aura::Window
* window
= view
->GetNativeView();
313 // If virtual keyboard failed to load, a widget that displays error
314 // message will be created and adds as a child of the virtual keyboard
315 // window. We want to avoid add BoundsChangedObserver to that window.
316 if (!keyboard_window
->Contains(window
) &&
317 window
->GetRootWindow() == root_window
) {
318 gfx::Rect window_bounds
= window
->GetBoundsInScreen();
319 gfx::Rect intersect
= gfx::IntersectRects(window_bounds
,
321 int overlap
= intersect
.height();
322 if (overlap
> 0 && overlap
< window_bounds
.height())
323 view
->SetInsets(gfx::Insets(0, 0, overlap
, 0));
325 view
->SetInsets(gfx::Insets());
326 AddBoundsChangedObserver(window
);
334 current_keyboard_bounds_
= gfx::Rect();
338 void KeyboardController::HideKeyboard(HideReason reason
) {
339 keyboard_visible_
= false;
340 ToggleTouchEventLogging(true);
342 keyboard::LogKeyboardControlEvent(
343 reason
== HIDE_REASON_AUTOMATIC
?
344 keyboard::KEYBOARD_CONTROL_HIDE_AUTO
:
345 keyboard::KEYBOARD_CONTROL_HIDE_USER
);
347 NotifyKeyboardBoundsChanging(gfx::Rect());
349 set_lock_keyboard(false);
351 ui::LayerAnimator
* container_animator
= container_
->layer()->GetAnimator();
352 animation_observer_
.reset(new CallbackAnimationObserver(
354 base::Bind(&KeyboardController::HideAnimationFinished
,
355 base::Unretained(this))));
356 container_animator
->AddObserver(animation_observer_
.get());
358 ui::ScopedLayerAnimationSettings
settings(container_animator
);
359 settings
.SetTweenType(gfx::Tween::FAST_OUT_LINEAR_IN
);
360 settings
.SetTransitionDuration(
361 base::TimeDelta::FromMilliseconds(kHideAnimationDurationMs
));
362 gfx::Transform transform
;
363 transform
.Translate(0, kAnimationDistance
);
364 container_
->SetTransform(transform
);
365 container_
->layer()->SetOpacity(kAnimationStartOrAfterHideOpacity
);
368 void KeyboardController::AddObserver(KeyboardControllerObserver
* observer
) {
369 observer_list_
.AddObserver(observer
);
372 void KeyboardController::RemoveObserver(KeyboardControllerObserver
* observer
) {
373 observer_list_
.RemoveObserver(observer
);
376 void KeyboardController::SetKeyboardMode(KeyboardMode mode
) {
377 keyboard_mode_
= mode
;
380 void KeyboardController::ShowKeyboard(bool lock
) {
381 set_lock_keyboard(lock
);
382 ShowKeyboardInternal();
385 void KeyboardController::OnWindowHierarchyChanged(
386 const HierarchyChangeParams
& params
) {
387 if (params
.new_parent
&& params
.target
== container_
.get())
388 OnTextInputStateChanged(proxy_
->GetInputMethod()->GetTextInputClient());
391 void KeyboardController::Reload() {
392 if (proxy_
->HasKeyboardWindow()) {
393 // A reload should never try to show virtual keyboard. If keyboard is not
394 // visible before reload, it should keep invisible after reload.
395 show_on_resize_
= false;
396 proxy_
->ReloadKeyboardIfNeeded();
400 void KeyboardController::OnTextInputStateChanged(
401 const ui::TextInputClient
* client
) {
402 if (!container_
.get())
405 type_
= client
? client
->GetTextInputType() : ui::TEXT_INPUT_TYPE_NONE
;
407 if (type_
== ui::TEXT_INPUT_TYPE_NONE
&& !lock_keyboard_
) {
408 if (keyboard_visible_
) {
409 // Set the visibility state here so that any queries for visibility
410 // before the timer fires returns the correct future value.
411 keyboard_visible_
= false;
412 base::MessageLoop::current()->PostDelayedTask(
414 base::Bind(&KeyboardController::HideKeyboard
,
415 weak_factory_
.GetWeakPtr(), HIDE_REASON_AUTOMATIC
),
416 base::TimeDelta::FromMilliseconds(kHideKeyboardDelayMs
));
419 // Abort a pending keyboard hide.
420 if (WillHideKeyboard()) {
421 weak_factory_
.InvalidateWeakPtrs();
422 keyboard_visible_
= true;
424 proxy_
->SetUpdateInputType(type_
);
425 // Do not explicitly show the Virtual keyboard unless it is in the process
426 // of hiding. Instead, the virtual keyboard is shown in response to a user
427 // gesture (mouse or touch) that is received while an element has input
428 // focus. Showing the keyboard requires an explicit call to
429 // OnShowImeIfNeeded.
433 void KeyboardController::OnInputMethodDestroyed(
434 const ui::InputMethod
* input_method
) {
435 DCHECK_EQ(input_method_
, input_method
);
436 input_method_
= NULL
;
439 void KeyboardController::OnShowImeIfNeeded() {
440 ShowKeyboardInternal();
443 bool KeyboardController::ShouldEnableInsets(aura::Window
* window
) {
444 aura::Window
*keyboard_window
= proxy_
->GetKeyboardWindow();
445 return (keyboard_window
->GetRootWindow() == window
->GetRootWindow() &&
446 keyboard::IsKeyboardOverscrollEnabled() &&
447 proxy_
->GetKeyboardWindow()->IsVisible() &&
451 void KeyboardController::UpdateWindowInsets(aura::Window
* window
) {
452 aura::Window
*keyboard_window
= proxy_
->GetKeyboardWindow();
453 if (window
== keyboard_window
)
456 scoped_ptr
<content::RenderWidgetHostIterator
> widgets(
457 content::RenderWidgetHost::GetRenderWidgetHosts());
458 while (content::RenderWidgetHost
* widget
= widgets
->GetNextHost()) {
459 content::RenderWidgetHostView
* view
= widget
->GetView();
460 if (view
&& window
->Contains(view
->GetNativeView())) {
461 gfx::Rect window_bounds
= view
->GetNativeView()->GetBoundsInScreen();
462 gfx::Rect intersect
= gfx::IntersectRects(window_bounds
,
463 proxy_
->GetKeyboardWindow()->bounds());
464 int overlap
= ShouldEnableInsets(window
) ? intersect
.height() : 0;
465 if (overlap
> 0 && overlap
< window_bounds
.height())
466 view
->SetInsets(gfx::Insets(0, 0, overlap
, 0));
468 view
->SetInsets(gfx::Insets());
474 void KeyboardController::ShowKeyboardInternal() {
475 if (!container_
.get())
478 if (container_
->children().empty()) {
479 keyboard::MarkKeyboardLoadStarted();
480 aura::Window
* keyboard
= proxy_
->GetKeyboardWindow();
482 container_
->AddChild(keyboard
);
483 keyboard
->set_owned_by_parent(false);
486 proxy_
->ReloadKeyboardIfNeeded();
488 if (keyboard_visible_
) {
490 } else if (proxy_
->GetKeyboardWindow()->bounds().height() == 0) {
491 show_on_resize_
= true;
495 keyboard_visible_
= true;
497 // If the controller is in the process of hiding the keyboard, do not log
498 // the stat here since the keyboard will not actually be shown.
499 if (!WillHideKeyboard())
500 keyboard::LogKeyboardControlEvent(keyboard::KEYBOARD_CONTROL_SHOW
);
502 weak_factory_
.InvalidateWeakPtrs();
504 // If |container_| has hide animation, its visibility is set to false when
505 // hide animation finished. So even if the container is visible at this
506 // point, it may in the process of hiding. We still need to show keyboard
507 // container in this case.
508 if (container_
->IsVisible() &&
509 !container_
->layer()->GetAnimator()->is_animating())
512 ToggleTouchEventLogging(false);
513 ui::LayerAnimator
* container_animator
= container_
->layer()->GetAnimator();
515 // If the container is not animating, makes sure the position and opacity
516 // are at begin states for animation.
517 if (!container_animator
->is_animating()) {
518 gfx::Transform transform
;
519 transform
.Translate(0, kAnimationDistance
);
520 container_
->SetTransform(transform
);
521 container_
->layer()->SetOpacity(kAnimationStartOrAfterHideOpacity
);
524 container_animator
->set_preemption_strategy(
525 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET
);
526 animation_observer_
.reset(new CallbackAnimationObserver(
528 base::Bind(&KeyboardController::ShowAnimationFinished
,
529 base::Unretained(this))));
530 container_animator
->AddObserver(animation_observer_
.get());
532 proxy_
->ShowKeyboardContainer(container_
.get());
535 // Scope the following animation settings as we don't want to animate
536 // visibility change that triggered by a call to the base class function
537 // ShowKeyboardContainer with these settings. The container should become
538 // visible immediately.
539 ui::ScopedLayerAnimationSettings
settings(container_animator
);
540 settings
.SetTweenType(gfx::Tween::LINEAR_OUT_SLOW_IN
);
541 settings
.SetTransitionDuration(
542 base::TimeDelta::FromMilliseconds(kShowAnimationDurationMs
));
543 container_
->SetTransform(gfx::Transform());
544 container_
->layer()->SetOpacity(1.0);
548 void KeyboardController::ResetWindowInsets() {
549 const gfx::Insets insets
;
550 scoped_ptr
<content::RenderWidgetHostIterator
> widgets(
551 content::RenderWidgetHost::GetRenderWidgetHosts());
552 while (content::RenderWidgetHost
* widget
= widgets
->GetNextHost()) {
553 content::RenderWidgetHostView
* view
= widget
->GetView();
555 view
->SetInsets(insets
);
557 window_bounds_observer_
->RemoveAllObservedWindows();
560 bool KeyboardController::WillHideKeyboard() const {
561 return weak_factory_
.HasWeakPtrs();
564 void KeyboardController::ShowAnimationFinished() {
565 // Notify observers after animation finished to prevent reveal desktop
566 // background during animation.
567 NotifyKeyboardBoundsChanging(proxy_
->GetKeyboardWindow()->bounds());
568 proxy_
->EnsureCaretInWorkArea();
571 void KeyboardController::HideAnimationFinished() {
572 proxy_
->HideKeyboardContainer(container_
.get());
575 void KeyboardController::AddBoundsChangedObserver(aura::Window
* window
) {
576 aura::Window
* target_window
= window
? window
->GetToplevelWindow() : nullptr;
578 window_bounds_observer_
->AddObservedWindow(target_window
);
581 } // namespace keyboard