OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import "ios/chrome/browser/ui/tab_switcher/tab_switcher_panel_cell.h" | 5 #import "ios/chrome/browser/ui/tab_switcher/tab_switcher_panel_cell.h" |
6 | 6 |
7 #include "base/mac/scoped_nsobject.h" | |
8 #import "ios/chrome/browser/tabs/tab.h" | 7 #import "ios/chrome/browser/tabs/tab.h" |
9 #import "ios/chrome/browser/ui/fade_truncated_label.h" | 8 #import "ios/chrome/browser/ui/fade_truncated_label.h" |
10 #import "ios/chrome/browser/ui/image_util.h" | 9 #import "ios/chrome/browser/ui/image_util.h" |
11 #import "ios/chrome/browser/ui/tab_switcher/tab_switcher_button.h" | 10 #import "ios/chrome/browser/ui/tab_switcher/tab_switcher_button.h" |
12 #import "ios/chrome/browser/ui/tab_switcher/tab_switcher_cache.h" | 11 #import "ios/chrome/browser/ui/tab_switcher/tab_switcher_cache.h" |
13 #import "ios/chrome/browser/ui/tab_switcher/tab_switcher_utils.h" | 12 #import "ios/chrome/browser/ui/tab_switcher/tab_switcher_utils.h" |
14 #import "ios/chrome/browser/ui/uikit_ui_util.h" | 13 #import "ios/chrome/browser/ui/uikit_ui_util.h" |
15 #include "ios/chrome/grit/ios_strings.h" | 14 #include "ios/chrome/grit/ios_strings.h" |
16 #include "ios/chrome/grit/ios_theme_resources.h" | 15 #include "ios/chrome/grit/ios_theme_resources.h" |
17 #import "ios/third_party/material_components_ios/src/components/Palettes/src/Mat
erialPalettes.h" | 16 #import "ios/third_party/material_components_ios/src/components/Palettes/src/Mat
erialPalettes.h" |
18 #import "ios/third_party/material_roboto_font_loader_ios/src/src/MaterialRobotoF
ontLoader.h" | 17 #import "ios/third_party/material_roboto_font_loader_ios/src/src/MaterialRobotoF
ontLoader.h" |
19 #import "ios/third_party/material_text_accessibility_ios/src/src/MDFTextAccessib
ility.h" | 18 #import "ios/third_party/material_text_accessibility_ios/src/src/MDFTextAccessib
ility.h" |
20 #include "ui/base/l10n/l10n_util.h" | 19 #include "ui/base/l10n/l10n_util.h" |
21 #include "ui/gfx/image/image.h" | 20 #include "ui/gfx/image/image.h" |
22 #include "url/gurl.h" | 21 #include "url/gurl.h" |
23 | 22 |
| 23 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 24 #error "This file requires ARC support." |
| 25 #endif |
| 26 |
24 namespace gfx { | 27 namespace gfx { |
25 class ImageSkia; | 28 class ImageSkia; |
26 } | 29 } |
27 | 30 |
28 namespace { | 31 namespace { |
29 const CGFloat kFontSize = 16; | 32 const CGFloat kFontSize = 16; |
30 const CGFloat kCellCornerRadius = 2; | 33 const CGFloat kCellCornerRadius = 2; |
31 const CGFloat kBarHeight = 44; | 34 const CGFloat kBarHeight = 44; |
32 const CGFloat kTitleLabelTextAlpha = .54; | 35 const CGFloat kTitleLabelTextAlpha = .54; |
33 const CGFloat kNewTabIconAlpha = .87; | 36 const CGFloat kNewTabIconAlpha = .87; |
34 } | 37 } |
35 | 38 |
36 CGFloat tabSwitcherLocalSessionCellTopBarHeight() { | 39 CGFloat tabSwitcherLocalSessionCellTopBarHeight() { |
37 return kBarHeight; | 40 return kBarHeight; |
38 } | 41 } |
39 | 42 |
40 @interface TabSwitcherSessionCell () | 43 @interface TabSwitcherSessionCell () |
41 | 44 |
42 // Returns the container view with rounded corners to which all cell subviews | 45 // Returns the container view with rounded corners to which all cell subviews |
43 // should be added. | 46 // should be added. |
44 - (UIView*)containerView; | 47 - (UIView*)containerView; |
45 | 48 |
46 @end | 49 @end |
47 | 50 |
48 @implementation TabSwitcherSessionCell { | 51 @implementation TabSwitcherSessionCell { |
49 base::scoped_nsobject<UIView> _containerView; | 52 UIView* _containerView; |
50 CGSize _cachedShadowSize; | 53 CGSize _cachedShadowSize; |
51 } | 54 } |
52 | 55 |
53 @synthesize delegate = _delegate; | 56 @synthesize delegate = _delegate; |
54 | 57 |
55 // Returns the cell's identifier used for the cell's re-use. | 58 // Returns the cell's identifier used for the cell's re-use. |
56 + (NSString*)identifier { | 59 + (NSString*)identifier { |
57 return NSStringFromClass([self class]); | 60 return NSStringFromClass([self class]); |
58 } | 61 } |
59 | 62 |
60 - (instancetype)initWithFrame:(CGRect)frame { | 63 - (instancetype)initWithFrame:(CGRect)frame { |
61 self = [super initWithFrame:frame]; | 64 self = [super initWithFrame:frame]; |
62 if (self) { | 65 if (self) { |
63 [self contentView].isAccessibilityElement = YES; | 66 [self contentView].isAccessibilityElement = YES; |
64 _containerView.reset([[UIView alloc] initWithFrame:self.bounds]); | 67 _containerView = [[UIView alloc] initWithFrame:self.bounds]; |
65 [_containerView setAutoresizingMask:UIViewAutoresizingFlexibleHeight | | 68 [_containerView setAutoresizingMask:UIViewAutoresizingFlexibleHeight | |
66 UIViewAutoresizingFlexibleWidth]; | 69 UIViewAutoresizingFlexibleWidth]; |
67 [[_containerView layer] setCornerRadius:kCellCornerRadius]; | 70 [[_containerView layer] setCornerRadius:kCellCornerRadius]; |
68 [[_containerView layer] setMasksToBounds:YES]; | 71 [[_containerView layer] setMasksToBounds:YES]; |
69 [[self contentView] addSubview:_containerView]; | 72 [[self contentView] addSubview:_containerView]; |
70 [self updateShadow]; | 73 [self updateShadow]; |
71 [[[self contentView] layer] setShouldRasterize:YES]; | 74 [[[self contentView] layer] setShouldRasterize:YES]; |
72 [[[self contentView] layer] | 75 [[[self contentView] layer] |
73 setRasterizationScale:[[UIScreen mainScreen] scale]]; | 76 setRasterizationScale:[[UIScreen mainScreen] scale]]; |
74 } | 77 } |
(...skipping 12 matching lines...) Expand all Loading... |
87 [UIBezierPath bezierPathWithRoundedRect:offsetedRectangle | 90 [UIBezierPath bezierPathWithRoundedRect:offsetedRectangle |
88 cornerRadius:kCellCornerRadius]; | 91 cornerRadius:kCellCornerRadius]; |
89 [[self contentView].layer setShadowPath:shadowPath.CGPath]; | 92 [[self contentView].layer setShadowPath:shadowPath.CGPath]; |
90 [[self contentView].layer setShadowColor:[UIColor blackColor].CGColor]; | 93 [[self contentView].layer setShadowColor:[UIColor blackColor].CGColor]; |
91 [[self contentView].layer setShadowOpacity:0.5]; | 94 [[self contentView].layer setShadowOpacity:0.5]; |
92 _cachedShadowSize = self.bounds.size; | 95 _cachedShadowSize = self.bounds.size; |
93 } | 96 } |
94 } | 97 } |
95 | 98 |
96 - (UIView*)containerView { | 99 - (UIView*)containerView { |
97 return _containerView.get(); | 100 return _containerView; |
98 } | 101 } |
99 | 102 |
100 @end | 103 @end |
101 | 104 |
102 @implementation TabSwitcherLocalSessionCell { | 105 @implementation TabSwitcherLocalSessionCell { |
103 base::scoped_nsobject<UIView> _topBar; | 106 UIView* _topBar; |
104 base::scoped_nsobject<UILabel> _titleLabel; | 107 UILabel* _titleLabel; |
105 base::scoped_nsobject<UIImageView> _favicon; | 108 UIImageView* _favicon; |
106 base::scoped_nsobject<UIButton> _closeButton; | 109 UIButton* _closeButton; |
107 base::scoped_nsobject<UIImageView> _shadow; | 110 UIImageView* _shadow; |
108 base::scoped_nsobject<UIImageView> _snapshot; | 111 UIImageView* _snapshot; |
109 base::scoped_nsobject<TabSwitcherButton> _snapshotButton; | 112 TabSwitcherButton* _snapshotButton; |
110 PendingSnapshotRequest _currentPendingSnapshotRequest; | 113 PendingSnapshotRequest _currentPendingSnapshotRequest; |
111 } | 114 } |
112 | 115 |
113 - (instancetype)initWithFrame:(CGRect)frame { | 116 - (instancetype)initWithFrame:(CGRect)frame { |
114 self = [super initWithFrame:frame]; | 117 self = [super initWithFrame:frame]; |
115 if (self) { | 118 if (self) { |
116 // Top bar. | 119 // Top bar. |
117 _topBar.reset([[UIView alloc] initWithFrame:CGRectZero]); | 120 _topBar = [[UIView alloc] initWithFrame:CGRectZero]; |
118 [_topBar setTranslatesAutoresizingMaskIntoConstraints:NO]; | 121 [_topBar setTranslatesAutoresizingMaskIntoConstraints:NO]; |
119 [[self containerView] addSubview:_topBar]; | 122 [[self containerView] addSubview:_topBar]; |
120 | 123 |
121 // Snapshot view. | 124 // Snapshot view. |
122 _snapshot.reset([[UIImageView alloc] initWithFrame:CGRectZero]); | 125 _snapshot = [[UIImageView alloc] initWithFrame:CGRectZero]; |
123 [_snapshot setTranslatesAutoresizingMaskIntoConstraints:NO]; | 126 [_snapshot setTranslatesAutoresizingMaskIntoConstraints:NO]; |
124 [_snapshot setContentMode:UIViewContentModeScaleAspectFill]; | 127 [_snapshot setContentMode:UIViewContentModeScaleAspectFill]; |
125 [_snapshot setClipsToBounds:YES]; | 128 [_snapshot setClipsToBounds:YES]; |
126 [[self containerView] addSubview:_snapshot]; | 129 [[self containerView] addSubview:_snapshot]; |
127 | 130 |
128 // Cell button. | 131 // Cell button. |
129 _snapshotButton.reset([[TabSwitcherButton alloc] initWithFrame:CGRectZero]); | 132 _snapshotButton = [[TabSwitcherButton alloc] initWithFrame:CGRectZero]; |
130 [_snapshotButton setTranslatesAutoresizingMaskIntoConstraints:NO]; | 133 [_snapshotButton setTranslatesAutoresizingMaskIntoConstraints:NO]; |
131 [_snapshotButton addTarget:self | 134 [_snapshotButton addTarget:self |
132 action:@selector(snapshotPressed) | 135 action:@selector(snapshotPressed) |
133 forControlEvents:UIControlEventTouchUpInside]; | 136 forControlEvents:UIControlEventTouchUpInside]; |
134 [[self containerView] addSubview:_snapshotButton]; | 137 [[self containerView] addSubview:_snapshotButton]; |
135 | 138 |
136 // Shadow view. | 139 // Shadow view. |
137 _shadow.reset([[UIImageView alloc] initWithFrame:CGRectZero]); | 140 _shadow = [[UIImageView alloc] initWithFrame:CGRectZero]; |
138 [_shadow setTranslatesAutoresizingMaskIntoConstraints:NO]; | 141 [_shadow setTranslatesAutoresizingMaskIntoConstraints:NO]; |
139 [_shadow setImage:NativeImage(IDR_IOS_TOOLBAR_SHADOW)]; | 142 [_shadow setImage:NativeImage(IDR_IOS_TOOLBAR_SHADOW)]; |
140 [[self containerView] addSubview:_shadow]; | 143 [[self containerView] addSubview:_shadow]; |
141 | 144 |
142 // Constraints on the Top bar, snapshot view, and shadow view. | 145 // Constraints on the Top bar, snapshot view, and shadow view. |
143 NSDictionary* viewsDictionary = @{ | 146 NSDictionary* viewsDictionary = @{ |
144 @"bar" : _topBar.get(), | 147 @"bar" : _topBar, |
145 @"shadow" : _shadow.get(), | 148 @"shadow" : _shadow, |
146 @"snapshot" : _snapshot.get(), | 149 @"snapshot" : _snapshot, |
147 @"snapshotButton" : _snapshotButton.get(), | 150 @"snapshotButton" : _snapshotButton, |
148 }; | 151 }; |
149 NSArray* constraints = @[ | 152 NSArray* constraints = @[ |
150 @"H:|-0-[bar]-0-|", | 153 @"H:|-0-[bar]-0-|", |
151 @"H:|-0-[shadow]-0-|", | 154 @"H:|-0-[shadow]-0-|", |
152 @"H:|-0-[snapshot]-0-|", | 155 @"H:|-0-[snapshot]-0-|", |
153 @"H:|-0-[snapshotButton]-0-|", | 156 @"H:|-0-[snapshotButton]-0-|", |
154 @"V:|-0-[bar(==barHeight)]-0-[snapshot]-0-|", | 157 @"V:|-0-[bar(==barHeight)]-0-[snapshot]-0-|", |
155 @"V:[bar]-0-[snapshotButton]-0-|", | 158 @"V:[bar]-0-[snapshotButton]-0-|", |
156 @"V:[bar]-0-[shadow]", | 159 @"V:[bar]-0-[shadow]", |
157 ]; | 160 ]; |
158 NSDictionary* metrics = | 161 NSDictionary* metrics = |
159 @{ @"barHeight" : @(tabSwitcherLocalSessionCellTopBarHeight()) }; | 162 @{ @"barHeight" : @(tabSwitcherLocalSessionCellTopBarHeight()) }; |
160 ApplyVisualConstraintsWithMetrics(constraints, viewsDictionary, metrics, | 163 ApplyVisualConstraintsWithMetrics(constraints, viewsDictionary, metrics, |
161 [self containerView]); | 164 [self containerView]); |
162 | 165 |
163 // Create and add subviews to the cell bar. | 166 // Create and add subviews to the cell bar. |
164 // Title label. | 167 // Title label. |
165 _titleLabel.reset([[UILabel alloc] initWithFrame:CGRectZero]); | 168 _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; |
166 [_titleLabel setTranslatesAutoresizingMaskIntoConstraints:NO]; | 169 [_titleLabel setTranslatesAutoresizingMaskIntoConstraints:NO]; |
167 [_titleLabel setFont:[[MDFRobotoFontLoader sharedInstance] | 170 [_titleLabel setFont:[[MDFRobotoFontLoader sharedInstance] |
168 regularFontOfSize:kFontSize]]; | 171 regularFontOfSize:kFontSize]]; |
169 [_topBar addSubview:_titleLabel]; | 172 [_topBar addSubview:_titleLabel]; |
170 | 173 |
171 // Favicon. | 174 // Favicon. |
172 _favicon.reset([[UIImageView alloc] initWithFrame:CGRectZero]); | 175 _favicon = [[UIImageView alloc] initWithFrame:CGRectZero]; |
173 [_favicon setTranslatesAutoresizingMaskIntoConstraints:NO]; | 176 [_favicon setTranslatesAutoresizingMaskIntoConstraints:NO]; |
174 [_topBar addSubview:_favicon]; | 177 [_topBar addSubview:_favicon]; |
175 | 178 |
176 // Close button. | 179 // Close button. |
177 _closeButton.reset([[UIButton alloc] initWithFrame:CGRectZero]); | 180 _closeButton = [[UIButton alloc] initWithFrame:CGRectZero]; |
178 [_closeButton | 181 [_closeButton |
179 setImage:[[UIImage imageNamed:@"card_close_button"] | 182 setImage:[[UIImage imageNamed:@"card_close_button"] |
180 imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] | 183 imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] |
181 forState:UIControlStateNormal]; | 184 forState:UIControlStateNormal]; |
182 [_closeButton setTranslatesAutoresizingMaskIntoConstraints:NO]; | 185 [_closeButton setTranslatesAutoresizingMaskIntoConstraints:NO]; |
183 [_closeButton addTarget:self | 186 [_closeButton addTarget:self |
184 action:@selector(closeButtonPressed) | 187 action:@selector(closeButtonPressed) |
185 forControlEvents:UIControlEventTouchUpInside]; | 188 forControlEvents:UIControlEventTouchUpInside]; |
186 [_closeButton setExclusiveTouch:YES]; | 189 [_closeButton setExclusiveTouch:YES]; |
187 [_topBar addSubview:_closeButton]; | 190 [_topBar addSubview:_closeButton]; |
188 | 191 |
189 // Constraints on the title label, favicon, and close button. | 192 // Constraints on the title label, favicon, and close button. |
190 NSDictionary* barViewsDictionary = @{ | 193 NSDictionary* barViewsDictionary = @{ |
191 @"favicon" : _favicon.get(), | 194 @"favicon" : _favicon, |
192 @"title" : _titleLabel.get(), | 195 @"title" : _titleLabel, |
193 @"closeButton" : _closeButton.get() | 196 @"closeButton" : _closeButton |
194 }; | 197 }; |
195 NSArray* barConstraints = @[ | 198 NSArray* barConstraints = @[ |
196 @"H:|-16-[favicon(==24)]-8-[title]-0-[closeButton(==32)]-8-|", | 199 @"H:|-16-[favicon(==24)]-8-[title]-0-[closeButton(==32)]-8-|", |
197 @"V:[favicon(==24)]", | 200 @"V:[favicon(==24)]", |
198 @"V:[closeButton(==32)]", | 201 @"V:[closeButton(==32)]", |
199 ]; | 202 ]; |
200 ApplyVisualConstraints(barConstraints, barViewsDictionary, _topBar); | 203 ApplyVisualConstraints(barConstraints, barViewsDictionary, _topBar); |
201 AddSameCenterYConstraint(_topBar, _favicon); | 204 AddSameCenterYConstraint(_topBar, _favicon); |
202 AddSameCenterYConstraint(_topBar, _titleLabel); | 205 AddSameCenterYConstraint(_topBar, _titleLabel); |
203 AddSameCenterYConstraint(_topBar, _closeButton); | 206 AddSameCenterYConstraint(_topBar, _closeButton); |
204 } | 207 } |
205 return self; | 208 return self; |
206 } | 209 } |
207 | 210 |
208 - (UIView*)topBar { | 211 - (UIView*)topBar { |
209 return _topBar.get(); | 212 return _topBar; |
210 } | 213 } |
211 | 214 |
212 - (UIImage*)screenshot { | 215 - (UIImage*)screenshot { |
213 return [_snapshot image]; | 216 return [_snapshot image]; |
214 } | 217 } |
215 | 218 |
216 - (void)setSnapshot:(UIImage*)image { | 219 - (void)setSnapshot:(UIImage*)image { |
217 DCHECK(!ImageHasAlphaChannel(image)); | 220 DCHECK(!ImageHasAlphaChannel(image)); |
218 [_snapshot setImage:image]; | 221 [_snapshot setImage:image]; |
219 } | 222 } |
220 | 223 |
221 - (void)setAppearanceForTab:(Tab*)tab cellSize:(CGSize)cellSize { | 224 - (void)setAppearanceForTab:(Tab*)tab cellSize:(CGSize)cellSize { |
222 [_titleLabel setText:tab.title]; | 225 [_titleLabel setText:tab.title]; |
223 [self contentView].accessibilityLabel = tab.title; | 226 [self contentView].accessibilityLabel = tab.title; |
224 if (tab.favicon) { | 227 if (tab.favicon) { |
225 [_favicon setImage:tab.favicon]; | 228 [_favicon setImage:tab.favicon]; |
226 } else { | 229 } else { |
227 [_favicon setImage:NativeImage(IDR_IOS_OMNIBOX_HTTP)]; | 230 [_favicon setImage:NativeImage(IDR_IOS_OMNIBOX_HTTP)]; |
228 } | 231 } |
229 | 232 |
230 CGSize snapshotSize = cellSize; | 233 CGSize snapshotSize = cellSize; |
231 snapshotSize.height -= tabSwitcherLocalSessionCellTopBarHeight(); | 234 snapshotSize.height -= tabSwitcherLocalSessionCellTopBarHeight(); |
232 base::WeakNSObject<TabSwitcherLocalSessionCell> weakCell(self); | 235 __weak TabSwitcherLocalSessionCell* weakCell = self; |
233 DCHECK(self.delegate); | 236 DCHECK(self.delegate); |
234 DCHECK([self cache]); | 237 DCHECK([self cache]); |
235 _currentPendingSnapshotRequest = | 238 _currentPendingSnapshotRequest = |
236 [[self cache] requestSnapshotForTab:tab | 239 [[self cache] requestSnapshotForTab:tab |
237 withSize:snapshotSize | 240 withSize:snapshotSize |
238 completionBlock:^(UIImage* image) { | 241 completionBlock:^(UIImage* image) { |
239 DCHECK([NSThread isMainThread]); | 242 DCHECK([NSThread isMainThread]); |
240 [weakCell setSnapshot:image]; | 243 [weakCell setSnapshot:image]; |
241 _currentPendingSnapshotRequest = {}; | 244 _currentPendingSnapshotRequest = {}; |
242 }]; | 245 }]; |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 [super prepareForReuse]; | 300 [super prepareForReuse]; |
298 } | 301 } |
299 | 302 |
300 - (TabSwitcherCache*)cache { | 303 - (TabSwitcherCache*)cache { |
301 return [self.delegate tabSwitcherCache]; | 304 return [self.delegate tabSwitcherCache]; |
302 } | 305 } |
303 | 306 |
304 #pragma mark - UIAccessibilityAction | 307 #pragma mark - UIAccessibilityAction |
305 | 308 |
306 - (NSArray*)accessibilityCustomActions { | 309 - (NSArray*)accessibilityCustomActions { |
307 base::scoped_nsobject<NSMutableArray> customActions( | 310 NSMutableArray* customActions = [[NSMutableArray alloc] init]; |
308 [[NSMutableArray alloc] init]); | 311 UIAccessibilityCustomAction* customAction = |
309 base::scoped_nsobject<UIAccessibilityCustomAction> customAction( | |
310 [[UIAccessibilityCustomAction alloc] | 312 [[UIAccessibilityCustomAction alloc] |
311 initWithName:l10n_util::GetNSString(IDS_IOS_TAB_SWITCHER_CLOSE_TAB) | 313 initWithName:l10n_util::GetNSString(IDS_IOS_TAB_SWITCHER_CLOSE_TAB) |
312 target:self | 314 target:self |
313 selector:@selector(closeButtonPressed)]); | 315 selector:@selector(closeButtonPressed)]; |
314 [customActions addObject:customAction.autorelease()]; | 316 [customActions addObject:customAction = nil]; |
315 return customActions.autorelease(); | 317 return customActions = nil; |
316 } | 318 } |
317 | 319 |
318 @end | 320 @end |
319 | 321 |
320 @implementation TabSwitcherDistantSessionCell { | 322 @implementation TabSwitcherDistantSessionCell { |
321 base::scoped_nsobject<UILabel> _titleLabel; | 323 UILabel* _titleLabel; |
322 base::scoped_nsobject<UIImageView> _favicon; | 324 UIImageView* _favicon; |
323 base::scoped_nsobject<UIImageView> _newTabIcon; | 325 UIImageView* _newTabIcon; |
324 base::scoped_nsobject<UIView> _verticallyCenteredView; | 326 UIView* _verticallyCenteredView; |
325 base::scoped_nsobject<TabSwitcherButton> _raisedButton; | 327 TabSwitcherButton* _raisedButton; |
326 base::scoped_nsobject<NSOperation> _faviconObtainer; | 328 NSOperation* _faviconObtainer; |
327 } | 329 } |
328 | 330 |
329 - (instancetype)initWithFrame:(CGRect)frame { | 331 - (instancetype)initWithFrame:(CGRect)frame { |
330 self = [super initWithFrame:frame]; | 332 self = [super initWithFrame:frame]; |
331 if (self) { | 333 if (self) { |
332 // Create and add the button that contains all other subviews. | 334 // Create and add the button that contains all other subviews. |
333 _raisedButton.reset([[TabSwitcherButton alloc] initWithFrame:CGRectZero]); | 335 _raisedButton = [[TabSwitcherButton alloc] initWithFrame:CGRectZero]; |
334 [_raisedButton setTranslatesAutoresizingMaskIntoConstraints:NO]; | 336 [_raisedButton setTranslatesAutoresizingMaskIntoConstraints:NO]; |
335 [_raisedButton addTarget:self | 337 [_raisedButton addTarget:self |
336 action:@selector(cellPressed) | 338 action:@selector(cellPressed) |
337 forControlEvents:UIControlEventTouchUpInside]; | 339 forControlEvents:UIControlEventTouchUpInside]; |
338 [[self containerView] addSubview:_raisedButton]; | 340 [[self containerView] addSubview:_raisedButton]; |
339 ApplyVisualConstraints(@[ @"H:|-0-[button]-0-|", @"V:|-0-[button]-0-|" ], | 341 ApplyVisualConstraints(@[ @"H:|-0-[button]-0-|", @"V:|-0-[button]-0-|" ], |
340 @{ @"button" : _raisedButton.get() }, | 342 @{ @"button" : _raisedButton }, |
341 [self containerView]); | 343 [self containerView]); |
342 | 344 |
343 // Create and add view that will be vertically centered in the space over | 345 // Create and add view that will be vertically centered in the space over |
344 // the favicon. | 346 // the favicon. |
345 _verticallyCenteredView.reset([[UIView alloc] initWithFrame:CGRectZero]); | 347 _verticallyCenteredView = [[UIView alloc] initWithFrame:CGRectZero]; |
346 [_verticallyCenteredView setTranslatesAutoresizingMaskIntoConstraints:NO]; | 348 [_verticallyCenteredView setTranslatesAutoresizingMaskIntoConstraints:NO]; |
347 [_verticallyCenteredView setUserInteractionEnabled:NO]; | 349 [_verticallyCenteredView setUserInteractionEnabled:NO]; |
348 [_raisedButton addSubview:_verticallyCenteredView]; | 350 [_raisedButton addSubview:_verticallyCenteredView]; |
349 | 351 |
350 // Create and add title label to |_verticallyCenteredContent|. | 352 // Create and add title label to |_verticallyCenteredContent|. |
351 _titleLabel.reset([[UILabel alloc] initWithFrame:CGRectZero]); | 353 _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; |
352 [_titleLabel setTranslatesAutoresizingMaskIntoConstraints:NO]; | 354 [_titleLabel setTranslatesAutoresizingMaskIntoConstraints:NO]; |
353 [_titleLabel setNumberOfLines:5]; | 355 [_titleLabel setNumberOfLines:5]; |
354 [_titleLabel setTextAlignment:NSTextAlignmentCenter]; | 356 [_titleLabel setTextAlignment:NSTextAlignmentCenter]; |
355 [_titleLabel setFont:[[MDFRobotoFontLoader sharedInstance] | 357 [_titleLabel setFont:[[MDFRobotoFontLoader sharedInstance] |
356 regularFontOfSize:kFontSize]]; | 358 regularFontOfSize:kFontSize]]; |
357 [_verticallyCenteredView addSubview:_titleLabel]; | 359 [_verticallyCenteredView addSubview:_titleLabel]; |
358 | 360 |
359 // Create and add new tab icon to |_verticallyCenteredContent|. | 361 // Create and add new tab icon to |_verticallyCenteredContent|. |
360 UIImage* newTabIcon = [[UIImage imageNamed:@"tabswitcher_new_tab"] | 362 UIImage* newTabIcon = [[UIImage imageNamed:@"tabswitcher_new_tab"] |
361 imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; | 363 imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; |
362 _newTabIcon.reset([[UIImageView alloc] initWithImage:newTabIcon]); | 364 _newTabIcon = [[UIImageView alloc] initWithImage:newTabIcon]; |
363 [_newTabIcon setAlpha:0]; | 365 [_newTabIcon setAlpha:0]; |
364 [_newTabIcon setTranslatesAutoresizingMaskIntoConstraints:NO]; | 366 [_newTabIcon setTranslatesAutoresizingMaskIntoConstraints:NO]; |
365 [_verticallyCenteredView addSubview:_newTabIcon]; | 367 [_verticallyCenteredView addSubview:_newTabIcon]; |
366 | 368 |
367 // Create and add favicon image container. | 369 // Create and add favicon image container. |
368 _favicon.reset([[UIImageView alloc] initWithFrame:CGRectZero]); | 370 _favicon = [[UIImageView alloc] initWithFrame:CGRectZero]; |
369 [_favicon setTranslatesAutoresizingMaskIntoConstraints:NO]; | 371 [_favicon setTranslatesAutoresizingMaskIntoConstraints:NO]; |
370 [_raisedButton addSubview:_favicon]; | 372 [_raisedButton addSubview:_favicon]; |
371 | 373 |
372 // Add constraints to the button's subviews. | 374 // Add constraints to the button's subviews. |
373 NSDictionary* viewsDictionary = @{ | 375 NSDictionary* viewsDictionary = @{ |
374 @"newTabIcon" : _newTabIcon.get(), | 376 @"newTabIcon" : _newTabIcon, |
375 @"title" : _titleLabel.get(), | 377 @"title" : _titleLabel, |
376 @"favicon" : _favicon.get(), | 378 @"favicon" : _favicon, |
377 @"centeredView" : _verticallyCenteredView.get(), | 379 @"centeredView" : _verticallyCenteredView, |
378 }; | 380 }; |
379 NSArray* constraintsInButton = @[ | 381 NSArray* constraintsInButton = @[ |
380 @"H:|-0-[centeredView]-0-|", | 382 @"H:|-0-[centeredView]-0-|", |
381 @"H:[favicon(==16)]", | 383 @"H:[favicon(==16)]", |
382 @"V:|-(>=16)-[centeredView]-(>=16)-[favicon(==16)]-16-|", | 384 @"V:|-(>=16)-[centeredView]-(>=16)-[favicon(==16)]-16-|", |
383 ]; | 385 ]; |
384 ApplyVisualConstraints(constraintsInButton, viewsDictionary, _raisedButton); | 386 ApplyVisualConstraints(constraintsInButton, viewsDictionary, _raisedButton); |
385 AddSameCenterXConstraint(_raisedButton, _favicon.get()); | 387 AddSameCenterXConstraint(_raisedButton, _favicon); |
386 [_raisedButton | 388 [_raisedButton addConstraint:[NSLayoutConstraint |
387 addConstraint:[NSLayoutConstraint | 389 constraintWithItem:_verticallyCenteredView |
388 constraintWithItem:_verticallyCenteredView.get() | 390 attribute:NSLayoutAttributeCenterY |
389 attribute:NSLayoutAttributeCenterY | 391 relatedBy:NSLayoutRelationEqual |
390 relatedBy:NSLayoutRelationEqual | 392 toItem:_favicon |
391 toItem:_favicon.get() | 393 attribute:NSLayoutAttributeCenterY |
392 attribute:NSLayoutAttributeCenterY | 394 multiplier:0.5 |
393 multiplier:0.5 | 395 constant:0]]; |
394 constant:0]]; | |
395 | 396 |
396 // Add constraints to the subviews of the vertically centered view. | 397 // Add constraints to the subviews of the vertically centered view. |
397 NSArray* constraintsInVerticallyCenteredView = @[ | 398 NSArray* constraintsInVerticallyCenteredView = @[ |
398 @"H:|-16-[title]-16-|", | 399 @"H:|-16-[title]-16-|", |
399 @"V:|-0-[newTabIcon(==24)]-16-[title(>=16)]-0-|", | 400 @"V:|-0-[newTabIcon(==24)]-16-[title(>=16)]-0-|", |
400 ]; | 401 ]; |
401 ApplyVisualConstraints(constraintsInVerticallyCenteredView, viewsDictionary, | 402 ApplyVisualConstraints(constraintsInVerticallyCenteredView, viewsDictionary, |
402 _verticallyCenteredView); | 403 _verticallyCenteredView); |
403 AddSameCenterXConstraint(_verticallyCenteredView, _newTabIcon.get()); | 404 AddSameCenterXConstraint(_verticallyCenteredView, _newTabIcon); |
404 } | 405 } |
405 return self; | 406 return self; |
406 } | 407 } |
407 | 408 |
408 - (void)setTitle:(NSString*)titleString { | 409 - (void)setTitle:(NSString*)titleString { |
409 [_titleLabel setText:titleString]; | 410 [_titleLabel setText:titleString]; |
410 [self contentView].accessibilityLabel = titleString; | 411 [self contentView].accessibilityLabel = titleString; |
411 } | 412 } |
412 | 413 |
413 - (void)setSessionGURL:(GURL const&)gurl | 414 - (void)setSessionGURL:(GURL const&)gurl |
(...skipping 16 matching lines...) Expand all Loading... |
430 [_titleLabel setTextColor:textColor]; | 431 [_titleLabel setTextColor:textColor]; |
431 [_newTabIcon setTintColor:iconColor]; | 432 [_newTabIcon setTintColor:iconColor]; |
432 [_newTabIcon setAlpha:1.0]; | 433 [_newTabIcon setAlpha:1.0]; |
433 [_favicon setImage:favicon]; | 434 [_favicon setImage:favicon]; |
434 [UIView animateWithDuration:0.2 | 435 [UIView animateWithDuration:0.2 |
435 animations:^{ | 436 animations:^{ |
436 [_raisedButton setAlpha:1.0]; | 437 [_raisedButton setAlpha:1.0]; |
437 }]; | 438 }]; |
438 }; | 439 }; |
439 GURL gurlCopy = gurl; | 440 GURL gurlCopy = gurl; |
440 _faviconObtainer.reset([[NSBlockOperation blockOperationWithBlock:^{ | 441 _faviconObtainer = [NSBlockOperation blockOperationWithBlock:^{ |
441 TabSwitcherGetFavicon(gurlCopy, browserState, block); | 442 TabSwitcherGetFavicon(gurlCopy, browserState, block); |
442 }] retain]); | 443 }]; |
443 NSOperationQueue* operationQueue = [NSOperationQueue mainQueue]; | 444 NSOperationQueue* operationQueue = [NSOperationQueue mainQueue]; |
444 [operationQueue addOperation:_faviconObtainer]; | 445 [operationQueue addOperation:_faviconObtainer]; |
445 } | 446 } |
446 | 447 |
447 - (void)cellPressed { | 448 - (void)cellPressed { |
448 [self.delegate cellPressed:self]; | 449 [self.delegate cellPressed:self]; |
449 } | 450 } |
450 | 451 |
451 - (void)prepareForReuse { | 452 - (void)prepareForReuse { |
452 [_newTabIcon setAlpha:0]; | 453 [_newTabIcon setAlpha:0]; |
453 [_faviconObtainer cancel]; | 454 [_faviconObtainer cancel]; |
454 _faviconObtainer.reset(); | 455 _faviconObtainer = nil; |
455 [_raisedButton setAlpha:0]; | 456 [_raisedButton setAlpha:0]; |
456 [_raisedButton resetState]; | 457 [_raisedButton resetState]; |
457 [super prepareForReuse]; | 458 [super prepareForReuse]; |
458 } | 459 } |
459 | 460 |
460 @end | 461 @end |
OLD | NEW |