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 "base/bind.h" | 7 #include "base/bind.h" |
8 #include "ui/aura/layout_manager.h" | 8 #include "ui/aura/layout_manager.h" |
9 #include "ui/aura/window.h" | 9 #include "ui/aura/window.h" |
10 #include "ui/aura/window_delegate.h" | 10 #include "ui/aura/window_delegate.h" |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 | 115 |
116 private: | 116 private: |
117 aura::Window* container_; | 117 aura::Window* container_; |
118 aura::Window* keyboard_; | 118 aura::Window* keyboard_; |
119 | 119 |
120 DISALLOW_COPY_AND_ASSIGN(KeyboardLayoutManager); | 120 DISALLOW_COPY_AND_ASSIGN(KeyboardLayoutManager); |
121 }; | 121 }; |
122 | 122 |
123 KeyboardController::KeyboardController(KeyboardControllerProxy* proxy) | 123 KeyboardController::KeyboardController(KeyboardControllerProxy* proxy) |
124 : proxy_(proxy), | 124 : proxy_(proxy), |
125 container_(NULL), | |
126 input_method_(NULL), | 125 input_method_(NULL), |
127 keyboard_visible_(false), | 126 keyboard_visible_(false), |
128 weak_factory_(this) { | 127 weak_factory_(this) { |
129 CHECK(proxy); | 128 CHECK(proxy); |
130 input_method_ = proxy_->GetInputMethod(); | 129 input_method_ = proxy_->GetInputMethod(); |
131 input_method_->AddObserver(this); | 130 input_method_->AddObserver(this); |
132 } | 131 } |
133 | 132 |
134 KeyboardController::~KeyboardController() { | 133 KeyboardController::~KeyboardController() { |
135 if (container_) | 134 if (container_.get()) |
136 container_->RemoveObserver(this); | 135 container_->RemoveObserver(this); |
137 if (input_method_) | 136 if (input_method_) |
138 input_method_->RemoveObserver(this); | 137 input_method_->RemoveObserver(this); |
139 } | 138 } |
140 | 139 |
141 aura::Window* KeyboardController::GetContainerWindow() { | 140 aura::Window* KeyboardController::GetContainerWindow() { |
142 if (!container_) { | 141 if (!container_.get()) { |
143 container_ = new aura::Window(new KeyboardWindowDelegate()); | 142 container_.reset(new aura::Window(new KeyboardWindowDelegate())); |
144 container_->SetName("KeyboardContainer"); | 143 container_->SetName("KeyboardContainer"); |
| 144 container_->set_owned_by_parent(false); |
145 container_->Init(ui::LAYER_NOT_DRAWN); | 145 container_->Init(ui::LAYER_NOT_DRAWN); |
146 container_->AddObserver(this); | 146 container_->AddObserver(this); |
147 container_->SetLayoutManager(new KeyboardLayoutManager(container_)); | 147 container_->SetLayoutManager(new KeyboardLayoutManager(container_.get())); |
148 } | 148 } |
149 return container_; | 149 return container_.get(); |
150 } | 150 } |
151 | 151 |
152 void KeyboardController::HideKeyboard() { | 152 void KeyboardController::HideKeyboard() { |
153 keyboard_visible_ = false; | 153 keyboard_visible_ = false; |
154 | 154 |
155 FOR_EACH_OBSERVER(KeyboardControllerObserver, | 155 FOR_EACH_OBSERVER(KeyboardControllerObserver, |
156 observer_list_, | 156 observer_list_, |
157 OnKeyboardBoundsChanging(gfx::Rect())); | 157 OnKeyboardBoundsChanging(gfx::Rect())); |
158 | 158 |
159 proxy_->HideKeyboardContainer(container_); | 159 proxy_->HideKeyboardContainer(container_.get()); |
160 } | 160 } |
161 | 161 |
162 void KeyboardController::AddObserver(KeyboardControllerObserver* observer) { | 162 void KeyboardController::AddObserver(KeyboardControllerObserver* observer) { |
163 observer_list_.AddObserver(observer); | 163 observer_list_.AddObserver(observer); |
164 } | 164 } |
165 | 165 |
166 void KeyboardController::RemoveObserver(KeyboardControllerObserver* observer) { | 166 void KeyboardController::RemoveObserver(KeyboardControllerObserver* observer) { |
167 observer_list_.RemoveObserver(observer); | 167 observer_list_.RemoveObserver(observer); |
168 } | 168 } |
169 | 169 |
170 void KeyboardController::OnWindowHierarchyChanged( | 170 void KeyboardController::OnWindowHierarchyChanged( |
171 const HierarchyChangeParams& params) { | 171 const HierarchyChangeParams& params) { |
172 if (params.new_parent && params.target == container_) | 172 if (params.new_parent && params.target == container_.get()) |
173 OnTextInputStateChanged(proxy_->GetInputMethod()->GetTextInputClient()); | 173 OnTextInputStateChanged(proxy_->GetInputMethod()->GetTextInputClient()); |
174 } | 174 } |
175 | 175 |
176 void KeyboardController::OnWindowDestroying(aura::Window* window) { | |
177 DCHECK_EQ(container_, window); | |
178 container_ = NULL; | |
179 } | |
180 | |
181 void KeyboardController::OnTextInputStateChanged( | 176 void KeyboardController::OnTextInputStateChanged( |
182 const ui::TextInputClient* client) { | 177 const ui::TextInputClient* client) { |
183 if (!container_) | 178 if (!container_.get()) |
184 return; | 179 return; |
185 | 180 |
186 bool was_showing = keyboard_visible_; | 181 bool was_showing = keyboard_visible_; |
187 bool should_show = was_showing; | 182 bool should_show = was_showing; |
188 ui::TextInputType type = | 183 ui::TextInputType type = |
189 client ? client->GetTextInputType() : ui::TEXT_INPUT_TYPE_NONE; | 184 client ? client->GetTextInputType() : ui::TEXT_INPUT_TYPE_NONE; |
190 if (type == ui::TEXT_INPUT_TYPE_NONE) { | 185 if (type == ui::TEXT_INPUT_TYPE_NONE) { |
191 should_show = false; | 186 should_show = false; |
192 } else { | 187 } else { |
193 if (container_->children().empty()) { | 188 if (container_->children().empty()) { |
194 aura::Window* keyboard = proxy_->GetKeyboardWindow(); | 189 aura::Window* keyboard = proxy_->GetKeyboardWindow(); |
195 keyboard->Show(); | 190 keyboard->Show(); |
196 container_->AddChild(keyboard); | 191 container_->AddChild(keyboard); |
197 container_->layout_manager()->OnWindowResized(); | 192 container_->layout_manager()->OnWindowResized(); |
198 } | 193 } |
199 proxy_->SetUpdateInputType(type); | 194 proxy_->SetUpdateInputType(type); |
200 container_->parent()->StackChildAtTop(container_); | 195 container_->parent()->StackChildAtTop(container_.get()); |
201 should_show = true; | 196 should_show = true; |
202 } | 197 } |
203 | 198 |
204 if (was_showing != should_show) { | 199 if (was_showing != should_show) { |
205 if (should_show) { | 200 if (should_show) { |
206 keyboard_visible_ = true; | 201 keyboard_visible_ = true; |
207 weak_factory_.InvalidateWeakPtrs(); | 202 weak_factory_.InvalidateWeakPtrs(); |
208 if (container_->IsVisible()) | 203 if (container_->IsVisible()) |
209 return; | 204 return; |
210 | 205 |
211 FOR_EACH_OBSERVER( | 206 FOR_EACH_OBSERVER( |
212 KeyboardControllerObserver, | 207 KeyboardControllerObserver, |
213 observer_list_, | 208 observer_list_, |
214 OnKeyboardBoundsChanging(container_->children()[0]->bounds())); | 209 OnKeyboardBoundsChanging(container_->children()[0]->bounds())); |
215 proxy_->ShowKeyboardContainer(container_); | 210 proxy_->ShowKeyboardContainer(container_.get()); |
216 } else { | 211 } else { |
217 // Set the visibility state here so that any queries for visibility | 212 // Set the visibility state here so that any queries for visibility |
218 // before the timer fires returns the correct future value. | 213 // before the timer fires returns the correct future value. |
219 keyboard_visible_ = false; | 214 keyboard_visible_ = false; |
220 base::MessageLoop::current()->PostDelayedTask( | 215 base::MessageLoop::current()->PostDelayedTask( |
221 FROM_HERE, | 216 FROM_HERE, |
222 base::Bind(&KeyboardController::HideKeyboard, | 217 base::Bind(&KeyboardController::HideKeyboard, |
223 weak_factory_.GetWeakPtr()), | 218 weak_factory_.GetWeakPtr()), |
224 base::TimeDelta::FromMilliseconds(kHideKeyboardDelayMs)); | 219 base::TimeDelta::FromMilliseconds(kHideKeyboardDelayMs)); |
225 } | 220 } |
226 } | 221 } |
227 // TODO(bryeung): whenever the TextInputClient changes we need to notify the | 222 // TODO(bryeung): whenever the TextInputClient changes we need to notify the |
228 // keyboard (with the TextInputType) so that it can reset it's state (e.g. | 223 // keyboard (with the TextInputType) so that it can reset it's state (e.g. |
229 // abandon compositions in progress) | 224 // abandon compositions in progress) |
230 } | 225 } |
231 | 226 |
232 void KeyboardController::OnInputMethodDestroyed( | 227 void KeyboardController::OnInputMethodDestroyed( |
233 const ui::InputMethod* input_method) { | 228 const ui::InputMethod* input_method) { |
234 DCHECK_EQ(input_method_, input_method); | 229 DCHECK_EQ(input_method_, input_method); |
235 input_method_ = NULL; | 230 input_method_ = NULL; |
236 } | 231 } |
237 | 232 |
238 bool KeyboardController::WillHideKeyboard() const { | 233 bool KeyboardController::WillHideKeyboard() const { |
239 return weak_factory_.HasWeakPtrs(); | 234 return weak_factory_.HasWeakPtrs(); |
240 } | 235 } |
241 | 236 |
242 } // namespace keyboard | 237 } // namespace keyboard |
OLD | NEW |