forked from tidev/titanium-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TiViewProxy.m
2555 lines (2224 loc) · 73.5 KB
/
TiViewProxy.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* 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 "TiViewProxy.h"
#import "LayoutConstraint.h"
#import "TiApp.h"
#import "TiBlob.h"
#import "TiLayoutQueue.h"
#import "TiAction.h"
#import "TiStylesheet.h"
#import "TiLocale.h"
#import "TiUIView.h"
#import <QuartzCore/QuartzCore.h>
#import <libkern/OSAtomic.h>
#import <pthread.h>
#define IGNORE_IF_NOT_OPENED if (!windowOpened||[self viewAttached]==NO) return;
@implementation TiViewProxy
#pragma mark public API
@synthesize vzIndex, parentVisible;
-(void)setVzIndex:(int)newZindex
{
if(newZindex == vzIndex)
{
return;
}
vzIndex = newZindex;
[self replaceValue:NUMINT(vzIndex) forKey:@"vzIndex" notification:NO];
[self willChangeZIndex];
}
@synthesize children;
-(NSArray*)children
{
pthread_rwlock_rdlock(&childrenLock);
if (windowOpened==NO && children==nil && pendingAdds!=nil)
{
NSArray *copy = [pendingAdds mutableCopy];
pthread_rwlock_unlock(&childrenLock);
return [copy autorelease];
}
pthread_rwlock_unlock(&childrenLock);
return children;
}
-(void)setVisible:(NSNumber *)newVisible withObject:(id)args
{
[self setHidden:![TiUtils boolValue:newVisible def:YES] withArgs:args];
[self replaceValue:newVisible forKey:@"visible" notification:YES];
}
-(void)setTempProperty:(id)propVal forKey:(id)propName {
if (layoutPropDictionary == nil) {
layoutPropDictionary = [[NSMutableDictionary alloc] init];
}
if (propVal != nil && propName != nil) {
[layoutPropDictionary setObject:propVal forKey:propName];
}
}
-(void)setProxyObserver:(id)arg
{
observer = arg;
}
-(void)processTempProperties:(NSDictionary*)arg
{
//arg will be non nil when called from updateLayout
if (arg != nil) {
NSEnumerator *enumerator = [arg keyEnumerator];
id key;
while ((key = [enumerator nextObject])) {
[self setTempProperty:[arg objectForKey:key] forKey:key];
}
}
if (layoutPropDictionary != nil) {
[self setValuesForKeysWithDictionary:layoutPropDictionary];
RELEASE_TO_NIL(layoutPropDictionary);
}
}
-(void)startLayout:(id)arg
{
updateStarted = YES;
allowLayoutUpdate = NO;
}
-(void)finishLayout:(id)arg
{
updateStarted = NO;
allowLayoutUpdate = YES;
[self processTempProperties:nil];
allowLayoutUpdate = NO;
}
-(void)updateLayout:(id)arg
{
id val = nil;
if ([arg isKindOfClass:[NSArray class]]) {
val = [arg objectAtIndex:0];
}
else
{
val = arg;
}
updateStarted = NO;
allowLayoutUpdate = YES;
ENSURE_TYPE_OR_NIL(val, NSDictionary);
[self processTempProperties:val];
allowLayoutUpdate = NO;
}
-(void)add:(id)arg
{
// allow either an array of arrays or an array of single proxy
if ([arg isKindOfClass:[NSArray class]])
{
for (id a in arg)
{
[self add:a];
}
return;
}
if ([NSThread isMainThread])
{
pthread_rwlock_wrlock(&childrenLock);
if (children==nil)
{
children = [[NSMutableArray alloc] initWithObjects:arg,nil];
}
else
{
[children addObject:arg];
}
//Turn on clipping because I have children
[self view].clipsToBounds = YES;
pthread_rwlock_unlock(&childrenLock);
[arg setParent:self];
[self contentsWillChange];
if(parentVisible && !hidden)
{
[arg parentWillShow];
}
// only call layout if the view is attached
// Maybe need to call layout children instead for non absolute layout
[self layoutChild:arg optimize:NO withMeasuredBounds:[[self size] rect]];
}
else
{
[self rememberProxy:arg];
if (windowOpened)
{
TiThreadPerformOnMainThread(^{[self add:arg];}, NO);
return;
}
pthread_rwlock_wrlock(&childrenLock);
if (pendingAdds==nil)
{
pendingAdds = [[NSMutableArray arrayWithObject:arg] retain];
}
else
{
[pendingAdds addObject:arg];
}
pthread_rwlock_unlock(&childrenLock);
[arg setParent:self];
}
}
-(void)remove:(id)arg
{
ENSURE_SINGLE_ARG(arg,TiViewProxy);
ENSURE_UI_THREAD_1_ARG(arg);
pthread_rwlock_wrlock(&childrenLock);
if ([children containsObject:arg])
{
[children removeObject:arg];
}
else if ([pendingAdds containsObject:arg])
{
[pendingAdds removeObject:arg];
}
else
{
pthread_rwlock_unlock(&childrenLock);
NSLog(@"[WARN] called remove for %@ on %@, but %@ isn't a child or has already been removed",arg,self,arg);
return;
}
[self contentsWillChange];
if(parentVisible && !hidden)
{
[arg parentWillHide];
}
if ([children count]==0)
{
RELEASE_TO_NIL(children);
}
pthread_rwlock_unlock(&childrenLock);
[arg setParent:nil];
if (view!=nil)
{
TiUIView *childView = [(TiViewProxy *)arg view];
BOOL layoutNeedsRearranging = !TiLayoutRuleIsAbsolute(layoutProperties.layoutStyle);
if ([NSThread isMainThread])
{
[childView removeFromSuperview];
if (layoutNeedsRearranging)
{
[self layoutChildren:NO];
}
}
else
{
TiThreadPerformOnMainThread(^{
[childView removeFromSuperview];
if (layoutNeedsRearranging)
{
[self layoutChildren:NO];
}
}, NO);
}
}
//Yes, we're being really lazy about letting this go. This is intentional.
[self forgetProxy:arg];
}
-(void)show:(id)arg
{
dispatch_async(dispatch_get_main_queue(), ^{
[self setHidden:NO withArgs:arg];
[self replaceValue:NUMBOOL(YES) forKey:@"visible" notification:YES];
});
}
-(void)hide:(id)arg
{
dispatch_async(dispatch_get_main_queue(), ^{
[self setHidden:YES withArgs:arg];
[self replaceValue:NUMBOOL(NO) forKey:@"visible" notification:YES];
});
}
-(void)animate:(id)arg
{
TiAnimation * newAnimation = [TiAnimation animationFromArg:arg context:[self executionContext] create:NO];
[self rememberProxy:newAnimation];
TiThreadPerformOnMainThread(^{
[parent contentsWillChange];
if ([view superview]==nil)
{
VerboseLog(@"Entering animation without a superview Parent is %@, props are %@",parent,dynprops);
[parent childWillResize:self];
}
[self windowWillOpen]; // we need to manually attach the window if you're animating
[parent layoutChildrenIfNeeded];
[[self view] animate:newAnimation];
}, NO);
}
-(void)setAnimation:(id)arg
{ //We don't actually store the animation this way.
//Because the setter doesn't have the argument array, we will be passing a nonarray to animate:
//In this RARE case, this is okay, because TiAnimation animationFromArg handles with or without array.
[self animate:arg];
}
#define CHECK_LAYOUT_UPDATE(layoutName,value) \
if (ENFORCE_BATCH_UPDATE) { \
if (updateStarted) { \
[self setTempProperty:value forKey:@#layoutName]; \
return; \
} \
else if(!allowLayoutUpdate){ \
return; \
} \
}
#define LAYOUTPROPERTIES_SETTER_IGNORES_AUTO(methodName,layoutName,converter,postaction) \
-(void)methodName:(id)value \
{ \
CHECK_LAYOUT_UPDATE(layoutName,value) \
TiDimension result = converter(value);\
if ( TiDimensionIsDip(result) || TiDimensionIsPercent(result) ) {\
layoutProperties.layoutName = result;\
}\
else {\
if (!TiDimensionIsUndefined(result)) {\
NSLog(@"[WARN] Invalid value %@ specified for property %@",[TiUtils stringValue:value],@#layoutName); \
} \
layoutProperties.layoutName = TiDimensionUndefined;\
}\
[self replaceValue:value forKey:@#layoutName notification:YES]; \
postaction; \
}
#define LAYOUTPROPERTIES_SETTER(methodName,layoutName,converter,postaction) \
-(void)methodName:(id)value \
{ \
CHECK_LAYOUT_UPDATE(layoutName,value) \
layoutProperties.layoutName = converter(value); \
[self replaceValue:value forKey:@#layoutName notification:YES]; \
postaction; \
}
LAYOUTPROPERTIES_SETTER_IGNORES_AUTO(setTop,top,TiDimensionFromObject,[self willChangePosition])
LAYOUTPROPERTIES_SETTER_IGNORES_AUTO(setBottom,bottom,TiDimensionFromObject,[self willChangePosition])
LAYOUTPROPERTIES_SETTER_IGNORES_AUTO(setLeft,left,TiDimensionFromObject,[self willChangePosition])
LAYOUTPROPERTIES_SETTER_IGNORES_AUTO(setRight,right,TiDimensionFromObject,[self willChangePosition])
LAYOUTPROPERTIES_SETTER(setWidth,width,TiDimensionFromObject,[self willChangeSize])
LAYOUTPROPERTIES_SETTER(setHeight,height,TiDimensionFromObject,[self willChangeSize])
// See below for how we handle setLayout
//LAYOUTPROPERTIES_SETTER(setLayout,layoutStyle,TiLayoutRuleFromObject,[self willChangeLayout])
LAYOUTPROPERTIES_SETTER(setMinWidth,minimumWidth,TiFixedValueRuleFromObject,[self willChangeSize])
LAYOUTPROPERTIES_SETTER(setMinHeight,minimumHeight,TiFixedValueRuleFromObject,[self willChangeSize])
// Special handling to try and avoid Apple's detection of private API 'layout'
-(void)setValue:(id)value forUndefinedKey:(NSString *)key
{
if ([key isEqualToString:[@"lay" stringByAppendingString:@"out"]]) {
//CAN NOT USE THE MACRO
if (ENFORCE_BATCH_UPDATE) {
if (updateStarted) {
[self setTempProperty:value forKey:key]; \
return;
}
else if(!allowLayoutUpdate){
return;
}
}
layoutProperties.layoutStyle = TiLayoutRuleFromObject(value);
[self replaceValue:value forKey:[@"lay" stringByAppendingString:@"out"] notification:YES];
[self willChangeLayout];
return;
}
[super setValue:value forUndefinedKey:key];
}
-(TiRect*)size
{
TiRect *rect = [[TiRect alloc] init];
if ([self viewAttached]) {
[self makeViewPerformSelector:@selector(fillBoundsToRect:) withObject:rect createIfNeeded:YES waitUntilDone:YES];
}
else {
[rect setRect:CGRectZero];
}
return [rect autorelease];
}
-(TiRect*)rect
{
TiRect *rect = [[TiRect alloc] init];
if ([self viewAttached]) {
__block CGRect viewRect;
__block CGPoint viewPosition;
__block CGAffineTransform viewTransform;
__block CGPoint viewAnchor;
TiThreadPerformOnMainThread(^{
TiUIView * ourView = [self view];
viewRect = [ourView bounds];
viewPosition = [ourView center];
viewTransform = [ourView transform];
viewAnchor = [[ourView layer] anchorPoint];
}, YES);
viewRect.origin = CGPointMake(-viewAnchor.x*viewRect.size.width, -viewAnchor.y*viewRect.size.height);
viewRect = CGRectApplyAffineTransform(viewRect, viewTransform);
viewRect.origin.x += viewPosition.x;
viewRect.origin.y += viewPosition.y;
[rect setRect:viewRect];
}
else {
[rect setRect:CGRectZero];
}
return [rect autorelease];
}
-(id)zIndex
{
return [self valueForUndefinedKey:@"zindex_"];
}
-(void)setZIndex:(id)value
{
CHECK_LAYOUT_UPDATE(zIndex, value);
if ([value respondsToSelector:@selector(intValue)]) {
[self setVzIndex:[TiUtils intValue:value]];
[self replaceValue:value forKey:@"zindex_" notification:NO];
}
}
-(NSMutableDictionary*)center
{
NSMutableDictionary* result = [[[NSMutableDictionary alloc] init] autorelease];
id xVal = [self valueForUndefinedKey:@"centerX_"];
if (xVal != nil) {
[result setObject:xVal forKey:@"x"];
}
id yVal = [self valueForUndefinedKey:@"centerY_"];
if (yVal != nil) {
[result setObject:yVal forKey:@"y"];
}
if ([[result allKeys] count] > 0) {
return result;
}
return nil;
}
-(void)setCenter:(id)value
{
CHECK_LAYOUT_UPDATE(center, value);
if ([value isKindOfClass:[NSDictionary class]])
{
TiDimension result;
id obj = [value objectForKey:@"x"];
if (obj != nil) {
[self replaceValue:obj forKey:@"centerX_" notification:NO];
result = TiDimensionFromObject(obj);
if ( TiDimensionIsDip(result) || TiDimensionIsPercent(result) ) {
layoutProperties.centerX = result;
}
else {
layoutProperties.centerX = TiDimensionUndefined;
}
}
obj = [value objectForKey:@"y"];
if (obj != nil) {
[self replaceValue:obj forKey:@"centerY_" notification:NO];
result = TiDimensionFromObject(obj);
if ( TiDimensionIsDip(result) || TiDimensionIsPercent(result) ) {
layoutProperties.centerY = result;
}
else {
layoutProperties.centerY = TiDimensionUndefined;
}
}
} else if ([value isKindOfClass:[TiPoint class]]) {
CGPoint p = [value point];
layoutProperties.centerX = TiDimensionDip(p.x);
layoutProperties.centerY = TiDimensionDip(p.y);
} else {
layoutProperties.centerX = TiDimensionUndefined;
layoutProperties.centerY = TiDimensionUndefined;
}
[self willChangePosition];
}
-(id)animatedCenter
{
if (![self viewAttached])
{
return nil;
}
__block CGPoint result;
TiThreadPerformOnMainThread(^{
UIView * ourView = view;
CALayer * ourLayer = [ourView layer];
CALayer * animatedLayer = [ourLayer presentationLayer];
if (animatedLayer !=nil) {
result = [animatedLayer position];
}
else {
result = [ourLayer position];
}
}, YES);
//TODO: Should this be a TiPoint? If so, the accessor fetcher might try to
//hold onto the point, which is undesired.
return [NSDictionary dictionaryWithObjectsAndKeys:NUMFLOAT(result.x),@"x",NUMFLOAT(result.y),@"y", nil];
}
-(void)setBackgroundGradient:(id)arg
{
TiGradient * newGradient = [TiGradient gradientFromObject:arg proxy:self];
[self replaceValue:newGradient forKey:@"backgroundGradient" notification:YES];
}
-(TiBlob*)toImage:(id)args
{
KrollCallback *callback = nil;
BOOL honorScale = NO;
NSObject *obj = nil;
if( [args count] > 0) {
obj = [args objectAtIndex:0];
if (obj == [NSNull null]) {
obj = nil;
}
if( [args count] > 1) {
honorScale = [TiUtils boolValue:[args objectAtIndex:1] def:NO];
}
}
callback = (KrollCallback*)obj;
TiBlob *blob = [[[TiBlob alloc] init] autorelease];
// we spin on the UI thread and have him convert and then add back to the blob
// if you pass a callback function, we'll run the render asynchronously, if you
// don't, we'll do it synchronously
TiThreadPerformOnMainThread(^{
[self windowWillOpen];
TiUIView *myview = [self view];
CGSize size = myview.bounds.size;
if (CGSizeEqualToSize(size, CGSizeZero) || size.width==0 || size.height==0)
{
CGFloat width = [self autoWidthForSize:CGSizeMake(1000,1000)];
CGFloat height = [self autoHeightForSize:CGSizeMake(width,1000)];
if (width > 0 && height > 0)
{
size = CGSizeMake(width, height);
}
if (CGSizeEqualToSize(size, CGSizeZero) || width==0 || height == 0)
{
size = [UIScreen mainScreen].bounds.size;
}
CGRect rect = CGRectMake(0, 0, size.width, size.height);
[TiUtils setView:myview positionRect:rect];
}
UIGraphicsBeginImageContextWithOptions(size, [myview.layer isOpaque], (honorScale ? 0.0 : 1.0));
[myview.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
[blob setImage:image];
[blob setMimeType:@"image/png" type:TiBlobTypeImage];
UIGraphicsEndImageContext();
if (callback != nil)
{
NSDictionary *event = [NSDictionary dictionaryWithObject:blob forKey:@"blob"];
[self _fireEventToListener:@"blob" withObject:event listener:callback thisObject:nil];
}
}, (callback==nil));
return blob;
}
-(TiPoint*)convertPointToView:(id)args
{
id arg1 = nil;
TiViewProxy* arg2 = nil;
ENSURE_ARG_AT_INDEX(arg1, args, 0, NSObject);
ENSURE_ARG_AT_INDEX(arg2, args, 1, TiViewProxy);
BOOL validPoint;
CGPoint oldPoint = [TiUtils pointValue:arg1 valid:&validPoint];
if (!validPoint) {
[self throwException:TiExceptionInvalidType subreason:@"Parameter is not convertable to a TiPoint" location:CODELOCATION];
}
__block BOOL validView = NO;
__block CGPoint p;
dispatch_sync(dispatch_get_main_queue(), ^{
if ([self viewAttached] && self.view.window && [arg2 viewAttached] && arg2.view.window) {
validView = YES;
p = [self.view convertPoint:oldPoint toView:arg2.view];
}
});
if (!validView) {
return (TiPoint*)[NSNull null];
}
return [[[TiPoint alloc] initWithPoint:p] autorelease];
}
#pragma mark nonpublic accessors not related to Housecleaning
@synthesize parent, barButtonItem;
-(void)setParent:(TiViewProxy*)parent_
{
parent = parent_;
if (parent_!=nil && [parent windowHasOpened])
{
[self windowWillOpen];
}
}
-(LayoutConstraint *)layoutProperties
{
return &layoutProperties;
}
@synthesize sandboxBounds;
-(void)setHidden:(BOOL)newHidden withArgs:(id)args
{
if(hidden == newHidden)
{
return;
}
hidden = newHidden;
//TODO: If we have an animated show, hide, or setVisible, here's the spot for it.
if(parentVisible)
{
if (hidden)
{
[self willHide];
}
else
{
[self willShow];
}
}
}
-(CGFloat)autoWidthForSize:(CGSize)size
{
CGFloat suggestedWidth = size.width;
//This is the content width, which is implemented by widgets
CGFloat contentWidth = -1.0;
if ([self respondsToSelector:@selector(contentWidthForWidth:)]) {
contentWidth = [self contentWidthForWidth:suggestedWidth];
}
BOOL isHorizontal = TiLayoutRuleIsHorizontal(layoutProperties.layoutStyle);
CGFloat result = 0.0;
CGRect bounds = CGRectZero;
if (isHorizontal) {
bounds.size.width = size.width;
bounds.size.height = size.height;
verticalLayoutBoundary = 0;
horizontalLayoutBoundary = 0;
horizontalLayoutRowHeight = 0;
}
CGRect sandBox = CGRectZero;
CGFloat thisWidth = 0.0;
pthread_rwlock_rdlock(&childrenLock);
for (TiViewProxy * thisChildProxy in self.children)
{
if (isHorizontal) {
sandBox = CGRectZero;
sandBox = [self computeChildSandbox:thisChildProxy withBounds:bounds];
thisWidth = sandBox.origin.x + sandBox.size.width;
}
else {
thisWidth = [thisChildProxy minimumParentWidthForSize:size];
}
if(result<thisWidth)
{
result = thisWidth;
}
}
pthread_rwlock_unlock(&childrenLock);
if (result < contentWidth) {
result = contentWidth;
}
if([self respondsToSelector:@selector(verifyWidth:)])
{
result = [self verifyWidth:result];
}
if (result == 0)
{
NSLog(@"[WARN] %@ has an auto width value of 0, meaning this view may not be visible.",self);
}
return result;
}
-(CGFloat)autoHeightForSize:(CGSize)size
{
CGFloat width = size.width;
//This is the content width, which is implemented by widgets
CGFloat contentHeight = -1.0;
if ([self respondsToSelector:@selector(contentHeightForWidth:)]) {
contentHeight = [self contentHeightForWidth:width];
}
BOOL isAbsolute = TiLayoutRuleIsAbsolute(layoutProperties.layoutStyle);
CGFloat result=0.0;
CGRect bounds = CGRectZero;
if (!isAbsolute) {
bounds.size.width = size.width;
bounds.size.height = size.height;
verticalLayoutBoundary = 0;
horizontalLayoutBoundary = 0;
horizontalLayoutRowHeight = 0;
}
CGRect sandBox = CGRectZero;
CGFloat thisHeight = 0.0;
pthread_rwlock_rdlock(&childrenLock);
NSArray* array = windowOpened ? children : pendingAdds;
for (TiViewProxy * thisChildProxy in array)
{
if (!isAbsolute) {
sandBox = CGRectZero;
sandBox = [self computeChildSandbox:thisChildProxy withBounds:bounds];
thisHeight = sandBox.origin.y + sandBox.size.height;
}
else {
thisHeight = [thisChildProxy minimumParentHeightForSize:size];
}
if(result<thisHeight)
{
result = thisHeight;
}
}
pthread_rwlock_unlock(&childrenLock);
//result += currentRowHeight;
if (result < contentHeight) {
result = contentHeight;
}
if([self respondsToSelector:@selector(verifyHeight:)])
{
result = [self verifyHeight:result];
}
if (result == 0)
{
NSLog(@"[WARN] %@ has an auto height value of 0, meaning this view may not be visible.",self);
}
return result;
}
-(CGFloat)minimumParentWidthForSize:(CGSize)size
{
CGFloat suggestedWidth = size.width;
BOOL followsFillBehavior = TiDimensionIsAutoFill([self defaultAutoWidthBehavior:nil]);
BOOL recheckForFill = NO;
CGFloat offset = TiDimensionCalculateValue(layoutProperties.left, size.width)
+ TiDimensionCalculateValue(layoutProperties.right, size.width);
CGFloat offset2 = TiDimensionCalculateValue(layoutProperties.top, size.height)
+ TiDimensionCalculateValue(layoutProperties.bottom, size.height);
CGFloat result = offset;
if (TiDimensionIsDip(layoutProperties.width) || TiDimensionIsPercent(layoutProperties.width))
{
result += TiDimensionCalculateValue(layoutProperties.width, suggestedWidth);
}
else if (TiDimensionIsAutoFill(layoutProperties.width) || (TiDimensionIsAuto(layoutProperties.width) && followsFillBehavior) )
{
recheckForFill = YES;
result += [self autoWidthForSize:CGSizeMake(size.width - offset, size.height - offset2)];
}
else if (TiDimensionIsUndefined(layoutProperties.width))
{
if (!TiDimensionIsUndefined(layoutProperties.left) && !TiDimensionIsUndefined(layoutProperties.centerX) ) {
result += 2 * ( TiDimensionCalculateValue(layoutProperties.centerX, suggestedWidth) - TiDimensionCalculateValue(layoutProperties.left, suggestedWidth) );
}
else if (!TiDimensionIsUndefined(layoutProperties.left) && !TiDimensionIsUndefined(layoutProperties.right) ) {
result += TiDimensionCalculateMargins(layoutProperties.left, layoutProperties.right, suggestedWidth);
}
else if (!TiDimensionIsUndefined(layoutProperties.centerX) && !TiDimensionIsUndefined(layoutProperties.right) ) {
result += 2 * ( size.width - TiDimensionCalculateValue(layoutProperties.right, suggestedWidth) - TiDimensionCalculateValue(layoutProperties.centerX, suggestedWidth));
}
else {
recheckForFill = followsFillBehavior;
result += [self autoWidthForSize:CGSizeMake(size.width - offset, size.height - offset2)];
}
}
else
{
result += [self autoWidthForSize:CGSizeMake(size.width - offset, size.height - offset2)];
}
if (recheckForFill && (result < suggestedWidth) ) {
result = suggestedWidth;
}
return result;
}
-(CGFloat)minimumParentHeightForSize:(CGSize)size
{
CGFloat suggestedHeight = size.height;
BOOL followsFillBehavior = TiDimensionIsAutoFill([self defaultAutoHeightBehavior:nil]);
BOOL recheckForFill = NO;
CGFloat offset = TiDimensionCalculateValue(layoutProperties.left, size.width)
+ TiDimensionCalculateValue(layoutProperties.right, size.width);
CGFloat offset2 = TiDimensionCalculateValue(layoutProperties.top, suggestedHeight)
+ TiDimensionCalculateValue(layoutProperties.bottom, suggestedHeight);
CGFloat result = offset2;
if (TiDimensionIsDip(layoutProperties.height) || TiDimensionIsPercent(layoutProperties.height)) {
result += TiDimensionCalculateValue(layoutProperties.height, suggestedHeight);
}
else if (TiDimensionIsAutoFill(layoutProperties.height) || (TiDimensionIsAuto(layoutProperties.height) && followsFillBehavior) )
{
recheckForFill = YES;
result += [self autoHeightForSize:CGSizeMake(size.width - offset, size.height - offset2)];
}
else if (TiDimensionIsUndefined(layoutProperties.height))
{
if (!TiDimensionIsUndefined(layoutProperties.top) && !TiDimensionIsUndefined(layoutProperties.centerY) ) {
result += 2 * ( TiDimensionCalculateValue(layoutProperties.centerY, suggestedHeight) - TiDimensionCalculateValue(layoutProperties.top, suggestedHeight) );
}
else if (!TiDimensionIsUndefined(layoutProperties.top) && !TiDimensionIsUndefined(layoutProperties.bottom) ) {
result += TiDimensionCalculateMargins(layoutProperties.top, layoutProperties.bottom, suggestedHeight);
}
else if (!TiDimensionIsUndefined(layoutProperties.centerY) && !TiDimensionIsUndefined(layoutProperties.bottom) ) {
result += 2 * ( suggestedHeight - TiDimensionCalculateValue(layoutProperties.bottom, suggestedHeight) - TiDimensionCalculateValue(layoutProperties.centerY, suggestedHeight));
}
else {
recheckForFill = followsFillBehavior;
result += [self autoHeightForSize:CGSizeMake(size.width - offset, size.height - offset2)];
}
}
else
{
result += [self autoHeightForSize:CGSizeMake(size.width - offset, size.height - offset2)];
}
if (recheckForFill && (result < suggestedHeight) ) {
result = suggestedHeight;
}
return result;
}
-(UIBarButtonItem*)barButtonItem
{
if (barButtonItem == nil)
{
isUsingBarButtonItem = YES;
barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:[self barButtonViewForSize:CGSizeZero]];
}
return barButtonItem;
}
- (TiUIView *)barButtonViewForSize:(CGSize)bounds
{
TiUIView * barButtonView = [self view];
//TODO: This logic should have a good place in case that refreshLayout is used.
LayoutConstraint barButtonLayout = layoutProperties;
if (TiDimensionIsUndefined(barButtonLayout.width))
{
barButtonLayout.width = TiDimensionAutoSize;
}
if (TiDimensionIsUndefined(barButtonLayout.height))
{
barButtonLayout.height = TiDimensionAutoSize;
}
if ( (bounds.width == 0) && !(TiDimensionIsDip(barButtonLayout.width) ) ) {
bounds.width = [self autoWidthForSize:CGSizeMake(1000, 1000)];
}
if ( (bounds.height == 0) && !(TiDimensionIsDip(barButtonLayout.height) ) ) {
bounds.height = [self autoHeightForSize:CGSizeMake(bounds.width, 1000)];
}
CGRect barBounds;
barBounds.origin = CGPointZero;
barBounds.size = SizeConstraintViewWithSizeAddingResizing(&barButtonLayout, self, bounds, NULL);
[TiUtils setView:barButtonView positionRect:barBounds];
[barButtonView setAutoresizingMask:UIViewAutoresizingNone];
//Ensure all the child views are laid out as well
[self windowWillOpen];
[self setParentVisible:YES];
[self willShow];
return barButtonView;
}
#pragma mark Recognizers
-(void)recognizedPinch:(UIPinchGestureRecognizer*)recognizer
{
NSDictionary *event = [NSDictionary dictionaryWithObjectsAndKeys:
NUMDOUBLE(recognizer.scale), @"scale",
NUMDOUBLE(recognizer.velocity), @"velocity",
nil];
[self fireEvent:@"pinch" withObject:event];
}
-(void)recognizedLongPress:(UILongPressGestureRecognizer*)recognizer
{
if ([recognizer state] == UIGestureRecognizerStateBegan) {
CGPoint p = [recognizer locationInView:self.view];
NSDictionary *event = [NSDictionary dictionaryWithObjectsAndKeys:
NUMFLOAT(p.x), @"x",
NUMFLOAT(p.y), @"y",
nil];
[self fireEvent:@"longpress" withObject:event];
}
}
-(TiUIView*)view
{
if (view == nil)
{
WARN_IF_BACKGROUND_THREAD_OBJ
#ifdef VERBOSE
if(![NSThread isMainThread])
{
NSLog(@"[WARN] Break here");
}
#endif
// on open we need to create a new view
[self viewWillAttach];
view = [self newView];
// check listeners dictionary to see if we need gesture recognizers
if ([self _hasListeners:@"pinch"]) {
UIPinchGestureRecognizer* r = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(recognizedPinch:)];
[view addGestureRecognizer:r];
[r release];
}
if ([self _hasListeners:@"longpress"]) {
UILongPressGestureRecognizer* r = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(recognizedLongPress:)];
[view addGestureRecognizer:r];
[r release];
}
view.proxy = self;
view.layer.transform = CATransform3DIdentity;
view.transform = CGAffineTransformIdentity;
[view initializeState];
// fire property changes for all properties to our delegate
[self firePropertyChanges];
[view configurationSet];
pthread_rwlock_rdlock(&childrenLock);
NSArray * childrenArray = [[self children] copy];
pthread_rwlock_unlock(&childrenLock);
for (id child in childrenArray)
{
TiUIView *childView = [(TiViewProxy*)child view];
[self insertSubview:childView forProxy:child];
}
[childrenArray release];
[self viewDidAttach];
// make sure we do a layout of ourselves
if(CGRectIsEmpty(sandboxBounds) && (view != nil)){
[self setSandboxBounds:view.bounds];
}
[self relayout];
viewInitialized = YES;
}
CGRect bounds = [view bounds];
if (!CGPointEqualToPoint(bounds.origin, CGPointZero))
{
[view setBounds:CGRectMake(0, 0, bounds.size.width, bounds.size.height)];
}
return view;
}
//CAUTION: TO BE USED ONLY WITH TABLEVIEW MAGIC
-(void)setView:(TiUIView *)newView
{
if (view != newView) {
[view removeFromSuperview];
[view release];
view = [newView retain];
}