| 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/aura/wm_window_aura.h" | |
| 8 #include "ash/common/wm/mru_window_tracker.h" | |
| 9 #include "ash/common/wm/window_state.h" | |
| 10 #include "ash/shell.h" | |
| 11 #include "ash/wm/window_state_aura.h" | |
| 12 #include "ash/wm/window_util.h" | |
| 13 #include "ui/aura/window.h" | |
| 14 #include "ui/views/accessible_pane_view.h" | |
| 15 #include "ui/views/focus/focus_search.h" | |
| 16 #include "ui/views/widget/widget.h" | |
| 17 #include "ui/wm/public/activation_client.h" | |
| 18 | |
| 19 namespace ash { | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 bool HasFocusableWindow() { | |
| 24 return !ash::Shell::GetInstance()-> | |
| 25 mru_window_tracker()->BuildMruWindowList().empty(); | |
| 26 } | |
| 27 | |
| 28 } // namespace | |
| 29 | |
| 30 FocusCycler::FocusCycler() : widget_activating_(NULL) { | |
| 31 } | |
| 32 | |
| 33 FocusCycler::~FocusCycler() { | |
| 34 } | |
| 35 | |
| 36 void FocusCycler::AddWidget(views::Widget* widget) { | |
| 37 widgets_.push_back(widget); | |
| 38 } | |
| 39 | |
| 40 void FocusCycler::RemoveWidget(views::Widget* widget) { | |
| 41 auto iter = std::find(widgets_.begin(), widgets_.end(), widget); | |
| 42 if (iter != widgets_.end()) | |
| 43 widgets_.erase(iter); | |
| 44 } | |
| 45 | |
| 46 void FocusCycler::RotateFocus(Direction direction) { | |
| 47 aura::Window* window = ash::wm::GetActiveWindow(); | |
| 48 if (window) { | |
| 49 views::Widget* widget = views::Widget::GetWidgetForNativeWindow(window); | |
| 50 // First try to rotate focus within the active widget. If that succeeds, | |
| 51 // we're done. | |
| 52 if (widget && widget->GetFocusManager()->RotatePaneFocus( | |
| 53 direction == BACKWARD ? | |
| 54 views::FocusManager::kBackward : views::FocusManager::kForward, | |
| 55 views::FocusManager::kNoWrap)) { | |
| 56 return; | |
| 57 } | |
| 58 } | |
| 59 | |
| 60 const bool has_window = HasFocusableWindow(); | |
| 61 int index = 0; | |
| 62 int count = static_cast<int>(widgets_.size()); | |
| 63 int browser_index = has_window ? count : -1; | |
| 64 | |
| 65 for (; index < count; ++index) { | |
| 66 if (widgets_[index]->IsActive()) | |
| 67 break; | |
| 68 } | |
| 69 | |
| 70 int start_index = index; | |
| 71 | |
| 72 if (has_window) | |
| 73 ++count; | |
| 74 | |
| 75 for (;;) { | |
| 76 if (direction == FORWARD) | |
| 77 index = (index + 1) % count; | |
| 78 else | |
| 79 index = ((index - 1) + count) % count; | |
| 80 | |
| 81 // Ensure that we don't loop more than once. | |
| 82 if (index == start_index) | |
| 83 break; | |
| 84 | |
| 85 if (index == browser_index) { | |
| 86 // Activate the most recently active browser window. | |
| 87 MruWindowTracker::WindowList mru_windows( | |
| 88 Shell::GetInstance()->mru_window_tracker()->BuildMruWindowList()); | |
| 89 if (mru_windows.empty()) | |
| 90 break; | |
| 91 WmWindow* window = mru_windows.front(); | |
| 92 window->GetWindowState()->Activate(); | |
| 93 views::Widget* widget = views::Widget::GetWidgetForNativeWindow( | |
| 94 WmWindowAura::GetAuraWindow(window)); | |
| 95 if (!widget) | |
| 96 break; | |
| 97 views::FocusManager* focus_manager = widget->GetFocusManager(); | |
| 98 focus_manager->ClearFocus(); | |
| 99 focus_manager->RotatePaneFocus( | |
| 100 direction == BACKWARD ? | |
| 101 views::FocusManager::kBackward : views::FocusManager::kForward, | |
| 102 views::FocusManager::kWrap); | |
| 103 break; | |
| 104 } else { | |
| 105 if (FocusWidget(widgets_[index])) | |
| 106 break; | |
| 107 } | |
| 108 } | |
| 109 } | |
| 110 | |
| 111 bool FocusCycler::FocusWidget(views::Widget* widget) { | |
| 112 // Note: It is not necessary to set the focus directly to the pane since that | |
| 113 // will be taken care of by the widget activation. | |
| 114 widget_activating_ = widget; | |
| 115 widget->Activate(); | |
| 116 widget_activating_ = NULL; | |
| 117 return widget->IsActive(); | |
| 118 } | |
| 119 | |
| 120 } // namespace ash | |
| OLD | NEW |