OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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/find_bar/find_bar_view.h" |
| 6 |
| 7 #include "base/mac/scoped_nsobject.h" |
| 8 #include "components/strings/grit/components_strings.h" |
| 9 #import "ios/chrome/browser/ui/commands/ios_command_ids.h" |
| 10 #import "ios/chrome/browser/ui/find_bar/find_bar_touch_forwarding_view.h" |
| 11 #import "ios/chrome/browser/ui/uikit_ui_util.h" |
| 12 #include "ios/chrome/grit/ios_strings.h" |
| 13 #import "ios/third_party/material_components_ios/src/components/Typography/src/M
aterialTypography.h" |
| 14 #include "ui/base/l10n/l10n_util_mac.h" |
| 15 #import "ui/gfx/ios/NSString+CrStringDrawing.h" |
| 16 |
| 17 NSString* const kFindInPageInputFieldId = @"kFindInPageInputFieldId"; |
| 18 NSString* const kFindInPageNextButtonId = @"kFindInPageNextButtonId"; |
| 19 NSString* const kFindInPagePreviousButtonId = @"kFindInPagePreviousButtonId"; |
| 20 NSString* const kFindInPageCloseButtonId = @"kFindInPageCloseButtonId"; |
| 21 |
| 22 @interface FindBarView () |
| 23 |
| 24 // The overlay that shows number of results in format "1 of 13". |
| 25 @property(nonatomic, assign) UILabel* resultsLabel; |
| 26 @property(nonatomic, assign) UIView* separator; |
| 27 |
| 28 // Initializes all subviews. |
| 29 - (void)setupSubviews; |
| 30 // Sets up appearance of subviews, such as fonts, background colors. |
| 31 - (void)configureApperance:(BOOL)isDark; |
| 32 // Convenience method that returns images for light and dark appearances. |
| 33 - (UIImage*)imageWithName:(NSString*)imageName isDark:(BOOL)isDark; |
| 34 |
| 35 @end |
| 36 |
| 37 @implementation FindBarView |
| 38 @synthesize inputField = _inputField; |
| 39 @synthesize resultsLabel = _resultsLabel; |
| 40 @synthesize previousButton = _previousButton; |
| 41 @synthesize nextButton = _nextButton; |
| 42 @synthesize closeButton = _closeButton; |
| 43 @synthesize separator = _separator; |
| 44 |
| 45 - (instancetype)initWithDarkAppearance:(BOOL)darkAppearance { |
| 46 self = [super initWithFrame:CGRectZero]; |
| 47 if (self) { |
| 48 [self setupSubviews]; |
| 49 [self configureApperance:darkAppearance]; |
| 50 } |
| 51 return self; |
| 52 } |
| 53 |
| 54 #pragma mark - Public methods |
| 55 |
| 56 - (void)updateResultsLabelWithText:(NSString*)text { |
| 57 self.resultsLabel.hidden = (text.length == 0); |
| 58 self.resultsLabel.text = text; |
| 59 } |
| 60 |
| 61 #pragma mark - Internal |
| 62 |
| 63 - (void)setupSubviews { |
| 64 [self setBackgroundColor:[UIColor clearColor]]; |
| 65 |
| 66 // Input field. |
| 67 base::scoped_nsobject<UITextField> inputFieldScoped( |
| 68 [[UITextField alloc] initWithFrame:CGRectZero]); |
| 69 self.inputField = inputFieldScoped; |
| 70 self.inputField.backgroundColor = [UIColor clearColor]; |
| 71 self.inputField.tag = IDC_FIND_UPDATE; |
| 72 self.inputField.translatesAutoresizingMaskIntoConstraints = NO; |
| 73 self.inputField.placeholder = |
| 74 l10n_util::GetNSString(IDS_IOS_PLACEHOLDER_FIND_IN_PAGE); |
| 75 |
| 76 // Label containing number of found results. |
| 77 base::scoped_nsobject<UILabel> resultsLabelScoped( |
| 78 [[UILabel alloc] initWithFrame:CGRectZero]); |
| 79 self.resultsLabel = resultsLabelScoped; |
| 80 self.resultsLabel.textColor = [UIColor lightGrayColor]; |
| 81 self.resultsLabel.font = [UIFont systemFontOfSize:14]; |
| 82 [self.resultsLabel |
| 83 setContentCompressionResistancePriority:UILayoutPriorityRequired |
| 84 forAxis:UILayoutConstraintAxisHorizontal]; |
| 85 [self.resultsLabel |
| 86 setContentHuggingPriority:UILayoutPriorityRequired |
| 87 forAxis:UILayoutConstraintAxisHorizontal]; |
| 88 |
| 89 // Stack view that holds |inputField| and |resultsLabel|. |
| 90 base::scoped_nsobject<UIStackView> inputStackView([[UIStackView alloc] |
| 91 initWithArrangedSubviews:@[ inputFieldScoped, resultsLabelScoped ]]); |
| 92 [inputStackView setLayoutMargins:UIEdgeInsetsMake(0, 12, 0, 12)]; |
| 93 [inputStackView setLayoutMarginsRelativeArrangement:YES]; |
| 94 [inputStackView setSpacing:12]; |
| 95 [inputStackView setTranslatesAutoresizingMaskIntoConstraints:NO]; |
| 96 [self addSubview:inputStackView]; |
| 97 |
| 98 base::scoped_nsobject<NSMutableArray> constraints( |
| 99 [[NSMutableArray alloc] init]); |
| 100 [constraints addObjectsFromArray:@[ |
| 101 [[inputStackView leadingAnchor] constraintEqualToAnchor:self.leadingAnchor], |
| 102 [[inputStackView topAnchor] constraintEqualToAnchor:self.topAnchor], |
| 103 [[inputStackView bottomAnchor] constraintEqualToAnchor:self.bottomAnchor], |
| 104 ]]; |
| 105 |
| 106 // Touch-forwarding view is put on top of |inputStackView| to forward touches |
| 107 // to |inputField|. |
| 108 // Unlike a gesture recognizer, forwarding all touch events allows for using |
| 109 // long press, pinch and other manipulatiosn on the target textfield. |
| 110 base::scoped_nsobject<FindBarTouchForwardingView> forwarder( |
| 111 [[FindBarTouchForwardingView alloc] init]); |
| 112 [forwarder setTargetView:self.inputField]; |
| 113 [self addSubview:forwarder]; |
| 114 [constraints addObjectsFromArray:@[ |
| 115 [[forwarder leadingAnchor] |
| 116 constraintEqualToAnchor:[inputStackView leadingAnchor]], |
| 117 [[forwarder topAnchor] constraintEqualToAnchor:[inputStackView topAnchor]], |
| 118 [[forwarder bottomAnchor] |
| 119 constraintEqualToAnchor:[inputStackView bottomAnchor]], |
| 120 [[forwarder trailingAnchor] |
| 121 constraintEqualToAnchor:[inputStackView trailingAnchor]], |
| 122 ]]; |
| 123 [forwarder setTranslatesAutoresizingMaskIntoConstraints:NO]; |
| 124 |
| 125 // Thin line separator between buttons and input. |
| 126 base::scoped_nsobject<UIView> separatorScoped( |
| 127 [[UIView alloc] initWithFrame:CGRectZero]); |
| 128 UIView* separator = separatorScoped; |
| 129 separator.backgroundColor = [UIColor colorWithWhite:0.83 alpha:1]; |
| 130 [self addSubview:separator]; |
| 131 [constraints addObjectsFromArray:@[ |
| 132 [separator.widthAnchor constraintEqualToConstant:1], |
| 133 [separator.bottomAnchor constraintEqualToAnchor:self.bottomAnchor |
| 134 constant:-8], |
| 135 [separator.topAnchor constraintEqualToAnchor:self.topAnchor constant:8], |
| 136 [separator.leadingAnchor |
| 137 constraintEqualToAnchor:inputStackView.get().trailingAnchor], |
| 138 ]]; |
| 139 separator.translatesAutoresizingMaskIntoConstraints = NO; |
| 140 self.separator = separator; |
| 141 |
| 142 // Previous button with an arrow. |
| 143 base::scoped_nsobject<UIButton> previousButtonScoped( |
| 144 [[UIButton alloc] initWithFrame:CGRectZero]); |
| 145 self.previousButton = previousButtonScoped; |
| 146 [self addSubview:self.previousButton]; |
| 147 [constraints addObjectsFromArray:@[ |
| 148 [self.previousButton.centerYAnchor |
| 149 constraintEqualToAnchor:self.centerYAnchor], |
| 150 [self.previousButton.widthAnchor constraintEqualToConstant:48], |
| 151 [self.previousButton.heightAnchor constraintEqualToConstant:56], |
| 152 [self.previousButton.leadingAnchor |
| 153 constraintEqualToAnchor:separator.trailingAnchor], |
| 154 ]]; |
| 155 self.previousButton.isAccessibilityElement = YES; |
| 156 self.previousButton.accessibilityTraits = UIAccessibilityTraitButton; |
| 157 self.previousButton.tag = IDC_FIND_PREVIOUS; |
| 158 self.previousButton.translatesAutoresizingMaskIntoConstraints = NO; |
| 159 |
| 160 // Next button with an arrow. |
| 161 base::scoped_nsobject<UIButton> nextButtonScoped( |
| 162 [[UIButton alloc] initWithFrame:CGRectZero]); |
| 163 self.nextButton = nextButtonScoped; |
| 164 [self addSubview:self.nextButton]; |
| 165 [constraints addObjectsFromArray:@[ |
| 166 [self.nextButton.centerYAnchor constraintEqualToAnchor:self.centerYAnchor], |
| 167 [self.nextButton.widthAnchor constraintEqualToConstant:48], |
| 168 [self.nextButton.heightAnchor constraintEqualToConstant:56], |
| 169 [self.nextButton.leadingAnchor |
| 170 constraintEqualToAnchor:self.previousButton.trailingAnchor], |
| 171 ]]; |
| 172 self.nextButton.tag = IDC_FIND_NEXT; |
| 173 self.nextButton.translatesAutoresizingMaskIntoConstraints = NO; |
| 174 |
| 175 // Close button with a cross. |
| 176 base::scoped_nsobject<UIButton> closeButtonScoped( |
| 177 [[UIButton alloc] initWithFrame:CGRectZero]); |
| 178 self.closeButton = closeButtonScoped; |
| 179 [self addSubview:self.closeButton]; |
| 180 [constraints addObjectsFromArray:@[ |
| 181 [self.closeButton.centerYAnchor constraintEqualToAnchor:self.centerYAnchor], |
| 182 [self.closeButton.trailingAnchor constraintEqualToAnchor:self.trailingAnchor |
| 183 constant:-4], |
| 184 [self.closeButton.widthAnchor constraintEqualToConstant:48], |
| 185 [self.closeButton.heightAnchor constraintEqualToConstant:56], |
| 186 [self.closeButton.leadingAnchor |
| 187 constraintEqualToAnchor:self.nextButton.trailingAnchor], |
| 188 ]]; |
| 189 self.closeButton.tag = IDC_FIND_CLOSE; |
| 190 self.closeButton.translatesAutoresizingMaskIntoConstraints = NO; |
| 191 |
| 192 // Connect outlets. |
| 193 [self.nextButton addTarget:self |
| 194 action:@selector(chromeExecuteCommand:) |
| 195 forControlEvents:UIControlEventTouchUpInside]; |
| 196 [self.previousButton addTarget:self |
| 197 action:@selector(chromeExecuteCommand:) |
| 198 forControlEvents:UIControlEventTouchUpInside]; |
| 199 [self.closeButton addTarget:self |
| 200 action:@selector(chromeExecuteCommand:) |
| 201 forControlEvents:UIControlEventTouchUpInside]; |
| 202 |
| 203 // A11y labels. |
| 204 SetA11yLabelAndUiAutomationName(self.closeButton, |
| 205 IDS_FIND_IN_PAGE_CLOSE_TOOLTIP, |
| 206 kFindInPageCloseButtonId); |
| 207 SetA11yLabelAndUiAutomationName(self.previousButton, |
| 208 IDS_FIND_IN_PAGE_PREVIOUS_TOOLTIP, |
| 209 kFindInPagePreviousButtonId); |
| 210 SetA11yLabelAndUiAutomationName( |
| 211 self.nextButton, IDS_FIND_IN_PAGE_NEXT_TOOLTIP, kFindInPageNextButtonId); |
| 212 self.inputField.accessibilityIdentifier = kFindInPageInputFieldId; |
| 213 |
| 214 // Configure fonts. |
| 215 [self.inputField setFont:[MDCTypography body1Font]]; |
| 216 [self.resultsLabel setFont:[MDCTypography body1Font]]; |
| 217 |
| 218 [NSLayoutConstraint activateConstraints:constraints]; |
| 219 } |
| 220 |
| 221 - (void)configureApperance:(BOOL)isDark { |
| 222 [self.closeButton setImage:[self imageWithName:@"find_close" isDark:isDark] |
| 223 forState:UIControlStateNormal]; |
| 224 [self.closeButton |
| 225 setImage:[self imageWithName:@"find_close_pressed" isDark:isDark] |
| 226 forState:UIControlStateHighlighted]; |
| 227 |
| 228 [self.previousButton setImage:[self imageWithName:@"find_prev" isDark:isDark] |
| 229 forState:UIControlStateNormal]; |
| 230 [self.previousButton |
| 231 setImage:[self imageWithName:@"find_prev_pressed" isDark:isDark] |
| 232 forState:UIControlStateHighlighted]; |
| 233 [self.previousButton |
| 234 setImage:[self imageWithName:@"find_prev_disabled" isDark:isDark] |
| 235 forState:UIControlStateDisabled]; |
| 236 |
| 237 [self.nextButton setImage:[self imageWithName:@"find_next" isDark:isDark] |
| 238 forState:UIControlStateNormal]; |
| 239 [self.nextButton |
| 240 setImage:[self imageWithName:@"find_next_pressed" isDark:isDark] |
| 241 forState:UIControlStateHighlighted]; |
| 242 [self.nextButton |
| 243 setImage:[self imageWithName:@"find_next_disabled" isDark:isDark] |
| 244 forState:UIControlStateDisabled]; |
| 245 |
| 246 if (!isDark) { |
| 247 return; |
| 248 } |
| 249 |
| 250 // Setup dark appearance. |
| 251 [self.inputField setTextColor:[UIColor whiteColor]]; |
| 252 NSString* placeholder = [self.inputField placeholder]; |
| 253 UIColor* inputTextColor = [UIColor colorWithWhite:1 alpha:0.7]; |
| 254 NSDictionary* attributes = @{NSForegroundColorAttributeName : inputTextColor}; |
| 255 [self.inputField |
| 256 setAttributedPlaceholder:[[[NSAttributedString alloc] |
| 257 initWithString:placeholder |
| 258 attributes:attributes] autorelease]]; |
| 259 UIColor* resultTextColor = [UIColor colorWithWhite:1 alpha:0.3]; |
| 260 [self.resultsLabel setTextColor:resultTextColor]; |
| 261 UIColor* separatorColor = [UIColor colorWithWhite:0 alpha:0.1]; |
| 262 [self.separator setBackgroundColor:separatorColor]; |
| 263 } |
| 264 |
| 265 - (UIImage*)imageWithName:(NSString*)imageName isDark:(BOOL)isDark { |
| 266 NSString* name = |
| 267 isDark ? [imageName stringByAppendingString:@"_incognito"] : imageName; |
| 268 return [UIImage imageNamed:name]; |
| 269 } |
| 270 |
| 271 @end |
OLD | NEW |