| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 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/toolbar/new_keyboard_accessory_view.h" | 5 #import "ios/chrome/browser/ui/toolbar/new_keyboard_accessory_view.h" |
| 6 | 6 |
| 7 #import <NotificationCenter/NotificationCenter.h> | 7 #import <NotificationCenter/NotificationCenter.h> |
| 8 | 8 |
| 9 #include "base/mac/foundation_util.h" | 9 #include "base/mac/foundation_util.h" |
| 10 #include "ios/chrome/browser/experimental_flags.h" | 10 #include "ios/chrome/browser/experimental_flags.h" |
| 11 #include "ios/chrome/browser/ui/commands/ios_command_ids.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" | 12 #include "ios/chrome/browser/ui/rtl_geometry.h" |
| 14 #include "ios/chrome/browser/ui/ui_util.h" | 13 #include "ios/chrome/browser/ui/ui_util.h" |
| 15 #import "ios/chrome/browser/ui/uikit_ui_util.h" | 14 #import "ios/chrome/browser/ui/uikit_ui_util.h" |
| 16 #include "ios/chrome/grit/ios_strings.h" | 15 #include "ios/chrome/grit/ios_strings.h" |
| 17 | 16 |
| 18 #if !defined(__has_feature) || !__has_feature(objc_arc) | 17 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 19 #error "This file requires ARC support." | 18 #error "This file requires ARC support." |
| 20 #endif | 19 #endif |
| 21 | 20 |
| 22 @interface NewKeyboardAccessoryView () | 21 @interface NewKeyboardAccessoryView () |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 _delegate = delegate; | 53 _delegate = delegate; |
| 55 [self addSubviews]; | 54 [self addSubviews]; |
| 56 } | 55 } |
| 57 return self; | 56 return self; |
| 58 } | 57 } |
| 59 | 58 |
| 60 - (void)addSubviews { | 59 - (void)addSubviews { |
| 61 if (!self.subviews.count) | 60 if (!self.subviews.count) |
| 62 return; | 61 return; |
| 63 | 62 |
| 64 const CGFloat kButtonMinWidth = 36.0; | 63 const CGFloat kButtonMinWidth = 34.0; |
| 65 const CGFloat kButtonHeight = 34.0; | 64 const CGFloat kButtonHeight = 34.0; |
| 66 const CGFloat kBetweenShortcutButtonSpacing = 5.0; | 65 const CGFloat kBetweenShortcutButtonSpacing = 5.0; |
| 67 const CGFloat kBetweenSearchButtonSpacing = 12.0; | 66 const CGFloat kBetweenSearchButtonSpacing = 12.0; |
| 68 const CGFloat kMarginFromBottom = 2.0; | |
| 69 const CGFloat kHorizontalMargin = 12.0; | 67 const CGFloat kHorizontalMargin = 12.0; |
| 70 | 68 |
| 71 // Create and add stackview filled with the shortcut buttons. | 69 // Create and add stackview filled with the shortcut buttons. |
| 72 UIStackView* shortcutStackView = [[UIStackView alloc] init]; | 70 UIStackView* shortcutStackView = [[UIStackView alloc] init]; |
| 73 shortcutStackView.translatesAutoresizingMaskIntoConstraints = NO; | 71 shortcutStackView.translatesAutoresizingMaskIntoConstraints = NO; |
| 74 shortcutStackView.spacing = kBetweenShortcutButtonSpacing; | 72 shortcutStackView.spacing = kBetweenShortcutButtonSpacing; |
| 75 for (NSString* title in self.buttonTitles) { | 73 for (NSString* title in self.buttonTitles) { |
| 76 UIView* button = [self shortcutButtonWithTitle:title]; | 74 UIView* button = [self shortcutButtonWithTitle:title]; |
| 77 [button setTranslatesAutoresizingMaskIntoConstraints:NO]; | 75 [button setTranslatesAutoresizingMaskIntoConstraints:NO]; |
| 78 [button.widthAnchor constraintGreaterThanOrEqualToConstant:kButtonMinWidth] | 76 [button.widthAnchor constraintGreaterThanOrEqualToConstant:kButtonMinWidth] |
| (...skipping 11 matching lines...) Expand all Loading... |
| 90 forControlEvents:UIControlEventTouchDown]; | 88 forControlEvents:UIControlEventTouchDown]; |
| 91 [voiceSearchButton | 89 [voiceSearchButton |
| 92 addTarget:_delegate | 90 addTarget:_delegate |
| 93 action:@selector(keyboardAccessoryVoiceSearchTouchUpInside) | 91 action:@selector(keyboardAccessoryVoiceSearchTouchUpInside) |
| 94 forControlEvents:UIControlEventTouchUpInside]; | 92 forControlEvents:UIControlEventTouchUpInside]; |
| 95 UIButton* cameraButton = [self iconButton:@"keyboard_accessory_qr_scanner"]; | 93 UIButton* cameraButton = [self iconButton:@"keyboard_accessory_qr_scanner"]; |
| 96 [cameraButton addTarget:_delegate | 94 [cameraButton addTarget:_delegate |
| 97 action:@selector(keyboardAccessoryCameraSearchTouchUpInside) | 95 action:@selector(keyboardAccessoryCameraSearchTouchUpInside) |
| 98 forControlEvents:UIControlEventTouchUpInside]; | 96 forControlEvents:UIControlEventTouchUpInside]; |
| 99 | 97 |
| 100 // Create a stackview containing containing the buttons for voice search | 98 // Create and add a stackview containing containing the buttons for voice |
| 101 // and camera search. | 99 // search and camera search. |
| 102 UIStackView* searchStackView = [[UIStackView alloc] init]; | 100 UIStackView* searchStackView = [[UIStackView alloc] init]; |
| 103 searchStackView.translatesAutoresizingMaskIntoConstraints = NO; | 101 searchStackView.translatesAutoresizingMaskIntoConstraints = NO; |
| 104 searchStackView.spacing = kBetweenSearchButtonSpacing; | 102 searchStackView.spacing = kBetweenSearchButtonSpacing; |
| 105 [searchStackView addArrangedSubview:voiceSearchButton]; | 103 [searchStackView addArrangedSubview:voiceSearchButton]; |
| 106 [searchStackView addArrangedSubview:cameraButton]; | 104 [searchStackView addArrangedSubview:cameraButton]; |
| 107 [self addSubview:searchStackView]; | 105 [self addSubview:searchStackView]; |
| 108 | 106 |
| 109 // Position the stack views. | 107 // Position the stack views. |
| 110 NSArray* constraints = @[ | 108 NSArray* constraints = @[ |
| 111 @"H:|-horizontalMargin-[searchStackView]-(>=0)-[shortcutStackView]", | 109 @"H:|-horizontalMargin-[searchStackView]-(>=0)-[shortcutStackView]", |
| 112 @"[shortcutStackView]-horizontalMargin-|", | 110 @"[shortcutStackView]-horizontalMargin-|", |
| 113 @"V:[searchStackView]-bottomMargin-|", | |
| 114 @"V:[shortcutStackView]-bottomMargin-|" | |
| 115 ]; | 111 ]; |
| 116 NSDictionary* viewsDictionary = @{ | 112 NSDictionary* viewsDictionary = @{ |
| 117 @"searchStackView" : searchStackView, | 113 @"searchStackView" : searchStackView, |
| 118 @"shortcutStackView" : shortcutStackView, | 114 @"shortcutStackView" : shortcutStackView, |
| 119 }; | 115 }; |
| 120 NSDictionary* metrics = @{ | 116 NSDictionary* metrics = @{ |
| 121 @"bottomMargin" : @(kMarginFromBottom), | 117 @"horizontalMargin" : @(kHorizontalMargin), |
| 122 @"horizontalMargin" : @(kHorizontalMargin) | |
| 123 }; | 118 }; |
| 124 ApplyVisualConstraintsWithMetrics(constraints, viewsDictionary, metrics); | 119 ApplyVisualConstraintsWithMetrics(constraints, viewsDictionary, metrics); |
| 120 AddSameCenterYConstraint(searchStackView, self); |
| 121 AddSameCenterYConstraint(shortcutStackView, self); |
| 125 } | 122 } |
| 126 | 123 |
| 127 - (UIView*)shortcutButtonWithTitle:(NSString*)title { | 124 - (UIView*)shortcutButtonWithTitle:(NSString*)title { |
| 128 const CGFloat kCornerRadius = 5.0; | |
| 129 const CGFloat kAlphaStateNormal = 0.3; | |
| 130 const CGFloat kAlphaStateHighlighted = 0.6; | |
| 131 const CGFloat kHorizontalEdgeInset = 8; | 125 const CGFloat kHorizontalEdgeInset = 8; |
| 132 const CGFloat kButtonTitleFontSize = 20.0; | 126 const CGFloat kButtonTitleFontSize = 16.0; |
| 133 const UIColor* kButtonBackgroundColor = | 127 UIColor* kTitleColorStateNormal = [UIColor colorWithWhite:0.0 alpha:1.0]; |
| 134 [UIColor colorWithRed:0.507 green:0.534 blue:0.57 alpha:1.0]; | 128 UIColor* kTitleColorStateHighlighted = [UIColor colorWithWhite:0.0 alpha:0.3]; |
| 135 | 129 |
| 136 ColoredButton* button = [ColoredButton buttonWithType:UIButtonTypeCustom]; | 130 UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom]; |
| 137 | 131 [button setTitleColor:kTitleColorStateNormal forState:UIControlStateNormal]; |
| 138 UIColor* stateNormalBackgroundColor = | 132 [button setTitleColor:kTitleColorStateHighlighted |
| 139 [kButtonBackgroundColor colorWithAlphaComponent:kAlphaStateNormal]; | 133 forState:UIControlStateHighlighted]; |
| 140 UIColor* stateHighlightedBackgroundColor = | |
| 141 [kButtonBackgroundColor colorWithAlphaComponent:kAlphaStateHighlighted]; | |
| 142 | |
| 143 [button setBackgroundColor:stateNormalBackgroundColor | |
| 144 forState:UIControlStateNormal]; | |
| 145 [button setBackgroundColor:stateHighlightedBackgroundColor | |
| 146 forState:UIControlStateHighlighted]; | |
| 147 | 134 |
| 148 [button setTitle:title forState:UIControlStateNormal]; | 135 [button setTitle:title forState:UIControlStateNormal]; |
| 149 [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; | 136 [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; |
| 150 button.layer.cornerRadius = kCornerRadius; | |
| 151 button.contentEdgeInsets = | 137 button.contentEdgeInsets = |
| 152 UIEdgeInsetsMake(0, kHorizontalEdgeInset, 0, kHorizontalEdgeInset); | 138 UIEdgeInsetsMake(0, kHorizontalEdgeInset, 0, kHorizontalEdgeInset); |
| 153 button.clipsToBounds = YES; | 139 button.clipsToBounds = YES; |
| 154 [button.titleLabel setFont:[UIFont systemFontOfSize:kButtonTitleFontSize | 140 [button.titleLabel setFont:[UIFont systemFontOfSize:kButtonTitleFontSize |
| 155 weight:UIFontWeightMedium]]; | 141 weight:UIFontWeightMedium]]; |
| 156 | 142 |
| 157 [button addTarget:self | 143 [button addTarget:self |
| 158 action:@selector(keyboardButtonPressed:) | 144 action:@selector(keyboardButtonPressed:) |
| 159 forControlEvents:UIControlEventTouchUpInside]; | 145 forControlEvents:UIControlEventTouchUpInside]; |
| 160 button.isAccessibilityElement = YES; | 146 button.isAccessibilityElement = YES; |
| 161 [button setAccessibilityLabel:title]; | 147 [button setAccessibilityLabel:title]; |
| 162 return button; | 148 return button; |
| 163 } | 149 } |
| 164 | 150 |
| 165 - (UIButton*)iconButton:(NSString*)iconName { | 151 - (UIButton*)iconButton:(NSString*)iconName { |
| 152 const CGFloat kButtonShadowOpacity = 0.35; |
| 153 const CGFloat kButtonShadowRadius = 1.0; |
| 154 const CGFloat kButtonShadowVerticalOffset = 1.0; |
| 155 |
| 166 UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom]; | 156 UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom]; |
| 167 [button setTranslatesAutoresizingMaskIntoConstraints:NO]; | 157 [button setTranslatesAutoresizingMaskIntoConstraints:NO]; |
| 168 UIImage* icon = [UIImage imageNamed:iconName]; | 158 UIImage* icon = [UIImage imageNamed:iconName]; |
| 169 [button setImage:icon forState:UIControlStateNormal]; | 159 [button setImage:icon forState:UIControlStateNormal]; |
| 160 button.layer.shadowColor = [UIColor blackColor].CGColor; |
| 161 button.layer.shadowOffset = CGSizeMake(0, kButtonShadowVerticalOffset); |
| 162 button.layer.shadowOpacity = kButtonShadowOpacity; |
| 163 button.layer.shadowRadius = kButtonShadowRadius; |
| 170 return button; | 164 return button; |
| 171 } | 165 } |
| 172 | 166 |
| 173 - (BOOL)enableInputClicksWhenVisible { | 167 - (BOOL)enableInputClicksWhenVisible { |
| 174 return YES; | 168 return YES; |
| 175 } | 169 } |
| 176 | 170 |
| 177 - (void)keyboardButtonPressed:(id)sender { | 171 - (void)keyboardButtonPressed:(id)sender { |
| 178 UIButton* button = base::mac::ObjCCastStrict<UIButton>(sender); | 172 UIButton* button = base::mac::ObjCCastStrict<UIButton>(sender); |
| 179 [[UIDevice currentDevice] playInputClick]; | 173 [[UIDevice currentDevice] playInputClick]; |
| 180 [_delegate keyPressed:[button currentTitle]]; | 174 [_delegate keyPressed:[button currentTitle]]; |
| 181 } | 175 } |
| 182 | 176 |
| 183 @end | 177 @end |
| OLD | NEW |