OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "ios/chrome/browser/ui/omnibox/omnibox_popup_material_row.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/mac/objc_property_releaser.h" |
| 9 #import "ios/chrome/browser/ui/omnibox/truncating_attributed_label.h" |
| 10 #include "ios/chrome/browser/ui/rtl_geometry.h" |
| 11 #include "ios/chrome/browser/ui/ui_util.h" |
| 12 #import "ios/chrome/browser/ui/uikit_ui_util.h" |
| 13 #include "ios/chrome/grit/ios_theme_resources.h" |
| 14 #include "ui/base/resource/resource_bundle.h" |
| 15 |
| 16 namespace { |
| 17 const CGFloat kImageDimensionLength = 19.0; |
| 18 const CGFloat kLeadingPaddingIpad = 164; |
| 19 const CGFloat kLeadingPaddingIpadCompact = 71; |
| 20 const CGFloat kAppendButtonTrailingMargin = 4; |
| 21 } |
| 22 |
| 23 @interface OmniboxPopupMaterialRow () { |
| 24 BOOL _incognito; |
| 25 base::mac::ObjCPropertyReleaser _propertyReleaser_OmniboxPopupMaterialRow; |
| 26 } |
| 27 |
| 28 // Set the append button normal and highlighted images. |
| 29 - (void)updateAppendButtonImages; |
| 30 |
| 31 // Set the Physical Web image view image. |
| 32 - (void)updatePhysicalWebImage; |
| 33 |
| 34 @end |
| 35 |
| 36 @implementation OmniboxPopupMaterialRow |
| 37 |
| 38 @synthesize textTruncatingLabel = _textTruncatingLabel; |
| 39 @synthesize detailTruncatingLabel = _detailTruncatingLabel; |
| 40 @synthesize appendButton = _appendButton; |
| 41 @synthesize answerImageView = _answerImageView; |
| 42 @synthesize imageView = _imageView; |
| 43 @synthesize physicalWebImageView = _physicalWebImageView; |
| 44 @synthesize rowHeight = _rowHeight; |
| 45 |
| 46 - (instancetype)initWithStyle:(UITableViewCellStyle)style |
| 47 reuseIdentifier:(NSString*)reuseIdentifier { |
| 48 return [self initWithIncognito:NO]; |
| 49 } |
| 50 |
| 51 - (instancetype)initWithIncognito:(BOOL)incognito { |
| 52 self = [super initWithStyle:UITableViewCellStyleDefault |
| 53 reuseIdentifier:@"OmniboxPopupMaterialRow"]; |
| 54 if (self) { |
| 55 self.isAccessibilityElement = YES; |
| 56 self.backgroundColor = [UIColor clearColor]; |
| 57 _incognito = incognito; |
| 58 _propertyReleaser_OmniboxPopupMaterialRow.Init( |
| 59 self, [OmniboxPopupMaterialRow class]); |
| 60 |
| 61 _textTruncatingLabel = |
| 62 [[OmniboxPopupTruncatingLabel alloc] initWithFrame:CGRectZero]; |
| 63 _textTruncatingLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth; |
| 64 _textTruncatingLabel.userInteractionEnabled = NO; |
| 65 [self addSubview:_textTruncatingLabel]; |
| 66 |
| 67 _detailTruncatingLabel = |
| 68 [[OmniboxPopupTruncatingLabel alloc] initWithFrame:CGRectZero]; |
| 69 _detailTruncatingLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth; |
| 70 _detailTruncatingLabel.userInteractionEnabled = NO; |
| 71 [self addSubview:_detailTruncatingLabel]; |
| 72 |
| 73 _appendButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain]; |
| 74 [_appendButton setContentMode:UIViewContentModeRight]; |
| 75 [self updateAppendButtonImages]; |
| 76 // TODO(justincohen): Consider using the UITableViewCell's accessory view. |
| 77 // The current implementation is from before using a UITableViewCell. |
| 78 [self addSubview:_appendButton]; |
| 79 |
| 80 _physicalWebImageView = [[UIImageView alloc] initWithFrame:CGRectZero]; |
| 81 _physicalWebImageView.userInteractionEnabled = NO; |
| 82 _physicalWebImageView.contentMode = UIViewContentModeCenter; |
| 83 [self updatePhysicalWebImage]; |
| 84 [self addSubview:_physicalWebImageView]; |
| 85 |
| 86 // Left icon is only displayed on iPad. |
| 87 if (IsIPadIdiom()) { |
| 88 _imageView = [[UIImageView alloc] initWithFrame:CGRectZero]; |
| 89 _imageView.userInteractionEnabled = NO; |
| 90 _imageView.contentMode = UIViewContentModeCenter; |
| 91 |
| 92 // TODO(justincohen): Consider using the UITableViewCell's image view. |
| 93 // The current implementation is from before using a UITableViewCell. |
| 94 [self addSubview:_imageView]; |
| 95 } |
| 96 |
| 97 _answerImageView = [[UIImageView alloc] initWithFrame:CGRectZero]; |
| 98 _answerImageView.userInteractionEnabled = NO; |
| 99 _answerImageView.contentMode = UIViewContentModeScaleAspectFit; |
| 100 [self addSubview:_answerImageView]; |
| 101 } |
| 102 return self; |
| 103 } |
| 104 |
| 105 - (void)layoutSubviews { |
| 106 [super layoutSubviews]; |
| 107 [self layoutAccessoryViews]; |
| 108 } |
| 109 |
| 110 - (void)layoutAccessoryViews { |
| 111 LayoutRect imageViewLayout = LayoutRectMake( |
| 112 IsCompactTablet() ? kLeadingPaddingIpadCompact : kLeadingPaddingIpad, |
| 113 CGRectGetWidth(self.bounds), |
| 114 floor((_rowHeight - kImageDimensionLength) / 2), kImageDimensionLength, |
| 115 kImageDimensionLength); |
| 116 _imageView.frame = LayoutRectGetRect(imageViewLayout); |
| 117 |
| 118 CGFloat appendButtonDimensionLength = CGRectGetHeight(self.bounds); |
| 119 DCHECK_GT(appendButtonDimensionLength, 40); |
| 120 LayoutRect rightAccessoryLayout = |
| 121 LayoutRectMake(CGRectGetWidth(self.bounds) - appendButtonDimensionLength - |
| 122 kAppendButtonTrailingMargin, |
| 123 CGRectGetWidth(self.bounds), |
| 124 floor((_rowHeight - appendButtonDimensionLength) / 2), |
| 125 appendButtonDimensionLength, appendButtonDimensionLength); |
| 126 _appendButton.frame = LayoutRectGetRect(rightAccessoryLayout); |
| 127 _physicalWebImageView.frame = LayoutRectGetRect(rightAccessoryLayout); |
| 128 } |
| 129 |
| 130 - (void)updateLeftImage:(int)imageId { |
| 131 // Update the image. |
| 132 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 133 UIImage* image = rb.GetNativeImageNamed(imageId).ToUIImage(); |
| 134 _imageView.image = image; |
| 135 |
| 136 // Adjust the vertical position based on the current size of the row. |
| 137 CGRect frame = _imageView.frame; |
| 138 frame.origin.y = floor((_rowHeight - kImageDimensionLength) / 2); |
| 139 _imageView.frame = frame; |
| 140 } |
| 141 |
| 142 - (void)updateHighlightBackground:(BOOL)highlighted { |
| 143 // Set the background color to match the color of selected table view cells |
| 144 // when their selection style is UITableViewCellSelectionStyleGray. |
| 145 if (highlighted) { |
| 146 self.backgroundColor = _incognito ? [UIColor colorWithWhite:1 alpha:0.1] |
| 147 : [UIColor colorWithWhite:0 alpha:0.05]; |
| 148 } else { |
| 149 self.backgroundColor = [UIColor clearColor]; |
| 150 } |
| 151 } |
| 152 |
| 153 - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated { |
| 154 [super setHighlighted:highlighted animated:animated]; |
| 155 [self updateHighlightBackground:highlighted]; |
| 156 } |
| 157 |
| 158 - (void)setHighlighted:(BOOL)highlighted { |
| 159 [super setHighlighted:highlighted]; |
| 160 [self updateHighlightBackground:highlighted]; |
| 161 } |
| 162 |
| 163 - (void)updateAppendButtonImages { |
| 164 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 165 int appendResourceID = _incognito |
| 166 ? IDR_IOS_OMNIBOX_KEYBOARD_VIEW_APPEND_INCOGNITO |
| 167 : IDR_IOS_OMNIBOX_KEYBOARD_VIEW_APPEND; |
| 168 UIImage* appendImage = rb.GetNativeImageNamed(appendResourceID).ToUIImage(); |
| 169 if (UseRTLLayout()) { |
| 170 appendImage = [appendImage imageFlippedForRightToLeftLayoutDirection]; |
| 171 } |
| 172 |
| 173 [_appendButton setImage:appendImage forState:UIControlStateNormal]; |
| 174 int appendSelectedResourceID = |
| 175 _incognito ? IDR_IOS_OMNIBOX_KEYBOARD_VIEW_APPEND_INCOGNITO_HIGHLIGHTED |
| 176 : IDR_IOS_OMNIBOX_KEYBOARD_VIEW_APPEND_HIGHLIGHTED; |
| 177 UIImage* appendImageSelected = |
| 178 rb.GetNativeImageNamed(appendSelectedResourceID).ToUIImage(); |
| 179 [_appendButton setImage:appendImageSelected |
| 180 forState:UIControlStateHighlighted]; |
| 181 } |
| 182 |
| 183 - (void)updatePhysicalWebImage { |
| 184 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 185 UIImage* physicalWebImage = |
| 186 rb.GetNativeImageNamed(IDR_IOS_OMNIBOX_PHYSICAL_WEB).ToUIImage(); |
| 187 _physicalWebImageView.image = physicalWebImage; |
| 188 } |
| 189 |
| 190 - (NSString*)accessibilityLabel { |
| 191 return _textTruncatingLabel.attributedText.string; |
| 192 } |
| 193 |
| 194 - (NSString*)accessibilityValue { |
| 195 return _detailTruncatingLabel.attributedText.string; |
| 196 } |
| 197 |
| 198 @end |
OLD | NEW |