|
8 | 8 | #import "RCTPlatformColorUtils.h" |
9 | 9 |
|
10 | 10 | #import <Foundation/Foundation.h> |
| 11 | +#import <TargetConditionals.h> |
11 | 12 | #import <React/RCTUIKit.h> // [macOS] |
| 13 | +#if TARGET_OS_OSX // [macOS |
| 14 | +#import <AppKit/AppKit.h> |
| 15 | +#endif // macOS] |
12 | 16 | #import <react/renderer/graphics/HostPlatformColor.h> |
13 | 17 | #import <react/utils/ManagedObjectWrapper.h> |
14 | 18 |
|
15 | 19 | #include <string> |
| 20 | +#include <vector> |
16 | 21 |
|
17 | 22 | NS_ASSUME_NONNULL_BEGIN |
18 | 23 |
|
19 | 24 | static NSString *const kColorSuffix = @"Color"; |
20 | 25 | 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] |
21 | 31 |
|
22 | 32 | static NSDictionary<NSString *, NSDictionary *> *_PlatformColorSelectorsDict() |
23 | 33 | { |
24 | 34 | static NSDictionary<NSString *, NSDictionary *> *dict; |
25 | 35 | static dispatch_once_t once_token; |
26 | 36 | dispatch_once(&once_token, ^(void) { |
| 37 | +#if !TARGET_OS_OSX // [macOS] |
27 | 38 | dict = @{ |
28 | 39 | // https://developer.apple.com/documentation/uikit/uicolor/ui_element_colors |
29 | 40 | // Label Colors |
|
130 | 141 | kFallbackARGBKey : @(0x00000000) // iOS 13.0 |
131 | 142 | }, |
132 | 143 | }; |
| 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] |
133 | 243 | }); |
134 | 244 | return dict; |
135 | 245 | } |
|
154 | 264 | NSDictionary<NSString *, NSDictionary *> *platformColorSelectorsDict = _PlatformColorSelectorsDict(); |
155 | 265 | NSDictionary<NSString *, id> *colorInfo = platformColorSelectorsDict[platformColorString]; |
156 | 266 | if (colorInfo) { |
| 267 | +#if !TARGET_OS_OSX // [macOS] |
157 | 268 | SEL objcColorSelector = NSSelectorFromString([platformColorString stringByAppendingString:kColorSuffix]); |
158 | | - if (![RCTUIColor respondsToSelector:objcColorSelector]) { // [macOS] |
| 269 | + if (![RCTUIColor respondsToSelector:objcColorSelector]) { |
159 | 270 | NSNumber *fallbackRGB = colorInfo[kFallbackARGBKey]; |
160 | 271 | if (fallbackRGB) { |
161 | 272 | return _UIColorFromHexValue(fallbackRGB); |
162 | 273 | } |
163 | 274 | } else { |
164 | | - Class uiColorClass = [RCTUIColor class]; // [macOS] |
| 275 | + Class uiColorClass = [RCTUIColor class]; |
165 | 276 | IMP imp = [uiColorClass methodForSelector:objcColorSelector]; |
166 | 277 | id (*getUIColor)(id, SEL) = ((id(*)(id, SEL))imp); |
167 | 278 | 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]]) { |
169 | 316 | return colorObject; |
170 | 317 | } |
171 | 318 | } |
| 319 | +#endif |
172 | 320 | } |
173 | 321 | return nil; |
174 | 322 | } |
|
0 commit comments