Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(276)

Side by Side Diff: ios/chrome/browser/ui/toolbar/keyboard_accessory_view.mm

Issue 2914193002: [iOS] Factor out the KeyboardAccessoryView. (Closed)
Patch Set: Addressed comments and broke retain cycle. Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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, weak) 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 // Center buttons in available space by placing them within a parent view
57 // that auto-centers.
58 CGFloat betweenButtonSpacing =
59 isCompact ? kBetweenButtonSpacingCompact : kBetweenButtonSpacing;
60 const CGFloat buttonWidth = isCompact ? kButtonSizeXCompact : kButtonSizeX;
61
62 CGFloat totalWidth = (buttonTitles.count * buttonWidth) +
63 ((buttonTitles.count - 1) * betweenButtonSpacing);
64 CGFloat indent = floor((width - totalWidth) / 2.0);
65 if (indent < kButtonInset)
66 indent = kButtonInset;
67 CGRect parentViewRect = CGRectMake(indent, 0.0, totalWidth, height);
68 UIView* parentView = [[UIView alloc] initWithFrame:parentViewRect];
69 [parentView setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin |
70 UIViewAutoresizingFlexibleRightMargin];
71 [self addSubview:parentView];
72
73 // Create the shortcut buttons, starting at the left edge of |parentView|.
74 CGRect currentFrame =
75 CGRectMake(0.0, kButtonInset, buttonWidth,
76 isCompact ? kButtonSizeYCompact : kButtonSizeY);
77
78 for (NSString* title in buttonTitles) {
79 UIView* button = [self keyboardButtonWithTitle:title frame:currentFrame];
80 [parentView addSubview:button];
81 currentFrame.origin.x =
82 CGRectGetMaxX(currentFrame) + betweenButtonSpacing;
83 }
84
85 // Create the voice search button and add it over the text buttons.
86 _voiceSearchButton = [UIButton buttonWithType:UIButtonTypeCustom];
87 [_voiceSearchButton setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
88 SetA11yLabelAndUiAutomationName(
89 _voiceSearchButton, IDS_IOS_ACCNAME_VOICE_SEARCH, @"Voice Search");
90 UIImage* voiceRow = [UIImage imageNamed:@"custom_row_voice"];
91 UIImage* voiceRowPressed = [UIImage imageNamed:@"custom_row_voice_pressed"];
92 [_voiceSearchButton setBackgroundImage:voiceRow
93 forState:UIControlStateNormal];
94 [_voiceSearchButton setBackgroundImage:voiceRowPressed
95 forState:UIControlStateHighlighted];
96
97 UIImage* voiceIcon = [UIImage imageNamed:@"voice_icon_keyboard_accessory"];
98 [_voiceSearchButton setAdjustsImageWhenHighlighted:NO];
99 [_voiceSearchButton setImage:voiceIcon forState:UIControlStateNormal];
100 [_voiceSearchButton setFrame:[self bounds]];
101
102 [_voiceSearchButton
103 addTarget:delegate
104 action:@selector(keyboardAccessoryVoiceSearchTouchDown)
105 forControlEvents:UIControlEventTouchDown];
106 [_voiceSearchButton
107 addTarget:delegate
108 action:@selector(keyboardAccessoryVoiceSearchTouchUpInside)
109 forControlEvents:UIControlEventTouchUpInside];
110 [self addSubview:_voiceSearchButton];
111 }
112
113 return self;
114 }
115
116 - (void)setMode:(KeyboardAccessoryViewMode)mode {
117 _mode = mode;
118 switch (mode) {
119 case VOICE_SEARCH:
120 [_voiceSearchButton setHidden:NO];
121 break;
122 case KEY_SHORTCUTS:
123 [_voiceSearchButton setHidden:YES];
124 break;
125 }
126 }
127
128 - (UIView*)keyboardButtonWithTitle:(NSString*)title frame:(CGRect)frame {
129 const CGFloat kIpadButtonTitleFontSize = 20.0;
130 const CGFloat kIphoneButtonTitleFontSize = 15.0;
131
132 UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
133 UIFont* font = nil;
134 UIImage* backgroundImage = nil;
135 if (IsIPadIdiom()) {
136 font = GetUIFont(FONT_HELVETICA, false, kIpadButtonTitleFontSize);
137 } else {
138 font = GetUIFont(FONT_HELVETICA, true, kIphoneButtonTitleFontSize);
139 }
140 backgroundImage = [UIImage imageNamed:@"keyboard_button"];
141
142 button.frame = frame;
143 [button setTitle:title forState:UIControlStateNormal];
144 [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
145 [button.titleLabel setFont:font];
146 [button setBackgroundImage:backgroundImage forState:UIControlStateNormal];
147 [button addTarget:self
148 action:@selector(keyboardButtonPressed:)
149 forControlEvents:UIControlEventTouchUpInside];
150 button.isAccessibilityElement = YES;
151 [button setAccessibilityLabel:title];
152 return button;
153 }
154
155 - (BOOL)enableInputClicksWhenVisible {
156 return YES;
157 }
158
159 - (void)keyboardButtonPressed:(id)sender {
160 UIButton* button = base::mac::ObjCCastStrict<UIButton>(sender);
161 [[UIDevice currentDevice] playInputClick];
162 [_delegate keyPressed:[button currentTitle]];
163 }
164
165 @end
OLDNEW
« no previous file with comments | « ios/chrome/browser/ui/toolbar/keyboard_accessory_view.h ('k') | ios/chrome/browser/ui/toolbar/web_toolbar_controller.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698