| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/views/focus/focus_manager.h" | 5 #include "ui/views/focus/focus_manager.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/auto_reset.h" | 10 #include "base/auto_reset.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
| 13 #include "ui/base/accelerators/accelerator.h" | 13 #include "ui/base/accelerators/accelerator.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_focus_manager.h" |
| 17 #include "ui/base/ui_base_switches_util.h" |
| 14 #include "ui/events/event.h" | 18 #include "ui/events/event.h" |
| 15 #include "ui/events/keycodes/keyboard_codes.h" | 19 #include "ui/events/keycodes/keyboard_codes.h" |
| 16 #include "ui/views/focus/focus_manager_delegate.h" | 20 #include "ui/views/focus/focus_manager_delegate.h" |
| 17 #include "ui/views/focus/focus_search.h" | 21 #include "ui/views/focus/focus_search.h" |
| 18 #include "ui/views/focus/view_storage.h" | 22 #include "ui/views/focus/view_storage.h" |
| 19 #include "ui/views/focus/widget_focus_manager.h" | 23 #include "ui/views/focus/widget_focus_manager.h" |
| 20 #include "ui/views/view.h" | 24 #include "ui/views/view.h" |
| 21 #include "ui/views/widget/root_view.h" | 25 #include "ui/views/widget/root_view.h" |
| 22 #include "ui/views/widget/widget.h" | 26 #include "ui/views/widget/widget.h" |
| 23 #include "ui/views/widget/widget_delegate.h" | 27 #include "ui/views/widget/widget_delegate.h" |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 // If the key combination matches an accelerator, the accelerator is | 114 // If the key combination matches an accelerator, the accelerator is |
| 111 // triggered, otherwise the key event is processed as usual. | 115 // triggered, otherwise the key event is processed as usual. |
| 112 if (ProcessAccelerator(accelerator)) { | 116 if (ProcessAccelerator(accelerator)) { |
| 113 // If a shortcut was activated for this keydown message, do not propagate | 117 // If a shortcut was activated for this keydown message, do not propagate |
| 114 // the event further. | 118 // the event further. |
| 115 return false; | 119 return false; |
| 116 } | 120 } |
| 117 return true; | 121 return true; |
| 118 } | 122 } |
| 119 | 123 |
| 124 void FocusManager::OnTextInputClientChanged(View* view) { |
| 125 if (view == focused_view_) |
| 126 FocusTextInputClient(view); |
| 127 } |
| 128 |
| 120 void FocusManager::ValidateFocusedView() { | 129 void FocusManager::ValidateFocusedView() { |
| 121 if (focused_view_ && !ContainsView(focused_view_)) | 130 if (focused_view_ && !ContainsView(focused_view_)) |
| 122 ClearFocus(); | 131 ClearFocus(); |
| 123 } | 132 } |
| 124 | 133 |
| 125 // Tests whether a view is valid, whether it still belongs to the window | 134 // Tests whether a view is valid, whether it still belongs to the window |
| 126 // hierarchy of the FocusManager. | 135 // hierarchy of the FocusManager. |
| 127 bool FocusManager::ContainsView(View* view) { | 136 bool FocusManager::ContainsView(View* view) { |
| 128 Widget* widget = view->GetWidget(); | 137 Widget* widget = view->GetWidget(); |
| 129 return widget ? widget->GetFocusManager() == this : false; | 138 return widget ? widget->GetFocusManager() == this : false; |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 314 | 323 |
| 315 base::AutoReset<bool> auto_changing_focus(&is_changing_focus_, true); | 324 base::AutoReset<bool> auto_changing_focus(&is_changing_focus_, true); |
| 316 // Update the reason for the focus change (since this is checked by | 325 // Update the reason for the focus change (since this is checked by |
| 317 // some listeners), then notify all listeners. | 326 // some listeners), then notify all listeners. |
| 318 focus_change_reason_ = reason; | 327 focus_change_reason_ = reason; |
| 319 FOR_EACH_OBSERVER(FocusChangeListener, focus_change_listeners_, | 328 FOR_EACH_OBSERVER(FocusChangeListener, focus_change_listeners_, |
| 320 OnWillChangeFocus(focused_view_, view)); | 329 OnWillChangeFocus(focused_view_, view)); |
| 321 | 330 |
| 322 View* old_focused_view = focused_view_; | 331 View* old_focused_view = focused_view_; |
| 323 focused_view_ = view; | 332 focused_view_ = view; |
| 324 if (old_focused_view) | 333 if (old_focused_view) { |
| 325 old_focused_view->Blur(); | 334 old_focused_view->Blur(); |
| 335 BlurTextInputClient(old_focused_view); |
| 336 } |
| 326 // Also make |focused_view_| the stored focus view. This way the stored focus | 337 // Also make |focused_view_| the stored focus view. This way the stored focus |
| 327 // view is remembered if focus changes are requested prior to a show or while | 338 // view is remembered if focus changes are requested prior to a show or while |
| 328 // hidden. | 339 // hidden. |
| 329 SetStoredFocusView(focused_view_); | 340 SetStoredFocusView(focused_view_); |
| 330 if (focused_view_) | 341 if (focused_view_) { |
| 342 FocusTextInputClient(focused_view_); |
| 331 focused_view_->Focus(); | 343 focused_view_->Focus(); |
| 344 } |
| 332 | 345 |
| 333 FOR_EACH_OBSERVER(FocusChangeListener, focus_change_listeners_, | 346 FOR_EACH_OBSERVER(FocusChangeListener, focus_change_listeners_, |
| 334 OnDidChangeFocus(old_focused_view, focused_view_)); | 347 OnDidChangeFocus(old_focused_view, focused_view_)); |
| 335 } | 348 } |
| 336 | 349 |
| 337 void FocusManager::ClearFocus() { | 350 void FocusManager::ClearFocus() { |
| 338 // SetFocusedView(NULL) is going to clear out the stored view to. We need to | 351 // SetFocusedView(NULL) is going to clear out the stored view to. We need to |
| 339 // persist it in this case. | 352 // persist it in this case. |
| 340 views::View* focused_view = GetStoredFocusView(); | 353 views::View* focused_view = GetStoredFocusView(); |
| 341 SetFocusedView(NULL); | 354 SetFocusedView(NULL); |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 420 return NULL; | 433 return NULL; |
| 421 } | 434 } |
| 422 | 435 |
| 423 return view_storage->RetrieveView(stored_focused_view_storage_id_); | 436 return view_storage->RetrieveView(stored_focused_view_storage_id_); |
| 424 } | 437 } |
| 425 | 438 |
| 426 void FocusManager::ClearStoredFocusedView() { | 439 void FocusManager::ClearStoredFocusedView() { |
| 427 SetStoredFocusView(NULL); | 440 SetStoredFocusView(NULL); |
| 428 } | 441 } |
| 429 | 442 |
| 443 void FocusManager::FocusTextInputClient(View* view) { |
| 444 if (!switches::IsTextInputFocusManagerEnabled()) |
| 445 return; |
| 446 |
| 447 // If the widget is not active, do not steal the text input focus. |
| 448 if (!widget_->IsActive()) |
| 449 return; |
| 450 |
| 451 ui::TextInputClient* text_input_client = |
| 452 view ? view->GetTextInputClient() : NULL; |
| 453 ui::TextInputFocusManager::GetInstance()-> |
| 454 FocusTextInputClient(text_input_client); |
| 455 ui::InputMethod* input_method = widget_->GetHostInputMethod(); |
| 456 if (input_method) { |
| 457 input_method->OnTextInputTypeChanged(text_input_client); |
| 458 input_method->OnCaretBoundsChanged(text_input_client); |
| 459 } |
| 460 } |
| 461 |
| 462 void FocusManager::BlurTextInputClient(View* view) { |
| 463 if (!switches::IsTextInputFocusManagerEnabled()) |
| 464 return; |
| 465 |
| 466 ui::TextInputClient* text_input_client = |
| 467 view ? view->GetTextInputClient() : NULL; |
| 468 if (text_input_client && text_input_client->HasCompositionText()) { |
| 469 text_input_client->ConfirmCompositionText(); |
| 470 ui::InputMethod* input_method = widget_->GetHostInputMethod(); |
| 471 if (input_method && input_method->GetTextInputClient() == text_input_client) |
| 472 input_method->CancelComposition(text_input_client); |
| 473 } |
| 474 ui::TextInputFocusManager::GetInstance()-> |
| 475 BlurTextInputClient(text_input_client); |
| 476 } |
| 477 |
| 430 // Find the next (previous if reverse is true) focusable view for the specified | 478 // Find the next (previous if reverse is true) focusable view for the specified |
| 431 // FocusTraversable, starting at the specified view, traversing down the | 479 // FocusTraversable, starting at the specified view, traversing down the |
| 432 // FocusTraversable hierarchy. | 480 // FocusTraversable hierarchy. |
| 433 View* FocusManager::FindFocusableView(FocusTraversable* focus_traversable, | 481 View* FocusManager::FindFocusableView(FocusTraversable* focus_traversable, |
| 434 View* starting_view, | 482 View* starting_view, |
| 435 bool reverse) { | 483 bool reverse) { |
| 436 FocusTraversable* new_focus_traversable = NULL; | 484 FocusTraversable* new_focus_traversable = NULL; |
| 437 View* new_starting_view = NULL; | 485 View* new_starting_view = NULL; |
| 438 View* v = focus_traversable->GetFocusSearch()->FindNextFocusableView( | 486 View* v = focus_traversable->GetFocusSearch()->FindNextFocusableView( |
| 439 starting_view, | 487 starting_view, |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 531 } | 579 } |
| 532 if (key_code == ui::VKEY_RIGHT || key_code == ui::VKEY_DOWN) { | 580 if (key_code == ui::VKEY_RIGHT || key_code == ui::VKEY_DOWN) { |
| 533 AdvanceFocus(false); | 581 AdvanceFocus(false); |
| 534 return true; | 582 return true; |
| 535 } | 583 } |
| 536 | 584 |
| 537 return false; | 585 return false; |
| 538 } | 586 } |
| 539 | 587 |
| 540 } // namespace views | 588 } // namespace views |
| OLD | NEW |