| 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" |
| 11 #include "base/mac/scoped_nsobject.h" | |
| 12 #include "base/strings/sys_string_conversions.h" | 11 #include "base/strings/sys_string_conversions.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 14 #import "components/image_fetcher/ios/ios_image_data_fetcher_wrapper.h" | 13 #import "components/image_fetcher/ios/ios_image_data_fetcher_wrapper.h" |
| 15 #include "components/omnibox/browser/autocomplete_input.h" | 14 #include "components/omnibox/browser/autocomplete_input.h" |
| 16 #include "components/omnibox/browser/autocomplete_match.h" | 15 #include "components/omnibox/browser/autocomplete_match.h" |
| 17 #include "components/omnibox/browser/autocomplete_result.h" | 16 #include "components/omnibox/browser/autocomplete_result.h" |
| 18 #include "components/omnibox/browser/suggestion_answer.h" | 17 #include "components/omnibox/browser/suggestion_answer.h" |
| 19 #include "ios/chrome/browser/ui/animation_util.h" | 18 #include "ios/chrome/browser/ui/animation_util.h" |
| 20 #import "ios/chrome/browser/ui/omnibox/omnibox_popup_material_row.h" | 19 #import "ios/chrome/browser/ui/omnibox/omnibox_popup_material_row.h" |
| 21 #import "ios/chrome/browser/ui/omnibox/omnibox_popup_view_ios.h" | 20 #import "ios/chrome/browser/ui/omnibox/omnibox_popup_view_ios.h" |
| 22 #include "ios/chrome/browser/ui/omnibox/omnibox_util.h" | 21 #include "ios/chrome/browser/ui/omnibox/omnibox_util.h" |
| 23 #import "ios/chrome/browser/ui/omnibox/truncating_attributed_label.h" | 22 #import "ios/chrome/browser/ui/omnibox/truncating_attributed_label.h" |
| 24 #include "ios/chrome/browser/ui/rtl_geometry.h" | 23 #include "ios/chrome/browser/ui/rtl_geometry.h" |
| 25 #include "ios/chrome/browser/ui/ui_util.h" | 24 #include "ios/chrome/browser/ui/ui_util.h" |
| 26 #import "ios/chrome/browser/ui/uikit_ui_util.h" | 25 #import "ios/chrome/browser/ui/uikit_ui_util.h" |
| 27 #include "ios/chrome/grit/ios_theme_resources.h" | 26 #include "ios/chrome/grit/ios_theme_resources.h" |
| 28 #import "ios/third_party/material_components_ios/src/components/Typography/src/M
aterialTypography.h" | 27 #import "ios/third_party/material_components_ios/src/components/Typography/src/M
aterialTypography.h" |
| 29 #import "ios/third_party/material_roboto_font_loader_ios/src/src/MaterialRobotoF
ontLoader.h" | 28 #import "ios/third_party/material_roboto_font_loader_ios/src/src/MaterialRobotoF
ontLoader.h" |
| 30 #include "net/base/escape.h" | 29 #include "net/base/escape.h" |
| 31 | 30 |
| 31 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 32 #error "This file requires ARC support." |
| 33 #endif |
| 34 |
| 32 namespace { | 35 namespace { |
| 33 const int kRowCount = 6; | 36 const int kRowCount = 6; |
| 34 const CGFloat kRowHeight = 48.0; | 37 const CGFloat kRowHeight = 48.0; |
| 35 const CGFloat kAnswerRowHeight = 64.0; | 38 const CGFloat kAnswerRowHeight = 64.0; |
| 36 const CGFloat kTopAndBottomPadding = 8.0; | 39 const CGFloat kTopAndBottomPadding = 8.0; |
| 37 // The color of the main text of a suggest cell. | 40 // The color of the main text of a suggest cell. |
| 38 UIColor* SuggestionTextColor() { | 41 UIColor* SuggestionTextColor() { |
| 39 return [UIColor colorWithWhite:(51 / 255.0) alpha:1.0]; | 42 return [UIColor colorWithWhite:(51 / 255.0) alpha:1.0]; |
| 40 } | 43 } |
| 41 // The color of the detail text of a suggest cell. | 44 // The color of the detail text of a suggest cell. |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 | 82 |
| 80 OmniboxPopupViewIOS* _popupView; // weak, owns us | 83 OmniboxPopupViewIOS* _popupView; // weak, owns us |
| 81 | 84 |
| 82 // Fetcher for Answers in Suggest images. | 85 // Fetcher for Answers in Suggest images. |
| 83 std::unique_ptr<image_fetcher::IOSImageDataFetcherWrapper> imageFetcher_; | 86 std::unique_ptr<image_fetcher::IOSImageDataFetcherWrapper> imageFetcher_; |
| 84 | 87 |
| 85 // The data source. | 88 // The data source. |
| 86 AutocompleteResult _currentResult; | 89 AutocompleteResult _currentResult; |
| 87 | 90 |
| 88 // Array containing the OmniboxPopupMaterialRow objects displayed in the view. | 91 // Array containing the OmniboxPopupMaterialRow objects displayed in the view. |
| 89 base::scoped_nsobject<NSArray> _rows; | 92 NSArray* _rows; |
| 90 | 93 |
| 91 // The height of the keyboard. Used to determine the content inset for the | 94 // The height of the keyboard. Used to determine the content inset for the |
| 92 // scroll view. | 95 // scroll view. |
| 93 CGFloat keyboardHeight_; | 96 CGFloat keyboardHeight_; |
| 94 } | 97 } |
| 95 | 98 |
| 96 @end | 99 @end |
| 97 | 100 |
| 98 @implementation OmniboxPopupMaterialViewController | 101 @implementation OmniboxPopupMaterialViewController |
| 99 | 102 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 121 name:UIKeyboardDidShowNotification | 124 name:UIKeyboardDidShowNotification |
| 122 object:nil]; | 125 object:nil]; |
| 123 } | 126 } |
| 124 } | 127 } |
| 125 return self; | 128 return self; |
| 126 } | 129 } |
| 127 | 130 |
| 128 - (void)dealloc { | 131 - (void)dealloc { |
| 129 self.tableView.delegate = nil; | 132 self.tableView.delegate = nil; |
| 130 [[NSNotificationCenter defaultCenter] removeObserver:self]; | 133 [[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 131 [super dealloc]; | |
| 132 } | 134 } |
| 133 | 135 |
| 134 - (UIScrollView*)scrollView { | 136 - (UIScrollView*)scrollView { |
| 135 return (UIScrollView*)self.tableView; | 137 return (UIScrollView*)self.tableView; |
| 136 } | 138 } |
| 137 | 139 |
| 138 - (void)viewDidLoad { | 140 - (void)viewDidLoad { |
| 139 [super viewDidLoad]; | 141 [super viewDidLoad]; |
| 140 | 142 |
| 141 // Initialize the same size as the parent view, autoresize will correct this. | 143 // Initialize the same size as the parent view, autoresize will correct this. |
| 142 [self.view setFrame:CGRectZero]; | 144 [self.view setFrame:CGRectZero]; |
| 143 if (_incognito) { | 145 if (_incognito) { |
| 144 self.view.backgroundColor = BackgroundColorIncognito(); | 146 self.view.backgroundColor = BackgroundColorIncognito(); |
| 145 } else { | 147 } else { |
| 146 self.view.backgroundColor = | 148 self.view.backgroundColor = |
| 147 IsIPadIdiom() ? BackgroundColorTablet() : BackgroundColorPhone(); | 149 IsIPadIdiom() ? BackgroundColorTablet() : BackgroundColorPhone(); |
| 148 } | 150 } |
| 149 [self.view setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | | 151 [self.view setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | |
| 150 UIViewAutoresizingFlexibleHeight)]; | 152 UIViewAutoresizingFlexibleHeight)]; |
| 151 | 153 |
| 152 // Cache fonts needed for omnibox attributed string. | 154 // Cache fonts needed for omnibox attributed string. |
| 153 NSMutableArray* rowsBuilder = [[[NSMutableArray alloc] init] autorelease]; | 155 NSMutableArray* rowsBuilder = [[NSMutableArray alloc] init]; |
| 154 for (int i = 0; i < kRowCount; i++) { | 156 for (int i = 0; i < kRowCount; i++) { |
| 155 OmniboxPopupMaterialRow* row = [[[OmniboxPopupMaterialRow alloc] | 157 OmniboxPopupMaterialRow* row = |
| 156 initWithIncognito:_incognito] autorelease]; | 158 [[OmniboxPopupMaterialRow alloc] initWithIncognito:_incognito]; |
| 157 row.accessibilityIdentifier = | 159 row.accessibilityIdentifier = |
| 158 [NSString stringWithFormat:@"omnibox suggestion %i", i]; | 160 [NSString stringWithFormat:@"omnibox suggestion %i", i]; |
| 159 row.autoresizingMask = UIViewAutoresizingFlexibleWidth; | 161 row.autoresizingMask = UIViewAutoresizingFlexibleWidth; |
| 160 [rowsBuilder addObject:row]; | 162 [rowsBuilder addObject:row]; |
| 161 [row.appendButton addTarget:self | 163 [row.appendButton addTarget:self |
| 162 action:@selector(appendButtonTapped:) | 164 action:@selector(appendButtonTapped:) |
| 163 forControlEvents:UIControlEventTouchUpInside]; | 165 forControlEvents:UIControlEventTouchUpInside]; |
| 164 [row.appendButton setTag:i]; | 166 [row.appendButton setTag:i]; |
| 165 row.rowHeight = kRowHeight; | 167 row.rowHeight = kRowHeight; |
| 166 } | 168 } |
| 167 _rows.reset([rowsBuilder copy]); | 169 _rows = [rowsBuilder copy]; |
| 168 | 170 |
| 169 // Table configuration. | 171 // Table configuration. |
| 170 self.tableView.allowsMultipleSelectionDuringEditing = NO; | 172 self.tableView.allowsMultipleSelectionDuringEditing = NO; |
| 171 self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; | 173 self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; |
| 172 self.tableView.separatorInset = UIEdgeInsetsZero; | 174 self.tableView.separatorInset = UIEdgeInsetsZero; |
| 173 if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) { | 175 if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) { |
| 174 [self.tableView setLayoutMargins:UIEdgeInsetsZero]; | 176 [self.tableView setLayoutMargins:UIEdgeInsetsZero]; |
| 175 } | 177 } |
| 176 self.automaticallyAdjustsScrollViewInsets = NO; | 178 self.automaticallyAdjustsScrollViewInsets = NO; |
| 177 [self.tableView setContentInset:UIEdgeInsetsMake(kTopAndBottomPadding, 0, | 179 [self.tableView setContentInset:UIEdgeInsetsMake(kTopAndBottomPadding, 0, |
| 178 kTopAndBottomPadding, 0)]; | 180 kTopAndBottomPadding, 0)]; |
| 179 } | 181 } |
| 180 | 182 |
| 181 - (void)didReceiveMemoryWarning { | 183 - (void)didReceiveMemoryWarning { |
| 182 [super didReceiveMemoryWarning]; | 184 [super didReceiveMemoryWarning]; |
| 183 if (![self isViewLoaded]) { | 185 if (![self isViewLoaded]) { |
| 184 _rows.reset(); | 186 _rows = nil; |
| 185 } | 187 } |
| 186 } | 188 } |
| 187 | 189 |
| 188 - (void)traitCollectionDidChange:(UITraitCollection*)previousTraitCollection { | 190 - (void)traitCollectionDidChange:(UITraitCollection*)previousTraitCollection { |
| 189 [self layoutRows]; | 191 [self layoutRows]; |
| 190 } | 192 } |
| 191 | 193 |
| 192 #pragma mark - | 194 #pragma mark - |
| 193 #pragma mark Updating data and UI | 195 #pragma mark Updating data and UI |
| 194 | 196 |
| (...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 416 frame = detailTextLabel.frame; | 418 frame = detailTextLabel.frame; |
| 417 frame.size.width -= kLTRTextInRTLLayoutLeftPadding - frame.origin.x; | 419 frame.size.width -= kLTRTextInRTLLayoutLeftPadding - frame.origin.x; |
| 418 frame.origin.x = kLTRTextInRTLLayoutLeftPadding; | 420 frame.origin.x = kLTRTextInRTLLayoutLeftPadding; |
| 419 detailTextLabel.frame = frame; | 421 detailTextLabel.frame = frame; |
| 420 } | 422 } |
| 421 } | 423 } |
| 422 | 424 |
| 423 - (NSMutableAttributedString*)attributedStringWithAnswerLine: | 425 - (NSMutableAttributedString*)attributedStringWithAnswerLine: |
| 424 (const SuggestionAnswer::ImageLine&)line { | 426 (const SuggestionAnswer::ImageLine&)line { |
| 425 NSMutableAttributedString* result = | 427 NSMutableAttributedString* result = |
| 426 [[[NSMutableAttributedString alloc] initWithString:@""] autorelease]; | 428 [[NSMutableAttributedString alloc] initWithString:@""]; |
| 427 | 429 |
| 428 for (size_t i = 0; i < line.text_fields().size(); i++) { | 430 for (size_t i = 0; i < line.text_fields().size(); i++) { |
| 429 const SuggestionAnswer::TextField& field = line.text_fields()[i]; | 431 const SuggestionAnswer::TextField& field = line.text_fields()[i]; |
| 430 [result | 432 [result |
| 431 appendAttributedString:[self attributedStringWithString:field.text() | 433 appendAttributedString:[self attributedStringWithString:field.text() |
| 432 type:field.type()]]; | 434 type:field.type()]]; |
| 433 } | 435 } |
| 434 | 436 |
| 435 base::scoped_nsobject<NSAttributedString> spacer( | 437 NSAttributedString* spacer = |
| 436 [[NSAttributedString alloc] initWithString:@" "]); | 438 [[NSAttributedString alloc] initWithString:@" "]; |
| 437 if (line.additional_text() != nil) { | 439 if (line.additional_text() != nil) { |
| 438 const SuggestionAnswer::TextField* field = line.additional_text(); | 440 const SuggestionAnswer::TextField* field = line.additional_text(); |
| 439 [result appendAttributedString:spacer]; | 441 [result appendAttributedString:spacer]; |
| 440 [result | 442 [result |
| 441 appendAttributedString:[self attributedStringWithString:field->text() | 443 appendAttributedString:[self attributedStringWithString:field->text() |
| 442 type:field->type()]]; | 444 type:field->type()]]; |
| 443 } | 445 } |
| 444 | 446 |
| 445 if (line.status_text() != nil) { | 447 if (line.status_text() != nil) { |
| 446 const SuggestionAnswer::TextField* field = line.status_text(); | 448 const SuggestionAnswer::TextField* field = line.status_text(); |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 526 NSString* unescapedString = | 528 NSString* unescapedString = |
| 527 base::SysUTF16ToNSString(net::UnescapeForHTML(string)); | 529 base::SysUTF16ToNSString(net::UnescapeForHTML(string)); |
| 528 // TODO(jdonnelly): Remove this tag stripping once the JSON parsing class | 530 // TODO(jdonnelly): Remove this tag stripping once the JSON parsing class |
| 529 // handles HTML tags. | 531 // handles HTML tags. |
| 530 unescapedString = [unescapedString stringByReplacingOccurrencesOfString:@"<b>" | 532 unescapedString = [unescapedString stringByReplacingOccurrencesOfString:@"<b>" |
| 531 withString:@""]; | 533 withString:@""]; |
| 532 unescapedString = | 534 unescapedString = |
| 533 [unescapedString stringByReplacingOccurrencesOfString:@"</b>" | 535 [unescapedString stringByReplacingOccurrencesOfString:@"</b>" |
| 534 withString:@""]; | 536 withString:@""]; |
| 535 | 537 |
| 536 return [[[NSAttributedString alloc] initWithString:unescapedString | 538 return [[NSAttributedString alloc] initWithString:unescapedString |
| 537 attributes:attributes] autorelease]; | 539 attributes:attributes]; |
| 538 } | 540 } |
| 539 | 541 |
| 540 - (void)updateMatches:(const AutocompleteResult&)result | 542 - (void)updateMatches:(const AutocompleteResult&)result |
| 541 withAnimation:(BOOL)animation { | 543 withAnimation:(BOOL)animation { |
| 542 AutocompleteResult oldResults; | 544 AutocompleteResult oldResults; |
| 543 AutocompleteInput emptyInput; | 545 AutocompleteInput emptyInput; |
| 544 oldResults.Swap(&_currentResult); | 546 oldResults.Swap(&_currentResult); |
| 545 _currentResult.CopyOldMatches(emptyInput, result, nil); | 547 _currentResult.CopyOldMatches(emptyInput, result, nil); |
| 546 | 548 |
| 547 [self layoutRows]; | 549 [self layoutRows]; |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 648 #pragma mark UIScrollViewDelegate | 650 #pragma mark UIScrollViewDelegate |
| 649 | 651 |
| 650 - (void)scrollViewDidScroll:(UIScrollView*)scrollView { | 652 - (void)scrollViewDidScroll:(UIScrollView*)scrollView { |
| 651 // Setting the top inset of the scrollView to |kTopAndBottomPadding| causes a | 653 // Setting the top inset of the scrollView to |kTopAndBottomPadding| causes a |
| 652 // one time scrollViewDidScroll to |-kTopAndBottomPadding|. It's easier to | 654 // one time scrollViewDidScroll to |-kTopAndBottomPadding|. It's easier to |
| 653 // just ignore this one scroll tick. | 655 // just ignore this one scroll tick. |
| 654 if (scrollView.contentOffset.y == 0 - kTopAndBottomPadding) | 656 if (scrollView.contentOffset.y == 0 - kTopAndBottomPadding) |
| 655 return; | 657 return; |
| 656 | 658 |
| 657 _popupView->DidScroll(); | 659 _popupView->DidScroll(); |
| 658 for (OmniboxPopupMaterialRow* row in _rows.get()) { | 660 for (OmniboxPopupMaterialRow* row in _rows) { |
| 659 row.highlighted = NO; | 661 row.highlighted = NO; |
| 660 } | 662 } |
| 661 } | 663 } |
| 662 | 664 |
| 663 // Set text alignment for popup cells. | 665 // Set text alignment for popup cells. |
| 664 - (void)setTextAlignment:(NSTextAlignment)alignment { | 666 - (void)setTextAlignment:(NSTextAlignment)alignment { |
| 665 _alignment = alignment; | 667 _alignment = alignment; |
| 666 } | 668 } |
| 667 | 669 |
| 668 - (BOOL)isSearchMatch:(const AutocompleteMatch::Type&)type { | 670 - (BOOL)isSearchMatch:(const AutocompleteMatch::Type&)type { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 680 smallFont:(BOOL)smallFont | 682 smallFont:(BOOL)smallFont |
| 681 color:(UIColor*)defaultColor | 683 color:(UIColor*)defaultColor |
| 682 dimColor:(UIColor*)dimColor { | 684 dimColor:(UIColor*)dimColor { |
| 683 if (text == nil) | 685 if (text == nil) |
| 684 return nil; | 686 return nil; |
| 685 | 687 |
| 686 UIFont* fontRef = | 688 UIFont* fontRef = |
| 687 smallFont ? [MDCTypography body1Font] : [MDCTypography subheadFont]; | 689 smallFont ? [MDCTypography body1Font] : [MDCTypography subheadFont]; |
| 688 | 690 |
| 689 NSMutableAttributedString* as = | 691 NSMutableAttributedString* as = |
| 690 [[[NSMutableAttributedString alloc] initWithString:text] autorelease]; | 692 [[NSMutableAttributedString alloc] initWithString:text]; |
| 691 | 693 |
| 692 // Set the base attributes to the default font and color. | 694 // Set the base attributes to the default font and color. |
| 693 NSDictionary* dict = @{ | 695 NSDictionary* dict = @{ |
| 694 NSFontAttributeName : fontRef, | 696 NSFontAttributeName : fontRef, |
| 695 NSForegroundColorAttributeName : defaultColor, | 697 NSForegroundColorAttributeName : defaultColor, |
| 696 }; | 698 }; |
| 697 [as addAttributes:dict range:NSMakeRange(0, [text length])]; | 699 [as addAttributes:dict range:NSMakeRange(0, [text length])]; |
| 698 | 700 |
| 699 if (classifications != NULL) { | 701 if (classifications != NULL) { |
| 700 UIFont* boldFontRef = [[MDFRobotoFontLoader sharedInstance] | 702 UIFont* boldFontRef = [[MDFRobotoFontLoader sharedInstance] |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 787 // The delete button never disappears if you don't call this after a tap. | 789 // The delete button never disappears if you don't call this after a tap. |
| 788 // It doesn't seem to be required anywhere else. | 790 // It doesn't seem to be required anywhere else. |
| 789 [_rows[indexPath.row] prepareForReuse]; | 791 [_rows[indexPath.row] prepareForReuse]; |
| 790 const AutocompleteMatch& match = | 792 const AutocompleteMatch& match = |
| 791 ((const AutocompleteResult&)_currentResult).match_at(indexPath.row); | 793 ((const AutocompleteResult&)_currentResult).match_at(indexPath.row); |
| 792 _popupView->DeleteMatch(match); | 794 _popupView->DeleteMatch(match); |
| 793 } | 795 } |
| 794 } | 796 } |
| 795 | 797 |
| 796 @end | 798 @end |
| OLD | NEW |