File: global_keyboard_shortcuts_mac.mm

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (275 lines) | stat: -rw-r--r-- 11,428 bytes parent folder | download | duplicates (5)
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
// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/global_keyboard_shortcuts_mac.h"

#import <AppKit/AppKit.h>
#include <Carbon/Carbon.h>

#include "base/apple/foundation_util.h"
#include "base/check.h"
#include "base/feature_list.h"
#include "base/no_destructor.h"
#include "build/buildflag.h"
#include "chrome/app/chrome_command_ids.h"
#import "chrome/browser/app_controller_mac.h"
#include "chrome/browser/ui/cocoa/accelerators_cocoa.h"
#include "chrome/browser/ui/tabs/features.h"
#include "chrome/browser/ui/ui_features.h"
#include "ui/base/accelerators/accelerator.h"
#include "ui/base/accelerators/platform_accelerator_cocoa.h"
#import "ui/base/cocoa/nsmenu_additions.h"
#import "ui/base/cocoa/nsmenuitem_additions.h"
#include "ui/base/ui_base_features.h"
#include "ui/events/event_constants.h"
#include "ui/events/keycodes/keyboard_code_conversion_mac.h"

namespace {

// Returns a ui::Accelerator given a KeyboardShortcutData.
ui::Accelerator AcceleratorFromShortcut(const KeyboardShortcutData& shortcut) {
  int modifiers = 0;
  if (shortcut.command_key)
    modifiers |= ui::EF_COMMAND_DOWN;
  if (shortcut.shift_key)
    modifiers |= ui::EF_SHIFT_DOWN;
  if (shortcut.cntrl_key)
    modifiers |= ui::EF_CONTROL_DOWN;
  if (shortcut.opt_key)
    modifiers |= ui::EF_ALT_DOWN;

  return ui::Accelerator(ui::KeyboardCodeFromKeyCode(shortcut.vkey_code),
                         modifiers);
}

int MenuCommandForKeyEvent(NSEvent* event) {
  NSMenuItem* item = [[NSApp mainMenu] cr_menuItemForKeyEquivalentEvent:event];

  if (!item)
    return NO_COMMAND;

  if ([item action] == @selector(commandDispatch:) && [item tag] > 0)
    return [item tag];

  // "Close window", "Quit", and other commands don't use the `commandDispatch:`
  // mechanism. Menu items that do not correspond to IDC_ constants need no
  // special treatment however, as they can't be reserved in
  // |BrowserCommandController::IsReservedCommandOrKey()| anyhow.
  SEL itemAction = [item action];

  if (itemAction == @selector(performClose:))
    return IDC_CLOSE_WINDOW;

  if (itemAction == @selector(terminate:))
    return IDC_EXIT;

  return NO_COMMAND;
}

bool MatchesEventForKeyboardShortcut(const KeyboardShortcutData& shortcut,
                                     bool command_key,
                                     bool shift_key,
                                     bool cntrl_key,
                                     bool opt_key,
                                     int vkey_code) {
  return shortcut.command_key == command_key &&
         shortcut.shift_key == shift_key && shortcut.cntrl_key == cntrl_key &&
         shortcut.opt_key == opt_key && shortcut.vkey_code == vkey_code;
}

const std::vector<KeyboardShortcutData>&
GetDelayedShortcutsNotPresentInMainMenu() {
  // clang-format off
  static base::NoDestructor<std::vector<KeyboardShortcutData>> keys({
    // cmd    shift  cntrl  option vkeycode               command
    // ---    -----  -----  ------ --------               -------
      {true,  false, false, false, kVK_LeftArrow,         IDC_BACK},
      {true,  false, false, false, kVK_RightArrow,        IDC_FORWARD},
  });
  // clang-format on
  return *keys;
}

CommandForKeyEventResult NoCommand() {
  return {NO_COMMAND, /*from_main_menu=*/false};
}

CommandForKeyEventResult MainMenuCommand(int cmd) {
  return {cmd, /*from_main_menu=*/true};
}

CommandForKeyEventResult ShortcutCommand(int cmd) {
  return {cmd, /*from_main_menu=*/false};
}

}  // namespace

