Skip to content

Commit

Permalink
Apply review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongwuzw committed Jan 3, 2024
1 parent 84d6c97 commit cbf9e4f
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -611,8 +611,7 @@ - (void)invalidateLayer
borderMetrics.borderWidths.left == 0 ||
colorComponentsFromColor(borderMetrics.borderColors.left).alpha == 0 || self.clipsToBounds);

CGColorRef backgroundColor;
backgroundColor = [_backgroundColor resolvedColorWithTraitCollection:self.traitCollection].CGColor;
CGColorRef backgroundColor = [_backgroundColor resolvedColorWithTraitCollection:self.traitCollection].CGColor;

if (useCoreAnimationBorderRendering) {
layer.mask = nil;
Expand Down
8 changes: 2 additions & 6 deletions packages/react-native/React/Fabric/RCTConversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,8 @@ inline UIColor *_Nullable RCTUIColorFromSharedColor(const facebook::react::Share
}

facebook::react::Color color = *sharedColor;
switch (color.getKind()) {
case facebook::react::DynamicKind:
return RCTUIColorFromDynamicColor(color);
case facebook::react::UndefinedKind:
case facebook::react::ComponentsKind:
break;
if (color.getKind() == facebook::react::DynamicKind) {
return RCTUIColorFromDynamicColor(color);
}

auto components = facebook::react::colorComponentsFromColor(sharedColor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ SharedColor colorFromComponents(ColorComponents components) {
}

ColorComponents colorComponentsFromColor(SharedColor sharedColor) {
return colorComponentsFromHostPlatformColor(*sharedColor);
return (*sharedColor).getColorComponents();
}

SharedColor clearColor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
#include <react/renderer/graphics/ColorComponents.h>
#include <react/utils/hash_combine.h>
#include <cmath>
#include <variant>

namespace facebook::react {

enum ColorKind { UndefinedKind, ComponentsKind, DynamicKind };
enum ColorKind { ComponentsKind, DynamicKind };

struct DynamicColor {
int32_t lightColor = 0;
Expand All @@ -30,11 +31,8 @@ struct DynamicColor {

class Color {
public:
Color() : Color(UndefinedKind) {}
Color(DynamicColor dynamicColor)
: kind_(DynamicKind),
dynamicColor_(dynamicColor),
color_(dynamicColor.lightColor) {}
Color(int32_t color) : kind_(ComponentsKind), color_(color) {}
Color(DynamicColor dynamicColor) : kind_(DynamicKind), color_(dynamicColor) {}
Color(ColorComponents components) {
float ratio = 255;
kind_ = ComponentsKind;
Expand All @@ -43,49 +41,47 @@ class Color {
((int)round(components.green * ratio) & 0xff) << 8 |
((int)round(components.blue * ratio) & 0xff);
}
int32_t getColor() const {
return color_;
}
int32_t getColor() const;
ColorKind getKind() const {
return kind_;
}
DynamicColor getDynamicColor() const {
return dynamicColor_;
return std::get<DynamicColor>(color_);
}
ColorComponents getColorComponents() const {
float ratio = 255;
int32_t primitiveColor = getColor();
return ColorComponents{
(float)((primitiveColor >> 16) & 0xff) / ratio,
(float)((primitiveColor >> 8) & 0xff) / ratio,
(float)((primitiveColor >> 0) & 0xff) / ratio,
(float)((primitiveColor >> 24) & 0xff) / ratio};
}
bool operator==(const Color& other) const {
return kind_ == other.kind_ && color_ == other.color_ &&
dynamicColor_ == other.dynamicColor_;
return kind_ == other.kind_ && color_ == other.color_;
}
bool operator!=(const Color& other) const {
return kind_ != other.kind_ || color_ != other.color_ ||
dynamicColor_ != other.dynamicColor_;
return kind_ != other.kind_ || color_ != other.color_;
}
operator int32_t() const {
return getColor();
}

private:
Color(ColorKind kind) : kind_(kind), color_(0) {}
ColorKind kind_;
int32_t color_ = 0;
DynamicColor dynamicColor_;
std::variant<int32_t, DynamicColor> color_;
};

namespace HostPlatformColor {
static const facebook::react::Color UndefinedColor = Color();
static const facebook::react::Color UndefinedColor =
std::numeric_limits<int32_t>::max();
}

inline Color hostPlatformColorFromComponents(ColorComponents components) {
return Color(components);
}

inline ColorComponents colorComponentsFromHostPlatformColor(Color color) {
float ratio = 255;
int32_t primitiveColor = color.getColor();
return ColorComponents{
(float)((primitiveColor >> 16) & 0xff) / ratio,
(float)((primitiveColor >> 8) & 0xff) / ratio,
(float)((primitiveColor >> 0) & 0xff) / ratio,
(float)((primitiveColor >> 24) & 0xff) / ratio};
}

} // namespace facebook::react

template <>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#import "HostPlatformColor.h"

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#include <react/renderer/graphics/HostPlatformColor.h>

#include <string>

using namespace facebook::react;

NS_ASSUME_NONNULL_BEGIN

namespace facebook::react {

int32_t RCTPlatformColorGetCurrentColorFromDynamicColor(facebook::react::DynamicColor dynamicColor)
{
int32_t lightColor = dynamicColor.lightColor;
int32_t darkColor = dynamicColor.darkColor;
int32_t highContrastLightColor = dynamicColor.highContrastLightColor;
int32_t highContrastDarkColor = dynamicColor.highContrastDarkColor;
UITraitCollection *currentTraitCollection = [UITraitCollection currentTraitCollection];
if (currentTraitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
if (currentTraitCollection.accessibilityContrast == UIAccessibilityContrastHigh && highContrastDarkColor) {
return highContrastDarkColor;
} else {
return darkColor;
}
} else {
if (currentTraitCollection.accessibilityContrast == UIAccessibilityContrastHigh && highContrastLightColor) {
return highContrastLightColor;
} else {
return lightColor;
}
}
}

int32_t Color::getColor() const
{
if (kind_ == DynamicKind) {
return RCTPlatformColorGetCurrentColorFromDynamicColor(getDynamicColor());
}
return std::get<int32_t>(color_);
}
} // namespace facebook::react

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,11 @@
#include <react/renderer/core/PropsParserContext.h>
#include <react/renderer/core/RawProps.h>
#include <react/renderer/graphics/Color.h>
#include <react/renderer/graphics/RCTPlatformColorUtils.h>
#include <unordered_map>

namespace facebook::react {

inline SharedColor parsePlatformColor(
SharedColor parsePlatformColor(
const PropsParserContext& context,
const RawValue& value) {
if (value.hasType<std::unordered_map<std::string, RawValue>>()) {
auto items = (std::unordered_map<std::string, RawValue>)value;
if (items.find("semantic") != items.end() &&
items.at("semantic").hasType<std::vector<std::string>>()) {
auto semanticItems = (std::vector<std::string>)items.at("semantic");
return {colorFromComponents(
RCTPlatformColorComponentsFromSemanticItems(semanticItems))};
} else if (
items.find("dynamic") != items.end() &&
items.at("dynamic")
.hasType<std::unordered_map<std::string, RawValue>>()) {
auto dynamicItems =
(std::unordered_map<std::string, RawValue>)items.at("dynamic");
return RCTPlatformColorComponentsFromDynamicItems(context, dynamicItems);
}
}

return clearColor();
}
const RawValue& value);

} // namespace facebook::react
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#import "PlatformColorParser.h"

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#include <react/renderer/core/graphicsConversions.h>
#include <react/renderer/graphics/HostPlatformColor.h>
#include <react/renderer/graphics/RCTPlatformColorUtils.h>
#include <string>
#include <unordered_map>

using namespace facebook::react;

NS_ASSUME_NONNULL_BEGIN

namespace facebook::react {

inline facebook::react::SharedColor RCTPlatformColorComponentsFromDynamicItems(
const facebook::react::PropsParserContext &context,
std::unordered_map<std::string, facebook::react::RawValue> &dynamicItems)
{
SharedColor lightSharedColor{};
SharedColor darkSharedColor{};
SharedColor highContrastLightSharedColor{};
SharedColor highContrastDarkSharedColor{};
if (dynamicItems.count("light")) {
fromRawValue(context, dynamicItems.at("light"), lightSharedColor);
}
if (dynamicItems.count("dark")) {
fromRawValue(context, dynamicItems.at("dark"), darkSharedColor);
}
if (dynamicItems.count("highContrastLight")) {
fromRawValue(context, dynamicItems.at("highContrastLight"), highContrastLightSharedColor);
}
if (dynamicItems.count("highContrastDark")) {
fromRawValue(context, dynamicItems.at("highContrastDark"), highContrastDarkSharedColor);
}

Color color = Color(DynamicColor{
(*lightSharedColor).getColor(),
(*darkSharedColor).getColor(),
(*highContrastLightSharedColor).getColor(),
(*highContrastDarkSharedColor).getColor()});
return SharedColor(color);
}

SharedColor parsePlatformColor(const PropsParserContext &context, const RawValue &value)
{
if (value.hasType<std::unordered_map<std::string, RawValue>>()) {
auto items = (std::unordered_map<std::string, RawValue>)value;
if (items.find("semantic") != items.end() && items.at("semantic").hasType<std::vector<std::string>>()) {
auto semanticItems = (std::vector<std::string>)items.at("semantic");
return {colorFromComponents(RCTPlatformColorComponentsFromSemanticItems(semanticItems))};
} else if (
items.find("dynamic") != items.end() &&
items.at("dynamic").hasType<std::unordered_map<std::string, RawValue>>()) {
auto dynamicItems = (std::unordered_map<std::string, RawValue>)items.at("dynamic");
return RCTPlatformColorComponentsFromDynamicItems(context, dynamicItems);
}
}

return clearColor();
}

} // namespace facebook::react

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,8 @@

#pragma once

#include <react/renderer/core/RawProps.h>
#include <react/renderer/graphics/Color.h>
#include <vector>

facebook::react::ColorComponents RCTPlatformColorComponentsFromSemanticItems(
std::vector<std::string>& semanticItems);

facebook::react::SharedColor RCTPlatformColorComponentsFromDynamicItems(
const facebook::react::PropsParserContext& context,
std::unordered_map<std::string, facebook::react::RawValue>& dynamicItems);
Original file line number Diff line number Diff line change
Expand Up @@ -203,33 +203,4 @@
return {0, 0, 0, 0};
}

facebook::react::SharedColor RCTPlatformColorComponentsFromDynamicItems(
const facebook::react::PropsParserContext &context,
std::unordered_map<std::string, facebook::react::RawValue> &dynamicItems)
{
SharedColor lightSharedColor{};
SharedColor darkSharedColor{};
SharedColor highContrastLightSharedColor{};
SharedColor highContrastDarkSharedColor{};
if (dynamicItems.count("light")) {
fromRawValue(context, dynamicItems.at("light"), lightSharedColor);
}
if (dynamicItems.count("dark")) {
fromRawValue(context, dynamicItems.at("dark"), darkSharedColor);
}
if (dynamicItems.count("highContrastLight")) {
fromRawValue(context, dynamicItems.at("highContrastLight"), highContrastLightSharedColor);
}
if (dynamicItems.count("highContrastDark")) {
fromRawValue(context, dynamicItems.at("highContrastDark"), highContrastDarkSharedColor);
}

Color color = Color(DynamicColor{
(*lightSharedColor).getColor(),
(*darkSharedColor).getColor(),
(*highContrastLightSharedColor).getColor(),
(*highContrastDarkSharedColor).getColor()});
return SharedColor(color);
}

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ inline UIColor *_Nullable RCTUIColorFromDynamicColor(const facebook::react::Colo
return nil;
}

inline static UIColor *RCTUIColorFromSharedColor(const facebook::react::SharedColor &sharedColor)
// TODO: this file has some duplicates method, we can remove it
inline static UIColor *_Nullable RCTUIColorFromSharedColor(const facebook::react::SharedColor &sharedColor)
{
if (!sharedColor) {
return nil;
Expand All @@ -151,12 +152,8 @@ inline static UIColor *RCTUIColorFromSharedColor(const facebook::react::SharedCo
}

facebook::react::Color color = *sharedColor;
switch (color.getKind()) {
case facebook::react::DynamicKind:
return RCTUIColorFromDynamicColor(color);
case facebook::react::UndefinedKind:
case facebook::react::ComponentsKind:
break;
if (color.getKind() == facebook::react::DynamicKind) {
return RCTUIColorFromDynamicColor(color);
}

auto components = facebook::react::colorComponentsFromColor(sharedColor);
Expand Down

0 comments on commit cbf9e4f

Please sign in to comment.