Refactor ui::Clipboard::ObjectMap sanitization in ClipboardMsgFilter.
[chromium-blink-merge.git] / ash / focus_cycler.cc
blobefd958cabc8491c6f0376c4cf120fa7f24d13544
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/mru_window_tracker.h"
9 #include "ash/wm/window_state.h"
10 #include "ash/wm/window_util.h"
11 #include "ui/aura/client/activation_client.h"
12 #include "ui/aura/window.h"
13 #include "ui/views/accessible_pane_view.h"
14 #include "ui/views/focus/focus_search.h"
15 #include "ui/views/widget/widget.h"
17 namespace ash {
19 namespace {
21 bool HasFocusableWindow() {
22 return !MruWindowTracker::BuildWindowList(false).empty();
25 } // namespace
27 namespace internal {
29 FocusCycler::FocusCycler() : widget_activating_(NULL) {
32 FocusCycler::~FocusCycler() {
35 void FocusCycler::AddWidget(views::Widget* widget) {
36 widgets_.push_back(widget);
39 void FocusCycler::RotateFocus(Direction direction) {
40 aura::Window* window = ash::wm::GetActiveWindow();
41 if (window) {
42 views::Widget* widget = views::Widget::GetWidgetForNativeWindow(window);
43 // First try to rotate focus within the active widget. If that succeeds,
44 // we're done.
45 if (widget && widget->GetFocusManager()->RotatePaneFocus(
46 direction == BACKWARD ?
47 views::FocusManager::kBackward : views::FocusManager::kForward,
48 views::FocusManager::kNoWrap)) {
49 return;
53 const bool has_window = HasFocusableWindow();
54 int index = 0;
55 int count = static_cast<int>(widgets_.size());
56 int browser_index = has_window ? count : -1;
58 for (; index < count; ++index) {
59 if (widgets_[index]->IsActive())
60 break;
63 int start_index = index;
65 if (has_window)
66 ++count;
68 for (;;) {
69 if (direction == FORWARD)
70 index = (index + 1) % count;
71 else
72 index = ((index - 1) + count) % count;
74 // Ensure that we don't loop more than once.
75 if (index == start_index)
76 break;
78 if (index == browser_index) {
79 // Activate the most recently active browser window.
80 MruWindowTracker::WindowList mru_windows(
81 Shell::GetInstance()->mru_window_tracker()->BuildMruWindowList());
82 if (mru_windows.empty())
83 break;
84 aura::Window* window = mru_windows.front();
85 wm::GetWindowState(window)->Activate();
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