Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(237)

Side by Side Diff: ui/views/focus/focus_manager.cc

Issue 173803002: Redesigns the text input focus handling. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed review comments. Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
530 return true; 543 return true;
531 } 544 }
532 if (key_code == ui::VKEY_RIGHT || key_code == ui::VKEY_DOWN) { 545 if (key_code == ui::VKEY_RIGHT || key_code == ui::VKEY_DOWN) {
533 AdvanceFocus(false); 546 AdvanceFocus(false);
534 return true; 547 return true;
535 } 548 }
536 549
537 return false; 550 return false;
538 } 551 }
539 552
553 void FocusManager::FocusTextInputClient(View* view) {
554 if (!switches::IsTextInputFocusManagerEnabled())
555 return;
556
557 // If the widget is not active, do not steal the text input focus.
558 if (!widget_->IsActive())
559 return;
560
561 ui::TextInputClient* text_input_client =
562 view ? view->GetTextInputClient() : NULL;
563 ui::TextInputFocusManager::GetInstance()->
564 FocusTextInputClient(text_input_client);
565 ui::InputMethod* input_method = widget_->GetHostInputMethod();
566 if (input_method) {
567 input_method->OnTextInputTypeChanged(text_input_client);
568 input_method->OnCaretBoundsChanged(text_input_client);
569 }
570 }
571
572 void FocusManager::BlurTextInputClient(View* view) {
573 if (!switches::IsTextInputFocusManagerEnabled())
574 return;
575
576 ui::TextInputClient* text_input_client =
577 view ? view->GetTextInputClient() : NULL;
578 if (text_input_client && text_input_client->HasCompositionText()) {
579 text_input_client->ConfirmCompositionText();
580 ui::InputMethod* input_method = widget_->GetHostInputMethod();
581 if (input_method && input_method->GetTextInputClient() == text_input_client)
582 input_method->CancelComposition(text_input_client);
583 }
584 ui::TextInputFocusManager::GetInstance()->
585 BlurTextInputClient(text_input_client);
586 }
587
540 } // namespace views 588 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698