OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ash/focus_cycler.h" |
| 6 |
| 7 #include "ash/shell.h" |
| 8 #include "ash/shell_delegate.h" |
| 9 #include "ui/views/widget/widget.h" |
| 10 #include "ui/views/focus/focus_search.h" |
| 11 #include "ui/aura/window.h" |
| 12 #include "ui/aura/client/activation_client.h" |
| 13 |
| 14 #include "ui/views/accessible_pane_view.h" |
| 15 |
| 16 namespace ash { |
| 17 |
| 18 namespace internal { |
| 19 |
| 20 FocusCycler::FocusCycler() { |
| 21 } |
| 22 |
| 23 FocusCycler::~FocusCycler() { |
| 24 } |
| 25 |
| 26 void FocusCycler::AddWidget(views::Widget* widget) { |
| 27 widgets_.push_back(widget); |
| 28 |
| 29 widget->GetFocusManager()->RegisterAccelerator( |
| 30 ui::Accelerator(ui::VKEY_F2, false, true, false), this); |
| 31 widget->GetFocusManager()->RegisterAccelerator( |
| 32 ui::Accelerator(ui::VKEY_F1, false, true, false), this); |
| 33 } |
| 34 |
| 35 void FocusCycler::RotateFocus(Direction direction) { |
| 36 int index = 0; |
| 37 int count = static_cast<int>(widgets_.size()); |
| 38 int browser_index = count; |
| 39 |
| 40 for (; index < count; ++index) { |
| 41 if (widgets_[index]->IsActive()) |
| 42 break; |
| 43 } |
| 44 |
| 45 int start_index = index; |
| 46 |
| 47 count = count + 1; |
| 48 |
| 49 for (;;) { |
| 50 if (direction == FORWARD) |
| 51 index = (index + 1) % count; |
| 52 else |
| 53 index = ((index - 1) + count) % count; |
| 54 |
| 55 // Ensure that we don't loop more than once. |
| 56 if (index == start_index) |
| 57 break; |
| 58 |
| 59 if (index == browser_index) { |
| 60 // Activate the browser window. |
| 61 const std::vector<aura::Window*>& windows = |
| 62 Shell::GetInstance()->delegate()->GetCycleWindowList( |
| 63 ShellDelegate::SOURCE_LAUNCHER, ShellDelegate::ORDER_MRU); |
| 64 if (!windows.empty()) { |
| 65 aura::client::GetActivationClient()->ActivateWindow(windows[0]); |
| 66 break; |
| 67 } |
| 68 } else { |
| 69 views::Widget* widget = widgets_[index]; |
| 70 |
| 71 views::AccessiblePaneView* view = |
| 72 static_cast<views::AccessiblePaneView*>(widget->GetContentsView()); |
| 73 if (view->SetPaneFocusAndFocusDefault()) { |
| 74 widget->Activate(); |
| 75 if (widget->IsActive()) |
| 76 break; |
| 77 } |
| 78 } |
| 79 } |
| 80 } |
| 81 |
| 82 bool FocusCycler::AcceleratorPressed(const ui::Accelerator& accelerator) { |
| 83 switch (accelerator.key_code()) { |
| 84 case ui::VKEY_F1: |
| 85 RotateFocus(BACKWARD); |
| 86 return true; |
| 87 case ui::VKEY_F2: |
| 88 RotateFocus(FORWARD); |
| 89 return true; |
| 90 default: |
| 91 return false; |
| 92 } |
| 93 } |
| 94 |
| 95 bool FocusCycler::CanHandleAccelerators() const { |
| 96 return true; |
| 97 } |
| 98 |
| 99 } // namespace internal |
| 100 |
| 101 } // namespace ash |
OLD | NEW |