Skip to content

[Fabric] Convert UIColor to RCTUIColor shim #1523

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

Merged
merged 5 commits into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ - (instancetype)initWithFrame:(CGRect)frame

CGRect bounds = self.bounds;
_label = [[UILabel alloc] initWithFrame:bounds];
_label.backgroundColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:0.3];
_label.backgroundColor = [RCTUIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:0.3]; // TODO(macOS GH#774)
_label.layoutMargins = UIEdgeInsetsMake(12, 12, 12, 12);
_label.lineBreakMode = NSLineBreakByWordWrapping;
_label.numberOfLines = 0;
_label.textAlignment = NSTextAlignmentCenter;
_label.textColor = [UIColor whiteColor];
_label.textColor = [RCTUIColor whiteColor]; // TODO(macOS GH#774)

self.contentView = _label;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ - (instancetype)initWithFrame:(CGRect)frame
_props = defaultProps;

_label = [[UILabel alloc] initWithFrame:self.bounds];
_label.backgroundColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:0.3];
_label.backgroundColor = [RCTUIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:0.3]; // TODO(macOS GH#774)
_label.lineBreakMode = NSLineBreakByCharWrapping;
_label.numberOfLines = 0;
_label.textAlignment = NSTextAlignmentCenter;
_label.textColor = [UIColor whiteColor];
_label.textColor = [RCTUIColor whiteColor]; // TODO(macOS GH#774)
_label.allowsDefaultTighteningForTruncation = YES;
_label.adjustsFontSizeToFitWidth = YES;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ NS_ASSUME_NONNULL_BEGIN
* Provides access to `foregroundColor` prop of the component.
* Must be used by subclasses only.
*/
@property (nonatomic, strong, nullable) UIColor *foregroundColor;
@property (nonatomic, strong, nullable) RCTUIColor *foregroundColor; // TODO(macOS GH#774)

