Chromium Code Reviews| 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/keyboard_accessory_view.h" | |
| 6 | |
| 7 #include "base/mac/foundation_util.h" | |
| 8 #include "ios/chrome/browser/ui/commands/ios_command_ids.h" | |
| 9 #include "ios/chrome/browser/ui/ui_util.h" | |
| 10 #import "ios/chrome/browser/ui/uikit_ui_util.h" | |
| 11 #include "ios/chrome/grit/ios_strings.h" | |
| 12 | |
| 13 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
| 14 #error "This file requires ARC support." | |
| 15 #endif | |
| 16 | |
| 17 @interface KeyboardAccessoryView () | |
| 18 | |
| 19 @property(nonatomic, retain) id<KeyboardAccessoryViewDelegate> delegate; | |
| 20 @property(nonatomic, retain) UIButton* voiceSearchButton; | |
| 21 | |
| 22 // Creates a button with the same appearance as a keyboard key. | |
| 23 - (UIView*)keyboardButtonWithTitle:(NSString*)title frame:(CGRect)frame; | |
| 24 // Called when a keyboard shortcut button is pressed. | |
| 25 - (void)keyboardButtonPressed:(NSString*)title; | |
| 26 | |
| 27 @end | |
| 28 | |
| 29 @implementation KeyboardAccessoryView | |
| 30 | |
| 31 @synthesize mode = _mode; | |
| 32 @synthesize delegate = _delegate; | |
| 33 @synthesize voiceSearchButton = _voiceSearchButton; | |
| 34 | |
| 35 - (instancetype)initWithButtons:(NSArray<NSString*>*)buttonTitles | |
| 36 delegate:(id<KeyboardAccessoryViewDelegate>)delegate { | |
| 37 const CGFloat kViewHeight = 70.0; | |
| 38 const CGFloat kViewHeightCompact = 43.0; | |
| 39 const CGFloat kButtonInset = 5.0; | |
| 40 const CGFloat kButtonSizeX = 61.0; | |
| 41 const CGFloat kButtonSizeXCompact = 46.0; | |
| 42 const CGFloat kButtonSizeY = 62.0; | |
| 43 const CGFloat kButtonSizeYCompact = 35.0; | |
| 44 const CGFloat kBetweenButtonSpacing = 15.0; | |
| 45 const CGFloat kBetweenButtonSpacingCompact = 7.0; | |
| 46 const BOOL isCompact = IsCompact(); | |
| 47 | |
| 48 CGFloat width = [[UIScreen mainScreen] bounds].size.width; | |
| 49 CGFloat height = isCompact ? kViewHeightCompact : kViewHeight; | |
| 50 CGRect frame = CGRectMake(0.0, 0.0, width, height); | |
| 51 | |
| 52 self = [super initWithFrame:frame inputViewStyle:UIInputViewStyleKeyboard]; | |
| 53 if (self) { | |
| 54 _delegate = delegate; | |
| 55 | |
| 56 // TODO(pinkerton): purge this view when low memory. | |
|
marq (ping after 24h)
2017/06/02 09:15:10
I realize this is mostly just extracted from the o
jif
2017/06/02 13:06:18
Done.
| |
| 57 // Center buttons in available space by placing them within a parent view | |
| 58 // that auto-centers. | |
| 59 CGFloat betweenButtonSpacing = | |
| 60 isCompact ? kBetweenButtonSpacingCompact : kBetweenButtonSpacing; | |
| 61 const CGFloat buttonWidth = isCompact ? kButtonSizeXCompact : kButtonSizeX; | |
| 62 | |
| 63 CGFloat totalWidth = (buttonTitles.count * buttonWidth) + | |
| 64 ((buttonTitles.count - 1) * betweenButtonSpacing); | |
| 65 CGFloat indent = floor((width - totalWidth) / 2.0); | |
| 66 if (indent < kButtonInset) | |
| 67 indent = kButtonInset; | |
| 68 CGRect parentViewRect = CGRectMake(indent, 0.0, totalWidth, height); | |
| 69 UIView* parentView = [[UIView alloc] initWithFrame:parentViewRect]; | |
| 70 [parentView setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | | |
| 71 UIViewAutoresizingFlexibleRightMargin]; | |
| 72 [self addSubview:parentView]; | |
| 73 | |
| 74 // Create the shortcut buttons, starting at the left edge of |parentView|. | |
| 75 CGRect currentFrame = | |
| 76 CGRectMake(0.0, kButtonInset, buttonWidth, | |
| 77 isCompact ? kButtonSizeYCompact : kButtonSizeY); | |
| 78 | |
| 79 for (NSString* title in buttonTitles) { | |
| 80 UIView* button = [self keyboardButtonWithTitle:title frame:currentFrame]; | |
| 81 [parentView addSubview:button]; | |
| 82 currentFrame.origin.x = | |
| 83 CGRectGetMaxX(currentFrame) + betweenButtonSpacing; | |
| 84 } | |
| 85 | |
| 86 // Create the voice search button and add it over the text buttons. | |
| 87 _voiceSearchButton = [UIButton buttonWithType:UIButtonTypeCustom]; | |
| 88 [_voiceSearchButton setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; | |
| 89 SetA11yLabelAndUiAutomationName( | |
| 90 _voiceSearchButton, IDS_IOS_ACCNAME_VOICE_SEARCH, @"Voice Search"); | |
| 91 // TODO(leng): Consider moving these icons into a pak file. | |
| 92 UIImage* voiceRow = [UIImage imageNamed:@"custom_row_voice"]; | |
| 93 UIImage* voiceRowPressed = [UIImage imageNamed:@"custom_row_voice_pressed"]; | |
| 94 [_voiceSearchButton setBackgroundImage:voiceRow | |
| 95 forState:UIControlStateNormal]; | |
| 96 [_voiceSearchButton setBackgroundImage:voiceRowPressed | |
| 97 forState:UIControlStateHighlighted]; | |
| 98 | |
| 99 UIImage* voiceIcon = [UIImage imageNamed:@"voice_icon_keyboard_accessory"]; | |
| 100 [_voiceSearchButton setAdjustsImageWhenHighlighted:NO]; | |
| 101 [_voiceSearchButton setImage:voiceIcon forState:UIControlStateNormal]; | |
| 102 [_voiceSearchButton setFrame:[self bounds]]; | |
| 103 | |
| 104 [_voiceSearchButton | |
| 105 addTarget:delegate | |
| 106 action:@selector(keyboardAccessoryVoiceSearchTouchDown) | |
| 107 forControlEvents:UIControlEventTouchDown]; | |
| 108 [_voiceSearchButton | |
| 109 addTarget:delegate | |
| 110 action:@selector(keyboardAccessoryVoiceSearchTouchUpInside) | |
| 111 forControlEvents:UIControlEventTouchUpInside]; | |
| 112 [self addSubview:_voiceSearchButton]; | |
| 113 } | |
| 114 | |
| 115 return self; | |
| 116 } | |
| 117 | |
| 118 - (void)setMode:(KeyboardAccessoryViewMode)mode { | |
| 119 _mode = mode; | |
| 120 switch (mode) { | |
| 121 case VOICE_SEARCH: | |
| 122 [_voiceSearchButton setHidden:NO]; | |
| 123 break; | |
| 124 case KEY_SHORTCUTS: | |
| 125 [_voiceSearchButton setHidden:YES]; | |
| 126 break; | |
| 127 } | |
| 128 } | |
| 129 | |
| 130 - (UIView*)keyboardButtonWithTitle:(NSString*)title frame:(CGRect)frame { | |
| 131 const CGFloat kIpadButtonTitleFontSize = 20.0; | |
| 132 const CGFloat kIphoneButtonTitleFontSize = 15.0; | |
| 133 | |
| 134 UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom]; | |
| 135 UIFont* font = nil; | |
| 136 UIImage* backgroundImage = nil; | |
| 137 if (IsIPadIdiom()) { | |
| 138 font = GetUIFont(FONT_HELVETICA, false, kIpadButtonTitleFontSize); | |
| 139 } else { | |
| 140 font = GetUIFont(FONT_HELVETICA, true, kIphoneButtonTitleFontSize); | |
| 141 } | |
| 142 // TODO(leng): Consider moving these images to pak files as well. | |
| 143 backgroundImage = [UIImage imageNamed:@"keyboard_button"]; | |
| 144 | |
| 145 button.frame = frame; | |
| 146 [button setTitle:title forState:UIControlStateNormal]; | |
| 147 [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; | |
| 148 [button.titleLabel setFont:font]; | |
| 149 [button setBackgroundImage:backgroundImage forState:UIControlStateNormal]; | |
| 150 [button addTarget:self | |
| 151 action:@selector(keyPressed:) | |
| 152 forControlEvents:UIControlEventTouchUpInside]; | |
| 153 button.isAccessibilityElement = YES; | |
| 154 [button setAccessibilityLabel:title]; | |
| 155 return button; | |
| 156 } | |
| 157 | |
| 158 - (BOOL)enableInputClicksWhenVisible { | |
| 159 return YES; | |
| 160 } | |
| 161 | |
| 162 - (void)keyboardButtonPressed:(id)sender { | |
| 163 UIButton* button = base::mac::ObjCCastStrict<UIButton>(sender); | |
| 164 [[UIDevice currentDevice] playInputClick]; | |
| 165 [_delegate keyPressed:[button currentTitle]]; | |
| 166 } | |
| 167 | |
| 168 @end | |
| OLD | NEW |