Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DynamicColorMacOS fixes #1028

Merged
merged 2 commits into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion Libraries/StyleSheet/PlatformColorValueTypes.macos.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export opaque type NativeColorValue = {
dynamic?: {
light: ?(ColorValue | ProcessedColorValue),
dark: ?(ColorValue | ProcessedColorValue),
highContrastLight?: ?(ColorValue | ProcessedColorValue),
highContrastDark?: ?(ColorValue | ProcessedColorValue),
},
colorWithSystemEffect?: {
baseColor: ?(ColorValue | ProcessedColorValue),
Expand Down Expand Up @@ -51,12 +53,21 @@ export const ColorWithSystemEffectMacOSPrivate = (
export type DynamicColorMacOSTuplePrivate = {
light: ColorValue,
dark: ColorValue,
highContrastLight?: ColorValue,
highContrastDark?: ColorValue,
};

export const DynamicColorMacOSPrivate = (
tuple: DynamicColorMacOSTuplePrivate,
): ColorValue => {
return {dynamic: {light: tuple.light, dark: tuple.dark}};
return {
dynamic: {
light: tuple.light,
dark: tuple.dark,
highContrastLight: tuple.highContrastLight,
highContrastDark: tuple.highContrastDark,
},
};
};

export const normalizeColorObject = (
Expand All @@ -74,6 +85,8 @@ export const normalizeColorObject = (
dynamic: {
light: normalizeColor(dynamic.light),
dark: normalizeColor(dynamic.dark),
highContrastLight: normalizeColor(dynamic.highContrastLight),
highContrastDark: normalizeColor(dynamic.highContrastDark),
},
};
return dynamicColor;
Expand Down Expand Up @@ -104,6 +117,8 @@ export const processColorObject = (
dynamic: {
light: processColor(dynamic.light),
dark: processColor(dynamic.dark),
highContrastLight: processColor(dynamic.highContrastLight),
highContrastDark: processColor(dynamic.highContrastDark),
},
};
return dynamicColor;
Expand Down
2 changes: 2 additions & 0 deletions Libraries/StyleSheet/PlatformColorValueTypesMacOS.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import type {ColorValue} from './StyleSheet';
export type DynamicColorMacOSTuple = {
light: ColorValue,
dark: ColorValue,
highContrastLight?: ColorValue,
highContrastDark?: ColorValue,
};

export const DynamicColorMacOS = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,19 @@ import {
export type DynamicColorMacOSTuple = {
light: ColorValue,
dark: ColorValue,
highContrastLight?: ColorValue,
highContrastDark?: ColorValue,
};

export const DynamicColorMacOS = (
tuple: DynamicColorMacOSTuple,
): ColorValue => {
return DynamicColorMacOSPrivate({light: tuple.light, dark: tuple.dark});
return DynamicColorMacOSPrivate({
light: tuple.light,
dark: tuple.dark,
highContrastLight: tuple.highContrastLight,
highContrastDark: tuple.highContrastDark,
});
};

export type SystemEffectMacOS =
Expand Down
27 changes: 22 additions & 5 deletions React/Base/RCTConvert.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@
#import "RCTParserUtils.h"
#import "RCTUtils.h"

#if TARGET_OS_OSX // [TODO(macOS GH#774)
#import "RCTDynamicColor.h"
#endif // ]TODO(macOS GH#774)

@implementation RCTConvert

RCT_CONVERTER(id, id, self)
Expand Down Expand Up @@ -1043,7 +1039,28 @@ + (RCTUIColor *)UIColor:(id)json // TODO(macOS GH#750)
RCTUIColor *highContrastDarkColor = [RCTConvert UIColor:highContrastDark]; // TODO(macOS GH#750)
if (lightColor != nil && darkColor != nil) {
#if TARGET_OS_OSX
RCTDynamicColor *color = [[RCTDynamicColor alloc] initWithAquaColor:lightColor darkAquaColor:darkColor];
NSColor *color = [NSColor colorWithName:nil dynamicProvider:^NSColor * _Nonnull(NSAppearance * _Nonnull appearance) {
NSMutableArray<NSAppearanceName> *appearances = [NSMutableArray arrayWithArray:@[NSAppearanceNameAqua,NSAppearanceNameDarkAqua]];
if (highContrastLightColor != nil) {
[appearances addObject:NSAppearanceNameAccessibilityHighContrastAqua];
}
if (highContrastDarkColor != nil) {
[appearances addObject:NSAppearanceNameAccessibilityHighContrastDarkAqua];
}
NSAppearanceName bestMatchingAppearance = [appearance bestMatchFromAppearancesWithNames:appearances];
if (bestMatchingAppearance == NSAppearanceNameAqua) {
return lightColor;
} else if (bestMatchingAppearance == NSAppearanceNameDarkAqua) {
return darkColor;
} else if (bestMatchingAppearance == NSAppearanceNameAccessibilityHighContrastAqua) {
return highContrastLightColor;
} else if (bestMatchingAppearance == NSAppearanceNameAccessibilityHighContrastDarkAqua) {
return highContrastDarkColor;
} else {
RCTLogConvertError(json, @"an NSColorColor. Could not resolve current appearance. Defaulting to light.");
return lightColor;
}
}];
return color;
#else
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
Expand Down
28 changes: 0 additions & 28 deletions React/Base/macOS/RCTDynamicColor.h

This file was deleted.

217 changes: 0 additions & 217 deletions React/Base/macOS/RCTDynamicColor.m

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,27 @@ function DynamicColorsExample() {
}}
/>
</View>
<View style={styles.row}>
<Text style={styles.labelCell}>
DynamicColorMacOS({'{\n'}
{' '}light: 'red',{'\n'}
{' '}dark: 'blue',{'\n'}
{' '}highContrastLight: 'green',{'\n'}
{' '}highContrastDark: 'orange',{'\n'}
{'}'})
</Text>
<View
style={{
...styles.colorCell,
backgroundColor: DynamicColorMacOS({
light: 'red',
dark: 'blue',
highContrastLight: 'green',
highContrastDark: 'orange',
}),
}}
/>
</View>
</View>
) : // ]TODO(macOS GH#774)
Platform.OS === 'ios' ? (
Expand Down