Skip to content

Commit 2e8e307

Browse files
CopilotSaadnajmi
andcommitted
Add ColorWithSystemEffect support to Fabric renderer
Co-authored-by: Saadnajmi <6722175+Saadnajmi@users.noreply.github.com>
1 parent 4fc0688 commit 2e8e307

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,33 @@ 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+
167194
int32_t hashFromUIColor(const std::shared_ptr<void> &uiColor)
168195
{
169196
if (uiColor == nullptr) {

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

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

88
#import "PlatformColorParser.h"
99

10+
#import <React/RCTUIKit.h> // [macOS]
1011
#import <react/renderer/core/RawValue.h>
1112
#import <react/renderer/graphics/HostPlatformColor.h>
1213
#import <react/renderer/graphics/RCTPlatformColorUtils.h>
@@ -18,6 +19,13 @@
1819

1920
NS_ASSUME_NONNULL_BEGIN
2021

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+
2129
namespace facebook::react {
2230

2331
inline facebook::react::SharedColor RCTPlatformColorComponentsFromDynamicItems(
@@ -63,6 +71,27 @@ SharedColor parsePlatformColor(const ContextContainer &contextContainer, int32_t
6371
auto dynamicItems = (std::unordered_map<std::string, RawValue>)items.at("dynamic");
6472
return RCTPlatformColorComponentsFromDynamicItems(contextContainer, surfaceId, dynamicItems);
6573
}
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]
6695
}
6796

6897
return clearColor();

0 commit comments

Comments
 (0)