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.
5 #include "ash/focus_cycler.h"
8 #include "ash/wm/window_cycle_controller.h"
9 #include "ui/aura/client/activation_client.h"
10 #include "ui/aura/window.h"
11 #include "ui/views/accessible_pane_view.h"
12 #include "ui/views/focus/focus_search.h"
13 #include "ui/views/widget/widget.h"
19 bool HasFocusableWindow() {
20 return !WindowCycleController::BuildWindowList(NULL
).empty();
27 FocusCycler::FocusCycler() : widget_activating_(NULL
) {
30 FocusCycler::~FocusCycler() {
33 void FocusCycler::AddWidget(views::Widget
* widget
) {
34 widgets_
.push_back(widget
);
37 void FocusCycler::RotateFocus(Direction direction
) {
38 const bool has_window
= HasFocusableWindow();
40 int count
= static_cast<int>(widgets_
.size());
41 int browser_index
= has_window
? count
: -1;
43 for (; index
< count
; ++index
) {
44 if (widgets_
[index
]->IsActive())
48 int start_index
= index
;
54 if (direction
== FORWARD
)
55 index
= (index
+ 1) % count
;
57 index
= ((index
- 1) + count
) % count
;
59 // Ensure that we don't loop more than once.
60 if (index
== start_index
)
63 if (index
== browser_index
) {
64 // Activate the first window.
65 WindowCycleController::Direction window_direction
=
66 direction
== FORWARD
? WindowCycleController::FORWARD
:
67 WindowCycleController::BACKWARD
;
68 ash::Shell::GetInstance()->window_cycle_controller()->HandleCycleWindow(
69 window_direction
, false);
72 if (FocusWidget(widgets_
[index
]))
78 bool FocusCycler::FocusWidget(views::Widget
* widget
) {
79 views::AccessiblePaneView
* view
=
80 static_cast<views::AccessiblePaneView
*>(widget
->GetContentsView());
81 if (view
->SetPaneFocusAndFocusDefault()) {
82 widget_activating_
= widget
;
84 widget_activating_
= NULL
;
85 if (widget
->IsActive())
91 } // namespace internal