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/accelerators/accelerator_controller.h"
11 #include "ash/accelerators/accelerator_commands.h"
12 #include "ash/accelerators/accelerator_table.h"
13 #include "ash/accelerators/debug_commands.h"
14 #include "ash/ash_switches.h"
15 #include "ash/debug.h"
16 #include "ash/display/display_controller.h"
17 #include "ash/display/display_manager.h"
18 #include "ash/focus_cycler.h"
19 #include "ash/gpu_support.h"
20 #include "ash/host/ash_window_tree_host.h"
21 #include "ash/ime_control_delegate.h"
22 #include "ash/magnifier/magnification_controller.h"
23 #include "ash/magnifier/partial_magnification_controller.h"
24 #include "ash/media_delegate.h"
25 #include "ash/multi_profile_uma.h"
26 #include "ash/new_window_delegate.h"
27 #include "ash/root_window_controller.h"
28 #include "ash/rotator/screen_rotation.h"
29 #include "ash/screenshot_delegate.h"
30 #include "ash/session/session_state_delegate.h"
31 #include "ash/shelf/shelf.h"
32 #include "ash/shelf/shelf_delegate.h"
33 #include "ash/shelf/shelf_model.h"
34 #include "ash/shelf/shelf_widget.h"
35 #include "ash/shell.h"
36 #include "ash/shell_delegate.h"
37 #include "ash/shell_window_ids.h"
38 #include "ash/system/brightness_control_delegate.h"
39 #include "ash/system/keyboard_brightness/keyboard_brightness_control_delegate.h"
40 #include "ash/system/status_area_widget.h"
41 #include "ash/system/tray/system_tray.h"
42 #include "ash/system/tray/system_tray_delegate.h"
43 #include "ash/system/tray/system_tray_notifier.h"
44 #include "ash/system/web_notification/web_notification_tray.h"
45 #include "ash/touch/touch_hud_debug.h"
46 #include "ash/volume_control_delegate.h"
47 #include "ash/wm/mru_window_tracker.h"
48 #include "ash/wm/overview/window_selector_controller.h"
49 #include "ash/wm/partial_screenshot_view.h"
50 #include "ash/wm/power_button_controller.h"
51 #include "ash/wm/window_cycle_controller.h"
52 #include "ash/wm/window_state.h"
53 #include "ash/wm/window_util.h"
54 #include "ash/wm/wm_event.h"
55 #include "base/bind.h"
56 #include "base/command_line.h"
57 #include "base/metrics/user_metrics.h"
58 #include "ui/aura/env.h"
59 #include "ui/aura/window_event_dispatcher.h"
60 #include "ui/base/accelerators/accelerator.h"
61 #include "ui/base/accelerators/accelerator_manager.h"
62 #include "ui/compositor/debug_utils.h"
63 #include "ui/compositor/layer.h"
64 #include "ui/compositor/layer_animation_sequence.h"
65 #include "ui/compositor/layer_animator.h"
66 #include "ui/events/event.h"
67 #include "ui/events/keycodes/keyboard_codes.h"
68 #include "ui/gfx/screen.h"
69 #include "ui/views/controls/webview/webview.h"
70 #include "ui/views/debug_utils.h"
71 #include "ui/views/widget/widget.h"
73 #if defined(OS_CHROMEOS)
74 #include "ash/session/session_state_delegate.h"
75 #include "ash/system/chromeos/keyboard_brightness_controller.h"
76 #include "base/sys_info.h"
77 #include "chromeos/ime/ime_keyboard.h"
78 #include "chromeos/ime/input_method_manager.h"
79 #endif // defined(OS_CHROMEOS)
84 using base::UserMetricsAction
;
86 bool DebugShortcutsEnabled() {
88 return CommandLine::ForCurrentProcess()->HasSwitch(
89 switches::kAshDebugShortcuts
);
95 bool HandleAccessibleFocusCycle(bool reverse
) {
97 base::RecordAction(UserMetricsAction("Accel_Accessible_Focus_Previous"));
99 base::RecordAction(UserMetricsAction("Accel_Accessible_Focus_Next"));
102 if (!Shell::GetInstance()->accessibility_delegate()->
103 IsSpokenFeedbackEnabled()) {
106 aura::Window
* active_window
= ash::wm::GetActiveWindow();
109 views::Widget
* widget
=
110 views::Widget::GetWidgetForNativeWindow(active_window
);
113 views::FocusManager
* focus_manager
= widget
->GetFocusManager();
116 views::View
* view
= focus_manager
->GetFocusedView();
119 if (!strcmp(view
->GetClassName(), views::WebView::kViewClassName
))
122 focus_manager
->AdvanceFocus(reverse
);
126 bool HandleCycleBackwardMRU(const ui::Accelerator
& accelerator
) {
127 if (accelerator
.key_code() == ui::VKEY_TAB
)
128 base::RecordAction(base::UserMetricsAction("Accel_PrevWindow_Tab"));
130 Shell::GetInstance()->window_cycle_controller()->HandleCycleWindow(
131 WindowCycleController::BACKWARD
);
135 bool HandleCycleForwardMRU(const ui::Accelerator
& accelerator
) {
136 if (accelerator
.key_code() == ui::VKEY_TAB
)
137 base::RecordAction(base::UserMetricsAction("Accel_NextWindow_Tab"));
139 Shell::GetInstance()->window_cycle_controller()->HandleCycleWindow(
140 WindowCycleController::FORWARD
);
144 bool ToggleOverview(const ui::Accelerator
& accelerator
) {
145 base::RecordAction(base::UserMetricsAction("Accel_Overview_F5"));
146 Shell::GetInstance()->window_selector_controller()->ToggleOverview();
150 bool HandleFocusLauncher() {
151 Shell
* shell
= Shell::GetInstance();
152 base::RecordAction(base::UserMetricsAction("Accel_Focus_Launcher"));
153 return shell
->focus_cycler()->FocusWidget(
154 Shelf::ForPrimaryDisplay()->shelf_widget());
157 bool HandleLaunchAppN(int n
) {
158 base::RecordAction(UserMetricsAction("Accel_Launch_App"));
159 Shelf::ForPrimaryDisplay()->LaunchAppIndexAt(n
);
163 bool HandleLaunchLastApp() {
164 base::RecordAction(UserMetricsAction("Accel_Launch_Last_App"));
165 Shelf::ForPrimaryDisplay()->LaunchAppIndexAt(-1);
169 // Magnify the screen
170 bool HandleMagnifyScreen(int delta_index
) {
171 if (ash::Shell::GetInstance()->magnification_controller()->IsEnabled()) {
172 // TODO(yoshiki): Move the following logic to MagnificationController.
174 ash::Shell::GetInstance()->magnification_controller()->GetScale();
175 // Calculate rounded logarithm (base kMagnificationScaleFactor) of scale.
177 std::floor(std::log(scale
) / std::log(kMagnificationScaleFactor
) + 0.5);
179 int new_scale_index
= std::max(0, std::min(8, scale_index
+ delta_index
));
181 ash::Shell::GetInstance()->magnification_controller()->
182 SetScale(std::pow(kMagnificationScaleFactor
, new_scale_index
), true);
183 } else if (ash::Shell::GetInstance()->
184 partial_magnification_controller()->is_enabled()) {
185 float scale
= delta_index
> 0 ? kDefaultPartialMagnifiedScale
: 1;
186 ash::Shell::GetInstance()->partial_magnification_controller()->
193 bool HandleMediaNextTrack() {
194 Shell::GetInstance()->media_delegate()->HandleMediaNextTrack();
198 bool HandleMediaPlayPause() {
199 Shell::GetInstance()->media_delegate()->HandleMediaPlayPause();
203 bool HandleMediaPrevTrack() {
204 Shell::GetInstance()->media_delegate()->HandleMediaPrevTrack();
208 bool HandleNewIncognitoWindow() {
209 base::RecordAction(UserMetricsAction("Accel_New_Incognito_Window"));
210 bool incognito_allowed
=
211 Shell::GetInstance()->delegate()->IsIncognitoAllowed();
212 if (incognito_allowed
)
213 Shell::GetInstance()->new_window_delegate()->NewWindow(
214 true /* is_incognito */);
215 return incognito_allowed
;
218 bool HandleNewTab(ui::KeyboardCode key_code
) {
219 if (key_code
== ui::VKEY_T
)
220 base::RecordAction(base::UserMetricsAction("Accel_NewTab_T"));
221 Shell::GetInstance()->new_window_delegate()->NewTab();
225 bool HandleNewWindow() {
226 base::RecordAction(base::UserMetricsAction("Accel_New_Window"));
227 Shell::GetInstance()->new_window_delegate()->NewWindow(
228 false /* is_incognito */);
232 bool HandleNextIme(ImeControlDelegate
* ime_control_delegate
,
233 ui::EventType previous_event_type
,
234 ui::KeyboardCode previous_key_code
) {
235 // This check is necessary e.g. not to process the Shift+Alt+
236 // ET_KEY_RELEASED accelerator for Chrome OS (see ash/accelerators/
237 // accelerator_controller.cc) when Shift+Alt+Tab is pressed and then Tab
239 if (previous_event_type
== ui::ET_KEY_RELEASED
&&
240 // Workaround for crbug.com/139556. CJK IME users tend to press
241 // Enter (or Space) and Shift+Alt almost at the same time to commit
242 // an IME string and then switch from the IME to the English layout.
243 // This workaround allows the user to trigger NEXT_IME even if the
244 // user presses Shift+Alt before releasing Enter.
245 // TODO(nona|mazda): Fix crbug.com/139556 in a cleaner way.
246 previous_key_code
!= ui::VKEY_RETURN
&&
247 previous_key_code
!= ui::VKEY_SPACE
) {
248 // We totally ignore this accelerator.
249 // TODO(mazda): Fix crbug.com/158217
252 base::RecordAction(UserMetricsAction("Accel_Next_Ime"));
253 if (ime_control_delegate
)
254 return ime_control_delegate
->HandleNextIme();
258 bool HandleOpenFeedbackPage() {
259 base::RecordAction(UserMetricsAction("Accel_Open_Feedback_Page"));
260 ash::Shell::GetInstance()->new_window_delegate()->OpenFeedbackPage();
264 bool HandlePositionCenter() {
265 base::RecordAction(UserMetricsAction("Accel_Window_Position_Center"));
266 aura::Window
* window
= wm::GetActiveWindow();
267 // Docked windows do not support centering and ignore accelerator.
268 if (window
&& !wm::GetWindowState(window
)->IsDocked()) {
269 wm::CenterWindow(window
);
275 bool HandlePreviousIme(ImeControlDelegate
* ime_control_delegate
,
276 const ui::Accelerator
& accelerator
) {
277 base::RecordAction(UserMetricsAction("Accel_Previous_Ime"));
278 if (ime_control_delegate
)
279 return ime_control_delegate
->HandlePreviousIme(accelerator
);
283 bool HandleRestoreTab() {
284 base::RecordAction(base::UserMetricsAction("Accel_Restore_Tab"));
285 Shell::GetInstance()->new_window_delegate()->RestoreTab();
289 bool HandleRotatePaneFocus(Shell::Direction direction
) {
290 Shell
* shell
= Shell::GetInstance();
292 // TODO(stevet): Not sure if this is the same as IDC_FOCUS_NEXT_PANE.
293 case Shell::FORWARD
: {
294 base::RecordAction(UserMetricsAction("Accel_Focus_Next_Pane"));
295 shell
->focus_cycler()->RotateFocus(FocusCycler::FORWARD
);
298 case Shell::BACKWARD
: {
299 base::RecordAction(UserMetricsAction("Accel_Focus_Previous_Pane"));
300 shell
->focus_cycler()->RotateFocus(FocusCycler::BACKWARD
);
307 // Rotate the active window.
308 bool HandleRotateActiveWindow() {
309 base::RecordAction(UserMetricsAction("Accel_Rotate_Window"));
310 aura::Window
* active_window
= wm::GetActiveWindow();
312 // The rotation animation bases its target transform on the current
313 // rotation and position. Since there could be an animation in progress
314 // right now, queue this animation so when it starts it picks up a neutral
315 // rotation and position. Use replace so we only enqueue one at a time.
316 active_window
->layer()->GetAnimator()->
317 set_preemption_strategy(ui::LayerAnimator::REPLACE_QUEUED_ANIMATIONS
);
318 active_window
->layer()->GetAnimator()->StartAnimation(
319 new ui::LayerAnimationSequence(
320 new ash::ScreenRotation(360, active_window
->layer())));
325 gfx::Display::Rotation
GetNextRotation(gfx::Display::Rotation current
) {
327 case gfx::Display::ROTATE_0
:
328 return gfx::Display::ROTATE_90
;
329 case gfx::Display::ROTATE_90
:
330 return gfx::Display::ROTATE_180
;
331 case gfx::Display::ROTATE_180
:
332 return gfx::Display::ROTATE_270
;
333 case gfx::Display::ROTATE_270
:
334 return gfx::Display::ROTATE_0
;
336 NOTREACHED() << "Unknown rotation:" << current
;
337 return gfx::Display::ROTATE_0
;
340 // Rotates the screen.
341 bool HandleRotateScreen() {
342 base::RecordAction(UserMetricsAction("Accel_Rotate_Window"));
343 gfx::Point point
= Shell::GetScreen()->GetCursorScreenPoint();
344 gfx::Display display
= Shell::GetScreen()->GetDisplayNearestPoint(point
);
345 const DisplayInfo
& display_info
=
346 Shell::GetInstance()->display_manager()->GetDisplayInfo(display
.id());
347 Shell::GetInstance()->display_manager()->SetDisplayRotation(
348 display
.id(), GetNextRotation(display_info
.rotation()));
352 bool HandleScaleReset() {
353 DisplayManager
* display_manager
= Shell::GetInstance()->display_manager();
354 int64 display_id
= display_manager
->GetDisplayIdForUIScaling();
355 if (display_id
== gfx::Display::kInvalidDisplayID
)
358 base::RecordAction(UserMetricsAction("Accel_Scale_Ui_Reset"));
360 display_manager
->SetDisplayUIScale(display_id
, 1.0f
);
364 bool HandleScaleUI(bool up
) {
365 DisplayManager
* display_manager
= Shell::GetInstance()->display_manager();
366 int64 display_id
= display_manager
->GetDisplayIdForUIScaling();
367 if (display_id
== gfx::Display::kInvalidDisplayID
)
371 base::RecordAction(UserMetricsAction("Accel_Scale_Ui_Up"));
373 base::RecordAction(UserMetricsAction("Accel_Scale_Ui_Down"));
376 const DisplayInfo
& display_info
= display_manager
->GetDisplayInfo(display_id
);
377 float next_scale
= DisplayManager::GetNextUIScale(display_info
, up
);
378 display_manager
->SetDisplayUIScale(display_id
, next_scale
);
382 #if defined(OS_CHROMEOS)
383 bool HandleSwapPrimaryDisplay() {
384 base::RecordAction(UserMetricsAction("Accel_Swap_Primary_Display"));
385 Shell::GetInstance()->display_controller()->SwapPrimaryDisplay();
390 bool HandleShowKeyboardOverlay() {
391 base::RecordAction(UserMetricsAction("Accel_Show_Keyboard_Overlay"));
392 ash::Shell::GetInstance()->new_window_delegate()->ShowKeyboardOverlay();
397 void HandleShowMessageCenterBubble() {
398 base::RecordAction(UserMetricsAction("Accel_Show_Message_Center_Bubble"));
399 RootWindowController
* controller
=
400 RootWindowController::ForTargetRootWindow();
401 StatusAreaWidget
* status_area_widget
=
402 controller
->shelf()->status_area_widget();
403 if (status_area_widget
) {
404 WebNotificationTray
* notification_tray
=
405 status_area_widget
->web_notification_tray();
406 if (notification_tray
->visible())
407 notification_tray
->ShowMessageCenterBubble();
411 bool HandleShowSystemTrayBubble() {
412 base::RecordAction(UserMetricsAction("Accel_Show_System_Tray_Bubble"));
413 RootWindowController
* controller
=
414 RootWindowController::ForTargetRootWindow();
415 if (!controller
->GetSystemTray()->HasSystemBubble()) {
416 controller
->GetSystemTray()->ShowDefaultView(BUBBLE_CREATE_NEW
);
422 bool HandleShowTaskManager() {
423 base::RecordAction(UserMetricsAction("Accel_Show_Task_Manager"));
424 Shell::GetInstance()->new_window_delegate()->ShowTaskManager();
428 #if defined(OS_CHROMEOS)
429 void HandleSilenceSpokenFeedback() {
430 base::RecordAction(UserMetricsAction("Accel_Silence_Spoken_Feedback"));
432 AccessibilityDelegate
* delegate
=
433 Shell::GetInstance()->accessibility_delegate();
434 if (!delegate
->IsSpokenFeedbackEnabled())
436 delegate
->SilenceSpokenFeedback();
440 bool HandleSwitchIme(ImeControlDelegate
* ime_control_delegate
,
441 const ui::Accelerator
& accelerator
) {
442 base::RecordAction(UserMetricsAction("Accel_Switch_Ime"));
443 if (ime_control_delegate
)
444 return ime_control_delegate
->HandleSwitchIme(accelerator
);
448 bool HandleTakePartialScreenshot(ScreenshotDelegate
* screenshot_delegate
) {
449 base::RecordAction(UserMetricsAction("Accel_Take_Partial_Screenshot"));
450 if (screenshot_delegate
) {
451 ash::PartialScreenshotView::StartPartialScreenshot(
452 screenshot_delegate
);
454 // Return true to prevent propagation of the key event because
455 // this key combination is reserved for partial screenshot.
459 bool HandleTakeScreenshot(ScreenshotDelegate
* screenshot_delegate
) {
460 base::RecordAction(UserMetricsAction("Accel_Take_Screenshot"));
461 if (screenshot_delegate
&&
462 screenshot_delegate
->CanTakeScreenshot()) {
463 screenshot_delegate
->HandleTakeScreenshotForAllRootWindows();
465 // Return true to prevent propagation of the key event.
469 bool HandleToggleAppList(ui::KeyboardCode key_code
,
470 ui::EventType previous_event_type
,
471 ui::KeyboardCode previous_key_code
,
472 const ui::Accelerator
& accelerator
) {
473 // If something else was pressed between the Search key (LWIN)
474 // being pressed and released, then ignore the release of the
476 if (key_code
== ui::VKEY_LWIN
&&
477 (previous_event_type
== ui::ET_KEY_RELEASED
||
478 previous_key_code
!= ui::VKEY_LWIN
))
480 if (key_code
== ui::VKEY_LWIN
)
481 base::RecordAction(base::UserMetricsAction("Accel_Search_LWin"));
482 // When spoken feedback is enabled, we should neither toggle the list nor
483 // consume the key since Search+Shift is one of the shortcuts the a11y
484 // feature uses. crbug.com/132296
485 DCHECK_EQ(ui::VKEY_LWIN
, accelerator
.key_code());
486 if (Shell::GetInstance()->accessibility_delegate()->
487 IsSpokenFeedbackEnabled())
489 ash::Shell::GetInstance()->ToggleAppList(NULL
);
493 bool HandleToggleFullscreen(ui::KeyboardCode key_code
) {
494 if (key_code
== ui::VKEY_MEDIA_LAUNCH_APP2
) {
495 base::RecordAction(UserMetricsAction("Accel_Fullscreen_F4"));
497 accelerators::ToggleFullscreen();
501 bool HandleToggleRootWindowFullScreen() {
502 Shell::GetPrimaryRootWindowController()->ash_host()->ToggleFullScreen();
506 bool HandleWindowSnap(int action
) {
507 wm::WindowState
* window_state
= wm::GetActiveWindowState();
508 // Disable window snapping shortcut key for full screen window due to
509 // http://crbug.com/135487.
511 window_state
->window()->type() != ui::wm::WINDOW_TYPE_NORMAL
||
512 window_state
->IsFullscreen()) {
516 if (action
== WINDOW_SNAP_LEFT
) {
517 base::RecordAction(UserMetricsAction("Accel_Window_Snap_Left"));
519 base::RecordAction(UserMetricsAction("Accel_Window_Snap_Right"));
521 const wm::WMEvent
event(action
== WINDOW_SNAP_LEFT
?
522 wm::WM_EVENT_SNAP_LEFT
: wm::WM_EVENT_SNAP_RIGHT
);
523 window_state
->OnWMEvent(&event
);
527 bool HandleWindowMinimize() {
529 base::UserMetricsAction("Accel_Toggle_Minimized_Minus"));
530 return accelerators::ToggleMinimized();
533 #if defined(OS_CHROMEOS)
534 bool HandleAddRemoveDisplay() {
535 base::RecordAction(UserMetricsAction("Accel_Add_Remove_Display"));
536 Shell::GetInstance()->display_manager()->AddRemoveDisplay();
541 base::RecordAction(UserMetricsAction("Accel_Open_Crosh"));
543 Shell::GetInstance()->new_window_delegate()->OpenCrosh();
547 bool HandleFileManager() {
548 base::RecordAction(UserMetricsAction("Accel_Open_File_Manager"));
550 Shell::GetInstance()->new_window_delegate()->OpenFileManager();
554 bool HandleLock(ui::KeyboardCode key_code
) {
555 base::RecordAction(UserMetricsAction("Accel_LockScreen_L"));
556 Shell::GetInstance()->session_state_delegate()->LockScreen();
560 bool HandleCycleUser(SessionStateDelegate::CycleUser cycle_user
) {
561 if (!Shell::GetInstance()->delegate()->IsMultiProfilesEnabled())
563 ash::SessionStateDelegate
* delegate
=
564 ash::Shell::GetInstance()->session_state_delegate();
565 if (delegate
->NumberOfLoggedInUsers() <= 1)
567 MultiProfileUMA::RecordSwitchActiveUser(
568 MultiProfileUMA::SWITCH_ACTIVE_USER_BY_ACCELERATOR
);
569 switch (cycle_user
) {
570 case SessionStateDelegate::CYCLE_TO_NEXT_USER
:
571 base::RecordAction(UserMetricsAction("Accel_Switch_To_Next_User"));
573 case SessionStateDelegate::CYCLE_TO_PREVIOUS_USER
:
574 base::RecordAction(UserMetricsAction("Accel_Switch_To_Previous_User"));
577 delegate
->CycleActiveUser(cycle_user
);
581 bool HandleToggleMirrorMode() {
582 base::RecordAction(UserMetricsAction("Accel_Toggle_Mirror_Mode"));
583 Shell::GetInstance()->display_controller()->ToggleMirrorMode();
587 bool HandleToggleSpokenFeedback() {
588 base::RecordAction(UserMetricsAction("Accel_Toggle_Spoken_Feedback"));
590 Shell::GetInstance()->accessibility_delegate()->
591 ToggleSpokenFeedback(A11Y_NOTIFICATION_SHOW
);
595 bool HandleToggleTouchViewTesting() {
596 // TODO(skuhne): This is only temporary! Remove this!
597 if (CommandLine::ForCurrentProcess()->HasSwitch(
598 switches::kAshEnableTouchViewTesting
)) {
599 Shell
* shell
= Shell::GetInstance();
600 shell
->EnableMaximizeModeWindowManager(
601 !shell
->IsMaximizeModeWindowManagerEnabled());
607 bool HandleTouchHudClear() {
608 RootWindowController
* controller
=
609 RootWindowController::ForTargetRootWindow();
610 if (controller
->touch_hud_debug()) {
611 controller
->touch_hud_debug()->Clear();
617 bool HandleTouchHudModeChange() {
618 RootWindowController
* controller
=
619 RootWindowController::ForTargetRootWindow();
620 if (controller
->touch_hud_debug()) {
621 controller
->touch_hud_debug()->ChangeToNextMode();
627 bool HandleTouchHudProjectToggle() {
628 base::RecordAction(UserMetricsAction("Accel_Touch_Hud_Clear"));
629 bool enabled
= Shell::GetInstance()->is_touch_hud_projection_enabled();
630 Shell::GetInstance()->SetTouchHudProjectionEnabled(!enabled
);
634 bool HandleDisableCapsLock(ui::KeyboardCode key_code
,
635 ui::EventType previous_event_type
,
636 ui::KeyboardCode previous_key_code
) {
637 if (previous_event_type
== ui::ET_KEY_RELEASED
||
638 (previous_key_code
!= ui::VKEY_LSHIFT
&&
639 previous_key_code
!= ui::VKEY_SHIFT
&&
640 previous_key_code
!= ui::VKEY_RSHIFT
)) {
641 // If something else was pressed between the Shift key being pressed
642 // and released, then ignore the release of the Shift key.
645 base::RecordAction(UserMetricsAction("Accel_Disable_Caps_Lock"));
646 chromeos::input_method::InputMethodManager
* ime
=
647 chromeos::input_method::InputMethodManager::Get();
648 chromeos::input_method::ImeKeyboard
* keyboard
=
649 ime
? ime
->GetImeKeyboard() : NULL
;
650 if (keyboard
&& keyboard
->CapsLockIsEnabled()) {
651 keyboard
->SetCapsLockEnabled(false);
657 bool HandleToggleCapsLock(ui::KeyboardCode key_code
,
658 ui::EventType previous_event_type
,
659 ui::KeyboardCode previous_key_code
) {
660 if (key_code
== ui::VKEY_LWIN
) {
661 // If something else was pressed between the Search key (LWIN)
662 // being pressed and released, then ignore the release of the
664 // TODO(danakj): Releasing Alt first breaks this: crbug.com/166495
665 if (previous_event_type
== ui::ET_KEY_RELEASED
||
666 previous_key_code
!= ui::VKEY_LWIN
)
669 base::RecordAction(UserMetricsAction("Accel_Toggle_Caps_Lock"));
670 chromeos::input_method::InputMethodManager
* ime
=
671 chromeos::input_method::InputMethodManager::Get();
672 chromeos::input_method::ImeKeyboard
* keyboard
=
673 ime
? ime
->GetImeKeyboard() : NULL
;
675 keyboard
->SetCapsLockEnabled(!keyboard
->CapsLockIsEnabled());
679 #endif // defined(OS_CHROMEOS)
681 // Debug print methods.
683 bool HandlePrintLayerHierarchy() {
684 aura::Window::Windows root_windows
= Shell::GetAllRootWindows();
685 for (size_t i
= 0; i
< root_windows
.size(); ++i
) {
686 ui::PrintLayerHierarchy(
687 root_windows
[i
]->layer(),
688 root_windows
[i
]->GetHost()->dispatcher()->GetLastMouseLocationInRoot());
693 bool HandlePrintViewHierarchy() {
694 aura::Window
* active_window
= ash::wm::GetActiveWindow();
697 views::Widget
* browser_widget
=
698 views::Widget::GetWidgetForNativeWindow(active_window
);
701 views::PrintViewHierarchy(browser_widget
->GetRootView());
705 void PrintWindowHierarchy(aura::Window
* window
,
707 std::ostringstream
* out
) {
708 std::string
indent_str(indent
, ' ');
709 std::string
name(window
->name());
712 *out
<< indent_str
<< name
<< " (" << window
<< ")"
713 << " type=" << window
->type()
714 << (wm::IsActiveWindow(window
) ? " [active] " : " ")
715 << (window
->IsVisible() ? " visible " : " ")
716 << window
->bounds().ToString()
719 for (size_t i
= 0; i
< window
->children().size(); ++i
)
720 PrintWindowHierarchy(window
->children()[i
], indent
+ 3, out
);
723 bool HandlePrintWindowHierarchy() {
724 Shell::RootWindowControllerList controllers
=
725 Shell::GetAllRootWindowControllers();
726 for (size_t i
= 0; i
< controllers
.size(); ++i
) {
727 std::ostringstream out
;
728 out
<< "RootWindow " << i
<< ":\n";
729 PrintWindowHierarchy(controllers
[i
]->GetRootWindow(), 0, &out
);
730 // Error so logs can be collected from end-users.
731 LOG(ERROR
) << out
.str();
736 bool HandlePrintUIHierarchies() {
737 // This is a separate command so the user only has to hit one key to generate
738 // all the logs. Developers use the individual dumps repeatedly, so keep
739 // those as separate commands to avoid spamming their logs.
740 HandlePrintLayerHierarchy();
741 HandlePrintWindowHierarchy();
742 HandlePrintViewHierarchy();
748 ////////////////////////////////////////////////////////////////////////////////
749 // AcceleratorControllerContext, public:
751 AcceleratorControllerContext::AcceleratorControllerContext() {
752 current_accelerator_
.set_type(ui::ET_UNKNOWN
);
753 previous_accelerator_
.set_type(ui::ET_UNKNOWN
);
756 void AcceleratorControllerContext::UpdateContext(
757 const ui::Accelerator
& accelerator
) {
758 previous_accelerator_
= current_accelerator_
;
759 current_accelerator_
= accelerator
;
762 ////////////////////////////////////////////////////////////////////////////////
763 // AcceleratorController, public:
765 AcceleratorController::AcceleratorController()
766 : accelerator_manager_(new ui::AcceleratorManager
) {
770 AcceleratorController::~AcceleratorController() {
773 void AcceleratorController::Init() {
774 for (size_t i
= 0; i
< kActionsAllowedAtLoginOrLockScreenLength
; ++i
) {
775 actions_allowed_at_login_screen_
.insert(
776 kActionsAllowedAtLoginOrLockScreen
[i
]);
777 actions_allowed_at_lock_screen_
.insert(
778 kActionsAllowedAtLoginOrLockScreen
[i
]);
780 for (size_t i
= 0; i
< kActionsAllowedAtLockScreenLength
; ++i
)
781 actions_allowed_at_lock_screen_
.insert(kActionsAllowedAtLockScreen
[i
]);
782 for (size_t i
= 0; i
< kActionsAllowedAtModalWindowLength
; ++i
)
783 actions_allowed_at_modal_window_
.insert(kActionsAllowedAtModalWindow
[i
]);
784 for (size_t i
= 0; i
< kReservedActionsLength
; ++i
)
785 reserved_actions_
.insert(kReservedActions
[i
]);
786 for (size_t i
= 0; i
< kNonrepeatableActionsLength
; ++i
)
787 nonrepeatable_actions_
.insert(kNonrepeatableActions
[i
]);
788 for (size_t i
= 0; i
< kActionsAllowedInAppModeLength
; ++i
)
789 actions_allowed_in_app_mode_
.insert(kActionsAllowedInAppMode
[i
]);
790 for (size_t i
= 0; i
< kActionsNeedingWindowLength
; ++i
)
791 actions_needing_window_
.insert(kActionsNeedingWindow
[i
]);
793 RegisterAccelerators(kAcceleratorData
, kAcceleratorDataLength
);
796 RegisterAccelerators(kDesktopAcceleratorData
, kDesktopAcceleratorDataLength
);
799 if (DebugShortcutsEnabled()) {
800 RegisterAccelerators(kDebugAcceleratorData
, kDebugAcceleratorDataLength
);
801 for (size_t i
= 0; i
< kReservedDebugActionsLength
; ++i
)
802 reserved_actions_
.insert(kReservedDebugActions
[i
]);
805 #if defined(OS_CHROMEOS)
806 keyboard_brightness_control_delegate_
.reset(
807 new KeyboardBrightnessController());
811 void AcceleratorController::Register(const ui::Accelerator
& accelerator
,
812 ui::AcceleratorTarget
* target
) {
813 accelerator_manager_
->Register(accelerator
,
814 ui::AcceleratorManager::kNormalPriority
,
818 void AcceleratorController::Unregister(const ui::Accelerator
& accelerator
,
819 ui::AcceleratorTarget
* target
) {
820 accelerator_manager_
->Unregister(accelerator
, target
);
823 void AcceleratorController::UnregisterAll(ui::AcceleratorTarget
* target
) {
824 accelerator_manager_
->UnregisterAll(target
);
827 bool AcceleratorController::Process(const ui::Accelerator
& accelerator
) {
828 if (ime_control_delegate_
) {
829 return accelerator_manager_
->Process(
830 ime_control_delegate_
->RemapAccelerator(accelerator
));
832 return accelerator_manager_
->Process(accelerator
);
835 bool AcceleratorController::IsRegistered(
836 const ui::Accelerator
& accelerator
) const {
837 return accelerator_manager_
->GetCurrentTarget(accelerator
) != NULL
;
840 bool AcceleratorController::IsReservedAccelerator(
841 const ui::Accelerator
& accelerator
) const {
842 const ui::Accelerator remapped_accelerator
= ime_control_delegate_
.get() ?
843 ime_control_delegate_
->RemapAccelerator(accelerator
) : accelerator
;
845 std::map
<ui::Accelerator
, int>::const_iterator iter
=
846 accelerators_
.find(remapped_accelerator
);
847 if (iter
== accelerators_
.end())
848 return false; // not an accelerator.
850 return reserved_actions_
.find(iter
->second
) != reserved_actions_
.end();
853 bool AcceleratorController::PerformAction(int action
,
854 const ui::Accelerator
& accelerator
) {
855 ash::Shell
* shell
= ash::Shell::GetInstance();
856 if (!shell
->session_state_delegate()->IsActiveUserSessionStarted() &&
857 actions_allowed_at_login_screen_
.find(action
) ==
858 actions_allowed_at_login_screen_
.end()) {
861 if (shell
->session_state_delegate()->IsScreenLocked() &&
862 actions_allowed_at_lock_screen_
.find(action
) ==
863 actions_allowed_at_lock_screen_
.end()) {
866 if (shell
->IsSystemModalWindowOpen() &&
867 actions_allowed_at_modal_window_
.find(action
) ==
868 actions_allowed_at_modal_window_
.end()) {
869 // Note: we return true. This indicates the shortcut is handled
870 // and will not be passed to the modal window. This is important
871 // for things like Alt+Tab that would cause an undesired effect
872 // in the modal window by cycling through its window elements.
875 if (shell
->delegate()->IsRunningInForcedAppMode() &&
876 actions_allowed_in_app_mode_
.find(action
) ==
877 actions_allowed_in_app_mode_
.end()) {
880 if (MruWindowTracker::BuildWindowList(false).empty() &&
881 actions_needing_window_
.find(action
) != actions_needing_window_
.end()) {
882 Shell::GetInstance()->accessibility_delegate()->TriggerAccessibilityAlert(
883 A11Y_ALERT_WINDOW_NEEDED
);
887 const ui::KeyboardCode key_code
= accelerator
.key_code();
888 // PerformAction() is performed from gesture controllers and passes
889 // empty Accelerator() instance as the second argument. Such events
890 // should never be suspended.
891 const bool gesture_event
= key_code
== ui::VKEY_UNKNOWN
;
893 // Ignore accelerators invoked as repeated (while holding a key for a long
894 // time, if their handling is nonrepeatable.
895 if (nonrepeatable_actions_
.find(action
) != nonrepeatable_actions_
.end() &&
896 context_
.repeated() && !gesture_event
) {
899 // Type of the previous accelerator. Used by NEXT_IME and DISABLE_CAPS_LOCK.
900 const ui::EventType previous_event_type
=
901 context_
.previous_accelerator().type();
902 const ui::KeyboardCode previous_key_code
=
903 context_
.previous_accelerator().key_code();
905 // You *MUST* return true when some action is performed. Otherwise, this
906 // function might be called *twice*, via BrowserView::PreHandleKeyboardEvent
907 // and BrowserView::HandleKeyboardEvent, for a single accelerator press.
909 // If your accelerator invokes more than one line of code, please either
910 // implement it in your module's controller code (like TOGGLE_MIRROR_MODE
911 // below) or pull it into a HandleFoo() function above.
913 case ACCESSIBLE_FOCUS_NEXT
:
914 return HandleAccessibleFocusCycle(false);
915 case ACCESSIBLE_FOCUS_PREVIOUS
:
916 return HandleAccessibleFocusCycle(true);
917 case CYCLE_BACKWARD_MRU
:
918 return HandleCycleBackwardMRU(accelerator
);
919 case CYCLE_FORWARD_MRU
:
920 return HandleCycleForwardMRU(accelerator
);
921 case TOGGLE_OVERVIEW
:
922 return ToggleOverview(accelerator
);
923 #if defined(OS_CHROMEOS)
924 case ADD_REMOVE_DISPLAY
:
925 return HandleAddRemoveDisplay();
926 case TOGGLE_MIRROR_MODE
:
927 return HandleToggleMirrorMode();
929 return HandleLock(key_code
);
930 case OPEN_FILE_MANAGER
:
931 return HandleFileManager();
933 return HandleCrosh();
934 case SILENCE_SPOKEN_FEEDBACK
:
935 HandleSilenceSpokenFeedback();
937 case SWAP_PRIMARY_DISPLAY
:
938 return HandleSwapPrimaryDisplay();
939 case SWITCH_TO_NEXT_USER
:
940 return HandleCycleUser(SessionStateDelegate::CYCLE_TO_NEXT_USER
);
941 case SWITCH_TO_PREVIOUS_USER
:
942 return HandleCycleUser(SessionStateDelegate::CYCLE_TO_PREVIOUS_USER
);
943 case TOGGLE_SPOKEN_FEEDBACK
:
944 return HandleToggleSpokenFeedback();
945 case TOGGLE_TOUCH_VIEW_TESTING
:
946 return HandleToggleTouchViewTesting();
948 Shell::GetInstance()->system_tray_notifier()->NotifyRequestToggleWifi();
950 case TOUCH_HUD_CLEAR
:
951 return HandleTouchHudClear();
952 case TOUCH_HUD_MODE_CHANGE
:
953 return HandleTouchHudModeChange();
954 case TOUCH_HUD_PROJECTION_TOGGLE
:
955 return HandleTouchHudProjectToggle();
956 case DISABLE_GPU_WATCHDOG
:
957 Shell::GetInstance()->gpu_support()->DisableGpuWatchdog();
959 case DISABLE_CAPS_LOCK
:
960 return HandleDisableCapsLock(
961 key_code
, previous_event_type
, previous_key_code
);
962 case TOGGLE_CAPS_LOCK
:
963 return HandleToggleCapsLock(
964 key_code
, previous_event_type
, previous_key_code
);
965 #endif // OS_CHROMEOS
966 case OPEN_FEEDBACK_PAGE
:
967 return HandleOpenFeedbackPage();
969 // UMA metrics are recorded in the handler.
970 exit_warning_handler_
.HandleAccelerator();
972 case NEW_INCOGNITO_WINDOW
:
973 return HandleNewIncognitoWindow();
975 return HandleNewTab(key_code
);
977 return HandleNewWindow();
979 return HandleRestoreTab();
980 case TAKE_SCREENSHOT
:
981 return HandleTakeScreenshot(screenshot_delegate_
.get());
982 case TAKE_PARTIAL_SCREENSHOT
:
983 return HandleTakePartialScreenshot(screenshot_delegate_
.get());
984 case TOGGLE_APP_LIST
:
985 return HandleToggleAppList(
986 key_code
, previous_event_type
, previous_key_code
, accelerator
);
987 case BRIGHTNESS_DOWN
:
988 if (brightness_control_delegate_
)
989 return brightness_control_delegate_
->HandleBrightnessDown(accelerator
);
992 if (brightness_control_delegate_
)
993 return brightness_control_delegate_
->HandleBrightnessUp(accelerator
);
995 case KEYBOARD_BRIGHTNESS_DOWN
:
996 if (keyboard_brightness_control_delegate_
)
997 return keyboard_brightness_control_delegate_
->
998 HandleKeyboardBrightnessDown(accelerator
);
1000 case KEYBOARD_BRIGHTNESS_UP
:
1001 if (keyboard_brightness_control_delegate_
)
1002 return keyboard_brightness_control_delegate_
->
1003 HandleKeyboardBrightnessUp(accelerator
);
1006 ash::VolumeControlDelegate
* volume_delegate
=
1007 shell
->system_tray_delegate()->GetVolumeControlDelegate();
1008 return volume_delegate
&& volume_delegate
->HandleVolumeMute(accelerator
);
1011 ash::VolumeControlDelegate
* volume_delegate
=
1012 shell
->system_tray_delegate()->GetVolumeControlDelegate();
1013 return volume_delegate
&& volume_delegate
->HandleVolumeDown(accelerator
);
1016 ash::VolumeControlDelegate
* volume_delegate
=
1017 shell
->system_tray_delegate()->GetVolumeControlDelegate();
1018 return volume_delegate
&& volume_delegate
->HandleVolumeUp(accelerator
);
1020 case FOCUS_LAUNCHER
:
1021 return HandleFocusLauncher();
1022 case FOCUS_NEXT_PANE
:
1023 return HandleRotatePaneFocus(Shell::FORWARD
);
1024 case FOCUS_PREVIOUS_PANE
:
1025 return HandleRotatePaneFocus(Shell::BACKWARD
);
1026 case SHOW_KEYBOARD_OVERLAY
:
1027 return HandleShowKeyboardOverlay();
1028 case SHOW_SYSTEM_TRAY_BUBBLE
:
1029 return HandleShowSystemTrayBubble();
1030 case SHOW_MESSAGE_CENTER_BUBBLE
:
1031 HandleShowMessageCenterBubble();
1033 case SHOW_TASK_MANAGER
:
1034 return HandleShowTaskManager();
1036 return HandleNextIme(
1037 ime_control_delegate_
.get(), previous_event_type
, previous_key_code
);
1039 return HandlePreviousIme(ime_control_delegate_
.get(), accelerator
);
1040 case PRINT_UI_HIERARCHIES
:
1041 return HandlePrintUIHierarchies();
1043 return HandleSwitchIme(ime_control_delegate_
.get(), accelerator
);
1045 return HandleLaunchAppN(0);
1047 return HandleLaunchAppN(1);
1049 return HandleLaunchAppN(2);
1051 return HandleLaunchAppN(3);
1053 return HandleLaunchAppN(4);
1055 return HandleLaunchAppN(5);
1057 return HandleLaunchAppN(6);
1059 return HandleLaunchAppN(7);
1060 case LAUNCH_LAST_APP
:
1061 return HandleLaunchLastApp();
1062 case WINDOW_SNAP_LEFT
:
1063 case WINDOW_SNAP_RIGHT
:
1064 return HandleWindowSnap(action
);
1065 case WINDOW_MINIMIZE
:
1066 return HandleWindowMinimize();
1067 case TOGGLE_FULLSCREEN
:
1068 return HandleToggleFullscreen(key_code
);
1069 case TOGGLE_MAXIMIZED
:
1070 accelerators::ToggleMaximized();
1072 case WINDOW_POSITION_CENTER
:
1073 return HandlePositionCenter();
1075 return HandleScaleUI(true /* up */);
1077 return HandleScaleUI(false /* down */);
1078 case SCALE_UI_RESET
:
1079 return HandleScaleReset();
1081 return HandleRotateActiveWindow();
1083 return HandleRotateScreen();
1084 case TOGGLE_DESKTOP_BACKGROUND_MODE
:
1085 return debug::CycleDesktopBackgroundMode();
1086 case TOGGLE_ROOT_WINDOW_FULL_SCREEN
:
1087 return HandleToggleRootWindowFullScreen();
1088 case DEBUG_TOGGLE_DEVICE_SCALE_FACTOR
:
1089 Shell::GetInstance()->display_manager()->ToggleDisplayScaleFactor();
1091 case DEBUG_TOGGLE_SHOW_DEBUG_BORDERS
:
1092 ash::debug::ToggleShowDebugBorders();
1094 case DEBUG_TOGGLE_SHOW_FPS_COUNTER
:
1095 ash::debug::ToggleShowFpsCounter();
1097 case DEBUG_TOGGLE_SHOW_PAINT_RECTS
:
1098 ash::debug::ToggleShowPaintRects();
1100 case MAGNIFY_SCREEN_ZOOM_IN
:
1101 return HandleMagnifyScreen(1);
1102 case MAGNIFY_SCREEN_ZOOM_OUT
:
1103 return HandleMagnifyScreen(-1);
1104 case MEDIA_NEXT_TRACK
:
1105 return HandleMediaNextTrack();
1106 case MEDIA_PLAY_PAUSE
:
1107 return HandleMediaPlayPause();
1108 case MEDIA_PREV_TRACK
:
1109 return HandleMediaPrevTrack();
1110 case POWER_PRESSED
: // fallthrough
1111 case POWER_RELEASED
:
1112 #if defined(OS_CHROMEOS)
1113 if (!base::SysInfo::IsRunningOnChromeOS()) {
1114 // There is no powerd in linux desktop, so call the
1115 // PowerButtonController here.
1116 Shell::GetInstance()->power_button_controller()->
1117 OnPowerButtonEvent(action
== POWER_PRESSED
, base::TimeTicks());
1120 // We don't do anything with these at present on the device,
1121 // (power button events are reported to us from powerm via
1122 // D-BUS), but we consume them to prevent them from getting
1123 // passed to apps -- see http://crbug.com/146609.
1127 Shell::GetInstance()->power_button_controller()->
1128 OnLockButtonEvent(action
== LOCK_PRESSED
, base::TimeTicks());
1130 case PRINT_LAYER_HIERARCHY
:
1131 return HandlePrintLayerHierarchy();
1132 case PRINT_VIEW_HIERARCHY
:
1133 return HandlePrintViewHierarchy();
1134 case PRINT_WINDOW_HIERARCHY
:
1135 return HandlePrintWindowHierarchy();
1137 NOTREACHED() << "Unhandled action " << action
;
1142 void AcceleratorController::SetBrightnessControlDelegate(
1143 scoped_ptr
<BrightnessControlDelegate
> brightness_control_delegate
) {
1144 brightness_control_delegate_
= brightness_control_delegate
.Pass();
1147 void AcceleratorController::SetImeControlDelegate(
1148 scoped_ptr
<ImeControlDelegate
> ime_control_delegate
) {
1149 ime_control_delegate_
= ime_control_delegate
.Pass();
1152 void AcceleratorController::SetScreenshotDelegate(
1153 scoped_ptr
<ScreenshotDelegate
> screenshot_delegate
) {
1154 screenshot_delegate_
= screenshot_delegate
.Pass();
1157 ////////////////////////////////////////////////////////////////////////////////
1158 // AcceleratorController, ui::AcceleratorTarget implementation:
1160 bool AcceleratorController::AcceleratorPressed(
1161 const ui::Accelerator
& accelerator
) {
1162 std::map
<ui::Accelerator
, int>::const_iterator it
=
1163 accelerators_
.find(accelerator
);
1164 DCHECK(it
!= accelerators_
.end());
1165 return PerformAction(static_cast<AcceleratorAction
>(it
->second
), accelerator
);
1168 void AcceleratorController::RegisterAccelerators(
1169 const AcceleratorData accelerators
[],
1170 size_t accelerators_length
) {
1171 for (size_t i
= 0; i
< accelerators_length
; ++i
) {
1172 ui::Accelerator
accelerator(accelerators
[i
].keycode
,
1173 accelerators
[i
].modifiers
);
1174 accelerator
.set_type(accelerators
[i
].trigger_on_press
?
1175 ui::ET_KEY_PRESSED
: ui::ET_KEY_RELEASED
);
1176 Register(accelerator
, this);
1177 accelerators_
.insert(
1178 std::make_pair(accelerator
, accelerators
[i
].action
));
1182 void AcceleratorController::SetKeyboardBrightnessControlDelegate(
1183 scoped_ptr
<KeyboardBrightnessControlDelegate
>
1184 keyboard_brightness_control_delegate
) {
1185 keyboard_brightness_control_delegate_
=
1186 keyboard_brightness_control_delegate
.Pass();
1189 bool AcceleratorController::CanHandleAccelerators() const {