1 // Copyright 2014 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"
7 #include "ash/metrics/user_metrics_recorder.h"
8 #include "ash/session/session_state_delegate.h"
10 #include "ash/wm/mru_window_tracker.h"
11 #include "ash/wm/window_cycle_list.h"
12 #include "base/metrics/histogram.h"
13 #include "ui/aura/window.h"
14 #include "ui/events/event.h"
15 #include "ui/events/event_handler.h"
21 // Returns the most recently active window from the |window_list| or nullptr
22 // if the list is empty.
23 aura::Window
* GetActiveWindow(const MruWindowTracker::WindowList
& window_list
) {
24 return window_list
.empty() ? nullptr : window_list
[0];
27 // Filter to watch for the termination of a keyboard gesture to cycle through
29 class WindowCycleEventFilter
: public ui::EventHandler
{
31 WindowCycleEventFilter();
32 ~WindowCycleEventFilter() override
;
34 // Overridden from ui::EventHandler:
35 void OnKeyEvent(ui::KeyEvent
* event
) override
;
38 DISALLOW_COPY_AND_ASSIGN(WindowCycleEventFilter
);
41 WindowCycleEventFilter::WindowCycleEventFilter() {
42 Shell::GetInstance()->AddPreTargetHandler(this);
45 WindowCycleEventFilter::~WindowCycleEventFilter() {
46 Shell::GetInstance()->RemovePreTargetHandler(this);
49 void WindowCycleEventFilter::OnKeyEvent(ui::KeyEvent
* event
) {
50 // Views uses VKEY_MENU for both left and right Alt keys.
51 if (event
->key_code() == ui::VKEY_MENU
&&
52 event
->type() == ui::ET_KEY_RELEASED
) {
53 Shell::GetInstance()->window_cycle_controller()->StopCycling();
54 // Warning: |this| will be deleted from here on.
60 //////////////////////////////////////////////////////////////////////////////
61 // WindowCycleController, public:
63 WindowCycleController::WindowCycleController() {
66 WindowCycleController::~WindowCycleController() {
70 bool WindowCycleController::CanCycle() {
71 // Don't allow window cycling if the screen is locked or a modal dialog is
73 return !Shell::GetInstance()->session_state_delegate()->IsScreenLocked() &&
74 !Shell::GetInstance()->IsSystemModalWindowOpen();
77 void WindowCycleController::HandleCycleWindow(Direction direction
) {
87 void WindowCycleController::StartCycling() {
88 MruWindowTracker::WindowList window_list
=
89 Shell::GetInstance()->mru_window_tracker()->BuildMruWindowList();
91 active_window_before_window_cycle_
= GetActiveWindow(window_list
);
93 window_cycle_list_
.reset(new WindowCycleList(window_list
));
94 event_handler_
.reset(new WindowCycleEventFilter());
95 cycle_start_time_
= base::Time::Now();
96 Shell::GetInstance()->metrics()->RecordUserMetricsAction(UMA_WINDOW_CYCLE
);
99 //////////////////////////////////////////////////////////////////////////////
100 // WindowCycleController, private:
102 void WindowCycleController::Step(Direction direction
) {
103 DCHECK(window_cycle_list_
.get());
104 window_cycle_list_
->Step(direction
);
107 void WindowCycleController::StopCycling() {
108 window_cycle_list_
.reset();
110 aura::Window
* active_window_after_window_cycle
= GetActiveWindow(
111 Shell::GetInstance()->mru_window_tracker()->BuildMruWindowList());
113 // Remove our key event filter.
114 event_handler_
.reset();
115 UMA_HISTOGRAM_MEDIUM_TIMES("Ash.WindowCycleController.CycleTime",
116 base::Time::Now() - cycle_start_time_
);
118 if (active_window_after_window_cycle
!= nullptr &&
119 active_window_before_window_cycle_
!= active_window_after_window_cycle
) {
122 ->task_switch_metrics_recorder()
123 .OnTaskSwitch(TaskSwitchMetricsRecorder::WINDOW_CYCLE_CONTROLLER
);
125 active_window_before_window_cycle_
= nullptr;