Support arbitrary shaped Material. (#14367)

For backward compatibility we keep supporting specifying the shape as a
combination of MaterialType and borderRadius, and we just use that as a
default when shapeBorder is null.

To cleanup the implementation if shapeBorder was not specified we just
translate the specified shape to a shapeBorder internally.
I benchmarked paint, layout and hit testing, with the specialized shape
clippers vs. the equivalent path clippers and did not see any
significant performance difference.

For testing, I extended the clippers/physicalShape matchers to match either the
specialized shape or the equivalent shape.
This commit is contained in:
amirh
2018-01-30 23:24:42 -08:00
committed by GitHub
parent 340d9e00cf
commit 0672055a72
10 changed files with 476 additions and 99 deletions

View File

@@ -782,25 +782,41 @@ class _IsMethodCall extends Matcher {
}
/// Asserts that a [Finder] locates a single object whose root RenderObject
/// is a [RenderClipRect] with no clipper set.
/// is a [RenderClipRect] with no clipper set, or an equivalent
/// [RenderClipPath].
const Matcher clipsWithBoundingRect = const _ClipsWithBoundingRect();
/// Asserts that a [Finder] locates a single object whose root RenderObject
/// is a [RenderClipRRect] with no clipper set, and border radius equals to
/// [borderRadius].
/// [borderRadius], or an equivalent [RenderClipPath].
Matcher clipsWithBoundingRRect({@required BorderRadius borderRadius}) {
return new _ClipsWithBoundingRRect(borderRadius: borderRadius);
}
/// Asserts that a [Finder] locates a single object whose root RenderObject
/// is a [RenderPhysicalModel].
///
/// - If [shape] is non null asserts that [RenderPhysicalModel.shape] is equal to
/// is a [RenderClipPath] with a [ShapeBorderClipper] that clips to
/// [shape].
/// - If [borderRadius] is non null asserts that [RenderPhysicalModel.borderRadius] is equal to
/// [borderRadius].
/// - If [elevation] is non null asserts that [RenderPhysicalModel.elevation] is equal to
/// [elevation].
Matcher clipsWithShapeBorder({@required ShapeBorder shape}) {
return new _ClipsWithShapeBorder(shape: shape);
}
/// Asserts that a [Finder] locates a single object whose root RenderObject
/// is a [RenderPhysicalModel] or a [RenderPhysicalShape].
///
/// - If the render object is a [RenderPhysicalModel]
/// - If [shape] is non null asserts that [RenderPhysicalModel.shape] is equal to
/// [shape].
/// - If [borderRadius] is non null asserts that [RenderPhysicalModel.borderRadius] is equal to
/// [borderRadius].
/// - If [elevation] is non null asserts that [RenderPhysicalModel.elevation] is equal to
/// [elevation].
/// - If the render object is a [RenderPhysicalShape]
/// - If [borderRadius] is non null asserts that the shape is a rounded
/// rectangle with this radius.
/// - If [borderRadius] is null, asserts that the shape is equivalent to
/// [shape].
/// - If [elevation] is non null asserts that [RenderPhysicalModel.elevation] is equal to
/// [elevation].
Matcher rendersOnPhysicalModel({
BoxShape shape,
BorderRadius borderRadius,
@@ -813,10 +829,26 @@ Matcher rendersOnPhysicalModel({
);
}
abstract class _MatchRenderObject<T extends RenderObject> extends Matcher {
/// Asserts that a [Finder] locates a single object whose root RenderObject
/// is [RenderPhysicalShape] that uses a [ShapeBorderClipper] that clips to
/// [shape] as its clipper.
/// If [elevation] is non null asserts that [RenderPhysicalShape.elevation] is
/// equal to [elevation].
Matcher rendersOnPhysicalShape({
ShapeBorder shape,
double elevation,
}) {
return new _RendersOnPhysicalShape(
shape: shape,
elevation: elevation,
);
}
abstract class _MatchRenderObject<M extends RenderObject, T extends RenderObject> extends Matcher {
const _MatchRenderObject();
bool renderObjectMatches(Map<dynamic, dynamic> matchState, T renderObject);
bool renderObjectMatchesT(Map<dynamic, dynamic> matchState, T renderObject);
bool renderObjectMatchesM(Map<dynamic, dynamic> matchState, M renderObject);
@override
bool matches(covariant Finder finder, Map<dynamic, dynamic> matchState) {
@@ -824,10 +856,14 @@ abstract class _MatchRenderObject<T extends RenderObject> extends Matcher {
if (nodes.length != 1)
return failWithDescription(matchState, 'did not have a exactly one child element');
final RenderObject renderObject = nodes.single.renderObject;
if (renderObject.runtimeType != T)
return failWithDescription(matchState, 'had a root render object of type: ${renderObject.runtimeType}');
return renderObjectMatches(matchState, renderObject);
if (renderObject.runtimeType == T)
return renderObjectMatchesT(matchState, renderObject);
if (renderObject.runtimeType == M)
return renderObjectMatchesM(matchState, renderObject);
return failWithDescription(matchState, 'had a root render object of type: ${renderObject.runtimeType}');
}
bool failWithDescription(Map<dynamic, dynamic> matchState, String description) {
@@ -846,7 +882,7 @@ abstract class _MatchRenderObject<T extends RenderObject> extends Matcher {
}
}
class _RendersOnPhysicalModel extends _MatchRenderObject<RenderPhysicalModel> {
class _RendersOnPhysicalModel extends _MatchRenderObject<RenderPhysicalShape, RenderPhysicalModel> {
const _RendersOnPhysicalModel({
this.shape,
this.borderRadius,
@@ -858,7 +894,7 @@ class _RendersOnPhysicalModel extends _MatchRenderObject<RenderPhysicalModel> {
final double elevation;
@override
bool renderObjectMatches(Map<dynamic, dynamic> matchState, RenderPhysicalModel renderObject) {
bool renderObjectMatchesT(Map<dynamic, dynamic> matchState, RenderPhysicalModel renderObject) {
if (shape != null && renderObject.shape != shape)
return failWithDescription(matchState, 'had shape: ${renderObject.shape}');
@@ -871,6 +907,50 @@ class _RendersOnPhysicalModel extends _MatchRenderObject<RenderPhysicalModel> {
return true;
}
@override
bool renderObjectMatchesM(Map<dynamic, dynamic> matchState, RenderPhysicalShape renderObject) {
if (renderObject.clipper.runtimeType != ShapeBorderClipper)
return failWithDescription(matchState, 'clipper was: ${renderObject.clipper}');
final ShapeBorderClipper shapeClipper = renderObject.clipper;
if (borderRadius != null && !assertRoundedRectangle(shapeClipper, borderRadius, matchState))
return false;
if (
borderRadius == null
&& shape == BoxShape.rectangle
&& !assertRoundedRectangle(shapeClipper, BorderRadius.zero, matchState)
)
return false;
if (
borderRadius == null
&& shape == BoxShape.circle
&& !assertCircle(shapeClipper, matchState)
)
return false;
if (elevation != null && renderObject.elevation != elevation)
return failWithDescription(matchState, 'had elevation: ${renderObject.elevation}');
return true;
}
bool assertRoundedRectangle(ShapeBorderClipper shapeClipper, BorderRadius borderRadius, Map<dynamic, dynamic> matchState) {
if (shapeClipper.shape.runtimeType != RoundedRectangleBorder)
return failWithDescription(matchState, 'had shape border: ${shapeClipper.shape}');
final RoundedRectangleBorder border = shapeClipper.shape;
if (border.borderRadius != borderRadius)
return failWithDescription(matchState, 'had borderRadius: ${border.borderRadius}');
return true;
}
bool assertCircle(ShapeBorderClipper shapeClipper, Map<dynamic, dynamic> matchState) {
if (shapeClipper.shape.runtimeType != CircleBorder)
return failWithDescription(matchState, 'had shape border: ${shapeClipper.shape}');
return true;
}
@override
Description describe(Description description) {
description.add('renders on a physical model');
@@ -884,29 +964,80 @@ class _RendersOnPhysicalModel extends _MatchRenderObject<RenderPhysicalModel> {
}
}
class _ClipsWithBoundingRect extends _MatchRenderObject<RenderClipRect> {
class _RendersOnPhysicalShape extends _MatchRenderObject<RenderPhysicalShape, Null> {
const _RendersOnPhysicalShape({
this.shape,
this.elevation,
});
final ShapeBorder shape;
final double elevation;
@override
bool renderObjectMatchesM(Map<dynamic, dynamic> matchState, RenderPhysicalShape renderObject) {
if (renderObject.clipper.runtimeType != ShapeBorderClipper)
return failWithDescription(matchState, 'clipper was: ${renderObject.clipper}');
final ShapeBorderClipper shapeClipper = renderObject.clipper;
if (shapeClipper.shape != shape)
return failWithDescription(matchState, 'shape was: ${shapeClipper.shape}');
if (elevation != null && renderObject.elevation != elevation)
return failWithDescription(matchState, 'had elevation: ${renderObject.elevation}');
return true;
}
@override
bool renderObjectMatchesT(Map<dynamic, dynamic> matchState, RenderPhysicalModel renderObject) {
return false;
}
@override
Description describe(Description description) {
description.add('renders on a physical model with shape $shape');
if (elevation != null)
description.add(' with elevation $elevation');
return description;
}
}
class _ClipsWithBoundingRect extends _MatchRenderObject<RenderClipPath, RenderClipRect> {
const _ClipsWithBoundingRect();
@override
bool renderObjectMatches(Map<dynamic, dynamic> matchState, RenderClipRect renderObject) {
bool renderObjectMatchesT(Map<dynamic, dynamic> matchState, RenderClipRect renderObject) {
if (renderObject.clipper != null)
return failWithDescription(matchState, 'had a non null clipper ${renderObject.clipper}');
return true;
}
@override
bool renderObjectMatchesM(Map<dynamic, dynamic> matchState, RenderClipPath renderObject) {
if (renderObject.clipper.runtimeType != ShapeBorderClipper)
return failWithDescription(matchState, 'clipper was: ${renderObject.clipper}');
final ShapeBorderClipper shapeClipper = renderObject.clipper;
if (shapeClipper.shape.runtimeType != RoundedRectangleBorder)
return failWithDescription(matchState, 'shape was: ${shapeClipper.shape}');
final RoundedRectangleBorder border = shapeClipper.shape;
if (border.borderRadius != BorderRadius.zero)
return failWithDescription(matchState, 'borderRadius was: ${border.borderRadius}');
return true;
}
@override
Description describe(Description description) =>
description.add('clips with bounding rectangle');
}
class _ClipsWithBoundingRRect extends _MatchRenderObject<RenderClipRRect> {
class _ClipsWithBoundingRRect extends _MatchRenderObject<RenderClipPath, RenderClipRRect> {
const _ClipsWithBoundingRRect({@required this.borderRadius});
final BorderRadius borderRadius;
@override
bool renderObjectMatches(Map<dynamic, dynamic> matchState, RenderClipRRect renderObject) {
bool renderObjectMatchesT(Map<dynamic, dynamic> matchState, RenderClipRRect renderObject) {
if (renderObject.clipper != null)
return failWithDescription(matchState, 'had a non null clipper ${renderObject.clipper}');
@@ -916,7 +1047,46 @@ class _ClipsWithBoundingRRect extends _MatchRenderObject<RenderClipRRect> {
return true;
}
@override
bool renderObjectMatchesM(Map<dynamic, dynamic> matchState, RenderClipPath renderObject) {
if (renderObject.clipper.runtimeType != ShapeBorderClipper)
return failWithDescription(matchState, 'clipper was: ${renderObject.clipper}');
final ShapeBorderClipper shapeClipper = renderObject.clipper;
if (shapeClipper.shape.runtimeType != RoundedRectangleBorder)
return failWithDescription(matchState, 'shape was: ${shapeClipper.shape}');
final RoundedRectangleBorder border = shapeClipper.shape;
if (border.borderRadius != borderRadius)
return failWithDescription(matchState, 'had borderRadius: ${border.borderRadius}');
return true;
}
@override
Description describe(Description description) =>
description.add('clips with bounding rounded rectangle with borderRadius: $borderRadius');
}
class _ClipsWithShapeBorder extends _MatchRenderObject<RenderClipPath, Null> {
const _ClipsWithShapeBorder({@required this.shape});
final ShapeBorder shape;
@override
bool renderObjectMatchesM(Map<dynamic, dynamic> matchState, RenderClipPath renderObject) {
if (renderObject.clipper.runtimeType != ShapeBorderClipper)
return failWithDescription(matchState, 'clipper was: ${renderObject.clipper}');
final ShapeBorderClipper shapeClipper = renderObject.clipper;
if (shapeClipper.shape != shape)
return failWithDescription(matchState, 'shape was: ${shapeClipper.shape}');
return true;
}
@override
bool renderObjectMatchesT(Map<dynamic, dynamic> matchState, RenderClipRRect renderObject) {
return false;
}
@override
Description describe(Description description) =>
description.add('clips with shape: $shape');
}