Skip to content

Commit

Permalink
[MDC/Button] Add rippleEdgeInsets API.
Browse files Browse the repository at this point in the history
Currently we make the ripple the entire size of the button. This limits clients being able to customize if the ripple is inset or outset on a particular side of the button. Since MDCFloatingButton sets its contentEdgeInsets this API is needed to allow for the two values to be independent.

https://developer.apple.com/documentation/uikit/uibutton/1624036-contentedgeinsets

PiperOrigin-RevId: 322199718
  • Loading branch information
codeman7 authored and material-automation committed Jul 20, 2020
1 parent 0a0f734 commit 3301259
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
5 changes: 5 additions & 0 deletions components/Buttons/src/MDCButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@
*/
@property(nonatomic) CGSize inkViewOffset;

/**
The inset or outset margins for the rectangle surrounding the button’s ripple.
*/
@property(nonatomic, assign) UIEdgeInsets rippleEdgeInsets;

/**
The minimum size of the button’s alignment rect. If either the height or width are non-positive
(negative or zero), they will be ignored and that axis will adjust to its content size.
Expand Down
7 changes: 7 additions & 0 deletions components/Buttons/src/MDCButton.m
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ - (void)layoutSubviews {
} else {
CGRect bounds = CGRectStandardize(self.bounds);
bounds = CGRectOffset(bounds, self.inkViewOffset.width, self.inkViewOffset.height);
bounds = UIEdgeInsetsInsetRect(bounds, self.rippleEdgeInsets);
_inkView.frame = bounds;
self.rippleView.frame = bounds;
}
Expand Down Expand Up @@ -641,6 +642,12 @@ - (void)setInkViewOffset:(CGSize)inkViewOffset {
[self setNeedsLayout];
}

- (void)setRippleEdgeInsets:(UIEdgeInsets)rippleEdgeInsets {
_rippleEdgeInsets = rippleEdgeInsets;

[self setNeedsLayout];
}

- (void)setEnableRippleBehavior:(BOOL)enableRippleBehavior {
_enableRippleBehavior = enableRippleBehavior;

Expand Down
10 changes: 10 additions & 0 deletions components/Buttons/tests/snapshot/ButtonsRippleSnapshotTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,14 @@ - (void)testButtonWhenRippleTouchDownAtPointIsCalledGeneratesCorrectImage {
[self generateSnapshotAndVerifyForView:self.button];
}

- (void)testButtonRippleWhenContentEdgeInsetsAreSet {
// When
self.button.rippleEdgeInsets = UIEdgeInsetsMake(1, 2, 3, 4);
self.button.rippleView.backgroundColor = UIColor.purpleColor;
[self.button layoutIfNeeded];

// Then
[self generateSnapshotAndVerifyForView:self.button];
}

@end
19 changes: 19 additions & 0 deletions components/Buttons/tests/unit/MDCButtonRippleTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,23 @@ - (void)testSettingSelectedUpdatesRippleTheming {
XCTAssertFalse(self.button.rippleView.isSelected);
}

- (void)testSettingContentEdgeInsetsUpdatesRipple {
// Given
self.button.enableRippleBehavior = YES;
CGRect buttonFrame = CGRectMake(0, 0, 100, 100);
self.button.frame = buttonFrame;
UIEdgeInsets buttonRippleInsets = UIEdgeInsetsMake(1, 2, 3, 4);

// When
self.button.rippleEdgeInsets = buttonRippleInsets;
[self.button layoutIfNeeded];

// Then
XCTAssertTrue(CGRectEqualToRect(UIEdgeInsetsInsetRect(buttonFrame, buttonRippleInsets),
self.button.rippleView.frame),
@"%@ is not equal to %@",
NSStringFromCGRect(UIEdgeInsetsInsetRect(buttonFrame, buttonRippleInsets)),
NSStringFromCGRect(self.button.rippleView.frame));
}

@end

0 comments on commit 3301259

Please sign in to comment.