Skip to content

Commit e5a68ba

Browse files
yarneomaterial-automation
authored andcommitted
[BottomNavigation] Adds opt-in from MDCShadowLayer to MDCShadow.
PiperOrigin-RevId: 369815810
1 parent 5cd7b82 commit e5a68ba

File tree

4 files changed

+89
-10
lines changed

4 files changed

+89
-10
lines changed

components/BottomNavigation/src/MDCBottomNavigationBar.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
// migrated to no longer import MDCBottomNavigationBarDelegate as a transitive dependency.
2020
#import "MDCBottomNavigationBarDelegate.h"
2121
#import "MaterialElevation.h"
22+
#import "MaterialShadow.h"
2223
#import "MaterialShadowElevations.h"
2324

2425
@protocol MDCBottomNavigationBarDelegate;

components/BottomNavigation/src/MDCBottomNavigationBar.m

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#import "MDCBottomNavigationBarDelegate.h"
2727
#import "MaterialPalettes.h"
2828
#import "MaterialRipple.h"
29+
#import "MaterialShadow.h"
2930
#import "MaterialShadowElevations.h"
3031
#import "MaterialShadowLayer.h"
3132
#import "MaterialTypography.h"
@@ -72,8 +73,13 @@ @interface MDCBottomNavigationBar () <MDCInkTouchControllerDelegate,
7273
@end
7374

7475
@implementation MDCBottomNavigationBar
76+
77+
static BOOL gEnablePerformantShadow = NO;
78+
7579
@synthesize mdc_overrideBaseElevation = _mdc_overrideBaseElevation;
7680
@synthesize mdc_elevationDidChangeBlock = _mdc_elevationDidChangeBlock;
81+
@synthesize shadowsCollection = _shadowsCollection;
82+
@synthesize elevation = _elevation;
7783

