Update Swarm DEPS
[chromium-blink-merge.git] / ash / focus_cycler.cc
blob801cd357616fe146878d91d05c1fc5df2686e70c
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"
7 #include "ash/shell.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"
15 namespace ash {
17 namespace {
19 bool HasFocusableWindow() {
20 return !WindowCycleController::BuildWindowList(NULL).empty();
23 } // namespace
25 namespace internal {
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();
39 int index = 0;
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())
45 break;
48 int start_index = index;
50 if (has_window)
51 ++count;
53 for (;;) {
54 if (direction == FORWARD)
55 index = (index + 1) % count;
56 else
57 index = ((index - 1) + count) % count;
59 // Ensure that we don't loop more than once.
60 if (index == start_index)
61 break;
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);
70 break;
71 } else {
72 if (FocusWidget(widgets_[index]))
73 break;
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;
83 widget->Activate();
84 widget_activating_ = NULL;
85 if (widget->IsActive())
86 return true;
88 return false;
91 } // namespace internal
93 } // namespace ash