| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "ash/common/wm/window_cycle_controller.h" | |
| 6 | |
| 7 #include "ash/common/metrics/task_switch_source.h" | |
| 8 #include "ash/common/session/session_state_delegate.h" | |
| 9 #include "ash/common/wm/mru_window_tracker.h" | |
| 10 #include "ash/common/wm/window_cycle_event_filter.h" | |
| 11 #include "ash/common/wm/window_cycle_list.h" | |
| 12 #include "ash/common/wm/window_state.h" | |
| 13 #include "ash/common/wm_shell.h" | |
| 14 #include "ash/common/wm_window.h" | |
| 15 #include "ash/public/cpp/shell_window_ids.h" | |
| 16 #include "base/metrics/histogram_macros.h" | |
| 17 | |
| 18 namespace ash { | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 // Returns the most recently active window from the |window_list| or nullptr | |
| 23 // if the list is empty. | |
| 24 WmWindow* GetActiveWindow(const MruWindowTracker::WindowList& window_list) { | |
| 25 return window_list.empty() ? nullptr : window_list[0]; | |
| 26 } | |
| 27 | |
| 28 } // namespace | |
| 29 | |
| 30 ////////////////////////////////////////////////////////////////////////////// | |
| 31 // WindowCycleController, public: | |
| 32 | |
| 33 WindowCycleController::WindowCycleController() {} | |
| 34 | |
| 35 WindowCycleController::~WindowCycleController() {} | |
| 36 | |
| 37 // static | |
| 38 bool WindowCycleController::CanCycle() { | |
| 39 // Prevent window cycling if the screen is locked or a modal dialog is open. | |
| 40 WmShell* wm_shell = WmShell::Get(); | |
| 41 return !wm_shell->GetSessionStateDelegate()->IsScreenLocked() && | |
| 42 !wm_shell->IsSystemModalWindowOpen() && !wm_shell->IsPinned(); | |
| 43 } | |
| 44 | |
| 45 void WindowCycleController::HandleCycleWindow(Direction direction) { | |
| 46 if (!CanCycle()) | |
| 47 return; | |
| 48 | |
| 49 if (!IsCycling()) | |
| 50 StartCycling(); | |
| 51 | |
| 52 Step(direction); | |
| 53 } | |
| 54 | |
| 55 void WindowCycleController::StartCycling() { | |
| 56 MruWindowTracker::WindowList window_list = | |
| 57 WmShell::Get()->mru_window_tracker()->BuildMruWindowList(); | |
| 58 // Exclude windows: | |
| 59 // - non user positionable windows, such as extension popups. | |
| 60 // - windows being dragged | |
| 61 // - the AppList window, which will hide as soon as cycling starts | |
| 62 // anyway. It doesn't make sense to count it as a "switchable" window, yet | |
| 63 // a lot of code relies on the MRU list returning the app window. If we | |
| 64 // don't manually remove it, the window cycling UI won't crash or misbehave, | |
| 65 // but there will be a flicker as the target window changes. Also exclude | |
| 66 // unselectable windows such as extension popups. | |
| 67 auto window_is_ineligible = [](WmWindow* window) { | |
| 68 wm::WindowState* state = window->GetWindowState(); | |
| 69 return !state->IsUserPositionable() || state->is_dragged() || | |
| 70 window->GetRootWindow() | |
| 71 ->GetChildByShellWindowId(kShellWindowId_AppListContainer) | |
| 72 ->Contains(window); | |
| 73 }; | |
| 74 window_list.erase(std::remove_if(window_list.begin(), window_list.end(), | |
| 75 window_is_ineligible), | |
| 76 window_list.end()); | |
| 77 | |
| 78 active_window_before_window_cycle_ = GetActiveWindow(window_list); | |
| 79 | |
| 80 window_cycle_list_.reset(new WindowCycleList(window_list)); | |
| 81 event_filter_ = WmShell::Get()->CreateWindowCycleEventFilter(); | |
| 82 cycle_start_time_ = base::Time::Now(); | |
| 83 WmShell::Get()->RecordUserMetricsAction(UMA_WINDOW_CYCLE); | |
| 84 UMA_HISTOGRAM_COUNTS_100("Ash.WindowCycleController.Items", | |
| 85 window_list.size()); | |
| 86 } | |
| 87 | |
| 88 void WindowCycleController::CompleteCycling() { | |
| 89 window_cycle_list_->set_user_did_accept(true); | |
| 90 StopCycling(); | |
| 91 } | |
| 92 | |
| 93 void WindowCycleController::CancelCycling() { | |
| 94 StopCycling(); | |
| 95 } | |
| 96 | |
| 97 ////////////////////////////////////////////////////////////////////////////// | |
| 98 // WindowCycleController, private: | |
| 99 | |
| 100 void WindowCycleController::Step(Direction direction) { | |
| 101 DCHECK(window_cycle_list_.get()); | |
| 102 window_cycle_list_->Step(direction); | |
| 103 } | |
| 104 | |
| 105 void WindowCycleController::StopCycling() { | |
| 106 UMA_HISTOGRAM_COUNTS_100("Ash.WindowCycleController.SelectionDepth", | |
| 107 window_cycle_list_->current_index() + 1); | |
| 108 window_cycle_list_.reset(); | |
| 109 | |
| 110 WmWindow* active_window_after_window_cycle = GetActiveWindow( | |
| 111 WmShell::Get()->mru_window_tracker()->BuildMruWindowList()); | |
| 112 | |
| 113 // Remove our key event filter. | |
| 114 event_filter_.reset(); | |
| 115 UMA_HISTOGRAM_MEDIUM_TIMES("Ash.WindowCycleController.CycleTime", | |
| 116 base::Time::Now() - cycle_start_time_); | |
| 117 | |
| 118 if (active_window_after_window_cycle != nullptr && | |
| 119 active_window_before_window_cycle_ != active_window_after_window_cycle) { | |
| 120 WmShell::Get()->RecordTaskSwitchMetric( | |
| 121 TaskSwitchSource::WINDOW_CYCLE_CONTROLLER); | |
| 122 } | |
| 123 active_window_before_window_cycle_ = nullptr; | |
| 124 } | |
| 125 | |
| 126 } // namespace ash | |
| OLD | NEW |