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 "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "content/public/browser/render_widget_host.h" | 9 #include "content/public/browser/render_widget_host.h" |
10 #include "content/public/browser/render_widget_host_iterator.h" | 10 #include "content/public/browser/render_widget_host_iterator.h" |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 void CallbackAnimationObserver::OnLayerAnimationAborted( | 199 void CallbackAnimationObserver::OnLayerAnimationAborted( |
200 ui::LayerAnimationSequence* seq) { | 200 ui::LayerAnimationSequence* seq) { |
201 animator_->RemoveObserver(this); | 201 animator_->RemoveObserver(this); |
202 } | 202 } |
203 | 203 |
204 class WindowBoundsChangeObserver : public aura::WindowObserver { | 204 class WindowBoundsChangeObserver : public aura::WindowObserver { |
205 public: | 205 public: |
206 virtual void OnWindowBoundsChanged(aura::Window* window, | 206 virtual void OnWindowBoundsChanged(aura::Window* window, |
207 const gfx::Rect& old_bounds, | 207 const gfx::Rect& old_bounds, |
208 const gfx::Rect& new_bounds) OVERRIDE; | 208 const gfx::Rect& new_bounds) OVERRIDE; |
209 virtual void OnWindowDestroyed(aura::Window* window) OVERRIDE; | |
210 }; | 209 }; |
211 | 210 |
212 void WindowBoundsChangeObserver::OnWindowBoundsChanged(aura::Window* window, | 211 void WindowBoundsChangeObserver::OnWindowBoundsChanged(aura::Window* window, |
213 const gfx::Rect& old_bounds, const gfx::Rect& new_bounds) { | 212 const gfx::Rect& old_bounds, const gfx::Rect& new_bounds) { |
214 KeyboardController* controller = KeyboardController::GetInstance(); | 213 KeyboardController* controller = KeyboardController::GetInstance(); |
215 if (controller) | 214 if (controller) |
216 controller->UpdateWindowInsets(window); | 215 controller->UpdateWindowInsets(window); |
217 } | 216 } |
218 | 217 |
219 void WindowBoundsChangeObserver::OnWindowDestroyed(aura::Window* window) { | |
220 if (window->HasObserver(this)) | |
221 window->RemoveObserver(this); | |
222 } | |
223 | |
224 // static | 218 // static |
225 KeyboardController* KeyboardController::instance_ = NULL; | 219 KeyboardController* KeyboardController::instance_ = NULL; |
226 | 220 |
227 KeyboardController::KeyboardController(KeyboardControllerProxy* proxy) | 221 KeyboardController::KeyboardController(KeyboardControllerProxy* proxy) |
228 : proxy_(proxy), | 222 : proxy_(proxy), |
229 input_method_(NULL), | 223 input_method_(NULL), |
230 keyboard_visible_(false), | 224 keyboard_visible_(false), |
231 lock_keyboard_(false), | 225 lock_keyboard_(false), |
232 type_(ui::TEXT_INPUT_TYPE_NONE), | 226 type_(ui::TEXT_INPUT_TYPE_NONE), |
233 weak_factory_(this) { | 227 weak_factory_(this) { |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
287 scoped_ptr<content::RenderWidgetHostIterator> widgets( | 281 scoped_ptr<content::RenderWidgetHostIterator> widgets( |
288 content::RenderWidgetHost::GetRenderWidgetHosts()); | 282 content::RenderWidgetHost::GetRenderWidgetHosts()); |
289 aura::Window *keyboard_window = proxy_->GetKeyboardWindow(); | 283 aura::Window *keyboard_window = proxy_->GetKeyboardWindow(); |
290 aura::Window *root_window = keyboard_window->GetRootWindow(); | 284 aura::Window *root_window = keyboard_window->GetRootWindow(); |
291 while (content::RenderWidgetHost* widget = widgets->GetNextHost()) { | 285 while (content::RenderWidgetHost* widget = widgets->GetNextHost()) { |
292 content::RenderWidgetHostView* view = widget->GetView(); | 286 content::RenderWidgetHostView* view = widget->GetView(); |
293 // Can be NULL, e.g. if the RenderWidget is being destroyed or | 287 // Can be NULL, e.g. if the RenderWidget is being destroyed or |
294 // the render process crashed. | 288 // the render process crashed. |
295 if (view) { | 289 if (view) { |
296 aura::Window *window = view->GetNativeView(); | 290 aura::Window *window = view->GetNativeView(); |
297 // If virtual keyboard failed to load, a widget that displays error | 291 if (window != keyboard_window && |
298 // message will be created and adds as a child of the virtual keyboard | |
299 // window. We want to avoid add BoundsChangedObserver to that window. | |
300 if (GetFrameWindow(window) != keyboard_window && | |
301 window->GetRootWindow() == root_window) { | 292 window->GetRootWindow() == root_window) { |
302 gfx::Rect window_bounds = window->GetBoundsInScreen(); | 293 gfx::Rect window_bounds = window->GetBoundsInScreen(); |
303 gfx::Rect intersect = gfx::IntersectRects(window_bounds, | 294 gfx::Rect intersect = gfx::IntersectRects(window_bounds, |
304 new_bounds); | 295 new_bounds); |
305 int overlap = intersect.height(); | 296 int overlap = intersect.height(); |
306 if (overlap > 0 && overlap < window_bounds.height()) | 297 if (overlap > 0 && overlap < window_bounds.height()) |
307 view->SetInsets(gfx::Insets(0, 0, overlap, 0)); | 298 view->SetInsets(gfx::Insets(0, 0, overlap, 0)); |
308 else | 299 else |
309 view->SetInsets(gfx::Insets()); | 300 view->SetInsets(gfx::Insets()); |
310 AddBoundsChangedObserver(window); | 301 AddBoundsChangedObserver(window); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
362 ShowKeyboardInternal(); | 353 ShowKeyboardInternal(); |
363 } | 354 } |
364 | 355 |
365 void KeyboardController::OnWindowHierarchyChanged( | 356 void KeyboardController::OnWindowHierarchyChanged( |
366 const HierarchyChangeParams& params) { | 357 const HierarchyChangeParams& params) { |
367 if (params.new_parent && params.target == container_.get()) | 358 if (params.new_parent && params.target == container_.get()) |
368 OnTextInputStateChanged(proxy_->GetInputMethod()->GetTextInputClient()); | 359 OnTextInputStateChanged(proxy_->GetInputMethod()->GetTextInputClient()); |
369 } | 360 } |
370 | 361 |
371 void KeyboardController::Reload() { | 362 void KeyboardController::Reload() { |
372 if (proxy_->HasKeyboardWindow()) { | 363 if (proxy_->HasKeyboardWindow()) |
373 // A reload should never try to show virtual keyboard. If keyboard is not | |
374 // visible before reload, it should keep invisible after reload. | |
375 show_on_resize_ = false; | |
376 proxy_->ReloadKeyboardIfNeeded(); | 364 proxy_->ReloadKeyboardIfNeeded(); |
377 } | |
378 } | 365 } |
379 | 366 |
380 void KeyboardController::OnTextInputStateChanged( | 367 void KeyboardController::OnTextInputStateChanged( |
381 const ui::TextInputClient* client) { | 368 const ui::TextInputClient* client) { |
382 if (!container_.get()) | 369 if (!container_.get()) |
383 return; | 370 return; |
384 | 371 |
385 if (IsKeyboardUsabilityExperimentEnabled()) { | 372 if (IsKeyboardUsabilityExperimentEnabled()) { |
386 ShowKeyboardInternal(); | 373 ShowKeyboardInternal(); |
387 return; | 374 return; |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
459 if (container_->children().empty()) { | 446 if (container_->children().empty()) { |
460 keyboard::MarkKeyboardLoadStarted(); | 447 keyboard::MarkKeyboardLoadStarted(); |
461 aura::Window* keyboard = proxy_->GetKeyboardWindow(); | 448 aura::Window* keyboard = proxy_->GetKeyboardWindow(); |
462 keyboard->Show(); | 449 keyboard->Show(); |
463 container_->AddChild(keyboard); | 450 container_->AddChild(keyboard); |
464 keyboard->set_owned_by_parent(false); | 451 keyboard->set_owned_by_parent(false); |
465 } | 452 } |
466 | 453 |
467 proxy_->ReloadKeyboardIfNeeded(); | 454 proxy_->ReloadKeyboardIfNeeded(); |
468 | 455 |
469 if (keyboard_visible_) { | 456 if (keyboard_visible_ || proxy_->GetKeyboardWindow()->bounds().height() == 0) |
470 return; | 457 return; |
471 } else if (proxy_->GetKeyboardWindow()->bounds().height() == 0) { | |
472 show_on_resize_ = true; | |
473 return; | |
474 } | |
475 | 458 |
476 keyboard_visible_ = true; | 459 keyboard_visible_ = true; |
477 | 460 |
478 // If the controller is in the process of hiding the keyboard, do not log | 461 // If the controller is in the process of hiding the keyboard, do not log |
479 // the stat here since the keyboard will not actually be shown. | 462 // the stat here since the keyboard will not actually be shown. |
480 if (!WillHideKeyboard()) | 463 if (!WillHideKeyboard()) |
481 keyboard::LogKeyboardControlEvent(keyboard::KEYBOARD_CONTROL_SHOW); | 464 keyboard::LogKeyboardControlEvent(keyboard::KEYBOARD_CONTROL_SHOW); |
482 | 465 |
483 weak_factory_.InvalidateWeakPtrs(); | 466 weak_factory_.InvalidateWeakPtrs(); |
484 | 467 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
565 | 548 |
566 void KeyboardController::RemoveBoundsChangedObserver(aura::Window* window) { | 549 void KeyboardController::RemoveBoundsChangedObserver(aura::Window* window) { |
567 aura::Window* target_window = GetFrameWindow(window); | 550 aura::Window* target_window = GetFrameWindow(window); |
568 if (target_window && | 551 if (target_window && |
569 target_window->HasObserver(window_bounds_observer_.get())) { | 552 target_window->HasObserver(window_bounds_observer_.get())) { |
570 target_window->RemoveObserver(window_bounds_observer_.get()); | 553 target_window->RemoveObserver(window_bounds_observer_.get()); |
571 } | 554 } |
572 } | 555 } |
573 | 556 |
574 } // namespace keyboard | 557 } // namespace keyboard |
OLD | NEW |