7884
- (instancetype)initWithFrame:(CGRect)frame {
7985
self = [super initWithFrame:frame];
@@ -134,11 +140,8 @@ - (void)commonMDCBottomNavigationBarInit {
134140
[_barView addSubview:_itemsLayoutView];
135141

136142
_itemsLayoutView.accessibilityTraits = UIAccessibilityTraitTabBar;
137-
_elevation = MDCShadowElevationBottomNavigationBar;
138-
[(MDCShadowLayer *)self.layer setElevation:_elevation];
139-
UIColor *defaultShadowColor = UIColor.blackColor;
140-
_shadowColor = defaultShadowColor;
141-
self.layer.shadowColor = defaultShadowColor.CGColor;
143+
self.elevation = MDCShadowElevationBottomNavigationBar;
144+
self.shadowColor = gEnablePerformantShadow ? MDCShadowColor() : UIColor.blackColor;
142145
_itemViews = [NSMutableArray array];
143146
_itemTitleFont = [UIFont mdc_standardFontForMaterialTextStyle:MDCFontTextStyleCaption];
144147

@@ -171,6 +174,10 @@ - (void)layoutSubviews {
171174
[self sizeItemsLayoutViewItemsDistributed:YES withBottomNavSize:size containerWidth:size.width];
172175
}
173176
[self layoutItemViews];
177+
178+
if (gEnablePerformantShadow) {
179+
[self updateShadow];
180+
}
174181
}
175182

176183
- (void)safeAreaInsetsDidChange {
@@ -205,16 +212,31 @@ - (CGSize)sizeThatFits:(CGSize)size {
205212
}
206213

207214
+ (Class)layerClass {
208-
return [MDCShadowLayer class];
215+
if (gEnablePerformantShadow) {
216+
return [super layerClass];
217+
} else {
218+
return [MDCShadowLayer class];
219+
}
209220
}
210221

211222
- (void)setElevation:(MDCShadowElevation)elevation {
212-
BOOL elevationChanged = !MDCCGFloatEqual(_elevation, elevation);
223+
if (MDCCGFloatEqual(_elevation, elevation)) {
224+
return;
225+
}
213226
_elevation = elevation;
214-
[(MDCShadowLayer *)self.layer setElevation:elevation];
215-
if (elevationChanged) {
216-
[self mdc_elevationDidChange];
227+
if (gEnablePerformantShadow) {
228+
[self updateShadow];
229+
} else {
230+
MDCShadowLayer *shadowLayer = (MDCShadowLayer *)self.layer;
231+
shadowLayer.elevation = elevation;
217232
}
233+
[self mdc_elevationDidChange];
234+
}
235+
236+
- (void)updateShadow {
237+
MDCConfigureShadowForView(self,
238+
[self.shadowsCollection shadowForElevation:self.mdc_currentElevation],
239+
self.shadowColor);
218240
}
219241

220242
- (void)setShadowColor:(UIColor *)shadowColor {
@@ -843,6 +865,19 @@ - (CGFloat)mdc_currentElevation {
843865
return self.elevation;
844866
}
845867

868+
- (MDCShadowsCollection *)shadowsCollection {
869+
if (!_shadowsCollection) {
870+
_shadowsCollection = MDCShadowsCollectionDefault();
871+
}
872+
return _shadowsCollection;
873+
}
874+
875+
- (void)setShadowsCollection:(MDCShadowsCollection *)shadowsCollection {
876+
_shadowsCollection = shadowsCollection;
877+
878+
[self updateShadow];
879+
}
880+
846881
- (void)cancelRippleInItemView:(MDCBottomNavigationItemView *)itemView animated:(BOOL)animated {
847882
if (self.enableRippleBehavior) {
848883
if (animated) {
@@ -942,4 +977,14 @@ - (UIPointerStyle *)pointerInteraction:(UIPointerInteraction *)interaction
942977
}
943978
#endif
944979

980+
#pragma mark - Performant Shadow Toggle
981+
982+
+ (void)setEnablePerformantShadow:(BOOL)enable {
983+
gEnablePerformantShadow = enable;
984+
}
985+
986+
+ (BOOL)enablePerformantShadow {
987+
return gEnablePerformantShadow;
988+
}
989+
945990
@end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#import <UIKit/UIKit.h>
2+
3+
#import "MDCBottomNavigationBar.h"
4+
5+
/**
6+
This category is temporary and is meant to allow us to A/B test the performance improvements of
7+
using the new shadow vs the old shadow. When such test is finalized, we will delete this file and
8+
the internal corresponding static property and move all clients to the new shadow.
9+
TODO(b/185206035): Remove category for enabling performant shadow.
10+
*/
11+
@interface MDCBottomNavigationBar (ShadowsPrivate)
12+
13+
/**
14+
Returns a bool indicating if the performant shadow is turned on or not.
15+
*/
16+
+ (BOOL)enablePerformantShadow;
17+
18+
/**
19+
Enables/disables the performant shadow for the bottom navigation bar.
20+
*/
21+
+ (void)setEnablePerformantShadow:(BOOL)enable;
22+
23+
@end

components/BottomNavigation/src/private/MDCBottomNavigationBar+Private.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@
2121

2222
@interface MDCBottomNavigationBar ()
2323

24+
/**
25+
A collection of MDCShadow instances each assigned an elevation (in dp).
26+
27+
To create your own MDCShadowsCollection, please use the provided MDCShadowsCollectionBuilder and
28+
populate it with MDCShadow instances using the provided MDCShadowBuilder.
29+
30+
Defaults to MDCShadowsCollectionDefault().
31+
*/
32+
@property(nonatomic, strong, null_resettable) MDCShadowsCollection *shadowsCollection;
33+
2434
/**
2535
* Returns the tab bar item whose corresponding view contains the given point.
2636
* @param point CGPoint The point at which to get the corresponding tab bar item. The point must be

0 commit comments

Comments
 (0)