OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/keyboard/keyboard_controller.h" | 5 #include "ui/keyboard/keyboard_controller.h" |
6 | 6 |
7 #include "ui/aura/layout_manager.h" | 7 #include "ui/aura/layout_manager.h" |
8 #include "ui/aura/window.h" | 8 #include "ui/aura/window.h" |
9 #include "ui/aura/window_delegate.h" | 9 #include "ui/aura/window_delegate.h" |
10 #include "ui/base/hit_test.h" | 10 #include "ui/base/hit_test.h" |
11 #include "ui/base/ime/input_method.h" | 11 #include "ui/base/ime/input_method.h" |
12 #include "ui/base/ime/text_input_client.h" | 12 #include "ui/base/ime/text_input_client.h" |
13 #include "ui/base/ime/text_input_type.h" | 13 #include "ui/base/ime/text_input_type.h" |
14 #include "ui/gfx/path.h" | 14 #include "ui/gfx/path.h" |
15 #include "ui/gfx/rect.h" | 15 #include "ui/gfx/rect.h" |
16 #include "ui/gfx/skia_util.h" | 16 #include "ui/gfx/skia_util.h" |
| 17 #include "ui/keyboard/keyboard_controller_observer.h" |
17 #include "ui/keyboard/keyboard_controller_proxy.h" | 18 #include "ui/keyboard/keyboard_controller_proxy.h" |
18 | 19 |
19 namespace { | 20 namespace { |
20 | 21 |
21 gfx::Rect KeyboardBoundsFromWindowBounds(const gfx::Rect& window_bounds) { | 22 gfx::Rect KeyboardBoundsFromWindowBounds(const gfx::Rect& window_bounds) { |
22 const float kKeyboardHeightRatio = 0.3f; | 23 const float kKeyboardHeightRatio = 0.3f; |
23 return gfx::Rect( | 24 return gfx::Rect( |
24 window_bounds.x(), | 25 window_bounds.x(), |
25 window_bounds.y() + window_bounds.height() * (1 - kKeyboardHeightRatio), | 26 window_bounds.y() + window_bounds.height() * (1 - kKeyboardHeightRatio), |
26 window_bounds.width(), | 27 window_bounds.width(), |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 if (!container_) { | 135 if (!container_) { |
135 container_ = new aura::Window(new KeyboardWindowDelegate()); | 136 container_ = new aura::Window(new KeyboardWindowDelegate()); |
136 container_->SetName("KeyboardContainer"); | 137 container_->SetName("KeyboardContainer"); |
137 container_->Init(ui::LAYER_NOT_DRAWN); | 138 container_->Init(ui::LAYER_NOT_DRAWN); |
138 container_->AddObserver(this); | 139 container_->AddObserver(this); |
139 container_->SetLayoutManager(new KeyboardLayoutManager(container_)); | 140 container_->SetLayoutManager(new KeyboardLayoutManager(container_)); |
140 } | 141 } |
141 return container_; | 142 return container_; |
142 } | 143 } |
143 | 144 |
| 145 void KeyboardController::AddObserver(KeyboardControllerObserver* observer) { |
| 146 observer_list_.AddObserver(observer); |
| 147 } |
| 148 |
| 149 void KeyboardController::RemoveObserver(KeyboardControllerObserver* observer) { |
| 150 observer_list_.RemoveObserver(observer); |
| 151 } |
| 152 |
144 void KeyboardController::OnWindowParentChanged(aura::Window* window, | 153 void KeyboardController::OnWindowParentChanged(aura::Window* window, |
145 aura::Window* parent) { | 154 aura::Window* parent) { |
146 OnTextInputStateChanged(proxy_->GetInputMethod()->GetTextInputClient()); | 155 OnTextInputStateChanged(proxy_->GetInputMethod()->GetTextInputClient()); |
147 } | 156 } |
148 | 157 |
149 void KeyboardController::OnWindowDestroying(aura::Window* window) { | 158 void KeyboardController::OnWindowDestroying(aura::Window* window) { |
150 DCHECK_EQ(container_, window); | 159 DCHECK_EQ(container_, window); |
151 container_ = NULL; | 160 container_ = NULL; |
152 } | 161 } |
153 | 162 |
154 void KeyboardController::OnTextInputStateChanged( | 163 void KeyboardController::OnTextInputStateChanged( |
155 const ui::TextInputClient* client) { | 164 const ui::TextInputClient* client) { |
156 if (!container_) | 165 if (!container_) |
157 return; | 166 return; |
158 | 167 |
| 168 bool was_showing = container_->IsVisible(); |
| 169 bool should_show = was_showing; |
159 if (!client || client->GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE) { | 170 if (!client || client->GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE) { |
160 container_->Hide(); | 171 should_show = false; |
161 } else { | 172 } else { |
162 if (container_->children().empty()) { | 173 if (container_->children().empty()) { |
163 aura::Window* keyboard = proxy_->GetKeyboardWindow(); | 174 aura::Window* keyboard = proxy_->GetKeyboardWindow(); |
164 keyboard->Show(); | 175 keyboard->Show(); |
165 container_->AddChild(keyboard); | 176 container_->AddChild(keyboard); |
166 container_->layout_manager()->OnWindowResized(); | 177 container_->layout_manager()->OnWindowResized(); |
167 } | 178 } |
168 container_->parent()->StackChildAtTop(container_); | 179 container_->parent()->StackChildAtTop(container_); |
169 container_->Show(); | 180 should_show = true; |
170 } | 181 } |
| 182 |
| 183 if (was_showing != should_show) { |
| 184 gfx::Rect new_bounds( |
| 185 should_show ? container_->children()[0]->bounds() : gfx::Rect()); |
| 186 |
| 187 FOR_EACH_OBSERVER( |
| 188 KeyboardControllerObserver, |
| 189 observer_list_, |
| 190 OnKeyboardBoundsChanging(new_bounds)); |
| 191 |
| 192 if (should_show) |
| 193 container_->Show(); |
| 194 else |
| 195 container_->Hide(); |
| 196 } |
| 197 |
171 // TODO(bryeung): whenever the TextInputClient changes we need to notify the | 198 // TODO(bryeung): whenever the TextInputClient changes we need to notify the |
172 // keyboard (with the TextInputType) so that it can reset it's state (e.g. | 199 // keyboard (with the TextInputType) so that it can reset it's state (e.g. |
173 // abandon compositions in progress) | 200 // abandon compositions in progress) |
174 } | 201 } |
175 | 202 |
176 void KeyboardController::OnInputMethodDestroyed( | 203 void KeyboardController::OnInputMethodDestroyed( |
177 const ui::InputMethod* input_method) { | 204 const ui::InputMethod* input_method) { |
178 DCHECK_EQ(input_method_, input_method); | 205 DCHECK_EQ(input_method_, input_method); |
179 input_method_ = NULL; | 206 input_method_ = NULL; |
180 } | 207 } |
181 | 208 |
182 } // namespace keyboard | 209 } // namespace keyboard |
OLD | NEW |