forked from tidev/titanium-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TiUIWindowProxy.m
1015 lines (886 loc) · 31.1 KB
/
TiUIWindowProxy.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-2014 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.
*/
#ifdef USE_TI_UIWINDOW
#import "TiUIWindowProxy.h"
#import "Webcolor.h"
#import "TiUIViewProxy.h"
#import "ImageLoader.h"
#import "TiComplexValue.h"
#import "TiApp.h"
#import "TiLayoutQueue.h"
// this is how long we should wait on the new JS context to be loaded
// holding the UI thread before we return during an window open. we
// attempt to hold it for a small period of time to allow the window
// to loaded before we return from the open such that the paint will be
// much smoother on the new window during a tab transition
#define EXTERNAL_JS_WAIT_TIME (150/1000)
/**
* This class is a helper that will be used when we have an external
* window w/ JS so that we can attempt to wait for the window context
* to be fully loaded on the UI thread (since JS runs in a different
* thread) and attempt to wait up til EXTERNAL_JS_WAIT_TIME before
* timing out. If timed out, will go ahead and start opening the window
* and as the JS context finishes, will continue opening from there -
* this has a nice effect of immediately opening if fast but not delaying
* if slow (so you get weird button delay effects for example)
*
*/
@interface TiUIWindowProxyLatch : NSObject
{
NSCondition *lock;
TiUIWindowProxy* window;
id args;
BOOL completed;
BOOL timeout;
}
@end
@implementation TiUIWindowProxyLatch
-(id)initWithTiWindow:(id)window_ args:(id)args_
{
if (self = [super init])
{
window = [window_ retain];
args = [args_ retain];
lock = [[NSCondition alloc] init];
}
return self;
}
-(void)dealloc
{
RELEASE_TO_NIL(lock);
RELEASE_TO_NIL(window);
RELEASE_TO_NIL(args);
[super dealloc];
}
-(void)booted:(id)arg
{
[lock lock];
completed = YES;
[lock signal];
if (timeout)
{
[window boot:YES args:args];
}
[lock unlock];
}
-(BOOL)waitForBoot
{
BOOL yn = NO;
[lock lock];
if(completed)
{
yn = YES;
}
else
{
if ([lock waitUntilDate:[NSDate dateWithTimeIntervalSinceNow:EXTERNAL_JS_WAIT_TIME]]==NO)
{
timeout = YES;
}
else
{
yn = YES;
}
}
[lock unlock];
return yn;
}
@end
@implementation TiUIWindowProxy
-(void)_destroy
{
if (!closing && opened) {
TiThreadPerformOnMainThread(^{[self close:nil];}, YES);
}
#ifdef TI_USE_KROLL_THREAD
TiThreadRemoveFromSuperviewOnMainThread(barImageView, NO);
TiThreadReleaseOnMainThread(barImageView, NO);
barImageView = nil;
#else
TiThreadPerformOnMainThread(^{
[barImageView removeFromSuperview];
RELEASE_TO_NIL(barImageView);
}, YES);
#endif
if (context!=nil)
{
[context shutdown:nil];
RELEASE_TO_NIL(context);
}
RELEASE_TO_NIL(oldBaseURL);
RELEASE_TO_NIL(latch);
[super _destroy];
}
-(void)_configure
{
[self replaceValue:nil forKey:@"barColor" notification:NO];
[self replaceValue:nil forKey:@"navTintColor" notification:NO];
[self replaceValue:nil forKey:@"barImage" notification:NO];
[self replaceValue:nil forKey:@"translucent" notification:NO];
[self replaceValue:nil forKey:@"titleAttributes" notification:NO];
[self replaceValue:NUMBOOL(NO) forKey:@"tabBarHidden" notification:NO];
[self replaceValue:NUMBOOL(NO) forKey:@"navBarHidden" notification:NO];
[self replaceValue:NUMBOOL(NO) forKey:@"hidesBarsOnSwipe" notification:NO];
[self replaceValue:NUMBOOL(NO) forKey:@"hidesBarsOnTap" notification:NO];
[self replaceValue:NUMBOOL(NO) forKey:@"hidesBarsWhenKeyboardAppears" notification:NO];
[super _configure];
}
-(NSString*)apiName
{
return @"Ti.UI.Window";
}
-(void)dealloc
{
RELEASE_TO_NIL(barImageView);
[super dealloc];
}
-(void)boot:(BOOL)timeout args:args
{
RELEASE_TO_NIL(latch);
if (timeout) {
if (![context evaluationError]) {
contextReady = YES;
[self open:args];
} else {
DebugLog(@"Could not boot context. Context has evaluation error");
}
}
}
-(BOOL)optimizeSubviewInsertion
{
return YES;
}
-(NSMutableDictionary*)langConversionTable
{
return [NSMutableDictionary dictionaryWithObjectsAndKeys:@"title",@"titleid",@"titlePrompt",@"titlepromptid",nil];
}
#pragma mark - TiWindowProtocol overrides
-(UIViewController*)hostingController;
{
if (controller == nil) {
UIViewController* theController = [super hostingController];
[theController setHidesBottomBarWhenPushed:[TiUtils boolValue:[self valueForUndefinedKey:@"tabBarHidden"] def:NO]];
return theController;
}
return [super hostingController];
}
-(BOOL)_handleOpen:(id)args
{
// this is a special case that calls open again above to cause the event lifecycle to
// happen after the JS context is fully up and ready
if (contextReady && context!=nil)
{
return [super _handleOpen:args];
}
//
// at this level, open is top-level since this is a window. if you want
// to open a window within a tab, you'll need to call tab.open(window)
//
NSURL *url = [TiUtils toURL:[self valueForKey:@"url"] proxy:self];
if (url != nil) {
DEPRECATED_REMOVED(@"UI.Window.url", @"2.0.0", @"6.0.0");
DebugLog(@"[ERROR] Please use require() to manage your application components.");
DebugLog(@"[ERROR] More infos: http://docs.appcelerator.com/platform/latest/#!/guide/CommonJS_Modules_in_Titanium");
}
return [super _handleOpen:args];
}
-(void) windowDidClose
{
// Because other windows or proxies we have open and wish to continue functioning might be relying
// on our created context, we CANNOT explicitly shut down here. Instead we should memory-manage
// contexts better so they stop when they're no longer in use.
// Sadly, today is not that day. Without shutdown, we leak all over the place.
if (context!=nil) {
NSMutableArray* childrenToRemove = [[NSMutableArray alloc] init];
pthread_rwlock_rdlock(&childrenLock);
for (TiViewProxy* child in children) {
if ([child belongsToContext:context]) {
[childrenToRemove addObject:child];
}
}
pthread_rwlock_unlock(&childrenLock);
[context performSelector:@selector(shutdown:) withObject:nil afterDelay:1.0];
RELEASE_TO_NIL(context);
for (TiViewProxy* child in childrenToRemove) {
[self remove:child];
}
[childrenToRemove release];
}
[super windowDidClose];
}
-(BOOL)_handleClose:(id)args
{
if (oldBaseURL!=nil)
{
[self _setBaseURL:oldBaseURL];
}
RELEASE_TO_NIL(oldBaseURL);
return [super _handleClose:args];
}
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator
{
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
[self willChangeSize];
}
- (void)systemLayoutFittingSizeDidChangeForChildContentContainer:(id <UIContentContainer>)container
{
[super systemLayoutFittingSizeDidChangeForChildContentContainer:container];
}
- (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator
{
[super willTransitionToTraitCollection:newCollection withTransitionCoordinator:coordinator];
}
- (void)preferredContentSizeDidChangeForChildContentContainer:(id <UIContentContainer>)container
{
[super preferredContentSizeDidChangeForChildContentContainer:container];
}
- (void)viewWillAppear:(BOOL)animated; // Called when the view is about to made visible. Default does nothing
{
shouldUpdateNavBar = YES;
[self setupWindowDecorations];
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated; // Called when the view has been fully transitioned onto the screen. Default does nothing
{
[self updateTitleView];
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated; // Called when the view is dismissed, covered or otherwise hidden. Default does nothing
{
shouldUpdateNavBar = NO;
[self cleanupWindowDecorations];
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated; // Called after the view was dismissed, covered or otherwise hidden. Default does nothing
{
[super viewDidDisappear:animated];
}
#pragma mark - UINavController, NavItem UI
-(void)setNavTintColor:(id)colorString
{
NSString *color = [TiUtils stringValue:colorString];
[self replaceValue:color forKey:@"navTintColor" notification:NO];
TiThreadPerformOnMainThread(^{
if(controller != nil) {
TiColor * newColor = [TiUtils colorValue:color];
if (newColor == nil) {
//Get from TabGroup
newColor = [TiUtils colorValue:[[self tabGroup] valueForKey:@"navTintColor"]];
}
UINavigationBar * navBar = [[controller navigationController] navigationBar];
[navBar setTintColor:[newColor color]];
[self performSelector:@selector(refreshBackButton) withObject:nil afterDelay:0.0];
}
}, NO);
}
-(void)setBarColor:(id)colorString
{
ENSURE_UI_THREAD(setBarColor,colorString);
NSString *color = [TiUtils stringValue:colorString];
[self replaceValue:color forKey:@"barColor" notification:NO];
if (shouldUpdateNavBar && controller!=nil && [controller navigationController] != nil)
{
TiColor * newColor = [TiUtils colorValue:color];
if (newColor == nil)
{
newColor =[TiUtils colorValue:[[self tabGroup] valueForKey:@"barColor"]];
}
UIColor * barColor = [TiUtils barColorForColor:newColor];
UIBarStyle navBarStyle = [TiUtils barStyleForColor:newColor];
UINavigationBar * navBar = [[controller navigationController] navigationBar];
[navBar setBarStyle:navBarStyle];
[navBar setBarTintColor:barColor];
[self performSelector:@selector(refreshBackButton) withObject:nil afterDelay:0.0];
}
}
-(void)setTitleAttributes:(id)args
{
ENSURE_UI_THREAD(setTitleAttributes,args);
ENSURE_SINGLE_ARG_OR_NIL(args, NSDictionary);
[self replaceValue:args forKey:@"titleAttributes" notification:NO];
if (args == nil) {
args = [[self tabGroup] valueForUndefinedKey:@"titleAttributes"];
}
NSMutableDictionary* theAttributes = nil;
if (args != nil) {
theAttributes = [NSMutableDictionary dictionary];
if ([args objectForKey:@"color"] != nil) {
UIColor* theColor = [[TiUtils colorValue:@"color" properties:args] _color];
if (theColor != nil) {
[theAttributes setObject:theColor forKey:NSForegroundColorAttributeName];
}
}
if ([args objectForKey:@"shadow"] != nil) {
NSShadow* shadow = [TiUtils shadowValue:[args objectForKey:@"shadow"]];
if (shadow != nil) {
[theAttributes setObject:shadow forKey:NSShadowAttributeName];
}
}
if ([args objectForKey:@"font"] != nil) {
UIFont* theFont = [[TiUtils fontValue:[args objectForKey:@"font"] def:nil] font];
if (theFont != nil) {
[theAttributes setObject:theFont forKey:NSFontAttributeName];
}
}
if ([theAttributes count] == 0) {
theAttributes = nil;
}
}
if (shouldUpdateNavBar && ([controller navigationController] != nil)) {
#if IS_XCODE_9
if ([TiUtils isIOS11OrGreater] && [TiUtils boolValue:[self valueForKey:@"largeTitleEnabled"] def:NO]) {
[[[controller navigationController] navigationBar] setLargeTitleTextAttributes:theAttributes];
}
#endif
[[[controller navigationController] navigationBar] setTitleTextAttributes:theAttributes];
}
}
-(void)updateBarImage
{
if (controller == nil || [controller navigationController] == nil || !shouldUpdateNavBar) {
return;
}
id barImageValue = [self valueForUndefinedKey:@"barImage"];
UINavigationBar* ourNB = [[controller navigationController] navigationBar];
UIImage* theImage = nil;
theImage = [TiUtils toImage:barImageValue proxy:self];
if (theImage == nil) {
[ourNB setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
} else {
UIImage* resizableImage = [theImage resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0) resizingMode:UIImageResizingModeStretch];
[ourNB setBackgroundImage:resizableImage forBarMetrics:UIBarMetricsDefault];
//You can only set up the shadow image with a custom background image.
id shadowImageValue = [self valueForUndefinedKey:@"shadowImage"];
theImage = [TiUtils toImage:shadowImageValue proxy:self];
if (theImage != nil) {
UIImage* resizableImage = [theImage resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0) resizingMode:UIImageResizingModeStretch];
ourNB.shadowImage = resizableImage;
} else {
BOOL clipValue = [TiUtils boolValue:[self valueForUndefinedKey:@"hideShadow"] def:NO];
if (clipValue) {
//Set an empty Image.
ourNB.shadowImage = [[[UIImage alloc] init] autorelease];
} else {
ourNB.shadowImage = nil;
}
}
}
}
-(void)setBarImage:(id)value
{
[self replaceValue:value forKey:@"barImage" notification:NO];
if (controller!=nil)
{
TiThreadPerformOnMainThread(^{[self updateBarImage];}, NO);
}
}
-(void)setShadowImage:(id)value
{
[self replaceValue:value forKey:@"shadowImage" notification:NO];
if (controller!=nil)
{
TiThreadPerformOnMainThread(^{[self updateBarImage];}, NO);
}
}
-(void)setHideShadow:(id)value
{
[self replaceValue:value forKey:@"hideShadow" notification:NO];
if (controller!=nil)
{
TiThreadPerformOnMainThread(^{[self updateBarImage];}, NO);
}
}
-(void)setTranslucent:(id)value
{
ENSURE_UI_THREAD(setTranslucent,value);
[self replaceValue:value forKey:@"translucent" notification:NO];
if (controller!=nil)
{
[controller navigationController].navigationBar.translucent = [TiUtils boolValue:value def:YES];
}
}
-(void)updateNavButtons
{
//Update LeftNavButton
NSDictionary* lProperties = [self valueForUndefinedKey:@"leftNavSettings"];
id leftNavButtons = [self valueForUndefinedKey:@"leftNavButtons"];
if (!IS_NULL_OR_NIL(leftNavButtons)) {
[self setLeftNavButtons:leftNavButtons withObject:lProperties];
} else {
leftNavButtons = [self valueForUndefinedKey:@"leftNavButton"];
[self setLeftNavButton:leftNavButtons withObject:lProperties];
}
//Update RightNavButton
NSDictionary* rProperties = [self valueForUndefinedKey:@"rightNavSettings"];
id rightNavButtons = [self valueForUndefinedKey:@"rightNavButtons"];
if (!IS_NULL_OR_NIL(rightNavButtons)) {
[self setRightNavButtons:rightNavButtons withObject:rProperties];
} else {
rightNavButtons = [self valueForUndefinedKey:@"rightNavButton"];
[self setRightNavButton:rightNavButtons withObject:rProperties];
}
}
-(void)refreshRightNavButtons:(id)unused
{
if (controller == nil || [controller navigationController] == nil) {
return; // No need to refresh
}
NSArray* theObjects = [self valueForUndefinedKey:@"rightNavButtons"];
NSDictionary* theProperties = [self valueForUndefinedKey:@"rightNavSettings"];
ENSURE_TYPE_OR_NIL(theObjects, NSArray);
ENSURE_TYPE_OR_NIL(theProperties, NSDictionary);
NSMutableArray* theItems = [[NSMutableArray alloc] init];
for (TiViewProxy* theProxy in theObjects) {
if ([theProxy supportsNavBarPositioning]) {
[theItems addObject:[theProxy barButtonItem]];
} else {
DebugLog(@"%@ does not support nav bar positioning", theProxy);
}
}
BOOL animated = [TiUtils boolValue:@"animated" properties:theProperties def:NO];
if ([theItems count] > 0) {
[controller.navigationItem setRightBarButtonItems:theItems animated:animated];
} else {
[controller.navigationItem setRightBarButtonItems:nil animated:animated];
}
[theItems release];
}
-(void)setRightNavButtons:(id)arg withObject:(id)properties
{
ENSURE_TYPE_OR_NIL(arg, NSArray);
ENSURE_TYPE_OR_NIL(properties, NSDictionary);
NSArray* curValues = [self valueForUndefinedKey:@"rightNavButtons"];
ENSURE_TYPE_OR_NIL(curValues, NSArray);
//Clean up current values
for (TiViewProxy* curProxy in curValues) {
if (![(NSArray*)arg containsObject:curProxy]) {
[curProxy removeBarButtonView];
[self forgetProxy:curProxy];
}
}
for(TiViewProxy* proxy in arg) {
if([proxy isKindOfClass:[TiViewProxy class]]) {
[self rememberProxy:proxy];
}
}
[self replaceValue:arg forKey:@"rightNavButtons" notification:NO];
[self replaceValue:properties forKey:@"rightNavSettings" notification:NO];
TiThreadPerformOnMainThread(^{
[self refreshRightNavButtons:nil];
}, NO);
}
-(void)setRightNavButton:(id)proxy withObject:(id)properties
{
[self replaceValue:proxy forKey:@"rightNavButton" notification:NO];
if (IS_NULL_OR_NIL(proxy)) {
[self setRightNavButtons:nil withObject:properties];
} else {
[self setRightNavButtons:[NSArray arrayWithObject:proxy] withObject:properties];
}
}
-(void)refreshLeftNavButtons:(id)unused
{
if (controller == nil || [controller navigationController] == nil) {
return; // No need to refresh
}
NSArray* theObjects = [self valueForUndefinedKey:@"leftNavButtons"];
NSDictionary* theProperties = [self valueForUndefinedKey:@"leftNavSettings"];
ENSURE_TYPE_OR_NIL(theObjects, NSArray);
ENSURE_TYPE_OR_NIL(theProperties, NSDictionary);
NSMutableArray* theItems = [[NSMutableArray alloc] init];
for (TiViewProxy* theProxy in theObjects) {
if ([theProxy supportsNavBarPositioning]) {
[theItems addObject:[theProxy barButtonItem]];
} else {
DebugLog(@"%@ does not support nav bar positioning", theProxy);
}
}
BOOL animated = [TiUtils boolValue:@"animated" properties:theProperties def:NO];
if ([theItems count] > 0) {
[controller.navigationItem setLeftBarButtonItems:theItems animated:animated];
} else {
[controller.navigationItem setLeftBarButtonItems:nil animated:animated];
}
[theItems release];
}
-(void)setLeftNavButtons:(id)arg withObject:(id)properties
{
ENSURE_TYPE_OR_NIL(arg, NSArray);
ENSURE_TYPE_OR_NIL(properties, NSDictionary);
NSArray* curValues = [self valueForUndefinedKey:@"leftNavButtons"];
ENSURE_TYPE_OR_NIL(curValues, NSArray);
//Clean up current values
for (TiViewProxy* curProxy in curValues) {
if (![(NSArray*)arg containsObject:curProxy]) {
[curProxy removeBarButtonView];
[self forgetProxy:curProxy];
}
}
for(TiViewProxy* proxy in arg) {
if([proxy isKindOfClass:[TiViewProxy class]]) {
[self rememberProxy:proxy];
}
}
[self replaceValue:arg forKey:@"leftNavButtons" notification:NO];
[self replaceValue:properties forKey:@"leftNavSettings" notification:NO];
TiThreadPerformOnMainThread(^{
[self refreshLeftNavButtons:nil];
}, NO);
}
-(void)setLeftNavButton:(id)proxy withObject:(id)properties
{
[self replaceValue:proxy forKey:@"leftNavButton" notification:NO];
if (IS_NULL_OR_NIL(proxy)) {
[self setLeftNavButtons:nil withObject:properties];
} else {
[self setLeftNavButtons:[NSArray arrayWithObject:proxy] withObject:properties];
}
}
-(void)setTabBarHidden:(id)value
{
[self replaceValue:value forKey:@"tabBarHidden" notification:NO];
TiThreadPerformOnMainThread(^{
if (controller != nil) {
[controller setHidesBottomBarWhenPushed:[TiUtils boolValue:value]];
}
}, NO);
}
-(void)hideTabBar:(id)value
{
[self setTabBarHidden:[NSNumber numberWithBool:YES]];
}
-(void)showTabBar:(id)value
{
[self setTabBarHidden:[NSNumber numberWithBool:NO]];
}
-(void)refreshBackButton
{
ENSURE_UI_THREAD_0_ARGS;
if (controller == nil || [controller navigationController] == nil) {
return; // No need to refresh
}
NSArray * controllerArray = [[controller navigationController] viewControllers];
NSUInteger controllerPosition = [controllerArray indexOfObject:controller];
if ((controllerPosition == 0) || (controllerPosition == NSNotFound))
{
return;
}
UIViewController * prevController = [controllerArray objectAtIndex:controllerPosition-1];
UIBarButtonItem * backButton = nil;
UIImage * backImage = [TiUtils image:[self valueForKey:@"backButtonTitleImage"] proxy:self];
if (backImage != nil)
{
backButton = [[UIBarButtonItem alloc] initWithImage:backImage style:UIBarButtonItemStylePlain target:nil action:nil];
}
else
{
NSString * backTitle = [TiUtils stringValue:[self valueForKey:@"backButtonTitle"]];
if ((backTitle == nil) && [prevController isKindOfClass:[TiViewController class]])
{
id tc = [(TiViewController*)prevController proxy];
backTitle = [TiUtils stringValue:[tc valueForKey:@"title"]];
}
if (backTitle != nil)
{
backButton = [[UIBarButtonItem alloc] initWithTitle:backTitle style:UIBarButtonItemStylePlain target:nil action:nil];
}
}
[[prevController navigationItem] setBackBarButtonItem:backButton];
[backButton release];
}
-(void)setBackButtonTitle:(id)proxy
{
ENSURE_UI_THREAD_1_ARG(proxy);
[self replaceValue:proxy forKey:@"backButtonTitle" notification:NO];
if (controller!=nil)
{
[self refreshBackButton]; //Because this is actually a property of a DIFFERENT view controller,
//we can't attach this until we're in the navbar stack.
}
}
-(void)setBackButtonTitleImage:(id)proxy
{
ENSURE_UI_THREAD_1_ARG(proxy);
[self replaceValue:proxy forKey:@"backButtonTitleImage" notification:NO];
if (controller!=nil)
{
[self refreshBackButton]; //Because this is actually a property of a DIFFERENT view controller,
//we can't attach this until we're in the navbar stack.
}
}
-(void)updateNavBar
{
//Called from the view when the screen rotates.
//Resize titleControl and barImage based on navbar bounds
if (!shouldUpdateNavBar || controller == nil || [controller navigationController] == nil) {
return; // No need to update the title if not in a nav controller
}
TiThreadPerformOnMainThread(^{
if ([[self valueForKey:@"titleControl"] isKindOfClass:[TiViewProxy class]]) {
[self updateTitleView];
}
}, NO);
}
-(void)updateTitleView
{
UIView * newTitleView = nil;
if (!shouldUpdateNavBar || controller == nil || [controller navigationController] == nil) {
return; // No need to update the title if not in a nav controller
}
UINavigationItem * ourNavItem = [controller navigationItem];
UINavigationBar * ourNB = [[controller navigationController] navigationBar];
CGRect barFrame = [ourNB bounds];
CGSize availableTitleSize = CGSizeZero;
availableTitleSize.width = barFrame.size.width - (2*TI_NAVBAR_BUTTON_WIDTH);
availableTitleSize.height = barFrame.size.height;
//Check for titlePrompt. Ugly hack. Assuming 50% for prompt height.
if (ourNavItem.prompt != nil) {
availableTitleSize.height /= 2.0f;
barFrame.origin.y = barFrame.size.height = availableTitleSize.height;
}
TiViewProxy * titleControl = [self valueForKey:@"titleControl"];
UIView * oldView = [ourNavItem titleView];
if ([oldView isKindOfClass:[TiUIView class]]) {
TiViewProxy * oldProxy = (TiViewProxy *)[(TiUIView *)oldView proxy];
if (oldProxy == titleControl) {
//relayout titleControl
CGRect barBounds;
barBounds.origin = CGPointZero;
#ifndef TI_USE_AUTOLAYOUT
barBounds.size = SizeConstraintViewWithSizeAddingResizing(titleControl.layoutProperties, titleControl, availableTitleSize, NULL);
#endif
[oldView setBounds:barBounds];
[oldView setAutoresizingMask:UIViewAutoresizingNone];
//layout the titleControl children
[titleControl layoutChildren:NO];
return;
}
[oldProxy removeBarButtonView];
}
if ([titleControl isKindOfClass:[TiViewProxy class]])
{
newTitleView = [titleControl barButtonViewForSize:availableTitleSize];
}
else
{
NSURL * path = [TiUtils toURL:[self valueForKey:@"titleImage"] proxy:self];
//Todo: This should be [TiUtils navBarTitleViewSize] with the thumbnail scaling. For now, however, we'll go with auto.
UIImage *image = [[ImageLoader sharedLoader] loadImmediateImage:path withSize:CGSizeZero];
if (image!=nil) {
if ([oldView isKindOfClass:[UIImageView class]]) {
[(UIImageView*)oldView setImage:image];
newTitleView = oldView;
} else {
newTitleView = [[[UIImageView alloc] initWithImage:image] autorelease];
}
}
}
if (oldView != newTitleView) {
[ourNavItem setTitleView:newTitleView];
}
}
-(void)setTitleControl:(id)proxy
{
ENSURE_UI_THREAD(setTitleControl,proxy);
[self replaceValue:proxy forKey:@"titleControl" notification:NO];
if (controller!=nil)
{
[self updateTitleView];
}
}
-(void)setTitleImage:(id)image
{
ENSURE_UI_THREAD(setTitleImage,image);
NSURL *path = [TiUtils toURL:image proxy:self];
[self replaceValue:[path absoluteString] forKey:@"titleImage" notification:NO];
if (controller!=nil)
{
[self updateTitleView];
}
}
-(void)setTitle:(NSString*)title_
{
NSString *title = [TiUtils stringValue:title_];
[self replaceValue:title forKey:@"title" notification:NO];
TiThreadPerformOnMainThread(^{
if (shouldUpdateNavBar && controller != nil && [controller navigationController] != nil) {
controller.navigationItem.title = title;
}
}, [NSThread isMainThread]);
}
#if IS_XCODE_9
- (void)setLargeTitleEnabled:(id)value
{
ENSURE_UI_THREAD(setLargeTitleEnabled, value);
ENSURE_TYPE(value, NSNumber);
[self replaceValue:value forKey:@"largeTitleEnabled" notification:NO];
if (@available(iOS 11.0, *) && shouldUpdateNavBar && controller != nil && [controller navigationController] != nil) {
[[[controller navigationController] navigationBar] setPrefersLargeTitles:[TiUtils boolValue:value def:NO]];
}
}
- (void)setLargeTitleDisplayMode:(id)value
{
ENSURE_UI_THREAD(setLargeTitleDisplayMode, value);
ENSURE_TYPE(value, NSNumber);
[self replaceValue:value forKey:@"largeTitleDisplayMode" notification:NO];
if (@available(iOS 11.0, *) && shouldUpdateNavBar && controller != nil && [controller navigationController] != nil) {
[[controller navigationItem] setLargeTitleDisplayMode:[TiUtils intValue:value def:UINavigationItemLargeTitleDisplayModeAutomatic]];
}
}
#endif
-(void)setTitlePrompt:(NSString*)title_
{
ENSURE_UI_THREAD(setTitlePrompt,title_);
NSString *title = [TiUtils stringValue:title_];
[self replaceValue:title forKey:@"titlePrompt" notification:NO];
if (controller!=nil && [controller navigationController] != nil)
{
controller.navigationItem.prompt = title;
}
}
-(void)setToolbar:(id)items withObject:(id)properties
{
ENSURE_TYPE_OR_NIL(items,NSArray);
if (properties == nil)
{
properties = [self valueForKey:@"toolbarSettings"];
}
else
{
[self setValue:properties forKey:@"toolbarSettings"];
}
NSArray * oldarray = [self valueForUndefinedKey:@"toolbar"];
if((id)oldarray == [NSNull null])
{
oldarray = nil;
}
for(TiViewProxy * oldProxy in oldarray)
{
if(![items containsObject:oldProxy])
{
[self forgetProxy:oldProxy];
}
}
for (TiViewProxy *proxy in items)
{
[self rememberProxy:proxy];
}
[self replaceValue:items forKey:@"toolbar" notification:NO];
TiThreadPerformOnMainThread( ^{
if (shouldUpdateNavBar && controller!=nil && [controller navigationController] != nil)
{
NSArray *existing = [controller toolbarItems];
UINavigationController * ourNC = [controller navigationController];
if (existing!=nil)
{
for (id current in existing)
{
if ([current respondsToSelector:@selector(proxy)])
{
TiViewProxy* p = (TiViewProxy*)[current performSelector:@selector(proxy)];
[p removeBarButtonView];
}
}
}
NSMutableArray * array = [[NSMutableArray alloc] initWithArray:@[]];
for (TiViewProxy *proxy in items)
{
if([proxy supportsNavBarPositioning])
{
UIBarButtonItem *item = [proxy barButtonItem];
[array addObject:item];
}
}
hasToolbar = (array != nil && [array count] > 0) ? YES : NO ;
BOOL translucent = [TiUtils boolValue:@"translucent" properties:properties def:YES];
BOOL animated = [TiUtils boolValue:@"animated" properties:properties def:hasToolbar];
TiColor* toolbarColor = [TiUtils colorValue:@"barColor" properties:properties];
UIColor* barColor = [TiUtils barColorForColor:toolbarColor];
[controller setToolbarItems:array animated:animated];
[ourNC setToolbarHidden:(hasToolbar == NO ? YES : NO) animated:animated];
[ourNC.toolbar setTranslucent:translucent];
UIColor* tintColor = [[TiUtils colorValue:@"tintColor" properties:properties] color];
[ourNC.toolbar setBarTintColor:barColor];
[ourNC.toolbar setTintColor:tintColor];
[array release];
}
},YES);
}
#define SETPROP(m,x) \
{\
id value = [self valueForKey:m]; \
if (value!=nil)\
{\
[self x:(value==[NSNull null]) ? nil : value];\
}\
else{\
[self replaceValue:nil forKey:m notification:NO];\
}\
}\
#define SETPROPOBJ(m,x) \
{\
id value = [self valueForKey:m]; \
if (value!=nil)\
{\
if ([value isKindOfClass:[TiComplexValue class]])\
{\
TiComplexValue *cv = (TiComplexValue*)value;\
[self x:(cv.value==[NSNull null]) ? nil : cv.value withObject:cv.properties];\
}\
else\
{\
[self x:(value==[NSNull null]) ? nil : value withObject:nil];\
}\
}\
else{\
[self replaceValue:nil forKey:m notification:NO];\
}\
}\
-(void)setupWindowDecorations
{
if ((controller == nil) || ([controller navigationController] == nil)) {
return;
}
[[controller navigationController] setToolbarHidden:!hasToolbar animated:YES];
//Need to clear title for titleAttributes to apply correctly on iOS6.
[[controller navigationItem] setTitle:nil];
SETPROP(@"titleAttributes",setTitleAttributes);
SETPROP(@"title",setTitle);
SETPROP(@"titlePrompt",setTitlePrompt);
SETPROP(@"largeTitleEnabled", setLargeTitleEnabled);
SETPROP(@"largeTitleDisplayMode", setLargeTitleDisplayMode);
[self updateTitleView];
SETPROP(@"barColor",setBarColor);
SETPROP(@"navTintColor",setNavTintColor);
SETPROP(@"translucent",setTranslucent);
SETPROP(@"tabBarHidden",setTabBarHidden);
SETPROPOBJ(@"toolbar",setToolbar);
[self updateBarImage];
[self updateNavButtons];
[self refreshBackButton];
}
-(void)cleanupWindowDecorations
{
if ((controller == nil) || ([controller navigationController] == nil)) {
return;
}
NSArray* curValues = [self valueForUndefinedKey:@"leftNavButtons"];
ENSURE_TYPE_OR_NIL(curValues, NSArray);
for (TiViewProxy* curProxy in curValues) {
[curProxy removeBarButtonView];