Skip to content

Commit

Permalink
[Shapes] Initial Shape Scheme implementation (#5014)
Browse files Browse the repository at this point in the history
This PR implements the initial Shape Scheme that is essential for allowing shape theming for components (*this doesn't include any themers and that will be included as a separate PR once this is approved*). More information can be seen in go/mdc-ios-shape-theming and go/material-shapes-eng

This closes #4609 #4612 #4613
  • Loading branch information
yarneo committed Sep 6, 2018
1 parent 8078a2c commit 97b830a
Show file tree
Hide file tree
Showing 12 changed files with 544 additions and 1 deletion.
7 changes: 7 additions & 0 deletions MaterialComponents.podspec
Expand Up @@ -864,6 +864,13 @@ Pod::Spec.new do |mdc|
scheme.public_header_files = "components/schemes/#{scheme.base_name}/src/*.h"
scheme.source_files = "components/schemes/#{scheme.base_name}/src/*.{h,m}"
end
scheme_spec.subspec "Shape" do |scheme|
scheme.ios.deployment_target = '8.0'
scheme.public_header_files = "components/schemes/#{scheme.base_name}/src/*.h"
scheme.source_files = "components/schemes/#{scheme.base_name}/src/*.{h,m}"
scheme.dependency "MaterialComponents/private/ShapeLibrary"
scheme.dependency "MaterialComponents/private/Shapes"
end
scheme_spec.subspec "Typography" do |scheme|
scheme.ios.deployment_target = '8.0'
scheme.public_header_files = "components/schemes/#{scheme.base_name}/src/*.h"
Expand Down
@@ -0,0 +1,27 @@
// 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 "MaterialShapes.h"

typedef NS_ENUM(NSInteger, MDCCornerType) {
MDCCornerTypeCurved,
MDCCornerTypeCut,
MDCCornerTypeRounded
};

@interface MDCCornerTreatment (CornerTypeInitalizer)

- (instancetype)initWithCornerType:(MDCCornerType)cornerType andSize:(NSNumber *)size;

@end
@@ -0,0 +1,45 @@
// 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 "MDCCornerTreatment+CornerTypeInitalizer.h"

#import "MDCCurvedCornerTreatment.h"
#import "MDCCutCornerTreatment.h"
#import "MDCRoundedCornerTreatment.h"

@implementation MDCCornerTreatment (CornerTypeInitalizer)

- (instancetype)initWithCornerType:(MDCCornerType)cornerType andSize:(NSNumber *)size {
self = [self init];
switch (cornerType) {
case MDCCornerTypeCurved: {
CGSize curvedSize = [size CGSizeValue];
self = [[MDCCurvedCornerTreatment alloc] initWithSize:curvedSize];
break;
}
case MDCCornerTypeCut: {
CGFloat cutSize = [size floatValue];
self = [[MDCCutCornerTreatment alloc] initWithCut:cutSize];
break;
}
case MDCCornerTypeRounded: {
CGFloat radiusSize = [size floatValue];
self = [[MDCRoundedCornerTreatment alloc] initWithRadius:radiusSize];
break;
}
}
return self;
}

@end
3 changes: 2 additions & 1 deletion components/private/ShapeLibrary/src/MaterialShapeLibrary.h
Expand Up @@ -12,10 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#import "MDCCornerTreatment+CornerTypeInitalizer.h"
#import "MDCCurvedCornerTreatment.h"
#import "MDCCurvedRectShapeGenerator.h"
#import "MDCCutCornerTreatment.h"
#import "MDCPillShapeGenerator.h"
#import "MDCRoundedCornerTreatment.h"
#import "MDCSlantedRectShapeGenerator.h"
#import "MDCTriangleEdgeTreatment.h"
#import "MDCCutCornerTreatment.h"
59 changes: 59 additions & 0 deletions components/schemes/Shape/BUILD
@@ -0,0 +1,59 @@
# 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.

load("//:material_components_ios.bzl",
"mdc_objc_library",
"mdc_public_objc_library",
"mdc_unit_test_suite")

licenses(["notice"]) # Apache 2.0

mdc_public_objc_library(
name = "Shape",
deps = [
"//components/private/ShapeLibrary",
],
)

package_group(
name = "test_targets",
packages = [
"//components/schemes/Shape/...",
],
)

mdc_objc_library(
name = "unit_test_sources",
testonly = 1,
srcs = glob(["tests/unit/*.m"]),
hdrs = glob(["tests/unit/*.h"]),
includes = ["src"],
sdk_frameworks = [
"CoreGraphics",
"UIKit",
"XCTest",
],
deps = [
":Shape",
],
visibility = ["//visibility:private"],
)

mdc_unit_test_suite(
deps = [
":unit_test_sources",
],
)
68 changes: 68 additions & 0 deletions components/schemes/Shape/src/MDCShapeCategory.h
@@ -0,0 +1,68 @@
// 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 <Foundation/Foundation.h>
#import "MDCShapeCorner.h"

/**
The MDCShapeCategory is the class containing the shape value as part of our shape scheme,
MDCShapeScheme.
MDCShapeCategory is built from 4 corners, that can be set to alter the shape value.
*/
@interface MDCShapeCategory : NSObject

/**
This property represents the shape of the top left corner of the shape.
*/
@property(strong, nonatomic) MDCShapeCorner *topLeftCorner;

/**
This property represents the shape of the top right corner of the shape.
*/
@property(strong, nonatomic) MDCShapeCorner *topRightCorner;

/**
This property represents the shape of the bottom left corner of the shape.
*/
@property(strong, nonatomic) MDCShapeCorner *bottomLeftCorner;

/**
This property represents the shape of the bottom right corner of the shape.
*/
@property(strong, nonatomic) MDCShapeCorner *bottomRightCorner;

/**
The default init of the class. It sets all 4 corners with a corner family of
MDCShapeCornerFamilyRounded and size of 0. This is equivalent to a "sharp" corner, or in terms of
Apple's API it is the same as setting the cornerRadius to 0.
@return returns an initialized MDCShapeCategory instance.
*/
- (instancetype)init;

/**
This method is a convenience initializer of setting the shape value of all our corners at once
to the provided cornerFamily and cornerSize.
The outcome is a symmetrical shape that has the same values for all its corners.
@param cornerFamily The family of our corner (rounded or angled).
@param cornerSize The shape value of the corner.
@return returns an MDCShapeCategory with the initialized values.
*/
- (instancetype)initCornersWithFamily:(MDCShapeCornerFamily)cornerFamily
andSize:(CGFloat)cornerSize;

@end
34 changes: 34 additions & 0 deletions components/schemes/Shape/src/MDCShapeCategory.m
@@ -0,0 +1,34 @@
// 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 "MDCShapeCategory.h"

@implementation MDCShapeCategory

- (instancetype)init {
return [self initCornersWithFamily:MDCShapeCornerFamilyRounded andSize:0];
}

- (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];
}
return self;
}

@end
93 changes: 93 additions & 0 deletions components/schemes/Shape/src/MDCShapeCorner.h
@@ -0,0 +1,93 @@
// 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 <Foundation/Foundation.h>
#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

0 comments on commit 97b830a

Please sign in to comment.