Skip to content

Commit

Permalink
[Floating Button] Add centerVisibleArea support for the FAB and depre…
Browse files Browse the repository at this point in the history
…cate visibleAreaMode

API.

PiperOrigin-RevId: 320291504
  • Loading branch information
attributeshift authored and material-automation committed Jul 8, 2020
1 parent 2207b7a commit 222ae37
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 2 deletions.
16 changes: 15 additions & 1 deletion components/Buttons/src/MDCFloatingButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,18 @@ typedef NS_ENUM(NSInteger, MDCFloatingButtonImageLocation) {
forShape:(MDCFloatingButtonShape)shape
inMode:(MDCFloatingButtonMode)mode UI_APPEARANCE_SELECTOR;

/**
Sets the @c centerVisibleArea value when the button has the specified @c shape and @c mode.
@param centerVisibleArea The boolean value that determines whether the visible area is centered in
the bounds of the view.
@param shape The floating action button's shape (Default, Mini).
@param mode The floating action button's mode (Normal, Expanded).
*/
- (void)setCenterVisibleArea:(BOOL)centerVisibleArea
forShape:(MDCFloatingButtonShape)shape
inMode:(MDCFloatingButtonMode)mode;

/**
Sets the @c visibleAreaInsets value when the button has the specified @c shape and @c mode.
Expand All @@ -246,6 +258,8 @@ typedef NS_ENUM(NSInteger, MDCFloatingButtonImageLocation) {
*/
- (void)setVisibleAreaInsets:(UIEdgeInsets)visibleAreaInsets
forShape:(MDCFloatingButtonShape)shape
inMode:(MDCFloatingButtonMode)mode;
inMode:(MDCFloatingButtonMode)mode
__attribute__((deprecated(
"Consider using setCenterVisibleArea:forShape:InMode: to adjust visible area.")));

@end
37 changes: 36 additions & 1 deletion components/Buttons/src/MDCFloatingButton.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ @interface MDCFloatingButton () <MDCFloatingButtonModeAnimatorDelegate>
NSMutableDictionary<NSNumber *, NSMutableDictionary<NSNumber *, NSValue *> *>
*shapeToModeToHitAreaInsets;

@property(nonatomic, readonly)
NSMutableDictionary<NSNumber *, NSMutableDictionary<NSNumber *, NSNumber *> *>
*shapeToModeToCenterVisibleArea;

@property(nonatomic, readonly)
NSMutableDictionary<NSNumber *, NSMutableDictionary<NSNumber *, NSValue *> *>
*shapeToModeToVisibleAreaInsets;
Expand Down Expand Up @@ -180,6 +184,8 @@ - (void)commonMDCFloatingButtonInit {
} mutableCopy];

_shapeToModeToVisibleAreaInsets = [[NSMutableDictionary alloc] init];

_shapeToModeToCenterVisibleArea = [[NSMutableDictionary alloc] init];
}

#pragma mark - UIView
Expand Down Expand Up @@ -239,7 +245,8 @@ - (CGSize)intrinsicContentSize {
contentSize.width = MIN(self.maximumSize.width, contentSize.width);
}

CGSize adjustedSize = CGSizeExpandWithInsets(contentSize, self.visibleAreaInsets);
UIEdgeInsets visibleAreaInsets = [self visibleAreaInsetsForMode:self.mode];
CGSize adjustedSize = CGSizeExpandWithInsets(contentSize, visibleAreaInsets);
return adjustedSize;
}

Expand Down Expand Up @@ -529,6 +536,33 @@ - (void)updateHitAreaInsets {
super.hitAreaInsets = [self hitAreaInsetsForMode:self.mode];
}

- (void)setCenterVisibleArea:(BOOL)centerVisibleArea
forShape:(MDCFloatingButtonShape)shape
inMode:(MDCFloatingButtonMode)mode {
NSMutableDictionary *modeToCenterVisibleArea = self.shapeToModeToCenterVisibleArea[@(shape)];
if (!modeToCenterVisibleArea) {
modeToCenterVisibleArea = [@{} mutableCopy];
self.shapeToModeToCenterVisibleArea[@(shape)] = modeToCenterVisibleArea;
}
modeToCenterVisibleArea[@(mode)] = @(centerVisibleArea);
if (shape == _shape && mode == self.mode) {
[self updateShapeAndAllowResize:NO];
}
}

- (BOOL)centerVisibleAreaForMode:(MDCFloatingButtonMode)mode {
NSMutableDictionary *modeToCenterVisibleArea = self.shapeToModeToCenterVisibleArea[@(_shape)];
if (!modeToCenterVisibleArea) {
return NO;
}

return [modeToCenterVisibleArea[@(mode)] boolValue];
}

