Small clean up to about_flags_unittest.cc.
[chromium-blink-merge.git] / ui / keyboard / keyboard_controller.cc
blob351cf5774bcaa36ac754f985ebd71c85939cb31d
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 <set>
9 #include "base/bind.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 #endif
37 namespace {
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 // The KeyboardWindowDelegate makes sure the keyboard-window does not get focus.
52 // This is necessary to make sure that the synthetic key-events reach the target
53 // window.
54 // The delegate deletes itself when the window is destroyed.
55 class KeyboardWindowDelegate : public aura::WindowDelegate {
56 public:
57 KeyboardWindowDelegate() {}
58 ~KeyboardWindowDelegate() override {}
60 private:
61 // Overridden from aura::WindowDelegate:
62 gfx::Size GetMinimumSize() const override { return gfx::Size(); }
63 gfx::Size GetMaximumSize() const override { return gfx::Size(); }
64 void OnBoundsChanged(const gfx::Rect& old_bounds,
65 const gfx::Rect& new_bounds) override {}
66 ui::TextInputClient* GetFocusedTextInputClient() override {
67 return nullptr;
69 gfx::NativeCursor GetCursor(const gfx::Point& point) override {
70 return gfx::kNullCursor;
72 int GetNonClientComponent(const gfx::Point& point) const override {
73 return HTNOWHERE;
75 bool ShouldDescendIntoChildForEventHandling(
76 aura::Window* child,
77 const gfx::Point& location) override {
78 return true;
80 bool CanFocus() override { return false; }
81 void OnCaptureLost() override {}
82 void OnPaint(const ui::PaintContext& context) override {}
83 void OnDeviceScaleFactorChanged(float device_scale_factor) override {}
84 void OnWindowDestroying(aura::Window* window) override {}
85 void OnWindowDestroyed(aura::Window* window) override { delete this; }
86 void OnWindowTargetVisibilityChanged(bool visible) override {}
87 bool HasHitTestMask() const override { return false; }
88 void GetHitTestMask(gfx::Path* mask) const override {}
90 DISALLOW_COPY_AND_ASSIGN(KeyboardWindowDelegate);
93 void ToggleTouchEventLogging(bool enable) {
94 #if defined(OS_CHROMEOS)
95 if (!base::SysInfo::IsRunningOnChromeOS())
96 return;
97 base::CommandLine command(
98 base::FilePath("/opt/google/touchscreen/toggle_touch_event_logging"));
99 if (enable)
100 command.AppendArg("1");
101 else
102 command.AppendArg("0");
103 VLOG(1) << "Running " << command.GetCommandLineString();
104 base::LaunchOptions options;
105 options.wait = true;
106 base::LaunchProcess(command, options);
107 #endif
110 } // namespace
112 namespace keyboard {
114 // Observer for both keyboard show and hide animations. It should be owned by
115 // KeyboardController.
116 class CallbackAnimationObserver : public ui::LayerAnimationObserver {
117 public:
118 CallbackAnimationObserver(const scoped_refptr<ui::LayerAnimator>& animator,
119 base::Callback<void(void)> callback);
120 ~CallbackAnimationObserver() override;
122 private:
123 // Overridden from ui::LayerAnimationObserver:
124 void OnLayerAnimationEnded(ui::LayerAnimationSequence* seq) override;
125 void OnLayerAnimationAborted(ui::LayerAnimationSequence* seq) override;
126 void OnLayerAnimationScheduled(ui::LayerAnimationSequence* seq) override {}
128 scoped_refptr<ui::LayerAnimator> animator_;
129 base::Callback<void(void)> callback_;
131 DISALLOW_COPY_AND_ASSIGN(CallbackAnimationObserver);
134 CallbackAnimationObserver::CallbackAnimationObserver(
135 const scoped_refptr<ui::LayerAnimator>& animator,
136 base::Callback<void(void)> callback)
137 : animator_(animator), callback_(callback) {
140 CallbackAnimationObserver::~CallbackAnimationObserver() {
141 animator_->RemoveObserver(this);
144 void CallbackAnimationObserver::OnLayerAnimationEnded(
145 ui::LayerAnimationSequence* seq) {
146 if (animator_->is_animating())
147 return;
148 animator_->RemoveObserver(this);
149 callback_.Run();
152 void CallbackAnimationObserver::OnLayerAnimationAborted(
153 ui::LayerAnimationSequence* seq) {
154 animator_->RemoveObserver(this);
157 class WindowBoundsChangeObserver : public aura::WindowObserver {
158 public:
159 void OnWindowBoundsChanged(aura::Window* window,
160 const gfx::Rect& old_bounds,
161 const gfx::Rect& new_bounds) override;
162 void OnWindowDestroyed(aura::Window* window) override;
164 void AddObservedWindow(aura::Window* window);
165 void RemoveAllObservedWindows();
167 private:
168 std::set<aura::Window*> observed_windows_;
171 void WindowBoundsChangeObserver::OnWindowBoundsChanged(aura::Window* window,
172 const gfx::Rect& old_bounds, const gfx::Rect& new_bounds) {
173 KeyboardController* controller = KeyboardController::GetInstance();
174 if (controller)
175 controller->UpdateWindowInsets(window);
178 void WindowBoundsChangeObserver::OnWindowDestroyed(aura::Window* window) {
179 if (window->HasObserver(this))
180 window->RemoveObserver(this);
181 observed_windows_.erase(window);
184 void WindowBoundsChangeObserver::AddObservedWindow(aura::Window* window) {
185 if (!window->HasObserver(this)) {
186 window->AddObserver(this);
187 observed_windows_.insert(window);
191 void WindowBoundsChangeObserver::RemoveAllObservedWindows() {
192 for (std::set<aura::Window*>::iterator it = observed_windows_.begin();
193 it != observed_windows_.end(); ++it)
194 (*it)->RemoveObserver(this);
195 observed_windows_.clear();
198 // static
199 KeyboardController* KeyboardController::instance_ = NULL;
201 KeyboardController::KeyboardController(KeyboardControllerProxy* proxy)
202 : proxy_(proxy),
203 input_method_(NULL),
204 keyboard_visible_(false),
205 show_on_resize_(false),
206 lock_keyboard_(false),
207 keyboard_mode_(FULL_WIDTH),
208 type_(ui::TEXT_INPUT_TYPE_NONE),
209 weak_factory_(this) {
210 CHECK(proxy);
211 input_method_ = proxy_->GetInputMethod();
212 input_method_->AddObserver(this);
213 window_bounds_observer_.reset(new WindowBoundsChangeObserver());
214 proxy_->SetController(this);
217 KeyboardController::~KeyboardController() {
218 if (container_) {
219 if (container_->GetRootWindow())
220 container_->GetRootWindow()->RemoveObserver(this);
221 container_->RemoveObserver(this);
223 if (input_method_)
224 input_method_->RemoveObserver(this);
225 ResetWindowInsets();
226 proxy_->SetController(nullptr);
229 // static
230 void KeyboardController::ResetInstance(KeyboardController* controller) {
231 if (instance_ && instance_ != controller)
232 delete instance_;
233 instance_ = controller;
236 // static
237 KeyboardController* KeyboardController::GetInstance() {
238 return instance_;
241 aura::Window* KeyboardController::GetContainerWindow() {
242 if (!container_.get()) {
243 container_.reset(new aura::Window(new KeyboardWindowDelegate()));
244 container_->SetName("KeyboardContainer");
245 container_->set_owned_by_parent(false);
246 container_->Init(ui::LAYER_NOT_DRAWN);
247 container_->AddObserver(this);
248 container_->SetLayoutManager(new KeyboardLayoutManager(this));
250 return container_.get();
253 void KeyboardController::NotifyKeyboardBoundsChanging(
254 const gfx::Rect& new_bounds) {
255 current_keyboard_bounds_ = new_bounds;
256 if (proxy_->HasKeyboardWindow() && proxy_->GetKeyboardWindow()->IsVisible()) {
257 FOR_EACH_OBSERVER(KeyboardControllerObserver,
258 observer_list_,
259 OnKeyboardBoundsChanging(new_bounds));
260 if (keyboard::IsKeyboardOverscrollEnabled()) {
261 // Adjust the height of the viewport for visible windows on the primary
262 // display.
263 // TODO(kevers): Add EnvObserver to properly initialize insets if a
264 // window is created while the keyboard is visible.
265 scoped_ptr<content::RenderWidgetHostIterator> widgets(
266 content::RenderWidgetHost::GetRenderWidgetHosts());
267 aura::Window* keyboard_window = proxy_->GetKeyboardWindow();
268 aura::Window* root_window = keyboard_window->GetRootWindow();
269 while (content::RenderWidgetHost* widget = widgets->GetNextHost()) {
270 content::RenderWidgetHostView* view = widget->GetView();
271 // Can be NULL, e.g. if the RenderWidget is being destroyed or
272 // the render process crashed.
273 if (view) {
274 aura::Window* window = view->GetNativeView();
275 // If virtual keyboard failed to load, a widget that displays error
276 // message will be created and adds as a child of the virtual keyboard
277 // window. We want to avoid add BoundsChangedObserver to that window.
278 if (!keyboard_window->Contains(window) &&
279 window->GetRootWindow() == root_window) {
280 gfx::Rect window_bounds = window->GetBoundsInScreen();
281 gfx::Rect intersect = gfx::IntersectRects(window_bounds,
282 new_bounds);
283 int overlap = intersect.height();
284 if (overlap > 0 && overlap < window_bounds.height())
285 view->SetInsets(gfx::Insets(0, 0, overlap, 0));
286 else
287 view->SetInsets(gfx::Insets());
288 AddBoundsChangedObserver(window);
292 } else {
293 ResetWindowInsets();
295 } else {
296 current_keyboard_bounds_ = gfx::Rect();
300 void KeyboardController::HideKeyboard(HideReason reason) {
301 keyboard_visible_ = false;
302 ToggleTouchEventLogging(true);
304 keyboard::LogKeyboardControlEvent(
305 reason == HIDE_REASON_AUTOMATIC ?
306 keyboard::KEYBOARD_CONTROL_HIDE_AUTO :
307 keyboard::KEYBOARD_CONTROL_HIDE_USER);
309 NotifyKeyboardBoundsChanging(gfx::Rect());
311 set_lock_keyboard(false);
313 ui::LayerAnimator* container_animator = container_->layer()->GetAnimator();
314 animation_observer_.reset(new CallbackAnimationObserver(
315 container_animator,
316 base::Bind(&KeyboardController::HideAnimationFinished,
317 base::Unretained(this))));
318 container_animator->AddObserver(animation_observer_.get());
320 ui::ScopedLayerAnimationSettings settings(container_animator);
321 settings.SetTweenType(gfx::Tween::FAST_OUT_LINEAR_IN);
322 settings.SetTransitionDuration(
323 base::TimeDelta::FromMilliseconds(kHideAnimationDurationMs));
324 gfx::Transform transform;
325 transform.Translate(0, kAnimationDistance);
326 container_->SetTransform(transform);
327 container_->layer()->SetOpacity(kAnimationStartOrAfterHideOpacity);
330 void KeyboardController::AddObserver(KeyboardControllerObserver* observer) {
331 observer_list_.AddObserver(observer);
334 void KeyboardController::RemoveObserver(KeyboardControllerObserver* observer) {
335 observer_list_.RemoveObserver(observer);
338 void KeyboardController::SetKeyboardMode(KeyboardMode mode) {
339 if (keyboard_mode_ == mode)
340 return;
342 keyboard_mode_ = mode;
343 // When keyboard is floating, no overscroll or resize is necessary. Sets
344 // keyboard bounds to zero so overscroll or resize is disabled.
345 if (keyboard_mode_ == FLOATING) {
346 NotifyKeyboardBoundsChanging(gfx::Rect());
347 } else if (keyboard_mode_ == FULL_WIDTH) {
348 // TODO(bshe): revisit this logic after we decide to support resize virtual
349 // keyboard.
350 int keyboard_height = GetContainerWindow()->bounds().height();
351 const gfx::Rect& root_bounds = container_->GetRootWindow()->bounds();
352 gfx::Rect new_bounds = root_bounds;
353 new_bounds.set_y(root_bounds.height() - keyboard_height);
354 new_bounds.set_height(keyboard_height);
355 GetContainerWindow()->SetBounds(new_bounds);
356 // No animation added, so call ShowAnimationFinished immediately.
357 ShowAnimationFinished();
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::OnWindowAddedToRootWindow(aura::Window* window) {
373 if (!window->GetRootWindow()->HasObserver(this))
374 window->GetRootWindow()->AddObserver(this);
377 void KeyboardController::OnWindowRemovingFromRootWindow(aura::Window* window,
378 aura::Window* new_root) {
379 if (window->GetRootWindow()->HasObserver(this))
380 window->GetRootWindow()->RemoveObserver(this);
383 void KeyboardController::OnWindowBoundsChanged(aura::Window* window,
384 const gfx::Rect& old_bounds,
385 const gfx::Rect& new_bounds) {
386 if (!window->IsRootWindow())
387 return;
388 // Keep the same height when window resize. It gets called when screen
389 // rotate.
390 if (!keyboard_container_initialized() || !proxy_->HasKeyboardWindow())
391 return;
393 int container_height = container_->bounds().height();
394 if (keyboard_mode_ == FULL_WIDTH) {
395 container_->SetBounds(gfx::Rect(new_bounds.x(),
396 new_bounds.bottom() - container_height,
397 new_bounds.width(),
398 container_height));
399 } else if (keyboard_mode_ == FLOATING) {
400 // When screen rotate, horizontally center floating virtual keyboard
401 // window and vertically align it to the bottom.
402 int container_width = container_->bounds().width();
403 container_->SetBounds(gfx::Rect(
404 new_bounds.x() + (new_bounds.width() - container_width) / 2,
405 new_bounds.bottom() - container_height,
406 container_width,
407 container_height));
411 void KeyboardController::Reload() {
412 if (proxy_->HasKeyboardWindow()) {
413 // A reload should never try to show virtual keyboard. If keyboard is not
414 // visible before reload, it should keep invisible after reload.
415 show_on_resize_ = false;
416 proxy_->ReloadKeyboardIfNeeded();
420 void KeyboardController::OnTextInputStateChanged(
421 const ui::TextInputClient* client) {
422 if (!container_.get())
423 return;
425 type_ = client ? client->GetTextInputType() : ui::TEXT_INPUT_TYPE_NONE;
427 if (type_ == ui::TEXT_INPUT_TYPE_NONE && !lock_keyboard_) {
428 if (keyboard_visible_) {
429 // Set the visibility state here so that any queries for visibility
430 // before the timer fires returns the correct future value.
431 keyboard_visible_ = false;
432 base::MessageLoop::current()->PostDelayedTask(
433 FROM_HERE,
434 base::Bind(&KeyboardController::HideKeyboard,
435 weak_factory_.GetWeakPtr(), HIDE_REASON_AUTOMATIC),
436 base::TimeDelta::FromMilliseconds(kHideKeyboardDelayMs));
438 } else {
439 // Abort a pending keyboard hide.
440 if (WillHideKeyboard()) {
441 weak_factory_.InvalidateWeakPtrs();
442 keyboard_visible_ = true;
444 proxy_->SetUpdateInputType(type_);
445 // Do not explicitly show the Virtual keyboard unless it is in the process
446 // of hiding. Instead, the virtual keyboard is shown in response to a user
447 // gesture (mouse or touch) that is received while an element has input
448 // focus. Showing the keyboard requires an explicit call to
449 // OnShowImeIfNeeded.
453 void KeyboardController::OnInputMethodDestroyed(
454 const ui::InputMethod* input_method) {
455 DCHECK_EQ(input_method_, input_method);
456 input_method_ = NULL;
459 void KeyboardController::OnShowImeIfNeeded() {
460 ShowKeyboardInternal();
463 bool KeyboardController::ShouldEnableInsets(aura::Window* window) {
464 aura::Window* keyboard_window = proxy_->GetKeyboardWindow();
465 return (keyboard_window->GetRootWindow() == window->GetRootWindow() &&
466 keyboard::IsKeyboardOverscrollEnabled() &&
467 keyboard_window->IsVisible() && keyboard_visible_);
470 void KeyboardController::UpdateWindowInsets(aura::Window* window) {
471 aura::Window* keyboard_window = proxy_->GetKeyboardWindow();
472 if (window == keyboard_window)
473 return;
475 scoped_ptr<content::RenderWidgetHostIterator> widgets(
476 content::RenderWidgetHost::GetRenderWidgetHosts());
477 while (content::RenderWidgetHost* widget = widgets->GetNextHost()) {
478 content::RenderWidgetHostView* view = widget->GetView();
479 if (view && window->Contains(view->GetNativeView())) {
480 gfx::Rect window_bounds = view->GetNativeView()->GetBoundsInScreen();
481 gfx::Rect intersect =
482 gfx::IntersectRects(window_bounds, keyboard_window->bounds());
483 int overlap = ShouldEnableInsets(window) ? intersect.height() : 0;
484 if (overlap > 0 && overlap < window_bounds.height())
485 view->SetInsets(gfx::Insets(0, 0, overlap, 0));
486 else
487 view->SetInsets(gfx::Insets());
488 return;
493 void KeyboardController::ShowKeyboardInternal() {
494 if (!container_.get())
495 return;
497 if (container_->children().empty()) {
498 keyboard::MarkKeyboardLoadStarted();
499 aura::Window* keyboard = proxy_->GetKeyboardWindow();
500 keyboard->Show();
501 container_->AddChild(keyboard);
502 keyboard->set_owned_by_parent(false);
505 proxy_->ReloadKeyboardIfNeeded();
507 if (keyboard_visible_) {
508 return;
509 } else if (proxy_->GetKeyboardWindow()->bounds().height() == 0) {
510 show_on_resize_ = true;
511 return;
514 keyboard_visible_ = true;
516 // If the controller is in the process of hiding the keyboard, do not log
517 // the stat here since the keyboard will not actually be shown.
518 if (!WillHideKeyboard())
519 keyboard::LogKeyboardControlEvent(keyboard::KEYBOARD_CONTROL_SHOW);
521 weak_factory_.InvalidateWeakPtrs();
523 // If |container_| has hide animation, its visibility is set to false when
524 // hide animation finished. So even if the container is visible at this
525 // point, it may in the process of hiding. We still need to show keyboard
526 // container in this case.
527 if (container_->IsVisible() &&
528 !container_->layer()->GetAnimator()->is_animating())
529 return;
531 ToggleTouchEventLogging(false);
532 ui::LayerAnimator* container_animator = container_->layer()->GetAnimator();
534 // If the container is not animating, makes sure the position and opacity
535 // are at begin states for animation.
536 if (!container_animator->is_animating()) {
537 gfx::Transform transform;
538 transform.Translate(0, kAnimationDistance);
539 container_->SetTransform(transform);
540 container_->layer()->SetOpacity(kAnimationStartOrAfterHideOpacity);
543 container_animator->set_preemption_strategy(
544 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
545 if (keyboard_mode_ == FLOATING) {
546 animation_observer_.reset();
547 } else {
548 animation_observer_.reset(new CallbackAnimationObserver(
549 container_animator,
550 base::Bind(&KeyboardController::ShowAnimationFinished,
551 base::Unretained(this))));
552 container_animator->AddObserver(animation_observer_.get());
555 proxy_->ShowKeyboardContainer(container_.get());
558 // Scope the following animation settings as we don't want to animate
559 // visibility change that triggered by a call to the base class function
560 // ShowKeyboardContainer with these settings. The container should become
561 // visible immediately.
562 ui::ScopedLayerAnimationSettings settings(container_animator);
563 settings.SetTweenType(gfx::Tween::LINEAR_OUT_SLOW_IN);
564 settings.SetTransitionDuration(
565 base::TimeDelta::FromMilliseconds(kShowAnimationDurationMs));
566 container_->SetTransform(gfx::Transform());
567 container_->layer()->SetOpacity(1.0);
571 void KeyboardController::ResetWindowInsets() {
572 const gfx::Insets insets;
573 scoped_ptr<content::RenderWidgetHostIterator> widgets(
574 content::RenderWidgetHost::GetRenderWidgetHosts());
575 while (content::RenderWidgetHost* widget = widgets->GetNextHost()) {
576 content::RenderWidgetHostView* view = widget->GetView();
577 if (view)
578 view->SetInsets(insets);
580 window_bounds_observer_->RemoveAllObservedWindows();
583 bool KeyboardController::WillHideKeyboard() const {
584 return weak_factory_.HasWeakPtrs();
587 void KeyboardController::ShowAnimationFinished() {
588 // Notify observers after animation finished to prevent reveal desktop
589 // background during animation.
590 NotifyKeyboardBoundsChanging(container_->bounds());
591 proxy_->EnsureCaretInWorkArea();
594 void KeyboardController::HideAnimationFinished() {
595 proxy_->HideKeyboardContainer(container_.get());
598 void KeyboardController::AddBoundsChangedObserver(aura::Window* window) {
599 aura::Window* target_window = window ? window->GetToplevelWindow() : nullptr;
600 if (target_window)
601 window_bounds_observer_->AddObservedWindow(target_window);
604 } // namespace keyboard