-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[Fabric] Add support for PlatformColor #7801
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/react-native-windows-b9691dda-2b02-4670-a043-8ecd90525929.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "prerelease", | ||
"comment": "[Fabric] Add support for PlatformColor", | ||
"packageName": "react-native-windows", | ||
"email": "[email protected]", | ||
"dependentChangeType": "patch" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
vnext/Microsoft.ReactNative/Fabric/platform/react/renderer/graphics/Color.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
#include "Color.h" | ||
#include <Utils/ValueUtils.h> | ||
|
||
namespace facebook { | ||
namespace react { | ||
|
||
xaml::Media::Brush SharedColor::AsWindowsBrush() const { | ||
if (!m_color) | ||
return nullptr; | ||
if (!m_color->m_platformColor.empty()) { | ||
return Microsoft::ReactNative::BrushFromColorObject(winrt::to_hstring(m_color->m_platformColor)); | ||
} | ||
return xaml::Media::SolidColorBrush(m_color->m_color); | ||
} | ||
|
||
SharedColor colorFromComponents(ColorComponents components) { | ||
float ratio = 255; | ||
return SharedColor(ui::ColorHelper::FromArgb( | ||
(int)round(components.alpha * ratio) & 0xff, | ||
(int)round(components.red * ratio) & 0xff, | ||
(int)round(components.green * ratio) & 0xff, | ||
(int)round(components.blue * ratio) & 0xff)); | ||
} | ||
|
||
ColorComponents colorComponentsFromColor(SharedColor sharedColor) { | ||
float ratio = 255; | ||
auto color = sharedColor.AsWindowsColor(); | ||
return ColorComponents{ | ||
(float)color.R / ratio, (float)color.G / ratio, (float)color.B / ratio, (float)color.A / ratio}; | ||
} | ||
|
||
SharedColor clearColor() { | ||
static SharedColor color = colorFromComponents(ColorComponents{0, 0, 0, 0}); | ||
return color; | ||
} | ||
|
||
SharedColor blackColor() { | ||
static SharedColor color = colorFromComponents(ColorComponents{0, 0, 0, 1}); | ||
return color; | ||
} | ||
|
||
SharedColor whiteColor() { | ||
static SharedColor color = colorFromComponents(ColorComponents{1, 1, 1, 1}); | ||
return color; | ||
} | ||
|
||
} // namespace react | ||
} // namespace facebook |
5 changes: 5 additions & 0 deletions
5
vnext/Microsoft.ReactNative/Fabric/platform/react/renderer/graphics/Color.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
// RN has some weird include directory redirections..this forwards the include to the right place | ||
#include <react/renderer/graphics/platform/cxx/react/renderer/graphics/Color.h> |
5 changes: 5 additions & 0 deletions
5
vnext/Microsoft.ReactNative/Fabric/platform/react/renderer/graphics/Float.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
// RN has some weird include directory redirections..this forwards the include to the right place | ||
#include <react/renderer/graphics/platform/cxx/react/renderer/graphics/Float.h> |
256 changes: 256 additions & 0 deletions
256
vnext/Microsoft.ReactNative/Fabric/platform/react/renderer/graphics/conversions.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,256 @@ | ||
/* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <better/map.h> | ||
#include <folly/dynamic.h> | ||
#include <glog/logging.h> | ||
#include <react/debug/react_native_assert.h> | ||
#include <react/renderer/core/RawProps.h> | ||
#include <react/renderer/graphics/Color.h> | ||
#include <react/renderer/graphics/Geometry.h> | ||
|
||
namespace facebook { | ||
namespace react { | ||
|
||
#pragma mark - Color | ||
|
||
inline void fromRawValue(const RawValue &value, SharedColor &result) { | ||
float red = 0; | ||
float green = 0; | ||
float blue = 0; | ||
float alpha = 0; | ||
|
||
if (value.hasType<int>()) { | ||
auto argb = (int64_t)value; | ||
auto ratio = 255.f; | ||
alpha = ((argb >> 24) & 0xFF) / ratio; | ||
red = ((argb >> 16) & 0xFF) / ratio; | ||
green = ((argb >> 8) & 0xFF) / ratio; | ||
blue = (argb & 0xFF) / ratio; | ||
} else if (value.hasType<std::vector<float>>()) { | ||
auto items = (std::vector<float>)value; | ||
auto length = items.size(); | ||
react_native_assert(length == 3 || length == 4); | ||
red = items.at(0); | ||
green = items.at(1); | ||
blue = items.at(2); | ||
alpha = length == 4 ? items.at(3) : 1.0f; | ||
// [Windows - Add support for windowsBrush colors (PlatformColor) | ||
} else if (value.hasType<better::map<std::string, std::string>>()) { | ||
auto map = (better::map<std::string, std::string>)value; | ||
for (const auto &pair : map) { | ||
if (pair.first == "windowsbrush") { | ||
result = SharedColor(std::string(pair.second)); | ||
return; | ||
} | ||
} | ||
} | ||
// Windows] | ||
|
||
result = colorFromComponents({red, green, blue, alpha}); | ||
} | ||
|
||
#ifdef ANDROID | ||
|
||
inline folly::dynamic toDynamic(const SharedColor &color) { | ||
ColorComponents components = colorComponentsFromColor(color); | ||
auto ratio = 255.f; | ||
return ( | ||
((int)round(components.alpha * ratio) & 0xff) << 24 | ((int)round(components.red * ratio) & 0xff) << 16 | | ||
((int)round(components.green * ratio) & 0xff) << 8 | ((int)round(components.blue * ratio) & 0xff)); | ||
} | ||
|
||
inline int toMapBuffer(const SharedColor &color) { | ||
ColorComponents components = colorComponentsFromColor(color); | ||
auto ratio = 255.f; | ||
return ( | ||
((int)round(components.alpha * ratio) & 0xff) << 24 | ((int)round(components.red * ratio) & 0xff) << 16 | | ||
((int)round(components.green * ratio) & 0xff) << 8 | ((int)round(components.blue * ratio) & 0xff)); | ||
} | ||
|
||
#endif | ||
|
||
inline std::string toString(const SharedColor &value) { | ||
ColorComponents components = colorComponentsFromColor(value); | ||
auto ratio = 255.f; | ||
return "rgba(" + folly::to<std::string>(round(components.red * ratio)) + ", " + | ||
folly::to<std::string>(round(components.green * ratio)) + ", " + | ||
folly::to<std::string>(round(components.blue * ratio)) + ", " + | ||
folly::to<std::string>(round(components.alpha * ratio)) + ")"; | ||
} | ||
|
||
#pragma mark - Geometry | ||
|
||
inline void fromRawValue(const RawValue &value, Point &result) { | ||
if (value.hasType<better::map<std::string, Float>>()) { | ||
auto map = (better::map<std::string, Float>)value; | ||
for (const auto &pair : map) { | ||
if (pair.first == "x") { | ||
result.x = pair.second; | ||
} else if (pair.first == "y") { | ||
result.y = pair.second; | ||
} | ||
} | ||
return; | ||
} | ||
|
||
react_native_assert(value.hasType<std::vector<Float>>()); | ||
if (value.hasType<std::vector<Float>>()) { | ||
auto array = (std::vector<Float>)value; | ||
react_native_assert(array.size() == 2); | ||
if (array.size() >= 2) { | ||
result = {array.at(0), array.at(1)}; | ||
} else { | ||
result = {0, 0}; | ||
LOG(ERROR) << "Unsupported Point vector size: " << array.size(); | ||
} | ||
} else { | ||
LOG(ERROR) << "Unsupported Point type"; | ||
} | ||
} | ||
|
||
inline void fromRawValue(const RawValue &value, Size &result) { | ||
if (value.hasType<better::map<std::string, Float>>()) { | ||
auto map = (better::map<std::string, Float>)value; | ||
for (const auto &pair : map) { | ||
if (pair.first == "width") { | ||
result.width = pair.second; | ||
} else if (pair.first == "height") { | ||
result.height = pair.second; | ||
} else { | ||
LOG(ERROR) << "Unsupported Size map key: " << pair.first; | ||
react_native_assert(false); | ||
} | ||
} | ||
return; | ||
} | ||
|
||
react_native_assert(value.hasType<std::vector<Float>>()); | ||
if (value.hasType<std::vector<Float>>()) { | ||
auto array = (std::vector<Float>)value; | ||
react_native_assert(array.size() == 2); | ||
if (array.size() >= 2) { | ||
result = {array.at(0), array.at(1)}; | ||
} else { | ||
result = {0, 0}; | ||
LOG(ERROR) << "Unsupported Size vector size: " << array.size(); | ||
} | ||
} else { | ||
LOG(ERROR) << "Unsupported Size type"; | ||
} | ||
} | ||
|
||
inline void fromRawValue(const RawValue &value, EdgeInsets &result) { | ||
if (value.hasType<Float>()) { | ||
auto number = (Float)value; | ||
result = {number, number, number, number}; | ||
} | ||
|
||
if (value.hasType<better::map<std::string, Float>>()) { | ||
auto map = (better::map<std::string, Float>)value; | ||
for (const auto &pair : map) { | ||
if (pair.first == "top") { | ||
result.top = pair.second; | ||
} else if (pair.first == "left") { | ||
result.left = pair.second; | ||
} else if (pair.first == "bottom") { | ||
result.bottom = pair.second; | ||
} else if (pair.first == "right") { | ||
result.right = pair.second; | ||
} else { | ||
LOG(ERROR) << "Unsupported EdgeInsets map key: " << pair.first; | ||
react_native_assert(false); | ||
} | ||
} | ||
return; | ||
} | ||
|
||
react_native_assert(value.hasType<std::vector<Float>>()); | ||
if (value.hasType<std::vector<Float>>()) { | ||
auto array = (std::vector<Float>)value; | ||
react_native_assert(array.size() == 4); | ||
if (array.size() >= 4) { | ||
result = {array.at(0), array.at(1), array.at(2), array.at(3)}; | ||
} else { | ||
result = {0, 0, 0, 0}; | ||
LOG(ERROR) << "Unsupported EdgeInsets vector size: " << array.size(); | ||
} | ||
} else { | ||
LOG(ERROR) << "Unsupported EdgeInsets type"; | ||
} | ||
} | ||
|
||
inline void fromRawValue(const RawValue &value, CornerInsets &result) { | ||
if (value.hasType<Float>()) { | ||
auto number = (Float)value; | ||
result = {number, number, number, number}; | ||
return; | ||
} | ||
|
||
if (value.hasType<better::map<std::string, Float>>()) { | ||
auto map = (better::map<std::string, Float>)value; | ||
for (const auto &pair : map) { | ||
if (pair.first == "topLeft") { | ||
result.topLeft = pair.second; | ||
} else if (pair.first == "topRight") { | ||
result.topRight = pair.second; | ||
} else if (pair.first == "bottomLeft") { | ||
result.bottomLeft = pair.second; | ||
} else if (pair.first == "bottomRight") { | ||
result.bottomRight = pair.second; | ||
} else { | ||
LOG(ERROR) << "Unsupported CornerInsets map key: " << pair.first; | ||
react_native_assert(false); | ||
} | ||
} | ||
return; | ||
} | ||
|
||
react_native_assert(value.hasType<std::vector<Float>>()); | ||
if (value.hasType<std::vector<Float>>()) { | ||
auto array = (std::vector<Float>)value; | ||
react_native_assert(array.size() == 4); | ||
if (array.size() >= 4) { | ||
result = {array.at(0), array.at(1), array.at(2), array.at(3)}; | ||
} else { | ||
LOG(ERROR) << "Unsupported CornerInsets vector size: " << array.size(); | ||
} | ||
} | ||
|
||
// Error case - we should only here if all other supported cases fail | ||
// In dev we would crash on assert before this point | ||
result = {0, 0, 0, 0}; | ||
LOG(ERROR) << "Unsupported CornerInsets type"; | ||
} | ||
|
||
inline std::string toString(const Point &point) { | ||
return "{" + folly::to<std::string>(point.x) + ", " + folly::to<std::string>(point.y) + "}"; | ||
} | ||
|
||
inline std::string toString(const Size &size) { | ||
return "{" + folly::to<std::string>(size.width) + ", " + folly::to<std::string>(size.height) + "}"; | ||
} | ||
|
||
inline std::string toString(const Rect &rect) { | ||
return "{" + toString(rect.origin) + ", " + toString(rect.size) + "}"; | ||
} | ||
|
||
inline std::string toString(const EdgeInsets &edgeInsets) { | ||
return "{" + folly::to<std::string>(edgeInsets.left) + ", " + folly::to<std::string>(edgeInsets.top) + ", " + | ||
folly::to<std::string>(edgeInsets.right) + ", " + folly::to<std::string>(edgeInsets.bottom) + "}"; | ||
} | ||
|
||
inline std::string toString(const CornerInsets &cornerInsets) { | ||
return "{" + folly::to<std::string>(cornerInsets.topLeft) + ", " + folly::to<std::string>(cornerInsets.topRight) + | ||
", " + folly::to<std::string>(cornerInsets.bottomLeft) + ", " + folly::to<std::string>(cornerInsets.bottomRight) + | ||
"}"; | ||
} | ||
|
||
} // namespace react | ||
} // namespace facebook |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.