| OLD | NEW | 
|---|
| 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. |  | 
| 50   NSArray* buttons_;  // UIButton |  | 
| 51   NSArray* items_;    // NewTabPageBarItem |  | 
| 52   // Which button is currently selected. | 52   // Which button is currently selected. | 
| 53   NSUInteger selectedIndex_; | 53   NSUInteger selectedIndex_; | 
| 54   // Popup button helper, for iPhone labels. |  | 
| 55   UIButton* popupButton_; |  | 
| 56   // Don't allow tabbar animations on startup, only after first tap. | 54   // Don't allow tabbar animations on startup, only after first tap. | 
| 57   BOOL canAnimate_; | 55   BOOL canAnimate_; | 
| 58   id<NewTabPageBarDelegate> delegate_;  // weak | 56   __weak id<NewTabPageBarDelegate> delegate_; | 
| 59   // Logo view, used to center the tab buttons. | 57   // Logo view, used to center the tab buttons. | 
| 60   base::scoped_nsobject<UIImageView> logoView_; | 58   UIImageView* logoView_; | 
| 61   // Overlay view, used to highlight the selected button. | 59   // Overlay view, used to highlight the selected button. | 
| 62   base::scoped_nsobject<UIImageView> overlayView_; | 60   UIImageView* overlayView_; | 
| 63   // Overlay view, used to highlight the selected button. | 61   // Overlay view, used to highlight the selected button. | 
| 64   base::scoped_nsobject<UIView> overlayColorView_; | 62   UIView* overlayColorView_; | 
| 65   // Width of a button. | 63   // Width of a button. | 
| 66   CGFloat buttonWidth_; | 64   CGFloat buttonWidth_; | 
| 67   // Percentage overlay sits over tab bar buttons. | 65   // Percentage overlay sits over tab bar buttons. | 
| 68   CGFloat overlayPercentage_; | 66   CGFloat overlayPercentage_; | 
| 69 |  | 
| 70   base::mac::ObjCPropertyReleaser propertyReleaser_NewTabPageBar_; |  | 
| 71 } | 67 } | 
| 72 | 68 | 
| 73 @synthesize items = items_; | 69 @synthesize items = items_; | 
| 74 @synthesize selectedIndex = selectedIndex_; | 70 @synthesize selectedIndex = selectedIndex_; | 
| 75 @synthesize popupButton = popupButton_; | 71 @synthesize popupButton = popupButton_; | 
| 76 @synthesize buttons = buttons_; | 72 @synthesize buttons = buttons_; | 
| 77 @synthesize delegate = delegate_; | 73 @synthesize delegate = delegate_; | 
| 78 @synthesize overlayPercentage = overlayPercentage_; | 74 @synthesize overlayPercentage = overlayPercentage_; | 
| 79 | 75 | 
| 80 - (id)initWithFrame:(CGRect)frame { | 76 - (id)initWithFrame:(CGRect)frame { | 
| 81   self = [super initWithFrame:frame]; | 77   self = [super initWithFrame:frame]; | 
| 82   if (self) { | 78   if (self) { | 
| 83     [self setup]; | 79     [self setup]; | 
| 84   } | 80   } | 
| 85   return self; | 81   return self; | 
| 86 } | 82 } | 
| 87 | 83 | 
| 88 - (id)initWithCoder:(NSCoder*)aDecoder { | 84 - (id)initWithCoder:(NSCoder*)aDecoder { | 
| 89   self = [super initWithCoder:aDecoder]; | 85   self = [super initWithCoder:aDecoder]; | 
| 90   if (self) { | 86   if (self) { | 
| 91     [self setup]; | 87     [self setup]; | 
| 92   } | 88   } | 
| 93   return self; | 89   return self; | 
| 94 } | 90 } | 
| 95 | 91 | 
| 96 - (void)setup { | 92 - (void)setup { | 
| 97   propertyReleaser_NewTabPageBar_.Init(self, [NewTabPageBar class]); |  | 
| 98   self.selectedIndex = NSNotFound; | 93   self.selectedIndex = NSNotFound; | 
| 99   canAnimate_ = NO; | 94   canAnimate_ = NO; | 
| 100   self.autoresizingMask = | 95   self.autoresizingMask = | 
| 101       UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin; | 96       UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin; | 
| 102   self.autoresizesSubviews = YES; | 97   self.autoresizesSubviews = YES; | 
| 103   self.backgroundColor = [UIColor clearColor]; | 98   self.backgroundColor = [UIColor clearColor]; | 
| 104 | 99 | 
| 105   if ([self showOverlay]) { | 100   if ([self showOverlay]) { | 
| 106     overlayView_.reset( | 101     overlayView_ = | 
| 107         [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, buttonWidth_, 2)]); | 102         [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, buttonWidth_, 2)]; | 
| 108 | 103 | 
| 109     // Center |overlayColorView_| inside |overlayView_|. | 104     // Center |overlayColorView_| inside |overlayView_|. | 
| 110     CGFloat colorX = AlignValueToPixel((buttonWidth_ - kOverlayColorWidth) / 2); | 105     CGFloat colorX = AlignValueToPixel((buttonWidth_ - kOverlayColorWidth) / 2); | 
| 111     overlayColorView_.reset([[UIView alloc] | 106     overlayColorView_ = [[UIView alloc] | 
| 112         initWithFrame:CGRectMake(colorX, 0, kOverlayColorWidth, 2)]); | 107         initWithFrame:CGRectMake(colorX, 0, kOverlayColorWidth, 2)]; | 
| 113     [overlayColorView_ | 108     [overlayColorView_ | 
| 114         setBackgroundColor:UIColorFromRGB(kOverlayViewColor, 1.0)]; | 109         setBackgroundColor:UIColorFromRGB(kOverlayViewColor, 1.0)]; | 
| 115     [overlayColorView_ layer].cornerRadius = 1.0; | 110     [overlayColorView_ layer].cornerRadius = 1.0; | 
| 116     [overlayColorView_ | 111     [overlayColorView_ | 
| 117         setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | | 112         setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | | 
| 118                             UIViewAutoresizingFlexibleRightMargin]; | 113                             UIViewAutoresizingFlexibleRightMargin]; | 
| 119     [overlayView_ addSubview:overlayColorView_]; | 114     [overlayView_ addSubview:overlayColorView_]; | 
| 120     [self addSubview:overlayView_]; | 115     [self addSubview:overlayView_]; | 
| 121   } | 116   } | 
| 122 | 117 | 
| 123   // Make the drop shadow. | 118   // Make the drop shadow. | 
| 124   UIImage* shadowImage = [UIImage imageNamed:@"ntp_bottom_bar_shadow"]; | 119   UIImage* shadowImage = [UIImage imageNamed:@"ntp_bottom_bar_shadow"]; | 
| 125   shadow_.reset([[UIImageView alloc] initWithImage:shadowImage]); | 120   shadow_ = [[UIImageView alloc] initWithImage:shadowImage]; | 
| 126   // Shadow is positioned directly above the new tab page bar. | 121   // Shadow is positioned directly above the new tab page bar. | 
| 127   [shadow_ | 122   [shadow_ | 
| 128       setFrame:CGRectMake(0, -shadowImage.size.height, self.bounds.size.width, | 123       setFrame:CGRectMake(0, -shadowImage.size.height, self.bounds.size.width, | 
| 129                           shadowImage.size.height)]; | 124                           shadowImage.size.height)]; | 
| 130   [shadow_ setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; | 125   [shadow_ setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; | 
| 131   [self addSubview:shadow_]; | 126   [self addSubview:shadow_]; | 
| 132 | 127 | 
| 133   self.contentMode = UIViewContentModeRedraw; | 128   self.contentMode = UIViewContentModeRedraw; | 
| 134 } | 129 } | 
| 135 | 130 | 
| 136 - (void)layoutSubviews { | 131 - (void)layoutSubviews { | 
| 137   [super layoutSubviews]; | 132   [super layoutSubviews]; | 
| 138 | 133 | 
| 139   // |buttonWidth_| changes with the screen orientation when the NTP button bar | 134   // |buttonWidth_| changes with the screen orientation when the NTP button bar | 
| 140   // is enabled. | 135   // is enabled. | 
| 141   [self calculateButtonWidth]; | 136   [self calculateButtonWidth]; | 
| 142 | 137 | 
| 143   CGFloat logoWidth = logoView_.get().image.size.width; | 138   CGFloat logoWidth = logoView_.image.size.width; | 
| 144   CGFloat padding = [self useIconsInButtons] ? logoWidth : 0; | 139   CGFloat padding = [self useIconsInButtons] ? logoWidth : 0; | 
| 145   CGFloat buttonPadding = floor((CGRectGetWidth(self.bounds) - padding - | 140   CGFloat buttonPadding = floor((CGRectGetWidth(self.bounds) - padding - | 
| 146                                  buttonWidth_ * self.buttons.count) / | 141                                  buttonWidth_ * self.buttons.count) / | 
| 147                                     2 + | 142                                     2 + | 
| 148                                 padding); | 143                                 padding); | 
| 149 | 144 | 
| 150   for (NSUInteger i = 0; i < self.buttons.count; ++i) { | 145   for (NSUInteger i = 0; i < self.buttons.count; ++i) { | 
| 151     NewTabPageBarButton* button = [self.buttons objectAtIndex:i]; | 146     NewTabPageBarButton* button = [self.buttons objectAtIndex:i]; | 
| 152     LayoutRect layout = LayoutRectMake( | 147     LayoutRect layout = LayoutRectMake( | 
| 153         buttonPadding + (i * buttonWidth_), CGRectGetWidth(self.bounds), 0, | 148         buttonPadding + (i * buttonWidth_), CGRectGetWidth(self.bounds), 0, | 
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 186 | 181 | 
| 187   buttonWidth_ = kRegularLayoutButtonWidth; | 182   buttonWidth_ = kRegularLayoutButtonWidth; | 
| 188 } | 183 } | 
| 189 | 184 | 
| 190 // When setting a new set of items on the tab bar, the buttons need to be | 185 // 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. | 186 // regenerated and the old buttons need to be removed. | 
| 192 - (void)setItems:(NSArray*)newItems { | 187 - (void)setItems:(NSArray*)newItems { | 
| 193   if (newItems == items_) | 188   if (newItems == items_) | 
| 194     return; | 189     return; | 
| 195 | 190 | 
| 196   [items_ autorelease]; | 191   items_ = newItems; | 
| 197   items_ = [newItems retain]; |  | 
| 198   // Remove all the existing buttons from the view. | 192   // Remove all the existing buttons from the view. | 
| 199   for (UIButton* button in self.buttons) { | 193   for (UIButton* button in self.buttons) { | 
| 200     [button removeFromSuperview]; | 194     [button removeFromSuperview]; | 
| 201   } | 195   } | 
| 202 | 196 | 
| 203   // Create a set of new buttons. | 197   // Create a set of new buttons. | 
| 204   [self calculateButtonWidth]; | 198   [self calculateButtonWidth]; | 
| 205   if (newItems.count) { | 199   if (newItems.count) { | 
| 206     NSMutableArray* newButtons = [NSMutableArray array]; | 200     NSMutableArray* newButtons = [NSMutableArray array]; | 
| 207     for (NSUInteger i = 0; i < newItems.count; ++i) { | 201     for (NSUInteger i = 0; i < newItems.count; ++i) { | 
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 322   return !IsIPadIdiom() || IsCompactTablet(); | 316   return !IsIPadIdiom() || IsCompactTablet(); | 
| 323 } | 317 } | 
| 324 | 318 | 
| 325 - (BOOL)showOverlay { | 319 - (BOOL)showOverlay { | 
| 326   // The bar buttons launch modal dialogs on tap on iPhone. Don't show overlay | 320   // The bar buttons launch modal dialogs on tap on iPhone. Don't show overlay | 
| 327   // in this case. | 321   // in this case. | 
| 328   return IsIPadIdiom(); | 322   return IsIPadIdiom(); | 
| 329 } | 323 } | 
| 330 | 324 | 
| 331 @end | 325 @end | 
| OLD | NEW | 
|---|