focus_cycler.cc 3.8 KB

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