Skip to content

Commit a62f622

Browse files
CopilotSaadnajmi
andcommitted
Refactor: Move macOS-specific color code to dedicated platform folder
Co-authored-by: Saadnajmi <6722175+Saadnajmi@users.noreply.github.com>
1 parent 2e8e307 commit a62f622

File tree

5 files changed

+165
-59
lines changed

5 files changed

+165
-59
lines changed

packages/react-native/ReactCommon/react/renderer/graphics/React-graphics.podspec

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ boost_config = get_boost_config()
2424
boost_compiler_flags = boost_config[:compiler_flags]
2525

2626
Pod::Spec.new do |s|
27-
source_files = "*.{m,mm,cpp,h}", "platform/ios/**/*.{m,mm,cpp,h}"
27+
# [macOS Use platform specific sources
28+
ios_source_files = "*.{m,mm,cpp,h}", "platform/ios/**/*.{m,mm,cpp,h}"
29+
macos_source_files = "*.{m,mm,cpp,h}", "platform/ios/**/*.{m,mm,cpp,h}", "platform/macos/**/*.{m,mm,cpp,h}"
30+
# macOS]
2831
header_search_paths = [
2932
"\"$(PODS_ROOT)/boost\"",
3033
"\"$(PODS_TARGET_SRCROOT)/../../../\"",
@@ -43,7 +46,11 @@ Pod::Spec.new do |s|
4346
s.platforms = min_supported_versions
4447
s.source = source
4548
s.compiler_flags = folly_compiler_flags + ' ' + boost_compiler_flags
46-
s.source_files = source_files
49+
# [macOS Use platform specific sources
50+
s.ios.source_files = ios_source_files
51+
s.visionos.source_files = ios_source_files
52+
s.osx.source_files = macos_source_files
53+
# macOS]
4754
s.header_dir = "react/renderer/graphics"
4855
# [macOS Restrict UIKit to iOS and visionOS
4956
s.ios.framework = "UIKit"
@@ -54,7 +61,7 @@ Pod::Spec.new do |s|
5461
if ENV['USE_FRAMEWORKS']
5562
s.module_name = "React_graphics"
5663
s.header_mappings_dir = "../../.."
57-
header_search_paths = header_search_paths + ["\"$(PODS_TARGET_SRCROOT)/platform/ios\""]
64+
header_search_paths = header_search_paths + ["\"$(PODS_TARGET_SRCROOT)/platform/ios\"", "\"$(PODS_TARGET_SRCROOT)/platform/macos\""] # [macOS]
5865
end
5966

6067
s.pod_target_xcconfig = { "USE_HEADERMAP" => "NO",

packages/react-native/ReactCommon/react/renderer/graphics/platform/ios/react/renderer/graphics/HostPlatformColor.mm

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -164,33 +164,6 @@ int32_t ColorFromUIColor(const std::shared_ptr<void> &uiColor)
164164
return uiColor;
165165
}
166166

167-
#if TARGET_OS_OSX // [macOS
168-
RCTUIColor *_Nullable UIColorFromColorWithSystemEffect(
169-
RCTUIColor *baseColor,
170-
const std::string &systemEffectString) // [macOS]
171-
{
172-
if (baseColor == nil) {
173-
return nil;
174-
}
175-
176-
NSColor *colorWithEffect = baseColor;
177-
if (!systemEffectString.empty()) {
178-
if (systemEffectString == "none") {
179-
colorWithEffect = [baseColor colorWithSystemEffect:NSColorSystemEffectNone];
180-
} else if (systemEffectString == "pressed") {
181-
colorWithEffect = [baseColor colorWithSystemEffect:NSColorSystemEffectPressed];
182-
} else if (systemEffectString == "deepPressed") {
183-
colorWithEffect = [baseColor colorWithSystemEffect:NSColorSystemEffectDeepPressed];
184-
} else if (systemEffectString == "disabled") {
185-
colorWithEffect = [baseColor colorWithSystemEffect:NSColorSystemEffectDisabled];
186-
} else if (systemEffectString == "rollover") {
187-
colorWithEffect = [baseColor colorWithSystemEffect:NSColorSystemEffectRollover];
188-
}
189-
}
190-
return colorWithEffect;
191-
}
192-
#endif // macOS]
193-
194167
int32_t hashFromUIColor(const std::shared_ptr<void> &uiColor)
195168
{
196169
if (uiColor == nullptr) {

packages/react-native/ReactCommon/react/renderer/graphics/platform/ios/react/renderer/graphics/PlatformColorParser.mm

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
#import "PlatformColorParser.h"
99

10-
#import <React/RCTUIKit.h> // [macOS]
1110
#import <react/renderer/core/RawValue.h>
1211
#import <react/renderer/graphics/HostPlatformColor.h>
1312
#import <react/renderer/graphics/RCTPlatformColorUtils.h>
@@ -19,13 +18,6 @@
1918

2019
NS_ASSUME_NONNULL_BEGIN
2120

22-
#if TARGET_OS_OSX // [macOS
23-
// Forward declaration for ColorWithSystemEffect helper
24-
RCTUIColor *_Nullable UIColorFromColorWithSystemEffect(
25-
RCTUIColor *baseColor,
26-
const std::string &systemEffectString);
27-
#endif // macOS]
28-
2921
namespace facebook::react {
3022

3123
inline facebook::react::SharedColor RCTPlatformColorComponentsFromDynamicItems(
@@ -71,27 +63,6 @@ SharedColor parsePlatformColor(const ContextContainer &contextContainer, int32_t
7163
auto dynamicItems = (std::unordered_map<std::string, RawValue>)items.at("dynamic");
7264
return RCTPlatformColorComponentsFromDynamicItems(contextContainer, surfaceId, dynamicItems);
7365
}
74-
#if TARGET_OS_OSX // [macOS
75-
else if (
76-
items.find("colorWithSystemEffect") != items.end() &&
77-
items.at("colorWithSystemEffect").hasType<std::unordered_map<std::string, RawValue>>()) {
78-
auto colorWithSystemEffectItems = (std::unordered_map<std::string, RawValue>)items.at("colorWithSystemEffect");
79-
if (colorWithSystemEffectItems.find("baseColor") != colorWithSystemEffectItems.end() &&
80-
colorWithSystemEffectItems.find("systemEffect") != colorWithSystemEffectItems.end() &&
81-
colorWithSystemEffectItems.at("systemEffect").hasType<std::string>()) {
82-
SharedColor baseColorShared{};
83-
fromRawValue(contextContainer, surfaceId, colorWithSystemEffectItems.at("baseColor"), baseColorShared);
84-
if (baseColorShared) {
85-
RCTUIColor *baseColor = RCTPlatformColorFromColor(*baseColorShared);
86-
std::string systemEffect = (std::string)colorWithSystemEffectItems.at("systemEffect");
87-
RCTUIColor *colorWithEffect = UIColorFromColorWithSystemEffect(baseColor, systemEffect);
88-
if (colorWithEffect != nil) {
89-
return SharedColor(Color(wrapManagedObject(colorWithEffect)));
90-
}
91-
}
92-
}
93-
}
94-
#endif // macOS]
9566
}
9667

9768
return clearColor();
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (c) Microsoft Corporation.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
// [macOS]
9+
10+
#import "HostPlatformColor.h"
11+
12+
#import <Foundation/Foundation.h>
13+
#import <React/RCTUIKit.h>
14+
#import <objc/runtime.h>
15+
#import <react/renderer/graphics/RCTPlatformColorUtils.h>
16+
#import <react/utils/ManagedObjectWrapper.h>
17+
#import <string>
18+
19+
using namespace facebook::react;
20+
21+
NS_ASSUME_NONNULL_BEGIN
22+
23+
namespace facebook::react {
24+
25+
namespace {
26+
27+
RCTUIColor *_Nullable UIColorFromColorWithSystemEffect(
28+
RCTUIColor *baseColor,
29+
const std::string &systemEffectString)
30+
{
31+
if (baseColor == nil) {
32+
return nil;
33+
}
34+
35+
NSColor *colorWithEffect = baseColor;
36+
if (!systemEffectString.empty()) {
37+
if (systemEffectString == "none") {
38+
colorWithEffect = [baseColor colorWithSystemEffect:NSColorSystemEffectNone];
39+
} else if (systemEffectString == "pressed") {
40+
colorWithEffect = [baseColor colorWithSystemEffect:NSColorSystemEffectPressed];
41+
} else if (systemEffectString == "deepPressed") {
42+
colorWithEffect = [baseColor colorWithSystemEffect:NSColorSystemEffectDeepPressed];
43+
} else if (systemEffectString == "disabled") {
44+
colorWithEffect = [baseColor colorWithSystemEffect:NSColorSystemEffectDisabled];
45+
} else if (systemEffectString == "rollover") {
46+
colorWithEffect = [baseColor colorWithSystemEffect:NSColorSystemEffectRollover];
47+
}
48+
}
49+
return colorWithEffect;
50+
}
51+
52+
} // anonymous namespace
53+
54+
} // namespace facebook::react
55+
56+
NS_ASSUME_NONNULL_END
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright (c) Microsoft Corporation.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
// [macOS]
9+
10+
#import "PlatformColorParser.h"
11+
12+
#import <React/RCTUIKit.h>
13+
#import <react/renderer/core/RawValue.h>
14+
#import <react/renderer/graphics/HostPlatformColor.h>
15+
#import <react/renderer/graphics/RCTPlatformColorUtils.h>
16+
#import <react/utils/ManagedObjectWrapper.h>
17+
#import <string>
18+
#import <unordered_map>
19+
20+
using namespace facebook::react;
21+
22+
NS_ASSUME_NONNULL_BEGIN
23+
24+
// Forward declaration for ColorWithSystemEffect helper
25+
RCTUIColor *_Nullable UIColorFromColorWithSystemEffect(
26+
RCTUIColor *baseColor,
27+
const std::string &systemEffectString);
28+
29+
namespace facebook::react {
30+
31+
inline facebook::react::SharedColor RCTPlatformColorComponentsFromDynamicItems(
32+
const facebook::react::ContextContainer &contextContainer,
33+
int32_t surfaceId,
34+
std::unordered_map<std::string, facebook::react::RawValue> &dynamicItems)
35+
{
36+
SharedColor lightSharedColor{};
37+
SharedColor darkSharedColor{};
38+
SharedColor highContrastLightSharedColor{};
39+
SharedColor highContrastDarkSharedColor{};
40+
if (dynamicItems.count("light")) {
41+
fromRawValue(contextContainer, surfaceId, dynamicItems.at("light"), lightSharedColor);
42+
}
43+
if (dynamicItems.count("dark")) {
44+
fromRawValue(contextContainer, surfaceId, dynamicItems.at("dark"), darkSharedColor);
45+
}
46+
if (dynamicItems.count("highContrastLight")) {
47+
fromRawValue(contextContainer, surfaceId, dynamicItems.at("highContrastLight"), highContrastLightSharedColor);
48+
}
49+
if (dynamicItems.count("highContrastDark")) {
50+
fromRawValue(contextContainer, surfaceId, dynamicItems.at("highContrastDark"), highContrastDarkSharedColor);
51+
}
52+
53+
Color color = Color(DynamicColor{
54+
(*lightSharedColor).getColor(),
55+
(*darkSharedColor).getColor(),
56+
(*highContrastLightSharedColor).getColor(),
57+
(*highContrastDarkSharedColor).getColor()});
58+
return SharedColor(color);
59+
}
60+
61+
SharedColor parsePlatformColor(const ContextContainer &contextContainer, int32_t surfaceId, const RawValue &value)
62+
{
63+
if (value.hasType<std::unordered_map<std::string, RawValue>>()) {
64+
auto items = (std::unordered_map<std::string, RawValue>)value;
65+
if (items.find("semantic") != items.end() && items.at("semantic").hasType<std::vector<std::string>>()) {
66+
auto semanticItems = (std::vector<std::string>)items.at("semantic");
67+
return SharedColor(Color::createSemanticColor(semanticItems));
68+
} else if (
69+
items.find("dynamic") != items.end() &&
70+
items.at("dynamic").hasType<std::unordered_map<std::string, RawValue>>()) {
71+
auto dynamicItems = (std::unordered_map<std::string, RawValue>)items.at("dynamic");
72+
return RCTPlatformColorComponentsFromDynamicItems(contextContainer, surfaceId, dynamicItems);
73+
} else if (
74+
items.find("colorWithSystemEffect") != items.end() &&
75+
items.at("colorWithSystemEffect").hasType<std::unordered_map<std::string, RawValue>>()) {
76+
auto colorWithSystemEffectItems = (std::unordered_map<std::string, RawValue>)items.at("colorWithSystemEffect");
77+
if (colorWithSystemEffectItems.find("baseColor") != colorWithSystemEffectItems.end() &&
78+
colorWithSystemEffectItems.find("systemEffect") != colorWithSystemEffectItems.end() &&
79+
colorWithSystemEffectItems.at("systemEffect").hasType<std::string>()) {
80+
SharedColor baseColorShared{};
81+
fromRawValue(contextContainer, surfaceId, colorWithSystemEffectItems.at("baseColor"), baseColorShared);
82+
if (baseColorShared) {
83+
RCTUIColor *baseColor = RCTPlatformColorFromColor(*baseColorShared);
84+
std::string systemEffect = (std::string)colorWithSystemEffectItems.at("systemEffect");
85+
RCTUIColor *colorWithEffect = UIColorFromColorWithSystemEffect(baseColor, systemEffect);
86+
if (colorWithEffect != nil) {
87+
return SharedColor(Color(wrapManagedObject(colorWithEffect)));
88+
}
89+
}
90+
}
91+
}
92+
}
93+
94+
return clearColor();
95+
}
96+
97+
} // namespace facebook::react
98+
99+
NS_ASSUME_NONNULL_END

0 commit comments

Comments
 (0)