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()) { | |
sky
2012/02/06 16:29:32
nit: add a comment here, and remove {}
| |
76 break; | |
77 } | |
78 } | |
79 } | |
80 } | |
81 } | |
82 | |
83 bool FocusCycler::AcceleratorPressed(const ui::Accelerator& accelerator) { | |
84 switch (accelerator.key_code()) { | |
85 case ui::VKEY_F1: | |
86 RotateFocus(BACKWARD); | |
87 return true; | |
88 case ui::VKEY_F2: | |
89 RotateFocus(FORWARD); | |
90 return true; | |
91 default: | |
92 return false; | |
93 } | |
94 } | |
95 | |
96 bool FocusCycler::CanHandleAccelerators() const { | |
97 return true; | |
98 } | |
99 | |
100 } // namespace internal | |
101 | |
102 } // namespace ash | |
OLD | NEW |