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"
35 #if defined(USE_OZONE)
36 #include "ui/ozone/public/input_controller.h"
37 #include "ui/ozone/public/ozone_platform.h"
39 #endif // if defined(OS_CHROMEOS)
43 const int kHideKeyboardDelayMs
= 100;
45 // The virtual keyboard show/hide animation duration.
46 const int kShowAnimationDurationMs
= 350;
47 const int kHideAnimationDurationMs
= 100;
49 // The opacity of virtual keyboard container when show animation starts or
50 // hide animation finishes. This cannot be zero because we call Show() on the
51 // keyboard window before setting the opacity back to 1.0. Since windows are not
52 // allowed to be shown with zero opacity, we always animate to 0.01 instead.
53 const float kAnimationStartOrAfterHideOpacity
= 0.01f
;
55 // The KeyboardWindowDelegate makes sure the keyboard-window does not get focus.
56 // This is necessary to make sure that the synthetic key-events reach the target
58 // The delegate deletes itself when the window is destroyed.
59 class KeyboardWindowDelegate
: public aura::WindowDelegate
{
61 KeyboardWindowDelegate() {}
62 ~KeyboardWindowDelegate() override
{}
65 // Overridden from aura::WindowDelegate:
66 gfx::Size
GetMinimumSize() const override
{ return gfx::Size(); }
67 gfx::Size
GetMaximumSize() const override
{ return gfx::Size(); }
68 void OnBoundsChanged(const gfx::Rect
& old_bounds
,
69 const gfx::Rect
& new_bounds
) override
{}
70 gfx::NativeCursor
GetCursor(const gfx::Point
& point
) override
{
71 return gfx::kNullCursor
;
73 int GetNonClientComponent(const gfx::Point
& point
) const override
{
76 bool ShouldDescendIntoChildForEventHandling(
78 const gfx::Point
& location
) override
{
81 bool CanFocus() override
{ return false; }
82 void OnCaptureLost() override
{}
83 void OnPaint(const ui::PaintContext
& context
) override
{}
84 void OnDeviceScaleFactorChanged(float device_scale_factor
) override
{}
85 void OnWindowDestroying(aura::Window
* window
) override
{}
86 void OnWindowDestroyed(aura::Window
* window
) override
{ delete this; }
87 void OnWindowTargetVisibilityChanged(bool visible
) override
{}
88 bool HasHitTestMask() const override
{ return false; }
89 void GetHitTestMask(gfx::Path
* mask
) const override
{}
91 DISALLOW_COPY_AND_ASSIGN(KeyboardWindowDelegate
);
94 void ToggleTouchEventLogging(bool enable
) {
95 #if defined(OS_CHROMEOS)
96 #if defined(USE_OZONE)
97 ui::OzonePlatform::GetInstance()
98 ->GetInputController()
99 ->SetTouchEventLoggingEnabled(enable
);
100 #elif defined(USE_X11)
101 if (!base::SysInfo::IsRunningOnChromeOS())
103 base::CommandLine
command(
104 base::FilePath("/opt/google/touchscreen/toggle_touch_event_logging"));
106 command
.AppendArg("1");
108 command
.AppendArg("0");
109 VLOG(1) << "Running " << command
.GetCommandLineString();
110 base::LaunchOptions options
;
112 base::LaunchProcess(command
, options
);
114 #endif // defined(OS_CHROMEOS)
121 // Observer for both keyboard show and hide animations. It should be owned by
122 // KeyboardController.
123 class CallbackAnimationObserver
: public ui::LayerAnimationObserver
{
125 CallbackAnimationObserver(const scoped_refptr
<ui::LayerAnimator
>& animator
,
126 base::Callback
<void(void)> callback
);
127 ~CallbackAnimationObserver() override
;
130 // Overridden from ui::LayerAnimationObserver:
131 void OnLayerAnimationEnded(ui::LayerAnimationSequence
* seq
) override
;
132 void OnLayerAnimationAborted(ui::LayerAnimationSequence
* seq
) override
;
133 void OnLayerAnimationScheduled(ui::LayerAnimationSequence
* seq
) override
{}
135 scoped_refptr
<ui::LayerAnimator
> animator_
;
136 base::Callback
<void(void)> callback_
;
138 DISALLOW_COPY_AND_ASSIGN(CallbackAnimationObserver
);
141 CallbackAnimationObserver::CallbackAnimationObserver(
142 const scoped_refptr
<ui::LayerAnimator
>& animator
,
143 base::Callback
<void(void)> callback
)
144 : animator_(animator
), callback_(callback
) {
147 CallbackAnimationObserver::~CallbackAnimationObserver() {
148 animator_
->RemoveObserver(this);
151 void CallbackAnimationObserver::OnLayerAnimationEnded(
152 ui::LayerAnimationSequence
* seq
) {
153 if (animator_
->is_animating())
155 animator_
->RemoveObserver(this);
159 void CallbackAnimationObserver::OnLayerAnimationAborted(
160 ui::LayerAnimationSequence
* seq
) {
161 animator_
->RemoveObserver(this);
164 class WindowBoundsChangeObserver
: public aura::WindowObserver
{
166 void OnWindowBoundsChanged(aura::Window
* window
,
167 const gfx::Rect
& old_bounds
,
168 const gfx::Rect
& new_bounds
) override
;
169 void OnWindowDestroyed(aura::Window
* window
) override
;
171 void AddObservedWindow(aura::Window
* window
);
172 void RemoveAllObservedWindows();
175 std::set
<aura::Window
*> observed_windows_
;
178 void WindowBoundsChangeObserver::OnWindowBoundsChanged(aura::Window
* window
,
179 const gfx::Rect
& old_bounds
, const gfx::Rect
& new_bounds
) {
180 KeyboardController
* controller
= KeyboardController::GetInstance();
182 controller
->UpdateWindowInsets(window
);
185 void WindowBoundsChangeObserver::OnWindowDestroyed(aura::Window
* window
) {
186 if (window
->HasObserver(this))
187 window
->RemoveObserver(this);
188 observed_windows_
.erase(window
);
191 void WindowBoundsChangeObserver::AddObservedWindow(aura::Window
* window
) {
192 if (!window
->HasObserver(this)) {
193 window
->AddObserver(this);
194 observed_windows_
.insert(window
);
198 void WindowBoundsChangeObserver::RemoveAllObservedWindows() {
199 for (std::set
<aura::Window
*>::iterator it
= observed_windows_
.begin();
200 it
!= observed_windows_
.end(); ++it
)
201 (*it
)->RemoveObserver(this);
202 observed_windows_
.clear();
206 KeyboardController
* KeyboardController::instance_
= NULL
;
208 KeyboardController::KeyboardController(KeyboardControllerProxy
* proxy
)
211 keyboard_visible_(false),
212 show_on_resize_(false),
213 lock_keyboard_(false),
214 keyboard_mode_(FULL_WIDTH
),
215 type_(ui::TEXT_INPUT_TYPE_NONE
),
216 weak_factory_(this) {
218 input_method_
= proxy_
->GetInputMethod();
219 input_method_
->AddObserver(this);
220 window_bounds_observer_
.reset(new WindowBoundsChangeObserver());
221 proxy_
->SetController(this);
224 KeyboardController::~KeyboardController() {
226 if (container_
->GetRootWindow())
227 container_
->GetRootWindow()->RemoveObserver(this);
228 container_
->RemoveObserver(this);
231 input_method_
->RemoveObserver(this);
233 proxy_
->SetController(nullptr);
237 void KeyboardController::ResetInstance(KeyboardController
* controller
) {
238 if (instance_
&& instance_
!= controller
)
240 instance_
= controller
;
244 KeyboardController
* KeyboardController::GetInstance() {
248 aura::Window
* KeyboardController::GetContainerWindow() {
249 if (!container_
.get()) {
250 container_
.reset(new aura::Window(new KeyboardWindowDelegate()));
251 container_
->SetName("KeyboardContainer");
252 container_
->set_owned_by_parent(false);
253 container_
->Init(ui::LAYER_NOT_DRAWN
);
254 container_
->AddObserver(this);
255 container_
->SetLayoutManager(new KeyboardLayoutManager(this));
257 return container_
.get();
260 void KeyboardController::NotifyKeyboardBoundsChanging(
261 const gfx::Rect
& new_bounds
) {
262 current_keyboard_bounds_
= new_bounds
;
263 if (proxy_
->HasKeyboardWindow() && proxy_
->GetKeyboardWindow()->IsVisible()) {
264 FOR_EACH_OBSERVER(KeyboardControllerObserver
,
266 OnKeyboardBoundsChanging(new_bounds
));
267 if (keyboard::IsKeyboardOverscrollEnabled()) {
268 // Adjust the height of the viewport for visible windows on the primary
270 // TODO(kevers): Add EnvObserver to properly initialize insets if a
271 // window is created while the keyboard is visible.
272 scoped_ptr
<content::RenderWidgetHostIterator
> widgets(
273 content::RenderWidgetHost::GetRenderWidgetHosts());
274 aura::Window
* keyboard_window
= proxy_
->GetKeyboardWindow();
275 aura::Window
* root_window
= keyboard_window
->GetRootWindow();
276 while (content::RenderWidgetHost
* widget
= widgets
->GetNextHost()) {
277 content::RenderWidgetHostView
* view
= widget
->GetView();
278 // Can be NULL, e.g. if the RenderWidget is being destroyed or
279 // the render process crashed.
281 aura::Window
* window
= view
->GetNativeView();
282 // If virtual keyboard failed to load, a widget that displays error
283 // message will be created and adds as a child of the virtual keyboard
284 // window. We want to avoid add BoundsChangedObserver to that window.
285 if (!keyboard_window
->Contains(window
) &&
286 window
->GetRootWindow() == root_window
) {
287 gfx::Rect window_bounds
= window
->GetBoundsInScreen();
288 gfx::Rect intersect
= gfx::IntersectRects(window_bounds
,
290 int overlap
= intersect
.height();
291 if (overlap
> 0 && overlap
< window_bounds
.height())
292 view
->SetInsets(gfx::Insets(0, 0, overlap
, 0));
294 view
->SetInsets(gfx::Insets());
295 AddBoundsChangedObserver(window
);
303 current_keyboard_bounds_
= gfx::Rect();
307 void KeyboardController::HideKeyboard(HideReason reason
) {
308 keyboard_visible_
= false;
309 ToggleTouchEventLogging(true);
311 keyboard::LogKeyboardControlEvent(
312 reason
== HIDE_REASON_AUTOMATIC
?
313 keyboard::KEYBOARD_CONTROL_HIDE_AUTO
:
314 keyboard::KEYBOARD_CONTROL_HIDE_USER
);
316 NotifyKeyboardBoundsChanging(gfx::Rect());
318 set_lock_keyboard(false);
320 ui::LayerAnimator
* container_animator
= container_
->layer()->GetAnimator();
321 animation_observer_
.reset(new CallbackAnimationObserver(
323 base::Bind(&KeyboardController::HideAnimationFinished
,
324 base::Unretained(this))));
325 container_animator
->AddObserver(animation_observer_
.get());
327 ui::ScopedLayerAnimationSettings
settings(container_animator
);
328 settings
.SetTweenType(gfx::Tween::FAST_OUT_LINEAR_IN
);
329 settings
.SetTransitionDuration(
330 base::TimeDelta::FromMilliseconds(kHideAnimationDurationMs
));
331 gfx::Transform transform
;
332 transform
.Translate(0, kAnimationDistance
);
333 container_
->SetTransform(transform
);
334 container_
->layer()->SetOpacity(kAnimationStartOrAfterHideOpacity
);
337 void KeyboardController::AddObserver(KeyboardControllerObserver
* observer
) {
338 observer_list_
.AddObserver(observer
);
341 void KeyboardController::RemoveObserver(KeyboardControllerObserver
* observer
) {
342 observer_list_
.RemoveObserver(observer
);
345 void KeyboardController::SetKeyboardMode(KeyboardMode mode
) {
346 if (keyboard_mode_
== mode
)
349 keyboard_mode_
= mode
;
350 // When keyboard is floating, no overscroll or resize is necessary. Sets
351 // keyboard bounds to zero so overscroll or resize is disabled.
352 if (keyboard_mode_
== FLOATING
) {
353 NotifyKeyboardBoundsChanging(gfx::Rect());
354 } else if (keyboard_mode_
== FULL_WIDTH
) {
355 // TODO(bshe): revisit this logic after we decide to support resize virtual
357 int keyboard_height
= GetContainerWindow()->bounds().height();
358 const gfx::Rect
& root_bounds
= container_
->GetRootWindow()->bounds();
359 gfx::Rect new_bounds
= root_bounds
;
360 new_bounds
.set_y(root_bounds
.height() - keyboard_height
);
361 new_bounds
.set_height(keyboard_height
);
362 GetContainerWindow()->SetBounds(new_bounds
);
363 // No animation added, so call ShowAnimationFinished immediately.
364 ShowAnimationFinished();
368 void KeyboardController::ShowKeyboard(bool lock
) {
369 set_lock_keyboard(lock
);
370 ShowKeyboardInternal();
373 void KeyboardController::OnWindowHierarchyChanged(
374 const HierarchyChangeParams
& params
) {
375 if (params
.new_parent
&& params
.target
== container_
.get())
376 OnTextInputStateChanged(proxy_
->GetInputMethod()->GetTextInputClient());
379 void KeyboardController::OnWindowAddedToRootWindow(aura::Window
* window
) {
380 if (!window
->GetRootWindow()->HasObserver(this))
381 window
->GetRootWindow()->AddObserver(this);
384 void KeyboardController::OnWindowRemovingFromRootWindow(aura::Window
* window
,
385 aura::Window
* new_root
) {
386 if (window
->GetRootWindow()->HasObserver(this))
387 window
->GetRootWindow()->RemoveObserver(this);
390 void KeyboardController::OnWindowBoundsChanged(aura::Window
* window
,
391 const gfx::Rect
& old_bounds
,
392 const gfx::Rect
& new_bounds
) {
393 if (!window
->IsRootWindow())
395 // Keep the same height when window resize. It gets called when screen
397 if (!keyboard_container_initialized() || !proxy_
->HasKeyboardWindow())
400 int container_height
= container_
->bounds().height();
401 if (keyboard_mode_
== FULL_WIDTH
) {
402 container_
->SetBounds(gfx::Rect(new_bounds
.x(),
403 new_bounds
.bottom() - container_height
,
406 } else if (keyboard_mode_
== FLOATING
) {
407 // When screen rotate, horizontally center floating virtual keyboard
408 // window and vertically align it to the bottom.
409 int container_width
= container_
->bounds().width();
410 container_
->SetBounds(gfx::Rect(
411 new_bounds
.x() + (new_bounds
.width() - container_width
) / 2,
412 new_bounds
.bottom() - container_height
,
418 void KeyboardController::Reload() {
419 if (proxy_
->HasKeyboardWindow()) {
420 // A reload should never try to show virtual keyboard. If keyboard is not
421 // visible before reload, it should keep invisible after reload.
422 show_on_resize_
= false;
423 proxy_
->ReloadKeyboardIfNeeded();
427 void KeyboardController::OnTextInputStateChanged(
428 const ui::TextInputClient
* client
) {
429 if (!container_
.get())
432 type_
= client
? client
->GetTextInputType() : ui::TEXT_INPUT_TYPE_NONE
;
434 if (type_
== ui::TEXT_INPUT_TYPE_NONE
&& !lock_keyboard_
) {
435 if (keyboard_visible_
) {
436 // Set the visibility state here so that any queries for visibility
437 // before the timer fires returns the correct future value.
438 keyboard_visible_
= false;
439 base::MessageLoop::current()->PostDelayedTask(
441 base::Bind(&KeyboardController::HideKeyboard
,
442 weak_factory_
.GetWeakPtr(), HIDE_REASON_AUTOMATIC
),
443 base::TimeDelta::FromMilliseconds(kHideKeyboardDelayMs
));
446 // Abort a pending keyboard hide.
447 if (WillHideKeyboard()) {
448 weak_factory_
.InvalidateWeakPtrs();
449 keyboard_visible_
= true;
451 proxy_
->SetUpdateInputType(type_
);
452 // Do not explicitly show the Virtual keyboard unless it is in the process
453 // of hiding. Instead, the virtual keyboard is shown in response to a user
454 // gesture (mouse or touch) that is received while an element has input
455 // focus. Showing the keyboard requires an explicit call to
456 // OnShowImeIfNeeded.
460 void KeyboardController::OnInputMethodDestroyed(
461 const ui::InputMethod
* input_method
) {
462 DCHECK_EQ(input_method_
, input_method
);
463 input_method_
= NULL
;
466 void KeyboardController::OnShowImeIfNeeded() {
467 ShowKeyboardInternal();
470 bool KeyboardController::ShouldEnableInsets(aura::Window
* window
) {
471 aura::Window
* keyboard_window
= proxy_
->GetKeyboardWindow();
472 return (keyboard_window
->GetRootWindow() == window
->GetRootWindow() &&
473 keyboard::IsKeyboardOverscrollEnabled() &&
474 keyboard_window
->IsVisible() && keyboard_visible_
);
477 void KeyboardController::UpdateWindowInsets(aura::Window
* window
) {
478 aura::Window
* keyboard_window
= proxy_
->GetKeyboardWindow();
479 if (window
== keyboard_window
)
482 scoped_ptr
<content::RenderWidgetHostIterator
> widgets(
483 content::RenderWidgetHost::GetRenderWidgetHosts());
484 while (content::RenderWidgetHost
* widget
= widgets
->GetNextHost()) {
485 content::RenderWidgetHostView
* view
= widget
->GetView();
486 if (view
&& window
->Contains(view
->GetNativeView())) {
487 gfx::Rect window_bounds
= view
->GetNativeView()->GetBoundsInScreen();
488 gfx::Rect intersect
=
489 gfx::IntersectRects(window_bounds
, keyboard_window
->bounds());
490 int overlap
= ShouldEnableInsets(window
) ? intersect
.height() : 0;
491 if (overlap
> 0 && overlap
< window_bounds
.height())
492 view
->SetInsets(gfx::Insets(0, 0, overlap
, 0));
494 view
->SetInsets(gfx::Insets());
500 void KeyboardController::ShowKeyboardInternal() {
501 if (!container_
.get())
504 if (container_
->children().empty()) {
505 keyboard::MarkKeyboardLoadStarted();
506 aura::Window
* keyboard
= proxy_
->GetKeyboardWindow();
508 container_
->AddChild(keyboard
);
509 keyboard
->set_owned_by_parent(false);
512 proxy_
->ReloadKeyboardIfNeeded();
514 if (keyboard_visible_
) {
516 } else if (proxy_
->GetKeyboardWindow()->bounds().height() == 0) {
517 show_on_resize_
= true;
521 keyboard_visible_
= true;
523 // If the controller is in the process of hiding the keyboard, do not log
524 // the stat here since the keyboard will not actually be shown.
525 if (!WillHideKeyboard())
526 keyboard::LogKeyboardControlEvent(keyboard::KEYBOARD_CONTROL_SHOW
);
528 weak_factory_
.InvalidateWeakPtrs();
530 // If |container_| has hide animation, its visibility is set to false when
531 // hide animation finished. So even if the container is visible at this
532 // point, it may in the process of hiding. We still need to show keyboard
533 // container in this case.
534 if (container_
->IsVisible() &&
535 !container_
->layer()->GetAnimator()->is_animating())
538 ToggleTouchEventLogging(false);
539 ui::LayerAnimator
* container_animator
= container_
->layer()->GetAnimator();
541 // If the container is not animating, makes sure the position and opacity
542 // are at begin states for animation.
543 if (!container_animator
->is_animating()) {
544 gfx::Transform transform
;
545 transform
.Translate(0, kAnimationDistance
);
546 container_
->SetTransform(transform
);
547 container_
->layer()->SetOpacity(kAnimationStartOrAfterHideOpacity
);
550 container_animator
->set_preemption_strategy(
551 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET
);
552 if (keyboard_mode_
== FLOATING
) {
553 animation_observer_
.reset();
555 animation_observer_
.reset(new CallbackAnimationObserver(
557 base::Bind(&KeyboardController::ShowAnimationFinished
,
558 base::Unretained(this))));
559 container_animator
->AddObserver(animation_observer_
.get());
562 proxy_
->ShowKeyboardContainer(container_
.get());
565 // Scope the following animation settings as we don't want to animate
566 // visibility change that triggered by a call to the base class function
567 // ShowKeyboardContainer with these settings. The container should become
568 // visible immediately.
569 ui::ScopedLayerAnimationSettings
settings(container_animator
);
570 settings
.SetTweenType(gfx::Tween::LINEAR_OUT_SLOW_IN
);
571 settings
.SetTransitionDuration(
572 base::TimeDelta::FromMilliseconds(kShowAnimationDurationMs
));
573 container_
->SetTransform(gfx::Transform());
574 container_
->layer()->SetOpacity(1.0);
578 void KeyboardController::ResetWindowInsets() {
579 const gfx::Insets insets
;
580 scoped_ptr
<content::RenderWidgetHostIterator
> widgets(
581 content::RenderWidgetHost::GetRenderWidgetHosts());
582 while (content::RenderWidgetHost
* widget
= widgets
->GetNextHost()) {
583 content::RenderWidgetHostView
* view
= widget
->GetView();
585 view
->SetInsets(insets
);
587 window_bounds_observer_
->RemoveAllObservedWindows();
590 bool KeyboardController::WillHideKeyboard() const {
591 return weak_factory_
.HasWeakPtrs();
594 void KeyboardController::ShowAnimationFinished() {
595 // Notify observers after animation finished to prevent reveal desktop
596 // background during animation.
597 NotifyKeyboardBoundsChanging(container_
->bounds());
598 proxy_
->EnsureCaretInWorkArea();
601 void KeyboardController::HideAnimationFinished() {
602 proxy_
->HideKeyboardContainer(container_
.get());
605 void KeyboardController::AddBoundsChangedObserver(aura::Window
* window
) {
606 aura::Window
* target_window
= window
? window
->GetToplevelWindow() : nullptr;
608 window_bounds_observer_
->AddObservedWindow(target_window
);
611 } // namespace keyboard