1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/accelerators/accelerator_controller.h"
#include <algorithm>
#include <cmath>
#include <string>
#include "ash/accelerators/accelerator_commands.h"
#include "ash/accelerators/accelerator_table.h"
#include "ash/accelerators/debug_commands.h"
#include "ash/ash_switches.h"
#include "ash/debug.h"
#include "ash/display/display_controller.h"
#include "ash/display/display_manager.h"
#include "ash/focus_cycler.h"
#include "ash/gpu_support.h"
#include "ash/ime_control_delegate.h"
#include "ash/magnifier/magnification_controller.h"
#include "ash/magnifier/partial_magnification_controller.h"
#include "ash/media_delegate.h"
#include "ash/multi_profile_uma.h"
#include "ash/new_window_delegate.h"
#include "ash/root_window_controller.h"
#include "ash/rotator/screen_rotation.h"
#include "ash/screenshot_delegate.h"
#include "ash/session/session_state_delegate.h"
#include "ash/shelf/shelf.h"
#include "ash/shelf/shelf_delegate.h"
#include "ash/shelf/shelf_model.h"
#include "ash/shelf/shelf_widget.h"
#include "ash/shell.h"
#include "ash/shell_delegate.h"
#include "ash/shell_window_ids.h"
#include "ash/system/brightness_control_delegate.h"
#include "ash/system/keyboard_brightness/keyboard_brightness_control_delegate.h"
#include "ash/system/status_area_widget.h"
#include "ash/system/tray/system_tray.h"
#include "ash/system/tray/system_tray_delegate.h"
#include "ash/system/tray/system_tray_notifier.h"
#include "ash/system/web_notification/web_notification_tray.h"
#include "ash/touch/touch_hud_debug.h"
#include "ash/volume_control_delegate.h"
#include "ash/wm/maximize_mode/maximize_mode_controller.h"
#include "ash/wm/mru_window_tracker.h"
#include "ash/wm/overview/window_selector_controller.h"
#include "ash/wm/partial_screenshot_view.h"
#include "ash/wm/power_button_controller.h"
#include "ash/wm/window_cycle_controller.h"
#include "ash/wm/window_state.h"
#include "ash/wm/window_util.h"
#include "ash/wm/wm_event.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/metrics/user_metrics.h"
#include "ui/aura/env.h"
#include "ui/base/accelerators/accelerator.h"
#include "ui/base/accelerators/accelerator_manager.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/layer_animation_sequence.h"
#include "ui/compositor/layer_animator.h"
#include "ui/events/event.h"
#include "ui/events/keycodes/keyboard_codes.h"
#include "ui/gfx/screen.h"
#include "ui/views/controls/webview/webview.h"
#if defined(OS_CHROMEOS)
#include "ash/system/chromeos/keyboard_brightness_controller.h"
#include "base/sys_info.h"
#include "chromeos/ime/ime_keyboard.h"
#include "chromeos/ime/input_method_manager.h"
#endif // defined(OS_CHROMEOS)
namespace ash {
namespace {
using base::UserMetricsAction;
bool HandleAccessibleFocusCycle(bool reverse) {
if (reverse) {
base::RecordAction(UserMetricsAction("Accel_Accessible_Focus_Previous"));
} else {
base::RecordAction(UserMetricsAction("Accel_Accessible_Focus_Next"));
}
if (!Shell::GetInstance()->accessibility_delegate()->
IsSpokenFeedbackEnabled()) {
return false;
}
aura::Window* active_window = ash::wm::GetActiveWindow();
if (!active_window)
return false;
views::Widget* widget =
views::Widget::GetWidgetForNativeWindow(active_window);
if (!widget)
return false;
views::FocusManager* focus_manager = widget->GetFocusManager();
if (!focus_manager)
return false;
views::View* view = focus_manager->GetFocusedView();
if (!view)
return false;
if (!strcmp(view->GetClassName(), views::WebView::kViewClassName))
return false;
focus_manager->AdvanceFocus(reverse);
return true;
}
bool HandleCycleBackwardMRU(const ui::Accelerator& accelerator) {
if (accelerator.key_code() == ui::VKEY_TAB)
base::RecordAction(base::UserMetricsAction("Accel_PrevWindow_Tab"));
Shell::GetInstance()->window_cycle_controller()->HandleCycleWindow(
WindowCycleController::BACKWARD);
return true;
}
bool HandleCycleForwardMRU(const ui::Accelerator& accelerator) {
if (accelerator.key_code() == ui::VKEY_TAB)
base::RecordAction(base::UserMetricsAction("Accel_NextWindow_Tab"));
Shell::GetInstance()->window_cycle_controller()->HandleCycleWindow(
WindowCycleController::FORWARD);
return true;
}
bool ToggleOverview(const ui::Accelerator& accelerator) {
base::RecordAction(base::UserMetricsAction("Accel_Overview_F5"));
Shell::GetInstance()->window_selector_controller()->ToggleOverview();
return true;
}
bool HandleFocusShelf() {
Shell* shell = Shell::GetInstance();
base::RecordAction(base::UserMetricsAction("Accel_Focus_Shelf"));
return shell->focus_cycler()->FocusWidget(
Shelf::ForPrimaryDisplay()->shelf_widget());
}
bool HandleLaunchAppN(int n) {
base::RecordAction(UserMetricsAction("Accel_Launch_App"));
Shelf::ForPrimaryDisplay()->LaunchAppIndexAt(n);
return true;
}
bool HandleLaunchLastApp() {
base::RecordAction(UserMetricsAction("Accel_Launch_Last_App"));
Shelf::ForPrimaryDisplay()->LaunchAppIndexAt(-1);
return true;
}
// Magnify the screen
bool HandleMagnifyScreen(int delta_index) {
if (ash::Shell::GetInstance()->magnification_controller()->IsEnabled()) {
// TODO(yoshiki): Move the following logic to MagnificationController.
float scale =
ash::Shell::GetInstance()->magnification_controller()->GetScale();
// Calculate rounded logarithm (base kMagnificationScaleFactor) of scale.
int scale_index =
std::floor(std::log(scale) / std::log(kMagnificationScaleFactor) + 0.5);
int new_scale_index = std::max(0, std::min(8, scale_index + delta_index));
ash::Shell::GetInstance()->magnification_controller()->
SetScale(std::pow(kMagnificationScaleFactor, new_scale_index), true);
return true;
} else if (ash::Shell::GetInstance()->
partial_magnification_controller()->is_enabled()) {
float scale = delta_index > 0 ? kDefaultPartialMagnifiedScale : 1;
ash::Shell::GetInstance()->partial_magnification_controller()->
SetScale(scale);
return true;
}
return false;
}
bool HandleMediaNextTrack() {
Shell::GetInstance()->media_delegate()->HandleMediaNextTrack();
return true;
}
bool HandleMediaPlayPause() {
Shell::GetInstance()->media_delegate()->HandleMediaPlayPause();
return true;
}
bool HandleMediaPrevTrack() {
Shell::GetInstance()->media_delegate()->HandleMediaPrevTrack();
return true;
}
bool HandleNewIncognitoWindow() {
base::RecordAction(UserMetricsAction("Accel_New_Incognito_Window"));
bool incognito_allowed =
Shell::GetInstance()->delegate()->IsIncognitoAllowed();
if (incognito_allowed)
Shell::GetInstance()->new_window_delegate()->NewWindow(
true /* is_incognito */);
return incognito_allowed;
}
bool HandleNewTab(ui::KeyboardCode key_code) {
if (key_code == ui::VKEY_T)
base::RecordAction(base::UserMetricsAction("Accel_NewTab_T"));
Shell::GetInstance()->new_window_delegate()->NewTab();
return true;
}
bool HandleNewWindow() {
base::RecordAction(base::UserMetricsAction("Accel_New_Window"));
Shell::GetInstance()->new_window_delegate()->NewWindow(
false /* is_incognito */);
return true;
}
void HandleNextIme(ImeControlDelegate* ime_control_delegate,
ui::EventType previous_event_type,
ui::KeyboardCode previous_key_code) {
// This check is necessary e.g. not to process the Shift+Alt+
// ET_KEY_RELEASED accelerator for Chrome OS (see ash/accelerators/
// accelerator_controller.cc) when Shift+Alt+Tab is pressed and then Tab
// is released.
if (previous_event_type == ui::ET_KEY_RELEASED &&
// Workaround for crbug.com/139556. CJK IME users tend to press
// Enter (or Space) and Shift+Alt almost at the same time to commit
// an IME string and then switch from the IME to the English layout.
// This workaround allows the user to trigger NEXT_IME even if the
// user presses Shift+Alt before releasing Enter.
// TODO(nona|mazda): Fix crbug.com/139556 in a cleaner way.
previous_key_code != ui::VKEY_RETURN &&
previous_key_code != ui::VKEY_SPACE) {
// We totally ignore this accelerator.
// TODO(mazda): Fix crbug.com/158217
return;
}
base::RecordAction(UserMetricsAction("Accel_Next_Ime"));
if (ime_control_delegate)
ime_control_delegate->HandleNextIme();
}
bool HandleOpenFeedbackPage() {
base::RecordAction(UserMetricsAction("Accel_Open_Feedback_Page"));
ash::Shell::GetInstance()->new_window_delegate()->OpenFeedbackPage();
return true;
}
bool HandlePositionCenter() {
base::RecordAction(UserMetricsAction("Accel_Window_Position_Center"));
aura::Window* window = wm::GetActiveWindow();
// Docked windows do not support centering and ignore accelerator.
if (window && !wm::GetWindowState(window)->IsDocked()) {
wm::CenterWindow(window);
return true;
}
return false;
}
bool HandlePreviousIme(ImeControlDelegate* ime_control_delegate,
const ui::Accelerator& accelerator) {
base::RecordAction(UserMetricsAction("Accel_Previous_Ime"));
if (ime_control_delegate)
return ime_control_delegate->HandlePreviousIme(accelerator);
return false;
}
bool HandleRestoreTab() {
base::RecordAction(base::UserMetricsAction("Accel_Restore_Tab"));
Shell::GetInstance()->new_window_delegate()->RestoreTab();
return true;
}
bool HandleRotatePaneFocus(Shell::Direction direction) {
Shell* shell = Shell::GetInstance();
switch (direction) {
// TODO(stevet): Not sure if this is the same as IDC_FOCUS_NEXT_PANE.
case Shell::FORWARD: {
base::RecordAction(UserMetricsAction("Accel_Focus_Next_Pane"));
shell->focus_cycler()->RotateFocus(FocusCycler::FORWARD);
break;
}
case Shell::BACKWARD: {
base::RecordAction(UserMetricsAction("Accel_Focus_Previous_Pane"));
shell->focus_cycler()->RotateFocus(FocusCycler::BACKWARD);
break;
}
}
return true;
}
// Rotate the active window.
bool HandleRotateActiveWindow() {
base::RecordAction(UserMetricsAction("Accel_Rotate_Window"));
aura::Window* active_window = wm::GetActiveWindow();
if (active_window) {
// The rotation animation bases its target transform on the current
// rotation and position. Since there could be an animation in progress
// right now, queue this animation so when it starts it picks up a neutral
// rotation and position. Use replace so we only enqueue one at a time.
active_window->layer()->GetAnimator()->
set_preemption_strategy(ui::LayerAnimator::REPLACE_QUEUED_ANIMATIONS);
active_window->layer()->GetAnimator()->StartAnimation(
new ui::LayerAnimationSequence(
new ash::ScreenRotation(360, active_window->layer())));
}
return true;
}
gfx::Display::Rotation GetNextRotation(gfx::Display::Rotation current) {
switch (current) {
case gfx::Display::ROTATE_0:
return gfx::Display::ROTATE_90;
case gfx::Display::ROTATE_90:
return gfx::Display::ROTATE_180;
case gfx::Display::ROTATE_180:
return gfx::Display::ROTATE_270;
case gfx::Display::ROTATE_270:
return gfx::Display::ROTATE_0;
}
NOTREACHED() << "Unknown rotation:" << current;
return gfx::Display::ROTATE_0;
}
// Rotates the screen.
bool HandleRotateScreen() {
base::RecordAction(UserMetricsAction("Accel_Rotate_Window"));
gfx::Point point = Shell::GetScreen()->GetCursorScreenPoint();
gfx::Display display = Shell::GetScreen()->GetDisplayNearestPoint(point);
const DisplayInfo& display_info =
Shell::GetInstance()->display_manager()->GetDisplayInfo(display.id());
Shell::GetInstance()->display_manager()->SetDisplayRotation(
display.id(), GetNextRotation(display_info.rotation()));
return true;
}
bool HandleScaleReset() {
DisplayManager* display_manager = Shell::GetInstance()->display_manager();
int64 display_id = display_manager->GetDisplayIdForUIScaling();
if (display_id == gfx::Display::kInvalidDisplayID)
return false;
base::RecordAction(UserMetricsAction("Accel_Scale_Ui_Reset"));
float ui_scale =
display_manager->GetDisplayInfo(display_id).configured_ui_scale();
if (ui_scale != 1.0f) {
display_manager->SetDisplayUIScale(display_id, 1.0f);
return true;
}
return false;
}
bool HandleScaleUI(bool up) {
DisplayManager* display_manager = Shell::GetInstance()->display_manager();
int64 display_id = display_manager->GetDisplayIdForUIScaling();
if (display_id == gfx::Display::kInvalidDisplayID)
return false;
if (up) {
base::RecordAction(UserMetricsAction("Accel_Scale_Ui_Up"));
} else {
base::RecordAction(UserMetricsAction("Accel_Scale_Ui_Down"));
}
const DisplayInfo& display_info = display_manager->GetDisplayInfo(display_id);
float next_scale = DisplayManager::GetNextUIScale(display_info, up);
display_manager->SetDisplayUIScale(display_id, next_scale);
return true;
}
#if defined(OS_CHROMEOS)
bool HandleSwapPrimaryDisplay() {
base::RecordAction(UserMetricsAction("Accel_Swap_Primary_Display"));
Shell::GetInstance()->display_controller()->SwapPrimaryDisplay();
return true;
}
#endif
bool HandleShowKeyboardOverlay() {
base::RecordAction(UserMetricsAction("Accel_Show_Keyboard_Overlay"));
ash::Shell::GetInstance()->new_window_delegate()->ShowKeyboardOverlay();
return true;
}
bool HandleShowMessageCenterBubble() {
base::RecordAction(UserMetricsAction("Accel_Show_Message_Center_Bubble"));
RootWindowController* controller =
RootWindowController::ForTargetRootWindow();
StatusAreaWidget* status_area_widget =
controller->shelf()->status_area_widget();
if (status_area_widget) {
WebNotificationTray* notification_tray =
status_area_widget->web_notification_tray();
if (notification_tray->visible()) {
notification_tray->ShowMessageCenterBubble();
return true;
}
}
return false;
}
bool HandleShowSystemTrayBubble() {
base::RecordAction(UserMetricsAction("Accel_Show_System_Tray_Bubble"));
RootWindowController* controller =
RootWindowController::ForTargetRootWindow();
if (!controller->GetSystemTray()->HasSystemBubble()) {
controller->GetSystemTray()->ShowDefaultView(BUBBLE_CREATE_NEW);
return true;
}
return false;
}
bool HandleShowTaskManager() {
base::RecordAction(UserMetricsAction("Accel_Show_Task_Manager"));
Shell::GetInstance()->new_window_delegate()->ShowTaskManager();
return true;
}
#if defined(OS_CHROMEOS)
void HandleSilenceSpokenFeedback() {
base::RecordAction(UserMetricsAction("Accel_Silence_Spoken_Feedback"));
AccessibilityDelegate* delegate =
Shell::GetInstance()->accessibility_delegate();
if (!delegate->IsSpokenFeedbackEnabled())
return;
delegate->SilenceSpokenFeedback();
}
#endif
bool HandleSwitchIme(ImeControlDelegate* ime_control_delegate,
const ui::Accelerator& accelerator) {
base::RecordAction(UserMetricsAction("Accel_Switch_Ime"));
if (ime_control_delegate)
return ime_control_delegate->HandleSwitchIme(accelerator);
return false;
}
bool HandleTakePartialScreenshot(ScreenshotDelegate* screenshot_delegate) {
base::RecordAction(UserMetricsAction("Accel_Take_Partial_Screenshot"));
if (screenshot_delegate) {
ash::PartialScreenshotView::StartPartialScreenshot(
screenshot_delegate);
}
// Return true to prevent propagation of the key event because
// this key combination is reserved for partial screenshot.
return true;
}
bool HandleTakeScreenshot(ScreenshotDelegate* screenshot_delegate) {
base::RecordAction(UserMetricsAction("Accel_Take_Screenshot"));
if (screenshot_delegate &&
screenshot_delegate->CanTakeScreenshot()) {
screenshot_delegate->HandleTakeScreenshotForAllRootWindows();
}
// Return true to prevent propagation of the key event.
return true;
}
bool HandleToggleAppList(ui::KeyboardCode key_code,
ui::EventType previous_event_type,
ui::KeyboardCode previous_key_code,
const ui::Accelerator& accelerator) {
// If something else was pressed between the Search key (LWIN)
// being pressed and released, then ignore the release of the
// Search key.
if (key_code == ui::VKEY_LWIN &&
(previous_event_type == ui::ET_KEY_RELEASED ||
previous_key_code != ui::VKEY_LWIN))
return false;
if (key_code == ui::VKEY_LWIN)
base::RecordAction(base::UserMetricsAction("Accel_Search_LWin"));
// When spoken feedback is enabled, we should neither toggle the list nor
// consume the key since Search+Shift is one of the shortcuts the a11y
// feature uses. crbug.com/132296
DCHECK_EQ(ui::VKEY_LWIN, accelerator.key_code());
if (Shell::GetInstance()->accessibility_delegate()->
IsSpokenFeedbackEnabled())
return false;
ash::Shell::GetInstance()->ToggleAppList(NULL);
return true;
}
bool HandleToggleFullscreen(ui::KeyboardCode key_code) {
if (key_code == ui::VKEY_MEDIA_LAUNCH_APP2) {
base::RecordAction(UserMetricsAction("Accel_Fullscreen_F4"));
}
accelerators::ToggleFullscreen();
return true;
}
bool HandleWindowSnapOrDock(int action) {
wm::WindowState* window_state = wm::GetActiveWindowState();
// Disable window snapping shortcut key for full screen window due to
// http://crbug.com/135487.
if (!window_state ||
(window_state->window()->type() != ui::wm::WINDOW_TYPE_NORMAL &&
window_state->window()->type() != ui::wm::WINDOW_TYPE_PANEL) ||
window_state->IsFullscreen()) {
return false;
}
if (action == WINDOW_CYCLE_SNAP_DOCK_LEFT)
base::RecordAction(UserMetricsAction("Accel_Window_Snap_Left"));
else
base::RecordAction(UserMetricsAction("Accel_Window_Snap_Right"));
const wm::WMEvent event(action == WINDOW_CYCLE_SNAP_DOCK_LEFT ?
wm::WM_EVENT_CYCLE_SNAP_DOCK_LEFT :
wm::WM_EVENT_CYCLE_SNAP_DOCK_RIGHT);
window_state->OnWMEvent(&event);
return true;
}
bool HandleWindowMinimize() {
base::RecordAction(
base::UserMetricsAction("Accel_Toggle_Minimized_Minus"));
return accelerators::ToggleMinimized();
}
#if defined(OS_CHROMEOS)
bool HandleCrosh() {
base::RecordAction(UserMetricsAction("Accel_Open_Crosh"));
Shell::GetInstance()->new_window_delegate()->OpenCrosh();
return true;
}
bool HandleFileManager() {
base::RecordAction(UserMetricsAction("Accel_Open_File_Manager"));
Shell::GetInstance()->new_window_delegate()->OpenFileManager();
return true;
}
bool HandleLock(ui::KeyboardCode key_code) {
base::RecordAction(UserMetricsAction("Accel_LockScreen_L"));
Shell::GetInstance()->session_state_delegate()->LockScreen();
return true;
}
bool HandleCycleUser(SessionStateDelegate::CycleUser cycle_user) {
if (!Shell::GetInstance()->delegate()->IsMultiProfilesEnabled())
return false;
ash::SessionStateDelegate* delegate =
ash::Shell::GetInstance()->session_state_delegate();
if (delegate->NumberOfLoggedInUsers() <= 1)
return false;
MultiProfileUMA::RecordSwitchActiveUser(
MultiProfileUMA::SWITCH_ACTIVE_USER_BY_ACCELERATOR);
switch (cycle_user) {
case SessionStateDelegate::CYCLE_TO_NEXT_USER:
base::RecordAction(UserMetricsAction("Accel_Switch_To_Next_User"));
break;
case SessionStateDelegate::CYCLE_TO_PREVIOUS_USER:
base::RecordAction(UserMetricsAction("Accel_Switch_To_Previous_User"));
break;
}
delegate->CycleActiveUser(cycle_user);
return true;
}
bool HandleToggleMirrorMode() {
base::RecordAction(UserMetricsAction("Accel_Toggle_Mirror_Mode"));
Shell::GetInstance()->display_controller()->ToggleMirrorMode();
return true;
}
bool HandleToggleSpokenFeedback() {
base::RecordAction(UserMetricsAction("Accel_Toggle_Spoken_Feedback"));
Shell::GetInstance()->accessibility_delegate()->
ToggleSpokenFeedback(A11Y_NOTIFICATION_SHOW);
return true;
}
bool HandleToggleTouchViewTesting() {
// TODO(skuhne): This is only temporary! Remove this!
if (CommandLine::ForCurrentProcess()->HasSwitch(
switches::kAshEnableTouchViewTesting)) {
MaximizeModeController* controller = Shell::GetInstance()->
maximize_mode_controller();
controller->EnableMaximizeModeWindowManager(
!controller->IsMaximizeModeWindowManagerEnabled());
return true;
}
return false;
}
bool HandleTouchHudClear() {
RootWindowController* controller =
RootWindowController::ForTargetRootWindow();
if (controller->touch_hud_debug()) {
controller->touch_hud_debug()->Clear();
return true;
}
return false;
}
bool HandleTouchHudModeChange() {
RootWindowController* controller =
RootWindowController::ForTargetRootWindow();
if (controller->touch_hud_debug()) {
controller->touch_hud_debug()->ChangeToNextMode();
return true;
}
return false;
}
bool HandleDisableCapsLock(ui::KeyboardCode key_code,
ui::EventType previous_event_type,
ui::KeyboardCode previous_key_code) {
if (previous_event_type == ui::ET_KEY_RELEASED ||
(previous_key_code != ui::VKEY_LSHIFT &&
previous_key_code != ui::VKEY_SHIFT &&
previous_key_code != ui::VKEY_RSHIFT)) {
// If something else was pressed between the Shift key being pressed
// and released, then ignore the release of the Shift key.
return false;
}
base::RecordAction(UserMetricsAction("Accel_Disable_Caps_Lock"));
chromeos::input_method::InputMethodManager* ime =
chromeos::input_method::InputMethodManager::Get();
chromeos::input_method::ImeKeyboard* keyboard =
ime ? ime->GetImeKeyboard() : NULL;
if (keyboard && keyboard->CapsLockIsEnabled()) {
keyboard->SetCapsLockEnabled(false);
return true;
}
return false;
}
bool HandleToggleCapsLock(ui::KeyboardCode key_code,
ui::EventType previous_event_type,
ui::KeyboardCode previous_key_code) {
if (key_code == ui::VKEY_LWIN) {
// If something else was pressed between the Search key (LWIN)
// being pressed and released, then ignore the release of the
// Search key.
// TODO(danakj): Releasing Alt first breaks this: crbug.com/166495
if (previous_event_type == ui::ET_KEY_RELEASED ||
previous_key_code != ui::VKEY_LWIN)
return false;
}
base::RecordAction(UserMetricsAction("Accel_Toggle_Caps_Lock"));
chromeos::input_method::InputMethodManager* ime =
chromeos::input_method::InputMethodManager::Get();
chromeos::input_method::ImeKeyboard* keyboard =
ime ? ime->GetImeKeyboard() : NULL;
if (keyboard)
keyboard->SetCapsLockEnabled(!keyboard->CapsLockIsEnabled());
return true;
}
#endif // defined(OS_CHROMEOS)
class AutoSet {
public:
AutoSet(ui::Accelerator* scoped, ui::Accelerator new_value)
: scoped_(scoped), new_value_(new_value) {}
~AutoSet() { *scoped_ = new_value_; }
private:
ui::Accelerator* scoped_;
const ui::Accelerator new_value_;
DISALLOW_COPY_AND_ASSIGN(AutoSet);
};
} // namespace
////////////////////////////////////////////////////////////////////////////////
// AcceleratorController, public:
AcceleratorController::AcceleratorController()
: accelerator_manager_(new ui::AcceleratorManager) {
Init();
}
AcceleratorController::~AcceleratorController() {
}
void AcceleratorController::Init() {
previous_accelerator_.set_type(ui::ET_UNKNOWN);
for (size_t i = 0; i < kActionsAllowedAtLoginOrLockScreenLength; ++i) {
actions_allowed_at_login_screen_.insert(
kActionsAllowedAtLoginOrLockScreen[i]);
actions_allowed_at_lock_screen_.insert(
kActionsAllowedAtLoginOrLockScreen[i]);
}
for (size_t i = 0; i < kActionsAllowedAtLockScreenLength; ++i)
actions_allowed_at_lock_screen_.insert(kActionsAllowedAtLockScreen[i]);
for (size_t i = 0; i < kActionsAllowedAtModalWindowLength; ++i)
actions_allowed_at_modal_window_.insert(kActionsAllowedAtModalWindow[i]);
for (size_t i = 0; i < kPreferredActionsLength; ++i)
preferred_actions_.insert(kPreferredActions[i]);
for (size_t i = 0; i < kReservedActionsLength; ++i)
reserved_actions_.insert(kReservedActions[i]);
for (size_t i = 0; i < kNonrepeatableActionsLength; ++i)
nonrepeatable_actions_.insert(kNonrepeatableActions[i]);
for (size_t i = 0; i < kActionsAllowedInAppModeLength; ++i)
actions_allowed_in_app_mode_.insert(kActionsAllowedInAppMode[i]);
for (size_t i = 0; i < kActionsNeedingWindowLength; ++i)
actions_needing_window_.insert(kActionsNeedingWindow[i]);
RegisterAccelerators(kAcceleratorData, kAcceleratorDataLength);
if (debug::DebugAcceleratorsEnabled()) {
RegisterAccelerators(kDebugAcceleratorData, kDebugAcceleratorDataLength);
// All debug accelerators are reserved.
for (size_t i = 0; i < kDebugAcceleratorDataLength; ++i)
reserved_actions_.insert(kDebugAcceleratorData[i].action);
}
#if defined(OS_CHROMEOS)
keyboard_brightness_control_delegate_.reset(
new KeyboardBrightnessController());
#endif
}
void AcceleratorController::Register(const ui::Accelerator& accelerator,
ui::AcceleratorTarget* target) {
accelerator_manager_->Register(accelerator,
ui::AcceleratorManager::kNormalPriority,
target);
}
void AcceleratorController::Unregister(const ui::Accelerator& accelerator,
ui::AcceleratorTarget* target) {
accelerator_manager_->Unregister(accelerator, target);
}
void AcceleratorController::UnregisterAll(ui::AcceleratorTarget* target) {
accelerator_manager_->UnregisterAll(target);
}
bool AcceleratorController::Process(const ui::Accelerator& accelerator) {
AutoSet auto_set(&previous_accelerator_, accelerator);
if (ime_control_delegate_) {
return accelerator_manager_->Process(
ime_control_delegate_->RemapAccelerator(accelerator));
}
return accelerator_manager_->Process(accelerator);
}
bool AcceleratorController::IsRegistered(
const ui::Accelerator& accelerator) const {
return accelerator_manager_->GetCurrentTarget(accelerator) != NULL;
}
bool AcceleratorController::IsPreferred(
const ui::Accelerator& accelerator) const {
const ui::Accelerator remapped_accelerator = ime_control_delegate_.get() ?
ime_control_delegate_->RemapAccelerator(accelerator) : accelerator;
std::map<ui::Accelerator, int>::const_iterator iter =
accelerators_.find(remapped_accelerator);
if (iter == accelerators_.end())
return false; // not an accelerator.
return preferred_actions_.find(iter->second) != preferred_actions_.end();
}
bool AcceleratorController::IsReserved(
const ui::Accelerator& accelerator) const {
const ui::Accelerator remapped_accelerator = ime_control_delegate_.get() ?
ime_control_delegate_->RemapAccelerator(accelerator) : accelerator;
std::map<ui::Accelerator, int>::const_iterator iter =
accelerators_.find(remapped_accelerator);
if (iter == accelerators_.end())
return false; // not an accelerator.
return reserved_actions_.find(iter->second) != reserved_actions_.end();
}
bool AcceleratorController::PerformAction(int action,
const ui::Accelerator& accelerator) {
ash::Shell* shell = ash::Shell::GetInstance();
AcceleratorProcessingRestriction restriction =
GetAcceleratorProcessingRestriction(action);
if (restriction != RESTRICTION_NONE)
return restriction == RESTRICTION_PREVENT_PROCESSING_AND_PROPAGATION;
const ui::KeyboardCode key_code = accelerator.key_code();
// PerformAction() is performed from gesture controllers and passes
// empty Accelerator() instance as the second argument. Such events
// should never be suspended.
const bool gesture_event = key_code == ui::VKEY_UNKNOWN;
// Ignore accelerators invoked as repeated (while holding a key for a long
// time, if their handling is nonrepeatable.
if (nonrepeatable_actions_.find(action) != nonrepeatable_actions_.end() &&
accelerator.IsRepeat() && !gesture_event) {
return true;
}
// Type of the previous accelerator. Used by NEXT_IME and DISABLE_CAPS_LOCK.
const ui::EventType previous_event_type = previous_accelerator_.type();
const ui::KeyboardCode previous_key_code = previous_accelerator_.key_code();
// You *MUST* return true when some action is performed. Otherwise, this
// function might be called *twice*, via BrowserView::PreHandleKeyboardEvent
// and BrowserView::HandleKeyboardEvent, for a single accelerator press.
//
// If your accelerator invokes more than one line of code, please either
// implement it in your module's controller code (like TOGGLE_MIRROR_MODE
// below) or pull it into a HandleFoo() function above.
switch (action) {
case ACCESSIBLE_FOCUS_NEXT:
return HandleAccessibleFocusCycle(false);
case ACCESSIBLE_FOCUS_PREVIOUS:
return HandleAccessibleFocusCycle(true);
case CYCLE_BACKWARD_MRU:
return HandleCycleBackwardMRU(accelerator);
case CYCLE_FORWARD_MRU:
return HandleCycleForwardMRU(accelerator);
case TOGGLE_OVERVIEW:
return ToggleOverview(accelerator);
#if defined(OS_CHROMEOS)
case TOGGLE_MIRROR_MODE:
return HandleToggleMirrorMode();
case LOCK_SCREEN:
return HandleLock(key_code);
case OPEN_FILE_MANAGER:
return HandleFileManager();
case OPEN_CROSH:
return HandleCrosh();
case SILENCE_SPOKEN_FEEDBACK:
HandleSilenceSpokenFeedback();
return false;
case SWAP_PRIMARY_DISPLAY:
return HandleSwapPrimaryDisplay();
case SWITCH_TO_NEXT_USER:
return HandleCycleUser(SessionStateDelegate::CYCLE_TO_NEXT_USER);
case SWITCH_TO_PREVIOUS_USER:
return HandleCycleUser(SessionStateDelegate::CYCLE_TO_PREVIOUS_USER);
case TOGGLE_SPOKEN_FEEDBACK:
return HandleToggleSpokenFeedback();
case TOGGLE_TOUCH_VIEW_TESTING:
return HandleToggleTouchViewTesting();
case TOGGLE_WIFI:
Shell::GetInstance()->system_tray_notifier()->NotifyRequestToggleWifi();
return true;
case TOUCH_HUD_CLEAR:
return HandleTouchHudClear();
case TOUCH_HUD_MODE_CHANGE:
return HandleTouchHudModeChange();
case TOUCH_HUD_PROJECTION_TOGGLE:
accelerators::ToggleTouchHudProjection();
return true;
case DISABLE_GPU_WATCHDOG:
Shell::GetInstance()->gpu_support()->DisableGpuWatchdog();
return true;
case DISABLE_CAPS_LOCK:
return HandleDisableCapsLock(
key_code, previous_event_type, previous_key_code);
case TOGGLE_CAPS_LOCK:
return HandleToggleCapsLock(
key_code, previous_event_type, previous_key_code);
#endif // OS_CHROMEOS
case OPEN_FEEDBACK_PAGE:
return HandleOpenFeedbackPage();
case EXIT:
// UMA metrics are recorded in the handler.
exit_warning_handler_.HandleAccelerator();
return true;
case NEW_INCOGNITO_WINDOW:
return HandleNewIncognitoWindow();
case NEW_TAB:
return HandleNewTab(key_code);
case NEW_WINDOW:
return HandleNewWindow();
case RESTORE_TAB:
return HandleRestoreTab();
case TAKE_SCREENSHOT:
return HandleTakeScreenshot(screenshot_delegate_.get());
case TAKE_PARTIAL_SCREENSHOT:
return HandleTakePartialScreenshot(screenshot_delegate_.get());
case TOGGLE_APP_LIST:
return HandleToggleAppList(
key_code, previous_event_type, previous_key_code, accelerator);
case BRIGHTNESS_DOWN:
if (brightness_control_delegate_)
return brightness_control_delegate_->HandleBrightnessDown(accelerator);
return false;
case BRIGHTNESS_UP:
if (brightness_control_delegate_)
return brightness_control_delegate_->HandleBrightnessUp(accelerator);
return false;
case KEYBOARD_BRIGHTNESS_DOWN:
if (keyboard_brightness_control_delegate_)
return keyboard_brightness_control_delegate_->
HandleKeyboardBrightnessDown(accelerator);
return false;
case KEYBOARD_BRIGHTNESS_UP:
if (keyboard_brightness_control_delegate_)
return keyboard_brightness_control_delegate_->
HandleKeyboardBrightnessUp(accelerator);
return false;
case VOLUME_MUTE: {
ash::VolumeControlDelegate* volume_delegate =
shell->system_tray_delegate()->GetVolumeControlDelegate();
return volume_delegate && volume_delegate->HandleVolumeMute(accelerator);
}
case VOLUME_DOWN: {
ash::VolumeControlDelegate* volume_delegate =
shell->system_tray_delegate()->GetVolumeControlDelegate();
return volume_delegate && volume_delegate->HandleVolumeDown(accelerator);
}
case VOLUME_UP: {
ash::VolumeControlDelegate* volume_delegate =
shell->system_tray_delegate()->GetVolumeControlDelegate();
return volume_delegate && volume_delegate->HandleVolumeUp(accelerator);
}
case FOCUS_SHELF:
return HandleFocusShelf();
case FOCUS_NEXT_PANE:
return HandleRotatePaneFocus(Shell::FORWARD);
case FOCUS_PREVIOUS_PANE:
return HandleRotatePaneFocus(Shell::BACKWARD);
case SHOW_KEYBOARD_OVERLAY:
return HandleShowKeyboardOverlay();
case SHOW_SYSTEM_TRAY_BUBBLE:
return HandleShowSystemTrayBubble();
case SHOW_MESSAGE_CENTER_BUBBLE:
return HandleShowMessageCenterBubble();
case SHOW_TASK_MANAGER:
return HandleShowTaskManager();
case NEXT_IME:
HandleNextIme(
ime_control_delegate_.get(), previous_event_type, previous_key_code);
// NEXT_IME is bound to Alt-Shift key up event. To be consistent with
// Windows behavior, do not consume the key event here.
return false;
case PREVIOUS_IME:
return HandlePreviousIme(ime_control_delegate_.get(), accelerator);
case PRINT_UI_HIERARCHIES:
debug::PrintUIHierarchies();
return true;
case SWITCH_IME:
return HandleSwitchIme(ime_control_delegate_.get(), accelerator);
case LAUNCH_APP_0:
return HandleLaunchAppN(0);
case LAUNCH_APP_1:
return HandleLaunchAppN(1);
case LAUNCH_APP_2:
return HandleLaunchAppN(2);
case LAUNCH_APP_3:
return HandleLaunchAppN(3);
case LAUNCH_APP_4:
return HandleLaunchAppN(4);
case LAUNCH_APP_5:
return HandleLaunchAppN(5);
case LAUNCH_APP_6:
return HandleLaunchAppN(6);
case LAUNCH_APP_7:
return HandleLaunchAppN(7);
case LAUNCH_LAST_APP:
return HandleLaunchLastApp();
case WINDOW_CYCLE_SNAP_DOCK_LEFT:
case WINDOW_CYCLE_SNAP_DOCK_RIGHT:
return HandleWindowSnapOrDock(action);
case WINDOW_MINIMIZE:
return HandleWindowMinimize();
case TOGGLE_FULLSCREEN:
return HandleToggleFullscreen(key_code);
case TOGGLE_MAXIMIZED:
accelerators::ToggleMaximized();
return true;
case WINDOW_POSITION_CENTER:
return HandlePositionCenter();
case SCALE_UI_UP:
return HandleScaleUI(true /* up */);
case SCALE_UI_DOWN:
return HandleScaleUI(false /* down */);
case SCALE_UI_RESET:
return HandleScaleReset();
case ROTATE_WINDOW:
return HandleRotateActiveWindow();
case ROTATE_SCREEN:
return HandleRotateScreen();
case MAGNIFY_SCREEN_ZOOM_IN:
return HandleMagnifyScreen(1);
case MAGNIFY_SCREEN_ZOOM_OUT:
return HandleMagnifyScreen(-1);
case MEDIA_NEXT_TRACK:
return HandleMediaNextTrack();
case MEDIA_PLAY_PAUSE:
return HandleMediaPlayPause();
case MEDIA_PREV_TRACK:
return HandleMediaPrevTrack();
case POWER_PRESSED: // fallthrough
case POWER_RELEASED:
#if defined(OS_CHROMEOS)
if (!base::SysInfo::IsRunningOnChromeOS()) {
// There is no powerd in linux desktop, so call the
// PowerButtonController here.
Shell::GetInstance()->power_button_controller()->
OnPowerButtonEvent(action == POWER_PRESSED, base::TimeTicks());
}
#endif
// We don't do anything with these at present on the device,
// (power button events are reported to us from powerm via
// D-BUS), but we consume them to prevent them from getting
// passed to apps -- see http://crbug.com/146609.
return true;
case LOCK_PRESSED:
case LOCK_RELEASED:
Shell::GetInstance()->power_button_controller()->
OnLockButtonEvent(action == LOCK_PRESSED, base::TimeTicks());
return true;
default:
DCHECK(debug::DebugAcceleratorsEnabled())
<< "Unhandled action " << action;
}
// If |action| is a debug action, run it.
debug::PerformDebugAction(action);
return false;
}
AcceleratorController::AcceleratorProcessingRestriction
AcceleratorController::GetCurrentAcceleratorRestriction() {
return GetAcceleratorProcessingRestriction(-1);
}
AcceleratorController::AcceleratorProcessingRestriction
AcceleratorController::GetAcceleratorProcessingRestriction(int action) {
ash::Shell* shell = ash::Shell::GetInstance();
if (!shell->session_state_delegate()->IsActiveUserSessionStarted() &&
actions_allowed_at_login_screen_.find(action) ==
actions_allowed_at_login_screen_.end()) {
return RESTRICTION_PREVENT_PROCESSING;
}
if (shell->session_state_delegate()->IsScreenLocked() &&
actions_allowed_at_lock_screen_.find(action) ==
actions_allowed_at_lock_screen_.end()) {
return RESTRICTION_PREVENT_PROCESSING;
}
if (shell->IsSystemModalWindowOpen() &&
actions_allowed_at_modal_window_.find(action) ==
actions_allowed_at_modal_window_.end()) {
// Note we prevent the shortcut from propagating so it will not
// be passed to the modal window. This is important for things like
// Alt+Tab that would cause an undesired effect in the modal window by
// cycling through its window elements.
return RESTRICTION_PREVENT_PROCESSING_AND_PROPAGATION;
}
if (shell->delegate()->IsRunningInForcedAppMode() &&
actions_allowed_in_app_mode_.find(action) ==
actions_allowed_in_app_mode_.end()) {
return RESTRICTION_PREVENT_PROCESSING;
}
if (MruWindowTracker::BuildWindowList(false).empty() &&
actions_needing_window_.find(action) != actions_needing_window_.end()) {
Shell::GetInstance()->accessibility_delegate()->TriggerAccessibilityAlert(
A11Y_ALERT_WINDOW_NEEDED);
return RESTRICTION_PREVENT_PROCESSING_AND_PROPAGATION;
}
return RESTRICTION_NONE;
}
void AcceleratorController::SetBrightnessControlDelegate(
scoped_ptr<BrightnessControlDelegate> brightness_control_delegate) {
brightness_control_delegate_ = brightness_control_delegate.Pass();
}
void AcceleratorController::SetImeControlDelegate(
scoped_ptr<ImeControlDelegate> ime_control_delegate) {
ime_control_delegate_ = ime_control_delegate.Pass();
}
void AcceleratorController::SetScreenshotDelegate(
scoped_ptr<ScreenshotDelegate> screenshot_delegate) {
screenshot_delegate_ = screenshot_delegate.Pass();
}
////////////////////////////////////////////////////////////////////////////////
// AcceleratorController, ui::AcceleratorTarget implementation:
bool AcceleratorController::AcceleratorPressed(
const ui::Accelerator& accelerator) {
std::map<ui::Accelerator, int>::const_iterator it =
accelerators_.find(accelerator);
DCHECK(it != accelerators_.end());
return PerformAction(static_cast<AcceleratorAction>(it->second), accelerator);
}
void AcceleratorController::RegisterAccelerators(
const AcceleratorData accelerators[],
size_t accelerators_length) {
for (size_t i = 0; i < accelerators_length; ++i) {
ui::Accelerator accelerator(accelerators[i].keycode,
accelerators[i].modifiers);
accelerator.set_type(accelerators[i].trigger_on_press ?
ui::ET_KEY_PRESSED : ui::ET_KEY_RELEASED);
Register(accelerator, this);
accelerators_.insert(
std::make_pair(accelerator, accelerators[i].action));
}
}
void AcceleratorController::SetKeyboardBrightnessControlDelegate(
scoped_ptr<KeyboardBrightnessControlDelegate>
keyboard_brightness_control_delegate) {
keyboard_brightness_control_delegate_ =
keyboard_brightness_control_delegate.Pass();
}
bool AcceleratorController::CanHandleAccelerators() const {
return true;
}
} // namespace ash
|