OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #import "ios/chrome/browser/ui/settings/cells/native_app_item.h" | |
6 | |
7 #import <QuartzCore/QuartzCore.h> | |
8 | |
9 #import "ios/chrome/browser/ui/colors/MDCPalette+CrAdditions.h" | |
10 #include "ios/chrome/grit/ios_strings.h" | |
11 #import "ios/third_party/material_components_ios/src/components/Buttons/src/Mate
rialButtons.h" | |
12 #import "ios/third_party/material_components_ios/src/components/Palettes/src/Mat
erialPalettes.h" | |
13 #import "ios/third_party/material_components_ios/src/components/Typography/src/M
aterialTypography.h" | |
14 #include "ui/base/l10n/l10n_util_mac.h" | |
15 | |
16 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
17 #error "This file requires ARC support." | |
18 #endif | |
19 | |
20 namespace { | |
21 | |
22 // Padding used on the leading and trailing edges of the cell. | |
23 const CGFloat kHorizontalPadding = 16; | |
24 | |
25 // Icon rounded corner radius. | |
26 const CGFloat kIconCornerRadius = 5.088; | |
27 | |
28 // Icon size. | |
29 const CGFloat kIconSize = 29; | |
30 | |
31 // Placeholder icon for native app cells. | |
32 UIImage* PlaceholderIcon() { | |
33 return [UIImage imageNamed:@"app_icon_placeholder"]; | |
34 } | |
35 | |
36 } // namespace | |
37 | |
38 @implementation NativeAppItem | |
39 | |
40 @synthesize name = _name; | |
41 @synthesize icon = _icon; | |
42 @synthesize state = _state; | |
43 | |
44 - (instancetype)initWithType:(NSInteger)type { | |
45 self = [super initWithType:type]; | |
46 if (self) { | |
47 self.cellClass = [NativeAppCell class]; | |
48 } | |
49 return self; | |
50 } | |
51 | |
52 #pragma mark CollectionViewItem | |
53 | |
54 - (void)configureCell:(NativeAppCell*)cell { | |
55 [super configureCell:cell]; | |
56 cell.nameLabel.text = self.name; | |
57 if (self.icon) { | |
58 cell.iconImageView.image = self.icon; | |
59 } | |
60 [cell updateWithState:self.state]; | |
61 } | |
62 | |
63 @end | |
64 | |
65 @implementation NativeAppCell | |
66 | |
67 @synthesize nameLabel = _nameLabel; | |
68 @synthesize iconImageView = _iconImageView; | |
69 @synthesize switchControl = _switchControl; | |
70 @synthesize installButton = _installButton; | |
71 | |
72 - (instancetype)initWithFrame:(CGRect)frame { | |
73 self = [super initWithFrame:frame]; | |
74 if (self) { | |
75 UIView* contentView = self.contentView; | |
76 self.isAccessibilityElement = YES; | |
77 | |
78 _iconImageView = [[UIImageView alloc] init]; | |
79 _iconImageView.translatesAutoresizingMaskIntoConstraints = NO; | |
80 _iconImageView.image = PlaceholderIcon(); | |
81 _iconImageView.layer.cornerRadius = kIconCornerRadius; | |
82 _iconImageView.layer.masksToBounds = YES; | |
83 self.layer.shouldRasterize = YES; | |
84 self.layer.rasterizationScale = [[UIScreen mainScreen] scale]; | |
85 [contentView addSubview:_iconImageView]; | |
86 | |
87 _nameLabel = [[UILabel alloc] init]; | |
88 _nameLabel.translatesAutoresizingMaskIntoConstraints = NO; | |
89 [contentView addSubview:_nameLabel]; | |
90 | |
91 _nameLabel.font = [[MDCTypography fontLoader] mediumFontOfSize:14]; | |
92 _nameLabel.textColor = [[MDCPalette greyPalette] tint900]; | |
93 | |
94 _switchControl = [[UISwitch alloc] init]; | |
95 _switchControl.onTintColor = [[MDCPalette cr_bluePalette] tint500]; | |
96 | |
97 _installButton = [[MDCFlatButton alloc] init]; | |
98 _installButton.translatesAutoresizingMaskIntoConstraints = NO; | |
99 _installButton.customTitleColor = [[MDCPalette cr_bluePalette] tint500]; | |
100 [_installButton | |
101 setTitle:l10n_util::GetNSString(IDS_IOS_GOOGLE_APPS_INSTALL_BUTTON) | |
102 forState:UIControlStateNormal]; | |
103 [_installButton setTitle:@"" forState:UIControlStateDisabled]; | |
104 _installButton.accessibilityHint = l10n_util::GetNSString( | |
105 IDS_IOS_GOOGLE_APPS_INSTALL_BUTTON_ACCESSIBILITY_HINT); | |
106 | |
107 // Set up the constraints. | |
108 [NSLayoutConstraint activateConstraints:@[ | |
109 [_iconImageView.leadingAnchor | |
110 constraintEqualToAnchor:contentView.leadingAnchor | |
111 constant:kHorizontalPadding], | |
112 [_iconImageView.trailingAnchor | |
113 constraintEqualToAnchor:_nameLabel.leadingAnchor | |
114 constant:-kHorizontalPadding], | |
115 [_iconImageView.widthAnchor constraintEqualToConstant:kIconSize], | |
116 [_iconImageView.heightAnchor constraintEqualToConstant:kIconSize], | |
117 [_nameLabel.trailingAnchor | |
118 constraintEqualToAnchor:contentView.trailingAnchor | |
119 constant:-kHorizontalPadding], | |
120 [_iconImageView.centerYAnchor | |
121 constraintEqualToAnchor:contentView.centerYAnchor], | |
122 [_nameLabel.centerYAnchor | |
123 constraintEqualToAnchor:contentView.centerYAnchor], | |
124 [_installButton.widthAnchor constraintGreaterThanOrEqualToConstant:79], | |
125 [_installButton.widthAnchor constraintLessThanOrEqualToConstant:127], | |
126 ]]; | |
127 } | |
128 return self; | |
129 } | |
130 | |
131 - (void)updateWithState:(NativeAppItemState)state { | |
132 switch (state) { | |
133 case NativeAppItemSwitchOn: | |
134 self.switchControl.on = YES; | |
135 self.accessoryView = self.switchControl; | |
136 break; | |
137 case NativeAppItemSwitchOff: | |
138 self.switchControl.on = NO; | |
139 self.accessoryView = self.switchControl; | |
140 break; | |
141 case NativeAppItemInstall: | |
142 self.accessoryView = self.installButton; | |
143 break; | |
144 } | |
145 } | |
146 | |
147 - (void)prepareForReuse { | |
148 [super prepareForReuse]; | |
149 [self.switchControl removeTarget:nil | |
150 action:nil | |
151 forControlEvents:[self.switchControl allControlEvents]]; | |
152 self.switchControl.tag = 0; | |
153 [self.installButton removeTarget:nil | |
154 action:nil | |
155 forControlEvents:[self.installButton allControlEvents]]; | |
156 self.iconImageView.image = PlaceholderIcon(); | |
157 self.iconImageView.tag = 0; | |
158 } | |
159 | |
160 #pragma mark - UIAccessibility | |
161 | |
162 - (CGPoint)accessibilityActivationPoint { | |
163 // Activate over the accessory view. | |
164 CGRect accessoryViewFrame = UIAccessibilityConvertFrameToScreenCoordinates( | |
165 [self.accessoryView frame], self); | |
166 return CGPointMake(CGRectGetMidX(accessoryViewFrame), | |
167 CGRectGetMidY(accessoryViewFrame)); | |
168 } | |
169 | |
170 - (NSString*)accessibilityHint { | |
171 return [self.accessoryView accessibilityHint]; | |
172 } | |
173 | |
174 - (NSString*)accessibilityValue { | |
175 if (self.accessoryView == self.switchControl) { | |
176 if (self.switchControl.on) { | |
177 return [NSString | |
178 stringWithFormat:@"%@, %@", self.nameLabel.text, | |
179 l10n_util::GetNSString(IDS_IOS_SETTING_ON)]; | |
180 } else { | |
181 return [NSString | |
182 stringWithFormat:@"%@, %@", self.nameLabel.text, | |
183 l10n_util::GetNSString(IDS_IOS_SETTING_OFF)]; | |
184 } | |
185 } | |
186 | |
187 return [NSString stringWithFormat:@"%@, %@", self.nameLabel.text, | |
188 l10n_util::GetNSString( | |
189 IDS_IOS_GOOGLE_APPS_INSTALL_BUTTON)]; | |
190 } | |
191 | |
192 @end | |
OLD | NEW |