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