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

Remove dependency on PropsParserContext #42616

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 @@ -14,6 +14,7 @@
#include <react/renderer/core/PropsParserContext.h>
#include <react/renderer/core/RawProps.h>
#include <react/renderer/graphics/Color.h>
#include <react/renderer/graphics/Float.h>
#include <react/renderer/graphics/PlatformColorParser.h>
#include <react/renderer/graphics/Point.h>
#include <react/renderer/graphics/Rect.h>
Expand All @@ -29,29 +30,7 @@ inline void fromRawValue(
const PropsParserContext& context,
const RawValue& value,
SharedColor& result) {
ColorComponents colorComponents = {0, 0, 0, 0};

if (value.hasType<int>()) {
auto argb = (int64_t)value;
auto ratio = 255.f;
colorComponents.alpha = ((argb >> 24) & 0xFF) / ratio;
colorComponents.red = ((argb >> 16) & 0xFF) / ratio;
colorComponents.green = ((argb >> 8) & 0xFF) / ratio;
colorComponents.blue = (argb & 0xFF) / ratio;
} else if (value.hasType<std::vector<float>>()) {
auto items = (std::vector<float>)value;
auto length = items.size();
react_native_expect(length == 3 || length == 4);
colorComponents.red = items.at(0);
colorComponents.green = items.at(1);
colorComponents.blue = items.at(2);
colorComponents.alpha = length == 4 ? items.at(3) : 1.0f;
} else {
result = parsePlatformColor(context, value);
return;
}

result = colorFromComponents(colorComponents);
fromRawValue(context.contextContainer, context.surfaceId, value, result);
}

#ifdef ANDROID
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.
*/

#include <react/debug/react_native_expect.h>
#include <react/renderer/core/RawValue.h>
#include <react/renderer/graphics/Color.h>
#include <react/utils/ContextContainer.h>

#pragma once

namespace facebook::react {
using parsePlatformColorFn =
SharedColor (*)(const ContextContainer&, int32_t, const RawValue&);

inline void fromRawValueShared(
const ContextContainer& contextContainer,
int32_t surfaceId,
const RawValue& value,
SharedColor& result,
parsePlatformColorFn parsePlatformColor) {
ColorComponents colorComponents = {0, 0, 0, 0};

if (value.hasType<int>()) {
auto argb = (int64_t)value;
auto ratio = 255.f;
colorComponents.alpha = ((argb >> 24) & 0xFF) / ratio;
colorComponents.red = ((argb >> 16) & 0xFF) / ratio;
colorComponents.green = ((argb >> 8) & 0xFF) / ratio;
colorComponents.blue = (argb & 0xFF) / ratio;

result = colorFromComponents(colorComponents);
} else if (value.hasType<std::vector<float>>()) {
auto items = (std::vector<float>)value;
auto length = items.size();
react_native_expect(length == 3 || length == 4);
colorComponents.red = items.at(0);
colorComponents.green = items.at(1);
colorComponents.blue = items.at(2);
colorComponents.alpha = length == 4 ? items.at(3) : 1.0f;

result = colorFromComponents(colorComponents);
} else {
result = parsePlatformColor(contextContainer, surfaceId, value);
}
}
} // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,25 @@
#pragma once

#include <fbjni/fbjni.h>
#include <react/renderer/core/PropsParserContext.h>
#include <react/renderer/core/RawProps.h>
#include <react/debug/react_native_expect.h>
#include <react/renderer/core/RawValue.h>
#include <react/renderer/graphics/Color.h>
#include <react/renderer/graphics/fromRawValueShared.h>
#include <react/utils/ContextContainer.h>
#include <unordered_map>

namespace facebook::react {

inline SharedColor parsePlatformColor(
const PropsParserContext& context,
const ContextContainer& contextContainer,
int32_t surfaceId,
const RawValue& value) {
ColorComponents colorComponents = {0, 0, 0, 0};

if (value.hasType<
std::unordered_map<std::string, std::vector<std::string>>>()) {
const auto& fabricUIManager =
context.contextContainer.at<jni::global_ref<jobject>>(
"FabricUIManager");
contextContainer.at<jni::global_ref<jobject>>("FabricUIManager");
static auto getColorFromJava =
fabricUIManager->getClass()
->getMethod<jint(jint, jni::JArrayClass<jni::JString>)>("getColor");
Expand All @@ -37,8 +39,8 @@ inline SharedColor parsePlatformColor(
for (int i = 0; i < resourcePaths.size(); i++) {
javaResourcePaths->setElement(i, *jni::make_jstring(resourcePaths[i]));
}
auto color = getColorFromJava(
fabricUIManager, context.surfaceId, *javaResourcePaths);
auto color =
getColorFromJava(fabricUIManager, surfaceId, *javaResourcePaths);

auto argb = (int64_t)color;
auto ratio = 255.f;
Expand All @@ -51,4 +53,13 @@ inline SharedColor parsePlatformColor(
return {colorFromComponents(colorComponents)};
}

inline void fromRawValue(
const ContextContainer& contextContainer,
int32_t surfaceId,
const RawValue& value,
SharedColor& result) {
fromRawValueShared(
contextContainer, surfaceId, value, result, parsePlatformColor);
}

} // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@

#pragma once

#include <react/renderer/core/PropsParserContext.h>
#include <react/renderer/core/RawProps.h>
#include <react/debug/react_native_expect.h>
#include <react/renderer/core/RawValue.h>
#include <react/renderer/graphics/Color.h>
#include <react/renderer/graphics/fromRawValueShared.h>
#include <react/utils/ContextContainer.h>

namespace facebook::react {

inline SharedColor parsePlatformColor(
const PropsParserContext& context,
const RawValue& value) {
const ContextContainer& /*contextContainer*/,
int32_t /*surfaceId*/,
const RawValue& /*value*/) {
float alpha = 0;
float red = 0;
float green = 0;
Expand All @@ -24,4 +27,13 @@ inline SharedColor parsePlatformColor(
return {colorFromComponents({red, green, blue, alpha})};
}

inline void fromRawValue(
const ContextContainer& contextContainer,
int32_t surfaceId,
const RawValue& value,
SharedColor& result) {
fromRawValueShared(
contextContainer, surfaceId, value, result, parsePlatformColor);
}

} // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@

#pragma once

#include <react/renderer/core/PropsParserContext.h>
#include <react/renderer/core/RawProps.h>
#include <react/debug/react_native_expect.h>
#include <react/renderer/core/rawValue.h>
#include <react/renderer/graphics/Color.h>
#include <react/renderer/graphics/RCTPlatformColorUtils.h>
#include <react/renderer/graphics/fromRawValueShared.h>
#include <react/utils/ContextContainer.h>
#include <unordered_map>

namespace facebook::react {

inline SharedColor parsePlatformColor(
const PropsParserContext& context,
const ContextContainer& /*contextContainer*/,
int32_t /*surfaceId*/,
const RawValue& value) {
if (value.hasType<std::unordered_map<std::string, RawValue>>()) {
auto items = (std::unordered_map<std::string, RawValue>)value;
Expand All @@ -31,4 +34,13 @@ inline SharedColor parsePlatformColor(
return clearColor();
}

inline void fromRawValue(
const ContextContainer& contextContainer,
int32_t surfaceId,
const RawValue& value,
SharedColor& result) {
fromRawValueShared(
contextContainer, surfaceId, value, result, parsePlatformColor);
}

} // namespace facebook::react
Loading