- add sources.
[platform/framework/web/crosswalk.git] / src / ui / keyboard / keyboard_controller.cc
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.
4
5 #include "ui/keyboard/keyboard_controller.h"
6
7 #include "base/bind.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"
24
25 namespace {
26
27 const int kHideKeyboardDelayMs = 100;
28
29 gfx::Rect KeyboardBoundsFromWindowBounds(const gfx::Rect& window_bounds) {
30   const float kKeyboardHeightRatio = 0.3f;
31   return gfx::Rect(
32       window_bounds.x(),
33       window_bounds.y() + window_bounds.height() * (1 - kKeyboardHeightRatio),
34       window_bounds.width(),
35       window_bounds.height() * kKeyboardHeightRatio);
36 }
37
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
40 // window.
41 // The delegate deletes itself when the window is destroyed.
42 class KeyboardWindowDelegate : public aura::WindowDelegate {
43  public:
44   KeyboardWindowDelegate() {}
45   virtual ~KeyboardWindowDelegate() {}
46
47  private:
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 {
53     bounds_ = new_bounds;
54   }
55   virtual gfx::NativeCursor GetCursor(const gfx::Point& point) OVERRIDE {
56     return gfx::kNullCursor;
57   }
58   virtual int GetNonClientComponent(const gfx::Point& point) const OVERRIDE {
59     return HTNOWHERE;
60   }
61   virtual bool ShouldDescendIntoChildForEventHandling(
62       aura::Window* child,
63       const gfx::Point& location) OVERRIDE {
64     return true;
65   }
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));
77   }
78   virtual void DidRecreateLayer(ui::Layer* old_layer,
79                                 ui::Layer* new_layer) OVERRIDE {}
80
81   gfx::Rect bounds_;
82   DISALLOW_COPY_AND_ASSIGN(KeyboardWindowDelegate);
83 };
84
85 }  // namespace
86
87 namespace keyboard {
88
89 // LayoutManager for the virtual keyboard container.  Manages a single window
90 // (the virtual keyboard) and keeps it positioned at the bottom of the
91 // owner window.
92 class KeyboardLayoutManager : public aura::LayoutManager {
93  public:
94   KeyboardLayoutManager(aura::Window* container)
95       : container_(container), keyboard_(NULL) {
96     CHECK(container_);
97   }
98
99   // Overridden from aura::LayoutManager
100   virtual void OnWindowResized() OVERRIDE {
101     if (!keyboard_)
102       return;
103     SetChildBoundsDirect(keyboard_,
104                          KeyboardBoundsFromWindowBounds(container_->bounds()));
105   }
106   virtual void OnWindowAddedToLayout(aura::Window* child) OVERRIDE {
107     DCHECK(!keyboard_);
108     keyboard_ = child;
109   }
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.
117   }
118
119  private:
120   aura::Window* container_;
121   aura::Window* keyboard_;
122
123   DISALLOW_COPY_AND_ASSIGN(KeyboardLayoutManager);
124 };
125
126 KeyboardController::KeyboardController(KeyboardControllerProxy* proxy)
127     : proxy_(proxy),
128       input_method_(NULL),
129       keyboard_visible_(false),
130       weak_factory_(this) {
131   CHECK(proxy);
132   input_method_ = proxy_->GetInputMethod();
133   input_method_->AddObserver(this);
134 }
135
136 KeyboardController::~KeyboardController() {
137   if (container_.get())
138     container_->RemoveObserver(this);
139   if (input_method_)
140     input_method_->RemoveObserver(this);
141 }
142
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()));
151   }
152   return container_.get();
153 }
154
155 void KeyboardController::HideKeyboard(HideReason reason) {
156   keyboard_visible_ = false;
157
158   keyboard::LogKeyboardControlEvent(
159       reason == HIDE_REASON_AUTOMATIC ?
160           keyboard::KEYBOARD_CONTROL_HIDE_AUTO :
161           keyboard::KEYBOARD_CONTROL_HIDE_USER);
162
163   FOR_EACH_OBSERVER(KeyboardControllerObserver,
164                     observer_list_,
165                     OnKeyboardBoundsChanging(gfx::Rect()));
166
167   proxy_->HideKeyboardContainer(container_.get());
168 }
169
170 void KeyboardController::AddObserver(KeyboardControllerObserver* observer) {
171   observer_list_.AddObserver(observer);
172 }
173
174 void KeyboardController::RemoveObserver(KeyboardControllerObserver* observer) {
175   observer_list_.RemoveObserver(observer);
176 }
177
178 void KeyboardController::OnWindowHierarchyChanged(
179     const HierarchyChangeParams& params) {
180   if (params.new_parent && params.target == container_.get())
181     OnTextInputStateChanged(proxy_->GetInputMethod()->GetTextInputClient());
182 }
183
184 void KeyboardController::OnTextInputStateChanged(
185     const ui::TextInputClient* client) {
186   if (!container_.get())
187     return;
188
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)) {
196     should_show = false;
197   } else {
198     if (container_->children().empty()) {
199       keyboard::MarkKeyboardLoadStarted();
200       aura::Window* keyboard = proxy_->GetKeyboardWindow();
201       keyboard->Show();
202       container_->AddChild(keyboard);
203       container_->layout_manager()->OnWindowResized();
204     }
205     proxy_->SetUpdateInputType(type);
206     container_->parent()->StackChildAtTop(container_.get());
207     should_show = true;
208   }
209
210   if (was_showing != should_show) {
211     if (should_show) {
212       keyboard_visible_ = true;
213
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);
218
219       weak_factory_.InvalidateWeakPtrs();
220       if (container_->IsVisible())
221         return;
222
223       FOR_EACH_OBSERVER(
224           KeyboardControllerObserver,
225           observer_list_,
226           OnKeyboardBoundsChanging(container_->children()[0]->bounds()));
227       proxy_->ShowKeyboardContainer(container_.get());
228     } else {
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(
233           FROM_HERE,
234           base::Bind(&KeyboardController::HideKeyboard,
235                      weak_factory_.GetWeakPtr(), HIDE_REASON_AUTOMATIC),
236           base::TimeDelta::FromMilliseconds(kHideKeyboardDelayMs));
237     }
238   }
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)
242 }
243
244 void KeyboardController::OnInputMethodDestroyed(
245     const ui::InputMethod* input_method) {
246   DCHECK_EQ(input_method_, input_method);
247   input_method_ = NULL;
248 }
249
250 bool KeyboardController::WillHideKeyboard() const {
251   return weak_factory_.HasWeakPtrs();
252 }
253
254 }  // namespace keyboard