1 // Copyright 2012 The Chromium Authors
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/mru_window_tracker.h"
9 #include "ash/wm/window_state.h"
10 #include "ash/wm/window_util.h"
11 #include "base/ranges/algorithm.h"
12 #include "ui/views/accessible_pane_view.h"
13 #include "ui/views/focus/focus_search.h"
14 #include "ui/views/widget/widget.h"
15 #include "ui/views/widget/widget_delegate.h"
16 #include "ui/wm/public/activation_client.h"
22 bool HasFocusableWindow() {
24 ->mru_window_tracker()
25 ->BuildMruWindowList(kActiveDesk)
31 FocusCycler::FocusCycler() : widget_activating_(nullptr) {}
33 FocusCycler::~FocusCycler() = default;
35 void FocusCycler::AddWidget(views::Widget* widget) {
36 widgets_.push_back(widget);
39 void FocusCycler::RemoveWidget(views::Widget* widget) {
40 auto iter = base::ranges::find(widgets_, widget);
41 if (iter != widgets_.end())
45 void FocusCycler::RotateFocus(Direction direction) {
46 if (aura::Window* window = window_util::GetActiveWindow(); window) {
47 views::Widget* widget = views::Widget::GetWidgetForNativeView(window);
48 // First try to rotate focus within the active widget. If that succeeds,
51 widget->GetFocusManager()->RotatePaneFocus(
52 direction == BACKWARD ? views::FocusManager::Direction::kBackward
53 : views::FocusManager::Direction::kForward,
54 views::FocusManager::FocusCycleWrapping::kDisabled)) {
59 const bool has_window = HasFocusableWindow();
61 int count = static_cast<int>(widgets_.size());
62 int browser_index = has_window ? count : -1;
64 for (; index < count; ++index) {
65 if (widgets_[index]->IsActive())
69 int start_index = index;
75 if (direction == FORWARD)
76 index = (index + 1) % count;
78 index = ((index - 1) + count) % count;
80 // Ensure that we don't loop more than once.
81 if (index == start_index)
84 if (index == browser_index) {
85 // Activate the most recently active browser window.
86 MruWindowTracker::WindowList mru_windows(
87 Shell::Get()->mru_window_tracker()->BuildMruWindowList(kActiveDesk));
88 if (mru_windows.empty())
90 auto* window = mru_windows.front();
91 WindowState::Get(window)->Activate();
92 views::Widget* widget = views::Widget::GetWidgetForNativeView(window);
95 views::FocusManager* focus_manager = widget->GetFocusManager();
96 focus_manager->ClearFocus();
97 focus_manager->RotatePaneFocus(
98 direction == BACKWARD ? views::FocusManager::Direction::kBackward
99 : views::FocusManager::Direction::kForward,
100 views::FocusManager::FocusCycleWrapping::kEnabled);
103 if (FocusWidget(widgets_[index]))
109 bool FocusCycler::FocusWidget(views::Widget* widget) {
110 // If the target is PIP window, temporarily make it activatable.
111 if (WindowState::Get(widget->GetNativeWindow())->IsPip())
112 widget->widget_delegate()->SetCanActivate(true);
114 // Note: It is not necessary to set the focus directly to the pane since that
115 // will be taken care of by the widget activation.
116 widget_activating_ = widget;
118 widget_activating_ = NULL;
119 return widget->IsActive();
122 views::Widget* FocusCycler::FindWidget(
123 base::RepeatingCallback<bool(views::Widget*)> callback) {
124 for (auto* widget : widgets_) {
125 if (callback.Run(widget))