OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import "ios/chrome/browser/ui/key_commands_provider.h" | 5 #import "ios/chrome/browser/ui/key_commands_provider.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "components/strings/grit/components_strings.h" | 8 #include "components/strings/grit/components_strings.h" |
9 #import "ios/chrome/browser/ui/commands/generic_chrome_command.h" | 9 #import "ios/chrome/browser/ui/commands/generic_chrome_command.h" |
10 #include "ios/chrome/browser/ui/commands/ios_command_ids.h" | 10 #include "ios/chrome/browser/ui/commands/ios_command_ids.h" |
(...skipping 21 matching lines...) Expand all Loading... |
32 }; | 32 }; |
33 | 33 |
34 // Block to have the tab model open the tab at |index|, if there is one. | 34 // Block to have the tab model open the tab at |index|, if there is one. |
35 void (^focusTab)(NSUInteger) = ^(NSUInteger index) { | 35 void (^focusTab)(NSUInteger) = ^(NSUInteger index) { |
36 [weakConsumer focusTabAtIndex:index]; | 36 [weakConsumer focusTabAtIndex:index]; |
37 }; | 37 }; |
38 | 38 |
39 const BOOL hasTabs = [consumer tabsCount] > 0; | 39 const BOOL hasTabs = [consumer tabsCount] > 0; |
40 | 40 |
41 const BOOL useRTLLayout = UseRTLLayout(); | 41 const BOOL useRTLLayout = UseRTLLayout(); |
42 const NSInteger browseLeft = useRTLLayout ? IDC_FORWARD : IDC_BACK; | 42 |
43 const NSInteger browseRight = useRTLLayout ? IDC_BACK : IDC_FORWARD; | 43 // Blocks for navigating forward/back. |
| 44 void (^browseLeft)(); |
| 45 void (^browseRight)(); |
| 46 if (useRTLLayout) { |
| 47 browseLeft = [^{ |
| 48 if ([weakConsumer canGoForward]) |
| 49 [weakDispatcher goForward]; |
| 50 } copy]; |
| 51 browseRight = [^{ |
| 52 if ([weakConsumer canGoBack]) |
| 53 [weakDispatcher goBack]; |
| 54 } copy]; |
| 55 } else { |
| 56 browseLeft = [^{ |
| 57 if ([weakConsumer canGoBack]) |
| 58 [weakDispatcher goBack]; |
| 59 } copy]; |
| 60 browseRight = [^{ |
| 61 if ([weakConsumer canGoForward]) |
| 62 [weakDispatcher goForward]; |
| 63 } copy]; |
| 64 } |
| 65 |
44 const int browseLeftDescriptionID = useRTLLayout | 66 const int browseLeftDescriptionID = useRTLLayout |
45 ? IDS_IOS_KEYBOARD_HISTORY_FORWARD | 67 ? IDS_IOS_KEYBOARD_HISTORY_FORWARD |
46 : IDS_IOS_KEYBOARD_HISTORY_BACK; | 68 : IDS_IOS_KEYBOARD_HISTORY_BACK; |
47 const int browseRightDescriptionID = useRTLLayout | 69 const int browseRightDescriptionID = useRTLLayout |
48 ? IDS_IOS_KEYBOARD_HISTORY_BACK | 70 ? IDS_IOS_KEYBOARD_HISTORY_BACK |
49 : IDS_IOS_KEYBOARD_HISTORY_FORWARD; | 71 : IDS_IOS_KEYBOARD_HISTORY_FORWARD; |
50 BOOL (^canBrowseLeft)() = ^() { | |
51 return useRTLLayout ? [weakConsumer canGoForward] | |
52 : [weakConsumer canGoBack]; | |
53 }; | |
54 BOOL (^canBrowseRight)() = ^() { | |
55 return useRTLLayout ? [weakConsumer canGoBack] | |
56 : [weakConsumer canGoForward]; | |
57 }; | |
58 | 72 |
59 // Initialize the array of commands with an estimated capacity. | 73 // Initialize the array of commands with an estimated capacity. |
60 NSMutableArray* keyCommands = [NSMutableArray arrayWithCapacity:32]; | 74 NSMutableArray* keyCommands = [NSMutableArray arrayWithCapacity:32]; |
61 | 75 |
62 // List the commands that always appear in the HUD. They appear in the HUD | 76 // List the commands that always appear in the HUD. They appear in the HUD |
63 // since they have titles. | 77 // since they have titles. |
64 [keyCommands addObjectsFromArray:@[ | 78 [keyCommands addObjectsFromArray:@[ |
65 [UIKeyCommand cr_keyCommandWithInput:@"t" | 79 [UIKeyCommand cr_keyCommandWithInput:@"t" |
66 modifierFlags:UIKeyModifierCommand | 80 modifierFlags:UIKeyModifierCommand |
67 title:l10n_util::GetNSStringWithFixup( | 81 title:l10n_util::GetNSStringWithFixup( |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 | 162 |
149 // Since cmd+left and cmd+right are valid system shortcuts when editing | 163 // Since cmd+left and cmd+right are valid system shortcuts when editing |
150 // text, don't register those if text is being edited. | 164 // text, don't register those if text is being edited. |
151 if (!editingText) { | 165 if (!editingText) { |
152 [keyCommands addObjectsFromArray:@[ | 166 [keyCommands addObjectsFromArray:@[ |
153 [UIKeyCommand cr_keyCommandWithInput:UIKeyInputLeftArrow | 167 [UIKeyCommand cr_keyCommandWithInput:UIKeyInputLeftArrow |
154 modifierFlags:UIKeyModifierCommand | 168 modifierFlags:UIKeyModifierCommand |
155 title:l10n_util::GetNSStringWithFixup( | 169 title:l10n_util::GetNSStringWithFixup( |
156 browseLeftDescriptionID) | 170 browseLeftDescriptionID) |
157 action:^{ | 171 action:^{ |
158 if (canBrowseLeft()) { | 172 browseLeft(); |
159 execute(browseLeft); | |
160 } | |
161 }], | 173 }], |
162 [UIKeyCommand cr_keyCommandWithInput:UIKeyInputRightArrow | 174 [UIKeyCommand cr_keyCommandWithInput:UIKeyInputRightArrow |
163 modifierFlags:UIKeyModifierCommand | 175 modifierFlags:UIKeyModifierCommand |
164 title:l10n_util::GetNSStringWithFixup( | 176 title:l10n_util::GetNSStringWithFixup( |
165 browseRightDescriptionID) | 177 browseRightDescriptionID) |
166 action:^{ | 178 action:^{ |
167 if (canBrowseRight()) { | 179 browseRight(); |
168 execute(browseRight); | |
169 } | |
170 }], | 180 }], |
171 ]]; | 181 ]]; |
172 } | 182 } |
173 | 183 |
174 NSString* voiceSearchTitle = l10n_util::GetNSStringWithFixup( | 184 NSString* voiceSearchTitle = l10n_util::GetNSStringWithFixup( |
175 IDS_IOS_VOICE_SEARCH_KEYBOARD_DISCOVERY_TITLE); | 185 IDS_IOS_VOICE_SEARCH_KEYBOARD_DISCOVERY_TITLE); |
176 [keyCommands addObjectsFromArray:@[ | 186 [keyCommands addObjectsFromArray:@[ |
177 [UIKeyCommand cr_keyCommandWithInput:@"y" | 187 [UIKeyCommand cr_keyCommandWithInput:@"y" |
178 modifierFlags:UIKeyModifierCommand | 188 modifierFlags:UIKeyModifierCommand |
179 title:l10n_util::GetNSStringWithFixup( | 189 title:l10n_util::GetNSStringWithFixup( |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 ]]; | 228 ]]; |
219 | 229 |
220 // List the commands that don't appear in the HUD and only appear when there | 230 // List the commands that don't appear in the HUD and only appear when there |
221 // is at least a tab. | 231 // is at least a tab. |
222 if (hasTabs) { | 232 if (hasTabs) { |
223 [keyCommands addObjectsFromArray:@[ | 233 [keyCommands addObjectsFromArray:@[ |
224 [UIKeyCommand cr_keyCommandWithInput:@"[" | 234 [UIKeyCommand cr_keyCommandWithInput:@"[" |
225 modifierFlags:UIKeyModifierCommand | 235 modifierFlags:UIKeyModifierCommand |
226 title:nil | 236 title:nil |
227 action:^{ | 237 action:^{ |
228 if (canBrowseLeft()) { | 238 browseLeft(); |
229 execute(browseLeft); | |
230 } | |
231 }], | 239 }], |
232 [UIKeyCommand cr_keyCommandWithInput:@"]" | 240 [UIKeyCommand cr_keyCommandWithInput:@"]" |
233 modifierFlags:UIKeyModifierCommand | 241 modifierFlags:UIKeyModifierCommand |
234 title:nil | 242 title:nil |
235 action:^{ | 243 action:^{ |
236 if (canBrowseRight()) { | 244 browseRight(); |
237 execute(browseRight); | |
238 } | |
239 }], | 245 }], |
240 [UIKeyCommand cr_keyCommandWithInput:@"." | 246 [UIKeyCommand cr_keyCommandWithInput:@"." |
241 modifierFlags:UIKeyModifierCommand | 247 modifierFlags:UIKeyModifierCommand |
242 title:nil | 248 title:nil |
243 action:^{ | 249 action:^{ |
244 execute(IDC_STOP); | 250 execute(IDC_STOP); |
245 }], | 251 }], |
246 [UIKeyCommand cr_keyCommandWithInput:@"?" | 252 [UIKeyCommand cr_keyCommandWithInput:@"?" |
247 modifierFlags:UIKeyModifierCommand | 253 modifierFlags:UIKeyModifierCommand |
248 title:nil | 254 title:nil |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 action:^{ | 323 action:^{ |
318 [weakConsumer focusNextTab]; | 324 [weakConsumer focusNextTab]; |
319 }], | 325 }], |
320 ]]; | 326 ]]; |
321 } | 327 } |
322 | 328 |
323 return keyCommands; | 329 return keyCommands; |
324 } | 330 } |
325 | 331 |
326 @end | 332 @end |
OLD | NEW |