/**
* Returns the object - usually (sub)view - which represents this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
using namespace facebook::react;

@implementation RCTViewComponentView {
UIColor *_backgroundColor;
RCTUIColor *_backgroundColor; // TODO(macOS GH#774)
CALayer *_borderLayer;
BOOL _needsInvalidateLayer;
BOOL _isJSResponder;
Expand Down Expand Up @@ -71,12 +71,12 @@ - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
return CGRectContainsPoint(hitFrame, point);
}

- (UIColor *)backgroundColor
- (RCTUIColor *)backgroundColor // TODO(macOS GH#774)
{
return _backgroundColor;
}

- (void)setBackgroundColor:(UIColor *)backgroundColor
- (void)setBackgroundColor:(RCTUIColor *)backgroundColor // TODO(macOS GH#774)
{
_backgroundColor = backgroundColor;
}
Expand Down Expand Up @@ -203,13 +203,13 @@ - (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &

// `backgroundColor`
if (oldViewProps.backgroundColor != newViewProps.backgroundColor) {
self.backgroundColor = RCTUIColorFromSharedColor(newViewProps.backgroundColor);
self.backgroundColor = RCTUIColorFromSharedColor(newViewProps.backgroundColor); // TODO(macOS GH#774)
needsInvalidateLayer = YES;
}

// `foregroundColor`
if (oldViewProps.foregroundColor != newViewProps.foregroundColor) {
self.foregroundColor = RCTUIColorFromSharedColor(newViewProps.foregroundColor);
self.foregroundColor = RCTUIColorFromSharedColor(newViewProps.foregroundColor); // TODO(macOS GH#774)
}

// `shadowColor`
Expand Down
10 changes: 5 additions & 5 deletions React/Fabric/RCTConversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,26 @@ inline std::string RCTStringFromNSString(NSString *string)
return std::string{string.UTF8String ?: ""};
}

inline UIColor *_Nullable RCTUIColorFromSharedColor(facebook::react::SharedColor const &sharedColor)
inline RCTUIColor *_Nullable RCTUIColorFromSharedColor(facebook::react::SharedColor const &sharedColor) // TODO(macOS GH#774)
{
if (!sharedColor) {
return nil;
}

if (*facebook::react::clearColor() == *sharedColor) {
return [UIColor clearColor];
return [RCTUIColor clearColor]; // TODO(macOS GH#774)
}

if (*facebook::react::blackColor() == *sharedColor) {
return [UIColor blackColor];
return [RCTUIColor blackColor]; // TODO(macOS GH#774)
}

if (*facebook::react::whiteColor() == *sharedColor) {
return [UIColor whiteColor];
return [RCTUIColor whiteColor]; // TODO(macOS GH#774)
}

auto components = facebook::react::colorComponentsFromColor(sharedColor);
return [UIColor colorWithRed:components.red green:components.green blue:components.blue alpha:components.alpha];
return [RCTUIColor colorWithRed:components.red green:components.green blue:components.blue alpha:components.alpha]; // TODO(macOS GH#774)
}

inline CF_RETURNS_RETAINED CGColorRef
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
return dict;
}

static UIColor *_UIColorFromHexValue(NSNumber *hexValue)
static RCTUIColor *_UIColorFromHexValue(NSNumber *hexValue) // TODO(macOS GH#774)
{
NSUInteger hexIntValue = [hexValue unsignedIntegerValue];

Expand All @@ -140,10 +140,10 @@
CGFloat blue = ((CGFloat)((hexIntValue & 0xFF00) >> 8)) / 255.0;
CGFloat alpha = ((CGFloat)(hexIntValue & 0xFF)) / 255.0;

return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
return [RCTUIColor colorWithRed:red green:green blue:blue alpha:alpha]; // TODO(macOS GH#774)
}

static UIColor *_Nullable _UIColorFromSemanticString(NSString *semanticString)
static RCTUIColor *_Nullable _UIColorFromSemanticString(NSString *semanticString) // TODO(macOS GH#774)
{
NSString *platformColorString = [semanticString hasSuffix:kColorSuffix]
? [semanticString substringToIndex:[semanticString length] - [kColorSuffix length]]
Expand All @@ -152,17 +152,17 @@
NSDictionary<NSString *, id> *colorInfo = platformColorSelectorsDict[platformColorString];
if (colorInfo) {
SEL objcColorSelector = NSSelectorFromString([platformColorString stringByAppendingString:kColorSuffix]);
if (![UIColor respondsToSelector:objcColorSelector]) {
if (![RCTUIColor respondsToSelector:objcColorSelector]) { // TODO(macOS GH#774)
NSNumber *fallbackRGB = colorInfo[kFallbackARGBKey];
if (fallbackRGB) {
return _UIColorFromHexValue(fallbackRGB);
}
} else {
Class uiColorClass = [UIColor class];
Class uiColorClass = [RCTUIColor class]; // TODO(macOS GH#774)
IMP imp = [uiColorClass methodForSelector:objcColorSelector];
id (*getUIColor)(id, SEL) = ((id(*)(id, SEL))imp);
id colorObject = getUIColor(uiColorClass, objcColorSelector);
if ([colorObject isKindOfClass:[UIColor class]]) {
if ([colorObject isKindOfClass:[RCTUIColor class]]) { // TODO(macOS GH#774)
return colorObject;
}
}
Expand All @@ -177,7 +177,7 @@
return [NSString stringWithCString:string.c_str() encoding:encoding];
}

static inline facebook::react::ColorComponents _ColorComponentsFromUIColor(UIColor *color)
static inline facebook::react::ColorComponents _ColorComponentsFromUIColor(RCTUIColor *color) // TODO(macOS GH#774)
{
CGFloat rgba[4];
RCTGetRGBAColorComponents(color.CGColor, rgba);
Expand All @@ -188,7 +188,7 @@
{
for (const auto &semanticCString : semanticItems) {
NSString *semanticNSString = _NSStringFromCString(semanticCString);
UIColor *uiColor = [UIColor colorNamed:semanticNSString];
RCTUIColor *uiColor = [RCTUIColor colorNamed:semanticNSString]; // TODO(macOS GH#774)
if (uiColor != nil) {
return _ColorComponentsFromUIColor(uiColor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ inline static CGFloat RCTEffectiveFontSizeMultiplierFromTextAttributes(const Tex
: 1.0;
}

inline static UIColor *RCTEffectiveForegroundColorFromTextAttributes(const TextAttributes &textAttributes)
inline static RCTUIColor *RCTEffectiveForegroundColorFromTextAttributes(const TextAttributes &textAttributes) // TODO(macOS GH#774)
{
UIColor *effectiveForegroundColor = RCTUIColorFromSharedColor(textAttributes.foregroundColor) ?: [UIColor blackColor];
RCTUIColor *effectiveForegroundColor = RCTUIColorFromSharedColor(textAttributes.foregroundColor) ?: [RCTUIColor blackColor]; // TODO(macOS GH#774)

if (!isnan(textAttributes.opacity)) {
effectiveForegroundColor = [effectiveForegroundColor
Expand All @@ -95,16 +95,16 @@ inline static CGFloat RCTEffectiveFontSizeMultiplierFromTextAttributes(const Tex
return effectiveForegroundColor;
}

inline static UIColor *RCTEffectiveBackgroundColorFromTextAttributes(const TextAttributes &textAttributes)
inline static RCTUIColor *RCTEffectiveBackgroundColorFromTextAttributes(const TextAttributes &textAttributes) // TODO(macOS GH#774)
{
UIColor *effectiveBackgroundColor = RCTUIColorFromSharedColor(textAttributes.backgroundColor);
RCTUIColor *effectiveBackgroundColor = RCTUIColorFromSharedColor(textAttributes.backgroundColor); // TODO(macOS GH#774)

if (effectiveBackgroundColor && !isnan(textAttributes.opacity)) {
effectiveBackgroundColor = [effectiveBackgroundColor
colorWithAlphaComponent:CGColorGetAlpha(effectiveBackgroundColor.CGColor) * textAttributes.opacity];
}

return effectiveBackgroundColor ?: [UIColor clearColor];
return effectiveBackgroundColor ?: [RCTUIColor clearColor]; // TODO(macOS GH#774)
}

NSDictionary<NSAttributedStringKey, id> *RCTNSTextAttributesFromTextAttributes(TextAttributes const &textAttributes)
Expand All @@ -118,7 +118,7 @@ inline static CGFloat RCTEffectiveFontSizeMultiplierFromTextAttributes(const Tex
}

// Colors
UIColor *effectiveForegroundColor = RCTEffectiveForegroundColorFromTextAttributes(textAttributes);
RCTUIColor *effectiveForegroundColor = RCTEffectiveForegroundColorFromTextAttributes(textAttributes); // TODO(macOS GH#774)

if (textAttributes.foregroundColor || !isnan(textAttributes.opacity)) {
attributes[NSForegroundColorAttributeName] = effectiveForegroundColor;
Expand Down Expand Up @@ -174,7 +174,7 @@ inline static CGFloat RCTEffectiveFontSizeMultiplierFromTextAttributes(const Tex
NSUnderlineStyle style = RCTNSUnderlineStyleFromTextDecorationStyle(
textAttributes.textDecorationStyle.value_or(TextDecorationStyle::Solid));

UIColor *textDecorationColor = RCTUIColorFromSharedColor(textAttributes.textDecorationColor);
RCTUIColor *textDecorationColor = RCTUIColorFromSharedColor(textAttributes.textDecorationColor); // TODO(macOS GH#774)

// Underline
if (textDecorationLineType == TextDecorationLineType::Underline ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,24 @@ inline static NSUnderlineStyle RCTNSUnderlineStyleFromTextDecorationStyle(TextDe
}
}

inline static UIColor *RCTUIColorFromSharedColor(const SharedColor &sharedColor)
inline static RCTUIColor *RCTUIColorFromSharedColor(const SharedColor &sharedColor) // TODO(macOS GH#774)
{
if (!sharedColor) {
return nil;
}

if (*facebook::react::clearColor() == *sharedColor) {
return [UIColor clearColor];
return [RCTUIColor clearColor]; // TODO(macOS GH#774)
}

if (*facebook::react::blackColor() == *sharedColor) {
return [UIColor blackColor];
return [RCTUIColor blackColor]; // TODO(macOS GH#774)
}

if (*facebook::react::whiteColor() == *sharedColor) {
return [UIColor whiteColor];
return [RCTUIColor whiteColor]; // TODO(macOS GH#774)
}

auto components = facebook::react::colorComponentsFromColor(sharedColor);
return [UIColor colorWithRed:components.red green:components.green blue:components.blue alpha:components.alpha];
return [RCTUIColor colorWithRed:components.red green:components.green blue:components.blue alpha:components.alpha]; // TODO(macOS GH#774)
}