forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaccelerator_controller_impl.cc
2578 lines (2284 loc) · 90.6 KB
/
accelerator_controller_impl.cc
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
// 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_impl.h"
#include <algorithm>
#include <cmath>
#include <string>
#include <utility>
#include "ash/accelerators/accelerator_commands.h"
#include "ash/accelerators/accelerator_confirmation_dialog.h"
#include "ash/accelerators/debug_commands.h"
#include "ash/accessibility/accessibility_controller_impl.h"
#include "ash/ambient/ambient_controller.h"
#include "ash/app_list/app_list_controller_impl.h"
#include "ash/assistant/assistant_controller_impl.h"
#include "ash/assistant/assistant_ui_controller.h"
#include "ash/assistant/model/assistant_ui_model.h"
#include "ash/debug.h"
#include "ash/display/display_configuration_controller.h"
#include "ash/display/display_move_window_util.h"
#include "ash/display/privacy_screen_controller.h"
#include "ash/display/screen_orientation_controller.h"
#include "ash/focus_cycler.h"
#include "ash/home_screen/home_screen_controller.h"
#include "ash/ime/ime_controller_impl.h"
#include "ash/ime/ime_switch_type.h"
#include "ash/keyboard/ui/keyboard_ui_controller.h"
#include "ash/magnifier/docked_magnifier_controller_impl.h"
#include "ash/magnifier/magnification_controller.h"
#include "ash/media/media_controller_impl.h"
#include "ash/metrics/user_metrics_recorder.h"
#include "ash/multi_profile_uma.h"
#include "ash/public/cpp/ash_features.h"
#include "ash/public/cpp/ash_pref_names.h"
#include "ash/public/cpp/ash_switches.h"
#include "ash/public/cpp/new_window_delegate.h"
#include "ash/public/cpp/notification_utils.h"
#include "ash/public/cpp/toast_data.h"
#include "ash/resources/vector_icons/vector_icons.h"
#include "ash/root_window_controller.h"
#include "ash/rotator/window_rotation.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shelf/home_button.h"
#include "ash/shelf/shelf.h"
#include "ash/shelf/shelf_focus_cycler.h"
#include "ash/shelf/shelf_navigation_widget.h"
#include "ash/shelf/shelf_widget.h"
#include "ash/shell.h"
#include "ash/shell_delegate.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/system/brightness_control_delegate.h"
#include "ash/system/ime_menu/ime_menu_tray.h"
#include "ash/system/keyboard_brightness_control_delegate.h"
#include "ash/system/model/enterprise_domain_model.h"
#include "ash/system/model/system_tray_model.h"
#include "ash/system/palette/palette_tray.h"
#include "ash/system/palette/palette_utils.h"
#include "ash/system/power/power_button_controller.h"
#include "ash/system/status_area_widget.h"
#include "ash/system/toast/toast_manager_impl.h"
#include "ash/system/tray/system_tray_notifier.h"
#include "ash/system/unified/unified_system_tray.h"
#include "ash/touch/touch_hud_debug.h"
#include "ash/utility/screenshot_controller.h"
#include "ash/wm/desks/desks_animations.h"
#include "ash/wm/desks/desks_controller.h"
#include "ash/wm/mru_window_tracker.h"
#include "ash/wm/overview/overview_controller.h"
#include "ash/wm/overview/overview_session.h"
#include "ash/wm/screen_pinning_controller.h"
#include "ash/wm/tablet_mode/tablet_mode_controller.h"
#include "ash/wm/window_cycle_controller.h"
#include "ash/wm/window_positioning_utils.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/bind_helpers.h"
#include "base/command_line.h"
#include "base/files/file_util.h"
#include "base/json/json_reader.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/metrics/user_metrics.h"
#include "base/optional.h"
#include "base/stl_util.h"
#include "base/strings/string_split.h"
#include "base/strings/utf_string_conversions.h"
#include "base/system/sys_info.h"
#include "chromeos/constants/chromeos_features.h"
#include "chromeos/dbus/power/power_manager_client.h"
#include "components/user_manager/user_type.h"
#include "ui/base/accelerators/accelerator.h"
#include "ui/base/accelerators/accelerator_manager.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/ui_base_features.h"
#include "ui/chromeos/events/keyboard_layout_util.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/layer_animation_sequence.h"
#include "ui/compositor/layer_animator.h"
#include "ui/display/display.h"
#include "ui/display/manager/managed_display_info.h"
#include "ui/display/screen.h"
#include "ui/events/devices/device_data_manager.h"
#include "ui/events/devices/input_device.h"
#include "ui/gfx/paint_vector_icon.h"
#include "ui/message_center/message_center.h"
namespace ash {
const char kNotifierAccelerator[] = "ash.accelerator-controller";
const char kTabletCountOfVolumeAdjustType[] = "Tablet.CountOfVolumeAdjustType";
const char kHighContrastToggleAccelNotificationId[] =
"chrome://settings/accessibility/highcontrast";
const char kDockedMagnifierToggleAccelNotificationId[] =
"chrome://settings/accessibility/dockedmagnifier";
const char kFullscreenMagnifierToggleAccelNotificationId[] =
"chrome://settings/accessibility/fullscreenmagnifier";
const char kSpokenFeedbackToggleAccelNotificationId[] =
"chrome://settings/accessibility/spokenfeedback";
const char kAccessibilityHighContrastShortcut[] =
"Accessibility.Shortcuts.CrosHighContrast";
const char kAccessibilitySpokenFeedbackShortcut[] =
"Accessibility.Shortcuts.CrosSpokenFeedback";
const char kAccessibilityScreenMagnifierShortcut[] =
"Accessibility.Shortcuts.CrosScreenMagnifier";
const char kAccessibilityDockedMagnifierShortcut[] =
"Accessibility.Shortcuts.CrosDockedMagnifier";
const char kAccelWindowSnap[] = "Ash.Accelerators.WindowSnap";
namespace {
using base::UserMetricsAction;
using message_center::Notification;
using message_center::SystemNotificationWarningLevel;
// Toast id and duration for Assistant shortcuts.
constexpr char kAssistantErrorToastId[] = "assistant_error";
constexpr int kToastDurationMs = 2500;
constexpr char kVirtualDesksToastId[] = "virtual_desks_toast";
// Path of the json file that contains side volume button location info.
constexpr char kSideVolumeButtonLocationFilePath[] =
"/usr/share/chromeos-assets/side_volume_button/location.json";
// The interval between two volume control actions within one volume adjust.
constexpr base::TimeDelta kVolumeAdjustTimeout =
base::TimeDelta::FromSeconds(2);
// These values are written to logs. New enum values can be added, but existing
// enums must never be renumbered or deleted and reused.
// Records the result of triggering the rotation accelerator.
enum class RotationAcceleratorAction {
kCancelledDialog = 0,
kAcceptedDialog = 1,
kAlreadyAcceptedDialog = 2,
kMaxValue = kAlreadyAcceptedDialog,
};
void RecordRotationAcceleratorAction(const RotationAcceleratorAction& action) {
UMA_HISTOGRAM_ENUMERATION("Ash.Accelerators.Rotation.Usage", action);
}
void RecordWindowSnapAcceleratorAction(WindowSnapAcceleratorAction action) {
UMA_HISTOGRAM_ENUMERATION(kAccelWindowSnap, action);
}
void RecordTabletVolumeAdjustTypeHistogram(TabletModeVolumeAdjustType type) {
UMA_HISTOGRAM_ENUMERATION(kTabletCountOfVolumeAdjustType, type);
}
// Ensures that there are no word breaks at the "+"s in the shortcut texts such
// as "Ctrl+Shift+Space".
void EnsureNoWordBreaks(base::string16* shortcut_text) {
std::vector<base::string16> keys =
base::SplitString(*shortcut_text, base::ASCIIToUTF16("+"),
base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
if (keys.size() < 2U)
return;
// The plus sign surrounded by the word joiner to guarantee an non-breaking
// shortcut.
const base::string16 non_breaking_plus =
base::UTF8ToUTF16("\xe2\x81\xa0+\xe2\x81\xa0");
shortcut_text->clear();
for (size_t i = 0; i < keys.size() - 1; ++i) {
*shortcut_text += keys[i];
*shortcut_text += non_breaking_plus;
}
*shortcut_text += keys.back();
}
// Gets the notification message after it formats it in such a way that there
// are no line breaks in the middle of the shortcut texts.
base::string16 GetNotificationText(int message_id,
int old_shortcut_id,
int new_shortcut_id) {
base::string16 old_shortcut = l10n_util::GetStringUTF16(old_shortcut_id);
base::string16 new_shortcut = l10n_util::GetStringUTF16(new_shortcut_id);
EnsureNoWordBreaks(&old_shortcut);
EnsureNoWordBreaks(&new_shortcut);
return l10n_util::GetStringFUTF16(message_id, new_shortcut, old_shortcut);
}
// Shows a warning the user is using a deprecated accelerator.
void ShowDeprecatedAcceleratorNotification(const char* const notification_id,
int message_id,
int old_shortcut_id,
int new_shortcut_id) {
const base::string16 message =
GetNotificationText(message_id, old_shortcut_id, new_shortcut_id);
auto delegate =
base::MakeRefCounted<message_center::HandleNotificationClickDelegate>(
base::BindRepeating([]() {
if (!Shell::Get()->session_controller()->IsUserSessionBlocked())
Shell::Get()->shell_delegate()->OpenKeyboardShortcutHelpPage();
}));
std::unique_ptr<Notification> notification = ash::CreateSystemNotification(
message_center::NOTIFICATION_TYPE_SIMPLE, notification_id,
l10n_util::GetStringUTF16(IDS_DEPRECATED_SHORTCUT_TITLE), message,
base::string16(), GURL(),
message_center::NotifierId(message_center::NotifierType::SYSTEM_COMPONENT,
kNotifierAccelerator),
message_center::RichNotificationData(), std::move(delegate),
kNotificationKeyboardIcon, SystemNotificationWarningLevel::NORMAL);
notification->set_priority(message_center::SYSTEM_PRIORITY);
message_center::MessageCenter::Get()->AddNotification(
std::move(notification));
}
void ShowToast(std::string id, const base::string16& text) {
ToastData toast(id, text, kToastDurationMs, base::nullopt,
/*visible_on_lock_screen=*/true);
Shell::Get()->toast_manager()->Show(toast);
}
ui::Accelerator CreateAccelerator(ui::KeyboardCode keycode,
int modifiers,
bool trigger_on_press) {
ui::Accelerator accelerator(keycode, modifiers);
accelerator.set_key_state(trigger_on_press
? ui::Accelerator::KeyState::PRESSED
: ui::Accelerator::KeyState::RELEASED);
return accelerator;
}
void RecordUmaHistogram(const char* histogram_name,
DeprecatedAcceleratorUsage sample) {
auto* histogram = base::LinearHistogram::FactoryGet(
histogram_name, 1, DEPRECATED_USAGE_COUNT, DEPRECATED_USAGE_COUNT + 1,
base::HistogramBase::kUmaTargetedHistogramFlag);
histogram->Add(sample);
}
void RecordImeSwitchByAccelerator() {
UMA_HISTOGRAM_ENUMERATION("InputMethod.ImeSwitch",
ImeSwitchType::kAccelerator);
}
void RecordImeSwitchByModeChangeKey() {
UMA_HISTOGRAM_ENUMERATION("InputMethod.ImeSwitch",
ImeSwitchType::kModeChangeKey);
}
void HandleCycleBackwardMRU(const ui::Accelerator& accelerator) {
if (accelerator.key_code() == ui::VKEY_TAB)
base::RecordAction(base::UserMetricsAction("Accel_PrevWindow_Tab"));
Shell::Get()->window_cycle_controller()->HandleCycleWindow(
WindowCycleController::BACKWARD);
}
void HandleCycleForwardMRU(const ui::Accelerator& accelerator) {
if (accelerator.key_code() == ui::VKEY_TAB)
base::RecordAction(base::UserMetricsAction("Accel_NextWindow_Tab"));
Shell::Get()->window_cycle_controller()->HandleCycleWindow(
WindowCycleController::FORWARD);
}
void HandleActivateDesk(const ui::Accelerator& accelerator) {
auto* desks_controller = DesksController::Get();
const bool success = desks_controller->ActivateAdjacentDesk(
/*going_left=*/
(accelerator.key_code() == ui::VKEY_OEM_4 ||
accelerator.key_code() == ui::VKEY_LEFT),
DesksSwitchSource::kDeskSwitchShortcut);
if (!success)
return;
switch (accelerator.key_code()) {
case ui::VKEY_OEM_4:
case ui::VKEY_LEFT:
base::RecordAction(base::UserMetricsAction("Accel_Desks_ActivateLeft"));
break;
case ui::VKEY_OEM_6:
case ui::VKEY_RIGHT:
base::RecordAction(base::UserMetricsAction("Accel_Desks_ActivateRight"));
break;
default:
NOTREACHED();
}
}
void HandleMoveActiveItem(const ui::Accelerator& accelerator) {
auto* desks_controller = DesksController::Get();
if (desks_controller->AreDesksBeingModified())
return;
aura::Window* window_to_move = nullptr;
auto* overview_controller = Shell::Get()->overview_controller();
const bool in_overview = overview_controller->InOverviewSession();
if (in_overview) {
window_to_move =
overview_controller->overview_session()->GetHighlightedWindow();
} else {
window_to_move = window_util::GetActiveWindow();
}
if (!window_to_move)
return;
Desk* target_desk = nullptr;
bool going_left = accelerator.key_code() == ui::VKEY_OEM_4 ||
accelerator.key_code() == ui::VKEY_LEFT;
if (going_left) {
target_desk = desks_controller->GetPreviousDesk();
base::RecordAction(base::UserMetricsAction("Accel_Desks_MoveWindowLeft"));
} else {
DCHECK(accelerator.key_code() == ui::VKEY_OEM_6 ||
accelerator.key_code() == ui::VKEY_RIGHT);
target_desk = desks_controller->GetNextDesk();
base::RecordAction(base::UserMetricsAction("Accel_Desks_MoveWindowRight"));
}
if (!target_desk)
return;
if (!in_overview) {
desks_animations::PerformWindowMoveToDeskAnimation(
window_to_move,
/*going_left=*/going_left);
}
if (!desks_controller->MoveWindowFromActiveDeskTo(
window_to_move, target_desk, window_to_move->GetRootWindow(),
DesksMoveWindowFromActiveDeskSource::kShortcut)) {
return;
}
if (in_overview) {
// We should not exit overview as a result of this shortcut.
DCHECK(overview_controller->InOverviewSession());
overview_controller->overview_session()->PositionWindows(/*animate=*/true);
}
}
void HandleNewDesk() {
auto* desks_controller = DesksController::Get();
if (!desks_controller->CanCreateDesks()) {
ShowToast(kVirtualDesksToastId,
l10n_util::GetStringUTF16(IDS_ASH_DESKS_MAX_NUM_REACHED));
return;
}
if (desks_controller->AreDesksBeingModified())
return;
// Add a new desk and switch to it.
const size_t new_desk_index = desks_controller->desks().size();
desks_controller->NewDesk(DesksCreationRemovalSource::kKeyboard);
const Desk* desk = desks_controller->desks()[new_desk_index].get();
desks_controller->ActivateDesk(desk, DesksSwitchSource::kNewDeskShortcut);
base::RecordAction(base::UserMetricsAction("Accel_Desks_NewDesk"));
}
void HandleRemoveCurrentDesk() {
if (window_util::IsAnyWindowDragged())
return;
auto* desks_controller = DesksController::Get();
if (!desks_controller->CanRemoveDesks()) {
ShowToast(kVirtualDesksToastId,
l10n_util::GetStringUTF16(IDS_ASH_DESKS_MIN_NUM_REACHED));
return;
}
if (desks_controller->AreDesksBeingModified())
return;
// TODO(afakhry): Finalize the desk removal animation outside of overview with
// UX. https://crbug.com/977434.
desks_controller->RemoveDesk(desks_controller->active_desk(),
DesksCreationRemovalSource::kKeyboard);
base::RecordAction(base::UserMetricsAction("Accel_Desks_RemoveDesk"));
}
void HandleRotatePaneFocus(FocusCycler::Direction direction) {
switch (direction) {
// TODO(stevet): Not sure if this is the same as IDC_FOCUS_NEXT_PANE.
case FocusCycler::FORWARD: {
base::RecordAction(UserMetricsAction("Accel_Focus_Next_Pane"));
break;
}
case FocusCycler::BACKWARD: {
base::RecordAction(UserMetricsAction("Accel_Focus_Previous_Pane"));
break;
}
}
Shell::Get()->focus_cycler()->RotateFocus(direction);
}
void HandleFocusShelf() {
base::RecordAction(UserMetricsAction("Accel_Focus_Shelf"));
// TODO(jamescook): Should this be GetRootWindowForNewWindows()?
// Focus the home button.
Shelf* shelf = Shelf::ForWindow(Shell::GetPrimaryRootWindow());
shelf->shelf_focus_cycler()->FocusNavigation(false /* lastElement */);
}
views::Widget* FindPipWidget() {
return Shell::Get()->focus_cycler()->FindWidget(
base::BindRepeating([](views::Widget* widget) {
return WindowState::Get(widget->GetNativeWindow())->IsPip();
}));
}
void HandleFocusPip() {
base::RecordAction(UserMetricsAction("Accel_Focus_Pip"));
auto* widget = FindPipWidget();
if (widget)
Shell::Get()->focus_cycler()->FocusWidget(widget);
}
void HandleLaunchAppN(int n) {
base::RecordAction(UserMetricsAction("Accel_Launch_App"));
Shelf::LaunchShelfItem(n);
}
void HandleLaunchLastApp() {
base::RecordAction(UserMetricsAction("Accel_Launch_Last_App"));
Shelf::LaunchShelfItem(-1);
}
void HandleMediaNextTrack() {
base::RecordAction(UserMetricsAction("Accel_Media_Next_Track"));
Shell::Get()->media_controller()->HandleMediaNextTrack();
}
void HandleMediaFastForward() {
base::RecordAction(UserMetricsAction("Accel_Media_Fast_Forward"));
Shell::Get()->media_controller()->HandleMediaSeekForward();
}
void HandleMediaPause() {
base::RecordAction(UserMetricsAction("Accel_Media_Pause"));
Shell::Get()->media_controller()->HandleMediaPause();
}
void HandleMediaPlay() {
base::RecordAction(UserMetricsAction("Accel_Media_Play"));
Shell::Get()->media_controller()->HandleMediaPlay();
}
void HandleMediaPlayPause() {
base::RecordAction(UserMetricsAction("Accel_Media_PlayPause"));
Shell::Get()->media_controller()->HandleMediaPlayPause();
}
void HandleMediaPrevTrack() {
base::RecordAction(UserMetricsAction("Accel_Media_Prev_Track"));
Shell::Get()->media_controller()->HandleMediaPrevTrack();
}
void HandleMediaRewind() {
base::RecordAction(UserMetricsAction("Accel_Media_Rewind"));
Shell::Get()->media_controller()->HandleMediaSeekBackward();
}
void HandleMediaStop() {
base::RecordAction(UserMetricsAction("Accel_Media_Stop"));
Shell::Get()->media_controller()->HandleMediaStop();
}
void HandleToggleMirrorMode() {
base::RecordAction(UserMetricsAction("Accel_Toggle_Mirror_Mode"));
bool mirror = !Shell::Get()->display_manager()->IsInMirrorMode();
Shell::Get()->display_configuration_controller()->SetMirrorMode(
mirror, true /* throttle */);
}
bool CanHandleNewIncognitoWindow() {
// Guest mode does not use incognito windows. The browser may have other
// restrictions on incognito mode (e.g. enterprise policy) but those are rare.
// For non-guest mode, consume the key and defer the decision to the browser.
base::Optional<user_manager::UserType> user_type =
Shell::Get()->session_controller()->GetUserType();
return user_type && *user_type != user_manager::USER_TYPE_GUEST;
}
void HandleNewIncognitoWindow() {
base::RecordAction(UserMetricsAction("Accel_New_Incognito_Window"));
NewWindowDelegate::GetInstance()->NewWindow(true /* is_incognito */);
}
void HandleNewTab(const ui::Accelerator& accelerator) {
if (accelerator.key_code() == ui::VKEY_T)
base::RecordAction(UserMetricsAction("Accel_NewTab_T"));
NewWindowDelegate::GetInstance()->NewTab();
}
void HandleNewWindow() {
base::RecordAction(UserMetricsAction("Accel_New_Window"));
NewWindowDelegate::GetInstance()->NewWindow(false /* is_incognito */);
}
bool CanCycleInputMethod() {
return Shell::Get()->ime_controller()->CanSwitchIme();
}
bool CanHandleCycleMru(const ui::Accelerator& accelerator) {
// Don't do anything when Alt+Tab is hit while a virtual keyboard is showing.
// Touchscreen users have better window switching options. It would be
// preferable if we could tell whether this event actually came from a virtual
// keyboard, but there's no easy way to do so, thus we block Alt+Tab when the
// virtual keyboard is showing, even if it came from a real keyboard. See
// http://crbug.com/638269
return !keyboard::KeyboardUIController::Get()->IsKeyboardVisible();
}
void HandleSwitchToNextIme(const ui::Accelerator& accelerator) {
base::RecordAction(UserMetricsAction("Accel_Next_Ime"));
if (accelerator.key_code() == ui::VKEY_MODECHANGE)
RecordImeSwitchByModeChangeKey();
else
RecordImeSwitchByAccelerator();
Shell::Get()->ime_controller()->SwitchToNextIme();
}
void HandleOpenFeedbackPage() {
base::RecordAction(UserMetricsAction("Accel_Open_Feedback_Page"));
NewWindowDelegate::GetInstance()->OpenFeedbackPage();
}
void HandleSwitchToLastUsedIme(const ui::Accelerator& accelerator) {
base::RecordAction(UserMetricsAction("Accel_Previous_Ime"));
if (accelerator.key_state() == ui::Accelerator::KeyState::PRESSED) {
RecordImeSwitchByAccelerator();
Shell::Get()->ime_controller()->SwitchToLastUsedIme();
}
// Else: consume the Ctrl+Space ET_KEY_RELEASED event but do not do anything.
}
display::Display::Rotation GetNextRotationInClamshell(
display::Display::Rotation current) {
switch (current) {
case display::Display::ROTATE_0:
return display::Display::ROTATE_90;
case display::Display::ROTATE_90:
return display::Display::ROTATE_180;
case display::Display::ROTATE_180:
return display::Display::ROTATE_270;
case display::Display::ROTATE_270:
return display::Display::ROTATE_0;
}
NOTREACHED() << "Unknown rotation:" << current;
return display::Display::ROTATE_0;
}
display::Display::Rotation GetNextRotationInTabletMode(
int64_t display_id,
display::Display::Rotation current) {
Shell* shell = Shell::Get();
DCHECK(shell->tablet_mode_controller()->InTabletMode());
if (!display::Display::HasInternalDisplay() ||
display_id != display::Display::InternalDisplayId()) {
return GetNextRotationInClamshell(current);
}
const OrientationLockType app_requested_lock =
shell->screen_orientation_controller()
->GetCurrentAppRequestedOrientationLock();
bool add_180_degrees = false;
switch (app_requested_lock) {
case OrientationLockType::kCurrent:
case OrientationLockType::kLandscapePrimary:
case OrientationLockType::kLandscapeSecondary:
case OrientationLockType::kPortraitPrimary:
case OrientationLockType::kPortraitSecondary:
case OrientationLockType::kNatural:
// Do not change the current orientation.
return current;
case OrientationLockType::kLandscape:
case OrientationLockType::kPortrait:
// App allows both primary and secondary orientations in either landscape
// or portrait, therefore switch to the next one by adding 180 degrees.
add_180_degrees = true;
break;
default:
break;
}
switch (current) {
case display::Display::ROTATE_0:
return add_180_degrees ? display::Display::ROTATE_180
: display::Display::ROTATE_90;
case display::Display::ROTATE_90:
return add_180_degrees ? display::Display::ROTATE_270
: display::Display::ROTATE_180;
case display::Display::ROTATE_180:
return add_180_degrees ? display::Display::ROTATE_0
: display::Display::ROTATE_270;
case display::Display::ROTATE_270:
return add_180_degrees ? display::Display::ROTATE_90
: display::Display::ROTATE_0;
}
NOTREACHED() << "Unknown rotation:" << current;
return display::Display::ROTATE_0;
}
bool ShouldLockRotation(int64_t display_id) {
return display::Display::HasInternalDisplay() &&
display_id == display::Display::InternalDisplayId() &&
Shell::Get()->tablet_mode_controller()->is_in_tablet_physical_state();
}
int64_t GetDisplayIdForRotation() {
const gfx::Point point = display::Screen::GetScreen()->GetCursorScreenPoint();
return display::Screen::GetScreen()->GetDisplayNearestPoint(point).id();
}
void RotateScreen() {
auto* shell = Shell::Get();
const bool in_tablet_mode =
Shell::Get()->tablet_mode_controller()->InTabletMode();
const int64_t display_id = GetDisplayIdForRotation();
const display::ManagedDisplayInfo& display_info =
shell->display_manager()->GetDisplayInfo(display_id);
const auto active_rotation = display_info.GetActiveRotation();
const auto next_rotation =
in_tablet_mode ? GetNextRotationInTabletMode(display_id, active_rotation)
: GetNextRotationInClamshell(active_rotation);
if (active_rotation == next_rotation)
return;
// When the device is in a physical tablet state, display rotation requests of
// the internal display are treated as requests to lock the user rotation.
if (ShouldLockRotation(display_id)) {
shell->screen_orientation_controller()->SetLockToRotation(next_rotation);
return;
}
shell->display_configuration_controller()->SetDisplayRotation(
display_id, next_rotation, display::Display::RotationSource::USER);
}
void OnRotationDialogAccepted() {
RecordRotationAcceleratorAction(RotationAcceleratorAction::kAcceptedDialog);
RotateScreen();
Shell::Get()
->accessibility_controller()
->SetDisplayRotationAcceleratorDialogBeenAccepted();
}
void OnRotationDialogCancelled() {
RecordRotationAcceleratorAction(RotationAcceleratorAction::kCancelledDialog);
}
// Rotates the screen.
void HandleRotateScreen() {
if (Shell::Get()->display_manager()->IsInUnifiedMode())
return;
base::RecordAction(UserMetricsAction("Accel_Rotate_Screen"));
const bool dialog_ever_accepted =
Shell::Get()
->accessibility_controller()
->HasDisplayRotationAcceleratorDialogBeenAccepted();
if (!dialog_ever_accepted) {
Shell::Get()->accelerator_controller()->MaybeShowConfirmationDialog(
IDS_ASH_ROTATE_SCREEN_TITLE, IDS_ASH_ROTATE_SCREEN_BODY,
base::BindOnce(&OnRotationDialogAccepted),
base::BindOnce(&OnRotationDialogCancelled));
} else {
RecordRotationAcceleratorAction(
RotationAcceleratorAction::kAlreadyAcceptedDialog);
RotateScreen();
}
}
void HandleRestoreTab() {
base::RecordAction(UserMetricsAction("Accel_Restore_Tab"));
NewWindowDelegate::GetInstance()->RestoreTab();
}
// Rotate the active window.
void HandleRotateActiveWindow() {
base::RecordAction(UserMetricsAction("Accel_Rotate_Active_Window"));
aura::Window* active_window = window_util::GetActiveWindow();
if (!active_window)
return;
// 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(
std::make_unique<WindowRotation>(360, active_window->layer())));
}
void HandleShowKeyboardShortcutViewer() {
NewWindowDelegate::GetInstance()->ShowKeyboardShortcutViewer();
}
void HandleTakeWindowScreenshot() {
base::RecordAction(UserMetricsAction("Accel_Take_Window_Screenshot"));
Shell::Get()->screenshot_controller()->StartWindowScreenshotSession();
}
void HandleTakePartialScreenshot() {
base::RecordAction(UserMetricsAction("Accel_Take_Partial_Screenshot"));
Shell::Get()->screenshot_controller()->StartPartialScreenshotSession(
true /* draw_overlay_immediately */);
}
void HandleTakeScreenshot() {
base::RecordAction(UserMetricsAction("Accel_Take_Screenshot"));
Shell::Get()->screenshot_controller()->TakeScreenshotForAllRootWindows();
}
void HandleToggleSystemTrayBubbleInternal(bool focus_message_center) {
aura::Window* target_root = Shell::GetRootWindowForNewWindows();
UnifiedSystemTray* tray = RootWindowController::ForWindow(target_root)
->GetStatusAreaWidget()
->unified_system_tray();
if (tray->IsBubbleShown()) {
tray->CloseBubble();
} else {
tray->ShowBubble(false /* show_by_click */);
tray->ActivateBubble();
if (focus_message_center)
tray->FocusFirstNotification();
}
}
void HandleToggleSystemTrayBubble() {
base::RecordAction(UserMetricsAction("Accel_Toggle_System_Tray_Bubble"));
HandleToggleSystemTrayBubbleInternal(false /*focus_message_center*/);
}
void HandleToggleMessageCenterBubble() {
base::RecordAction(UserMetricsAction("Accel_Toggle_Message_Center_Bubble"));
HandleToggleSystemTrayBubbleInternal(true /*focus_message_center*/);
}
void HandleShowTaskManager() {
base::RecordAction(UserMetricsAction("Accel_Show_Task_Manager"));
NewWindowDelegate::GetInstance()->ShowTaskManager();
}
void HandleSwapPrimaryDisplay() {
base::RecordAction(UserMetricsAction("Accel_Swap_Primary_Display"));
accelerators::ShiftPrimaryDisplay();
}
bool CanHandleSwitchIme(const ui::Accelerator& accelerator) {
return Shell::Get()->ime_controller()->CanSwitchImeWithAccelerator(
accelerator);
}
void HandleSwitchIme(const ui::Accelerator& accelerator) {
base::RecordAction(UserMetricsAction("Accel_Switch_Ime"));
RecordImeSwitchByAccelerator();
Shell::Get()->ime_controller()->SwitchImeWithAccelerator(accelerator);
}
bool CanHandleToggleAppList(const ui::Accelerator& accelerator,
const ui::Accelerator& previous_accelerator) {
if (accelerator.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.
if (previous_accelerator.key_state() !=
ui::Accelerator::KeyState::PRESSED ||
previous_accelerator.key_code() != ui::VKEY_LWIN ||
previous_accelerator.interrupted_by_mouse_event()) {
return false;
}
// 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
if (Shell::Get()->accessibility_controller()->spoken_feedback_enabled())
return false;
}
return true;
}
void HandleToggleAppList(const ui::Accelerator& accelerator,
AppListShowSource show_source) {
if (accelerator.key_code() == ui::VKEY_LWIN)
base::RecordAction(UserMetricsAction("Accel_Search_LWin"));
aura::Window* const root_window = Shell::GetRootWindowForNewWindows();
Shell::Get()->app_list_controller()->ToggleAppList(
display::Screen::GetScreen()->GetDisplayNearestWindow(root_window).id(),
show_source, accelerator.time_stamp());
}
void HandleToggleFullscreen(const ui::Accelerator& accelerator) {
if (accelerator.key_code() == ui::VKEY_MEDIA_LAUNCH_APP2)
base::RecordAction(UserMetricsAction("Accel_Fullscreen_F4"));
accelerators::ToggleFullscreen();
}
void HandleToggleOverview() {
base::RecordAction(base::UserMetricsAction("Accel_Overview_F5"));
OverviewController* overview_controller = Shell::Get()->overview_controller();
if (overview_controller->InOverviewSession())
overview_controller->EndOverview();
else
overview_controller->StartOverview();
}
void HandleToggleUnifiedDesktop() {
Shell::Get()->display_manager()->SetUnifiedDesktopEnabled(
!Shell::Get()->display_manager()->unified_desktop_enabled());
}
bool CanHandleWindowSnap() {
aura::Window* active_window = window_util::GetActiveWindow();
if (!active_window)
return false;
WindowState* window_state = WindowState::Get(active_window);
// Disable window snapping shortcut key for full screen window due to
// http://crbug.com/135487.
return (window_state && window_state->IsUserPositionable() &&
!window_state->IsFullscreen());
}
void HandleWindowSnap(AcceleratorAction action) {
Shell* shell = Shell::Get();
const bool in_tablet = shell->tablet_mode_controller()->InTabletMode();
const bool in_overview = shell->overview_controller()->InOverviewSession();
if (action == WINDOW_CYCLE_SNAP_LEFT) {
base::RecordAction(UserMetricsAction("Accel_Window_Snap_Left"));
if (in_tablet) {
RecordWindowSnapAcceleratorAction(
WindowSnapAcceleratorAction::kCycleLeftSnapInTablet);
} else if (in_overview) {
RecordWindowSnapAcceleratorAction(
WindowSnapAcceleratorAction::kCycleLeftSnapInClamshellOverview);
} else {
RecordWindowSnapAcceleratorAction(
WindowSnapAcceleratorAction::kCycleLeftSnapInClamshellNoOverview);
}
} else {
base::RecordAction(UserMetricsAction("Accel_Window_Snap_Right"));
if (in_tablet) {
RecordWindowSnapAcceleratorAction(
WindowSnapAcceleratorAction::kCycleRightSnapInTablet);
} else if (in_overview) {
RecordWindowSnapAcceleratorAction(
WindowSnapAcceleratorAction::kCycleRightSnapInClamshellOverview);
} else {
RecordWindowSnapAcceleratorAction(
WindowSnapAcceleratorAction::kCycleRightSnapInClamshellNoOverview);
}
}
const WMEvent event(action == WINDOW_CYCLE_SNAP_LEFT
? WM_EVENT_CYCLE_SNAP_LEFT
: WM_EVENT_CYCLE_SNAP_RIGHT);
aura::Window* active_window = window_util::GetActiveWindow();
DCHECK(active_window);
WindowState::Get(active_window)->OnWMEvent(&event);
}
void HandleWindowMinimize() {
base::RecordAction(base::UserMetricsAction("Accel_Toggle_Minimized_Minus"));
accelerators::ToggleMinimized();
}
void HandleTopWindowMinimizeOnBack() {
base::RecordAction(
base::UserMetricsAction("Accel_Minimize_Top_Window_On_Back"));
WindowState::Get(window_util::GetTopWindow())->Minimize();
}
void HandleShowImeMenuBubble() {
base::RecordAction(UserMetricsAction("Accel_Show_Ime_Menu_Bubble"));
StatusAreaWidget* status_area_widget =
Shelf::ForWindow(Shell::GetPrimaryRootWindow())->GetStatusAreaWidget();
if (status_area_widget) {
ImeMenuTray* ime_menu_tray = status_area_widget->ime_menu_tray();
if (ime_menu_tray && ime_menu_tray->GetVisible() &&
!ime_menu_tray->GetBubbleView()) {
ime_menu_tray->ShowBubble(false /* show_by_click */);
}
}
}
void HandleCrosh() {
base::RecordAction(UserMetricsAction("Accel_Open_Crosh"));
NewWindowDelegate::GetInstance()->OpenCrosh();
}
bool CanHandleDisableCapsLock(const ui::Accelerator& previous_accelerator) {
ui::KeyboardCode previous_key_code = previous_accelerator.key_code();
if (previous_accelerator.key_state() == ui::Accelerator::KeyState::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;
}
return Shell::Get()->ime_controller()->IsCapsLockEnabled();
}
void HandleDisableCapsLock() {
base::RecordAction(UserMetricsAction("Accel_Disable_Caps_Lock"));
Shell::Get()->ime_controller()->SetCapsLockEnabled(false);
}
void HandleFileManager() {
base::RecordAction(UserMetricsAction("Accel_Open_File_Manager"));
NewWindowDelegate::GetInstance()->OpenFileManager();
}
void HandleGetHelp() {
NewWindowDelegate::GetInstance()->OpenGetHelp();
}
bool CanHandleLock() {
return Shell::Get()->session_controller()->CanLockScreen();
}
void HandleLock() {
base::RecordAction(UserMetricsAction("Accel_LockScreen_L"));
Shell::Get()->session_controller()->LockScreen();
}
PaletteTray* GetPaletteTray() {
return Shelf::ForWindow(Shell::GetRootWindowForNewWindows())
->GetStatusAreaWidget()
->palette_tray();
}
void HandleShowStylusTools() {
base::RecordAction(UserMetricsAction("Accel_Show_Stylus_Tools"));
GetPaletteTray()->ShowBubble(false /* show_by_click */);
}
bool CanHandleShowStylusTools() {
return GetPaletteTray()->ShouldShowPalette();
}
bool CanHandleStartAmbientMode() {
return chromeos::features::IsAmbientModeEnabled();
}
void HandleToggleAmbientMode(const ui::Accelerator& accelerator) {
Shell::Get()->ambient_controller()->Toggle();
}
void HandleToggleAssistant(const ui::Accelerator& accelerator) {
if (accelerator.IsCmdDown() && accelerator.key_code() == ui::VKEY_SPACE) {
base::RecordAction(
base::UserMetricsAction("VoiceInteraction.Started.Search_Space"));
} else if (accelerator.IsCmdDown() && accelerator.key_code() == ui::VKEY_A) {
// Search+A shortcut is disabled on device with an assistant key.
if (ui::DeviceKeyboardHasAssistantKey())
return;
base::RecordAction(
base::UserMetricsAction("VoiceInteraction.Started.Search_A"));