Skip to content

Commit a6cbf7c

Browse files
committed
Make back into iOS idfefs
1 parent fadacf9 commit a6cbf7c

File tree

9 files changed

+288
-467
lines changed

9 files changed

+288
-467
lines changed

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

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

2626
Pod::Spec.new do |s|
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]
27+
source_files = ["*.{m,mm,cpp,h}", "platform/ios/**/*.{m,mm,cpp,h}"]
3128
header_search_paths = [
3229
"\"$(PODS_ROOT)/boost\"",
3330
"\"$(PODS_TARGET_SRCROOT)/../../../\"",
@@ -46,11 +43,7 @@ Pod::Spec.new do |s|
4643
s.platforms = min_supported_versions
4744
s.source = source
4845
s.compiler_flags = folly_compiler_flags + ' ' + boost_compiler_flags
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]
46+
s.source = source
5447
s.header_dir = "react/renderer/graphics"
5548
# [macOS Restrict UIKit to iOS and visionOS
5649
s.ios.framework = "UIKit"

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,19 @@ struct DynamicColor {
2020
int32_t highContrastDarkColor = 0;
2121
};
2222

23+
#if TARGET_OS_OSX // [macOS
24+
struct ColorWithSystemEffect {
25+
int32_t color = 0;
26+
std::string effect;
27+
};
28+
#endif // macOS]
29+
2330
struct Color {
2431
Color(int32_t color);
2532
Color(const DynamicColor& dynamicColor);
33+
#if TARGET_OS_OSX // [macOS
34+
Color(const ColorWithSystemEffect& colorWithSystemEffect);
35+
#endif // macOS]
2636
Color(const ColorComponents& components);
2737
Color() : uiColor_(nullptr){};
2838
int32_t getColor() const;

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,33 @@
2020

2121
namespace facebook::react {
2222

23+
#if TARGET_OS_OSX // [macOS
24+
RCTUIColor *_Nullable UIColorFromColorWithSystemEffect(
25+
RCTUIColor *baseColor,
26+
const std::string &systemEffectString)
27+
{
28+
if (baseColor == nil) {
29+
return nil;
30+
}
31+
32+
NSColor *colorWithEffect = baseColor;
33+
if (!systemEffectString.empty()) {
34+
if (systemEffectString == "none") {
35+
colorWithEffect = [baseColor colorWithSystemEffect:NSColorSystemEffectNone];
36+
} else if (systemEffectString == "pressed") {
37+
colorWithEffect = [baseColor colorWithSystemEffect:NSColorSystemEffectPressed];
38+
} else if (systemEffectString == "deepPressed") {
39+
colorWithEffect = [baseColor colorWithSystemEffect:NSColorSystemEffectDeepPressed];
40+
} else if (systemEffectString == "disabled") {
41+
colorWithEffect = [baseColor colorWithSystemEffect:NSColorSystemEffectDisabled];
42+
} else if (systemEffectString == "rollover") {
43+
colorWithEffect = [baseColor colorWithSystemEffect:NSColorSystemEffectRollover];
44+
}
45+
}
46+
return colorWithEffect;
47+
}
48+
#endif // macOS]
49+
2350
namespace {
2451

2552
bool UIColorIsP3ColorSpace(const std::shared_ptr<void> &uiColor)
@@ -224,6 +251,21 @@ int32_t hashFromUIColor(const std::shared_ptr<void> &uiColor)
224251
0);
225252
}
226253

254+
#if TARGET_OS_OSX // [macOS
255+
Color::Color(const ColorWithSystemEffect &colorWithSystemEffect)
256+
{
257+
RCTUIColor *baseColor = UIColorFromInt32(colorWithSystemEffect.color);
258+
RCTUIColor *colorWithEffect =
259+
UIColorFromColorWithSystemEffect(baseColor, colorWithSystemEffect.effect);
260+
if (colorWithEffect != nil) {
261+
uiColor_ = wrapManagedObject(colorWithEffect);
262+
}
263+
uiColorHashValue_ = facebook::react::hash_combine(
264+
colorWithSystemEffect.color,
265+
std::hash<std::string>{}(colorWithSystemEffect.effect));
266+
}
267+
#endif // macOS
268+
227269
Color::Color(const ColorComponents &components)
228270
{
229271
uiColor_ = wrapManagedObject(UIColorFromComponentsColor(components));

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,24 @@ SharedColor parsePlatformColor(const ContextContainer &contextContainer, int32_t
6262
items.at("dynamic").hasType<std::unordered_map<std::string, RawValue>>()) {
6363
auto dynamicItems = (std::unordered_map<std::string, RawValue>)items.at("dynamic");
6464
return RCTPlatformColorComponentsFromDynamicItems(contextContainer, surfaceId, dynamicItems);
65+
#if TARGET_OS_OSX // [macOS
66+
} else if (
67+
items.find("colorWithSystemEffect") != items.end() &&
68+
items.at("colorWithSystemEffect").hasType<std::unordered_map<std::string, RawValue>>()) {
69+
auto colorWithSystemEffectItems =
70+
(std::unordered_map<std::string, RawValue>)items.at("colorWithSystemEffect");
71+
if (colorWithSystemEffectItems.find("baseColor") != colorWithSystemEffectItems.end() &&
72+
colorWithSystemEffectItems.find("systemEffect") != colorWithSystemEffectItems.end() &&
73+
colorWithSystemEffectItems.at("systemEffect").hasType<std::string>()) {
74+
SharedColor baseColorShared{};
75+
fromRawValue(contextContainer, surfaceId, colorWithSystemEffectItems.at("baseColor"), baseColorShared);
76+
if (baseColorShared) {
77+
std::string systemEffect = (std::string)colorWithSystemEffectItems.at("systemEffect");
78+
auto baseColor = (*baseColorShared).getColor();
79+
return SharedColor(Color(ColorWithSystemEffect{baseColor, systemEffect}));
80+
}
81+
}
82+
#endif // macOS]
6583
}
6684
}
6785

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

Lines changed: 151 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,33 @@
88
#import "RCTPlatformColorUtils.h"
99

1010
#import <Foundation/Foundation.h>
11+
#import <TargetConditionals.h>
1112
#import <React/RCTUIKit.h> // [macOS]
13+
#if TARGET_OS_OSX // [macOS
14+
#import <AppKit/AppKit.h>
15+
#endif // macOS]
1216
#import <react/renderer/graphics/HostPlatformColor.h>
1317
#import <react/utils/ManagedObjectWrapper.h>
1418

1519
#include <string>
20+
#include <vector>
1621

1722
NS_ASSUME_NONNULL_BEGIN
1823

1924
static NSString *const kColorSuffix = @"Color";
2025
static NSString *const kFallbackARGBKey = @"fallback-argb";
26+
#if TARGET_OS_OSX // [macOS
27+
static NSString *const kFallbackKey = @"fallback";
28+
static NSString *const kSelectorKey = @"selector";
29+
static NSString *const kIndexKey = @"index";
30+
#endif // macOS]
2131

2232
static NSDictionary<NSString *, NSDictionary *> *_PlatformColorSelectorsDict()
2333
{
2434
static NSDictionary<NSString *, NSDictionary *> *dict;
2535
static dispatch_once_t once_token;
2636
dispatch_once(&once_token, ^(void) {
37+
#if !TARGET_OS_OSX // [macOS]
2738
dict = @{
2839
// https://developer.apple.com/documentation/uikit/uicolor/ui_element_colors
2940
// Label Colors
@@ -130,6 +141,105 @@
130141
kFallbackARGBKey : @(0x00000000) // iOS 13.0
131142
},
132143
};
144+
#else // [macOS
145+
NSMutableDictionary<NSString *, NSDictionary *> *map = [@{
146+
// https://developer.apple.com/documentation/appkit/nscolor/ui_element_colors
147+
// Label Colors
148+
@"labelColor": @{},
149+
@"secondaryLabelColor": @{},
150+
@"tertiaryLabelColor": @{},
151+
@"quaternaryLabelColor": @{},
152+
// Text Colors
153+
@"textColor": @{},
154+
@"placeholderTextColor": @{},
155+
@"selectedTextColor": @{},
156+
@"textBackgroundColor": @{},
157+
@"selectedTextBackgroundColor": @{},
158+
@"keyboardFocusIndicatorColor": @{},
159+
@"unemphasizedSelectedTextColor": @{
160+
kFallbackKey: @"selectedTextColor"
161+
},
162+
@"unemphasizedSelectedTextBackgroundColor": @{
163+
kFallbackKey: @"textBackgroundColor"
164+
},
165+
// Content Colors
166+
@"linkColor": @{},
167+
@"separatorColor": @{
168+
kFallbackKey: @"gridColor"
169+
},
170+
@"selectedContentBackgroundColor": @{
171+
kFallbackKey: @"alternateSelectedControlColor"
172+
},
173+
@"unemphasizedSelectedContentBackgroundColor": @{
174+
kFallbackKey: @"secondarySelectedControlColor"
175+
},
176+
// Menu Colors
177+
@"selectedMenuItemTextColor": @{},
178+
// Table Colors
179+
@"gridColor": @{},
180+
@"headerTextColor": @{},
181+
@"alternatingEvenContentBackgroundColor": @{
182+
kSelectorKey: @"alternatingContentBackgroundColors",
183+
kIndexKey: @0,
184+
kFallbackKey: @"controlAlternatingRowBackgroundColors"
185+
},
186+
@"alternatingOddContentBackgroundColor": @{
187+
kSelectorKey: @"alternatingContentBackgroundColors",
188+
kIndexKey: @1,
189+
kFallbackKey: @"controlAlternatingRowBackgroundColors"
190+
},
191+
// Control Colors
192+
@"controlAccentColor": @{
193+
kFallbackKey: @"controlColor"
194+
},
195+
@"controlColor": @{},
196+
@"controlBackgroundColor": @{},
197+
@"controlTextColor": @{},
198+
@"disabledControlTextColor": @{},
199+
@"selectedControlColor": @{},
200+
@"selectedControlTextColor": @{},
201+
@"alternateSelectedControlTextColor": @{},
202+
@"scrubberTexturedBackgroundColor": @{},
203+
// Window Colors
204+
@"windowBackgroundColor": @{},
205+
@"windowFrameTextColor": @{},
206+
@"underPageBackgroundColor": @{},
207+
// Highlights and Shadows
208+
@"findHighlightColor": @{
209+
kFallbackKey: @"highlightColor"
210+
},
211+
@"highlightColor": @{},
212+
@"shadowColor": @{},
213+
// https://developer.apple.com/documentation/appkit/nscolor/standard_colors
214+
// Standard Colors
215+
@"systemBlueColor": @{},
216+
@"systemBrownColor": @{},
217+
@"systemGrayColor": @{},
218+
@"systemGreenColor": @{},
219+
@"systemOrangeColor": @{},
220+
@"systemPinkColor": @{},
221+
@"systemPurpleColor": @{},
222+
@"systemRedColor": @{},
223+
@"systemYellowColor": @{},
224+
// Transparent Color
225+
@"clearColor" : @{},
226+
} mutableCopy];
227+
228+
NSMutableDictionary<NSString *, NSDictionary *> *aliases = [NSMutableDictionary new];
229+
for (NSString *objcSelector in map) {
230+
NSMutableDictionary *entry = [map[objcSelector] mutableCopy];
231+
if ([entry objectForKey:kSelectorKey] == nil) {
232+
entry[kSelectorKey] = objcSelector;
233+
}
234+
if ([objcSelector hasSuffix:kColorSuffix]) {
235+
NSString *swiftSelector = [objcSelector substringToIndex:[objcSelector length] - [kColorSuffix length]];
236+
aliases[swiftSelector] = entry;
237+
}
238+
}
239+
[map addEntriesFromDictionary:aliases];
240+
241+
dict = [map copy];
242+
#endif // macOS]
133243
});
134244
return dict;
135245
}
@@ -154,21 +264,59 @@
154264
NSDictionary<NSString *, NSDictionary *> *platformColorSelectorsDict = _PlatformColorSelectorsDict();
155265
NSDictionary<NSString *, id> *colorInfo = platformColorSelectorsDict[platformColorString];
156266
if (colorInfo) {
267+
#if !TARGET_OS_OSX // [macOS]
157268
SEL objcColorSelector = NSSelectorFromString([platformColorString stringByAppendingString:kColorSuffix]);
158-
if (![RCTUIColor respondsToSelector:objcColorSelector]) { // [macOS]
269+
if (![RCTUIColor respondsToSelector:objcColorSelector]) {
159270
NSNumber *fallbackRGB = colorInfo[kFallbackARGBKey];
160271
if (fallbackRGB) {
161272
return _UIColorFromHexValue(fallbackRGB);
162273
}
163274
} else {
164-
Class uiColorClass = [RCTUIColor class]; // [macOS]
275+
Class uiColorClass = [RCTUIColor class];
165276
IMP imp = [uiColorClass methodForSelector:objcColorSelector];
166277
id (*getUIColor)(id, SEL) = ((id(*)(id, SEL))imp);
167278
id colorObject = getUIColor(uiColorClass, objcColorSelector);
168-
if ([colorObject isKindOfClass:[RCTUIColor class]]) { // [macOS]
279+
if ([colorObject isKindOfClass:[RCTUIColor class]]) {
280+
return colorObject;
281+
}
282+
}
283+
#else // [macOS
284+
NSString *selectorName = colorInfo[kSelectorKey];
285+
if (selectorName == nil) {
286+
selectorName = [platformColorString stringByAppendingString:kColorSuffix];
287+
}
288+
289+
SEL objcColorSelector = NSSelectorFromString(selectorName);
290+
if (![RCTUIColor respondsToSelector:objcColorSelector]) {
291+
NSNumber *fallbackRGB = colorInfo[kFallbackARGBKey];
292+
if (fallbackRGB) {
293+
return _UIColorFromHexValue(fallbackRGB);
294+
}
295+
NSString *fallbackColorName = colorInfo[kFallbackKey];
296+
if (fallbackColorName) {
297+
return _UIColorFromSemanticString(fallbackColorName);
298+
}
299+
} else {
300+
Class colorClass = [RCTUIColor class];
301+
IMP imp = [colorClass methodForSelector:objcColorSelector];
302+
id (*getColor)(id, SEL) = ((id(*)(id, SEL))imp);
303+
id colorObject = getColor(colorClass, objcColorSelector);
304+
if ([colorObject isKindOfClass:[NSArray class]]) {
305+
NSNumber *index = colorInfo[kIndexKey];
306+
if (index != nil) {
307+
NSArray *colors = colorObject;
308+
NSUInteger idx = [index unsignedIntegerValue];
309+
if (idx < colors.count) {
310+
colorObject = colors[idx];
311+
}
312+
}
313+
}
314+
315+
if ([colorObject isKindOfClass:[RCTUIColor class]]) {
169316
return colorObject;
170317
}
171318
}
319+
#endif
172320
}
173321
return nil;
174322
}

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

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)