// Returns a vector of hidden keyboard shortcuts (i.e. ones that arent present
// in the menus). Note that the hidden "Cmd =" shortcut is somehow enabled by
// the ui::VKEY_OEM_PLUS entry in accelerators_cocoa.mm.
const std::vector<KeyboardShortcutData>& GetShortcutsNotPresentInMainMenu() {
  static const base::NoDestructor<std::vector<KeyboardShortcutData>> keys([]() {
    // clang-format off
    std::vector<KeyboardShortcutData> keys({
    // cmd    shift  cntrl  option vkeycode               command
    // ---    -----  -----  ------ --------               -------
      {true,  true,  false, false, kVK_ANSI_RightBracket, IDC_SELECT_NEXT_TAB},
      {true,  true,  false, false, kVK_ANSI_LeftBracket,  IDC_SELECT_PREVIOUS_TAB},
      {false, false, true,  false, kVK_PageDown,          IDC_SELECT_NEXT_TAB},
      {false, false, true,  false, kVK_PageUp,            IDC_SELECT_PREVIOUS_TAB},
      {true,  false, false, true,  kVK_RightArrow,        IDC_SELECT_NEXT_TAB},
      {true,  false, false, true,  kVK_LeftArrow,         IDC_SELECT_PREVIOUS_TAB},
      {false, true,  true,  false, kVK_PageDown,          IDC_MOVE_TAB_NEXT},
      {false, true,  true,  false, kVK_PageUp,            IDC_MOVE_TAB_PREVIOUS},

      // Cmd-0..8 select the nth tab, with cmd-9 being "last tab".
      {true,  false, false, false, kVK_ANSI_1,            IDC_SELECT_TAB_0},
      {true,  false, false, false, kVK_ANSI_Keypad1,      IDC_SELECT_TAB_0},
      {true,  false, false, false, kVK_ANSI_2,            IDC_SELECT_TAB_1},
      {true,  false, false, false, kVK_ANSI_Keypad2,      IDC_SELECT_TAB_1},
      {true,  false, false, false, kVK_ANSI_3,            IDC_SELECT_TAB_2},
      {true,  false, false, false, kVK_ANSI_Keypad3,      IDC_SELECT_TAB_2},
      {true,  false, false, false, kVK_ANSI_4,            IDC_SELECT_TAB_3},
      {true,  false, false, false, kVK_ANSI_Keypad4,      IDC_SELECT_TAB_3},
      {true,  false, false, false, kVK_ANSI_5,            IDC_SELECT_TAB_4},
      {true,  false, false, false, kVK_ANSI_Keypad5,      IDC_SELECT_TAB_4},
      {true,  false, false, false, kVK_ANSI_6,            IDC_SELECT_TAB_5},
      {true,  false, false, false, kVK_ANSI_Keypad6,      IDC_SELECT_TAB_5},
      {true,  false, false, false, kVK_ANSI_7,            IDC_SELECT_TAB_6},
      {true,  false, false, false, kVK_ANSI_Keypad7,      IDC_SELECT_TAB_6},
      {true,  false, false, false, kVK_ANSI_8,            IDC_SELECT_TAB_7},
      {true,  false, false, false, kVK_ANSI_Keypad8,      IDC_SELECT_TAB_7},
      {true,  false, false, false, kVK_ANSI_9,            IDC_SELECT_LAST_TAB},
      {true,  false, false, false, kVK_ANSI_Keypad9,      IDC_SELECT_LAST_TAB},

      {true,  true,  false, false, kVK_ANSI_M,            IDC_SHOW_AVATAR_MENU},
      {true,  false, false, true,  kVK_ANSI_L,            IDC_SHOW_DOWNLOADS},
      {true,  true,  false, false, kVK_ANSI_C,            IDC_DEV_TOOLS_INSPECT},
      {true,  false, false, true,  kVK_ANSI_C,            IDC_DEV_TOOLS_INSPECT},
      {true,  false, false, true,  kVK_DownArrow,         IDC_FOCUS_NEXT_PANE},
      {true,  false, false, true,  kVK_UpArrow,           IDC_FOCUS_PREVIOUS_PANE},
      {true,  true,  false, true,  kVK_ANSI_A,            IDC_FOCUS_INACTIVE_POPUP_FOR_ACCESSIBILITY},
    });
    // clang-format on

    if (tabs::AreTabGroupShortcutsEnabled()) {
      keys.push_back(
          {true, false, true, false, kVK_ANSI_C, IDC_ADD_NEW_TAB_TO_GROUP});
      keys.push_back(
          {true, false, true, false, kVK_ANSI_P, IDC_CREATE_NEW_TAB_GROUP});
      keys.push_back(
          {true, false, true, false, kVK_ANSI_W, IDC_CLOSE_TAB_GROUP});
      keys.push_back(
          {true, false, true, false, kVK_ANSI_X, IDC_FOCUS_NEXT_TAB_GROUP});
      keys.push_back(
          {true, false, true, false, kVK_ANSI_Z, IDC_FOCUS_PREV_TAB_GROUP});
    }

    if (base::FeatureList::IsEnabled(features::kUIDebugTools)) {
      keys.push_back(
          {false, true, true, true, kVK_ANSI_T, IDC_DEBUG_TOGGLE_TABLET_MODE});
      keys.push_back(
          {false, true, true, true, kVK_ANSI_V, IDC_DEBUG_PRINT_VIEW_TREE});
      keys.push_back({false, true, true, true, kVK_ANSI_M,
                      IDC_DEBUG_PRINT_VIEW_TREE_DETAILS});
    }
    return keys;
  }());
  return *keys;
}

