Skip to content

Commit

Permalink
[Tabs] Remove internal layout type. (material-components#8519)
Browse files Browse the repository at this point in the history
Refactors the MDCTabBarView to remove an internal layout style type that
mirrored the external one. To support an upcoming API that allows per-style
customization, the internal type is no longer useful.

Part of #8490
  • Loading branch information
Robert Moore authored and randallli committed Sep 27, 2019
1 parent 0ecae71 commit f9560f6
Showing 1 changed file with 40 additions and 69 deletions.
109 changes: 40 additions & 69 deletions components/Tabs/src/TabBarView/MDCTabBarView.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,39 +54,6 @@
static NSString *const kAccessibilityIdentifierKeyPath = @"accessibilityIdentifier";
static NSString *const kAccessibilityTraitsKeyPath = @"accessibilityTraits";

/** The internal layout styles of the tab bar. */
typedef NS_ENUM(NSUInteger, MDCTabBarViewInternalLayoutStyle) {

/** Items are laid-out from leading to trailing and sized to their intrinsic content size. */
MDCTabBarViewInternalLayoutStyleScrollable = 0,

/**
Items are equally-sized where each of the items receives an equal portion of the bar's width.
For example, if there are 3 items and the bar is 360 points wide, each item receives 120 points
in width.
*/
MDCTabBarViewInternalLayoutStyleFixedJustified = 1,

/**
Items are equally-sized where each of the items is the width of the widest item. The items are
positioned at the center of the bar. The bar is not scrollable.
*/
MDCTabBarViewInternalLayoutStyleFixedClusteredCentered = 2,

/**
Items are equally-sized where each of the items is the width of the widest item. The items are
positioned at the leading edge of the bar. The bar is not scrollable.
*/
MDCTabBarViewInternalLayoutStyleFixedClusteredLeading = 3,

/**
Items are equally-sized where each of the items is the width of the widest item. The items are
positioned at the trailing edge of the bar. The bar is not scrollable.
*/
MDCTabBarViewInternalLayoutStyleFixedClusteredTrailing = 4,

};

@interface MDCTabBarView ()

/** The views representing each tab bar item. */
Expand Down Expand Up @@ -596,18 +563,18 @@ - (void)markIntrinsicContentSizeAndLayoutNeedingUpdateForSelfAndItemView:(UIView
- (void)layoutSubviews {
[super layoutSubviews];

switch ([self layoutStyle]) {
case MDCTabBarViewInternalLayoutStyleFixedJustified: {
switch ([self effectiveLayoutStyle]) {
case MDCTabBarViewLayoutStyleFixed: {
[self layoutSubviewsForJustifiedLayout];
break;
}
case MDCTabBarViewInternalLayoutStyleFixedClusteredCentered:
case MDCTabBarViewInternalLayoutStyleFixedClusteredTrailing:
case MDCTabBarViewInternalLayoutStyleFixedClusteredLeading: {
[self layoutSubviewsForFixedClusteredLayout:[self layoutStyle]];
case MDCTabBarViewLayoutStyleFixedClusteredCentered:
case MDCTabBarViewLayoutStyleFixedClusteredTrailing:
case MDCTabBarViewLayoutStyleFixedClusteredLeading: {
[self layoutSubviewsForFixedClusteredLayout:[self effectiveLayoutStyle]];
break;
}
case MDCTabBarViewInternalLayoutStyleScrollable: {
case MDCTabBarViewLayoutStyleScrollable: {
[self layoutSubviewsForScrollableLayout];
break;
}
Expand Down Expand Up @@ -642,47 +609,51 @@ - (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
}

- (BOOL)isScrollableLayoutStyle {
return [self layoutStyle] == MDCTabBarViewInternalLayoutStyleScrollable;
return [self effectiveLayoutStyle] == MDCTabBarViewLayoutStyleScrollable;
}

- (MDCTabBarViewInternalLayoutStyle)layoutStyle {
/**
The current layout style of the Tab Bar. Although the user sets a preferred layout style, not all
combinations of items, bounds, and style can be rendered correctly.
*/
- (MDCTabBarViewLayoutStyle)effectiveLayoutStyle {
if (self.items.count == 0) {
return MDCTabBarViewInternalLayoutStyleFixedJustified;
return MDCTabBarViewLayoutStyleFixed;
}

CGSize availableSize = [self availableSizeForSubviewLayout];
CGFloat requiredWidthForJustifiedLayout = [self intrinsicContentSizeForJustifiedLayout].width;
CGFloat requiredWidthForClusteredLayout = [self intrinsicContentSizeForClusteredLayout].width;
switch (self.preferredLayoutStyle) {
case MDCTabBarViewLayoutStyleScrollable: {
return MDCTabBarViewInternalLayoutStyleScrollable;
return MDCTabBarViewLayoutStyleScrollable;
}
case MDCTabBarViewLayoutStyleFixed: {
if (availableSize.width < requiredWidthForJustifiedLayout) {
return MDCTabBarViewInternalLayoutStyleScrollable;
return MDCTabBarViewLayoutStyleScrollable;
}
if ((availableSize.width / self.items.count) > kMaxItemWidth) {
return MDCTabBarViewInternalLayoutStyleFixedClusteredCentered;
return MDCTabBarViewLayoutStyleFixedClusteredCentered;
}
return MDCTabBarViewInternalLayoutStyleFixedJustified;
return MDCTabBarViewLayoutStyleFixed;
}
case MDCTabBarViewLayoutStyleFixedClusteredCentered: {
if (availableSize.width < requiredWidthForClusteredLayout) {
return MDCTabBarViewInternalLayoutStyleScrollable;
return MDCTabBarViewLayoutStyleScrollable;
}
return MDCTabBarViewInternalLayoutStyleFixedClusteredCentered;
return MDCTabBarViewLayoutStyleFixedClusteredCentered;
}
case MDCTabBarViewLayoutStyleFixedClusteredLeading: {
if (availableSize.width < requiredWidthForClusteredLayout) {
return MDCTabBarViewInternalLayoutStyleScrollable;
return MDCTabBarViewLayoutStyleScrollable;
}
return MDCTabBarViewInternalLayoutStyleFixedClusteredLeading;
return MDCTabBarViewLayoutStyleFixedClusteredLeading;
}
case MDCTabBarViewLayoutStyleFixedClusteredTrailing: {
if (availableSize.width < requiredWidthForClusteredLayout) {
return MDCTabBarViewInternalLayoutStyleScrollable;
return MDCTabBarViewLayoutStyleScrollable;
}
return MDCTabBarViewInternalLayoutStyleFixedClusteredTrailing;
return MDCTabBarViewLayoutStyleFixedClusteredTrailing;
}
}
}
Expand All @@ -708,7 +679,7 @@ - (void)layoutSubviewsForJustifiedLayout {
}
}

- (void)layoutSubviewsForFixedClusteredLayout:(MDCTabBarViewInternalLayoutStyle)layoutStyle {
- (void)layoutSubviewsForFixedClusteredLayout:(MDCTabBarViewLayoutStyle)layoutStyle {
if (self.itemViews.count == 0) {
return;
}
Expand All @@ -722,13 +693,13 @@ - (void)layoutSubviewsForFixedClusteredLayout:(MDCTabBarViewInternalLayoutStyle)
// Start-out assuming left-aligned because it requires no computation.
CGFloat itemViewOriginX = 0;
// Right-aligned
if ((isRTL && layoutStyle == MDCTabBarViewInternalLayoutStyleFixedClusteredLeading) ||
(!isRTL && layoutStyle == MDCTabBarViewInternalLayoutStyleFixedClusteredTrailing)) {
if ((isRTL && layoutStyle == MDCTabBarViewLayoutStyleFixedClusteredLeading) ||
(!isRTL && layoutStyle == MDCTabBarViewLayoutStyleFixedClusteredTrailing)) {
itemViewOriginX = CGRectGetMinX([self availableBoundsForSubviewLayout]) +
(contentSize.width - totalRequiredWidth);
}
// Centered
else if (layoutStyle == MDCTabBarViewInternalLayoutStyleFixedClusteredCentered) {
else if (layoutStyle == MDCTabBarViewLayoutStyleFixedClusteredCentered) {
itemViewOriginX = (contentSize.width - totalRequiredWidth) / 2;
}

Expand Down Expand Up @@ -790,16 +761,16 @@ - (CGSize)intrinsicContentSize {
}

- (CGSize)calculatedContentSize {
switch ([self layoutStyle]) {
case MDCTabBarViewInternalLayoutStyleFixedJustified: {
switch ([self effectiveLayoutStyle]) {
case MDCTabBarViewLayoutStyleFixed: {
return [self intrinsicContentSizeForJustifiedLayout];
}
case MDCTabBarViewInternalLayoutStyleFixedClusteredCentered:
case MDCTabBarViewInternalLayoutStyleFixedClusteredTrailing:
case MDCTabBarViewInternalLayoutStyleFixedClusteredLeading: {
case MDCTabBarViewLayoutStyleFixedClusteredCentered:
case MDCTabBarViewLayoutStyleFixedClusteredTrailing:
case MDCTabBarViewLayoutStyleFixedClusteredLeading: {
return [self intrinsicContentSizeForClusteredLayout];
}
case MDCTabBarViewInternalLayoutStyleScrollable: {
case MDCTabBarViewLayoutStyleScrollable: {
return [self intrinsicContentSizeForScrollableLayout];
}
}
Expand Down Expand Up @@ -887,20 +858,20 @@ - (CGSize)expectedSizeForView:(UIView *)view {
return CGSizeZero;
}

switch ([self layoutStyle]) {
case MDCTabBarViewInternalLayoutStyleFixedJustified: {
switch ([self effectiveLayoutStyle]) {
case MDCTabBarViewLayoutStyleFixed: {
if (CGRectGetWidth(self.bounds) > 0) {
CGSize contentSize = [self availableSizeForSubviewLayout];
return CGSizeMake(contentSize.width / self.itemViews.count, contentSize.height);
}
return [self estimatedIntrinsicSizeForView:view];
}
case MDCTabBarViewInternalLayoutStyleFixedClusteredCentered:
case MDCTabBarViewInternalLayoutStyleFixedClusteredTrailing:
case MDCTabBarViewInternalLayoutStyleFixedClusteredLeading: {
case MDCTabBarViewLayoutStyleFixedClusteredCentered:
case MDCTabBarViewLayoutStyleFixedClusteredTrailing:
case MDCTabBarViewLayoutStyleFixedClusteredLeading: {
return [self estimatedItemViewSizeForClusteredFixedLayout];
}
case MDCTabBarViewInternalLayoutStyleScrollable: {
case MDCTabBarViewLayoutStyleScrollable: {
return [self estimatedIntrinsicSizeForView:view];
}
}
Expand Down

0 comments on commit f9560f6

Please sign in to comment.