accelerator_commands.cc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. // Copyright 2013 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. #include "ash/accelerators/accelerator_commands.h"
  5. #include "ash/constants/ash_features.h"
  6. #include "ash/display/display_configuration_controller.h"
  7. #include "ash/focus_cycler.h"
  8. #include "ash/frame/non_client_frame_view_ash.h"
  9. #include "ash/ime/ime_controller_impl.h"
  10. #include "ash/media/media_controller_impl.h"
  11. #include "ash/public/cpp/new_window_delegate.h"
  12. #include "ash/root_window_controller.h"
  13. #include "ash/session/session_controller_impl.h"
  14. #include "ash/shelf/shelf.h"
  15. #include "ash/shell.h"
  16. #include "ash/system/keyboard_brightness_control_delegate.h"
  17. #include "ash/system/model/system_tray_model.h"
  18. #include "ash/system/status_area_widget.h"
  19. #include "ash/system/time/calendar_metrics.h"
  20. #include "ash/system/time/calendar_model.h"
  21. #include "ash/system/unified/date_tray.h"
  22. #include "ash/system/unified/unified_system_tray.h"
  23. #include "ash/system/unified/unified_system_tray_bubble.h"
  24. #include "ash/wm/float/float_controller.h"
  25. #include "ash/wm/mru_window_tracker.h"
  26. #include "ash/wm/screen_pinning_controller.h"
  27. #include "ash/wm/window_cycle/window_cycle_controller.h"
  28. #include "ash/wm/window_state.h"
  29. #include "ash/wm/window_util.h"
  30. #include "ash/wm/wm_event.h"
  31. #include "base/metrics/user_metrics.h"
  32. #include "chromeos/ash/components/audio/cras_audio_handler.h"
  33. #include "chromeos/dbus/power/power_manager_client.h"
  34. #include "chromeos/ui/base/window_properties.h"
  35. #include "ui/display/display.h"
  36. #include "ui/display/display_switches.h"
  37. #include "ui/display/manager/display_manager.h"
  38. #include "ui/display/manager/managed_display_info.h"
  39. #include "ui/display/screen.h"
  40. #include "ui/events/event.h"
  41. #include "ui/gfx/geometry/point.h"
  42. #include "ui/views/widget/widget.h"
  43. // Keep the functions in this file in alphabetical order.
  44. namespace ash {
  45. namespace accelerators {
  46. namespace {
  47. views::Widget* FindPipWidget() {
  48. return Shell::Get()->focus_cycler()->FindWidget(
  49. base::BindRepeating([](views::Widget* widget) {
  50. return WindowState::Get(widget->GetNativeWindow())->IsPip();
  51. }));
  52. }
  53. } // namespace
  54. void DumpCalendarModel() {
  55. Shell::Get()->system_tray_model()->calendar_model()->DebugDump();
  56. }
  57. void CycleBackwardMru() {
  58. Shell::Get()->window_cycle_controller()->HandleCycleWindow(
  59. WindowCycleController::WindowCyclingDirection::kBackward);
  60. }
  61. void FocusPip() {
  62. auto* widget = FindPipWidget();
  63. if (widget)
  64. Shell::Get()->focus_cycler()->FocusWidget(widget);
  65. }
  66. void CycleForwardMru() {
  67. Shell::Get()->window_cycle_controller()->HandleCycleWindow(
  68. WindowCycleController::WindowCyclingDirection::kForward);
  69. }
  70. void DisableCapsLock() {
  71. Shell::Get()->ime_controller()->SetCapsLockEnabled(false);
  72. }
  73. void LaunchAppN(int n) {
  74. Shelf::LaunchShelfItem(n);
  75. }
  76. void LaunchLastApp() {
  77. Shelf::LaunchShelfItem(-1);
  78. }
  79. void LockScreen() {
  80. Shell::Get()->session_controller()->LockScreen();
  81. }
  82. void MediaFastForward() {
  83. Shell::Get()->media_controller()->HandleMediaSeekForward();
  84. }
  85. void MediaNextTrack() {
  86. Shell::Get()->media_controller()->HandleMediaNextTrack();
  87. }
  88. void MediaPause() {
  89. Shell::Get()->media_controller()->HandleMediaPause();
  90. }
  91. void MediaPlay() {
  92. Shell::Get()->media_controller()->HandleMediaPlay();
  93. }
  94. void MediaPlayPause() {
  95. Shell::Get()->media_controller()->HandleMediaPlayPause();
  96. }
  97. void MediaPrevTrack() {
  98. Shell::Get()->media_controller()->HandleMediaPrevTrack();
  99. }
  100. void MediaRewind() {
  101. Shell::Get()->media_controller()->HandleMediaSeekBackward();
  102. }
  103. void MediaStop() {
  104. Shell::Get()->media_controller()->HandleMediaStop();
  105. }
  106. void MicrophoneMuteToggle() {
  107. auto* const audio_handler = CrasAudioHandler::Get();
  108. const bool mute = !audio_handler->IsInputMuted();
  109. if (mute)
  110. base::RecordAction(base::UserMetricsAction("Keyboard_Microphone_Muted"));
  111. else
  112. base::RecordAction(base::UserMetricsAction("Keyboard_Microphone_Unmuted"));
  113. audio_handler->SetInputMute(mute);
  114. }
  115. void NewIncognitoWindow() {
  116. NewWindowDelegate::GetPrimary()->NewWindow(
  117. /*is_incognito=*/true,
  118. /*should_trigger_session_restore=*/false);
  119. }
  120. void NewWindow() {
  121. NewWindowDelegate::GetPrimary()->NewWindow(
  122. /*is_incognito=*/false,
  123. /*should_trigger_session_restore=*/false);
  124. }
  125. void OpenCalculator() {
  126. NewWindowDelegate::GetInstance()->OpenCalculator();
  127. }
  128. void OpenCrosh() {
  129. NewWindowDelegate::GetInstance()->OpenCrosh();
  130. }
  131. void OpenDiagnostics() {
  132. NewWindowDelegate::GetInstance()->OpenDiagnostics();
  133. }
  134. void OpenFeedbackPage() {
  135. NewWindowDelegate::GetInstance()->OpenFeedbackPage();
  136. }
  137. void OpenFileManager() {
  138. NewWindowDelegate::GetInstance()->OpenFileManager();
  139. }
  140. void OpenHelp() {
  141. NewWindowDelegate::GetInstance()->OpenGetHelp();
  142. }
  143. void ResetDisplayZoom() {
  144. base::RecordAction(base::UserMetricsAction("Accel_Scale_Ui_Reset"));
  145. display::DisplayManager* display_manager = Shell::Get()->display_manager();
  146. gfx::Point point = display::Screen::GetScreen()->GetCursorScreenPoint();
  147. display::Display display =
  148. display::Screen::GetScreen()->GetDisplayNearestPoint(point);
  149. display_manager->ResetDisplayZoom(display.id());
  150. }
  151. void RestoreTab() {
  152. NewWindowDelegate::GetPrimary()->RestoreTab();
  153. }
  154. void ShiftPrimaryDisplay() {
  155. display::DisplayManager* display_manager = Shell::Get()->display_manager();
  156. CHECK_GE(display_manager->GetNumDisplays(), 2U);
  157. const int64_t primary_display_id =
  158. display::Screen::GetScreen()->GetPrimaryDisplay().id();
  159. const display::Displays& active_display_list =
  160. display_manager->active_display_list();
  161. auto primary_display_iter =
  162. std::find_if(active_display_list.begin(), active_display_list.end(),
  163. [id = primary_display_id](const display::Display& display) {
  164. return display.id() == id;
  165. });
  166. DCHECK(primary_display_iter != active_display_list.end());
  167. ++primary_display_iter;
  168. // If we've reach the end of |active_display_list|, wrap back around to the
  169. // front.
  170. if (primary_display_iter == active_display_list.end())
  171. primary_display_iter = active_display_list.begin();
  172. Shell::Get()->display_configuration_controller()->SetPrimaryDisplayId(
  173. primary_display_iter->id(), true /* throttle */);
  174. }
  175. void ToggleCalendar() {
  176. aura::Window* target_root = Shell::GetRootWindowForNewWindows();
  177. StatusAreaWidget* status_area_widget =
  178. RootWindowController::ForWindow(target_root)->GetStatusAreaWidget();
  179. UnifiedSystemTray* tray = status_area_widget->unified_system_tray();
  180. // If currently showing the calendar view, close it.
  181. if (tray->IsShowingCalendarView()) {
  182. tray->CloseBubble();
  183. return;
  184. }
  185. // If currently not showing the calendar view, show the bubble if needed then
  186. // show the calendar view.
  187. if (!tray->IsBubbleShown()) {
  188. // Set `DateTray` to be active prior to showing the bubble, this prevents
  189. // flashing of the status area. See crbug.com/1332603.
  190. status_area_widget->date_tray()->SetIsActive(true);
  191. tray->ShowBubble();
  192. }
  193. tray->bubble()->ShowCalendarView(
  194. calendar_metrics::CalendarViewShowSource::kAccelerator,
  195. calendar_metrics::CalendarEventSource::kKeyboard);
  196. }
  197. void ToggleFullscreen() {
  198. aura::Window* active_window = window_util::GetActiveWindow();
  199. if (!active_window)
  200. return;
  201. const WMEvent event(WM_EVENT_TOGGLE_FULLSCREEN);
  202. WindowState::Get(active_window)->OnWMEvent(&event);
  203. }
  204. void ToggleKeyboardBacklight() {
  205. KeyboardBrightnessControlDelegate* delegate =
  206. Shell::Get()->keyboard_brightness_control_delegate();
  207. delegate->HandleToggleKeyboardBacklight();
  208. }
  209. void ToggleMaximized() {
  210. aura::Window* active_window = window_util::GetActiveWindow();
  211. if (!active_window)
  212. return;
  213. base::RecordAction(base::UserMetricsAction("Accel_Toggle_Maximized"));
  214. WMEvent event(WM_EVENT_TOGGLE_MAXIMIZE);
  215. WindowState::Get(active_window)->OnWMEvent(&event);
  216. }
  217. bool ToggleMinimized() {
  218. aura::Window* window = window_util::GetActiveWindow();
  219. // Attempt to restore the window that would be cycled through next from
  220. // the launcher when there is no active window.
  221. if (!window) {
  222. // Do not unminimize a window on an inactive desk, since this will cause
  223. // desks to switch and that will be unintentional for the user.
  224. MruWindowTracker::WindowList mru_windows(
  225. Shell::Get()->mru_window_tracker()->BuildMruWindowList(kActiveDesk));
  226. if (!mru_windows.empty())
  227. WindowState::Get(mru_windows.front())->Activate();
  228. return true;
  229. }
  230. WindowState* window_state = WindowState::Get(window);
  231. if (!window_state->CanMinimize())
  232. return false;
  233. window_state->Minimize();
  234. return true;
  235. }
  236. void ToggleResizeLockMenu() {
  237. aura::Window* active_window = window_util::GetActiveWindow();
  238. auto* frame_view = ash::NonClientFrameViewAsh::Get(active_window);
  239. frame_view->GetToggleResizeLockMenuCallback().Run();
  240. }
  241. void UnpinWindow() {
  242. aura::Window* pinned_window =
  243. Shell::Get()->screen_pinning_controller()->pinned_window();
  244. if (pinned_window)
  245. WindowState::Get(pinned_window)->Restore();
  246. }
  247. bool ZoomDisplay(bool up) {
  248. if (up)
  249. base::RecordAction(base::UserMetricsAction("Accel_Scale_Ui_Up"));
  250. else
  251. base::RecordAction(base::UserMetricsAction("Accel_Scale_Ui_Down"));
  252. display::DisplayManager* display_manager = Shell::Get()->display_manager();
  253. gfx::Point point = display::Screen::GetScreen()->GetCursorScreenPoint();
  254. display::Display display =
  255. display::Screen::GetScreen()->GetDisplayNearestPoint(point);
  256. return display_manager->ZoomDisplay(display.id(), up);
  257. }
  258. } // namespace accelerators
  259. } // namespace ash