Skip to content

Commit

Permalink
[private/ThumbView] Add centerVisibleArea support with snapshot tests.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 323572514
  • Loading branch information
attributeshift authored and material-automation committed Jul 28, 2020
1 parent 8410fe2 commit a142aae
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 1 deletion.
10 changes: 10 additions & 0 deletions components/private/ThumbTrack/src/MDCThumbView.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@
/** The corner radius of the thumbview layer. */
@property(nonatomic, assign) CGFloat cornerRadius;

/**
A Boolean value that determines whether the visible area is centered in the bounds of the view.
If set to YES, the visible area is centered in the bounds of the view, which is often used to
configure invisible tappable area. If set to NO, the visible area fills its bounds.
The default value is @c NO.
*/
@property(nonatomic, assign) BOOL centerVisibleArea;

/** Set the @c icon shown on the thumb. */
- (void)setIcon:(nullable UIImage *)icon;

Expand Down
41 changes: 40 additions & 1 deletion components/private/ThumbTrack/src/MDCThumbView.m
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,50 @@ - (void)setCornerRadius:(CGFloat)cornerRadius {
}
_cornerRadius = cornerRadius;

[self configureShapeGeneratorWithCornerRadius:cornerRadius
centerVisibleArea:self.centerVisibleArea];

[self setNeedsLayout];
}

- (void)setCenterVisibleArea:(BOOL)centerVisibleArea {
if (centerVisibleArea == _centerVisibleArea) {
return;
}
_centerVisibleArea = centerVisibleArea;

[self configureShapeGeneratorWithCornerRadius:self.cornerRadius
centerVisibleArea:centerVisibleArea];

[self setNeedsLayout];
}

- (void)configureShapeGeneratorWithCornerRadius:(CGFloat)cornerRadius
centerVisibleArea:(BOOL)centerVisibleArea {
MDCCornerTreatment *cornerTreatment =
[[MDCRoundedCornerTreatment alloc] initWithRadius:cornerRadius];
[self.shapeGenerator setCorners:cornerTreatment];

[self setNeedsLayout];
if (centerVisibleArea) {
UIEdgeInsets visibleAreaInsets = UIEdgeInsetsZero;
CGSize visibleAreaSize = CGSizeMake(cornerRadius * 2, cornerRadius * 2);
CGFloat additionalRequiredHeight =
MAX(0, CGRectGetHeight(self.bounds) - visibleAreaSize.height);
CGFloat additionalRequiredWidth = MAX(0, CGRectGetWidth(self.bounds) - visibleAreaSize.width);
visibleAreaInsets.top = MDCCeil(additionalRequiredHeight * 0.5f);
visibleAreaInsets.bottom = additionalRequiredHeight - visibleAreaInsets.top;
visibleAreaInsets.left = MDCCeil(additionalRequiredWidth * 0.5f);
visibleAreaInsets.right = additionalRequiredWidth - visibleAreaInsets.left;

self.shapeGenerator.topLeftCornerOffset =
CGPointMake(visibleAreaInsets.left, visibleAreaInsets.top);
self.shapeGenerator.topRightCornerOffset =
CGPointMake(-visibleAreaInsets.right, visibleAreaInsets.top);
self.shapeGenerator.bottomLeftCornerOffset =
CGPointMake(visibleAreaInsets.left, -visibleAreaInsets.bottom);
self.shapeGenerator.bottomRightCornerOffset =
CGPointMake(-visibleAreaInsets.right, -visibleAreaInsets.bottom);
}
}

- (MDCShadowElevation)elevation {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright 2020-present the Material Components for iOS authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#import <UIKit/UIKit.h>

#import "MaterialSnapshot.h"
#import "MaterialThumbTrack.h"

@interface MDCThumbViewSnapshotTests : MDCSnapshotTestCase

@property(nonatomic, strong) MDCThumbView *thumbView;

@end

@implementation MDCThumbViewSnapshotTests

- (void)setUp {
[super setUp];

// Uncomment below to recreate all the goldens (or add the following line to the specific
// test you wish to recreate the golden for).
// self.recordMode = YES;

self.thumbView = [[MDCThumbView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
self.thumbView.backgroundColor = UIColor.blueColor;
}

- (void)tearDown {
self.thumbView = nil;

[super tearDown];
}

- (void)generateSnapshotWithBorderAndVerifyForView:(MDCThumbView *)view {
// TODO(b/161927075): Refactor when this functionality is available in MaterialSnapshot.
UIView *thumbViewFrameView = [[UIView alloc] initWithFrame:view.bounds];
[view addSubview:thumbViewFrameView];
thumbViewFrameView.layer.borderColor = UIColor.yellowColor.CGColor;
thumbViewFrameView.layer.borderWidth = 0.5f;

UIView *snapshotView = [view mdc_addToBackgroundView];
[self snapshotVerifyView:snapshotView];

[thumbViewFrameView removeFromSuperview];
}

#pragma mark - Tests

- (void)testThumbViewFullyRoundedDefaults {
// When
self.thumbView.cornerRadius = CGRectGetWidth(self.thumbView.bounds) / 2;

// Then
[self generateSnapshotWithBorderAndVerifyForView:self.thumbView];
}

- (void)testThumbViewWhenCenterVisibleAreaIsTrue {
// When
self.thumbView.centerVisibleArea = YES;
self.thumbView.cornerRadius = 10;

// Then
[self generateSnapshotWithBorderAndVerifyForView:self.thumbView];
}

@end

0 comments on commit a142aae

Please sign in to comment.