From 75ddb21ed0aab3a286663ab99fec79284b159ed5 Mon Sep 17 00:00:00 2001 From: Yarden Eitan Date: Tue, 11 Sep 2018 17:01:58 -0400 Subject: [PATCH] [Shapes] merge MDCShapeCorner and MDCCornerTreatment into one (#5090) This will allow us to set percentage support for a corner treatment as a follow up PR. Reason being is that the MDCCornerTreatment is part of the shape of the component itself, and hence has control to set the value of the corner when it is right in the lifecycle and the frame has been set. If we keep the percentage value setting in the Shape Themer, then the themer could be applied too early in the lifecycle and thus take a percentage off a 0 value (frame height). Overall this simplifies the API and causes less duplication. The other approach would be to *also* add percentage support to MDCCornerTreatment and leave MDCShapeCorner as is, but that would just make it too cumbersome and redundant. --- .../MDCBottomSheetControllerShapeThemer.m | 3 +- .../unit/BottomSheetShapeThemerTests.swift | 8 +- .../src/ShapeThemer/MDCCardsShapeThemer.m | 3 +- .../tests/unit/MDCCardShapeThemerTests.swift | 16 ++-- .../schemes/Shape/src/MDCShapeCategory.h | 21 ++++- .../schemes/Shape/src/MDCShapeCategory.m | 18 +++- components/schemes/Shape/src/MDCShapeCorner.h | 93 ------------------- components/schemes/Shape/src/MDCShapeCorner.m | 70 -------------- .../schemes/Shape/src/MaterialShapeScheme.h | 1 - 9 files changed, 44 insertions(+), 189 deletions(-) delete mode 100644 components/schemes/Shape/src/MDCShapeCorner.h delete mode 100644 components/schemes/Shape/src/MDCShapeCorner.m diff --git a/components/BottomSheet/src/ShapeThemer/MDCBottomSheetControllerShapeThemer.m b/components/BottomSheet/src/ShapeThemer/MDCBottomSheetControllerShapeThemer.m index 369494931f8..29404996fca 100644 --- a/components/BottomSheet/src/ShapeThemer/MDCBottomSheetControllerShapeThemer.m +++ b/components/BottomSheet/src/ShapeThemer/MDCBottomSheetControllerShapeThemer.m @@ -22,8 +22,7 @@ + (void)applyShapeScheme:(id)shapeScheme toBottomSheetController:(MDCBottomSheetController *)bottomSheetController { // Shape Generator for the Extended state of the Bottom Sheet. MDCRectangleShapeGenerator *rectangleShapeExtended = [[MDCRectangleShapeGenerator alloc] init]; - MDCCornerTreatment *cornerTreatmentExtended = - [shapeScheme.largeSurfaceShape.topLeftCorner cornerTreatmentValue]; + MDCCornerTreatment *cornerTreatmentExtended = shapeScheme.largeSurfaceShape.topLeftCorner; [rectangleShapeExtended setCorners:cornerTreatmentExtended]; [bottomSheetController setShapeGenerator:rectangleShapeExtended forState:MDCSheetStateExtended]; diff --git a/components/BottomSheet/tests/unit/BottomSheetShapeThemerTests.swift b/components/BottomSheet/tests/unit/BottomSheetShapeThemerTests.swift index d7147e88e41..095aaa3a1d7 100644 --- a/components/BottomSheet/tests/unit/BottomSheetShapeThemerTests.swift +++ b/components/BottomSheet/tests/unit/BottomSheetShapeThemerTests.swift @@ -33,13 +33,13 @@ class BottomSheetShapeThemerTests: XCTestCase { XCTAssert(extendedShapeGenerator is MDCRectangleShapeGenerator) if let rectangleGenerator = extendedShapeGenerator as? MDCRectangleShapeGenerator { XCTAssertEqual(rectangleGenerator.topLeftCorner, - shapeScheme.largeSurfaceShape.topLeftCorner.cornerTreatmentValue()) + shapeScheme.largeSurfaceShape.topLeftCorner) XCTAssertEqual(rectangleGenerator.topRightCorner, - shapeScheme.largeSurfaceShape.topRightCorner.cornerTreatmentValue()) + shapeScheme.largeSurfaceShape.topRightCorner) XCTAssertEqual(rectangleGenerator.bottomLeftCorner, - shapeScheme.largeSurfaceShape.bottomLeftCorner.cornerTreatmentValue()) + shapeScheme.largeSurfaceShape.bottomLeftCorner) XCTAssertEqual(rectangleGenerator.bottomRightCorner, - shapeScheme.largeSurfaceShape.bottomRightCorner.cornerTreatmentValue()) + shapeScheme.largeSurfaceShape.bottomRightCorner) } } diff --git a/components/Cards/src/ShapeThemer/MDCCardsShapeThemer.m b/components/Cards/src/ShapeThemer/MDCCardsShapeThemer.m index 4e6c91b3fab..ddfb04159ee 100644 --- a/components/Cards/src/ShapeThemer/MDCCardsShapeThemer.m +++ b/components/Cards/src/ShapeThemer/MDCCardsShapeThemer.m @@ -27,8 +27,7 @@ + (void)applyShapeScheme:(id)shapeScheme + (id)cardShapeGeneratorFromScheme:(id)shapeScheme { MDCRectangleShapeGenerator *rectangleShape = [[MDCRectangleShapeGenerator alloc] init]; - MDCCornerTreatment *cornerTreatment = - shapeScheme.mediumSurfaceShape.topLeftCorner.cornerTreatmentValue; + MDCCornerTreatment *cornerTreatment = shapeScheme.mediumSurfaceShape.topLeftCorner; [rectangleShape setCorners:cornerTreatment]; return rectangleShape; } diff --git a/components/Cards/tests/unit/MDCCardShapeThemerTests.swift b/components/Cards/tests/unit/MDCCardShapeThemerTests.swift index af342e0cc62..c494c1f95f3 100644 --- a/components/Cards/tests/unit/MDCCardShapeThemerTests.swift +++ b/components/Cards/tests/unit/MDCCardShapeThemerTests.swift @@ -31,13 +31,13 @@ class CardShapeThemerTests: XCTestCase { // Then XCTAssert(card.shapeGenerator is MDCRectangleShapeGenerator); XCTAssertEqual((card.shapeGenerator as! MDCRectangleShapeGenerator).topLeftCorner, - shapeScheme.mediumSurfaceShape.topLeftCorner.cornerTreatmentValue()) + shapeScheme.mediumSurfaceShape.topLeftCorner) XCTAssertEqual((card.shapeGenerator as! MDCRectangleShapeGenerator).topRightCorner, - shapeScheme.mediumSurfaceShape.topRightCorner.cornerTreatmentValue()) + shapeScheme.mediumSurfaceShape.topRightCorner) XCTAssertEqual((card.shapeGenerator as! MDCRectangleShapeGenerator).bottomLeftCorner, - shapeScheme.mediumSurfaceShape.bottomLeftCorner.cornerTreatmentValue()) + shapeScheme.mediumSurfaceShape.bottomLeftCorner) XCTAssertEqual((card.shapeGenerator as! MDCRectangleShapeGenerator).bottomRightCorner, - shapeScheme.mediumSurfaceShape.bottomRightCorner.cornerTreatmentValue()) + shapeScheme.mediumSurfaceShape.bottomRightCorner) } func testCardCollectionCellShapeThemer() { @@ -53,12 +53,12 @@ class CardShapeThemerTests: XCTestCase { // Then XCTAssert(cardCell.shapeGenerator is MDCRectangleShapeGenerator); XCTAssertEqual((cardCell.shapeGenerator as! MDCRectangleShapeGenerator).topLeftCorner, - shapeScheme.mediumSurfaceShape.topLeftCorner.cornerTreatmentValue()) + shapeScheme.mediumSurfaceShape.topLeftCorner) XCTAssertEqual((cardCell.shapeGenerator as! MDCRectangleShapeGenerator).topRightCorner, - shapeScheme.mediumSurfaceShape.topRightCorner.cornerTreatmentValue()) + shapeScheme.mediumSurfaceShape.topRightCorner) XCTAssertEqual((cardCell.shapeGenerator as! MDCRectangleShapeGenerator).bottomLeftCorner, - shapeScheme.mediumSurfaceShape.bottomLeftCorner.cornerTreatmentValue()) + shapeScheme.mediumSurfaceShape.bottomLeftCorner) XCTAssertEqual((cardCell.shapeGenerator as! MDCRectangleShapeGenerator).bottomRightCorner, - shapeScheme.mediumSurfaceShape.bottomRightCorner.cornerTreatmentValue()) + shapeScheme.mediumSurfaceShape.bottomRightCorner) } } diff --git a/components/schemes/Shape/src/MDCShapeCategory.h b/components/schemes/Shape/src/MDCShapeCategory.h index f47d3e9842a..3e8937c144f 100644 --- a/components/schemes/Shape/src/MDCShapeCategory.h +++ b/components/schemes/Shape/src/MDCShapeCategory.h @@ -13,7 +13,18 @@ // limitations under the License. #import -#import "MDCShapeCorner.h" +#import "MaterialShapes.h" + +/** + This enum consists of the different types of shape corners. + + - MDCShapeCornerFamilyRounded: A rounded corner. + - MDCShapeCornerFamilyAngled: An angled/cut corner. + */ +typedef NS_ENUM(NSInteger, MDCShapeCornerFamily) { + MDCShapeCornerFamilyRounded, + MDCShapeCornerFamilyAngled, +}; /** The MDCShapeCategory is the class containing the shape value as part of our shape scheme, @@ -26,22 +37,22 @@ /** This property represents the shape of the top left corner of the shape. */ -@property(strong, nonatomic) MDCShapeCorner *topLeftCorner; +@property(strong, nonatomic) MDCCornerTreatment *topLeftCorner; /** This property represents the shape of the top right corner of the shape. */ -@property(strong, nonatomic) MDCShapeCorner *topRightCorner; +@property(strong, nonatomic) MDCCornerTreatment *topRightCorner; /** This property represents the shape of the bottom left corner of the shape. */ -@property(strong, nonatomic) MDCShapeCorner *bottomLeftCorner; +@property(strong, nonatomic) MDCCornerTreatment *bottomLeftCorner; /** This property represents the shape of the bottom right corner of the shape. */ -@property(strong, nonatomic) MDCShapeCorner *bottomRightCorner; +@property(strong, nonatomic) MDCCornerTreatment *bottomRightCorner; /** The default init of the class. It sets all 4 corners with a corner family of diff --git a/components/schemes/Shape/src/MDCShapeCategory.m b/components/schemes/Shape/src/MDCShapeCategory.m index e2bfe71f94e..5c02277de84 100644 --- a/components/schemes/Shape/src/MDCShapeCategory.m +++ b/components/schemes/Shape/src/MDCShapeCategory.m @@ -13,6 +13,7 @@ // limitations under the License. #import "MDCShapeCategory.h" +#import "MaterialShapeLibrary.h" @implementation MDCShapeCategory @@ -23,10 +24,19 @@ - (instancetype)init { - (instancetype)initCornersWithFamily:(MDCShapeCornerFamily)cornerFamily andSize:(CGFloat)cornerSize { if (self = [super init]) { - _topLeftCorner = [[MDCShapeCorner alloc] initWithFamily:cornerFamily andSize:cornerSize]; - _topRightCorner = [[MDCShapeCorner alloc] initWithFamily:cornerFamily andSize:cornerSize]; - _bottomLeftCorner = [[MDCShapeCorner alloc] initWithFamily:cornerFamily andSize:cornerSize]; - _bottomRightCorner = [[MDCShapeCorner alloc] initWithFamily:cornerFamily andSize:cornerSize]; + MDCCornerTreatment *cornerTreatment; + switch (cornerFamily) { + case MDCShapeCornerFamilyAngled: + cornerTreatment = [MDCCornerTreatment cornerWithCut:cornerSize]; + break; + case MDCShapeCornerFamilyRounded: + cornerTreatment = [MDCCornerTreatment cornerWithRadius:cornerSize]; + break; + } + _topLeftCorner = cornerTreatment; + _topRightCorner = cornerTreatment; + _bottomLeftCorner = cornerTreatment; + _bottomRightCorner = cornerTreatment; } return self; } diff --git a/components/schemes/Shape/src/MDCShapeCorner.h b/components/schemes/Shape/src/MDCShapeCorner.h deleted file mode 100644 index f00fcaea9bd..00000000000 --- a/components/schemes/Shape/src/MDCShapeCorner.h +++ /dev/null @@ -1,93 +0,0 @@ -// Copyright 2018-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 -#import "MaterialShapeLibrary.h" - -/** - This enum consists of the different types of shape corners. - - - MDCShapeCornerFamilyRounded: A rounded corner. - - MDCShapeCornerFamilyAngled: An angled/cut corner. - */ -typedef NS_ENUM(NSInteger, MDCShapeCornerFamily) { - MDCShapeCornerFamilyRounded, - MDCShapeCornerFamilyAngled, -}; - -/** - This enum consists of the different types of shape values that can be provided. - - - MDCShapeCornerSizeTypeAbsolute: If an absolute shape size is provided. - - MDCShapeCornerSizeTypePercentage: If a relative shape size is provided. - - See MDCShapeCorner's @c size property for additional details. - */ -typedef NS_ENUM(NSInteger, MDCShapeCornerSizeType) { - MDCShapeCornerSizeTypeAbsolute, - MDCShapeCornerSizeTypePercentage, -}; - -/** - An MDCShapeCorner is the shape value of a corner. It takes a family, a size type, and a size. - */ -@interface MDCShapeCorner : NSObject - -/** - The shape family of our corner. - */ -@property(assign, nonatomic) MDCShapeCornerFamily family; - -/** - The size type of our shape. - */ -@property(assign, nonatomic) MDCShapeCornerSizeType sizeType; - -/** - The size of our shape. - - When MDCShapeSizeType is MDCShapeCornerSizeTypeAbsolute, this accepts absolute values - that are positive. - - When MDCShapeSizeType is MDCShapeCornerSizeTypePercentage, values are expected to be in the range - of 0 to 1 (0% - 100%). These values are percentages based on the height of the surface. - */ -@property(assign, nonatomic) CGFloat size; - -/** - This initializer takes in a shape family and size, and returns a shape corner. - - @param cornerFamily The shape family. - @param cornerSize The shape size. - @return an MDCShapeCorner. - */ -- (instancetype)initWithFamily:(MDCShapeCornerFamily)cornerFamily andSize:(CGFloat)cornerSize; - -/** - This method returns an MDCCornerTreament representation of the MDCShapeCorner. - - @return an MDCCornerTreatment. - */ -- (MDCCornerTreatment *)cornerTreatmentValue; - -/** - This method returns an MDCCornerTreatment representation of the MDCShapeCorner. - It receives the bounds of the shape when a percentage or relative value is given. - - @param bounds The bounds of the shape. - @return an MDCCornerTreatment. - */ -- (MDCCornerTreatment *)cornerTreatmentValueWithViewBounds:(CGRect)bounds; - -@end diff --git a/components/schemes/Shape/src/MDCShapeCorner.m b/components/schemes/Shape/src/MDCShapeCorner.m deleted file mode 100644 index 51514ceb836..00000000000 --- a/components/schemes/Shape/src/MDCShapeCorner.m +++ /dev/null @@ -1,70 +0,0 @@ -// Copyright 2018-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 "MDCShapeCorner.h" - -@implementation MDCShapeCorner - -- (instancetype)init { - _sizeType = MDCShapeCornerSizeTypeAbsolute; - return [self initWithFamily:MDCShapeCornerFamilyRounded andSize:0]; -} - -- (instancetype)initWithFamily:(MDCShapeCornerFamily)cornerFamily andSize:(CGFloat)cornerSize { - if (self = [super init]) { - _sizeType = MDCShapeCornerSizeTypeAbsolute; - _family = cornerFamily; - _size = cornerSize; - } - return self; -} - -- (instancetype)initWithFamily:(MDCShapeCornerFamily)cornerFamily - andPercentageSize:(CGFloat)cornerSize { - if (self = [super init]) { - _sizeType = MDCShapeCornerSizeTypePercentage; - _family = cornerFamily; - _size = cornerSize; - } - return self; -} - -- (MDCCornerTreatment *)cornerTreatmentValue { - return [self cornerTreatmentSizeWithNormalizedShapeSize:_size]; -} - -- (MDCCornerTreatment *)cornerTreatmentValueWithViewBounds:(CGRect)bounds { - MDCCornerTreatment *cornerTreatment; - if (_sizeType == MDCShapeCornerSizeTypePercentage) { - CGFloat normalizedShapeSize = bounds.size.height * _size; - cornerTreatment = [self cornerTreatmentSizeWithNormalizedShapeSize:normalizedShapeSize]; - } else { - cornerTreatment = [self cornerTreatmentValue]; - } - return cornerTreatment; -} - -- (MDCCornerTreatment *)cornerTreatmentSizeWithNormalizedShapeSize:(CGFloat)shapeSize { - MDCCornerTreatment *cornerTreatment; - switch (_family) { - case MDCShapeCornerFamilyAngled: - cornerTreatment = [MDCCornerTreatment cornerWithCut:shapeSize]; - break; - case MDCShapeCornerFamilyRounded: - cornerTreatment = [MDCCornerTreatment cornerWithRadius:shapeSize]; - break; - } - return cornerTreatment; -} -@end diff --git a/components/schemes/Shape/src/MaterialShapeScheme.h b/components/schemes/Shape/src/MaterialShapeScheme.h index dbbc41135c1..a1e1924a3ba 100644 --- a/components/schemes/Shape/src/MaterialShapeScheme.h +++ b/components/schemes/Shape/src/MaterialShapeScheme.h @@ -13,5 +13,4 @@ // limitations under the License. #import "MDCShapeCategory.h" -#import "MDCShapeCorner.h" #import "MDCShapeScheme.h"