Skip to content

Commit 0f55676

Browse files
committed
manual changes
1 parent f1400a0 commit 0f55676

5 files changed

Lines changed: 78 additions & 47 deletions

File tree

packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1177,7 +1177,7 @@ - (void)invalidateLayer
11771177
#if !TARGET_OS_OSX // [macOS]
11781178
RCTPlatformColor *backgroundColor = [_backgroundColor resolvedColorWithTraitCollection:self.traitCollection];
11791179
#else // [macOS
1180-
RCTPlatformColor *backgroundColor = _backgroundColor;
1180+
RCTPlatformColor *backgroundColor = [_backgroundColor resolvedColorWithAppearance:self.effectiveAppearance];
11811181
#endif // macOS]
11821182
// The reason we sometimes do not set self.layer's backgroundColor is because
11831183
// we want to support non-uniform border radii, which apple does not natively
@@ -1208,6 +1208,9 @@ - (void)invalidateLayer
12081208

12091209
layer.borderWidth = (CGFloat)borderMetrics.borderWidths.left;
12101210
RCTPlatformColor *borderColor = RCTUIColorFromSharedColor(borderMetrics.borderColors.left); // [macOS]
1211+
#if TARGET_OS_OSX // [macOS
1212+
borderColor = [borderColor resolvedColorWithAppearance:self.effectiveAppearance];
1213+
#endif // macOS]
12111214
layer.borderColor = borderColor.CGColor;
12121215
layer.cornerRadius = (CGFloat)borderMetrics.borderRadii.topLeft.horizontal;
12131216

@@ -1229,6 +1232,12 @@ - (void)invalidateLayer
12291232
layer.cornerRadius = 0;
12301233

12311234
RCTBorderColors borderColors = RCTCreateRCTBorderColorsFromBorderColors(borderMetrics.borderColors);
1235+
#if TARGET_OS_OSX // [macOS
1236+
borderColors.top = [borderColors.top resolvedColorWithAppearance:self.effectiveAppearance];
1237+
borderColors.left = [borderColors.left resolvedColorWithAppearance:self.effectiveAppearance];
1238+
borderColors.bottom = [borderColors.bottom resolvedColorWithAppearance:self.effectiveAppearance];
1239+
borderColors.right = [borderColors.right resolvedColorWithAppearance:self.effectiveAppearance];
1240+
#endif // macOS]
12321241

12331242
RCTAddContourEffectToLayer(
12341243
_borderLayer,
@@ -1255,10 +1264,16 @@ - (void)invalidateLayer
12551264

12561265
if (borderMetrics.borderRadii.isUniform() && borderMetrics.borderRadii.topLeft.horizontal == 0) {
12571266
RCTPlatformColor *outlineColor = RCTUIColorFromSharedColor(_props->outlineColor); // [macOS]
1267+
#if TARGET_OS_OSX // [macOS
1268+
outlineColor = [outlineColor resolvedColorWithAppearance:self.effectiveAppearance];
1269+
#endif // macOS]
12581270
_outlineLayer.borderWidth = _props->outlineWidth;
12591271
_outlineLayer.borderColor = outlineColor.CGColor;
12601272
} else {
12611273
RCTPlatformColor *outlineColor = RCTUIColorFromSharedColor(_props->outlineColor); // [macOS]
1274+
#if TARGET_OS_OSX // [macOS
1275+
outlineColor = [outlineColor resolvedColorWithAppearance:self.effectiveAppearance];
1276+
#endif // macOS]
12621277

12631278
RCTAddContourEffectToLayer(
12641279
_outlineLayer,

packages/react-native/React/RCTUIKit/RCTUIKitCompat.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ NS_ASSUME_NONNULL_BEGIN
2727
#else
2828
@compatibility_alias RCTPlatformColor NSColor;
2929
@compatibility_alias RCTUIColor NSColor;
30+
31+
@interface NSColor (RCTAppearanceResolving)
32+
/// Resolve a dynamic/semantic NSColor for a specific appearance, analogous to
33+
/// UIColor's -resolvedColorWithTraitCollection: on iOS.
34+
- (NSColor *)resolvedColorWithAppearance:(NSAppearance *)appearance;
35+
@end
36+
3037
#endif
3138

3239
// MARK: - Event types
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 "RCTUIKitCompat.h"
11+
12+
#if TARGET_OS_OSX
13+
14+
@implementation NSColor (RCTAppearanceResolving)
15+
16+
- (NSColor *)resolvedColorWithAppearance:(NSAppearance *)appearance
17+
{
18+
__block NSColor *resolved = self;
19+
[appearance performAsCurrentDrawingAppearance:^{
20+
CGColorRef cgColor = self.CGColor;
21+
if (cgColor) {
22+
NSColor *fromCG = [NSColor colorWithCGColor:cgColor];
23+
if (fromCG) {
24+
resolved = fromCG;
25+
}
26+
}
27+
}];
28+
return resolved;
29+
}
30+
31+
@end
32+
33+
#endif // TARGET_OS_OSX

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

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
namespace 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

182187
int32_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]

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -327,18 +327,7 @@
327327
static inline facebook::react::ColorComponents _ColorComponentsFromUIColor(RCTPlatformColor *color) // [macOS]
328328
{
329329
CGFloat rgba[4];
330-
#if !TARGET_OS_OSX // [macOS]
331330
[color getRed:&rgba[0] green:&rgba[1] blue:&rgba[2] alpha:&rgba[3]];
332-
#else // [macOS
333-
// Resolve dynamic/semantic colors against the current effective appearance
334-
// so that dark mode colors are correctly extracted.
335-
NSAppearance *previousAppearance = NSAppearance.currentAppearance;
336-
NSAppearance.currentAppearance = [NSApp effectiveAppearance];
337-
NSColor *resolvedColor = [color colorUsingColorSpace:[NSColorSpace sRGBColorSpace]];
338-
NSAppearance.currentAppearance = previousAppearance;
339-
NSColor *finalColor = resolvedColor ?: [color colorUsingColorSpace:[NSColorSpace genericRGBColorSpace]];
340-
[finalColor getRed:&rgba[0] green:&rgba[1] blue:&rgba[2] alpha:&rgba[3]];
341-
#endif // macOS]
342331
return {(float)rgba[0], (float)rgba[1], (float)rgba[2], (float)rgba[3]};
343332
}
344333

0 commit comments

Comments
 (0)