Swiftgram/AsyncDisplayKit/Layout/ASRatioLayoutSpec.mm
Michael Schneider 8897614f0e [Layout] Layout API based on content area (#2110)
* Initial commit for adding a size constraint to ASLayoutable's

* Some more commits

* Fix sample projects in extra/

* Remove preferredFrameSize test of ASEditableTextNode

* Remove preferredFrameSize from examples_extra

* Add deprecation warning to -[ASDisplayNode preferredFrameSize]

* Add deprecation warning to -[ASDisplayNode measureWithSizeRange:]

* Commit

* Commit

* Remove ASRelativeSizeRange

* Make ASRelativeSize private

* Adjust examples

* Improve setting of -[ASLayoutable size] with points or fractions

* Add ASWrapperLayoutSpec

* Improve creation of ASRelativeDimension

* Add `preferredFrameSize` back and add deprecated logging

* Add `layoutSpecBlock` setter and getter and add locking for it

* Add better documentation and fix macros to create ASRelativeDimension

* Create new ASSizeRangeMake with just a CGSize as parameter

* Update Kitten and Social App Layout example

* Add layoutThatFits: and deprecate measure:

* Rename ASRelativeDimension to ASDimension

* Fix examples for ASDimension renaming

* Remove fancy height and width setter

* Fix ASDimension helper

* Rename -[ASLayout layoutableObject] to -[ASLayout layoutable]

* Update layout related methods and more clearer documentation around how to use it

* Deprecate old ASLayout class constructors

* Don't unnecessary recalculate layout if constrained or parent size did not change

* Use shared pointer for ASDisplayNodeLayout

* Fix rebase conflicts

* Add documentation and move implementation in mm file of ASDisplayNodeLayout

* Fix test errors

* Rename ASSize to ASLayoutableSize

* Address comments

* Rename setSizeFromCGSize to setSizeWithCGSize

* Improve inline functions in ASDimension

* Fix rebase conflicts
2016-09-07 08:44:48 -07:00

93 lines
3.0 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 *)calculateLayoutThatFits:(ASSizeRange)constrainedSize
{
std::vector<CGSize> sizeOptions;
// TODO: layout: isValidForLayout() call should not be necessary if INFINITY is used
if (!isinf(constrainedSize.max.width) && ASPointsAreValidForLayout(constrainedSize.max.width)) {
sizeOptions.push_back(ASSizeRangeClamp(constrainedSize, {
constrainedSize.max.width,
ASFloorPixelValue(_ratio * constrainedSize.max.width)
}));
}
// TODO: layout: isValidForLayout() call should not be necessary if INFINITY is used
if (!isinf(constrainedSize.max.height) && ASPointsAreValidForLayout(constrainedSize.max.width)) {
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);
const CGSize parentSize = (bestSize == sizeOptions.end()) ? ASLayoutableParentSizeUndefined : *bestSize;
ASLayout *sublayout = [self.child layoutThatFits:childRange parentSize:parentSize];
sublayout.position = CGPointZero;
return [ASLayout layoutWithLayoutable:self size:sublayout.size sublayouts:@[sublayout]];
}
@end
@implementation ASRatioLayoutSpec (Debugging)
#pragma mark - ASLayoutableAsciiArtProtocol
- (NSString *)asciiArtName
{
return [NSString stringWithFormat:@"%@ (%.1f)", NSStringFromClass([self class]), self.ratio];
}
@end