Skip to content

Commit 810e6da

Browse files
authored
Add shadow inconsistency opacity fix with updated tests (cherry pick of #1402) (#1411)
* Redo shadow opacity consistency fix * Update test that was introducing redboxes * Fix yarn flow-check-ios errors * Add comment linking to github issue * Update comment
1 parent 262d5b8 commit 810e6da

File tree

4 files changed

+61
-15
lines changed

4 files changed

+61
-15
lines changed

React/Views/RCTView.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,12 @@ extern const UIAccessibilityTraits SwitchAccessibilityTrait;
148148
@property (nonatomic, copy) RCTDirectEventBlock onKeyUp;
149149
@property (nonatomic, copy) NSArray<NSString*> *validKeysDown;
150150
@property (nonatomic, copy) NSArray<NSString*> *validKeysUp;
151+
152+
// Shadow Properties
153+
@property (nonatomic, strong) NSColor *shadowColor;
154+
@property (nonatomic, assign) CGFloat shadowOpacity;
155+
@property (nonatomic, assign) CGFloat shadowRadius;
156+
@property (nonatomic, assign) CGSize shadowOffset;
151157
#endif // ]TODO(macOS GH#774)
152158

153159
/**

React/Views/RCTView.m

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ - (instancetype)initWithFrame:(CGRect)frame
164164
_hitTestEdgeInsets = UIEdgeInsetsZero;
165165
#if TARGET_OS_OSX // TODO(macOS GH#774)
166166
_transform3D = CATransform3DIdentity;
167+
_shadowColor = nil;
167168
#endif // TODO(macOS GH#774)
168169

169170
_backgroundColor = super.backgroundColor;
@@ -726,20 +727,49 @@ - (NSString *)description
726727
#endif // TODO(macOS GH#774)
727728

728729
#if TARGET_OS_OSX // [TODO(macOS GH#774)
729-
// Workaround AppKit issue with directly manipulating the view layer's shadow.
730-
- (NSShadow*)shadow
730+
- (void)setShadowColor:(NSColor *)shadowColor
731731
{
732-
CALayer *layer = self.layer;
733-
NSShadow *shadow = nil;
732+
if (_shadowColor != shadowColor)
733+
{
734+
_shadowColor = shadowColor;
735+
[self didUpdateShadow];
736+
}
737+
}
734738

735-
if (layer.shadowColor != nil && layer.shadowOpacity > 0) {
736-
shadow = [NSShadow new];
739+
- (void)setShadowOffset:(CGSize)shadowOffset
740+
{
741+
if (!CGSizeEqualToSize(_shadowOffset, shadowOffset))
742+
{
743+
_shadowOffset = shadowOffset;
744+
[self didUpdateShadow];
745+
}
746+
}
737747

738-
shadow.shadowColor = [[NSColor colorWithCGColor:layer.shadowColor] colorWithAlphaComponent:layer.shadowOpacity];
739-
shadow.shadowOffset = layer.shadowOffset;
740-
shadow.shadowBlurRadius = layer.shadowRadius;
741-
}
742-
return shadow;
748+
- (void)setShadowOpacity:(CGFloat)shadowOpacity
749+
{
750+
if (_shadowOpacity != shadowOpacity)
751+
{
752+
_shadowOpacity = shadowOpacity;
753+
[self didUpdateShadow];
754+
}
755+
}
756+
757+
- (void)setShadowRadius:(CGFloat)shadowRadius
758+
{
759+
if (_shadowRadius != shadowRadius)
760+
{
761+
_shadowRadius = shadowRadius;
762+
[self didUpdateShadow];
763+
}
764+
}
765+
766+
-(void)didUpdateShadow
767+
{
768+
NSShadow *shadow = [NSShadow new];
769+
shadow.shadowColor = [[self shadowColor] colorWithAlphaComponent:[self shadowOpacity]];
770+
shadow.shadowOffset = [self shadowOffset];
771+
shadow.shadowBlurRadius = [self shadowRadius];
772+
[self setShadow:shadow];
743773
}
744774

745775
- (void)viewDidMoveToWindow

React/Views/RCTViewManager.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,19 @@ - (RCTShadowView *)shadowView
192192
#else // [TODO(macOS GH#774)
193193
RCT_REMAP_VIEW_PROPERTY(opacity, alphaValue, CGFloat)
194194
#endif // ]TODO(macOS GH#774)
195+
196+
#if !TARGET_OS_OSX // TODO(macOS GH#774)
195197
RCT_REMAP_VIEW_PROPERTY(shadowColor, layer.shadowColor, CGColor)
196198
RCT_REMAP_VIEW_PROPERTY(shadowOffset, layer.shadowOffset, CGSize)
197199
RCT_REMAP_VIEW_PROPERTY(shadowOpacity, layer.shadowOpacity, float)
198200
RCT_REMAP_VIEW_PROPERTY(shadowRadius, layer.shadowRadius, CGFloat)
201+
#else // [TODO(macOS GH#774)
202+
RCT_EXPORT_VIEW_PROPERTY(shadowColor, NSColor)
203+
RCT_EXPORT_VIEW_PROPERTY(shadowOffset, CGSize)
204+
RCT_EXPORT_VIEW_PROPERTY(shadowOpacity, CGFloat)
205+
RCT_EXPORT_VIEW_PROPERTY(shadowRadius, CGFloat)
206+
#endif // ]TODO(macOS GH#774)
207+
199208
RCT_REMAP_VIEW_PROPERTY(needsOffscreenAlphaCompositing, layer.allowsGroupOpacity, BOOL)
200209
RCT_CUSTOM_VIEW_PROPERTY(overflow, YGOverflow, RCTView)
201210
{

packages/rn-tester/js/examples/AnimatedGratuitousApp/AnExSet.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,11 @@ const styles = StyleSheet.create({
139139
headerText: {
140140
fontSize: 25,
141141
color: 'white',
142-
shadowRadius: 3,
143-
shadowColor: 'black',
144-
shadowOpacity: 1,
145-
shadowOffset: {height: 1},
142+
// [TODO(macOS GH#1409)
143+
textShadowRadius: 3,
144+
textShadowColor: 'rgba(0, 0, 0, 1.0)',
145+
textShadowOffset: {height: 1, width: 0},
146+
// ]TODO(macOS GH#1409)
146147
},
147148
});
148149

0 commit comments

Comments
 (0)