OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 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/toolbar/new_keyboard_accessory_view.h" | |
6 | |
7 #import <NotificationCenter/NotificationCenter.h> | |
8 | |
9 #include "base/mac/foundation_util.h" | |
10 #include "ios/chrome/browser/experimental_flags.h" | |
11 #include "ios/chrome/browser/ui/commands/ios_command_ids.h" | |
12 #import "ios/chrome/browser/ui/fancy_ui/colored_button.h" | |
13 #include "ios/chrome/browser/ui/rtl_geometry.h" | |
14 #include "ios/chrome/browser/ui/ui_util.h" | |
15 #import "ios/chrome/browser/ui/uikit_ui_util.h" | |
16 #include "ios/chrome/grit/ios_strings.h" | |
17 | |
18 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
19 #error "This file requires ARC support." | |
20 #endif | |
21 | |
22 @interface NewKeyboardAccessoryView () | |
23 | |
24 @property(nonatomic, weak) id<KeyboardAccessoryViewDelegate> delegate; | |
25 | |
26 // Called when a keyboard shortcut button is pressed. | |
27 - (void)keyboardButtonPressed:(NSString*)title; | |
28 | |
marq (ping after 24h)
2017/06/12 17:34:36
Either delete this newline, or add one after line
| |
29 // Creates a button shortcut for |title|. | |
30 - (UIView*)shortcutButtonWithTitle:(NSString*)title; | |
31 // Creates a button with an icon based on |iconName|. | |
32 - (UIButton*)iconButton:(NSString*)iconName; | |
33 | |
34 @end | |
35 | |
36 @implementation NewKeyboardAccessoryView | |
37 | |
38 // Unused by this implementation of |KeyboardAccessoryViewProtocol|. | |
39 @synthesize mode = _mode; | |
40 | |
41 @synthesize delegate = _delegate; | |
42 | |
43 - (instancetype)initWithButtons:(NSArray<NSString*>*)buttonTitles | |
44 delegate:(id<KeyboardAccessoryViewDelegate>)delegate { | |
45 const CGFloat kButtonMinSizeX = 61.0; | |
46 const CGFloat kButtonMinSizeXCompact = 32.0; | |
47 const CGFloat kButtonSizeY = 62.0; | |
48 const CGFloat kButtonSizeYCompact = 30.0; | |
49 const CGFloat kBetweenButtonSpacing = 15.0; | |
50 const CGFloat kBetweenButtonSpacingCompact = 6.0; | |
51 const CGFloat kSeparatorAlpha = 0.1; | |
52 const CGFloat kViewHeight = 70.0; | |
53 const CGFloat kViewHeightCompact = 43.0; | |
54 const BOOL isCompact = IsCompact(); | |
55 | |
56 CGFloat width = [[UIScreen mainScreen] bounds].size.width; | |
57 CGFloat height = isCompact ? kViewHeightCompact : kViewHeight; | |
58 CGRect frame = CGRectMake(0.0, 0.0, width, height); | |
59 | |
60 self = [super initWithFrame:frame inputViewStyle:UIInputViewStyleKeyboard]; | |
61 if (self) { | |
62 _delegate = delegate; | |
63 | |
64 // Create and add stackview filled with the shortcut buttons. | |
65 UIStackView* stackView = [[UIStackView alloc] init]; | |
66 [stackView setTranslatesAutoresizingMaskIntoConstraints:NO]; | |
67 stackView.spacing = | |
68 isCompact ? kBetweenButtonSpacingCompact : kBetweenButtonSpacing; | |
69 for (NSString* title in buttonTitles) { | |
70 UIView* button = [self shortcutButtonWithTitle:title]; | |
71 [button setTranslatesAutoresizingMaskIntoConstraints:NO]; | |
72 CGFloat buttonMinWidth = | |
73 isCompact ? kButtonMinSizeXCompact : kButtonMinSizeX; | |
74 [button.widthAnchor constraintGreaterThanOrEqualToConstant:buttonMinWidth] | |
75 .active = YES; | |
76 CGFloat buttonHeight = isCompact ? kButtonSizeYCompact : kButtonSizeY; | |
77 [button.heightAnchor constraintEqualToConstant:buttonHeight].active = YES; | |
78 [stackView addArrangedSubview:button]; | |
79 } | |
80 [self addSubview:stackView]; | |
81 | |
82 // Create and add buttons for voice and camera search. | |
83 UIButton* cameraButton = [self iconButton:@"qr_scanner_keyboard_accessory"]; | |
84 [cameraButton | |
85 addTarget:_delegate | |
86 action:@selector(keyboardAccessoryCameraSearchTouchUpInside) | |
87 forControlEvents:UIControlEventTouchUpInside]; | |
88 [self addSubview:cameraButton]; | |
89 | |
90 UIButton* voiceSearchButton = | |
91 [self iconButton:@"voice_icon_keyboard_accessory"]; | |
92 [self addSubview:voiceSearchButton]; | |
93 [voiceSearchButton | |
94 addTarget:_delegate | |
95 action:@selector(keyboardAccessoryVoiceSearchTouchDown) | |
96 forControlEvents:UIControlEventTouchDown]; | |
97 [voiceSearchButton | |
98 addTarget:_delegate | |
99 action:@selector(keyboardAccessoryVoiceSearchTouchUpInside) | |
100 forControlEvents:UIControlEventTouchUpInside]; | |
101 | |
102 // Create and add 1 pixel high separator view. | |
103 UIView* separator = [[UIView alloc] init]; | |
104 [separator setTranslatesAutoresizingMaskIntoConstraints:NO]; | |
105 [separator | |
106 setBackgroundColor:[UIColor colorWithWhite:0 alpha:kSeparatorAlpha]]; | |
107 [separator.heightAnchor | |
108 constraintEqualToConstant:1.0 / [[UIScreen mainScreen] scale]] | |
109 .active = YES; | |
110 [self addSubview:separator]; | |
111 | |
112 // Position all the views. | |
113 NSDictionary* viewsDictionary = @{ | |
114 @"cameraButton" : cameraButton, | |
115 @"voiceSearch" : voiceSearchButton, | |
116 @"stackView" : stackView, | |
117 @"separator" : separator, | |
118 }; | |
119 NSArray* constraints = @[ | |
120 @"H:|-8-[cameraButton]-8-[voiceSearch]-(>=16)-[stackView]", | |
121 @"H:|-0-[separator]-0-|", | |
122 @"V:[separator]-0-|", | |
123 ]; | |
124 ApplyVisualConstraintsWithOptions(constraints, viewsDictionary, | |
125 LayoutOptionForRTLSupport(), self); | |
126 AddSameCenterYConstraint(self, cameraButton); | |
127 AddSameCenterYConstraint(self, voiceSearchButton); | |
128 AddSameCenterYConstraint(self, stackView); | |
129 // The following constraint is supposed to break if there's not enough | |
130 // space to center the stack view. | |
131 NSLayoutConstraint* horizontallyCenterStackViewConstraint = | |
132 [self.centerXAnchor constraintEqualToAnchor:stackView.centerXAnchor]; | |
133 horizontallyCenterStackViewConstraint.priority = | |
134 UILayoutPriorityDefaultHigh; | |
135 horizontallyCenterStackViewConstraint.active = YES; | |
136 } | |
137 return self; | |
138 } | |
139 | |
140 - (UIView*)shortcutButtonWithTitle:(NSString*)title { | |
141 const CGFloat kCornerRadius = 4.0; | |
142 const CGFloat kAlphaStateNormal = 0.1; | |
143 const CGFloat kAlphaStateHighlighted = 0.2; | |
144 const CGFloat kHorizontalEdgeInset = 8; | |
145 const CGFloat kIpadButtonTitleFontSize = 20.0; | |
146 const CGFloat kIphoneButtonTitleFontSize = 15.0; | |
147 | |
148 ColoredButton* button = [ColoredButton buttonWithType:UIButtonTypeCustom]; | |
149 UIFont* font = nil; | |
150 | |
151 [button setTitle:title forState:UIControlStateNormal]; | |
152 [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; | |
153 | |
154 [button setBackgroundColor:UIColorFromRGB(0, kAlphaStateNormal) | |
155 forState:UIControlStateNormal]; | |
156 [button setBackgroundColor:UIColorFromRGB(0, kAlphaStateHighlighted) | |
157 forState:UIControlStateHighlighted]; | |
158 | |
159 button.layer.cornerRadius = kCornerRadius; | |
160 button.contentEdgeInsets = | |
161 UIEdgeInsetsMake(0, kHorizontalEdgeInset, 0, kHorizontalEdgeInset); | |
162 button.clipsToBounds = YES; | |
163 | |
164 if (IsIPadIdiom()) { | |
165 font = GetUIFont(FONT_HELVETICA, false, kIpadButtonTitleFontSize); | |
marq (ping after 24h)
2017/06/12 17:34:36
is this actually Helvetica, or is it San Francisco
jif
2017/06/12 18:26:01
Switching to San Francisco to match the iOS keyboa
| |
166 } else { | |
167 font = GetUIFont(FONT_HELVETICA, true, kIphoneButtonTitleFontSize); | |
168 } | |
169 | |
170 [button.titleLabel setFont:font]; | |
171 | |
172 [button addTarget:self | |
173 action:@selector(keyboardButtonPressed:) | |
174 forControlEvents:UIControlEventTouchUpInside]; | |
175 button.isAccessibilityElement = YES; | |
176 [button setAccessibilityLabel:title]; | |
177 return button; | |
178 } | |
179 | |
180 - (UIButton*)iconButton:(NSString*)iconName { | |
181 const CGFloat kIconTintAlphaStateNormal = 0.45; | |
182 const CGFloat kIconTintAlphaStateHighlighted = 0.6; | |
183 | |
184 UIColor* iconTintStateNormal = UIColorFromRGB(0, kIconTintAlphaStateNormal); | |
185 UIColor* iconTintStateHighlighted = | |
186 UIColorFromRGB(0, kIconTintAlphaStateHighlighted); | |
187 | |
188 ColoredButton* button = [ColoredButton buttonWithType:UIButtonTypeCustom]; | |
189 | |
190 [button setTintColor:iconTintStateNormal forState:UIControlStateNormal]; | |
191 [button setTintColor:iconTintStateHighlighted | |
192 forState:UIControlStateHighlighted]; | |
193 [button setTranslatesAutoresizingMaskIntoConstraints:NO]; | |
194 UIImage* icon = [[UIImage imageNamed:iconName] | |
195 imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; | |
196 [button setImage:icon forState:UIControlStateNormal]; | |
197 return button; | |
198 } | |
199 | |
200 - (BOOL)enableInputClicksWhenVisible { | |
201 return YES; | |
202 } | |
203 | |
204 - (void)keyboardButtonPressed:(id)sender { | |
205 UIButton* button = base::mac::ObjCCastStrict<UIButton>(sender); | |
206 [[UIDevice currentDevice] playInputClick]; | |
207 [_delegate keyPressed:[button currentTitle]]; | |
208 } | |
209 | |
210 @end | |
OLD | NEW |