1 // Copyright 2015 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.
5 #import "ios/chrome/browser/ui/keyboard/UIKeyCommand+Chrome.h"
7 #import <objc/runtime.h>
9 UIKeyModifierFlags Cr_UIKeyModifierNone = 0;
11 @implementation UIApplication (ChromeKeyCommandHandler)
13 - (void)cr_handleKeyCommand:(UIKeyCommand*)keyCommand {
14 [keyCommand cr_action]();
19 @implementation UIKeyCommand (Chrome)
23 - (UIKeyCommandAction _Nonnull)cr_action {
24 return objc_getAssociatedObject(self, @selector(cr_action));
27 - (void)cr_setAction:(UIKeyCommandAction _Nonnull)action {
28 objc_setAssociatedObject(self, @selector(cr_action), action,
29 OBJC_ASSOCIATION_COPY_NONATOMIC);
32 #pragma mark - Symbolic Description
34 - (NSString*)cr_symbolicDescription {
35 NSMutableString* description = [NSMutableString string];
37 if (self.modifierFlags & UIKeyModifierNumericPad)
38 [description appendString:@"Num lock "];
39 if (self.modifierFlags & UIKeyModifierControl)
40 [description appendString:@"⌃"];
41 if (self.modifierFlags & UIKeyModifierShift)
42 [description appendString:@"⇧"];
43 if (self.modifierFlags & UIKeyModifierAlphaShift)
44 [description appendString:@"⇪"];
45 if (self.modifierFlags & UIKeyModifierAlternate)
46 [description appendString:@"⌥"];
47 if (self.modifierFlags & UIKeyModifierCommand)
48 [description appendString:@"⌘"];
50 if ([self.input isEqualToString:@"\b"])
51 [description appendString:@"⌫"];
52 else if ([self.input isEqualToString:@"\r"])
53 [description appendString:@"↵"];
54 else if ([self.input isEqualToString:@"\t"])
55 [description appendString:@"⇥"];
56 else if ([self.input isEqualToString:UIKeyInputUpArrow])
57 [description appendString:@"↑"];
58 else if ([self.input isEqualToString:UIKeyInputDownArrow])
59 [description appendString:@"↓"];
60 else if ([self.input isEqualToString:UIKeyInputLeftArrow])
61 [description appendString:@"←"];
62 else if ([self.input isEqualToString:UIKeyInputRightArrow])
63 [description appendString:@"→"];
64 else if ([self.input isEqualToString:UIKeyInputEscape])
65 [description appendString:@"⎋"];
66 else if ([self.input isEqualToString:@" "])
67 [description appendString:@"␣"];
69 [description appendString:[self.input uppercaseString]];
73 #pragma mark - Factory
75 + (nonnull instancetype)
76 cr_keyCommandWithInput:(nonnull NSString*)input
77 modifierFlags:(UIKeyModifierFlags)modifierFlags
78 title:(nullable NSString*)discoveryTitle
79 action:(nonnull UIKeyCommandAction)action {
80 UIKeyCommand* keyCommand =
81 [self keyCommandWithInput:input
82 modifierFlags:modifierFlags
83 action:@selector(cr_handleKeyCommand:)];
84 #if defined(__IPHONE_9_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0
85 if ([keyCommand respondsToSelector:@selector(discoverabilityTitle)])
86 keyCommand.discoverabilityTitle = discoveryTitle;
88 keyCommand.cr_action = action;