- (void)updateCenterVisibleArea {
super.centerVisibleArea = [self centerVisibleAreaForMode:self.mode];
}

- (void)setVisibleAreaInsets:(UIEdgeInsets)insets
forShape:(MDCFloatingButtonShape)shape
inMode:(MDCFloatingButtonMode)mode {
Expand Down Expand Up @@ -566,6 +600,7 @@ - (void)updateShapeAndAllowResize:(BOOL)allowsResize {
BOOL maximumSizeChanged = [self updateMaximumSize];
[self updateContentEdgeInsets];
[self updateHitAreaInsets];
[self updateCenterVisibleArea];
[self updateVisibleAreaInsets];

if (allowsResize && (minimumSizeChanged || maximumSizeChanged)) {
Expand Down
66 changes: 66 additions & 0 deletions components/Buttons/tests/unit/MDCFloatingButtonTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -848,4 +848,70 @@ - (void)testChangingVisibleAreaInsetsWontTriggerResizing {
XCTAssertFalse(button.layoutSubviewsCalled);
}

#pragma mark - setCenterVisibleArea:forShape:inMode:

- (void)testDefaultCenterVisibleAreaValues {
// Given
MDCFloatingButton *defaultButtonNormal =
[[MDCFloatingButton alloc] initWithFrame:CGRectZero shape:MDCFloatingButtonShapeDefault];
MDCFloatingButton *defaultButtonExpanded =
[[MDCFloatingButton alloc] initWithFrame:CGRectZero shape:MDCFloatingButtonShapeDefault];
defaultButtonExpanded.mode = MDCFloatingButtonModeExpanded;
MDCFloatingButton *miniButtonNormal =
[[MDCFloatingButton alloc] initWithFrame:CGRectZero shape:MDCFloatingButtonShapeMini];
MDCFloatingButton *miniButtonExpanded =
[[MDCFloatingButton alloc] initWithFrame:CGRectZero shape:MDCFloatingButtonShapeMini];
miniButtonExpanded.mode = MDCFloatingButtonModeExpanded;

// Then
XCTAssertFalse(defaultButtonNormal.centerVisibleArea);
XCTAssertFalse(defaultButtonExpanded.centerVisibleArea);
XCTAssertFalse(miniButtonNormal.centerVisibleArea);
XCTAssertFalse(miniButtonExpanded.centerVisibleArea);
}

- (void)testSetCenterVisibleAreaForShapeInNormalMode {
// Given
MDCFloatingButton *defaultButton = [[MDCFloatingButton alloc] init]; // Default shape
defaultButton.mode = MDCFloatingButtonModeExpanded;
MDCFloatingButton *miniButton =
[[MDCFloatingButton alloc] initWithFrame:CGRectZero shape:MDCFloatingButtonShapeMini];
miniButton.mode = MDCFloatingButtonModeExpanded;

// When
[defaultButton setCenterVisibleArea:YES
forShape:MDCFloatingButtonShapeDefault
inMode:MDCFloatingButtonModeNormal];
defaultButton.mode = MDCFloatingButtonModeNormal;
[miniButton setCenterVisibleArea:YES
forShape:MDCFloatingButtonShapeMini
inMode:MDCFloatingButtonModeNormal];
miniButton.mode = MDCFloatingButtonModeNormal;

// Then
XCTAssertTrue(defaultButton.centerVisibleArea);
XCTAssertTrue(miniButton.centerVisibleArea);
}

- (void)testSetCenterVisibleAreaForShapeInExpandedMode {
// Given
MDCFloatingButton *defaultButton = [[MDCFloatingButton alloc] init]; // Default shape
MDCFloatingButton *miniButton =
[[MDCFloatingButton alloc] initWithFrame:CGRectZero shape:MDCFloatingButtonShapeMini];

// When
[defaultButton setCenterVisibleArea:YES
forShape:MDCFloatingButtonShapeDefault
inMode:MDCFloatingButtonModeExpanded];
defaultButton.mode = MDCFloatingButtonModeExpanded;
[miniButton setCenterVisibleArea:YES
forShape:MDCFloatingButtonShapeMini
inMode:MDCFloatingButtonModeExpanded];
miniButton.mode = MDCFloatingButtonModeExpanded;

// Then
XCTAssertTrue(defaultButton.centerVisibleArea);
XCTAssertTrue(miniButton.centerVisibleArea);
}

@end

0 comments on commit 222ae37

Please sign in to comment.