Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(179)

Side by Side Diff: ios/chrome/browser/ui/settings/cells/sync_switch_item.mm

Issue 2589583003: Upstream Chrome on iOS source code [7/11]. (Closed)
Patch Set: Created 3 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/sync_switch_item.h"
6
7 #import "ios/chrome/browser/ui/colors/MDCPalette+CrAdditions.h"
8 #import "ios/third_party/material_components_ios/src/components/Palettes/src/Mat erialPalettes.h"
9 #import "ios/third_party/material_roboto_font_loader_ios/src/src/MaterialRobotoF ontLoader.h"
10
11 #if !defined(__has_feature) || !__has_feature(objc_arc)
12 #error "This file requires ARC support."
13 #endif
14
15 namespace {
16 // Padding used on the leading and trailing edges of the cell.
17 const CGFloat kHorizontalPadding = 16;
18
19 // Padding used on the top and bottom edges of the cell.
20 const CGFloat kVerticalPadding = 16;
21
22 // Padding used between the switch and text.
23 const CGFloat kHorizontalSwitchPadding = 10;
24 } // namespace
25
26 #pragma mark - SyncSwitchItem
27
28 @implementation SyncSwitchItem
29
30 @synthesize text = _text;
31 @synthesize detailText = _detailText;
32 @synthesize on = _on;
33 @synthesize enabled = _enabled;
34 @synthesize dataType = _dataType;
35
36 - (instancetype)initWithType:(NSInteger)type {
37 self = [super initWithType:type];
38 if (self) {
39 self.cellClass = [SyncSwitchCell class];
40 self.enabled = YES;
41 self.accessibilityTraits |= UIAccessibilityTraitButton;
42 }
43 return self;
44 }
45
46 - (void)configureCell:(SyncSwitchCell*)cell {
47 [super configureCell:cell];
48 cell.textLabel.text = self.text;
49 cell.detailTextLabel.text = self.detailText;
50 cell.switchView.enabled = self.isEnabled;
51 cell.switchView.on = self.isOn;
52 cell.textLabel.textColor =
53 [SyncSwitchCell defaultTextColorForState:cell.switchView.state];
54 if (self.isEnabled) {
55 cell.textLabel.textColor = [[MDCPalette greyPalette] tint900];
56 cell.switchView.enabled = YES;
57 cell.userInteractionEnabled = YES;
58 } else {
59 cell.textLabel.textColor = [[MDCPalette greyPalette] tint500];
60 cell.switchView.enabled = NO;
61 cell.userInteractionEnabled = NO;
62 }
63 }
64
65 @end
66
67 @implementation SyncSwitchCell
68
69 @synthesize textLabel = _textLabel;
70 @synthesize detailTextLabel = _detailTextLabel;
71 @synthesize switchView = _switchView;
72
73 - (instancetype)initWithFrame:(CGRect)frame {
74 self = [super initWithFrame:frame];
75 if (self) {
76 self.isAccessibilityElement = YES;
77
78 _textLabel = [[UILabel alloc] init];
79 _textLabel.translatesAutoresizingMaskIntoConstraints = NO;
80 [self.contentView addSubview:_textLabel];
81
82 _textLabel.font =
83 [[MDFRobotoFontLoader sharedInstance] mediumFontOfSize:14];
84 _textLabel.textColor = [[MDCPalette greyPalette] tint900];
85 _textLabel.numberOfLines = 0;
86
87 _detailTextLabel = [[UILabel alloc] init];
88 _detailTextLabel.translatesAutoresizingMaskIntoConstraints = NO;
89 [self.contentView addSubview:_detailTextLabel];
90
91 _detailTextLabel.font =
92 [[MDFRobotoFontLoader sharedInstance] mediumFontOfSize:14];
93 _detailTextLabel.textColor = [[MDCPalette greyPalette] tint500];
94 _detailTextLabel.numberOfLines = 0;
95
96 _switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
97 _switchView.translatesAutoresizingMaskIntoConstraints = NO;
98 _switchView.onTintColor = [[MDCPalette cr_bluePalette] tint500];
99 [self.contentView addSubview:_switchView];
100
101 [self setConstraints];
102 }
103
104 return self;
105 }
106
107 - (void)setConstraints {
108 UIView* contentView = self.contentView;
109
110 [NSLayoutConstraint activateConstraints:@[
111 [_textLabel.leadingAnchor constraintEqualToAnchor:contentView.leadingAnchor
112 constant:kHorizontalPadding],
113 [_detailTextLabel.leadingAnchor
114 constraintEqualToAnchor:_textLabel.leadingAnchor],
115 [_switchView.trailingAnchor
116 constraintEqualToAnchor:contentView.trailingAnchor
117 constant:-kHorizontalPadding],
118 [_textLabel.trailingAnchor
119 constraintLessThanOrEqualToAnchor:_switchView.leadingAnchor
120 constant:-kHorizontalSwitchPadding],
121 [_detailTextLabel.trailingAnchor
122 constraintEqualToAnchor:_textLabel.trailingAnchor],
123 [_textLabel.topAnchor constraintEqualToAnchor:contentView.topAnchor
124 constant:kVerticalPadding],
125 [_textLabel.bottomAnchor
126 constraintEqualToAnchor:_detailTextLabel.topAnchor],
127 [_detailTextLabel.bottomAnchor
128 constraintEqualToAnchor:contentView.bottomAnchor
129 constant:-kVerticalPadding],
130 [_switchView.centerYAnchor
131 constraintEqualToAnchor:contentView.centerYAnchor],
132 ]];
133 }
134
135 + (UIColor*)defaultTextColorForState:(UIControlState)state {
136 MDCPalette* grey = [MDCPalette greyPalette];
137 return (state & UIControlStateDisabled) ? grey.tint500 : grey.tint900;
138 }
139
140 // Implement -layoutSubviews as per instructions in documentation for
141 // +[MDCCollectionViewCell cr_preferredHeightForWidth:forItem:].
142 - (void)layoutSubviews {
143 [super layoutSubviews];
144 // Adjust the text and detailText label preferredMaxLayoutWidth when the
145 // parent's width
146 // changes, for instance on screen rotation.
147 CGFloat prefferedMaxLayoutWidth = CGRectGetWidth(self.contentView.frame) -
148 CGRectGetWidth(_switchView.frame) -
149 2 * kHorizontalPadding -
150 kHorizontalSwitchPadding;
151 _textLabel.preferredMaxLayoutWidth = prefferedMaxLayoutWidth;
152 _detailTextLabel.preferredMaxLayoutWidth = prefferedMaxLayoutWidth;
153
154 // Re-layout with the new preferred width to allow the label to adjust its
155 // height.
156 [super layoutSubviews];
157 }
158
159 - (void)prepareForReuse {
160 [super prepareForReuse];
161 self.tag = 0;
162 self.textLabel.textColor = [[MDCPalette greyPalette] tint900];
163 [self.switchView setEnabled:YES];
164 [self setUserInteractionEnabled:YES];
165
166 [_switchView removeTarget:nil
167 action:nil
168 forControlEvents:[_switchView allControlEvents]];
169 }
170
171 #pragma mark - UIAccessibility
172
173 - (CGPoint)accessibilityActivationPoint {
174 // Center the activation point over the switch, so that double-tapping toggles
175 // the switch.
176 CGRect switchFrame =
177 UIAccessibilityConvertFrameToScreenCoordinates(_switchView.frame, self);
178 return CGPointMake(CGRectGetMidX(switchFrame), CGRectGetMidY(switchFrame));
179 }
180
181 - (NSString*)accessibilityHint {
182 return _switchView.accessibilityHint;
183 }
184
185 - (NSString*)accessibilityLabel {
186 return _textLabel.text;
187 }
188
189 - (NSString*)accessibilityValue {
190 return _switchView.accessibilityValue;
191 }
192
193 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698