Skip to content

Commit

Permalink
Remove #if TARGET_OS_OSX blocks, since these changes are potentially …
Browse files Browse the repository at this point in the history
…upstreamable
  • Loading branch information
Adam Gleitman committed Jul 9, 2024
1 parent 7fbf20c commit bc4d5d0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 38 deletions.
33 changes: 9 additions & 24 deletions packages/react-native/Libraries/Text/Text/RCTTextShadowView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -85,22 +85,9 @@ - (void)uiManagerWillPerformMounting

NSNumber *tag = self.reactTag;
NSMutableArray<NSNumber *> *descendantViewTags = [NSMutableArray new];
#if TARGET_OS_OSX // [macOS
NSMutableArray<NSNumber *> *virtualSubviewTags = [NSMutableArray new];
#endif // macOS]
NSMutableArray<NSNumber *> *virtualSubviewTags = [NSMutableArray new]; // [macOS]

#if !TARGET_OS_OSX // [macOS]
[textStorage enumerateAttribute:RCTBaseTextShadowViewEmbeddedShadowViewAttributeName
inRange:NSMakeRange(0, textStorage.length)
options:0
usingBlock:^(RCTShadowView *shadowView, NSRange range, __unused BOOL *stop) {
if (!shadowView) {
return;
}

[descendantViewTags addObject:shadowView.reactTag];
}];
#else // [macOS
// [macOS - Enumerate embedded shadow views and virtual subviews in one loop
[textStorage enumerateAttributesInRange:NSMakeRange(0, textStorage.length)
options:0
usingBlock:^(NSDictionary<NSAttributedStringKey, id> *_Nonnull attrs, NSRange range, __unused BOOL * _Nonnull stop) {
Expand All @@ -115,7 +102,7 @@ - (void)uiManagerWillPerformMounting
[virtualSubviewTags addObject:tagAttribute];
}
}];
#endif // macOS]
// macOS]

[_bridge.uiManager addUIBlock:^(RCTUIManager *uiManager, NSDictionary<NSNumber *, RCTUIView *> *viewRegistry) { // [macOS]
RCTTextView *textView = (RCTTextView *)viewRegistry[tag];
Expand All @@ -134,8 +121,7 @@ - (void)uiManagerWillPerformMounting
[descendantViews addObject:descendantView];
}];


#if TARGET_OS_OSX // [macOS
// [macOS
NSMutableArray<RCTVirtualTextView *> *virtualSubviews = [NSMutableArray arrayWithCapacity:virtualSubviewTags.count];
[virtualSubviewTags
enumerateObjectsUsingBlock:^(NSNumber *_Nonnull virtualSubviewTag, NSUInteger index, BOOL *_Nonnull stop) {
Expand All @@ -144,17 +130,16 @@ - (void)uiManagerWillPerformMounting
[virtualSubviews addObject:(RCTVirtualTextView *)virtualSubview];
}
}];
#endif // macOS]
// macOS]

// Removing all references to Shadow Views to avoid unnecessary retaining.
[textStorage removeAttribute:RCTBaseTextShadowViewEmbeddedShadowViewAttributeName
range:NSMakeRange(0, textStorage.length)];

[textView setTextStorage:textStorage contentFrame:contentFrame descendantViews:descendantViews
#if TARGET_OS_OSX // [macOS
virtualSubviews:virtualSubviews
#endif // macOS]
];
[textView setTextStorage:textStorage
contentFrame:contentFrame
descendantViews:descendantViews
virtualSubviews:virtualSubviews]; // [macOS]
}];
}

Expand Down
6 changes: 3 additions & 3 deletions packages/react-native/Libraries/Text/Text/RCTTextView.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#import <React/RCTComponent.h>
#import <React/RCTEventDispatcher.h> // [macOS]
#import <React/RCTVirtualTextView.h>
#import <React/RCTVirtualTextView.h> // [macOS]

#import <React/RCTUIKit.h> // [macOS]

Expand All @@ -23,12 +23,12 @@ NS_ASSUME_NONNULL_BEGIN
contentFrame:(CGRect)contentFrame
descendantViews:(NSArray<RCTPlatformView *> *)descendantViews; // [macOS]

#if TARGET_OS_OSX // [macOS
// [macOS
- (void)setTextStorage:(NSTextStorage *)textStorage
contentFrame:(CGRect)contentFrame
descendantViews:(NSArray<RCTPlatformView *> *)descendantViews
virtualSubviews:(NSArray<RCTVirtualTextView *> *_Nullable)virtualSubviews;
#endif // macOS]
// macOS]

#if TARGET_OS_OSX // [macOS
- (NSRect)getRectForCharRange:(NSRange)charRange;
Expand Down
18 changes: 7 additions & 11 deletions packages/react-native/Libraries/Text/Text/RCTTextView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ @implementation RCTTextView {

id<RCTEventDispatcherProtocol> _eventDispatcher; // [macOS]
NSArray<RCTUIView *> *_Nullable _descendantViews; // [macOS]
#if TARGET_OS_OSX // [macOS
NSArray<RCTVirtualTextView *> *_Nullable _virtualSubviews;
#endif // macOS]
NSArray<RCTVirtualTextView *> *_Nullable _virtualSubviews; // [macOS]
RCTUIView *_Nullable _currentHoveredSubview; // [macOS]
NSTextStorage *_Nullable _textStorage;
CGRect _contentFrame;
Expand Down Expand Up @@ -168,19 +166,20 @@ - (void)setTextStorage:(NSTextStorage *)textStorage
contentFrame:(CGRect)contentFrame
descendantViews:(NSArray<RCTPlatformView *> *)descendantViews // [macOS]
{
// [macOS]
// [macOS - to keep track of virtualSubviews as well
[self setTextStorage:textStorage
contentFrame:contentFrame
descendantViews:descendantViews
virtualSubviews:nil];
}

// [macOS]
- (void)setTextStorage:(NSTextStorage *)textStorage
contentFrame:(CGRect)contentFrame
descendantViews:(NSArray<RCTPlatformView *> *)descendantViews
virtualSubviews:(NSArray<RCTVirtualTextView *> *)virtualSubviews
{
// macOS]

// This lets the textView own its text storage on macOS
// We update and replace the text container `_textView.textStorage.attributedString` when text/layout changes
#if !TARGET_OS_OSX // [macOS]
Expand Down Expand Up @@ -223,9 +222,7 @@ - (void)setTextStorage:(NSTextStorage *)textStorage
[self addSubview:view];
}

#if TARGET_OS_OSX // [macOS
_virtualSubviews = virtualSubviews;
#endif // macOS]
_virtualSubviews = virtualSubviews; // [macOS]

[self setNeedsDisplay];
}
Expand Down Expand Up @@ -443,9 +440,8 @@ - (BOOL)hasMouseHoverEvent
return YES;
}

// All descendant views of an RCTTextView are RCTVirtualTextViews
// TODO: This isn't right in the case of embedded views. But maybe we want to keep these separate?
// TODO: How does current RNM handle mouse events in embedded views?
// We only care about virtual subviews here.
// Embedded views (e.g., <Text> <View /> </Text>) handle mouse hover events themselves.
NSUInteger indexOfChildWithMouseHoverEvent = [_virtualSubviews indexOfObjectPassingTest:^BOOL(RCTVirtualTextView *_Nonnull childView, NSUInteger idx, BOOL *_Nonnull stop) {
*stop = [childView hasMouseHoverEvent];
return *stop;
Expand Down

0 comments on commit bc4d5d0

Please sign in to comment.