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_roboto_font_loader_ios/src/src/MaterialRobotoF
ontLoader.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 |
| 77 _iconImageView = [[UIImageView alloc] init]; |
| 78 _iconImageView.translatesAutoresizingMaskIntoConstraints = NO; |
| 79 _iconImageView.image = PlaceholderIcon(); |
| 80 _iconImageView.layer.cornerRadius = kIconCornerRadius; |
| 81 _iconImageView.layer.masksToBounds = YES; |
| 82 self.layer.shouldRasterize = YES; |
| 83 self.layer.rasterizationScale = [[UIScreen mainScreen] scale]; |
| 84 [contentView addSubview:_iconImageView]; |
| 85 |
| 86 _nameLabel = [[UILabel alloc] init]; |
| 87 _nameLabel.translatesAutoresizingMaskIntoConstraints = NO; |
| 88 [contentView addSubview:_nameLabel]; |
| 89 |
| 90 _nameLabel.font = |
| 91 [[MDFRobotoFontLoader sharedInstance] 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 - (UIAccessibilityTraits)accessibilityTraits { |
| 175 // Remove the "button" accessibility trait from the switch. This matches |
| 176 // native switch content views such as Settings > Airplane Mode. |
| 177 return |
| 178 [super accessibilityTraits] | |
| 179 ([self.switchControl accessibilityTraits] & ~UIAccessibilityTraitButton); |
| 180 } |
| 181 |
| 182 - (NSString*)accessibilityValue { |
| 183 if (self.accessoryView == self.switchControl) { |
| 184 return (self.switchControl.on) |
| 185 ? l10n_util::GetNSString(IDS_IOS_SETTING_ON) |
| 186 : l10n_util::GetNSString(IDS_IOS_SETTING_OFF); |
| 187 } |
| 188 return [self.accessoryView accessibilityValue]; |
| 189 } |
| 190 |
| 191 @end |
OLD | NEW |