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"
8 #include "base/command_line.h"
9 #include "ui/aura/layout_manager.h"
10 #include "ui/aura/window.h"
11 #include "ui/aura/window_delegate.h"
12 #include "ui/base/cursor/cursor.h"
13 #include "ui/base/hit_test.h"
14 #include "ui/base/ime/input_method.h"
15 #include "ui/base/ime/text_input_client.h"
16 #include "ui/base/ime/text_input_type.h"
17 #include "ui/gfx/path.h"
18 #include "ui/gfx/rect.h"
19 #include "ui/gfx/skia_util.h"
20 #include "ui/keyboard/keyboard_controller_observer.h"
21 #include "ui/keyboard/keyboard_controller_proxy.h"
22 #include "ui/keyboard/keyboard_switches.h"
23 #include "ui/keyboard/keyboard_util.h"
27 const int kHideKeyboardDelayMs = 100;
29 gfx::Rect KeyboardBoundsFromWindowBounds(const gfx::Rect& window_bounds) {
30 const float kKeyboardHeightRatio = 0.3f;
33 window_bounds.y() + window_bounds.height() * (1 - kKeyboardHeightRatio),
34 window_bounds.width(),
35 window_bounds.height() * kKeyboardHeightRatio);
38 // The KeyboardWindowDelegate makes sure the keyboard-window does not get focus.
39 // This is necessary to make sure that the synthetic key-events reach the target
41 // The delegate deletes itself when the window is destroyed.
42 class KeyboardWindowDelegate : public aura::WindowDelegate {
44 KeyboardWindowDelegate() {}
45 virtual ~KeyboardWindowDelegate() {}
48 // Overridden from aura::WindowDelegate:
49 virtual gfx::Size GetMinimumSize() const OVERRIDE { return gfx::Size(); }
50 virtual gfx::Size GetMaximumSize() const OVERRIDE { return gfx::Size(); }
51 virtual void OnBoundsChanged(const gfx::Rect& old_bounds,
52 const gfx::Rect& new_bounds) OVERRIDE {
55 virtual gfx::NativeCursor GetCursor(const gfx::Point& point) OVERRIDE {
56 return gfx::kNullCursor;
58 virtual int GetNonClientComponent(const gfx::Point& point) const OVERRIDE {
61 virtual bool ShouldDescendIntoChildForEventHandling(
63 const gfx::Point& location) OVERRIDE {
66 virtual bool CanFocus() OVERRIDE { return false; }
67 virtual void OnCaptureLost() OVERRIDE {}
68 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {}
69 virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE {}
70 virtual void OnWindowDestroying() OVERRIDE {}
71 virtual void OnWindowDestroyed() OVERRIDE { delete this; }
72 virtual void OnWindowTargetVisibilityChanged(bool visible) OVERRIDE {}
73 virtual bool HasHitTestMask() const OVERRIDE { return true; }
74 virtual void GetHitTestMask(gfx::Path* mask) const OVERRIDE {
75 gfx::Rect keyboard_bounds = KeyboardBoundsFromWindowBounds(bounds_);
76 mask->addRect(RectToSkRect(keyboard_bounds));
78 virtual void DidRecreateLayer(ui::Layer* old_layer,
79 ui::Layer* new_layer) OVERRIDE {}
82 DISALLOW_COPY_AND_ASSIGN(KeyboardWindowDelegate);
89 // LayoutManager for the virtual keyboard container. Manages a single window
90 // (the virtual keyboard) and keeps it positioned at the bottom of the
92 class KeyboardLayoutManager : public aura::LayoutManager {
94 KeyboardLayoutManager(aura::Window* container)
95 : container_(container), keyboard_(NULL) {
99 // Overridden from aura::LayoutManager
100 virtual void OnWindowResized() OVERRIDE {
103 SetChildBoundsDirect(keyboard_,
104 KeyboardBoundsFromWindowBounds(container_->bounds()));
106 virtual void OnWindowAddedToLayout(aura::Window* child) OVERRIDE {
110 virtual void OnWillRemoveWindowFromLayout(aura::Window* child) OVERRIDE {}
111 virtual void OnWindowRemovedFromLayout(aura::Window* child) OVERRIDE {}
112 virtual void OnChildWindowVisibilityChanged(aura::Window* child,
113 bool visible) OVERRIDE {}
114 virtual void SetChildBounds(aura::Window* child,
115 const gfx::Rect& requested_bounds) OVERRIDE {
116 // Drop these: the size should only be set in OnWindowResized.
120 aura::Window* container_;
121 aura::Window* keyboard_;
123 DISALLOW_COPY_AND_ASSIGN(KeyboardLayoutManager);
126 KeyboardController::KeyboardController(KeyboardControllerProxy* proxy)
129 keyboard_visible_(false),
130 weak_factory_(this) {
132 input_method_ = proxy_->GetInputMethod();
133 input_method_->AddObserver(this);
136 KeyboardController::~KeyboardController() {
137 if (container_.get())
138 container_->RemoveObserver(this);
140 input_method_->RemoveObserver(this);
143 aura::Window* KeyboardController::GetContainerWindow() {
144 if (!container_.get()) {
145 container_.reset(new aura::Window(new KeyboardWindowDelegate()));
146 container_->SetName("KeyboardContainer");
147 container_->set_owned_by_parent(false);
148 container_->Init(ui::LAYER_NOT_DRAWN);
149 container_->AddObserver(this);
150 container_->SetLayoutManager(new KeyboardLayoutManager(container_.get()));
152 return container_.get();
155 void KeyboardController::HideKeyboard(HideReason reason) {
156 keyboard_visible_ = false;
158 keyboard::LogKeyboardControlEvent(
159 reason == HIDE_REASON_AUTOMATIC ?
160 keyboard::KEYBOARD_CONTROL_HIDE_AUTO :
161 keyboard::KEYBOARD_CONTROL_HIDE_USER);
163 FOR_EACH_OBSERVER(KeyboardControllerObserver,
165 OnKeyboardBoundsChanging(gfx::Rect()));
167 proxy_->HideKeyboardContainer(container_.get());
170 void KeyboardController::AddObserver(KeyboardControllerObserver* observer) {
171 observer_list_.AddObserver(observer);
174 void KeyboardController::RemoveObserver(KeyboardControllerObserver* observer) {
175 observer_list_.RemoveObserver(observer);
178 void KeyboardController::OnWindowHierarchyChanged(
179 const HierarchyChangeParams& params) {
180 if (params.new_parent && params.target == container_.get())
181 OnTextInputStateChanged(proxy_->GetInputMethod()->GetTextInputClient());
184 void KeyboardController::OnTextInputStateChanged(
185 const ui::TextInputClient* client) {
186 if (!container_.get())
189 bool was_showing = keyboard_visible_;
190 bool should_show = was_showing;
191 ui::TextInputType type =
192 client ? client->GetTextInputType() : ui::TEXT_INPUT_TYPE_NONE;
193 if (type == ui::TEXT_INPUT_TYPE_NONE &&
194 !CommandLine::ForCurrentProcess()->HasSwitch(
195 switches::kKeyboardUsabilityTest)) {
198 if (container_->children().empty()) {
199 keyboard::MarkKeyboardLoadStarted();
200 aura::Window* keyboard = proxy_->GetKeyboardWindow();
202 container_->AddChild(keyboard);
203 container_->layout_manager()->OnWindowResized();
205 proxy_->SetUpdateInputType(type);
206 container_->parent()->StackChildAtTop(container_.get());
210 if (was_showing != should_show) {
212 keyboard_visible_ = true;
214 // If the controller is in the process of hiding the keyboard, do not log
215 // the stat here since the keyboard will not actually be shown.
216 if (!WillHideKeyboard())
217 keyboard::LogKeyboardControlEvent(keyboard::KEYBOARD_CONTROL_SHOW);
219 weak_factory_.InvalidateWeakPtrs();
220 if (container_->IsVisible())
224 KeyboardControllerObserver,
226 OnKeyboardBoundsChanging(container_->children()[0]->bounds()));
227 proxy_->ShowKeyboardContainer(container_.get());
229 // Set the visibility state here so that any queries for visibility
230 // before the timer fires returns the correct future value.
231 keyboard_visible_ = false;
232 base::MessageLoop::current()->PostDelayedTask(
234 base::Bind(&KeyboardController::HideKeyboard,
235 weak_factory_.GetWeakPtr(), HIDE_REASON_AUTOMATIC),
236 base::TimeDelta::FromMilliseconds(kHideKeyboardDelayMs));
239 // TODO(bryeung): whenever the TextInputClient changes we need to notify the
240 // keyboard (with the TextInputType) so that it can reset it's state (e.g.
241 // abandon compositions in progress)
244 void KeyboardController::OnInputMethodDestroyed(
245 const ui::InputMethod* input_method) {
246 DCHECK_EQ(input_method_, input_method);
247 input_method_ = NULL;
250 bool KeyboardController::WillHideKeyboard() const {
251 return weak_factory_.HasWeakPtrs();
254 } // namespace keyboard