const std::vector<NSMenuItem*>& GetMenuItemsNotPresentInMainMenu() {
  static base::NoDestructor<std::vector<NSMenuItem*>> menu_items([]() {
    std::vector<NSMenuItem*> menu_items;
    for (const auto& shortcut : GetShortcutsNotPresentInMainMenu()) {
      ui::Accelerator accelerator = AcceleratorFromShortcut(shortcut);
      KeyEquivalentAndModifierMask* equivalent =
          ui::GetKeyEquivalentAndModifierMaskFromAccelerator(accelerator);

      // Intentionally leaked!
      NSMenuItem* item =
          [[NSMenuItem alloc] initWithTitle:@""
                                     action:nullptr
                              keyEquivalent:equivalent.keyEquivalent];
      item.keyEquivalentModifierMask = equivalent.modifierMask;

      // We store the command in the tag.
      item.tag = shortcut.chrome_command;
      menu_items.push_back(item);
    }
    return menu_items;
  }());
  return *menu_items;
}

CommandForKeyEventResult CommandForKeyEvent(NSEvent* event) {
  DCHECK(event);
  if ([event type] != NSEventTypeKeyDown)
    return NoCommand();

  int cmdNum = MenuCommandForKeyEvent(event);
  if (cmdNum != NO_COMMAND)
    return MainMenuCommand(cmdNum);

  // Scan through keycodes and see if it corresponds to one of the non-menu
  // shortcuts.
  for (NSMenuItem* menu_item : GetMenuItemsNotPresentInMainMenu()) {
    if ([menu_item cr_firesForKeyEquivalentEvent:event])
      return ShortcutCommand(menu_item.tag);
  }

  return NoCommand();
}

int DelayedWebContentsCommandForKeyEvent(NSEvent* event) {
  DCHECK(event);
  if ([event type] != NSEventTypeKeyDown)
    return NO_COMMAND;

  // Look in secondary keyboard shortcuts.
  NSUInteger modifiers = [event modifierFlags];
  const bool cmdKey = (modifiers & NSEventModifierFlagCommand) != 0;
  const bool shiftKey = (modifiers & NSEventModifierFlagShift) != 0;
  const bool cntrlKey = (modifiers & NSEventModifierFlagControl) != 0;
  const bool optKey = (modifiers & NSEventModifierFlagOption) != 0;
  const int keyCode = [event keyCode];

  // Scan through keycodes and see if it corresponds to one of the non-menu
  // shortcuts.
  for (const auto& shortcut : GetDelayedShortcutsNotPresentInMainMenu()) {
    if (MatchesEventForKeyboardShortcut(shortcut, cmdKey, shiftKey, cntrlKey,
                                        optKey, keyCode)) {
      return shortcut.chrome_command;
    }
  }

  return NO_COMMAND;
}

// AppKit sends an event via performKeyEquivalent: if it has at least one of the
// command or control modifiers, and is an NSEventTypeKeyDown event.
// CommandDispatcher supplements this by also sending event with the option
// modifier to performKeyEquivalent:.
bool EventUsesPerformKeyEquivalent(NSEvent* event) {
  return ([event modifierFlags] & ui::cocoa::ModifierMaskForKeyEvent(event)) !=
         0;
}

bool GetDefaultMacAcceleratorForCommandId(int command_id,
                                          ui::Accelerator* accelerator) {
  // See if it corresponds to one of the non-menu shortcuts.
  for (const auto& shortcut : GetShortcutsNotPresentInMainMenu()) {
    if (shortcut.chrome_command == command_id) {
      *accelerator = AcceleratorFromShortcut(shortcut);
      return true;
    }
  }

  // See if it corresponds to one of the default NSMenu keyEquivalents.
  const ui::Accelerator* default_nsmenu_equivalent =
      AcceleratorsCocoa::GetInstance()->GetAcceleratorForCommand(command_id);
  if (default_nsmenu_equivalent)
    *accelerator = *default_nsmenu_equivalent;
  return default_nsmenu_equivalent != nullptr;
}