Swiftgram/AsyncDisplayKit/Layout/ASRatioLayoutSpec.mm
Hannah Troisi 893e601e81 Convert to type-generic math (#2050)
* [Optimization] Convert to type-generic math

* add std:: prefix in obj-c++ files

* more cleanup

* revert test changes

* convert min and max back to fmin/fmax
2016-08-09 15:31:37 -07:00

93 lines
2.8 KiB
Plaintext

//
// ASRatioLayoutSpec.mm
// AsyncDisplayKit
//
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
//
#import "ASRatioLayoutSpec.h"
#import <algorithm>
#import <tgmath.h>
#import <vector>
#import "ASAssert.h"
#import "ASInternalHelpers.h"
#import "ASLayout.h"
@implementation ASRatioLayoutSpec
{
CGFloat _ratio;
}
+ (instancetype)ratioLayoutSpecWithRatio:(CGFloat)ratio child:(id<ASLayoutable>)child
{
return [[self alloc] initWithRatio:ratio child:child];
}
- (instancetype)initWithRatio:(CGFloat)ratio child:(id<ASLayoutable>)child;
{
if (!(self = [super init])) {
return nil;
}
ASDisplayNodeAssertNotNil(child, @"Child cannot be nil");
ASDisplayNodeAssert(ratio > 0, @"Ratio should be strictly positive, but received %f", ratio);
_ratio = ratio;
[self setChild:child];
return self;
}
- (void)setRatio:(CGFloat)ratio
{
ASDisplayNodeAssert(self.isMutable, @"Cannot set properties when layout spec is not mutable");
_ratio = ratio;
}
- (ASLayout *)measureWithSizeRange:(ASSizeRange)constrainedSize
{
std::vector<CGSize> sizeOptions;
if (!isinf(constrainedSize.max.width)) {
sizeOptions.push_back(ASSizeRangeClamp(constrainedSize, {
constrainedSize.max.width,
ASFloorPixelValue(_ratio * constrainedSize.max.width)
}));
}
if (!isinf(constrainedSize.max.height)) {
sizeOptions.push_back(ASSizeRangeClamp(constrainedSize, {
ASFloorPixelValue(constrainedSize.max.height / _ratio),
constrainedSize.max.height
}));
}
// Choose the size closest to the desired ratio.
const auto &bestSize = std::max_element(sizeOptions.begin(), sizeOptions.end(), [&](const CGSize &a, const CGSize &b){
return std::fabs((a.height / a.width) - _ratio) > std::fabs((b.height / b.width) - _ratio);
});
// If there is no max size in *either* dimension, we can't apply the ratio, so just pass our size range through.
const ASSizeRange childRange = (bestSize == sizeOptions.end()) ? constrainedSize : ASSizeRangeMake(*bestSize, *bestSize);
ASLayout *sublayout = [self.child measureWithSizeRange:childRange];
sublayout.position = CGPointZero;
return [ASLayout layoutWithLayoutableObject:self
constrainedSizeRange:constrainedSize
size:sublayout.size
sublayouts:@[sublayout]];
}
@end
@implementation ASRatioLayoutSpec (Debugging)
#pragma mark - ASLayoutableAsciiArtProtocol
- (NSString *)asciiArtName
{
return [NSString stringWithFormat:@"%@ (%.1f)", NSStringFromClass([self class]), self.ratio];
}
@end