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

Side by Side Diff: ios/chrome/browser/ui/ntp/new_tab_page_bar.mm

Issue 2889963002: [ObjC ARC] Converts ios/chrome/browser/ui/ntp:ntp_internal to ARC.
Patch Set: Moved testing method accessing ivars to a category Created 3 years, 6 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
1 // Copyright 2012 The Chromium Authors. All rights reserved. 1 // Copyright 2012 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/ntp/new_tab_page_bar.h" 5 #import "ios/chrome/browser/ui/ntp/new_tab_page_bar.h"
6 6
7 #import <QuartzCore/QuartzCore.h> 7 #import <QuartzCore/QuartzCore.h>
8 #include <cmath> 8 #include <cmath>
9 9
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/mac/objc_property_releaser.h" 11
12 #include "base/mac/scoped_nsobject.h"
13 #import "ios/chrome/browser/ui/bookmarks/bookmark_utils_ios.h" 12 #import "ios/chrome/browser/ui/bookmarks/bookmark_utils_ios.h"
14 #import "ios/chrome/browser/ui/ntp/new_tab_page_bar_button.h" 13 #import "ios/chrome/browser/ui/ntp/new_tab_page_bar_button.h"
15 #import "ios/chrome/browser/ui/ntp/new_tab_page_bar_item.h" 14 #import "ios/chrome/browser/ui/ntp/new_tab_page_bar_item.h"
16 #import "ios/chrome/browser/ui/rtl_geometry.h" 15 #import "ios/chrome/browser/ui/rtl_geometry.h"
17 #include "ios/chrome/browser/ui/ui_util.h" 16 #include "ios/chrome/browser/ui/ui_util.h"
18 #import "ios/chrome/browser/ui/uikit_ui_util.h" 17 #import "ios/chrome/browser/ui/uikit_ui_util.h"
19 #import "ui/gfx/ios/NSString+CrStringDrawing.h" 18 #import "ui/gfx/ios/NSString+CrStringDrawing.h"
20 #include "ui/gfx/scoped_ui_graphics_push_context_ios.h" 19 #include "ui/gfx/scoped_ui_graphics_push_context_ios.h"
21 20
21 #if !defined(__has_feature) || !__has_feature(objc_arc)
22 #error "This file requires ARC support."
23 #endif
24
22 namespace { 25 namespace {
23 26
24 const CGFloat kBarHeight = 48.0f; 27 const CGFloat kBarHeight = 48.0f;
25 28
26 const CGFloat kRegularLayoutButtonWidth = 168; 29 const CGFloat kRegularLayoutButtonWidth = 168;
27 30
28 const int kOverlayViewColor = 0x5A7EF5; 31 const int kOverlayViewColor = 0x5A7EF5;
29 const int kOverlayColorWidth = 98; 32 const int kOverlayColorWidth = 98;
30 const int kNumberOfTabsIncognito = 2; 33 const int kNumberOfTabsIncognito = 2;
31 34
32 } // anonymous namespace 35 } // anonymous namespace
33 36
34 @interface NewTabPageBar () { 37 @interface NewTabPageBar () {
35 base::scoped_nsobject<UIImageView> shadow_; 38 UIImageView* shadow_;
36 } 39 }
37 40
38 @property(nonatomic, readwrite, retain) NSArray* buttons; 41 @property(nonatomic, readwrite, strong) NSArray* buttons;
39 @property(nonatomic, readwrite, retain) UIButton* popupButton; 42 @property(nonatomic, readwrite, strong) UIButton* popupButton;
40 43
41 - (void)setup; 44 - (void)setup;
42 - (void)calculateButtonWidth; 45 - (void)calculateButtonWidth;
43 - (void)setupButton:(UIButton*)button; 46 - (void)setupButton:(UIButton*)button;
44 - (BOOL)useIconsInButtons; 47 - (BOOL)useIconsInButtons;
45 - (BOOL)showOverlay; 48 - (BOOL)showOverlay;
46 @end 49 @end
47 50
48 @implementation NewTabPageBar { 51 @implementation NewTabPageBar {
49 // Tabbar buttons. 52 // Tabbar buttons.
50 NSArray* buttons_; // UIButton 53 NSArray* buttons_; // UIButton
51 NSArray* items_; // NewTabPageBarItem 54 NSArray* items_; // NewTabPageBarItem
52 // Which button is currently selected. 55 // Which button is currently selected.
53 NSUInteger selectedIndex_; 56 NSUInteger selectedIndex_;
54 // Popup button helper, for iPhone labels. 57 // Popup button helper, for iPhone labels.
55 UIButton* popupButton_; 58 UIButton* popupButton_;
56 // Don't allow tabbar animations on startup, only after first tap. 59 // Don't allow tabbar animations on startup, only after first tap.
57 BOOL canAnimate_; 60 BOOL canAnimate_;
58 id<NewTabPageBarDelegate> delegate_; // weak 61 __weak id<NewTabPageBarDelegate> delegate_;
59 // Logo view, used to center the tab buttons. 62 // Logo view, used to center the tab buttons.
60 base::scoped_nsobject<UIImageView> logoView_; 63 UIImageView* logoView_;
61 // Overlay view, used to highlight the selected button. 64 // Overlay view, used to highlight the selected button.
62 base::scoped_nsobject<UIImageView> overlayView_; 65 UIImageView* overlayView_;
63 // Overlay view, used to highlight the selected button. 66 // Overlay view, used to highlight the selected button.
64 base::scoped_nsobject<UIView> overlayColorView_; 67 UIView* overlayColorView_;
65 // Width of a button. 68 // Width of a button.
66 CGFloat buttonWidth_; 69 CGFloat buttonWidth_;
67 // Percentage overlay sits over tab bar buttons. 70 // Percentage overlay sits over tab bar buttons.
68 CGFloat overlayPercentage_; 71 CGFloat overlayPercentage_;
69
70 base::mac::ObjCPropertyReleaser propertyReleaser_NewTabPageBar_;
71 } 72 }
72 73
73 @synthesize items = items_; 74 @synthesize items = items_;
74 @synthesize selectedIndex = selectedIndex_; 75 @synthesize selectedIndex = selectedIndex_;
75 @synthesize popupButton = popupButton_; 76 @synthesize popupButton = popupButton_;
76 @synthesize buttons = buttons_; 77 @synthesize buttons = buttons_;
77 @synthesize delegate = delegate_; 78 @synthesize delegate = delegate_;
78 @synthesize overlayPercentage = overlayPercentage_; 79 @synthesize overlayPercentage = overlayPercentage_;
79 80
80 - (id)initWithFrame:(CGRect)frame { 81 - (id)initWithFrame:(CGRect)frame {
81 self = [super initWithFrame:frame]; 82 self = [super initWithFrame:frame];
82 if (self) { 83 if (self) {
83 [self setup]; 84 [self setup];
84 } 85 }
85 return self; 86 return self;
86 } 87 }
87 88
88 - (id)initWithCoder:(NSCoder*)aDecoder { 89 - (id)initWithCoder:(NSCoder*)aDecoder {
89 self = [super initWithCoder:aDecoder]; 90 self = [super initWithCoder:aDecoder];
90 if (self) { 91 if (self) {
91 [self setup]; 92 [self setup];
92 } 93 }
93 return self; 94 return self;
94 } 95 }
95 96
96 - (void)setup { 97 - (void)setup {
97 propertyReleaser_NewTabPageBar_.Init(self, [NewTabPageBar class]);
98 self.selectedIndex = NSNotFound; 98 self.selectedIndex = NSNotFound;
99 canAnimate_ = NO; 99 canAnimate_ = NO;
100 self.autoresizingMask = 100 self.autoresizingMask =
101 UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin; 101 UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
102 self.autoresizesSubviews = YES; 102 self.autoresizesSubviews = YES;
103 self.backgroundColor = [UIColor clearColor]; 103 self.backgroundColor = [UIColor clearColor];
104 104
105 if ([self showOverlay]) { 105 if ([self showOverlay]) {
106 overlayView_.reset( 106 overlayView_ =
107 [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, buttonWidth_, 2)]); 107 [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, buttonWidth_, 2)];
108 108
109 // Center |overlayColorView_| inside |overlayView_|. 109 // Center |overlayColorView_| inside |overlayView_|.
110 CGFloat colorX = AlignValueToPixel((buttonWidth_ - kOverlayColorWidth) / 2); 110 CGFloat colorX = AlignValueToPixel((buttonWidth_ - kOverlayColorWidth) / 2);
111 overlayColorView_.reset([[UIView alloc] 111 overlayColorView_ = [[UIView alloc]
112 initWithFrame:CGRectMake(colorX, 0, kOverlayColorWidth, 2)]); 112 initWithFrame:CGRectMake(colorX, 0, kOverlayColorWidth, 2)];
113 [overlayColorView_ 113 [overlayColorView_
114 setBackgroundColor:UIColorFromRGB(kOverlayViewColor, 1.0)]; 114 setBackgroundColor:UIColorFromRGB(kOverlayViewColor, 1.0)];
115 [overlayColorView_ layer].cornerRadius = 1.0; 115 [overlayColorView_ layer].cornerRadius = 1.0;
116 [overlayColorView_ 116 [overlayColorView_
117 setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | 117 setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin |
118 UIViewAutoresizingFlexibleRightMargin]; 118 UIViewAutoresizingFlexibleRightMargin];
119 [overlayView_ addSubview:overlayColorView_]; 119 [overlayView_ addSubview:overlayColorView_];
120 [self addSubview:overlayView_]; 120 [self addSubview:overlayView_];
121 } 121 }
122 122
123 // Make the drop shadow. 123 // Make the drop shadow.
124 UIImage* shadowImage = [UIImage imageNamed:@"ntp_bottom_bar_shadow"]; 124 UIImage* shadowImage = [UIImage imageNamed:@"ntp_bottom_bar_shadow"];
125 shadow_.reset([[UIImageView alloc] initWithImage:shadowImage]); 125 shadow_ = [[UIImageView alloc] initWithImage:shadowImage];
126 // Shadow is positioned directly above the new tab page bar. 126 // Shadow is positioned directly above the new tab page bar.
127 [shadow_ 127 [shadow_
128 setFrame:CGRectMake(0, -shadowImage.size.height, self.bounds.size.width, 128 setFrame:CGRectMake(0, -shadowImage.size.height, self.bounds.size.width,
129 shadowImage.size.height)]; 129 shadowImage.size.height)];
130 [shadow_ setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; 130 [shadow_ setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
131 [self addSubview:shadow_]; 131 [self addSubview:shadow_];
132 132
133 self.contentMode = UIViewContentModeRedraw; 133 self.contentMode = UIViewContentModeRedraw;
134 } 134 }
135 135
136 - (void)layoutSubviews { 136 - (void)layoutSubviews {
137 [super layoutSubviews]; 137 [super layoutSubviews];
138 138
139 // |buttonWidth_| changes with the screen orientation when the NTP button bar 139 // |buttonWidth_| changes with the screen orientation when the NTP button bar
140 // is enabled. 140 // is enabled.
141 [self calculateButtonWidth]; 141 [self calculateButtonWidth];
142 142
143 CGFloat logoWidth = logoView_.get().image.size.width; 143 CGFloat logoWidth = logoView_.image.size.width;
144 CGFloat padding = [self useIconsInButtons] ? logoWidth : 0; 144 CGFloat padding = [self useIconsInButtons] ? logoWidth : 0;
145 CGFloat buttonPadding = floor((CGRectGetWidth(self.bounds) - padding - 145 CGFloat buttonPadding = floor((CGRectGetWidth(self.bounds) - padding -
146 buttonWidth_ * self.buttons.count) / 146 buttonWidth_ * self.buttons.count) /
147 2 + 147 2 +
148 padding); 148 padding);
149 149
150 for (NSUInteger i = 0; i < self.buttons.count; ++i) { 150 for (NSUInteger i = 0; i < self.buttons.count; ++i) {
151 NewTabPageBarButton* button = [self.buttons objectAtIndex:i]; 151 NewTabPageBarButton* button = [self.buttons objectAtIndex:i];
152 LayoutRect layout = LayoutRectMake( 152 LayoutRect layout = LayoutRectMake(
153 buttonPadding + (i * buttonWidth_), CGRectGetWidth(self.bounds), 0, 153 buttonPadding + (i * buttonWidth_), CGRectGetWidth(self.bounds), 0,
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 186
187 buttonWidth_ = kRegularLayoutButtonWidth; 187 buttonWidth_ = kRegularLayoutButtonWidth;
188 } 188 }
189 189
190 // When setting a new set of items on the tab bar, the buttons need to be 190 // When setting a new set of items on the tab bar, the buttons need to be
191 // regenerated and the old buttons need to be removed. 191 // regenerated and the old buttons need to be removed.
192 - (void)setItems:(NSArray*)newItems { 192 - (void)setItems:(NSArray*)newItems {
193 if (newItems == items_) 193 if (newItems == items_)
194 return; 194 return;
195 195
196 [items_ autorelease]; 196 items_ = newItems;
197 items_ = [newItems retain];
198 // Remove all the existing buttons from the view. 197 // Remove all the existing buttons from the view.
199 for (UIButton* button in self.buttons) { 198 for (UIButton* button in self.buttons) {
200 [button removeFromSuperview]; 199 [button removeFromSuperview];
201 } 200 }
202 201
203 // Create a set of new buttons. 202 // Create a set of new buttons.
204 [self calculateButtonWidth]; 203 [self calculateButtonWidth];
205 if (newItems.count) { 204 if (newItems.count) {
206 NSMutableArray* newButtons = [NSMutableArray array]; 205 NSMutableArray* newButtons = [NSMutableArray array];
207 for (NSUInteger i = 0; i < newItems.count; ++i) { 206 for (NSUInteger i = 0; i < newItems.count; ++i) {
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
322 return !IsIPadIdiom() || IsCompactTablet(); 321 return !IsIPadIdiom() || IsCompactTablet();
323 } 322 }
324 323
325 - (BOOL)showOverlay { 324 - (BOOL)showOverlay {
326 // The bar buttons launch modal dialogs on tap on iPhone. Don't show overlay 325 // The bar buttons launch modal dialogs on tap on iPhone. Don't show overlay
327 // in this case. 326 // in this case.
328 return IsIPadIdiom(); 327 return IsIPadIdiom();
329 } 328 }
330 329
331 @end 330 @end
OLDNEW
« no previous file with comments | « ios/chrome/browser/ui/ntp/new_tab_page_bar.h ('k') | ios/chrome/browser/ui/ntp/new_tab_page_bar_button.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698