Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[0.75-stable] Send JS blur/focus events when switching to another NSWindow #2363

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions packages/react-native/React/Base/RCTRootView.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#import "RCTBridge.h"
#import "RCTConstants.h"
#import "RCTDevSettings.h" // [macOS]
#import "RCTFocusChangeEvent.h" // [macOS]
// [macOS] remove #import "RCTKeyCommands.h"
#import "RCTLog.h"
#import "RCTPerformanceLogger.h"
Expand Down Expand Up @@ -432,6 +433,63 @@ - (void)viewDidChangeEffectiveAppearance
#endif // macOS]


#pragma mark - Key window blur/focus

#if TARGET_OS_OSX // [macOS
- (void)viewDidMoveToWindow {
[super viewDidMoveToWindow];

NSWindow *window = [self window];
if (window == nil) {
return;
}

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(containingWindowDidBecomeKey)
name:NSWindowDidBecomeKeyNotification
object:window];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(containingWindowDidResignKey)
name:NSWindowDidResignKeyNotification
object:window];
}

+ (NSNumber *)reactTagClosestToView:(RCTPlatformView *)view {
// The first responder may not necessarily have a React tag. For example, if we're focused on
// a multiline <TextInput>, the RN component whose blur/focus events we need to touch is an
// RCTMultilineTextInputView, but the first responder will be the underlying RCTUITextView.
for (RCTPlatformView *testView = view; testView != nil; testView = [testView superview]) {
NSNumber *reactTag = [testView reactTag];
if (reactTag != nil) {
return reactTag;
}
}
return nil;
}

- (void)containingWindowDidBecomeKey {
NSResponder *firstResponder = [[self window] firstResponder];
if ([firstResponder isKindOfClass:[RCTPlatformView class]]) {
NSNumber *reactTag = [RCTRootView reactTagClosestToView:(RCTPlatformView *)firstResponder];
if (reactTag != nil) {
[[[self bridge] eventDispatcher] sendEvent:[RCTFocusChangeEvent focusEventWithReactTag:reactTag]];
}
}
}

- (void)containingWindowDidResignKey {
NSResponder *firstResponder = [[self window] firstResponder];
if ([firstResponder isKindOfClass:[RCTPlatformView class]]) {
NSNumber *reactTag = [RCTRootView reactTagClosestToView:(RCTPlatformView *)firstResponder];
if (reactTag != nil) {
[[[self bridge] eventDispatcher] sendEvent:[RCTFocusChangeEvent blurEventWithReactTag:reactTag]];
}
}
}
#endif // macOS]


#if TARGET_OS_OSX // [macOS
- (NSMenu *)menuForEvent:(NSEvent *)event
{
Expand Down
Loading