Skip to content

Commit

Permalink
feat(ios): add Ti.UI.Window.focused property
Browse files Browse the repository at this point in the history
attempt to make logic of TabGroup fall in line with Window, so the initial focus event happens after open like for Window

Fixes TIMOB-27711
  • Loading branch information
sgtcoolguy committed Jun 22, 2020
1 parent 49d143f commit c5de6e2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 22 deletions.
34 changes: 20 additions & 14 deletions iphone/Classes/TiUITabGroup.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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
Expand All @@ -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
9 changes: 9 additions & 0 deletions iphone/Classes/TiUITabGroupProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
24 changes: 16 additions & 8 deletions iphone/TitaniumKit/TitaniumKit/Sources/API/TiWindowProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,22 @@ - (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;
opened = YES;
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];
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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];
Expand Down

0 comments on commit c5de6e2

Please sign in to comment.