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/autofill/cells/storage_switch_item.h" |
| 6 |
| 7 #include "components/grit/components_scaled_resources.h" |
| 8 #include "components/strings/grit/components_strings.h" |
| 9 #import "ios/chrome/browser/ui/colors/MDCPalette+CrAdditions.h" |
| 10 #import "ios/third_party/material_components_ios/src/components/Palettes/src/Mat
erialPalettes.h" |
| 11 #import "ios/third_party/material_roboto_font_loader_ios/src/src/MaterialRobotoF
ontLoader.h" |
| 12 #include "ui/base/l10n/l10n_util_mac.h" |
| 13 #include "ui/base/resource/resource_bundle.h" |
| 14 |
| 15 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 16 #error "This file requires ARC support." |
| 17 #endif |
| 18 |
| 19 namespace { |
| 20 // Padding used on the leading and trailing edges of the cell. |
| 21 const CGFloat kHorizontalPadding = 16; |
| 22 // Padding used on the top and bottom edges of the cell. |
| 23 const CGFloat kVerticalPadding = 16; |
| 24 // Space at the leading side of the tooltip button. |
| 25 const CGFloat kTooltipButtonLeadingSpacing = 8; |
| 26 // Space at the trailing side of the tooltip button. |
| 27 const CGFloat kTooltipButtonTrailingSpacing = 30; |
| 28 } |
| 29 |
| 30 @implementation StorageSwitchItem |
| 31 |
| 32 @synthesize on = _on; |
| 33 |
| 34 - (instancetype)initWithType:(NSInteger)type { |
| 35 self = [super initWithType:type]; |
| 36 if (self) { |
| 37 self.cellClass = [StorageSwitchCell class]; |
| 38 } |
| 39 return self; |
| 40 } |
| 41 |
| 42 #pragma mark CollectionViewItem |
| 43 |
| 44 - (void)configureCell:(StorageSwitchCell*)cell { |
| 45 [super configureCell:cell]; |
| 46 cell.switchView.on = self.on; |
| 47 } |
| 48 |
| 49 @end |
| 50 |
| 51 @implementation StorageSwitchCell |
| 52 |
| 53 @synthesize textLabel = _textLabel; |
| 54 @synthesize tooltipButton = _tooltipButton; |
| 55 @synthesize switchView = _switchView; |
| 56 |
| 57 - (instancetype)initWithFrame:(CGRect)frame { |
| 58 self = [super initWithFrame:frame]; |
| 59 if (self) { |
| 60 UIView* contentView = self.contentView; |
| 61 |
| 62 _textLabel = [[UILabel alloc] init]; |
| 63 _textLabel.translatesAutoresizingMaskIntoConstraints = NO; |
| 64 [contentView addSubview:_textLabel]; |
| 65 |
| 66 _switchView = [[UISwitch alloc] init]; |
| 67 _switchView.translatesAutoresizingMaskIntoConstraints = NO; |
| 68 [contentView addSubview:_switchView]; |
| 69 |
| 70 _tooltipButton = [UIButton buttonWithType:UIButtonTypeCustom]; |
| 71 _tooltipButton.translatesAutoresizingMaskIntoConstraints = NO; |
| 72 [contentView addSubview:_tooltipButton]; |
| 73 |
| 74 _textLabel.text = l10n_util::GetNSString( |
| 75 IDS_AUTOFILL_CARD_UNMASK_PROMPT_STORAGE_CHECKBOX); |
| 76 _textLabel.font = |
| 77 [[MDFRobotoFontLoader sharedInstance] mediumFontOfSize:14]; |
| 78 _textLabel.textColor = [[MDCPalette greyPalette] tint500]; |
| 79 _textLabel.numberOfLines = 0; |
| 80 [_textLabel |
| 81 setContentCompressionResistancePriority:UILayoutPriorityDefaultLow |
| 82 forAxis: |
| 83 UILayoutConstraintAxisHorizontal]; |
| 84 |
| 85 _switchView.onTintColor = [[MDCPalette cr_bluePalette] tint500]; |
| 86 |
| 87 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 88 UIImage* highlightedImage = |
| 89 rb.GetNativeImageNamed(IDR_AUTOFILL_TOOLTIP_ICON_H).ToUIImage(); |
| 90 [_tooltipButton setImage:highlightedImage forState:UIControlStateSelected]; |
| 91 UIImage* normalImage = |
| 92 rb.GetNativeImageNamed(IDR_AUTOFILL_TOOLTIP_ICON).ToUIImage(); |
| 93 [_tooltipButton setImage:normalImage forState:UIControlStateNormal]; |
| 94 |
| 95 // Set up the constraints. |
| 96 [NSLayoutConstraint activateConstraints:@[ |
| 97 [_textLabel.topAnchor constraintEqualToAnchor:contentView.topAnchor |
| 98 constant:kVerticalPadding], |
| 99 [_textLabel.bottomAnchor constraintEqualToAnchor:contentView.bottomAnchor |
| 100 constant:-kVerticalPadding], |
| 101 [_textLabel.leadingAnchor |
| 102 constraintEqualToAnchor:contentView.leadingAnchor |
| 103 constant:kHorizontalPadding], |
| 104 [_textLabel.trailingAnchor |
| 105 constraintLessThanOrEqualToAnchor:_tooltipButton.leadingAnchor |
| 106 constant:-kTooltipButtonLeadingSpacing], |
| 107 [_tooltipButton.centerYAnchor |
| 108 constraintEqualToAnchor:contentView.centerYAnchor], |
| 109 [_tooltipButton.trailingAnchor |
| 110 constraintEqualToAnchor:_switchView.leadingAnchor |
| 111 constant:-kTooltipButtonTrailingSpacing], |
| 112 [_switchView.centerYAnchor |
| 113 constraintEqualToAnchor:contentView.centerYAnchor], |
| 114 [_switchView.trailingAnchor |
| 115 constraintEqualToAnchor:contentView.trailingAnchor |
| 116 constant:-kHorizontalPadding], |
| 117 ]]; |
| 118 } |
| 119 return self; |
| 120 } |
| 121 |
| 122 // Implement -layoutSubviews as per instructions in documentation for |
| 123 // +[MDCCollectionViewCell cr_preferredHeightForWidth:forItem:]. |
| 124 - (void)layoutSubviews { |
| 125 [super layoutSubviews]; |
| 126 |
| 127 // Adjust the text label preferredMaxLayoutWidth when the parent's width |
| 128 // changes, for instance on screen rotation. |
| 129 self.textLabel.preferredMaxLayoutWidth = |
| 130 CGRectGetWidth(self.contentView.frame) - 2 * kHorizontalPadding - |
| 131 kTooltipButtonLeadingSpacing - CGRectGetWidth(self.tooltipButton.frame) - |
| 132 kTooltipButtonTrailingSpacing - CGRectGetWidth(self.switchView.frame); |
| 133 |
| 134 // Re-layout with the new preferred width to allow the label to adjust its |
| 135 // height. |
| 136 [super layoutSubviews]; |
| 137 } |
| 138 |
| 139 - (void)prepareForReuse { |
| 140 [super prepareForReuse]; |
| 141 [self.tooltipButton removeTarget:nil |
| 142 action:nil |
| 143 forControlEvents:self.tooltipButton.allControlEvents]; |
| 144 [self.switchView removeTarget:nil |
| 145 action:nil |
| 146 forControlEvents:self.switchView.allControlEvents]; |
| 147 } |
| 148 |
| 149 @end |
OLD | NEW |