Skip to content
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

[Fabric] Convert UIColor to RCTUIColor shim #2

Closed
wants to merge 1 commit into from
Closed
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];
_label.layoutMargins = UIEdgeInsetsMake(12, 12, 12, 12);
_label.lineBreakMode = NSLineBreakByWordWrapping;
_label.numberOfLines = 0;
_label.textAlignment = NSTextAlignmentCenter;
_label.textColor = [UIColor whiteColor];
_label.textColor = [RCTUIColor whiteColor];

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];
_label.lineBreakMode = NSLineBreakByCharWrapping;
_label.numberOfLines = 0;
_label.textAlignment = NSTextAlignmentCenter;
_label.textColor = [UIColor whiteColor];
_label.textColor = [RCTUIColor whiteColor];
_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;

/**
* 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;
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
{
return _backgroundColor;
}

- (void)setBackgroundColor:(UIColor *)backgroundColor
- (void)setBackgroundColor:(RCTUIColor *)backgroundColor
{
_backgroundColor = backgroundColor;
}
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)
{
if (!sharedColor) {
return nil;
}

if (*facebook::react::clearColor() == *sharedColor) {
return [UIColor clearColor];
return [RCTUIColor clearColor];
}

if (*facebook::react::blackColor() == *sharedColor) {
return [UIColor blackColor];
return [RCTUIColor blackColor];
}

if (*facebook::react::whiteColor() == *sharedColor) {
return [UIColor whiteColor];
return [RCTUIColor whiteColor];
}

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];
}

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)
{
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];
}

static UIColor *_Nullable _UIColorFromSemanticString(NSString *semanticString)
static RCTUIColor *_Nullable _UIColorFromSemanticString(NSString *semanticString)
{
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]) {
NSNumber *fallbackRGB = colorInfo[kFallbackARGBKey];
if (fallbackRGB) {
return _UIColorFromHexValue(fallbackRGB);
}
} else {
Class uiColorClass = [UIColor class];
Class uiColorClass = [RCTUIColor class];
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]]) {
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)
{
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];
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)
{
UIColor *effectiveForegroundColor = RCTUIColorFromSharedColor(textAttributes.foregroundColor) ?: [UIColor blackColor];
RCTUIColor *effectiveForegroundColor = RCTUIColorFromSharedColor(textAttributes.foregroundColor) ?: [RCTUIColor blackColor];

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)
{
UIColor *effectiveBackgroundColor = RCTUIColorFromSharedColor(textAttributes.backgroundColor);
RCTUIColor *effectiveBackgroundColor = RCTUIColorFromSharedColor(textAttributes.backgroundColor);

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

return effectiveBackgroundColor ?: [UIColor clearColor];
return effectiveBackgroundColor ?: [RCTUIColor clearColor];
}

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);

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);

// 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)
{
if (!sharedColor) {
return nil;
}

if (*facebook::react::clearColor() == *sharedColor) {
return [UIColor clearColor];
return [RCTUIColor clearColor];
}

if (*facebook::react::blackColor() == *sharedColor) {
return [UIColor blackColor];
return [RCTUIColor blackColor];
}

if (*facebook::react::whiteColor() == *sharedColor) {
return [UIColor whiteColor];
return [RCTUIColor whiteColor];
}

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];
}