OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "ios/chrome/browser/ui/key_commands_provider.h" |
| 6 |
| 7 #import "base/ios/weak_nsobject.h" |
| 8 #include "base/logging.h" |
| 9 #include "components/strings/grit/components_strings.h" |
| 10 #import "ios/chrome/browser/ui/commands/generic_chrome_command.h" |
| 11 #include "ios/chrome/browser/ui/commands/ios_command_ids.h" |
| 12 #import "ios/chrome/browser/ui/keyboard/UIKeyCommand+Chrome.h" |
| 13 #include "ios/chrome/browser/ui/rtl_geometry.h" |
| 14 #include "ios/chrome/grit/ios_strings.h" |
| 15 #include "ui/base/l10n/l10n_util_mac.h" |
| 16 |
| 17 @implementation KeyCommandsProvider |
| 18 |
| 19 - (NSArray*)keyCommandsForConsumer:(id<KeyCommandsPlumbing>)consumer |
| 20 editingText:(BOOL)editingText { |
| 21 base::WeakNSProtocol<id<KeyCommandsPlumbing>> weakConsumer(consumer); |
| 22 |
| 23 // Block to execute a command from the |tag|. |
| 24 void (^execute)(NSInteger) = [[^(NSInteger tag) { |
| 25 [weakConsumer |
| 26 chromeExecuteCommand:[GenericChromeCommand commandWithTag:tag]]; |
| 27 } copy] autorelease]; |
| 28 |
| 29 // Block to have the tab model open the tab at |index|, if there is one. |
| 30 void (^focusTab)(NSUInteger) = [[^(NSUInteger index) { |
| 31 [weakConsumer focusTabAtIndex:index]; |
| 32 } copy] autorelease]; |
| 33 |
| 34 const BOOL hasTabs = [consumer tabsCount] > 0; |
| 35 |
| 36 const BOOL useRTLLayout = UseRTLLayout(); |
| 37 const NSInteger browseLeft = useRTLLayout ? IDC_FORWARD : IDC_BACK; |
| 38 const NSInteger browseRight = useRTLLayout ? IDC_BACK : IDC_FORWARD; |
| 39 const int browseLeftDescriptionID = useRTLLayout |
| 40 ? IDS_IOS_KEYBOARD_HISTORY_FORWARD |
| 41 : IDS_IOS_KEYBOARD_HISTORY_BACK; |
| 42 const int browseRightDescriptionID = useRTLLayout |
| 43 ? IDS_IOS_KEYBOARD_HISTORY_BACK |
| 44 : IDS_IOS_KEYBOARD_HISTORY_FORWARD; |
| 45 |
| 46 // Initialize the array of commands with an estimated capacity. |
| 47 NSMutableArray* keyCommands = [NSMutableArray arrayWithCapacity:32]; |
| 48 |
| 49 // List the commands that always appear in the HUD. They appear in the HUD |
| 50 // since they have titles. |
| 51 [keyCommands addObjectsFromArray:@[ |
| 52 [UIKeyCommand cr_keyCommandWithInput:@"t" |
| 53 modifierFlags:UIKeyModifierCommand |
| 54 title:l10n_util::GetNSStringWithFixup( |
| 55 IDS_IOS_TOOLS_MENU_NEW_TAB) |
| 56 action:^{ |
| 57 if ([weakConsumer isOffTheRecord]) { |
| 58 execute(IDC_NEW_INCOGNITO_TAB); |
| 59 } else { |
| 60 execute(IDC_NEW_TAB); |
| 61 } |
| 62 }], |
| 63 [UIKeyCommand |
| 64 cr_keyCommandWithInput:@"n" |
| 65 modifierFlags:UIKeyModifierCommand | UIKeyModifierShift |
| 66 title:l10n_util::GetNSStringWithFixup( |
| 67 IDS_IOS_TOOLS_MENU_NEW_INCOGNITO_TAB) |
| 68 action:^{ |
| 69 execute(IDC_NEW_INCOGNITO_TAB); |
| 70 }], |
| 71 [UIKeyCommand |
| 72 cr_keyCommandWithInput:@"t" |
| 73 modifierFlags:UIKeyModifierCommand | UIKeyModifierShift |
| 74 title:l10n_util::GetNSStringWithFixup( |
| 75 IDS_IOS_KEYBOARD_REOPEN_CLOSED_TAB) |
| 76 action:^{ |
| 77 [weakConsumer reopenClosedTab]; |
| 78 }], |
| 79 ]]; |
| 80 |
| 81 // List the commands that only appear when there is at least a tab. When they |
| 82 // appear, they are in the HUD since they have titles. |
| 83 if (hasTabs) { |
| 84 [keyCommands addObjectsFromArray:@[ |
| 85 [UIKeyCommand cr_keyCommandWithInput:@"l" |
| 86 modifierFlags:UIKeyModifierCommand |
| 87 title:l10n_util::GetNSStringWithFixup( |
| 88 IDS_IOS_KEYBOARD_OPEN_LOCATION) |
| 89 action:^{ |
| 90 [weakConsumer focusOmnibox]; |
| 91 }], |
| 92 [UIKeyCommand cr_keyCommandWithInput:@"w" |
| 93 modifierFlags:UIKeyModifierCommand |
| 94 title:l10n_util::GetNSStringWithFixup( |
| 95 IDS_IOS_TOOLS_MENU_CLOSE_TAB) |
| 96 action:^{ |
| 97 execute(IDC_CLOSE_TAB); |
| 98 }], |
| 99 [UIKeyCommand |
| 100 cr_keyCommandWithInput:@"d" |
| 101 modifierFlags:UIKeyModifierCommand |
| 102 title:l10n_util::GetNSStringWithFixup( |
| 103 IDS_IOS_KEYBOARD_BOOKMARK_THIS_PAGE) |
| 104 action:^{ |
| 105 execute(IDC_BOOKMARK_PAGE); |
| 106 }], |
| 107 [UIKeyCommand cr_keyCommandWithInput:@"f" |
| 108 modifierFlags:UIKeyModifierCommand |
| 109 title:l10n_util::GetNSStringWithFixup( |
| 110 IDS_IOS_TOOLS_MENU_FIND_IN_PAGE) |
| 111 action:^{ |
| 112 execute(IDC_FIND); |
| 113 }], |
| 114 [UIKeyCommand cr_keyCommandWithInput:@"g" |
| 115 modifierFlags:UIKeyModifierCommand |
| 116 title:nil |
| 117 action:^{ |
| 118 execute(IDC_FIND_NEXT); |
| 119 }], |
| 120 [UIKeyCommand |
| 121 cr_keyCommandWithInput:@"g" |
| 122 modifierFlags:UIKeyModifierCommand | UIKeyModifierShift |
| 123 title:nil |
| 124 action:^{ |
| 125 execute(IDC_FIND_PREVIOUS); |
| 126 }], |
| 127 [UIKeyCommand cr_keyCommandWithInput:@"r" |
| 128 modifierFlags:UIKeyModifierCommand |
| 129 title:l10n_util::GetNSStringWithFixup( |
| 130 IDS_IOS_ACCNAME_RELOAD) |
| 131 action:^{ |
| 132 execute(IDC_RELOAD); |
| 133 }], |
| 134 ]]; |
| 135 |
| 136 // Since cmd+left and cmd+right are valid system shortcuts when editing |
| 137 // text, don't register those if text is being edited. |
| 138 if (!editingText) { |
| 139 [keyCommands addObjectsFromArray:@[ |
| 140 [UIKeyCommand cr_keyCommandWithInput:UIKeyInputLeftArrow |
| 141 modifierFlags:UIKeyModifierCommand |
| 142 title:l10n_util::GetNSStringWithFixup( |
| 143 browseLeftDescriptionID) |
| 144 action:^{ |
| 145 execute(browseLeft); |
| 146 }], |
| 147 [UIKeyCommand cr_keyCommandWithInput:UIKeyInputRightArrow |
| 148 modifierFlags:UIKeyModifierCommand |
| 149 title:l10n_util::GetNSStringWithFixup( |
| 150 browseRightDescriptionID) |
| 151 action:^{ |
| 152 execute(browseRight); |
| 153 }], |
| 154 ]]; |
| 155 } |
| 156 |
| 157 NSString* voiceSearchTitle = l10n_util::GetNSStringWithFixup( |
| 158 IDS_IOS_VOICE_SEARCH_KEYBOARD_DISCOVERY_TITLE); |
| 159 [keyCommands addObjectsFromArray:@[ |
| 160 [UIKeyCommand cr_keyCommandWithInput:@"y" |
| 161 modifierFlags:UIKeyModifierCommand |
| 162 title:l10n_util::GetNSStringWithFixup( |
| 163 IDS_HISTORY_SHOW_HISTORY) |
| 164 action:^{ |
| 165 execute(IDC_SHOW_HISTORY); |
| 166 }], |
| 167 [UIKeyCommand |
| 168 cr_keyCommandWithInput:@"." |
| 169 modifierFlags:UIKeyModifierCommand | UIKeyModifierShift |
| 170 title:voiceSearchTitle |
| 171 action:^{ |
| 172 execute(IDC_VOICE_SEARCH); |
| 173 }], |
| 174 ]]; |
| 175 } |
| 176 |
| 177 // List the commands that don't appear in the HUD but are always present. |
| 178 [keyCommands addObjectsFromArray:@[ |
| 179 [UIKeyCommand cr_keyCommandWithInput:UIKeyInputEscape |
| 180 modifierFlags:Cr_UIKeyModifierNone |
| 181 title:nil |
| 182 action:^{ |
| 183 execute(IDC_CLOSE_MODALS); |
| 184 }], |
| 185 [UIKeyCommand cr_keyCommandWithInput:@"n" |
| 186 modifierFlags:UIKeyModifierCommand |
| 187 title:nil |
| 188 action:^{ |
| 189 if ([weakConsumer isOffTheRecord]) { |
| 190 execute(IDC_NEW_INCOGNITO_TAB); |
| 191 } else { |
| 192 execute(IDC_NEW_TAB); |
| 193 } |
| 194 }], |
| 195 [UIKeyCommand cr_keyCommandWithInput:@"," |
| 196 modifierFlags:UIKeyModifierCommand |
| 197 title:nil |
| 198 action:^{ |
| 199 execute(IDC_OPTIONS); |
| 200 }], |
| 201 ]]; |
| 202 |
| 203 // List the commands that don't appear in the HUD and only appear when there |
| 204 // is at least a tab. |
| 205 if (hasTabs) { |
| 206 [keyCommands addObjectsFromArray:@[ |
| 207 [UIKeyCommand cr_keyCommandWithInput:@"[" |
| 208 modifierFlags:UIKeyModifierCommand |
| 209 title:nil |
| 210 action:^{ |
| 211 execute(browseLeft); |
| 212 }], |
| 213 [UIKeyCommand cr_keyCommandWithInput:@"]" |
| 214 modifierFlags:UIKeyModifierCommand |
| 215 title:nil |
| 216 action:^{ |
| 217 execute(browseRight); |
| 218 }], |
| 219 [UIKeyCommand cr_keyCommandWithInput:@"." |
| 220 modifierFlags:UIKeyModifierCommand |
| 221 title:nil |
| 222 action:^{ |
| 223 execute(IDC_STOP); |
| 224 }], |
| 225 [UIKeyCommand cr_keyCommandWithInput:@"?" |
| 226 modifierFlags:UIKeyModifierCommand |
| 227 title:nil |
| 228 action:^{ |
| 229 execute(IDC_HELP_PAGE_VIA_MENU); |
| 230 }], |
| 231 [UIKeyCommand cr_keyCommandWithInput:@"1" |
| 232 modifierFlags:UIKeyModifierCommand |
| 233 title:nil |
| 234 action:^{ |
| 235 focusTab(0); |
| 236 }], |
| 237 [UIKeyCommand cr_keyCommandWithInput:@"2" |
| 238 modifierFlags:UIKeyModifierCommand |
| 239 title:nil |
| 240 action:^{ |
| 241 focusTab(1); |
| 242 }], |
| 243 [UIKeyCommand cr_keyCommandWithInput:@"3" |
| 244 modifierFlags:UIKeyModifierCommand |
| 245 title:nil |
| 246 action:^{ |
| 247 focusTab(2); |
| 248 }], |
| 249 [UIKeyCommand cr_keyCommandWithInput:@"4" |
| 250 modifierFlags:UIKeyModifierCommand |
| 251 title:nil |
| 252 action:^{ |
| 253 focusTab(3); |
| 254 }], |
| 255 [UIKeyCommand cr_keyCommandWithInput:@"5" |
| 256 modifierFlags:UIKeyModifierCommand |
| 257 title:nil |
| 258 action:^{ |
| 259 focusTab(4); |
| 260 }], |
| 261 [UIKeyCommand cr_keyCommandWithInput:@"6" |
| 262 modifierFlags:UIKeyModifierCommand |
| 263 title:nil |
| 264 action:^{ |
| 265 focusTab(5); |
| 266 }], |
| 267 [UIKeyCommand cr_keyCommandWithInput:@"7" |
| 268 modifierFlags:UIKeyModifierCommand |
| 269 title:nil |
| 270 action:^{ |
| 271 focusTab(6); |
| 272 }], |
| 273 [UIKeyCommand cr_keyCommandWithInput:@"8" |
| 274 modifierFlags:UIKeyModifierCommand |
| 275 title:nil |
| 276 action:^{ |
| 277 focusTab(7); |
| 278 }], |
| 279 [UIKeyCommand cr_keyCommandWithInput:@"9" |
| 280 modifierFlags:UIKeyModifierCommand |
| 281 title:nil |
| 282 action:^{ |
| 283 focusTab([weakConsumer tabsCount] - 1); |
| 284 }], |
| 285 [UIKeyCommand |
| 286 cr_keyCommandWithInput:UIKeyInputLeftArrow |
| 287 modifierFlags:UIKeyModifierCommand | UIKeyModifierAlternate |
| 288 title:nil |
| 289 action:^{ |
| 290 [weakConsumer focusPreviousTab]; |
| 291 }], |
| 292 [UIKeyCommand |
| 293 cr_keyCommandWithInput:UIKeyInputRightArrow |
| 294 modifierFlags:UIKeyModifierCommand | UIKeyModifierAlternate |
| 295 title:nil |
| 296 action:^{ |
| 297 [weakConsumer focusNextTab]; |
| 298 }], |
| 299 ]]; |
| 300 } |
| 301 |
| 302 return keyCommands; |
| 303 } |
| 304 |
| 305 @end |
OLD | NEW |