forked from tidev/titanium-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLayoutConstraint.m
383 lines (338 loc) · 14.4 KB
/
LayoutConstraint.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2010 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
#import "LayoutConstraint.h"
#import "QuartzCore/QuartzCore.h"
#import "TiUtils.h"
#import "TiUIView.h"
#import "TiViewProxy.h"
/* BEGIN PSEUDOCODE
First try width.
Width is constant or percent: create a width value appropriate. Consult view if it's a valid width.
Width is auto: Consult view on preferred width. If so, use it. If not, act as if it's undefined.
Okay, see if we have a width. If so, look to see if we have x. If so, we're done for horizontal.
If width is valid:
if x is constant or percent:
create a valid x
else if left and right are defined:
Balance springily.
else if left is defined
x = left + width*anchorpoint
else if right is defined
x = superviewwidth - right - width*anchorpoint
else (left and right are undefined)
x = superviewwidth/2 - width*anchorpoint
else (width is invalid)
(Same as before)
*/
CGSize SizeConstraintViewWithSizeAddingResizing(LayoutConstraint * constraint, NSObject<LayoutAutosizing> * autoSizer, CGSize referenceSize, UIViewAutoresizing * resultResizing)
{
//TODO: Refactor for elegance.
CGFloat width;
BOOL ignorePercent = NO;
CGSize parentSize = CGSizeZero;
if ([autoSizer isKindOfClass:[TiViewProxy class]]) {
TiViewProxy* parent = [(TiViewProxy*)autoSizer parent];
if (parent != nil && (!TiLayoutRuleIsAbsolute([parent layoutProperties]->layoutStyle))) {
//Sandbox with percent values is garbage
ignorePercent = YES;
parentSize = [parent size].rect.size;
}
}
if(resultResizing != NULL)
{
*resultResizing &= ~(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
}
switch (constraint->width.type)
{
case TiDimensionTypeDip:
width = TiDimensionCalculateValue(constraint->width, referenceSize.width);
break;
case TiDimensionTypePercent:
if (ignorePercent) {
width = TiDimensionCalculateValue(constraint->width, parentSize.width);
}
else {
width = TiDimensionCalculateValue(constraint->width, referenceSize.width);
}
break;
case TiDimensionTypeUndefined:
if (!TiDimensionIsUndefined(constraint->left) && !TiDimensionIsUndefined(constraint->centerX) ) {
width = 2 * ( TiDimensionCalculateValue(constraint->centerX, referenceSize.width) - TiDimensionCalculateValue(constraint->left, referenceSize.width) );
break;
}
else if (!TiDimensionIsUndefined(constraint->left) && !TiDimensionIsUndefined(constraint->right) ) {
width = TiDimensionCalculateMargins(constraint->left, constraint->right, referenceSize.width);
break;
}
else if (!TiDimensionIsUndefined(constraint->centerX) && !TiDimensionIsUndefined(constraint->right) ) {
width = 2 * ( referenceSize.width - TiDimensionCalculateValue(constraint->right, referenceSize.width) - TiDimensionCalculateValue(constraint->centerX, referenceSize.width));
break;
}
case TiDimensionTypeAuto:
case TiDimensionTypeAutoSize:
case TiDimensionTypeAutoFill:
{
width = TiDimensionCalculateMargins(constraint->left, constraint->right, referenceSize.width);
BOOL autoFill = NO;
//Undefined falls to auto behavior
if ( TiDimensionIsUndefined(constraint->width) || TiDimensionIsAuto(constraint->width) )
{
//Check if default auto behavior is fill
if ([autoSizer respondsToSelector:@selector(defaultAutoWidthBehavior:)]) {
if (TiDimensionIsAutoFill([autoSizer defaultAutoWidthBehavior:nil])) {
autoFill = YES;
}
}
}
if (TiDimensionIsAutoFill(constraint->width) || autoFill) {
if(resultResizing != NULL){
*resultResizing |= UIViewAutoresizingFlexibleWidth;
}
break;
}
//If it comes here it has to follow SIZE behavior
if ([autoSizer respondsToSelector:@selector(autoWidthForSize:)])
{
CGFloat desiredWidth = [autoSizer autoWidthForSize:CGSizeMake(width, referenceSize.height)];
width = width < desiredWidth?width:desiredWidth;
}
else if(resultResizing != NULL)
{
*resultResizing |= UIViewAutoresizingFlexibleWidth;
}
break;
}
}
//Should we always do this or only for auto
if ([autoSizer respondsToSelector:@selector(verifyWidth:)])
{
width = [autoSizer verifyWidth:width];
}
CGFloat height;
switch (constraint->height.type)
{
case TiDimensionTypeDip:
height = TiDimensionCalculateValue(constraint->height, referenceSize.height);
break;
case TiDimensionTypePercent:
if (ignorePercent) {
height = TiDimensionCalculateValue(constraint->height, parentSize.height);
}
else {
height = TiDimensionCalculateValue(constraint->height, referenceSize.height);
}
break;
case TiDimensionTypeUndefined:
if (!TiDimensionIsUndefined(constraint->top) && !TiDimensionIsUndefined(constraint->centerY) ) {
height = 2 * ( TiDimensionCalculateValue(constraint->centerY, referenceSize.height) - TiDimensionCalculateValue(constraint->top, referenceSize.height) );
break;
}
else if (!TiDimensionIsUndefined(constraint->top) && !TiDimensionIsUndefined(constraint->bottom) ) {
height = TiDimensionCalculateMargins(constraint->top, constraint->bottom, referenceSize.height);
break;
}
else if (!TiDimensionIsUndefined(constraint->centerY) && !TiDimensionIsUndefined(constraint->bottom) ) {
height = 2 * ( referenceSize.height - TiDimensionCalculateValue(constraint->centerY, referenceSize.height) - TiDimensionCalculateValue(constraint->bottom, referenceSize.height) );
break;
}
case TiDimensionTypeAuto:
case TiDimensionTypeAutoSize:
case TiDimensionTypeAutoFill:
{
height = TiDimensionCalculateMargins(constraint->top, constraint->bottom, referenceSize.height);
BOOL autoFill = NO;
//Undefined falls to auto behavior
if ( TiDimensionIsUndefined(constraint->height) || TiDimensionIsAuto(constraint->height) )
{
//Check if default auto behavior is fill
if ([autoSizer respondsToSelector:@selector(defaultAutoHeightBehavior:)]) {
if (TiDimensionIsAutoFill([autoSizer defaultAutoHeightBehavior:nil])) {
autoFill = YES;
}
}
}
if (TiDimensionIsAutoFill(constraint->height) || autoFill) {
if(resultResizing != NULL){
*resultResizing |= UIViewAutoresizingFlexibleHeight;
}
break;
}
//If it comes here it has to follow size behavior
if ([autoSizer respondsToSelector:@selector(autoHeightForSize:)])
{
CGFloat desiredHeight = [autoSizer autoHeightForSize:CGSizeMake(width, height)];
height = height < desiredHeight?height:desiredHeight;
}
else if(resultResizing != NULL)
{
*resultResizing |= UIViewAutoresizingFlexibleHeight;
}
break;
}
}
//Should we always do this or only for auto
if ([autoSizer respondsToSelector:@selector(verifyHeight:)])
{
height = [autoSizer verifyHeight:height];
}
// when you use negative top, you get into a situation where you get smaller
// then intended sizes when using auto. this allows you to set a floor for
// the height/width so that it won't be smaller than specified - defaults to 0
height = MAX(constraint->minimumHeight,height);
width = MAX(constraint->minimumWidth,width);
if ((resultResizing != NULL) && [autoSizer respondsToSelector:@selector(verifyAutoresizing:)])
{
*resultResizing = [autoSizer verifyAutoresizing:*resultResizing];
}
return CGSizeMake(width, height);
}
CGPoint PositionConstraintGivenSizeBoundsAddingResizing(LayoutConstraint * constraint, TiViewProxy* viewProxy, CGSize viewSize, CGPoint anchorPoint, CGSize referenceSize, CGSize sandboxSize, UIViewAutoresizing * resultResizing)
{
BOOL flexibleSize = *resultResizing & UIViewAutoresizingFlexibleWidth;
*resultResizing &= ~(UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin);
CGFloat centerX = 0.0f;
BOOL ignoreMargins = NO;
BOOL isSizeUndefined = TiDimensionIsUndefined(constraint->width);
CGFloat frameLeft = 0.0;
if (!flexibleSize) {
if (TiDimensionIsUndefined(constraint->width)) {
ignoreMargins = TiDimensionDidCalculateValue(constraint->centerX, referenceSize.width, ¢erX);
}
else if(!TiDimensionDidCalculateValue(constraint->left, referenceSize.width, &frameLeft))
{
ignoreMargins = TiDimensionDidCalculateValue(constraint->centerX, referenceSize.width, ¢erX);
}
}
if (!ignoreMargins)
{
//Either the view has flexible width or pins were not defined for positioning
int marginSuggestions=0;
if(TiDimensionDidCalculateValue(constraint->left, referenceSize.width, &frameLeft))
{
marginSuggestions++;
}
else if (!flexibleSize)
{
*resultResizing |= UIViewAutoresizingFlexibleLeftMargin;
}
if (isSizeUndefined || (marginSuggestions == 0) || flexibleSize) {
CGFloat frameRight;
if(TiDimensionDidCalculateValue(constraint->right, referenceSize.width, &frameRight))
{
marginSuggestions++;
frameLeft += sandboxSize.width - viewSize.width - frameRight;
}
else if (!flexibleSize)
{
*resultResizing |= UIViewAutoresizingFlexibleRightMargin;
}
}
if (marginSuggestions < 1)
{
centerX = sandboxSize.width/2.0 + viewSize.width*(anchorPoint.x-0.5);
}
else
{
centerX = frameLeft/marginSuggestions + viewSize.width*anchorPoint.x;
}
}
flexibleSize = *resultResizing & UIViewAutoresizingFlexibleHeight;
CGFloat centerY = 0.0f;
isSizeUndefined = TiDimensionIsUndefined(constraint->height);
ignoreMargins = NO;
CGFloat frameTop = 0.0;
if(!flexibleSize) {
if (TiDimensionIsUndefined(constraint->height)) {
ignoreMargins = TiDimensionDidCalculateValue(constraint->centerY, referenceSize.height, ¢erY);
}
else if(!TiDimensionDidCalculateValue(constraint->top, referenceSize.height, &frameTop))
{
ignoreMargins = TiDimensionDidCalculateValue(constraint->centerY, referenceSize.height, ¢erY);;
}
}
if (!ignoreMargins)
{
//Either the view has flexible height or pins were not defined for positioning
int marginSuggestions=0;
if(TiDimensionDidCalculateValue(constraint->top, referenceSize.height, &frameTop))
{
marginSuggestions++;
}
else if (!flexibleSize)
{
*resultResizing |= UIViewAutoresizingFlexibleTopMargin;
}
if (isSizeUndefined || (marginSuggestions == 0) || flexibleSize) {
CGFloat frameBottom;
if(TiDimensionDidCalculateValue(constraint->bottom, referenceSize.height, &frameBottom))
{
marginSuggestions++;
frameTop += sandboxSize.height - viewSize.height - frameBottom;
}
else if (!flexibleSize)
{
*resultResizing |= UIViewAutoresizingFlexibleBottomMargin;
}
}
if (marginSuggestions < 1)
{
centerY = sandboxSize.height/2.0 + viewSize.height*(anchorPoint.y-0.5);
}
else
{
centerY = frameTop/marginSuggestions + viewSize.height*anchorPoint.y;
}
}
return CGPointMake(centerX, centerY);
}
void ApplyConstraintToViewWithBounds(LayoutConstraint * constraint, TiUIView * subView, CGRect viewBounds)
{
if(constraint == NULL)
{
DebugLog(@"[ERROR] No constraints available for view %@.", subView);
return;
}
UIViewAutoresizing resultMask = UIViewAutoresizingNone;
CGRect resultBounds;
resultBounds.origin = CGPointZero;
resultBounds.size = SizeConstraintViewWithSizeAddingResizing(constraint,(TiViewProxy *)[subView proxy], viewBounds.size, &resultMask);
CGPoint resultCenter = PositionConstraintGivenSizeBoundsAddingResizing(constraint, (TiViewProxy *)[subView proxy], resultBounds.size,
[[subView layer] anchorPoint], viewBounds.size, viewBounds.size, &resultMask);
resultCenter.x += resultBounds.origin.x + viewBounds.origin.x;
resultCenter.y += resultBounds.origin.y + viewBounds.origin.y;
[subView setAutoresizingMask:resultMask];
[subView setCenter:resultCenter];
[subView setBounds:resultBounds];
}
CGFloat WidthFromConstraintGivenWidth(LayoutConstraint * constraint, CGFloat viewWidth)
{
switch (constraint->width.type)
{
case TiDimensionTypeDip:
{
return constraint->width.value;
}
case TiDimensionTypePercent:
{
return constraint->width.value * viewWidth;
}
default: {
break;
}
}
return viewWidth - (TiDimensionCalculateValue(constraint->left, viewWidth) + TiDimensionCalculateValue(constraint->right, viewWidth));
}
BOOL IsLayoutUndefined(LayoutConstraint *constraint)
{
// if all values are undefined, the layout is considered undefined.
return TiDimensionIsUndefined(constraint->top)&&
TiDimensionIsUndefined(constraint->left)&&
TiDimensionIsUndefined(constraint->right)&&
TiDimensionIsUndefined(constraint->bottom)&&
TiDimensionIsUndefined(constraint->width)&&
TiDimensionIsUndefined(constraint->height);
}