| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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/favicon/favicon_view.h" | 5 #import "ios/chrome/browser/ui/favicon/favicon_view.h" |
| 6 | 6 |
| 7 #import "ios/chrome/browser/ui/uikit_ui_util.h" | 7 #import "ios/chrome/browser/ui/uikit_ui_util.h" |
| 8 | 8 |
| 9 #if !defined(__has_feature) || !__has_feature(objc_arc) | 9 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 10 #error "This file requires ARC support." | 10 #error "This file requires ARC support." |
| 11 #endif | 11 #endif |
| 12 | 12 |
| 13 namespace { | 13 namespace { |
| 14 // Default corner radius for the favicon image view. | 14 // Default corner radius for the favicon image view. |
| 15 const CGFloat kDefaultCornerRadius = 3; | 15 const CGFloat kDefaultCornerRadius = 3; |
| 16 // Percentage of white for the default background, when there is no attributes. |
| 17 const CGFloat kDefaultWhitePercentage = 0.47; |
| 16 } | 18 } |
| 17 | 19 |
| 18 @interface FaviconViewNew () { | 20 @interface FaviconViewNew () { |
| 19 // Property releaser for FaviconViewNew. | 21 // Property releaser for FaviconViewNew. |
| 20 } | 22 } |
| 21 | 23 |
| 22 // Image view for the favicon. | 24 // Image view for the favicon. |
| 23 @property(nonatomic, strong) UIImageView* faviconImageView; | 25 @property(nonatomic, strong) UIImageView* faviconImageView; |
| 24 // Label for fallback favicon placeholder. | 26 // Label for fallback favicon placeholder. |
| 25 @property(nonatomic, strong) UILabel* faviconFallbackLabel; | 27 @property(nonatomic, strong) UILabel* faviconFallbackLabel; |
| 26 | 28 |
| 27 @end | 29 @end |
| 28 | 30 |
| 29 @implementation FaviconViewNew | 31 @implementation FaviconViewNew |
| 30 @synthesize faviconImageView = _faviconImageView; | 32 @synthesize faviconImageView = _faviconImageView; |
| 31 @synthesize faviconFallbackLabel = _faviconFallbackLabel; | 33 @synthesize faviconFallbackLabel = _faviconFallbackLabel; |
| 32 | 34 |
| 33 - (instancetype)initWithFrame:(CGRect)frame { | 35 - (instancetype)initWithFrame:(CGRect)frame { |
| 34 self = [super initWithFrame:frame]; | 36 self = [super initWithFrame:frame]; |
| 35 if (self) { | 37 if (self) { |
| 36 _faviconImageView = [[UIImageView alloc] initWithFrame:self.bounds]; | 38 _faviconImageView = [[UIImageView alloc] initWithFrame:self.bounds]; |
| 37 _faviconImageView.clipsToBounds = YES; | 39 _faviconImageView.clipsToBounds = YES; |
| 38 _faviconImageView.layer.cornerRadius = kDefaultCornerRadius; | 40 _faviconImageView.layer.cornerRadius = kDefaultCornerRadius; |
| 39 _faviconImageView.image = nil; | 41 _faviconImageView.image = nil; |
| 40 | 42 |
| 41 _faviconFallbackLabel = [[UILabel alloc] initWithFrame:self.bounds]; | 43 _faviconFallbackLabel = [[UILabel alloc] initWithFrame:self.bounds]; |
| 42 _faviconFallbackLabel.backgroundColor = [UIColor clearColor]; | 44 _faviconFallbackLabel.backgroundColor = |
| 45 [UIColor colorWithWhite:kDefaultWhitePercentage alpha:1]; |
| 43 _faviconFallbackLabel.textAlignment = NSTextAlignmentCenter; | 46 _faviconFallbackLabel.textAlignment = NSTextAlignmentCenter; |
| 44 _faviconFallbackLabel.isAccessibilityElement = NO; | 47 _faviconFallbackLabel.isAccessibilityElement = NO; |
| 45 _faviconFallbackLabel.clipsToBounds = YES; | 48 _faviconFallbackLabel.clipsToBounds = YES; |
| 46 _faviconFallbackLabel.layer.cornerRadius = kDefaultCornerRadius; | 49 _faviconFallbackLabel.layer.cornerRadius = kDefaultCornerRadius; |
| 47 _faviconFallbackLabel.text = nil; | 50 _faviconFallbackLabel.text = nil; |
| 48 | 51 |
| 49 [self addSubview:_faviconFallbackLabel]; | 52 [self addSubview:_faviconFallbackLabel]; |
| 50 [self addSubview:_faviconImageView]; | 53 [self addSubview:_faviconImageView]; |
| 51 | 54 |
| 52 [_faviconImageView setTranslatesAutoresizingMaskIntoConstraints:NO]; | 55 [_faviconImageView setTranslatesAutoresizingMaskIntoConstraints:NO]; |
| 53 [_faviconFallbackLabel setTranslatesAutoresizingMaskIntoConstraints:NO]; | 56 [_faviconFallbackLabel setTranslatesAutoresizingMaskIntoConstraints:NO]; |
| 54 | 57 |
| 55 // Both image and fallback label are centered and match the size of favicon. | 58 // Both image and fallback label are centered and match the size of favicon. |
| 56 AddSameCenterConstraints(_faviconImageView, self); | 59 AddSameCenterConstraints(_faviconImageView, self); |
| 57 AddSameCenterConstraints(_faviconFallbackLabel, self); | 60 AddSameCenterConstraints(_faviconFallbackLabel, self); |
| 58 AddSameSizeConstraint(_faviconFallbackLabel, self); | 61 AddSameSizeConstraint(_faviconFallbackLabel, self); |
| 59 AddSameSizeConstraint(_faviconImageView, self); | 62 AddSameSizeConstraint(_faviconImageView, self); |
| 60 } | 63 } |
| 61 return self; | 64 return self; |
| 62 } | 65 } |
| 63 | 66 |
| 64 - (void)configureWithAttributes:(FaviconAttributes*)attributes { | 67 - (void)configureWithAttributes:(FaviconAttributes*)attributes { |
| 68 if (!attributes) { |
| 69 self.faviconFallbackLabel.backgroundColor = |
| 70 [UIColor colorWithWhite:kDefaultWhitePercentage alpha:1]; |
| 71 self.faviconFallbackLabel.text = nil; |
| 72 self.faviconFallbackLabel.hidden = NO; |
| 73 self.faviconImageView.hidden = YES; |
| 74 return; |
| 75 } |
| 76 |
| 65 if (attributes.faviconImage) { | 77 if (attributes.faviconImage) { |
| 66 self.faviconImageView.image = attributes.faviconImage; | 78 self.faviconImageView.image = attributes.faviconImage; |
| 67 self.faviconImageView.hidden = NO; | 79 self.faviconImageView.hidden = NO; |
| 68 self.faviconFallbackLabel.hidden = YES; | 80 self.faviconFallbackLabel.hidden = YES; |
| 69 } else { | 81 } else { |
| 70 self.faviconFallbackLabel.backgroundColor = attributes.backgroundColor; | 82 self.faviconFallbackLabel.backgroundColor = attributes.backgroundColor; |
| 71 self.faviconFallbackLabel.textColor = attributes.textColor; | 83 self.faviconFallbackLabel.textColor = attributes.textColor; |
| 72 self.faviconFallbackLabel.text = attributes.monogramString; | 84 self.faviconFallbackLabel.text = attributes.monogramString; |
| 73 self.faviconFallbackLabel.hidden = NO; | 85 self.faviconFallbackLabel.hidden = NO; |
| 74 self.faviconImageView.hidden = YES; | 86 self.faviconImageView.hidden = YES; |
| 75 } | 87 } |
| 76 } | 88 } |
| 77 | 89 |
| 78 - (void)setFont:(UIFont*)font { | 90 - (void)setFont:(UIFont*)font { |
| 79 self.faviconFallbackLabel.font = font; | 91 self.faviconFallbackLabel.font = font; |
| 80 } | 92 } |
| 81 | 93 |
| 82 #pragma mark - UIView | 94 #pragma mark - UIView |
| 83 | 95 |
| 84 - (CGSize)intrinsicContentSize { | 96 - (CGSize)intrinsicContentSize { |
| 85 return CGSizeMake(kFaviconPreferredSize, kFaviconPreferredSize); | 97 return CGSizeMake(kFaviconPreferredSize, kFaviconPreferredSize); |
| 86 } | 98 } |
| 87 | 99 |
| 88 @end | 100 @end |
| OLD | NEW |