| 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" | 16 #include "ui/base/ime/text_input_focus_manager.h" |
| 17 #include "ui/base/ui_base_switches_util.h" | 17 #include "ui/base/ui_base_switches_util.h" |
| 18 #include "ui/events/event.h" | 18 #include "ui/events/event.h" |
| 19 #include "ui/events/keycodes/keyboard_codes.h" | 19 #include "ui/events/keycodes/keyboard_codes.h" |
| 20 #include "ui/views/focus/focus_manager_delegate.h" | |
| 21 #include "ui/views/focus/focus_search.h" | 20 #include "ui/views/focus/focus_search.h" |
| 22 #include "ui/views/focus/view_storage.h" | 21 #include "ui/views/focus/view_storage.h" |
| 23 #include "ui/views/focus/widget_focus_manager.h" | 22 #include "ui/views/focus/widget_focus_manager.h" |
| 24 #include "ui/views/view.h" | 23 #include "ui/views/view.h" |
| 25 #include "ui/views/widget/root_view.h" | 24 #include "ui/views/widget/root_view.h" |
| 26 #include "ui/views/widget/widget.h" | 25 #include "ui/views/widget/widget.h" |
| 27 #include "ui/views/widget/widget_delegate.h" | 26 #include "ui/views/widget/widget_delegate.h" |
| 28 | 27 |
| 29 namespace views { | 28 namespace views { |
| 30 | 29 |
| 31 namespace { | 30 namespace { |
| 32 | 31 |
| 33 #if defined(OS_MACOSX) || defined(OS_CHROMEOS) | 32 #if defined(OS_MACOSX) || defined(OS_CHROMEOS) |
| 34 static const int kEventFlagsMask = ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN | | 33 static const int kEventFlagsMask = ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN | |
| 35 ui::EF_ALT_DOWN | ui::EF_COMMAND_DOWN; | 34 ui::EF_ALT_DOWN | ui::EF_COMMAND_DOWN; |
| 36 #else | 35 #else |
| 37 static const int kEventFlagsMask = ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN | | 36 static const int kEventFlagsMask = ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN | |
| 38 ui::EF_ALT_DOWN; | 37 ui::EF_ALT_DOWN; |
| 39 #endif | 38 #endif |
| 40 | 39 |
| 41 static inline int CalculateModifiers(const ui::KeyEvent& event) { | 40 static inline int CalculateModifiers(const ui::KeyEvent& event) { |
| 42 return event.flags() & kEventFlagsMask; | 41 return event.flags() & kEventFlagsMask; |
| 43 } | 42 } |
| 44 | 43 |
| 45 } // namespace | 44 } // namespace |
| 46 | 45 |
| 47 bool FocusManager::shortcut_handling_suspended_ = false; | |
| 48 bool FocusManager::arrow_key_traversal_enabled_ = false; | 46 bool FocusManager::arrow_key_traversal_enabled_ = false; |
| 49 | 47 |
| 50 FocusManager::FocusManager(Widget* widget, FocusManagerDelegate* delegate) | 48 FocusManager::FocusManager(Widget* widget) |
| 51 : widget_(widget), | 49 : widget_(widget), |
| 52 delegate_(delegate), | |
| 53 focused_view_(NULL), | 50 focused_view_(NULL), |
| 54 accelerator_manager_(new ui::AcceleratorManager), | 51 accelerator_manager_(new ui::AcceleratorManager), |
| 55 focus_change_reason_(kReasonDirectFocusChange), | 52 focus_change_reason_(kReasonDirectFocusChange), |
| 56 is_changing_focus_(false) { | 53 is_changing_focus_(false) { |
| 57 DCHECK(widget_); | 54 DCHECK(widget_); |
| 58 stored_focused_view_storage_id_ = | 55 stored_focused_view_storage_id_ = |
| 59 ViewStorage::GetInstance()->CreateStorageID(); | 56 ViewStorage::GetInstance()->CreateStorageID(); |
| 57 accelerator_processors_.push_back(accelerator_manager_.get()); |
| 60 } | 58 } |
| 61 | 59 |
| 62 FocusManager::~FocusManager() { | 60 FocusManager::~FocusManager() { |
| 63 } | 61 } |
| 64 | 62 |
| 65 bool FocusManager::OnKeyEvent(const ui::KeyEvent& event) { | 63 bool FocusManager::OnKeyEvent(const ui::KeyEvent& event) { |
| 66 const int key_code = event.key_code(); | 64 const int key_code = event.key_code(); |
| 67 | 65 |
| 68 if (event.type() != ui::ET_KEY_PRESSED && event.type() != ui::ET_KEY_RELEASED) | 66 if (event.type() != ui::ET_KEY_PRESSED && event.type() != ui::ET_KEY_RELEASED) |
| 69 return false; | 67 return false; |
| 70 | 68 |
| 71 if (shortcut_handling_suspended()) | |
| 72 return true; | |
| 73 | |
| 74 int modifiers = CalculateModifiers(event); | 69 int modifiers = CalculateModifiers(event); |
| 75 ui::Accelerator accelerator(event.key_code(), modifiers); | 70 ui::Accelerator accelerator(event.key_code(), modifiers); |
| 76 accelerator.set_type(event.type()); | 71 accelerator.set_type(event.type()); |
| 77 accelerator.set_is_repeat(event.IsRepeat()); | 72 accelerator.set_is_repeat(event.IsRepeat()); |
| 78 | 73 |
| 79 if (event.type() == ui::ET_KEY_PRESSED) { | 74 if (event.type() == ui::ET_KEY_PRESSED) { |
| 80 // If the focused view wants to process the key event as is, let it be. | 75 // If the focused view wants to process the key event as is, let it be. |
| 81 if (focused_view_ && focused_view_->SkipDefaultKeyEventProcessing(event) && | 76 if (focused_view_ && focused_view_->SkipDefaultKeyEventProcessing(event) && |
| 82 !accelerator_manager_->HasPriorityHandler(accelerator)) | 77 !accelerator_manager_->HasPriorityHandler(accelerator)) |
| 83 return true; | 78 return true; |
| (...skipping 461 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 545 void FocusManager::UnregisterAccelerator(const ui::Accelerator& accelerator, | 540 void FocusManager::UnregisterAccelerator(const ui::Accelerator& accelerator, |
| 546 ui::AcceleratorTarget* target) { | 541 ui::AcceleratorTarget* target) { |
| 547 accelerator_manager_->Unregister(accelerator, target); | 542 accelerator_manager_->Unregister(accelerator, target); |
| 548 } | 543 } |
| 549 | 544 |
| 550 void FocusManager::UnregisterAccelerators(ui::AcceleratorTarget* target) { | 545 void FocusManager::UnregisterAccelerators(ui::AcceleratorTarget* target) { |
| 551 accelerator_manager_->UnregisterAll(target); | 546 accelerator_manager_->UnregisterAll(target); |
| 552 } | 547 } |
| 553 | 548 |
| 554 bool FocusManager::ProcessAccelerator(const ui::Accelerator& accelerator) { | 549 bool FocusManager::ProcessAccelerator(const ui::Accelerator& accelerator) { |
| 555 if (accelerator_manager_->Process(accelerator)) | 550 for (ui::AcceleratorProcessor* processor : accelerator_processors_) { |
| 556 return true; | 551 if (processor->ProcessAccelerator(accelerator)) |
| 557 if (delegate_.get()) | 552 return true; |
| 558 return delegate_->ProcessAccelerator(accelerator); | 553 } |
| 559 return false; | 554 return false; |
| 560 } | 555 } |
| 561 | 556 |
| 562 ui::AcceleratorTarget* FocusManager::GetCurrentTargetForAccelerator( | 557 ui::AcceleratorTarget* FocusManager::GetTargetForAccelerator( |
| 563 const ui::Accelerator& accelerator) const { | 558 const ui::Accelerator& accelerator) const { |
| 564 ui::AcceleratorTarget* target = | 559 for (ui::AcceleratorProcessor* processor : accelerator_processors_) { |
| 565 accelerator_manager_->GetCurrentTarget(accelerator); | 560 ui::AcceleratorTarget* target = |
| 566 if (!target && delegate_.get()) | 561 processor->GetTargetForAccelerator(accelerator); |
| 567 target = delegate_->GetCurrentTargetForAccelerator(accelerator); | 562 if (target) |
| 568 return target; | 563 return target; |
| 564 } |
| 565 return nullptr; |
| 566 } |
| 567 |
| 568 void FocusManager::AddAcceleratorPreProcessor( |
| 569 ui::AcceleratorProcessor* processor) { |
| 570 accelerator_processors_.push_front(processor); |
| 571 } |
| 572 |
| 573 void FocusManager::AddAcceleratorPostProcessor( |
| 574 ui::AcceleratorProcessor* processor) { |
| 575 accelerator_processors_.push_back(processor); |
| 576 } |
| 577 |
| 578 void FocusManager::RemoveAcceleratorProcessor( |
| 579 ui::AcceleratorProcessor* processor) { |
| 580 accelerator_processors_.remove(processor); |
| 569 } | 581 } |
| 570 | 582 |
| 571 bool FocusManager::HasPriorityHandler( | 583 bool FocusManager::HasPriorityHandler( |
| 572 const ui::Accelerator& accelerator) const { | 584 const ui::Accelerator& accelerator) const { |
| 573 return accelerator_manager_->HasPriorityHandler(accelerator); | 585 return accelerator_manager_->HasPriorityHandler(accelerator); |
| 574 } | 586 } |
| 575 | 587 |
| 576 // static | 588 // static |
| 577 bool FocusManager::IsTabTraversalKeyEvent(const ui::KeyEvent& key_event) { | 589 bool FocusManager::IsTabTraversalKeyEvent(const ui::KeyEvent& key_event) { |
| 578 return key_event.key_code() == ui::VKEY_TAB && !key_event.IsControlDown(); | 590 return key_event.key_code() == ui::VKEY_TAB && !key_event.IsControlDown(); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 606 } | 618 } |
| 607 if (key_code == ui::VKEY_RIGHT || key_code == ui::VKEY_DOWN) { | 619 if (key_code == ui::VKEY_RIGHT || key_code == ui::VKEY_DOWN) { |
| 608 AdvanceFocus(false); | 620 AdvanceFocus(false); |
| 609 return true; | 621 return true; |
| 610 } | 622 } |
| 611 | 623 |
| 612 return false; | 624 return false; |
| 613 } | 625 } |
| 614 | 626 |
| 615 } // namespace views | 627 } // namespace views |
| OLD | NEW |