2019-10-21 09:59:22 +08:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2019-07-30 15:20:11 +08:00
|
|
|
//
|
|
|
|
// DoricStackNode.m
|
|
|
|
// Doric
|
|
|
|
//
|
|
|
|
// Created by pengfei.zhou on 2019/7/30.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "DoricStackNode.h"
|
2019-07-31 18:13:22 +08:00
|
|
|
#import "DoricUtil.h"
|
2019-07-30 15:20:11 +08:00
|
|
|
|
|
|
|
@implementation DoricStackNode
|
|
|
|
|
2019-07-31 14:18:20 +08:00
|
|
|
- (instancetype)init {
|
2019-10-12 14:48:19 +08:00
|
|
|
if (self = [super init]) {
|
2019-07-31 14:18:20 +08:00
|
|
|
_gravity = 0;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)measureByParent:(DoricGroupNode *)parent {
|
|
|
|
DoricLayoutDesc widthSpec = self.layoutParams.width;
|
|
|
|
DoricLayoutDesc heightSpec = self.layoutParams.height;
|
2019-10-12 14:48:19 +08:00
|
|
|
CGFloat maxWidth = 0, maxHeight = 0;
|
2019-07-31 14:18:20 +08:00
|
|
|
for (DoricViewNode *child in self.indexedChildren) {
|
|
|
|
[child measureByParent:self];
|
2019-07-31 17:43:26 +08:00
|
|
|
CGFloat placeWidth = child.measuredWidth;
|
|
|
|
CGFloat placeHeight = child.measuredHeight;
|
2019-07-31 14:18:20 +08:00
|
|
|
maxWidth = MAX(maxWidth, placeWidth);
|
|
|
|
maxHeight = MAX(maxHeight, placeHeight);
|
|
|
|
}
|
|
|
|
self.desiredWidth = maxWidth;
|
|
|
|
self.desiredHeight = maxHeight;
|
2019-10-12 14:48:19 +08:00
|
|
|
|
2019-07-31 14:18:20 +08:00
|
|
|
if (widthSpec == LAYOUT_WRAP_CONTENT) {
|
2019-07-31 17:43:26 +08:00
|
|
|
self.width = maxWidth;
|
2019-07-31 14:18:20 +08:00
|
|
|
}
|
2019-10-12 14:48:19 +08:00
|
|
|
|
2019-07-31 14:18:20 +08:00
|
|
|
if (heightSpec == LAYOUT_WRAP_CONTENT) {
|
2019-07-31 17:43:26 +08:00
|
|
|
self.height = maxHeight;
|
2019-07-31 14:18:20 +08:00
|
|
|
}
|
2019-07-31 17:43:26 +08:00
|
|
|
}
|
2019-10-12 14:48:19 +08:00
|
|
|
|
2019-07-31 17:43:26 +08:00
|
|
|
- (LayoutParams *)generateDefaultLayoutParams {
|
|
|
|
return [[StackLayoutParams alloc] init];
|
2019-07-31 14:18:20 +08:00
|
|
|
}
|
|
|
|
|
2019-07-31 18:13:22 +08:00
|
|
|
- (void)blendChild:(DoricViewNode *)child layoutConfig:(NSDictionary *)layoutconfig {
|
|
|
|
[super blendChild:child layoutConfig:layoutconfig];
|
|
|
|
if (![child.layoutParams isKindOfClass:StackLayoutParams.class]) {
|
|
|
|
DoricLog(@"blend Stack child error,layout params not match");
|
|
|
|
return;
|
|
|
|
}
|
2019-10-12 14:48:19 +08:00
|
|
|
StackLayoutParams *params = (StackLayoutParams *) child.layoutParams;
|
2019-07-31 18:13:22 +08:00
|
|
|
// NSDictionary *margin = [layoutconfig objectForKey:@"margin"];
|
|
|
|
// if (margin) {
|
|
|
|
// params.margin.top = [(NSNumber *)[margin objectForKey:@"top"] floatValue];
|
|
|
|
// params.margin.left = [(NSNumber *)[margin objectForKey:@"left"] floatValue];
|
|
|
|
// params.margin.right = [(NSNumber *)[margin objectForKey:@"right"] floatValue];
|
|
|
|
// params.margin.bottom = [(NSNumber *)[margin objectForKey:@"bottom"] floatValue];
|
|
|
|
// }
|
2019-10-12 14:48:19 +08:00
|
|
|
NSNumber *alignment = layoutconfig[@"alignment"];
|
2019-07-31 18:13:22 +08:00
|
|
|
if (alignment) {
|
|
|
|
params.alignment = [alignment integerValue];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 14:18:20 +08:00
|
|
|
- (void)layoutByParent:(DoricGroupNode *)parent {
|
|
|
|
for (DoricViewNode *child in self.indexedChildren) {
|
|
|
|
if (child.layoutParams.width == LAYOUT_MATCH_PARENT) {
|
2019-07-31 17:43:26 +08:00
|
|
|
child.width = self.width;
|
2019-07-31 14:18:20 +08:00
|
|
|
}
|
|
|
|
if (child.layoutParams.height == LAYOUT_MATCH_PARENT) {
|
2019-07-31 17:43:26 +08:00
|
|
|
child.height = self.height;
|
|
|
|
}
|
|
|
|
DoricGravity gravity = self.gravity;
|
|
|
|
if ([child.layoutParams isKindOfClass:StackLayoutParams.class]) {
|
2019-10-12 14:48:19 +08:00
|
|
|
StackLayoutParams *layoutParams = (StackLayoutParams *) child.layoutParams;
|
2019-07-31 17:43:26 +08:00
|
|
|
gravity |= layoutParams.alignment;
|
2019-07-31 14:18:20 +08:00
|
|
|
}
|
2019-10-12 14:48:19 +08:00
|
|
|
|
2019-07-31 14:18:20 +08:00
|
|
|
if ((gravity & LEFT) == LEFT) {
|
2019-07-31 17:43:26 +08:00
|
|
|
child.left = self.left;
|
2019-07-31 14:18:20 +08:00
|
|
|
}
|
|
|
|
if ((gravity & RIGHT) == RIGHT) {
|
2019-07-31 17:43:26 +08:00
|
|
|
child.right = self.right;
|
2019-07-31 14:18:20 +08:00
|
|
|
}
|
|
|
|
if ((gravity & TOP) == TOP) {
|
2019-07-31 17:43:26 +08:00
|
|
|
child.top = self.top;
|
2019-07-31 14:18:20 +08:00
|
|
|
}
|
|
|
|
if ((gravity & BOTTOM) == BOTTOM) {
|
2019-07-31 17:43:26 +08:00
|
|
|
child.bottom = self.bottom;
|
2019-07-31 14:18:20 +08:00
|
|
|
}
|
|
|
|
if ((gravity & CENTER_X) == CENTER_X) {
|
2019-07-31 17:43:26 +08:00
|
|
|
child.centerX = self.centerX;
|
2019-07-31 14:18:20 +08:00
|
|
|
}
|
|
|
|
if ((gravity & CENTER_Y) == CENTER_Y) {
|
2019-07-31 17:43:26 +08:00
|
|
|
child.centerY = self.centerY;
|
2019-07-31 14:18:20 +08:00
|
|
|
}
|
2019-07-31 18:13:22 +08:00
|
|
|
[child layoutByParent:self];
|
2019-07-31 14:18:20 +08:00
|
|
|
}
|
|
|
|
}
|
2019-07-30 15:20:11 +08:00
|
|
|
@end
|