ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / ui / keyboard / keyboard_controller.cc
blob0ae21bc2120fe8a07a04f5cdcc047f49afb3c3ec
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 // Event targeter for the keyboard container.
52 class KeyboardContainerTargeter : public wm::MaskedWindowTargeter {
53 public:
54 KeyboardContainerTargeter(aura::Window* container,
55 keyboard::KeyboardControllerProxy* proxy)
56 : wm::MaskedWindowTargeter(container),
57 proxy_(proxy) {
60 ~KeyboardContainerTargeter() override {}
62 private:
63 // wm::MaskedWindowTargeter:
64 bool GetHitTestMask(aura::Window* window, gfx::Path* mask) const override {
65 if (proxy_ && !proxy_->HasKeyboardWindow())
66 return true;
67 gfx::Rect keyboard_bounds = proxy_ ? proxy_->GetKeyboardWindow()->bounds() :
68 keyboard::DefaultKeyboardBoundsFromWindowBounds(window->bounds());
69 mask->addRect(RectToSkRect(keyboard_bounds));
70 return true;
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
80 // window.
81 // The delegate deletes itself when the window is destroyed.
82 class KeyboardWindowDelegate : public aura::WindowDelegate {
83 public:
84 explicit KeyboardWindowDelegate(keyboard::KeyboardControllerProxy* proxy)
85 : proxy_(proxy) {}
86 ~KeyboardWindowDelegate() override {}
88 private:
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 {
94 bounds_ = new_bounds;
96 ui::TextInputClient* GetFocusedTextInputClient() override {
97 return nullptr;
99 gfx::NativeCursor GetCursor(const gfx::Point& point) override {
100 return gfx::kNullCursor;
102 int GetNonClientComponent(const gfx::Point& point) const override {
103 return HTNOWHERE;
105 bool ShouldDescendIntoChildForEventHandling(
106 aura::Window* child,
107 const gfx::Point& location) override {
108 return true;
110 bool CanFocus() override { return false; }
111 void OnCaptureLost() override {}
112 void OnPaint(gfx::Canvas* canvas) 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())
122 return;
123 gfx::Rect keyboard_bounds = proxy_ ? proxy_->GetKeyboardWindow()->bounds() :
124 keyboard::DefaultKeyboardBoundsFromWindowBounds(bounds_);
125 mask->addRect(RectToSkRect(keyboard_bounds));
128 gfx::Rect 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())
137 return;
138 base::CommandLine command(
139 base::FilePath("/opt/google/touchscreen/toggle_touch_event_logging"));
140 if (enable)
141 command.AppendArg("1");
142 else
143 command.AppendArg("0");
144 VLOG(1) << "Running " << command.GetCommandLineString();
145 base::LaunchOptions options;
146 options.wait = true;
147 base::LaunchProcess(command, options);
148 #endif
151 } // namespace
153 namespace keyboard {
155 // Observer for both keyboard show and hide animations. It should be owned by
156 // KeyboardController.
157 class CallbackAnimationObserver : public ui::LayerAnimationObserver {
158 public:
159 CallbackAnimationObserver(ui::LayerAnimator* animator,
160 base::Callback<void(void)> callback);
161 ~CallbackAnimationObserver() override;
163 private:
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())
187 return;
188 animator_->RemoveObserver(this);
189 callback_.Run();
192 void CallbackAnimationObserver::OnLayerAnimationAborted(
193 ui::LayerAnimationSequence* seq) {
194 animator_->RemoveObserver(this);
197 class WindowBoundsChangeObserver : public aura::WindowObserver {
198 public:
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();
207 private:
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();
214 if (controller)
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();
238 // static
239 KeyboardController* KeyboardController::instance_ = NULL;
241 KeyboardController::KeyboardController(KeyboardControllerProxy* proxy)
242 : proxy_(proxy),
243 input_method_(NULL),
244 keyboard_visible_(false),
245 show_on_resize_(false),
246 lock_keyboard_(false),
247 type_(ui::TEXT_INPUT_TYPE_NONE),
248 weak_factory_(this) {
249 CHECK(proxy);
250 input_method_ = proxy_->GetInputMethod();
251 input_method_->AddObserver(this);
252 window_bounds_observer_.reset(new WindowBoundsChangeObserver());
255 KeyboardController::~KeyboardController() {
256 if (container_)
257 container_->RemoveObserver(this);
258 if (input_method_)
259 input_method_->RemoveObserver(this);
260 ResetWindowInsets();
263 // static
264 void KeyboardController::ResetInstance(KeyboardController* controller) {
265 if (instance_ && instance_ != controller)
266 delete instance_;
267 instance_ = controller;
270 // static
271 KeyboardController* KeyboardController::GetInstance() {
272 return instance_;
275 aura::Window* KeyboardController::GetContainerWindow() {
276 if (!container_.get()) {
277 container_.reset(new aura::Window(
278 new KeyboardWindowDelegate(proxy_.get())));
279 container_->SetEventTargeter(scoped_ptr<ui::EventTargeter>(
280 new KeyboardContainerTargeter(container_.get(), proxy_.get())));
281 container_->SetName("KeyboardContainer");
282 container_->set_owned_by_parent(false);
283 container_->Init(aura::WINDOW_LAYER_NOT_DRAWN);
284 container_->AddObserver(this);
285 container_->SetLayoutManager(new KeyboardLayoutManager(this));
287 return container_.get();
290 void KeyboardController::NotifyKeyboardBoundsChanging(
291 const gfx::Rect& new_bounds) {
292 current_keyboard_bounds_ = new_bounds;
293 if (proxy_->HasKeyboardWindow() && proxy_->GetKeyboardWindow()->IsVisible()) {
294 FOR_EACH_OBSERVER(KeyboardControllerObserver,
295 observer_list_,
296 OnKeyboardBoundsChanging(new_bounds));
297 if (keyboard::IsKeyboardOverscrollEnabled()) {
298 // Adjust the height of the viewport for visible windows on the primary
299 // display.
300 // TODO(kevers): Add EnvObserver to properly initialize insets if a
301 // window is created while the keyboard is visible.
302 scoped_ptr<content::RenderWidgetHostIterator> widgets(
303 content::RenderWidgetHost::GetRenderWidgetHosts());
304 aura::Window* keyboard_window = proxy_->GetKeyboardWindow();
305 aura::Window* root_window = keyboard_window->GetRootWindow();
306 while (content::RenderWidgetHost* widget = widgets->GetNextHost()) {
307 content::RenderWidgetHostView* view = widget->GetView();
308 // Can be NULL, e.g. if the RenderWidget is being destroyed or
309 // the render process crashed.
310 if (view) {
311 aura::Window* window = view->GetNativeView();
312 // If virtual keyboard failed to load, a widget that displays error
313 // message will be created and adds as a child of the virtual keyboard
314 // window. We want to avoid add BoundsChangedObserver to that window.
315 if (!keyboard_window->Contains(window) &&
316 window->GetRootWindow() == root_window) {
317 gfx::Rect window_bounds = window->GetBoundsInScreen();
318 gfx::Rect intersect = gfx::IntersectRects(window_bounds,
319 new_bounds);
320 int overlap = intersect.height();
321 if (overlap > 0 && overlap < window_bounds.height())
322 view->SetInsets(gfx::Insets(0, 0, overlap, 0));
323 else
324 view->SetInsets(gfx::Insets());
325 AddBoundsChangedObserver(window);
329 } else {
330 ResetWindowInsets();
332 } else {
333 current_keyboard_bounds_ = gfx::Rect();
337 void KeyboardController::HideKeyboard(HideReason reason) {
338 keyboard_visible_ = false;
339 ToggleTouchEventLogging(true);
341 keyboard::LogKeyboardControlEvent(
342 reason == HIDE_REASON_AUTOMATIC ?
343 keyboard::KEYBOARD_CONTROL_HIDE_AUTO :
344 keyboard::KEYBOARD_CONTROL_HIDE_USER);
346 NotifyKeyboardBoundsChanging(gfx::Rect());
348 set_lock_keyboard(false);
350 ui::LayerAnimator* container_animator = container_->layer()->GetAnimator();
351 animation_observer_.reset(new CallbackAnimationObserver(
352 container_animator,
353 base::Bind(&KeyboardController::HideAnimationFinished,
354 base::Unretained(this))));
355 container_animator->AddObserver(animation_observer_.get());
357 ui::ScopedLayerAnimationSettings settings(container_animator);
358 settings.SetTweenType(gfx::Tween::FAST_OUT_LINEAR_IN);
359 settings.SetTransitionDuration(
360 base::TimeDelta::FromMilliseconds(kHideAnimationDurationMs));
361 gfx::Transform transform;
362 transform.Translate(0, kAnimationDistance);
363 container_->SetTransform(transform);
364 container_->layer()->SetOpacity(kAnimationStartOrAfterHideOpacity);
367 void KeyboardController::AddObserver(KeyboardControllerObserver* observer) {
368 observer_list_.AddObserver(observer);
371 void KeyboardController::RemoveObserver(KeyboardControllerObserver* observer) {
372 observer_list_.RemoveObserver(observer);
375 void KeyboardController::ShowKeyboard(bool lock) {
376 set_lock_keyboard(lock);
377 ShowKeyboardInternal();
380 void KeyboardController::OnWindowHierarchyChanged(
381 const HierarchyChangeParams& params) {
382 if (params.new_parent && params.target == container_.get())
383 OnTextInputStateChanged(proxy_->GetInputMethod()->GetTextInputClient());
386 void KeyboardController::Reload() {
387 if (proxy_->HasKeyboardWindow()) {
388 // A reload should never try to show virtual keyboard. If keyboard is not
389 // visible before reload, it should keep invisible after reload.
390 show_on_resize_ = false;
391 proxy_->ReloadKeyboardIfNeeded();
395 void KeyboardController::OnTextInputStateChanged(
396 const ui::TextInputClient* client) {
397 if (!container_.get())
398 return;
400 type_ = client ? client->GetTextInputType() : ui::TEXT_INPUT_TYPE_NONE;
402 if (type_ == ui::TEXT_INPUT_TYPE_NONE && !lock_keyboard_) {
403 if (keyboard_visible_) {
404 // Set the visibility state here so that any queries for visibility
405 // before the timer fires returns the correct future value.
406 keyboard_visible_ = false;
407 base::MessageLoop::current()->PostDelayedTask(
408 FROM_HERE,
409 base::Bind(&KeyboardController::HideKeyboard,
410 weak_factory_.GetWeakPtr(), HIDE_REASON_AUTOMATIC),
411 base::TimeDelta::FromMilliseconds(kHideKeyboardDelayMs));
413 } else {
414 // Abort a pending keyboard hide.
415 if (WillHideKeyboard()) {
416 weak_factory_.InvalidateWeakPtrs();
417 keyboard_visible_ = true;
419 proxy_->SetUpdateInputType(type_);
420 // Do not explicitly show the Virtual keyboard unless it is in the process
421 // of hiding. Instead, the virtual keyboard is shown in response to a user
422 // gesture (mouse or touch) that is received while an element has input
423 // focus. Showing the keyboard requires an explicit call to
424 // OnShowImeIfNeeded.
428 void KeyboardController::OnInputMethodDestroyed(
429 const ui::InputMethod* input_method) {
430 DCHECK_EQ(input_method_, input_method);
431 input_method_ = NULL;
434 void KeyboardController::OnShowImeIfNeeded() {
435 ShowKeyboardInternal();
438 bool KeyboardController::ShouldEnableInsets(aura::Window* window) {
439 aura::Window *keyboard_window = proxy_->GetKeyboardWindow();
440 return (keyboard_window->GetRootWindow() == window->GetRootWindow() &&
441 keyboard::IsKeyboardOverscrollEnabled() &&
442 proxy_->GetKeyboardWindow()->IsVisible() &&
443 keyboard_visible_);
446 void KeyboardController::UpdateWindowInsets(aura::Window* window) {
447 aura::Window *keyboard_window = proxy_->GetKeyboardWindow();
448 if (window == keyboard_window)
449 return;
451 scoped_ptr<content::RenderWidgetHostIterator> widgets(
452 content::RenderWidgetHost::GetRenderWidgetHosts());
453 while (content::RenderWidgetHost* widget = widgets->GetNextHost()) {
454 content::RenderWidgetHostView* view = widget->GetView();
455 if (view && window->Contains(view->GetNativeView())) {
456 gfx::Rect window_bounds = view->GetNativeView()->GetBoundsInScreen();
457 gfx::Rect intersect = gfx::IntersectRects(window_bounds,
458 proxy_->GetKeyboardWindow()->bounds());
459 int overlap = ShouldEnableInsets(window) ? intersect.height() : 0;
460 if (overlap > 0 && overlap < window_bounds.height())
461 view->SetInsets(gfx::Insets(0, 0, overlap, 0));
462 else
463 view->SetInsets(gfx::Insets());
464 return;
469 void KeyboardController::ShowKeyboardInternal() {
470 if (!container_.get())
471 return;
473 if (container_->children().empty()) {
474 keyboard::MarkKeyboardLoadStarted();
475 aura::Window* keyboard = proxy_->GetKeyboardWindow();
476 keyboard->Show();
477 container_->AddChild(keyboard);
478 keyboard->set_owned_by_parent(false);
481 proxy_->ReloadKeyboardIfNeeded();
483 if (keyboard_visible_) {
484 return;
485 } else if (proxy_->GetKeyboardWindow()->bounds().height() == 0) {
486 show_on_resize_ = true;
487 return;
490 keyboard_visible_ = true;
492 // If the controller is in the process of hiding the keyboard, do not log
493 // the stat here since the keyboard will not actually be shown.
494 if (!WillHideKeyboard())
495 keyboard::LogKeyboardControlEvent(keyboard::KEYBOARD_CONTROL_SHOW);
497 weak_factory_.InvalidateWeakPtrs();
499 // If |container_| has hide animation, its visibility is set to false when
500 // hide animation finished. So even if the container is visible at this
501 // point, it may in the process of hiding. We still need to show keyboard
502 // container in this case.
503 if (container_->IsVisible() &&
504 !container_->layer()->GetAnimator()->is_animating())
505 return;
507 ToggleTouchEventLogging(false);
508 ui::LayerAnimator* container_animator = container_->layer()->GetAnimator();
510 // If the container is not animating, makes sure the position and opacity
511 // are at begin states for animation.
512 if (!container_animator->is_animating()) {
513 gfx::Transform transform;
514 transform.Translate(0, kAnimationDistance);
515 container_->SetTransform(transform);
516 container_->layer()->SetOpacity(kAnimationStartOrAfterHideOpacity);
519 container_animator->set_preemption_strategy(
520 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
521 animation_observer_.reset(new CallbackAnimationObserver(
522 container_animator,
523 base::Bind(&KeyboardController::ShowAnimationFinished,
524 base::Unretained(this))));
525 container_animator->AddObserver(animation_observer_.get());
527 proxy_->ShowKeyboardContainer(container_.get());
530 // Scope the following animation settings as we don't want to animate
531 // visibility change that triggered by a call to the base class function
532 // ShowKeyboardContainer with these settings. The container should become
533 // visible immediately.
534 ui::ScopedLayerAnimationSettings settings(container_animator);
535 settings.SetTweenType(gfx::Tween::LINEAR_OUT_SLOW_IN);
536 settings.SetTransitionDuration(
537 base::TimeDelta::FromMilliseconds(kShowAnimationDurationMs));
538 container_->SetTransform(gfx::Transform());
539 container_->layer()->SetOpacity(1.0);
543 void KeyboardController::ResetWindowInsets() {
544 const gfx::Insets insets;
545 scoped_ptr<content::RenderWidgetHostIterator> widgets(
546 content::RenderWidgetHost::GetRenderWidgetHosts());
547 while (content::RenderWidgetHost* widget = widgets->GetNextHost()) {
548 content::RenderWidgetHostView* view = widget->GetView();
549 if (view)
550 view->SetInsets(insets);
552 window_bounds_observer_->RemoveAllObservedWindows();
555 bool KeyboardController::WillHideKeyboard() const {
556 return weak_factory_.HasWeakPtrs();
559 void KeyboardController::ShowAnimationFinished() {
560 // Notify observers after animation finished to prevent reveal desktop
561 // background during animation.
562 NotifyKeyboardBoundsChanging(proxy_->GetKeyboardWindow()->bounds());
563 proxy_->EnsureCaretInWorkArea();
566 void KeyboardController::HideAnimationFinished() {
567 proxy_->HideKeyboardContainer(container_.get());
570 void KeyboardController::AddBoundsChangedObserver(aura::Window* window) {
571 aura::Window* target_window = window ? window->GetToplevelWindow() : nullptr;
572 if (target_window)
573 window_bounds_observer_->AddObservedWindow(target_window);
576 } // namespace keyboard