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

Side by Side Diff: ios/chrome/browser/ui/collection_view/cells/collection_view_switch_item.mm

Issue 2588713002: Upstream Chrome on iOS source code [4/11]. (Closed)
Patch Set: Created 4 years 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/collection_view/cells/collection_view_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 } // namespace
22
23 @implementation CollectionViewSwitchItem
24
25 @synthesize text = _text;
26 @synthesize on = _on;
27 @synthesize enabled = _enabled;
28
29 - (instancetype)initWithType:(NSInteger)type {
30 self = [super initWithType:type];
31 if (self) {
32 self.cellClass = [CollectionViewSwitchCell class];
33 self.enabled = YES;
34 self.accessibilityTraits |= UIAccessibilityTraitButton;
35 }
36 return self;
37 }
38
39 #pragma mark CollectionViewItem
40
41 - (void)configureCell:(CollectionViewSwitchCell*)cell {
42 [super configureCell:cell];
43 cell.textLabel.text = self.text;
44 cell.switchView.enabled = self.isEnabled;
45
46 // Force disabled cells to be drawn in the "off" state, but do not change the
47 // value of the |on| property.
48 cell.switchView.on = self.isOn && self.isEnabled;
49 cell.textLabel.textColor =
50 [CollectionViewSwitchCell defaultTextColorForState:cell.switchView.state];
51 }
52
53 @end
54
55 @implementation CollectionViewSwitchCell
56
57 @synthesize textLabel = _textLabel;
58 @synthesize switchView = _switchView;
59
60 - (instancetype)initWithFrame:(CGRect)frame {
61 self = [super initWithFrame:frame];
62 if (self) {
63 self.isAccessibilityElement = YES;
64
65 _textLabel = [[UILabel alloc] init];
66 _textLabel.translatesAutoresizingMaskIntoConstraints = NO;
67 [self.contentView addSubview:_textLabel];
68
69 _textLabel.font =
70 [[MDFRobotoFontLoader sharedInstance] mediumFontOfSize:14];
71 _textLabel.textColor = [[MDCPalette greyPalette] tint900];
72 _textLabel.numberOfLines = 0;
73
74 _switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
75 _switchView.translatesAutoresizingMaskIntoConstraints = NO;
76 _switchView.onTintColor = [[MDCPalette cr_bluePalette] tint500];
77 [self.contentView addSubview:_switchView];
78
79 // Set up the constraints.
80 [NSLayoutConstraint activateConstraints:@[
81 [_textLabel.leadingAnchor
82 constraintEqualToAnchor:self.contentView.leadingAnchor
83 constant:kHorizontalPadding],
84 [_switchView.trailingAnchor
85 constraintEqualToAnchor:self.contentView.trailingAnchor
86 constant:-kHorizontalPadding],
87 [_textLabel.trailingAnchor
88 constraintLessThanOrEqualToAnchor:_switchView.leadingAnchor
89 constant:-kHorizontalPadding],
90 [_textLabel.topAnchor constraintEqualToAnchor:self.contentView.topAnchor
91 constant:kVerticalPadding],
92 [_textLabel.bottomAnchor
93 constraintEqualToAnchor:self.contentView.bottomAnchor
94 constant:-kVerticalPadding],
95 [_switchView.centerYAnchor
96 constraintEqualToAnchor:self.contentView.centerYAnchor],
97 ]];
98 }
99 return self;
100 }
101
102 + (UIColor*)defaultTextColorForState:(UIControlState)state {
103 MDCPalette* grey = [MDCPalette greyPalette];
104 return (state & UIControlStateDisabled) ? grey.tint500 : grey.tint900;
105 }
106
107 // Implement -layoutSubviews as per instructions in documentation for
108 // +[MDCCollectionViewCell cr_preferredHeightForWidth:forItem:].
109 - (void)layoutSubviews {
110 [super layoutSubviews];
111 // Adjust the text label preferredMaxLayoutWidth when the parent's width
112 // changes, for instance on screen rotation.
113 _textLabel.preferredMaxLayoutWidth = CGRectGetWidth(self.contentView.frame) -
114 CGRectGetWidth(_switchView.frame) -
115 3 * kHorizontalPadding;
116
117 // Re-layout with the new preferred width to allow the label to adjust its
118 // height.
119 [super layoutSubviews];
120 }
121
122 - (void)prepareForReuse {
123 [super prepareForReuse];
124
125 [_switchView removeTarget:nil
126 action:nil
127 forControlEvents:[_switchView allControlEvents]];
128 }
129
130 #pragma mark - UIAccessibility
131
132 - (CGPoint)accessibilityActivationPoint {
133 // Center the activation point over the switch, so that double-tapping toggles
134 // the switch.
135 CGRect switchFrame =
136 UIAccessibilityConvertFrameToScreenCoordinates(_switchView.frame, self);
137 return CGPointMake(CGRectGetMidX(switchFrame), CGRectGetMidY(switchFrame));
138 }
139
140 - (NSString*)accessibilityHint {
141 return _switchView.accessibilityHint;
142 }
143
144 - (NSString*)accessibilityLabel {
145 return _textLabel.text;
146 }
147
148 - (NSString*)accessibilityValue {
149 return _switchView.accessibilityValue;
150 }
151
152 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698