2121namespace facebook ::react {
2222
2323#if TARGET_OS_OSX // [macOS
24- RCTUIColor *_Nullable UIColorFromColorWithSystemEffect (
24+ RCTPlatformColor *_Nullable UIColorFromColorWithSystemEffect (
2525 RCTUIColor *baseColor,
2626 const std::string &systemEffectString)
2727{
@@ -131,7 +131,6 @@ bool UIColorIsP3ColorSpace(const std::shared_ptr<void> &uiColor)
131131 } else {
132132 return nil ;
133133 }
134-
135134 return nil ;
136135}
137136
@@ -150,16 +149,7 @@ int32_t ColorFromUIColor(RCTPlatformColor *color) // [macOS]
150149#if !TARGET_OS_OSX // [macOS]
151150 [color getRed: &rgba[0 ] green: &rgba[1 ] blue: &rgba[2 ] alpha: &rgba[3 ]];
152151#else // [macOS
153- // Resolve dynamic/semantic colors against the current effective appearance
154- // so that dark mode colors are correctly extracted.
155- [[NSApp effectiveAppearance ] performAsCurrentDrawingAppearance: ^{
156- NSColor *resolvedColor = [color colorUsingColorSpace: [NSColorSpace sRGBColorSpace ]];
157- if (resolvedColor) {
158- [resolvedColor getRed: &rgba[0 ] green: &rgba[1 ] blue: &rgba[2 ] alpha: &rgba[3 ]];
159- } else {
160- [color getRed: &rgba[0 ] green: &rgba[1 ] blue: &rgba[2 ] alpha: &rgba[3 ]];
161- }
162- }];
152+ [[color colorUsingColorSpace: [NSColorSpace genericRGBColorSpace ]] getRed: &rgba[0 ] green: &rgba[1 ] blue: &rgba[2 ] alpha: &rgba[3 ]];
163153#endif // macOS]
164154 return ColorFromColorComponents ({(float )rgba[0 ], (float )rgba[1 ], (float )rgba[2 ], (float )rgba[3 ]});
165155}
@@ -177,15 +167,29 @@ int32_t ColorFromUIColorForSpecificTraitCollection(
177167
178168 return 0 ;
179169}
170+ #else // [macOS
171+ int32_t ColorFromUIColorForSpecificAppearance (
172+ const std::shared_ptr<void > &uiColor,
173+ NSAppearance *appearance)
174+ {
175+ RCTPlatformColor *color = (RCTPlatformColor *)unwrapManagedObject (uiColor);
176+ if (color) {
177+ __block int32_t resolvedColorInt = 0 ;
178+ [appearance performAsCurrentDrawingAppearance: ^{
179+ resolvedColorInt = ColorFromUIColor (color);
180+ }];
181+ return resolvedColorInt;
182+ }
183+ return 0 ;
184+ }
180185#endif // [macOS]
181186
182187int32_t ColorFromUIColor (const std::shared_ptr<void > &uiColor)
183188{
184189#if !TARGET_OS_OSX // [macOS]
185190 return ColorFromUIColorForSpecificTraitCollection (uiColor, [UITraitCollection currentTraitCollection ]);
186191#else // [macOS
187- RCTPlatformColor *color = (RCTPlatformColor *)unwrapManagedObject (uiColor);
188- return ColorFromUIColor (color);
192+ return ColorFromUIColorForSpecificAppearance (uiColor, [NSApp effectiveAppearance ]);
189193#endif // macOS]
190194}
191195
@@ -243,27 +247,10 @@ int32_t ColorFromUIColor(const std::shared_ptr<void> &uiColor)
243247#else // [macOS
244248 // Hash both light and dark appearance colors to properly distinguish
245249 // dynamic colors that change with appearance.
246- RCTPlatformColor *color = (RCTPlatformColor *)unwrapManagedObject (uiColor);
247- __block int32_t darkColor = 0 ;
248- __block int32_t lightColor = 0 ;
249-
250- [[NSAppearance appearanceNamed: NSAppearanceNameDarkAqua ] performAsCurrentDrawingAppearance: ^{
251- NSColor *resolved = [color colorUsingColorSpace: [NSColorSpace sRGBColorSpace ]];
252- if (resolved) {
253- CGFloat rgba[4 ];
254- [resolved getRed: &rgba[0 ] green: &rgba[1 ] blue: &rgba[2 ] alpha: &rgba[3 ]];
255- darkColor = ColorFromColorComponents ({(float )rgba[0 ], (float )rgba[1 ], (float )rgba[2 ], (float )rgba[3 ]});
256- }
257- }];
258-
259- [[NSAppearance appearanceNamed: NSAppearanceNameAqua ] performAsCurrentDrawingAppearance: ^{
260- NSColor *resolved = [color colorUsingColorSpace: [NSColorSpace sRGBColorSpace ]];
261- if (resolved) {
262- CGFloat rgba[4 ];
263- [resolved getRed: &rgba[0 ] green: &rgba[1 ] blue: &rgba[2 ] alpha: &rgba[3 ]];
264- lightColor = ColorFromColorComponents ({(float )rgba[0 ], (float )rgba[1 ], (float )rgba[2 ], (float )rgba[3 ]});
265- }
266- }];
250+ auto darkColor = ColorFromUIColorForSpecificAppearance (
251+ uiColor, [NSAppearance appearanceNamed: NSAppearanceNameDarkAqua ]);
252+ auto lightColor = ColorFromUIColorForSpecificAppearance (
253+ uiColor, [NSAppearance appearanceNamed: NSAppearanceNameAqua ]);
267254
268255 return facebook::react::hash_combine (darkColor, lightColor);
269256#endif // macOS]
0 commit comments