Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / ash / wm / window_cycle_controller.cc
blobca58f04fe7340a2b3fb456a63bd42b0ba0a387d4
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"
9 #include "ash/shell.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"
17 namespace ash {
19 namespace {
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
28 // multiple windows.
29 class WindowCycleEventFilter : public ui::EventHandler {
30 public:
31 WindowCycleEventFilter();
32 ~WindowCycleEventFilter() override;
34 // Overridden from ui::EventHandler:
35 void OnKeyEvent(ui::KeyEvent* event) override;
37 private:
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.
58 } // namespace
60 //////////////////////////////////////////////////////////////////////////////
61 // WindowCycleController, public:
63 WindowCycleController::WindowCycleController() {
66 WindowCycleController::~WindowCycleController() {
69 // static
70 bool WindowCycleController::CanCycle() {
71 // Don't allow window cycling if the screen is locked or a modal dialog is
72 // open.
73 return !Shell::GetInstance()->session_state_delegate()->IsScreenLocked() &&
74 !Shell::GetInstance()->IsSystemModalWindowOpen();
77 void WindowCycleController::HandleCycleWindow(Direction direction) {
78 if (!CanCycle())
79 return;
81 if (!IsCycling())
82 StartCycling();
84 Step(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) {
120 Shell::GetInstance()
121 ->metrics()
122 ->task_switch_metrics_recorder()
123 .OnTaskSwitch(TaskSwitchMetricsRecorder::WINDOW_CYCLE_CONTROLLER);
125 active_window_before_window_cycle_ = nullptr;
128 } // namespace ash