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.
5 #ifndef ASH_WM_WINDOW_CYCLE_CONTROLLER_H_
6 #define ASH_WM_WINDOW_CYCLE_CONTROLLER_H_
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"
21 class ActivationClient
;
31 class WindowCycleList
;
33 // Controls cycling through windows with the keyboard, for example, via alt-tab.
34 // Windows are sorted primarily by most recently used, and then by screen order.
35 // We activate windows as you cycle through them, so the order on the screen
36 // may change during the gesture, but the most recently used list isn't updated
37 // until the cycling ends. Thus we maintain the state of the windows
38 // at the beginning of the gesture so you can cycle through in a consistent
40 class ASH_EXPORT WindowCycleController
41 : public aura::client::ActivationChangeObserver
,
42 public aura::WindowObserver
{
48 explicit WindowCycleController(
49 aura::client::ActivationClient
* activation_client
);
50 virtual ~WindowCycleController();
52 // Returns true if cycling through windows is enabled. This is false at
53 // certain times, such as when the lock screen is visible.
54 static bool CanCycle();
56 // Cycles between windows in the given |direction|. If |is_alt_down| then
57 // interprets this call as the start of a multi-step cycle sequence and
58 // installs a key filter to watch for alt being released.
59 void HandleCycleWindow(Direction direction
, bool is_alt_down
);
61 // Cycles between windows without maintaining a multi-step cycle sequence
63 void HandleLinearCycleWindow();
65 // Informs the controller that the Alt key has been released and it can
66 // terminate the existing multi-step cycle.
67 void AltKeyReleased();
69 // Returns true if we are in the middle of a window cycling gesture.
70 bool IsCycling() const { return windows_
.get() != NULL
; }
72 // Returns the WindowCycleList. Really only useful for testing.
73 const WindowCycleList
* windows() const { return windows_
.get(); }
75 // Set up the observers to handle window changes for the containers we care
76 // about. Called when a new root window is added.
77 void OnRootWindowAdded(aura::RootWindow
* root_window
);
79 // Returns the set of windows to cycle through. This method creates
80 // the vector based on the current set of windows across all valid root
81 // windows. As a result it is not necessarily the same as the set of
82 // windows being iterated over.
83 // If |mru_windows| is not NULL, windows in this list are put at the head of
85 // If |top_most_at_end| the window list will return in ascending order instead
86 // of the default descending.
87 static std::vector
<aura::Window
*> BuildWindowList(
88 const std::list
<aura::Window
*>* mru_windows
,
89 bool top_most_at_end
);
92 // Call to start cycling windows. You must call StopCycling() when done.
95 // Cycles to the next or previous window based on |direction|.
96 void Step(Direction direction
);
98 // Installs an event filter to watch for release of the alt key.
99 void InstallEventFilter();
101 // Stops the current window cycle and cleans up the event filter.
104 // Checks if the window represents a container whose children we track.
105 static bool IsTrackedContainer(aura::Window
* window
);
107 // Overridden from aura::client::ActivationChangeObserver:
108 virtual void OnWindowActivated(aura::Window
* gained_active
,
109 aura::Window
* lost_active
) OVERRIDE
;
111 // Overridden from WindowObserver:
112 virtual void OnWindowAdded(aura::Window
* window
) OVERRIDE
;
113 virtual void OnWillRemoveWindow(aura::Window
* window
) OVERRIDE
;
114 virtual void OnWindowDestroying(aura::Window
* window
) OVERRIDE
;
116 scoped_ptr
<WindowCycleList
> windows_
;
118 // Event handler to watch for release of alt key.
119 scoped_ptr
<ui::EventHandler
> event_handler_
;
121 // List of windows that have been activated in containers that we cycle
122 // through, sorted by most recently used.
123 std::list
<aura::Window
*> mru_windows_
;
125 aura::client::ActivationClient
* activation_client_
;
127 DISALLOW_COPY_AND_ASSIGN(WindowCycleController
);
132 #endif // ASH_WM_WINDOW_CYCLE_CONTROLLER_H_