Add a struct for spellcheck marker
[chromium-blink-merge.git] / ash / focus_cycler.cc
blob81611dc8020d41b64d403b1faa7b8324b0a8ef43
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 "ash/wm/window_util.h"
10 #include "ui/aura/client/activation_client.h"
11 #include "ui/aura/window.h"
12 #include "ui/views/accessible_pane_view.h"
13 #include "ui/views/focus/focus_search.h"
14 #include "ui/views/widget/widget.h"
16 namespace ash {
18 namespace {
20 bool HasFocusableWindow() {
21 return !WindowCycleController::BuildWindowList(NULL, false).empty();
24 } // namespace
26 namespace internal {
28 FocusCycler::FocusCycler() : widget_activating_(NULL) {
31 FocusCycler::~FocusCycler() {
34 void FocusCycler::AddWidget(views::Widget* widget) {
35 widgets_.push_back(widget);
38 void FocusCycler::RotateFocus(Direction direction) {
39 aura::Window* window = ash::wm::GetActiveWindow();
40 if (window) {
41 views::Widget* widget = views::Widget::GetWidgetForNativeWindow(window);
42 // First try to rotate focus within the active widget. If that succeeds,
43 // we're done.
44 if (widget && widget->GetFocusManager()->RotatePaneFocus(
45 direction == BACKWARD ?
46 views::FocusManager::kBackward : views::FocusManager::kForward,
47 views::FocusManager::kNoWrap)) {
48 return;
52 const bool has_window = HasFocusableWindow();
53 int index = 0;
54 int count = static_cast<int>(widgets_.size());
55 int browser_index = has_window ? count : -1;
57 for (; index < count; ++index) {
58 if (widgets_[index]->IsActive())
59 break;
62 int start_index = index;
64 if (has_window)
65 ++count;
67 for (;;) {
68 if (direction == FORWARD)
69 index = (index + 1) % count;
70 else
71 index = ((index - 1) + count) % count;
73 // Ensure that we don't loop more than once.
74 if (index == start_index)
75 break;
77 if (index == browser_index) {
78 // Activate the most recently active browser window.
79 ash::Shell::GetInstance()->window_cycle_controller()->HandleCycleWindow(
80 WindowCycleController::FORWARD, false);
82 // Rotate pane focus within that window.
83 aura::Window* window = ash::wm::GetActiveWindow();
84 if (!window)
85 break;
86 views::Widget* widget = views::Widget::GetWidgetForNativeWindow(window);
87 if (!widget)
88 break;
89 views::FocusManager* focus_manager = widget->GetFocusManager();
90 focus_manager->ClearFocus();
91 focus_manager->RotatePaneFocus(
92 direction == BACKWARD ?
93 views::FocusManager::kBackward : views::FocusManager::kForward,
94 views::FocusManager::kWrap);
95 break;
96 } else {
97 if (FocusWidget(widgets_[index]))
98 break;
103 bool FocusCycler::FocusWidget(views::Widget* widget) {
104 // Note: It is not necessary to set the focus directly to the pane since that
105 // will be taken care of by the widget activation.
106 widget_activating_ = widget;
107 widget->Activate();
108 widget_activating_ = NULL;
109 return widget->IsActive();
112 } // namespace internal
114 } // namespace ash