Skip to content

Instantly share code, notes, and snippets.

@lyahdav
Created September 15, 2020 19:08
Show Gist options
  • Save lyahdav/e8de6782ea07a2b6f3ed19e7f58d47c9 to your computer and use it in GitHub Desktop.
Save lyahdav/e8de6782ea07a2b6f3ed19e7f58d47c9 to your computer and use it in GitHub Desktop.
// (c) Facebook, Inc. and its affiliates. Confidential and proprietary.
#import "FocusZoneViewManager-macOS.h"
#import <React/RCTView.h>
@interface FocusZoneView : RCTView
@end
@implementation FocusZoneView
- (NSView*)firstViewInKeyViewLoop {
NSView* nextValidKeyView = self.nextValidKeyView;
return [nextValidKeyView isDescendantOf:self] ? nextValidKeyView : nil;
}
- (NSView*)lastViewInKeyViewLoop {
NSView* view = self.nextValidKeyView;
NSView* previousView = self.nextValidKeyView;
while ([view isDescendantOf:self]) {
previousView = view;
view = view.nextValidKeyView;
}
return previousView;
}
- (BOOL)isFirstControlSelected {
return self.window.firstResponder == [self firstViewInKeyViewLoop];
}
- (BOOL)isLastControlSelected {
return self.window.firstResponder == [self lastViewInKeyViewLoop];
}
- (void)keyDown:(NSEvent*)event {
unichar const firstCharacter = [event.characters characterAtIndex:0];
NSUInteger modifierFlags = event.modifierFlags & NSEventModifierFlagDeviceIndependentFlagsMask;
// NOTE: we ignore NSEventModifierFlagFunction since it seems to get set even when fn isn't
// pressed
BOOL isArrowKeyModifierFlag =
(modifierFlags & ~NSEventModifierFlagFunction) == NSEventModifierFlagNumericPad;
switch (firstCharacter) {
case NSUpArrowFunctionKey: {
if (isArrowKeyModifierFlag && ![self isFirstControlSelected]) {
[self.window selectPreviousKeyView:self.window.firstResponder];
return;
}
break;
}
case NSDownArrowFunctionKey: {
if (isArrowKeyModifierFlag && ![self isLastControlSelected]) {
[self.window selectNextKeyView:self.window.firstResponder];
return;
}
break;
}
case NSTabCharacter: {
[self.window selectKeyViewFollowingView:[self lastViewInKeyViewLoop]];
return;
}
case NSBackTabCharacter: {
[self.window selectKeyViewPrecedingView:[self firstViewInKeyViewLoop]];
return;
}
}
[super keyDown:event];
}
@end
@implementation FocusZoneViewManager
RCT_EXPORT_MODULE()
- (RCTPlatformView*)view {
return [FocusZoneView new];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment