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 #include "ios/chrome/browser/ui/fancy_ui/tinted_button.h" | 5 #include "ios/chrome/browser/ui/fancy_ui/colored_button.h" |
6 | 6 |
7 #if !defined(__has_feature) || !__has_feature(objc_arc) | 7 #if !defined(__has_feature) || !__has_feature(objc_arc) |
8 #error "This file requires ARC support." | 8 #error "This file requires ARC support." |
9 #endif | 9 #endif |
10 | 10 |
11 @interface TintedButton () { | 11 @interface ColoredButton () { |
12 UIColor* normalStateTint_; | 12 UIColor* normalStateTint_; |
13 UIColor* highlightedTint_; | 13 UIColor* highlightedTint_; |
| 14 UIColor* normalStateBackgroundColor_; |
| 15 UIColor* highlightedBackgroundColor_; |
14 } | 16 } |
15 | 17 |
16 // Makes the button's tint color reflect its current state. | 18 // Makes the button's tint color reflect its current state. |
17 - (void)updateTint; | 19 - (void)updateTint; |
18 | 20 |
| 21 // Makes the button's background color reflect its current state. |
| 22 - (void)updateBackgroundColor; |
| 23 |
19 @end | 24 @end |
20 | 25 |
21 @implementation TintedButton | 26 @implementation ColoredButton |
22 | 27 |
23 - (void)setTintColor:(UIColor*)color forState:(UIControlState)state { | 28 - (void)setTintColor:(UIColor*)color forState:(UIControlState)state { |
24 switch (state) { | 29 switch (state) { |
25 case UIControlStateNormal: | 30 case UIControlStateNormal: |
26 normalStateTint_ = [color copy]; | 31 normalStateTint_ = [color copy]; |
27 break; | 32 break; |
28 case UIControlStateHighlighted: | 33 case UIControlStateHighlighted: |
29 highlightedTint_ = [color copy]; | 34 highlightedTint_ = [color copy]; |
30 break; | 35 break; |
31 default: | 36 default: |
32 return; | 37 return; |
33 } | 38 } |
34 | 39 |
35 if (normalStateTint_ || highlightedTint_) | 40 if (normalStateTint_ || highlightedTint_) |
36 self.adjustsImageWhenHighlighted = NO; | 41 self.adjustsImageWhenHighlighted = NO; |
37 else | 42 else |
38 self.adjustsImageWhenHighlighted = YES; | 43 self.adjustsImageWhenHighlighted = YES; |
39 [self updateTint]; | 44 [self updateTint]; |
40 } | 45 } |
41 | 46 |
| 47 - (void)setBackgroundColor:(UIColor*)color forState:(UIControlState)state { |
| 48 switch (state) { |
| 49 case UIControlStateNormal: |
| 50 normalStateBackgroundColor_ = [color copy]; |
| 51 break; |
| 52 case UIControlStateHighlighted: |
| 53 highlightedBackgroundColor_ = [color copy]; |
| 54 break; |
| 55 default: |
| 56 return; |
| 57 } |
| 58 |
| 59 [self updateBackgroundColor]; |
| 60 } |
| 61 |
42 #pragma mark - UIControl | 62 #pragma mark - UIControl |
43 | 63 |
44 - (void)setHighlighted:(BOOL)highlighted { | 64 - (void)setHighlighted:(BOOL)highlighted { |
45 [super setHighlighted:highlighted]; | 65 [super setHighlighted:highlighted]; |
46 [self updateTint]; | 66 [self updateTint]; |
| 67 [self updateBackgroundColor]; |
47 } | 68 } |
48 | 69 |
49 #pragma mark - Private | 70 #pragma mark - Private |
50 | 71 |
51 - (void)updateTint { | 72 - (void)updateTint { |
52 UIColor* newTint = nil; | 73 UIColor* newTint = nil; |
53 switch (self.state) { | 74 switch (self.state) { |
54 case UIControlStateNormal: | 75 case UIControlStateNormal: |
55 newTint = normalStateTint_; | 76 newTint = normalStateTint_; |
56 break; | 77 break; |
57 case UIControlStateHighlighted: | 78 case UIControlStateHighlighted: |
58 newTint = highlightedTint_; | 79 newTint = highlightedTint_; |
59 break; | 80 break; |
60 default: | 81 default: |
61 newTint = normalStateTint_; | 82 newTint = normalStateTint_; |
62 break; | 83 break; |
63 } | 84 } |
64 self.tintColor = newTint; | 85 self.tintColor = newTint; |
65 } | 86 } |
66 | 87 |
| 88 - (void)updateBackgroundColor { |
| 89 UIColor* newBackgroundColor = nil; |
| 90 switch (self.state) { |
| 91 case UIControlStateNormal: |
| 92 newBackgroundColor = normalStateBackgroundColor_; |
| 93 break; |
| 94 case UIControlStateHighlighted: |
| 95 newBackgroundColor = highlightedBackgroundColor_; |
| 96 break; |
| 97 default: |
| 98 newBackgroundColor = normalStateBackgroundColor_; |
| 99 break; |
| 100 } |
| 101 self.backgroundColor = newBackgroundColor; |
| 102 } |
| 103 |
67 @end | 104 @end |
OLD | NEW |