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 #include "ash/wm/window_cycle_controller.h"
9 #include "ash/session_state_delegate.h"
10 #include "ash/shell.h"
11 #include "ash/shell_window_ids.h"
12 #include "ash/wm/mru_window_tracker.h"
13 #include "ash/wm/window_cycle_list.h"
14 #include "ash/wm/window_util.h"
15 #include "ash/wm/workspace_controller.h"
16 #include "ui/aura/root_window.h"
17 #include "ui/events/event.h"
18 #include "ui/events/event_handler.h"
24 // Filter to watch for the termination of a keyboard gesture to cycle through
26 class WindowCycleEventFilter : public ui::EventHandler {
28 WindowCycleEventFilter();
29 virtual ~WindowCycleEventFilter();
31 // Overridden from ui::EventHandler:
32 virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE;
34 DISALLOW_COPY_AND_ASSIGN(WindowCycleEventFilter);
37 // Watch for all keyboard events by filtering the root window.
38 WindowCycleEventFilter::WindowCycleEventFilter() {
41 WindowCycleEventFilter::~WindowCycleEventFilter() {
44 void WindowCycleEventFilter::OnKeyEvent(ui::KeyEvent* event) {
45 // Views uses VKEY_MENU for both left and right Alt keys.
46 if (event->key_code() == ui::VKEY_MENU &&
47 event->type() == ui::ET_KEY_RELEASED) {
48 Shell::GetInstance()->window_cycle_controller()->AltKeyReleased();
49 // Warning: |this| will be deleted from here on.
55 //////////////////////////////////////////////////////////////////////////////
56 // WindowCycleController, public:
58 WindowCycleController::WindowCycleController() {
61 WindowCycleController::~WindowCycleController() {
66 bool WindowCycleController::CanCycle() {
67 // Don't allow window cycling if the screen is locked or a modal dialog is
69 return !Shell::GetInstance()->session_state_delegate()->IsScreenLocked() &&
70 !Shell::GetInstance()->IsSystemModalWindowOpen();
73 void WindowCycleController::HandleCycleWindow(Direction direction,
80 // This is the start of an alt-tab cycle through multiple windows, so
81 // listen for the alt key being released to stop cycling.
86 // We're in the middle of an alt-tab cycle, just step forward.
90 // This is a simple, single-step window cycle.
97 void WindowCycleController::HandleLinearCycleWindow() {
98 if (!CanCycle() || IsCycling())
101 // Use the reversed list of windows to prevent a 2-cycle of the most recent
102 // windows occurring.
103 WindowCycleList cycle_list(MruWindowTracker::BuildWindowList(true));
104 cycle_list.Step(WindowCycleList::FORWARD);
107 void WindowCycleController::AltKeyReleased() {
111 //////////////////////////////////////////////////////////////////////////////
112 // WindowCycleController, private:
114 void WindowCycleController::StartCycling() {
115 windows_.reset(new WindowCycleList(ash::Shell::GetInstance()->
116 mru_window_tracker()->BuildMruWindowList()));
119 void WindowCycleController::Step(Direction direction) {
120 DCHECK(windows_.get());
121 windows_->Step(direction == FORWARD ? WindowCycleList::FORWARD :
122 WindowCycleList::BACKWARD);
125 void WindowCycleController::StopCycling() {
127 // Remove our key event filter.
128 if (event_handler_) {
129 Shell::GetInstance()->RemovePreTargetHandler(event_handler_.get());
130 event_handler_.reset();
134 void WindowCycleController::InstallEventFilter() {
135 event_handler_.reset(new WindowCycleEventFilter());
136 Shell::GetInstance()->AddPreTargetHandler(event_handler_.get());