mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Allow views to become focusable (#56141)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/56141 Changelog: [Internal] Wires up the native side of `focusable` to fabric views. Reviewed By: joevilches Differential Revision: D96751956 fbshipit-source-id: 9867755a11ab0df6faef8e7cdff4eb168345097f
This commit is contained in:
1 parent
bea42e5ed9
commit
0f92aa6213
3 files changed
+98
-2
No files matched your search
+5
@@ -39,6 +39,11 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
@property (nonatomic, assign) NSTimeInterval loadingViewFadeDuration;
|
||||
@property (nonatomic, assign) CGSize minimumSize;
|
||||
|
||||
#if TARGET_OS_TV
|
||||
@property (nonatomic, copy, nullable) NSArray<id<UIFocusEnvironment>> *reactPreferredFocusEnvironments;
|
||||
@property (nonatomic, weak, nullable) UIView *reactPreferredFocusedView;
|
||||
#endif
|
||||
|
||||
- (instancetype)init NS_UNAVAILABLE;
|
||||
+ (instancetype)new NS_UNAVAILABLE;
|
||||
- (instancetype)initWithFrame:(CGRect)frame NS_UNAVAILABLE;
|
||||
|
||||
+26
@@ -121,6 +121,13 @@ RCT_NOT_IMPLEMENTED(-(instancetype)initWithCoder : (NSCoder *)aDecoder)
|
||||
[super surface:surface didChangeStage:stage];
|
||||
if (RCTSurfaceStageIsRunning(stage)) {
|
||||
[_bridge.performanceLogger markStopForTag:RCTPLTTI];
|
||||
#if TARGET_OS_TV
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
self.reactPreferredFocusedView = nil;
|
||||
[self setNeedsFocusUpdate];
|
||||
[self updateFocusIfNeeded];
|
||||
});
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -154,4 +161,23 @@ RCT_NOT_IMPLEMENTED(-(instancetype)initWithCoder : (NSCoder *)aDecoder)
|
||||
// Not supported.
|
||||
}
|
||||
|
||||
#if TARGET_OS_TV
|
||||
#pragma mark - UIFocusEnvironment
|
||||
|
||||
- (NSArray<id<UIFocusEnvironment>> *)preferredFocusEnvironments
|
||||
{
|
||||
if (self.reactPreferredFocusEnvironments != nil && self.reactPreferredFocusedView.window != nil) {
|
||||
NSArray<id<UIFocusEnvironment>> *tempReactPreferredFocusEnvironments = self.reactPreferredFocusEnvironments;
|
||||
self.reactPreferredFocusEnvironments = nil;
|
||||
return tempReactPreferredFocusEnvironments;
|
||||
}
|
||||
|
||||
if (self.reactPreferredFocusedView && self.reactPreferredFocusedView.window != nil) {
|
||||
return @[ self.reactPreferredFocusedView ];
|
||||
}
|
||||
|
||||
return [super preferredFocusEnvironments];
|
||||
}
|
||||
#endif
|
||||
|
||||
@end
|
||||
+67
-2
@@ -6,6 +6,7 @@
|
||||
*/
|
||||
|
||||
#import "RCTViewComponentView.h"
|
||||
#import <React/RCTSurfaceHostingProxyRootView.h>
|
||||
|
||||
#import <CoreGraphics/CoreGraphics.h>
|
||||
#import <QuartzCore/QuartzCore.h>
|
||||
@@ -53,6 +54,7 @@ const CGFloat BACKGROUND_COLOR_ZPOSITION = -1024.0f;
|
||||
BOOL _useCustomContainerView;
|
||||
NSMutableSet<NSString *> *_accessibilityOrderNativeIDs;
|
||||
RCTSwiftUIContainerViewWrapper *_swiftUIWrapper;
|
||||
BOOL _focusable;
|
||||
}
|
||||
|
||||
#ifdef RCT_DYNAMIC_FRAMEWORKS
|
||||
@@ -481,6 +483,13 @@ const CGFloat BACKGROUND_COLOR_ZPOSITION = -1024.0f;
|
||||
needsInvalidateLayer = YES;
|
||||
}
|
||||
|
||||
// `focusable`
|
||||
#if TARGET_OS_TV
|
||||
if (oldViewProps.focusable != newViewProps.focusable) {
|
||||
_focusable = (bool)newViewProps.focusable;
|
||||
}
|
||||
#endif
|
||||
|
||||
// `mixBlendMode`
|
||||
if (oldViewProps.mixBlendMode != newViewProps.mixBlendMode) {
|
||||
switch (newViewProps.mixBlendMode) {
|
||||
@@ -1422,6 +1431,11 @@ static NSString *RCTRecursiveAccessibilityLabel(UIView *view)
|
||||
return !super.accessibilityLabel && super.isAccessibilityElement;
|
||||
}
|
||||
|
||||
- (BOOL)canBecomeFocused
|
||||
{
|
||||
return _focusable;
|
||||
}
|
||||
|
||||
- (BOOL)isAccessibilityElement
|
||||
{
|
||||
if (self.contentView != nil) {
|
||||
@@ -1671,6 +1685,8 @@ static NSString *RCTRecursiveAccessibilityLabel(UIView *view)
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - Focus Events
|
||||
|
||||
- (BOOL)canBecomeFirstResponder
|
||||
{
|
||||
return YES;
|
||||
@@ -1689,9 +1705,36 @@ static NSString *RCTRecursiveAccessibilityLabel(UIView *view)
|
||||
}
|
||||
}
|
||||
|
||||
#if TARGET_OS_TV
|
||||
/// Finds the containing RCTSurfaceHostingProxyRootView by walking up the view
|
||||
/// hierarchy.
|
||||
- (RCTSurfaceHostingProxyRootView *)containingRootView
|
||||
{
|
||||
UIView *view = self;
|
||||
while (view != nil) {
|
||||
if ([view isKindOfClass:[RCTSurfaceHostingProxyRootView class]]) {
|
||||
return (RCTSurfaceHostingProxyRootView *)view;
|
||||
}
|
||||
view = view.superview;
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
#endif
|
||||
|
||||
- (void)focus
|
||||
{
|
||||
[self becomeFirstResponder];
|
||||
|
||||
#if TARGET_OS_TV
|
||||
RCTSurfaceHostingProxyRootView *rootView = [self containingRootView];
|
||||
if (rootView == nil) {
|
||||
return;
|
||||
}
|
||||
|
||||
rootView.reactPreferredFocusedView = self;
|
||||
[rootView setNeedsFocusUpdate];
|
||||
[rootView updateFocusIfNeeded];
|
||||
#endif
|
||||
}
|
||||
|
||||
- (void)blur
|
||||
@@ -1699,8 +1742,6 @@ static NSString *RCTRecursiveAccessibilityLabel(UIView *view)
|
||||
[self resignFirstResponder];
|
||||
}
|
||||
|
||||
#pragma mark - Focus Events
|
||||
|
||||
- (BOOL)becomeFirstResponder
|
||||
{
|
||||
if (![super becomeFirstResponder]) {
|
||||
@@ -1727,6 +1768,30 @@ static NSString *RCTRecursiveAccessibilityLabel(UIView *view)
|
||||
return YES;
|
||||
}
|
||||
|
||||
#if TARGET_OS_TV
|
||||
|
||||
- (void)didUpdateFocusInContext:(UIFocusUpdateContext *)context
|
||||
withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator
|
||||
{
|
||||
if (context.previouslyFocusedView == context.nextFocusedView) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (context.nextFocusedView == self) {
|
||||
if (_eventEmitter) {
|
||||
_eventEmitter->onFocus();
|
||||
}
|
||||
} else {
|
||||
if (_eventEmitter) {
|
||||
_eventEmitter->onBlur();
|
||||
}
|
||||
}
|
||||
|
||||
[super didUpdateFocusInContext:context withAnimationCoordinator:coordinator];
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@end
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
Reference in new issue
Block a user