This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Doric/iOS/Pod/Classes/Shader/DoricLayouts.m

518 lines
16 KiB
Mathematica
Raw Normal View History

/*
* Copyright [2019] [Doric.Pub]
*
* 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.
*/
//
// Created by pengfei.zhou on 2019/10/23.
//
2019-10-23 20:06:21 +08:00
#import "DoricLayouts.h"
#import <objc/runtime.h>
#import "UIView+Doric.h"
2019-10-23 20:06:21 +08:00
DoricMargin DoricMarginMake(CGFloat left, CGFloat top, CGFloat right, CGFloat bottom) {
DoricMargin margin;
margin.left = left;
margin.top = top;
margin.right = right;
margin.bottom = bottom;
return margin;
}
2019-10-23 20:06:21 +08:00
@implementation DoricLayoutConfig
- (instancetype)init {
if (self = [super init]) {
2019-10-23 20:06:21 +08:00
_widthSpec = DoricLayoutExact;
_heightSpec = DoricLayoutExact;
}
return self;
}
2019-10-23 20:06:21 +08:00
- (instancetype)initWithWidth:(DoricLayoutSpec)width height:(DoricLayoutSpec)height {
if (self = [super init]) {
_widthSpec = width;
_heightSpec = height;
}
return self;
}
@end
2019-10-23 20:06:21 +08:00
@implementation DoricMarginConfig
- (instancetype)init {
if (self = [super init]) {
2019-10-23 20:06:21 +08:00
_margin = DoricMarginMake(0, 0, 0, 0);
}
return self;
}
2019-10-23 20:06:21 +08:00
- (instancetype)initWithWidth:(DoricLayoutSpec)width height:(DoricLayoutSpec)height margin:(DoricMargin)margin {
if (self = [super initWithWidth:width height:height]) {
_margin = margin;
}
return self;
}
@end
2019-10-23 20:06:21 +08:00
@implementation DoricStackConfig
@end
2019-10-23 20:06:21 +08:00
@implementation DoricLinearConfig
@end
2019-10-23 20:06:21 +08:00
@interface DoricLayoutContainer ()
@property(nonatomic, assign) BOOL waitingLayout;
@end
2019-10-23 20:06:21 +08:00
@implementation DoricLayoutContainer
- (instancetype)init {
if (self = [super init]) {
_waitingLayout = NO;
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
_waitingLayout = NO;
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)coder {
if (self = [super initWithCoder:coder]) {
_waitingLayout = NO;
}
return self;
}
2019-10-23 20:06:21 +08:00
- (DoricLayoutConfig *)configForChild:(UIView *)child {
DoricLayoutConfig *config = child.layoutConfig;
if (!config) {
2019-10-23 20:06:21 +08:00
config = [[DoricLayoutConfig alloc] init];
}
return config;
}
- (void)requestLayout {
2019-10-23 20:11:24 +08:00
if ([self.superview isKindOfClass:[DoricLinearView class]]) {
[(DoricLinearView *) self.superview requestLayout];
return;
}
if (self.waitingLayout) {
return;
}
self.waitingLayout = YES;
__weak typeof(self) _self = self;
dispatch_async(dispatch_get_main_queue(), ^{
__strong typeof(_self) self = _self;
[self sizeToFit];
[self layout];
self.waitingLayout = NO;
});
}
- (void)setNeedsLayout {
[super setNeedsLayout];
if (self.waitingLayout) {
return;
}
[self requestLayout];
}
- (BOOL)waitingLayout {
if ([self.superview isKindOfClass:[DoricLayoutContainer class]]) {
return [(DoricLayoutContainer *) self.superview waitingLayout];
}
return _waitingLayout;
}
- (void)layout {
[self.subviews enumerateObjectsUsingBlock:^(__kindof UIView *child, NSUInteger idx, BOOL *stop) {
2019-10-23 20:06:21 +08:00
if ([child isKindOfClass:[DoricLayoutContainer class]]) {
[(DoricLayoutContainer *) child layout];
}
}];
}
@end
2019-10-23 20:06:21 +08:00
@interface DoricStackView ()
@property(nonatomic, assign) CGFloat contentWidth;
@property(nonatomic, assign) CGFloat contentHeight;
@end
2019-10-23 20:06:21 +08:00
@implementation DoricStackView
- (DoricStackConfig *)configForChild:(UIView *)child {
DoricStackConfig *config = (DoricStackConfig *) child.layoutConfig;
if (!config) {
2019-10-23 20:06:21 +08:00
config = [[DoricStackConfig alloc] init];
}
return config;
}
- (void)sizeToFit {
2019-10-23 20:06:21 +08:00
DoricLayoutConfig *config = self.layoutConfig;
self.contentWidth = 0;
self.contentHeight = 0;
for (UIView *child in self.subviews) {
if (child.isHidden) {
continue;
}
2019-10-23 20:06:21 +08:00
DoricStackConfig *childConfig = [self configForChild:child];
if ([child isKindOfClass:[DoricLayoutContainer class]]
|| childConfig.widthSpec == DoricLayoutWrapContent
|| childConfig.heightSpec == DoricLayoutWrapContent) {
[child sizeToFit];
}
self.contentWidth = MAX(self.contentWidth, child.width);
self.contentHeight = MAX(self.contentHeight, child.height);
}
2019-10-23 20:06:21 +08:00
if (config.widthSpec == DoricLayoutWrapContent) {
self.width = self.contentWidth;
2019-10-23 20:06:21 +08:00
} else if (config.widthSpec == DoricLayoutAtMost) {
self.width = self.superview.width;
}
2019-10-23 20:06:21 +08:00
if (config.heightSpec == DoricLayoutWrapContent) {
self.height = self.contentHeight;
2019-10-23 20:06:21 +08:00
} else if (config.heightSpec == DoricLayoutAtMost) {
self.height = self.superview.height;
}
}
- (void)layout {
for (UIView *child in self.subviews) {
if (child.isHidden) {
continue;
}
2019-10-23 20:06:21 +08:00
DoricStackConfig *childConfig = [self configForChild:child];
DoricGravity gravity = childConfig.alignment | self.gravity;
if ((gravity & LEFT) == LEFT) {
child.left = 0;
} else if ((gravity & RIGHT) == RIGHT) {
child.right = self.width;
} else if ((gravity & CENTER_X) == CENTER_X) {
child.centerX = self.width / 2;
}
if ((gravity & TOP) == TOP) {
child.top = 0;
} else if ((gravity & BOTTOM) == BOTTOM) {
child.bottom = self.height;
} else if ((gravity & CENTER_Y) == CENTER_Y) {
child.centerY = self.height / 2;
}
2019-10-23 20:06:21 +08:00
if (childConfig.widthSpec == DoricLayoutAtMost) {
child.width = self.width;
}
2019-10-23 20:06:21 +08:00
if (childConfig.heightSpec == DoricLayoutAtMost) {
child.height = self.height;
}
2019-10-23 20:06:21 +08:00
if ([child isKindOfClass:[DoricLayoutContainer class]]) {
[(DoricLayoutContainer *) child layout];
}
}
}
@end
2019-10-23 20:11:24 +08:00
@interface DoricLinearView ()
@property(nonatomic, assign) CGFloat contentWidth;
@property(nonatomic, assign) CGFloat contentHeight;
@property(nonatomic, assign) NSUInteger contentWeight;
@end
2019-10-23 20:11:24 +08:00
@implementation DoricLinearView
2019-10-23 20:06:21 +08:00
- (DoricLinearConfig *)configForChild:(UIView *)child {
DoricLinearConfig *config = (DoricLinearConfig *) child.layoutConfig;
if (!config) {
2019-10-23 20:06:21 +08:00
config = [[DoricLinearConfig alloc] init];
}
return config;
}
@end
2019-10-23 20:11:24 +08:00
@implementation DoricVLayoutView
- (void)sizeToFit {
2019-10-23 20:06:21 +08:00
DoricLayoutConfig *config = self.layoutConfig;
self.contentWidth = 0;
self.contentHeight = 0;
self.contentWeight = 0;
for (UIView *child in self.subviews) {
if (child.isHidden) {
continue;
}
2019-10-23 20:06:21 +08:00
DoricLinearConfig *childConfig = [self configForChild:child];
if ([child isKindOfClass:[DoricLayoutContainer class]]
|| childConfig.widthSpec == DoricLayoutWrapContent
|| childConfig.heightSpec == DoricLayoutWrapContent) {
[child sizeToFit];
}
self.contentWidth = MAX(self.contentWidth, child.width + childConfig.margin.left + childConfig.margin.right);
self.contentHeight += child.height + self.space + childConfig.margin.top + childConfig.margin.bottom;
self.contentWeight += childConfig.weight;
}
self.contentHeight -= self.space;
2019-10-23 20:06:21 +08:00
if (config.widthSpec == DoricLayoutWrapContent) {
self.width = self.contentWidth;
2019-10-23 20:06:21 +08:00
} else if (config.widthSpec == DoricLayoutAtMost) {
self.width = self.superview.width;
}
2019-10-23 20:06:21 +08:00
if (config.heightSpec == DoricLayoutWrapContent) {
self.height = self.contentHeight;
2019-10-23 20:06:21 +08:00
} else if (config.heightSpec == DoricLayoutAtMost) {
self.height = self.superview.height;
}
if (self.contentWeight) {
CGFloat remain = self.height - self.contentHeight;
for (UIView *child in self.subviews) {
if (child.isHidden) {
continue;
}
2019-10-23 20:06:21 +08:00
DoricLinearConfig *childConfig = [self configForChild:child];
if (childConfig.weight) {
child.height += remain / self.contentWeight * childConfig.weight;
}
}
self.contentHeight = self.height;
}
}
- (void)layout {
CGFloat yStart = 0;
if ((self.gravity & TOP) == TOP) {
yStart = 0;
} else if ((self.gravity & BOTTOM) == BOTTOM) {
yStart = self.height - self.contentHeight;
} else if ((self.gravity & CENTER_Y) == CENTER_Y) {
yStart = (self.height - self.contentHeight) / 2;
}
for (UIView *child in self.subviews) {
if (child.isHidden) {
continue;
}
2019-10-23 20:06:21 +08:00
DoricLinearConfig *childConfig = [self configForChild:child];
DoricGravity gravity = childConfig.alignment | self.gravity;
if ((gravity & LEFT) == LEFT) {
child.left = 0;
} else if ((gravity & RIGHT) == RIGHT) {
child.right = self.width;
} else if ((gravity & CENTER_X) == CENTER_X) {
child.centerX = self.width / 2;
} else {
if (childConfig.margin.left) {
child.left = childConfig.margin.left;
} else if (childConfig.margin.right) {
child.right = self.width - childConfig.margin.right;
}
}
2019-10-23 20:06:21 +08:00
if (childConfig.widthSpec == DoricLayoutAtMost) {
child.width = self.width;
}
2019-10-23 20:06:21 +08:00
if (childConfig.heightSpec == DoricLayoutAtMost) {
child.height = self.height - yStart - childConfig.margin.top - childConfig.margin.bottom - self.space;
}
if (childConfig.margin.top) {
yStart += childConfig.margin.top;
}
child.top = yStart;
yStart = child.bottom + self.space;
if (childConfig.margin.bottom) {
yStart += childConfig.margin.bottom;
}
2019-10-23 20:06:21 +08:00
if ([child isKindOfClass:[DoricLayoutContainer class]]) {
[(DoricLayoutContainer *) child layout];
}
}
}
@end
2019-10-23 20:11:24 +08:00
@implementation DoricHLayoutView
- (void)sizeToFit {
2019-10-23 20:06:21 +08:00
DoricLinearConfig *config;
2019-10-23 20:11:24 +08:00
if ([self.superview isKindOfClass:[DoricLinearView class]]) {
config = [(DoricLinearView *) self.superview configForChild:self];
} else {
2019-10-23 20:06:21 +08:00
config = (DoricLinearConfig *) self.layoutConfig;
if (!config) {
2019-10-23 20:06:21 +08:00
config = [[DoricLinearConfig alloc] init];
}
}
self.contentWidth = 0;
self.contentHeight = 0;
self.contentWeight = 0;
for (UIView *child in self.subviews) {
if (child.isHidden) {
continue;
}
2019-10-23 20:06:21 +08:00
DoricLinearConfig *childConfig = [self configForChild:child];
if ([child isKindOfClass:[DoricLayoutContainer class]]
|| childConfig.widthSpec == DoricLayoutWrapContent
|| childConfig.heightSpec == DoricLayoutWrapContent) {
[child sizeToFit];
}
self.contentHeight = MAX(self.contentHeight, child.height + childConfig.margin.top + childConfig.margin.bottom);
self.contentWidth += child.width + self.space + childConfig.margin.left + childConfig.margin.right;
self.contentWeight += childConfig.weight;
}
self.contentWidth -= self.space;
2019-10-23 20:06:21 +08:00
if (config.widthSpec == DoricLayoutWrapContent) {
self.width = self.contentWidth;
2019-10-23 20:06:21 +08:00
} else if (config.widthSpec == DoricLayoutAtMost) {
self.width = self.superview.width;
}
2019-10-23 20:06:21 +08:00
if (config.heightSpec == DoricLayoutWrapContent) {
self.height = self.contentHeight;
2019-10-23 20:06:21 +08:00
} else if (config.heightSpec == DoricLayoutAtMost) {
self.height = self.superview.height;
}
if (self.contentWeight) {
CGFloat remain = self.width - self.contentWidth;
for (UIView *child in self.subviews) {
if (child.isHidden) {
continue;
}
2019-10-23 20:06:21 +08:00
DoricLinearConfig *childConfig = [self configForChild:child];
if (childConfig.weight) {
child.width += remain / self.contentWeight * childConfig.weight;
}
}
self.contentWidth = self.width;
}
}
- (void)layout {
CGFloat xStart = 0;
if ((self.gravity & LEFT) == LEFT) {
xStart = 0;
} else if ((self.gravity & RIGHT) == RIGHT) {
xStart = self.width - self.contentWidth;
} else if ((self.gravity & CENTER_X) == CENTER_X) {
xStart = (self.width - self.contentWidth) / 2;
}
for (UIView *child in self.subviews) {
if (child.isHidden) {
continue;
}
2019-10-23 20:06:21 +08:00
DoricLinearConfig *childConfig = [self configForChild:child];
DoricGravity gravity = childConfig.alignment | self.gravity;
if ((gravity & TOP) == TOP) {
child.top = 0;
} else if ((gravity & BOTTOM) == BOTTOM) {
child.bottom = self.height;
} else if ((gravity & CENTER_Y) == CENTER_Y) {
child.centerY = self.height / 2;
} else {
if (childConfig.margin.top) {
child.top = childConfig.margin.top;
} else if (childConfig.margin.bottom) {
child.bottom = self.height - childConfig.margin.bottom;
}
}
2019-10-23 20:06:21 +08:00
if (childConfig.heightSpec == DoricLayoutAtMost) {
child.height = self.height;
}
2019-10-23 20:06:21 +08:00
if (childConfig.widthSpec == DoricLayoutAtMost) {
child.width = self.width - xStart - childConfig.margin.right - childConfig.margin.left - self.space;
}
if (childConfig.margin.left) {
xStart += childConfig.margin.left;
}
child.left = xStart;
xStart = child.right + self.space;
if (childConfig.margin.right) {
xStart += childConfig.margin.right;
}
2019-10-23 20:06:21 +08:00
if ([child isKindOfClass:[DoricLayoutContainer class]]) {
[(DoricLayoutContainer *) child layout];
}
}
}
@end
static const void *kLayoutConfig = &kLayoutConfig;
static const void *kTagString = &kTagString;
2019-10-23 20:11:24 +08:00
@implementation UIView (DoricLayoutConfig)
@dynamic layoutConfig;
2019-10-23 20:06:21 +08:00
- (void)setLayoutConfig:(DoricLayoutConfig *)layoutConfig {
objc_setAssociatedObject(self, kLayoutConfig, layoutConfig, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
2019-10-23 20:06:21 +08:00
- (DoricLayoutConfig *)layoutConfig {
return objc_getAssociatedObject(self, kLayoutConfig);
}
- (void)setTagString:(NSString *)tagString {
objc_setAssociatedObject(self, kTagString, tagString, OBJC_ASSOCIATION_COPY_NONATOMIC);
self.tag = [tagString hash];
}
- (NSString *)tagString {
return objc_getAssociatedObject(self, kTagString);
}
- (UIView *)viewWithTagString:(NSString *)tagString {
// notice the potential hash collision
return [self viewWithTag:[tagString hash]];
}
@end
2019-10-23 20:11:24 +08:00
DoricVLayoutView *vLayout(NSArray <__kindof UIView *> *views) {
DoricVLayoutView *layout = [[DoricVLayoutView alloc] initWithFrame:CGRectZero];
for (__kindof UIView *uiView in views) {
[layout addSubview:uiView];
}
2019-10-23 20:06:21 +08:00
layout.layoutConfig = [[DoricLayoutConfig alloc] initWithWidth:DoricLayoutWrapContent height:DoricLayoutWrapContent];
return layout;
}
2019-10-23 20:11:24 +08:00
DoricHLayoutView *hLayout(NSArray <__kindof UIView *> *views) {
DoricHLayoutView *layout = [[DoricHLayoutView alloc] initWithFrame:CGRectZero];
for (__kindof UIView *uiView in views) {
[layout addSubview:uiView];
}
2019-10-23 20:06:21 +08:00
layout.layoutConfig = [[DoricLayoutConfig alloc] initWithWidth:DoricLayoutWrapContent height:DoricLayoutWrapContent];
return layout;
}
2019-10-23 20:11:24 +08:00
DoricVLayoutView *vLayoutWithBlock(NSArray <UIView *(^)()> *blocks) {
DoricVLayoutView *layout = [[DoricVLayoutView alloc] initWithFrame:CGRectZero];
UIView *(^block)();
for (block in blocks) {
[layout addSubview:block()];
}
2019-10-23 20:06:21 +08:00
layout.layoutConfig = [[DoricLayoutConfig alloc] initWithWidth:DoricLayoutWrapContent height:DoricLayoutWrapContent];
return layout;
}
2019-10-23 20:11:24 +08:00
DoricHLayoutView *hLayoutWithBlock(NSArray <UIView *(^)()> *blocks) {
DoricHLayoutView *layout = [[DoricHLayoutView alloc] initWithFrame:CGRectZero];
UIView *(^block)();
for (block in blocks) {
[layout addSubview:block()];
}
2019-10-23 20:06:21 +08:00
layout.layoutConfig = [[DoricLayoutConfig alloc] initWithWidth:DoricLayoutWrapContent height:DoricLayoutWrapContent];
return layout;
2019-10-23 20:06:21 +08:00
}