Skip to content

Commit

Permalink
Merge pull request tidev#5443 from vishalduggal/timob-15855
Browse files Browse the repository at this point in the history
[TIMOB-15855] iOS: refactor subview insertion based on zIndex
  • Loading branch information
srahim committed Mar 18, 2014
2 parents 20b8eee + 90b4676 commit de2be9a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 89 deletions.
5 changes: 5 additions & 0 deletions iphone/Classes/TiUIWindowProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ -(void)boot:(BOOL)timeout args:args
}
}

-(BOOL)optimizeSubviewInsertion
{
return YES;
}

-(NSMutableDictionary*)langConversionTable
{
return [NSMutableDictionary dictionaryWithObjectsAndKeys:@"title",@"titleid",@"titlePrompt",@"titlepromptid",nil];
Expand Down
122 changes: 33 additions & 89 deletions iphone/Classes/TiViewProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -2103,65 +2103,50 @@ -(void)refreshView:(TiUIView *)transferView
-(void)refreshPosition
{
OSAtomicTestAndClearBarrier(TiRefreshViewPosition, &dirtyflags);

}

-(void)refreshSize
{
OSAtomicTestAndClearBarrier(TiRefreshViewSize, &dirtyflags);


}

-(void)insertSubview:(UIView *)childView forProxy:(TiViewProxy *)childProxy
{

int result = 0;
int childZindex = [childProxy vzIndex];
BOOL earlierSibling = YES;
UIView * ourView = [self parentViewForChild:childProxy];

NSUInteger result = 0;
UIView * ourView = [self parentViewForChild:childProxy];

if (ourView==nil || childView == nil) {
return;
}

if (![self optimizeSubviewInsertion]) {
for (UIView* subview in [ourView subviews])
{
NSArray* subViews = [[ourView subviews] retain];

for (UIView* subview in subViews) {
if (![subview isKindOfClass:[TiUIView class]]) {
result++;
}
}
[subViews release];
}
pthread_rwlock_rdlock(&childrenLock);
for (TiViewProxy * thisChildProxy in children)
{
if(thisChildProxy == childProxy)
{
earlierSibling = NO;
continue;
}

if(![thisChildProxy viewHasSuperview:ourView])
{
continue;
}

int thisChildZindex = [thisChildProxy vzIndex];
if((thisChildZindex < childZindex) ||
(earlierSibling && (thisChildZindex == childZindex)))
{
result ++;
}
}
pthread_rwlock_unlock(&childrenLock);
if (result == 0) {
[ourView insertSubview:childView atIndex:result];
}
else {
//Doing a blind insert at index messes up the underlying sublayer indices
//if there are layers which do not belong to subviews (backgroundGradient)
//So ensure the subview layer goes at the right index
//See TIMOB-11586 for fail case
UIView *sibling = [[ourView subviews] objectAtIndex:result-1];
[ourView insertSubview:childView aboveSubview:sibling];

pthread_rwlock_rdlock(&childrenLock);

NSArray *sortedArray;
sortedArray = [children sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
int first = [(TiViewProxy*)a vzIndex];
int second = [(TiViewProxy*)b vzIndex];
return (first > second) ? NSOrderedDescending : ( first < second ? NSOrderedAscending : NSOrderedSame );
}];

for (TiViewProxy * thisChildProxy in sortedArray) {
if ([thisChildProxy viewInitialized] ) {
[ourView insertSubview:[thisChildProxy view] atIndex:result];
result ++;
}
}
pthread_rwlock_unlock(&childrenLock);

}


Expand Down Expand Up @@ -2201,8 +2186,9 @@ -(void)relayout
[view setAutoresizingMask:autoresizeCache];
[view setCenter:positionCache];
[view setBounds:sizeCache];

[parent insertSubview:view forProxy:self];
if ([view superview] != parentView) {
[parent insertSubview:view forProxy:self];
}

[self refreshSize];
[self refreshPosition];
Expand Down Expand Up @@ -2712,50 +2698,8 @@ -(void)layoutChild:(TiViewProxy*)child optimize:(BOOL)optimize withMeasuredBound
if (optimize==NO)
{
TiUIView *childView = [child view];
if ([childView superview]!=ourView)
{
//TODO: Optimize!
int insertPosition = 0;
int childZIndex = [child vzIndex];

pthread_rwlock_rdlock(&childrenLock);
int childProxyIndex = [children indexOfObject:child];

BOOL optimizeInsertion = [self optimizeSubviewInsertion];

for (TiUIView * thisView in [ourView subviews])
{
if ( (!optimizeInsertion) && (![thisView isKindOfClass:[TiUIView class]]) )
{
insertPosition ++;
continue;
}

int thisZIndex=[(TiViewProxy *)[thisView proxy] vzIndex];
if (childZIndex < thisZIndex) //We've found our stop!
{
break;
}
if (childZIndex == thisZIndex)
{
TiProxy * thisProxy = [thisView proxy];
if (childProxyIndex <= [children indexOfObject:thisProxy])
{
break;
}
}
insertPosition ++;
}

[ourView insertSubview:childView atIndex:insertPosition];
pthread_rwlock_unlock(&childrenLock); // must release before calling resize

// TIMOB-14488. This is a bad message. We should not be signalling a child
// resize to the parent when the parent is laying out the child.
// if ( !CGSizeEqualToSize(child.sandboxBounds.size, bounds.size) ) {
// //Child will not resize if sandbox size does not change
// [self childWillResize:child];
// }
if ([childView superview]!=ourView) {
[self insertSubview:childView forProxy:child];
}
}
[child setSandboxBounds:bounds];
Expand Down

0 comments on commit de2be9a

Please sign in to comment.