window_cycle_controller.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
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#ifndef ASH_WM_WINDOW_CYCLE_CONTROLLER_H_ 6#define ASH_WM_WINDOW_CYCLE_CONTROLLER_H_ 7 8#include <list> 9#include <vector> 10 11#include "ash/ash_export.h" 12#include "base/basictypes.h" 13#include "base/memory/scoped_ptr.h" 14#include "ui/aura/client/activation_change_observer.h" 15#include "ui/aura/window_observer.h" 16 17namespace aura { 18class EventFilter; 19class RootWindow; 20class Window; 21} 22 23namespace ash { 24 25namespace internal { 26class ActivationController; 27} 28 29class WindowCycleList; 30 31// Controls cycling through windows with the keyboard, for example, via alt-tab. 32// Windows are sorted primarily by most recently used, and then by screen order. 33// We activate windows as you cycle through them, so the order on the screen 34// may change during the gesture, but the most recently used list isn't updated 35// until the cycling ends. Thus we maintain the state of the windows 36// at the beginning of the gesture so you can cycle through in a consistent 37// order. 38class ASH_EXPORT WindowCycleController 39 : public aura::client::ActivationChangeObserver, 40 public aura::WindowObserver { 41 public: 42 enum Direction { 43 FORWARD, 44 BACKWARD 45 }; 46 explicit WindowCycleController( 47 internal::ActivationController* activation_controller); 48 virtual ~WindowCycleController(); 49 50 // Returns true if cycling through windows is enabled. This is false at 51 // certain times, such as when the lock screen is visible. 52 static bool CanCycle(); 53 54 // Cycles between windows in the given |direction|. If |is_alt_down| then 55 // interprets this call as the start of a multi-step cycle sequence and 56 // installs a key filter to watch for alt being released. 57 void HandleCycleWindow(Direction direction, bool is_alt_down); 58 59 // Informs the controller that the Alt key has been released and it can 60 // terminate the existing multi-step cycle. 61 void AltKeyReleased(); 62 63 // Returns true if we are in the middle of a window cycling gesture. 64 bool IsCycling() const { return windows_.get() != NULL; } 65 66 // Returns the WindowCycleList. Really only useful for testing. 67 const WindowCycleList* windows() const { return windows_.get(); } 68 69 // Set up the observers to handle window changes for the containers we care 70 // about. Called when a new root window is added. 71 void OnRootWindowAdded(aura::RootWindow* root_window); 72 73 // Returns the set of windows to cycle through. This method creates 74 // the vector based on the current set of windows across all valid root 75 // windows. As a result it is not necessarily the same as the set of 76 // windows being iterated over. 77 // If |mru_windows| is not NULL, windows in this list are put at the head of 78 // the window list. 79 static std::vector<aura::Window*> BuildWindowList( 80 const std::list<aura::Window*>* mru_windows); 81 82 private: 83 // Call to start cycling windows. You must call StopCycling() when done. 84 void StartCycling(); 85 86 // Cycles to the next or previous window based on |direction|. 87 void Step(Direction direction); 88 89 // Installs an event filter to watch for release of the alt key. 90 void InstallEventFilter(); 91 92 // Stops the current window cycle and cleans up the event filter. 93 void StopCycling(); 94 95 // Checks if the window represents a container whose children we track. 96 static bool IsTrackedContainer(aura::Window* window); 97 98 // Overridden from ActivationChangeObserver: 99 virtual void OnWindowActivated(aura::Window* active, 100 aura::Window* old_active) OVERRIDE; 101 102 // Overridden from WindowObserver: 103 virtual void OnWindowAdded(aura::Window* window) OVERRIDE; 104 virtual void OnWillRemoveWindow(aura::Window* window) OVERRIDE; 105 virtual void OnWindowDestroying(aura::Window* window) OVERRIDE; 106 107 scoped_ptr<WindowCycleList> windows_; 108 109 // Event filter to watch for release of alt key. 110 scoped_ptr<aura::EventFilter> event_filter_; 111 112 // List of windows that have been activated in containers that we cycle 113 // through, sorted by most recently used. 114 std::list<aura::Window*> mru_windows_; 115 116 internal::ActivationController* activation_controller_; 117 118 DISALLOW_COPY_AND_ASSIGN(WindowCycleController); 119}; 120 121} // namespace ash 122 123#endif // ASH_WM_WINDOW_CYCLE_CONTROLLER_H_ 124