OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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/ntp/new_tab_page_bar_button.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/mac/objc_property_releaser.h" |
| 9 #import "ios/chrome/browser/ui/ntp/new_tab_page_bar_item.h" |
| 10 #import "ios/chrome/browser/ui/uikit_ui_util.h" |
| 11 #import "ios/third_party/material_components_ios/src/components/Typography/src/M
aterialTypography.h" |
| 12 |
| 13 namespace { |
| 14 |
| 15 const int kButtonColor = 0x333333; |
| 16 const int kButtonSelectedColor = 0x4285F4; |
| 17 |
| 18 } // anonymous namespace |
| 19 |
| 20 @interface NewTabPageBarButton () { |
| 21 UIColor* _color; |
| 22 UIColor* _selectedColor; |
| 23 UIColor* _incognitoColor; |
| 24 UIColor* _incognitoSelectedColor; |
| 25 UIColor* _interpolatedColor; |
| 26 UIColor* _interpolatedSelectedColor; |
| 27 |
| 28 UIImage* _image; |
| 29 NSString* _title; |
| 30 base::mac::ObjCPropertyReleaser _propertyReleaser_NewTabPageBarButton; |
| 31 } |
| 32 |
| 33 @property(nonatomic, retain) UIColor* color; |
| 34 @property(nonatomic, retain) UIColor* selectedColor; |
| 35 @property(nonatomic, retain) UIColor* incognitoColor; |
| 36 @property(nonatomic, retain) UIColor* incognitoSelectedColor; |
| 37 @property(nonatomic, retain) UIColor* interpolatedColor; |
| 38 @property(nonatomic, retain) UIColor* interpolatedSelectedColor; |
| 39 @property(nonatomic, retain) UIImage* image; |
| 40 @property(nonatomic, copy) NSString* title; |
| 41 |
| 42 // Sets the tint color of the button to |interpolatedColor| or |
| 43 // |interpolatedSelectedColor|, depending on the state of the button. |
| 44 - (void)refreshTintColor; |
| 45 |
| 46 @end |
| 47 |
| 48 @implementation NewTabPageBarButton |
| 49 |
| 50 @synthesize color = _color; |
| 51 @synthesize selectedColor = _selectedColor; |
| 52 @synthesize incognitoColor = _incognitoColor; |
| 53 @synthesize incognitoSelectedColor = _incognitoSelectedColor; |
| 54 @synthesize interpolatedColor = _interpolatedColor; |
| 55 @synthesize interpolatedSelectedColor = _interpolatedSelectedColor; |
| 56 @synthesize image = _image; |
| 57 @synthesize title = _title; |
| 58 |
| 59 + (instancetype)buttonWithItem:(NewTabPageBarItem*)item { |
| 60 DCHECK(item); |
| 61 DCHECK(item.title); |
| 62 DCHECK(item.image); |
| 63 NewTabPageBarButton* button = |
| 64 [[self class] buttonWithType:UIButtonTypeCustom]; |
| 65 button->_propertyReleaser_NewTabPageBarButton.Init( |
| 66 button, [NewTabPageBarButton class]); |
| 67 |
| 68 button.title = item.title; |
| 69 button.image = |
| 70 [item.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; |
| 71 button.color = UIColorFromRGB(kButtonColor, 1.0); |
| 72 button.selectedColor = UIColorFromRGB(kButtonSelectedColor, 1.0); |
| 73 button.incognitoColor = [UIColor colorWithWhite:1 alpha:0.5]; |
| 74 button.incognitoSelectedColor = [UIColor whiteColor]; |
| 75 |
| 76 button.autoresizingMask = UIViewAutoresizingFlexibleWidth; |
| 77 button.adjustsImageWhenHighlighted = NO; |
| 78 button.accessibilityLabel = item.title; |
| 79 button.titleLabel.font = [MDCTypography body2Font]; |
| 80 button.titleLabel.adjustsFontSizeToFitWidth = YES; |
| 81 button.titleLabel.minimumScaleFactor = 0.6; |
| 82 |
| 83 [button useIncognitoColorScheme:0]; |
| 84 [button setContentToDisplay:new_tab_page_bar_button::ContentType::TEXT]; |
| 85 return button; |
| 86 } |
| 87 |
| 88 - (void)useIncognitoColorScheme:(CGFloat)percentage { |
| 89 DCHECK(percentage >= 0 && percentage <= 1); |
| 90 self.interpolatedColor = |
| 91 InterpolateFromColorToColor(_color, _incognitoColor, percentage); |
| 92 self.interpolatedSelectedColor = InterpolateFromColorToColor( |
| 93 _selectedColor, _incognitoSelectedColor, percentage); |
| 94 |
| 95 [self setTitleColor:_interpolatedColor forState:UIControlStateNormal]; |
| 96 [self setTitleColor:_interpolatedSelectedColor |
| 97 forState:UIControlStateSelected]; |
| 98 [self setTitleColor:_interpolatedSelectedColor |
| 99 forState:UIControlStateHighlighted]; |
| 100 |
| 101 [self refreshTintColor]; |
| 102 } |
| 103 |
| 104 - (void)setContentToDisplay:(new_tab_page_bar_button::ContentType)contentType { |
| 105 switch (contentType) { |
| 106 case new_tab_page_bar_button::ContentType::IMAGE: |
| 107 [self setImage:_image forState:UIControlStateNormal]; |
| 108 [self setTitle:nil forState:UIControlStateNormal]; |
| 109 break; |
| 110 case new_tab_page_bar_button::ContentType::TEXT: |
| 111 [self setImage:nil forState:UIControlStateNormal]; |
| 112 [self setTitle:_title forState:UIControlStateNormal]; |
| 113 break; |
| 114 } |
| 115 } |
| 116 |
| 117 - (void)refreshTintColor { |
| 118 if (self.selected || self.highlighted) { |
| 119 [self setTintColor:_interpolatedSelectedColor]; |
| 120 } else { |
| 121 [self setTintColor:_interpolatedColor]; |
| 122 } |
| 123 } |
| 124 |
| 125 #pragma mark - |
| 126 #pragma mark UIButton overrides |
| 127 |
| 128 - (void)setSelected:(BOOL)selected { |
| 129 [super setSelected:selected]; |
| 130 [self refreshTintColor]; |
| 131 } |
| 132 |
| 133 - (void)setHighlighted:(BOOL)highlighted { |
| 134 [super setHighlighted:highlighted]; |
| 135 [self refreshTintColor]; |
| 136 } |
| 137 |
| 138 @end |
OLD | NEW |