diff --git a/iphone/Classes/TiUITabGroup.m b/iphone/Classes/TiUITabGroup.m index a410b13e45a..23a32a4341a 100644 --- a/iphone/Classes/TiUITabGroup.m +++ b/iphone/Classes/TiUITabGroup.m @@ -99,6 +99,8 @@ - (void)handleDidShowTab:(TiUITabProxy *)newFocus return; } + // FIXME: COmbine with focusEvent! That one builds the very first focus event + // This builds later ones NSMutableDictionary *event = [NSMutableDictionary dictionaryWithCapacity:4]; NSArray *tabArray = [controller viewControllers]; @@ -596,20 +598,6 @@ - (void)open:(id)args UIView *view = [self tabController].view; [view setFrame:[self bounds]]; [self addSubview:view]; - - // on an open, make sure we send the focus event to focused tab - NSArray *tabArray = [controller viewControllers]; - NSInteger index = 0; - if (focusedTabProxy != nil) { - index = [tabArray indexOfObject:[(TiUITabProxy *)focusedTabProxy controller]]; - } - NSDictionary *event = [NSDictionary dictionaryWithObjectsAndKeys:focusedTabProxy, @"tab", NUMINTEGER(index), @"index", NUMINT(-1), @"previousIndex", [NSNull null], @"previousTab", nil]; - if ([self.proxy _hasListeners:@"focus"]) { - [self.proxy fireEvent:@"focus" withObject:event]; - } - - // Tab has already been focused by the tab controller delegate - //[focused handleDidFocus:event]; } - (void)close:(id)args @@ -620,6 +608,24 @@ - (void)close:(id)args RELEASE_TO_NIL(controller); } +// This is the focus event we fire on initial open, or when the TabGroup gets focus +// Note that handleDidShowTab builds the more complex event for switching tabs +// (and also fires a blur event) +- (NSDictionary *)focusEvent +{ + NSArray *tabArray = [controller viewControllers]; + NSInteger index = 0; + if (focusedTabProxy != nil) { + index = [tabArray indexOfObject:[(TiUITabProxy *)focusedTabProxy controller]]; + } + return @{ + @"tab" : focusedTabProxy, + @"index" : NUMINTEGER(index), + @"previousIndex" : NUMINT(-1), + @"previousTab" : [NSNull null] + }; +} + @end #endif diff --git a/iphone/Classes/TiUITabGroupProxy.m b/iphone/Classes/TiUITabGroupProxy.m index 3f6f3e379aa..7c4f89a730d 100644 --- a/iphone/Classes/TiUITabGroupProxy.m +++ b/iphone/Classes/TiUITabGroupProxy.m @@ -169,6 +169,15 @@ - (BOOL)handleFocusEvents return NO; } +- (void)fireFocusEvent +{ + if ([self _hasListeners:@"focus"]) { + // on an open, make sure we send the focus event to focused tab + NSDictionary *event = [((TiUITabGroup *)self.view)focusEvent]; + [self fireEvent:@"focus" withObject:event]; + } +} + - (void)gainFocus { if (!focussed) { diff --git a/iphone/TitaniumKit/TitaniumKit/Sources/API/TiWindowProxy.m b/iphone/TitaniumKit/TitaniumKit/Sources/API/TiWindowProxy.m index b957955c58c..e84b37feb93 100644 --- a/iphone/TitaniumKit/TitaniumKit/Sources/API/TiWindowProxy.m +++ b/iphone/TitaniumKit/TitaniumKit/Sources/API/TiWindowProxy.m @@ -99,6 +99,13 @@ - (void)windowWillOpen } } +- (void)fireFocusEvent +{ + if ([self _hasListeners:@"focus"]) { + [self fireEvent:@"focus" withObject:nil withSource:self propagate:NO reportSuccess:NO errorCode:0 message:nil]; + } +} + - (void)windowDidOpen { opening = NO; @@ -106,10 +113,8 @@ - (void)windowDidOpen if ([self _hasListeners:@"open"]) { [self fireEvent:@"open" withObject:nil withSource:self propagate:NO reportSuccess:NO errorCode:0 message:nil]; } - if (focussed && [self handleFocusEvents]) { - if ([self _hasListeners:@"focus"]) { - [self fireEvent:@"focus" withObject:nil withSource:self propagate:NO reportSuccess:NO errorCode:0 message:nil]; - } + if (focussed) { + [self fireFocusEvent]; } [super windowDidOpen]; [self forgetProxy:openAnimation]; @@ -337,6 +342,11 @@ - (NSNumber *)closed return NUMBOOL(!opening && !opened && !closing); } +- (NSNumber *)focused +{ + return NUMBOOL(focussed); +} + - (BOOL)_handleOpen:(id)args { TiRootViewController *theController = [[TiApp app] controller]; @@ -437,10 +447,8 @@ - (void)gainFocus { if (!focussed) { focussed = YES; - if ([self handleFocusEvents] && opened) { - if ([self _hasListeners:@"focus"]) { - [self fireEvent:@"focus" withObject:nil withSource:self propagate:NO reportSuccess:NO errorCode:0 message:nil]; - } + if (opened) { + [self fireFocusEvent]; } UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification, nil); [[self view] setAccessibilityElementsHidden:NO];