| OLD | NEW |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2014 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/omnibox/omnibox_popup_material_view_controller.h" | 5 #import "ios/chrome/browser/ui/omnibox/omnibox_popup_material_view_controller.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/ios/ios_util.h" | 9 #include "base/ios/ios_util.h" |
| 10 #include "base/mac/scoped_cftyperef.h" | 10 #include "base/mac/scoped_cftyperef.h" |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 } | 67 } |
| 68 UIColor* BackgroundColorIncognito() { | 68 UIColor* BackgroundColorIncognito() { |
| 69 return [UIColor colorWithRed:(50 / 255.0) | 69 return [UIColor colorWithRed:(50 / 255.0) |
| 70 green:(50 / 255.0) | 70 green:(50 / 255.0) |
| 71 blue:(50 / 255.0) | 71 blue:(50 / 255.0) |
| 72 alpha:1.0]; | 72 alpha:1.0]; |
| 73 } | 73 } |
| 74 } // namespace | 74 } // namespace |
| 75 | 75 |
| 76 @interface OmniboxPopupMaterialViewController () { | 76 @interface OmniboxPopupMaterialViewController () { |
| 77 // CTFontRef's are needed for drawing attributed strings but are expensive | |
| 78 // to create. Since we only need four, we create them here and hold on to | |
| 79 // them. | |
| 80 base::ScopedCFTypeRef<CTFontRef> _smallFont; | |
| 81 base::ScopedCFTypeRef<CTFontRef> _bigFont; | |
| 82 base::ScopedCFTypeRef<CTFontRef> _smallBoldFont; | |
| 83 base::ScopedCFTypeRef<CTFontRef> _bigBoldFont; | |
| 84 | |
| 85 // Alignment of omnibox text. Popup text should match this alignment. | 77 // Alignment of omnibox text. Popup text should match this alignment. |
| 86 NSTextAlignment _alignment; | 78 NSTextAlignment _alignment; |
| 87 | 79 |
| 88 OmniboxPopupViewIOS* _popupView; // weak, owns us | 80 OmniboxPopupViewIOS* _popupView; // weak, owns us |
| 89 | 81 |
| 90 // Fetcher for Answers in Suggest images. | 82 // Fetcher for Answers in Suggest images. |
| 91 std::unique_ptr<web::ImageDataFetcher> imageFetcher_; | 83 std::unique_ptr<web::ImageDataFetcher> imageFetcher_; |
| 92 | 84 |
| 93 // The data source. | 85 // The data source. |
| 94 AutocompleteResult _currentResult; | 86 AutocompleteResult _currentResult; |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 if (_incognito) { | 142 if (_incognito) { |
| 151 self.view.backgroundColor = BackgroundColorIncognito(); | 143 self.view.backgroundColor = BackgroundColorIncognito(); |
| 152 } else { | 144 } else { |
| 153 self.view.backgroundColor = | 145 self.view.backgroundColor = |
| 154 IsIPadIdiom() ? BackgroundColorTablet() : BackgroundColorPhone(); | 146 IsIPadIdiom() ? BackgroundColorTablet() : BackgroundColorPhone(); |
| 155 } | 147 } |
| 156 [self.view setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | | 148 [self.view setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | |
| 157 UIViewAutoresizingFlexibleHeight)]; | 149 UIViewAutoresizingFlexibleHeight)]; |
| 158 | 150 |
| 159 // Cache fonts needed for omnibox attributed string. | 151 // Cache fonts needed for omnibox attributed string. |
| 160 UIFont* smallFont = [MDCTypography body1Font]; | |
| 161 UIFont* bigFont = [MDCTypography subheadFont]; | |
| 162 UIFont* smallBoldFont = [[MDFRobotoFontLoader sharedInstance] | |
| 163 mediumFontOfSize:smallFont.pointSize]; | |
| 164 UIFont* bigBoldFont = | |
| 165 [[MDFRobotoFontLoader sharedInstance] mediumFontOfSize:bigFont.pointSize]; | |
| 166 _smallFont.reset(CTFontCreateWithName((CFStringRef)smallFont.fontName, | |
| 167 smallFont.pointSize, NULL)); | |
| 168 _bigFont.reset(CTFontCreateWithName((CFStringRef)bigFont.fontName, | |
| 169 bigFont.pointSize, NULL)); | |
| 170 _smallBoldFont.reset(CTFontCreateWithName((CFStringRef)smallBoldFont.fontName, | |
| 171 smallBoldFont.pointSize, NULL)); | |
| 172 _bigBoldFont.reset(CTFontCreateWithName((CFStringRef)bigBoldFont.fontName, | |
| 173 bigBoldFont.pointSize, NULL)); | |
| 174 NSMutableArray* rowsBuilder = [[[NSMutableArray alloc] init] autorelease]; | 152 NSMutableArray* rowsBuilder = [[[NSMutableArray alloc] init] autorelease]; |
| 175 for (int i = 0; i < kRowCount; i++) { | 153 for (int i = 0; i < kRowCount; i++) { |
| 176 OmniboxPopupMaterialRow* row = [[[OmniboxPopupMaterialRow alloc] | 154 OmniboxPopupMaterialRow* row = [[[OmniboxPopupMaterialRow alloc] |
| 177 initWithIncognito:_incognito] autorelease]; | 155 initWithIncognito:_incognito] autorelease]; |
| 178 row.accessibilityIdentifier = | 156 row.accessibilityIdentifier = |
| 179 [NSString stringWithFormat:@"omnibox suggestion %i", i]; | 157 [NSString stringWithFormat:@"omnibox suggestion %i", i]; |
| 180 row.autoresizingMask = UIViewAutoresizingFlexibleWidth; | 158 row.autoresizingMask = UIViewAutoresizingFlexibleWidth; |
| 181 [rowsBuilder addObject:row]; | 159 [rowsBuilder addObject:row]; |
| 182 [row.appendButton addTarget:self | 160 [row.appendButton addTarget:self |
| 183 action:@selector(appendButtonTapped:) | 161 action:@selector(appendButtonTapped:) |
| (...skipping 11 matching lines...) Expand all Loading... |
| 195 [self.tableView setLayoutMargins:UIEdgeInsetsZero]; | 173 [self.tableView setLayoutMargins:UIEdgeInsetsZero]; |
| 196 } | 174 } |
| 197 self.automaticallyAdjustsScrollViewInsets = NO; | 175 self.automaticallyAdjustsScrollViewInsets = NO; |
| 198 [self.tableView setContentInset:UIEdgeInsetsMake(kTopAndBottomPadding, 0, | 176 [self.tableView setContentInset:UIEdgeInsetsMake(kTopAndBottomPadding, 0, |
| 199 kTopAndBottomPadding, 0)]; | 177 kTopAndBottomPadding, 0)]; |
| 200 } | 178 } |
| 201 | 179 |
| 202 - (void)didReceiveMemoryWarning { | 180 - (void)didReceiveMemoryWarning { |
| 203 [super didReceiveMemoryWarning]; | 181 [super didReceiveMemoryWarning]; |
| 204 if (![self isViewLoaded]) { | 182 if (![self isViewLoaded]) { |
| 205 _smallFont.reset(); | |
| 206 _bigFont.reset(); | |
| 207 _smallBoldFont.reset(); | |
| 208 _bigBoldFont.reset(); | |
| 209 _rows.reset(); | 183 _rows.reset(); |
| 210 } | 184 } |
| 211 } | 185 } |
| 212 | 186 |
| 213 - (void)traitCollectionDidChange:(UITraitCollection*)previousTraitCollection { | 187 - (void)traitCollectionDidChange:(UITraitCollection*)previousTraitCollection { |
| 214 [self layoutRows]; | 188 [self layoutRows]; |
| 215 } | 189 } |
| 216 | 190 |
| 217 #pragma mark - | 191 #pragma mark - |
| 218 #pragma mark Updating data and UI | 192 #pragma mark Updating data and UI |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 443 type:field->type()]]; | 417 type:field->type()]]; |
| 444 } | 418 } |
| 445 | 419 |
| 446 return result; | 420 return result; |
| 447 } | 421 } |
| 448 | 422 |
| 449 - (NSAttributedString*)attributedStringWithString:(const base::string16&)string | 423 - (NSAttributedString*)attributedStringWithString:(const base::string16&)string |
| 450 type:(int)type { | 424 type:(int)type { |
| 451 NSDictionary* attributes = nil; | 425 NSDictionary* attributes = nil; |
| 452 | 426 |
| 453 const id font = (id)kCTFontAttributeName; | 427 const id font = (id)NSFontAttributeName; |
| 454 NSString* foregroundColor = (NSString*)kCTForegroundColorAttributeName; | 428 NSString* foregroundColor = (NSString*)NSForegroundColorAttributeName; |
| 455 const id baselineOffset = (id)NSBaselineOffsetAttributeName; | 429 const id baselineOffset = (id)NSBaselineOffsetAttributeName; |
| 456 | 430 |
| 457 // Answer types, sizes and colors specified at http://goto.google.com/ais_api. | 431 // Answer types, sizes and colors specified at http://goto.google.com/ais_api. |
| 458 switch (type) { | 432 switch (type) { |
| 459 case SuggestionAnswer::TOP_ALIGNED: | 433 case SuggestionAnswer::TOP_ALIGNED: |
| 460 attributes = @{ | 434 attributes = @{ |
| 461 font : [[MDFRobotoFontLoader sharedInstance] regularFontOfSize:12], | 435 font : [[MDFRobotoFontLoader sharedInstance] regularFontOfSize:12], |
| 462 baselineOffset : @10.0f, | 436 baselineOffset : @10.0f, |
| 463 foregroundColor : (id)[UIColor grayColor].CGColor, | 437 foregroundColor : [UIColor grayColor], |
| 464 }; | 438 }; |
| 465 break; | 439 break; |
| 466 case SuggestionAnswer::DESCRIPTION_POSITIVE: | 440 case SuggestionAnswer::DESCRIPTION_POSITIVE: |
| 467 attributes = @{ | 441 attributes = @{ |
| 468 font : [[MDFRobotoFontLoader sharedInstance] regularFontOfSize:16], | 442 font : [[MDFRobotoFontLoader sharedInstance] regularFontOfSize:16], |
| 469 foregroundColor : (id)[UIColor colorWithRed:11 / 255.0 | 443 foregroundColor : [UIColor colorWithRed:11 / 255.0 |
| 470 green:128 / 255.0 | 444 green:128 / 255.0 |
| 471 blue:67 / 255.0 | 445 blue:67 / 255.0 |
| 472 alpha:1.0] | 446 alpha:1.0], |
| 473 .CGColor, | |
| 474 }; | 447 }; |
| 475 break; | 448 break; |
| 476 case SuggestionAnswer::DESCRIPTION_NEGATIVE: | 449 case SuggestionAnswer::DESCRIPTION_NEGATIVE: |
| 477 attributes = @{ | 450 attributes = @{ |
| 478 font : [[MDFRobotoFontLoader sharedInstance] regularFontOfSize:16], | 451 font : [[MDFRobotoFontLoader sharedInstance] regularFontOfSize:16], |
| 479 foregroundColor : (id)[UIColor colorWithRed:197 / 255.0 | 452 foregroundColor : [UIColor colorWithRed:197 / 255.0 |
| 480 green:57 / 255.0 | 453 green:57 / 255.0 |
| 481 blue:41 / 255.0 | 454 blue:41 / 255.0 |
| 482 alpha:1.0] | 455 alpha:1.0], |
| 483 .CGColor, | |
| 484 }; | 456 }; |
| 485 break; | 457 break; |
| 486 case SuggestionAnswer::PERSONALIZED_SUGGESTION: | 458 case SuggestionAnswer::PERSONALIZED_SUGGESTION: |
| 487 attributes = @{ | 459 attributes = @{ |
| 488 font : [[MDFRobotoFontLoader sharedInstance] regularFontOfSize:16], | 460 font : [[MDFRobotoFontLoader sharedInstance] regularFontOfSize:16], |
| 489 }; | 461 }; |
| 490 break; | 462 break; |
| 491 case SuggestionAnswer::ANSWER_TEXT_MEDIUM: | 463 case SuggestionAnswer::ANSWER_TEXT_MEDIUM: |
| 492 attributes = @{ | 464 attributes = @{ |
| 493 font : [[MDFRobotoFontLoader sharedInstance] regularFontOfSize:20], | 465 font : [[MDFRobotoFontLoader sharedInstance] regularFontOfSize:20], |
| 494 }; | 466 }; |
| 495 break; | 467 break; |
| 496 case SuggestionAnswer::ANSWER_TEXT_LARGE: | 468 case SuggestionAnswer::ANSWER_TEXT_LARGE: |
| 497 attributes = @{ | 469 attributes = @{ |
| 498 font : [[MDFRobotoFontLoader sharedInstance] regularFontOfSize:24], | 470 font : [[MDFRobotoFontLoader sharedInstance] regularFontOfSize:24], |
| 499 }; | 471 }; |
| 500 break; | 472 break; |
| 501 case SuggestionAnswer::SUGGESTION_SECONDARY_TEXT_SMALL: | 473 case SuggestionAnswer::SUGGESTION_SECONDARY_TEXT_SMALL: |
| 502 attributes = @{ | 474 attributes = @{ |
| 503 font : [[MDFRobotoFontLoader sharedInstance] regularFontOfSize:12], | 475 font : [[MDFRobotoFontLoader sharedInstance] regularFontOfSize:12], |
| 504 foregroundColor : (id)[UIColor grayColor].CGColor, | 476 foregroundColor : [UIColor grayColor], |
| 505 }; | 477 }; |
| 506 break; | 478 break; |
| 507 case SuggestionAnswer::SUGGESTION_SECONDARY_TEXT_MEDIUM: | 479 case SuggestionAnswer::SUGGESTION_SECONDARY_TEXT_MEDIUM: |
| 508 attributes = @{ | 480 attributes = @{ |
| 509 font : [[MDFRobotoFontLoader sharedInstance] regularFontOfSize:14], | 481 font : [[MDFRobotoFontLoader sharedInstance] regularFontOfSize:14], |
| 510 foregroundColor : (id)[UIColor grayColor].CGColor, | 482 foregroundColor : [UIColor grayColor], |
| 511 }; | 483 }; |
| 512 break; | 484 break; |
| 513 case SuggestionAnswer::SUGGESTION: | 485 case SuggestionAnswer::SUGGESTION: |
| 514 // Fall through. | 486 // Fall through. |
| 515 default: | 487 default: |
| 516 attributes = @{ | 488 attributes = @{ |
| 517 font : [[MDFRobotoFontLoader sharedInstance] regularFontOfSize:16], | 489 font : [[MDFRobotoFontLoader sharedInstance] regularFontOfSize:16], |
| 518 }; | 490 }; |
| 519 } | 491 } |
| 520 | 492 |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 670 | 642 |
| 671 - (NSMutableAttributedString*) | 643 - (NSMutableAttributedString*) |
| 672 attributedStringWithString:(NSString*)text | 644 attributedStringWithString:(NSString*)text |
| 673 classifications:(const ACMatchClassifications*)classifications | 645 classifications:(const ACMatchClassifications*)classifications |
| 674 smallFont:(BOOL)smallFont | 646 smallFont:(BOOL)smallFont |
| 675 color:(UIColor*)defaultColor | 647 color:(UIColor*)defaultColor |
| 676 dimColor:(UIColor*)dimColor { | 648 dimColor:(UIColor*)dimColor { |
| 677 if (text == nil) | 649 if (text == nil) |
| 678 return nil; | 650 return nil; |
| 679 | 651 |
| 680 CTFontRef fontRef = smallFont ? _smallFont : _bigFont; | 652 UIFont* fontRef = |
| 653 smallFont ? [MDCTypography body1Font] : [MDCTypography subheadFont]; |
| 681 | 654 |
| 682 NSMutableAttributedString* as = | 655 NSMutableAttributedString* as = |
| 683 [[[NSMutableAttributedString alloc] initWithString:text] autorelease]; | 656 [[[NSMutableAttributedString alloc] initWithString:text] autorelease]; |
| 684 | 657 |
| 685 // Set the base attributes to the default font and color. | 658 // Set the base attributes to the default font and color. |
| 686 NSDictionary* dict = [NSDictionary | 659 NSDictionary* dict = @{ |
| 687 dictionaryWithObjectsAndKeys:(id)fontRef, (NSString*)kCTFontAttributeName, | 660 NSFontAttributeName : fontRef, |
| 688 defaultColor.CGColor, | 661 NSForegroundColorAttributeName : defaultColor, |
| 689 (NSString*)kCTForegroundColorAttributeName, | 662 }; |
| 690 nil]; | |
| 691 [as addAttributes:dict range:NSMakeRange(0, [text length])]; | 663 [as addAttributes:dict range:NSMakeRange(0, [text length])]; |
| 692 | 664 |
| 693 if (classifications != NULL) { | 665 if (classifications != NULL) { |
| 694 CTFontRef boldFontRef = smallFont ? _smallBoldFont : _bigBoldFont; | 666 UIFont* boldFontRef = [[MDFRobotoFontLoader sharedInstance] |
| 667 mediumFontOfSize:fontRef.pointSize]; |
| 695 | 668 |
| 696 for (ACMatchClassifications::const_iterator i = classifications->begin(); | 669 for (ACMatchClassifications::const_iterator i = classifications->begin(); |
| 697 i != classifications->end(); ++i) { | 670 i != classifications->end(); ++i) { |
| 698 const BOOL isLast = (i + 1) == classifications->end(); | 671 const BOOL isLast = (i + 1) == classifications->end(); |
| 699 const size_t nextOffset = (isLast ? [text length] : (i + 1)->offset); | 672 const size_t nextOffset = (isLast ? [text length] : (i + 1)->offset); |
| 700 const NSInteger location = static_cast<NSInteger>(i->offset); | 673 const NSInteger location = static_cast<NSInteger>(i->offset); |
| 701 const NSInteger length = static_cast<NSInteger>(nextOffset - i->offset); | 674 const NSInteger length = static_cast<NSInteger>(nextOffset - i->offset); |
| 702 // Guard against bad, off-the-end classification ranges due to | 675 // Guard against bad, off-the-end classification ranges due to |
| 703 // crbug.com/121703 and crbug.com/131370. | 676 // crbug.com/121703 and crbug.com/131370. |
| 704 if (i->offset + length > [text length] || length <= 0) | 677 if (i->offset + length > [text length] || length <= 0) |
| 705 break; | 678 break; |
| 706 const NSRange range = NSMakeRange(location, length); | 679 const NSRange range = NSMakeRange(location, length); |
| 707 if (0 != (i->style & ACMatchClassification::MATCH)) { | 680 if (0 != (i->style & ACMatchClassification::MATCH)) { |
| 708 [as addAttribute:(id)kCTFontAttributeName | 681 [as addAttribute:NSFontAttributeName value:boldFontRef range:range]; |
| 709 value:(id)boldFontRef | |
| 710 range:range]; | |
| 711 } | 682 } |
| 712 | 683 |
| 713 if (0 != (i->style & ACMatchClassification::DIM)) { | 684 if (0 != (i->style & ACMatchClassification::DIM)) { |
| 714 [as addAttribute:(id)kCTForegroundColorAttributeName | 685 [as addAttribute:NSForegroundColorAttributeName |
| 715 value:(id)dimColor.CGColor | 686 value:dimColor |
| 716 range:range]; | 687 range:range]; |
| 717 } | 688 } |
| 718 } | 689 } |
| 719 } | 690 } |
| 720 return as; | 691 return as; |
| 721 } | 692 } |
| 722 | 693 |
| 723 #pragma mark - | 694 #pragma mark - |
| 724 #pragma mark Table view delegate | 695 #pragma mark Table view delegate |
| 725 | 696 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 782 // The delete button never disappears if you don't call this after a tap. | 753 // The delete button never disappears if you don't call this after a tap. |
| 783 // It doesn't seem to be required anywhere else. | 754 // It doesn't seem to be required anywhere else. |
| 784 [_rows[indexPath.row] prepareForReuse]; | 755 [_rows[indexPath.row] prepareForReuse]; |
| 785 const AutocompleteMatch& match = | 756 const AutocompleteMatch& match = |
| 786 ((const AutocompleteResult&)_currentResult).match_at(indexPath.row); | 757 ((const AutocompleteResult&)_currentResult).match_at(indexPath.row); |
| 787 _popupView->DeleteMatch(match); | 758 _popupView->DeleteMatch(match); |
| 788 } | 759 } |
| 789 } | 760 } |
| 790 | 761 |
| 791 @end | 762 @end |
| OLD | NEW |