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

Send JS blur/focus events when switching to another NSWindow #2323

Merged
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
41 changes: 41 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,46 @@ - (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
amgleitman marked this conversation as resolved.
Show resolved Hide resolved
selector:@selector(containingWindowDidBecomeKey)
name:NSWindowDidBecomeKeyNotification
object:window];

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

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

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


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