| 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" | 14 #include "ui/base/ime/input_method.h" |
| 15 #include "ui/base/ime/text_input_client.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" | |
| 18 #include "ui/events/event.h" | 16 #include "ui/events/event.h" |
| 19 #include "ui/events/keycodes/keyboard_codes.h" | 17 #include "ui/events/keycodes/keyboard_codes.h" |
| 20 #include "ui/views/focus/focus_manager_delegate.h" | 18 #include "ui/views/focus/focus_manager_delegate.h" |
| 21 #include "ui/views/focus/focus_search.h" | 19 #include "ui/views/focus/focus_search.h" |
| 22 #include "ui/views/focus/view_storage.h" | 20 #include "ui/views/focus/view_storage.h" |
| 23 #include "ui/views/focus/widget_focus_manager.h" | 21 #include "ui/views/focus/widget_focus_manager.h" |
| 24 #include "ui/views/view.h" | 22 #include "ui/views/view.h" |
| 25 #include "ui/views/widget/root_view.h" | 23 #include "ui/views/widget/root_view.h" |
| 26 #include "ui/views/widget/widget.h" | 24 #include "ui/views/widget/widget.h" |
| 27 #include "ui/views/widget/widget_delegate.h" | 25 #include "ui/views/widget/widget_delegate.h" |
| (...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 294 if (widget->widget_delegate()->ShouldAdvanceFocusToTopLevelWidget()) | 292 if (widget->widget_delegate()->ShouldAdvanceFocusToTopLevelWidget()) |
| 295 widget = widget_; | 293 widget = widget_; |
| 296 return GetNextFocusableView(NULL, widget, reverse, true); | 294 return GetNextFocusableView(NULL, widget, reverse, true); |
| 297 } | 295 } |
| 298 } | 296 } |
| 299 return NULL; | 297 return NULL; |
| 300 } | 298 } |
| 301 | 299 |
| 302 void FocusManager::SetFocusedViewWithReason( | 300 void FocusManager::SetFocusedViewWithReason( |
| 303 View* view, FocusChangeReason reason) { | 301 View* view, FocusChangeReason reason) { |
| 304 if (focused_view_ == view) { | 302 if (focused_view_ == view) |
| 305 // In the case that the widget lost the focus and gained it back without | |
| 306 // changing the focused view, we have to make the text input client focused. | |
| 307 // TODO(yukishiino): Remove this hack once we fix http://crbug.com/383236 | |
| 308 FocusTextInputClient(focused_view_); | |
| 309 return; | 303 return; |
| 310 } | |
| 311 | 304 |
| 312 base::AutoReset<bool> auto_changing_focus(&is_changing_focus_, true); | 305 base::AutoReset<bool> auto_changing_focus(&is_changing_focus_, true); |
| 313 // Update the reason for the focus change (since this is checked by | 306 // Update the reason for the focus change (since this is checked by |
| 314 // some listeners), then notify all listeners. | 307 // some listeners), then notify all listeners. |
| 315 focus_change_reason_ = reason; | 308 focus_change_reason_ = reason; |
| 316 FOR_EACH_OBSERVER(FocusChangeListener, focus_change_listeners_, | 309 FOR_EACH_OBSERVER(FocusChangeListener, focus_change_listeners_, |
| 317 OnWillChangeFocus(focused_view_, view)); | 310 OnWillChangeFocus(focused_view_, view)); |
| 318 | 311 |
| 319 View* old_focused_view = focused_view_; | 312 View* old_focused_view = focused_view_; |
| 320 focused_view_ = view; | 313 focused_view_ = view; |
| 321 if (old_focused_view) { | 314 if (old_focused_view) |
| 322 old_focused_view->Blur(); | 315 old_focused_view->Blur(); |
| 323 BlurTextInputClient(old_focused_view); | |
| 324 } | |
| 325 // Also make |focused_view_| the stored focus view. This way the stored focus | 316 // Also make |focused_view_| the stored focus view. This way the stored focus |
| 326 // view is remembered if focus changes are requested prior to a show or while | 317 // view is remembered if focus changes are requested prior to a show or while |
| 327 // hidden. | 318 // hidden. |
| 328 SetStoredFocusView(focused_view_); | 319 SetStoredFocusView(focused_view_); |
| 329 if (focused_view_) { | 320 if (focused_view_) |
| 330 FocusTextInputClient(focused_view_); | |
| 331 focused_view_->Focus(); | 321 focused_view_->Focus(); |
| 332 } | |
| 333 | 322 |
| 334 FOR_EACH_OBSERVER(FocusChangeListener, focus_change_listeners_, | 323 FOR_EACH_OBSERVER(FocusChangeListener, focus_change_listeners_, |
| 335 OnDidChangeFocus(old_focused_view, focused_view_)); | 324 OnDidChangeFocus(old_focused_view, focused_view_)); |
| 336 } | 325 } |
| 337 | 326 |
| 338 void FocusManager::ClearFocus() { | 327 void FocusManager::ClearFocus() { |
| 339 // SetFocusedView(NULL) is going to clear out the stored view to. We need to | 328 // SetFocusedView(NULL) is going to clear out the stored view to. We need to |
| 340 // persist it in this case. | 329 // persist it in this case. |
| 341 views::View* focused_view = GetStoredFocusView(); | 330 views::View* focused_view = GetStoredFocusView(); |
| 342 SetFocusedView(NULL); | 331 SetFocusedView(NULL); |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 436 return NULL; | 425 return NULL; |
| 437 } | 426 } |
| 438 | 427 |
| 439 return view_storage->RetrieveView(stored_focused_view_storage_id_); | 428 return view_storage->RetrieveView(stored_focused_view_storage_id_); |
| 440 } | 429 } |
| 441 | 430 |
| 442 void FocusManager::ClearStoredFocusedView() { | 431 void FocusManager::ClearStoredFocusedView() { |
| 443 SetStoredFocusView(NULL); | 432 SetStoredFocusView(NULL); |
| 444 } | 433 } |
| 445 | 434 |
| 446 void FocusManager::OnTextInputClientChanged(View* view) { | |
| 447 if (view == focused_view_) | |
| 448 FocusTextInputClient(view); | |
| 449 } | |
| 450 | |
| 451 void FocusManager::FocusTextInputClient(View* view) { | |
| 452 if (!switches::IsTextInputFocusManagerEnabled()) | |
| 453 return; | |
| 454 | |
| 455 // If the widget is not active, do not steal the text input focus. | |
| 456 if (!widget_->IsActive()) | |
| 457 return; | |
| 458 | |
| 459 ui::TextInputClient* text_input_client = | |
| 460 view ? view->GetTextInputClient() : NULL; | |
| 461 ui::TextInputFocusManager::GetInstance()-> | |
| 462 FocusTextInputClient(text_input_client); | |
| 463 ui::InputMethod* input_method = widget_->GetHostInputMethod(); | |
| 464 if (input_method) { | |
| 465 input_method->OnTextInputTypeChanged(text_input_client); | |
| 466 input_method->OnCaretBoundsChanged(text_input_client); | |
| 467 } | |
| 468 } | |
| 469 | |
| 470 void FocusManager::BlurTextInputClient(View* view) { | |
| 471 if (!switches::IsTextInputFocusManagerEnabled()) | |
| 472 return; | |
| 473 | |
| 474 ui::TextInputClient* text_input_client = | |
| 475 view ? view->GetTextInputClient() : NULL; | |
| 476 if (text_input_client && text_input_client->HasCompositionText()) { | |
| 477 text_input_client->ConfirmCompositionText(); | |
| 478 ui::InputMethod* input_method = widget_->GetHostInputMethod(); | |
| 479 if (input_method && input_method->GetTextInputClient() == text_input_client) | |
| 480 input_method->CancelComposition(text_input_client); | |
| 481 } | |
| 482 ui::TextInputFocusManager::GetInstance()-> | |
| 483 BlurTextInputClient(text_input_client); | |
| 484 } | |
| 485 | |
| 486 // Find the next (previous if reverse is true) focusable view for the specified | 435 // Find the next (previous if reverse is true) focusable view for the specified |
| 487 // FocusTraversable, starting at the specified view, traversing down the | 436 // FocusTraversable, starting at the specified view, traversing down the |
| 488 // FocusTraversable hierarchy. | 437 // FocusTraversable hierarchy. |
| 489 View* FocusManager::FindFocusableView(FocusTraversable* focus_traversable, | 438 View* FocusManager::FindFocusableView(FocusTraversable* focus_traversable, |
| 490 View* starting_view, | 439 View* starting_view, |
| 491 bool reverse) { | 440 bool reverse) { |
| 492 FocusTraversable* new_focus_traversable = NULL; | 441 FocusTraversable* new_focus_traversable = NULL; |
| 493 View* new_starting_view = NULL; | 442 View* new_starting_view = NULL; |
| 494 View* v = focus_traversable->GetFocusSearch()->FindNextFocusableView( | 443 View* v = focus_traversable->GetFocusSearch()->FindNextFocusableView( |
| 495 starting_view, | 444 starting_view, |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 587 } | 536 } |
| 588 if (key_code == ui::VKEY_RIGHT || key_code == ui::VKEY_DOWN) { | 537 if (key_code == ui::VKEY_RIGHT || key_code == ui::VKEY_DOWN) { |
| 589 AdvanceFocus(false); | 538 AdvanceFocus(false); |
| 590 return true; | 539 return true; |
| 591 } | 540 } |
| 592 | 541 |
| 593 return false; | 542 return false; |
| 594 } | 543 } |
| 595 | 544 |
| 596 } // namespace views | 545 } // namespace views |
| OLD | NEW |