From f4be8260baa1e90e24ea066d0d3b28896cd138ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Minh=20Nguye=CC=82=CC=83n?= Date: Fri, 26 Aug 2016 22:05:48 -0700 Subject: [PATCH] [ios, macos] Specialize color properties by platform MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Duplicate each color-typed property once per platform using conditional compilation macros. Let’s just hope we never have to throw CGColor into the mix. --- .../darwin/scripts/generate-style-code.js | 31 ++++++++--- platform/darwin/src/MGLBackgroundStyleLayer.h | 13 ++++- platform/darwin/src/MGLCircleStyleLayer.h | 11 +++- platform/darwin/src/MGLFillStyleLayer.h | 13 ++++- platform/darwin/src/MGLLineStyleLayer.h | 13 ++++- platform/darwin/src/MGLSymbolStyleLayer.h | 52 +++++++++++++++++-- 6 files changed, 119 insertions(+), 14 deletions(-) diff --git a/platform/darwin/scripts/generate-style-code.js b/platform/darwin/scripts/generate-style-code.js index cd4ab94e3..29040d3c5 100644 --- a/platform/darwin/scripts/generate-style-code.js +++ b/platform/darwin/scripts/generate-style-code.js @@ -150,15 +150,15 @@ global.describeValue = function (value, property, layerType) { throw new Error(`unrecognized color format in default value of ${property.name}`); } if (color.r === 0 && color.g === 0 && color.b === 0 && color.a === 0) { - return '`NSColor.clearColor` or `UIColor.clearColor`'; + return '`MGLColor.clearColor`'; } if (color.r === 0 && color.g === 0 && color.b === 0 && color.a === 1) { - return '`NSColor.blackColor` or `UIColor.blackColor`'; + return '`MGLColor.blackColor`'; } if (color.r === 1 && color.g === 1 && color.b === 1 && color.a === 1) { - return '`NSColor.whiteColor` or `UIColor.whiteColor`'; + return '`MGLColor.whiteColor`'; } - return 'an `NSColor` or `UIColor`' + ` object whose RGB value is ${color.r}, ${color.g}, ${color.b} and whose alpha value is ${color.a}`; + return 'an `MGLColor`' + ` object whose RGB value is ${color.r}, ${color.g}, ${color.b} and whose alpha value is ${color.a}`; case 'array': let units = property.units || ''; if (units) { @@ -310,8 +310,27 @@ const layers = spec.layer.type.values.map((type) => { }; }); -for (const layer of layers) { - fs.writeFileSync(`platform/darwin/src/${prefix}${camelize(layer.type)}${suffix}.h`, layerH(layer)); +function duplicatePlatformDecls(src) { + // Look for a documentation comment that contains “MGLColor” and the + // subsequent function, method, or property declaration. Try not to match + // greedily. + return src.replace(/(\/\*\*(?:\*[^\/]|[^*])*?\bMGLColor\b[\s\S]*?\*\/)(\s*.+?;)/g, + (match, comment, decl) => { + let iosComment = comment.replace(/\bMGLColor\b/g, 'UIColor') + // Use the correct indefinite article. + .replace(/\b(a)n(\s+`?UIColor)\b/gi, '$1$2'); + let macosComment = comment.replace(/\bMGLColor\b/g, 'NSColor'); + return `\ +#if TARGET_OS_IPHONE +${iosComment}${decl} +#else +${macosComment}${decl} +#endif`; + }); +} + +for (var layer of layers) { + fs.writeFileSync(`platform/darwin/src/${prefix}${camelize(layer.type)}${suffix}.h`, duplicatePlatformDecls(layerH(layer))); fs.writeFileSync(`platform/darwin/src/${prefix}${camelize(layer.type)}${suffix}.mm`, layerM(layer)); fs.writeFileSync(`platform/darwin/test/${prefix}${camelize(layer.type)}${suffix}Tests.m`, testLayers(layer)); } diff --git a/platform/darwin/src/MGLBackgroundStyleLayer.h b/platform/darwin/src/MGLBackgroundStyleLayer.h index 577767814..0d99b38b6 100644 --- a/platform/darwin/src/MGLBackgroundStyleLayer.h +++ b/platform/darwin/src/MGLBackgroundStyleLayer.h @@ -10,14 +10,25 @@ NS_ASSUME_NONNULL_BEGIN #pragma mark - Accessing the Paint Attributes +#if TARGET_OS_IPHONE /** The color with which the background will be drawn. - The default value of this property is `NSColor.blackColor` or `UIColor.blackColor`. Set this property to `nil` to reset it to the default value. + The default value of this property is `UIColor.blackColor`. Set this property to `nil` to reset it to the default value. This property is only applied to the style if `backgroundPattern` is set to `nil`. Otherwise, it is ignored. */ @property (nonatomic, null_resettable) id backgroundColor; +#else +/** + The color with which the background will be drawn. + + The default value of this property is `NSColor.blackColor`. Set this property to `nil` to reset it to the default value. + + This property is only applied to the style if `backgroundPattern` is set to `nil`. Otherwise, it is ignored. + */ +@property (nonatomic, null_resettable) id backgroundColor; +#endif /** Name of image in sprite to use for drawing an image background. For seamless patterns, image width and height must be a factor of two (2, 4, 8, ..., 512). diff --git a/platform/darwin/src/MGLCircleStyleLayer.h b/platform/darwin/src/MGLCircleStyleLayer.h index e489bd4e5..7d2ef0584 100644 --- a/platform/darwin/src/MGLCircleStyleLayer.h +++ b/platform/darwin/src/MGLCircleStyleLayer.h @@ -29,12 +29,21 @@ typedef NS_ENUM(NSUInteger, MGLCircleStyleLayerCirclePitchScale) { */ @property (nonatomic, null_resettable) id circleRadius; +#if TARGET_OS_IPHONE /** The color of the circle. - The default value of this property is `NSColor.blackColor` or `UIColor.blackColor`. Set this property to `nil` to reset it to the default value. + The default value of this property is `UIColor.blackColor`. Set this property to `nil` to reset it to the default value. */ @property (nonatomic, null_resettable) id circleColor; +#else +/** + The color of the circle. + + The default value of this property is `NSColor.blackColor`. Set this property to `nil` to reset it to the default value. + */ +@property (nonatomic, null_resettable) id circleColor; +#endif /** Amount to blur the circle. 1 blurs the circle such that only the centerpoint is full opacity. diff --git a/platform/darwin/src/MGLFillStyleLayer.h b/platform/darwin/src/MGLFillStyleLayer.h index bbf284833..c03e8ecae 100644 --- a/platform/darwin/src/MGLFillStyleLayer.h +++ b/platform/darwin/src/MGLFillStyleLayer.h @@ -29,14 +29,25 @@ typedef NS_ENUM(NSUInteger, MGLFillStyleLayerFillTranslateAnchor) { */ @property (nonatomic, null_resettable) id fillOpacity; +#if TARGET_OS_IPHONE /** The color of the filled part of this layer. This color can be specified as rgba with an alpha component and the color's opacity will not affect the opacity of the 1pt stroke, if it is used. - The default value of this property is `NSColor.blackColor` or `UIColor.blackColor`. Set this property to `nil` to reset it to the default value. + The default value of this property is `UIColor.blackColor`. Set this property to `nil` to reset it to the default value. This property is only applied to the style if `fillPattern` is set to `nil`. Otherwise, it is ignored. */ @property (nonatomic, null_resettable) id fillColor; +#else +/** + The color of the filled part of this layer. This color can be specified as rgba with an alpha component and the color's opacity will not affect the opacity of the 1pt stroke, if it is used. + + The default value of this property is `NSColor.blackColor`. Set this property to `nil` to reset it to the default value. + + This property is only applied to the style if `fillPattern` is set to `nil`. Otherwise, it is ignored. + */ +@property (nonatomic, null_resettable) id fillColor; +#endif /** The outline color of the fill. Matches the value of `fillColor` if unspecified. diff --git a/platform/darwin/src/MGLLineStyleLayer.h b/platform/darwin/src/MGLLineStyleLayer.h index a52664c53..435829c50 100644 --- a/platform/darwin/src/MGLLineStyleLayer.h +++ b/platform/darwin/src/MGLLineStyleLayer.h @@ -68,14 +68,25 @@ typedef NS_ENUM(NSUInteger, MGLLineStyleLayerLineTranslateAnchor) { */ @property (nonatomic, null_resettable) id lineOpacity; +#if TARGET_OS_IPHONE /** The color with which the line will be drawn. - The default value of this property is `NSColor.blackColor` or `UIColor.blackColor`. Set this property to `nil` to reset it to the default value. + The default value of this property is `UIColor.blackColor`. Set this property to `nil` to reset it to the default value. This property is only applied to the style if `linePattern` is set to `nil`. Otherwise, it is ignored. */ @property (nonatomic, null_resettable) id lineColor; +#else +/** + The color with which the line will be drawn. + + The default value of this property is `NSColor.blackColor`. Set this property to `nil` to reset it to the default value. + + This property is only applied to the style if `linePattern` is set to `nil`. Otherwise, it is ignored. + */ +@property (nonatomic, null_resettable) id lineColor; +#endif /** The geometry's offset. Values are [x, y] where negatives indicate left and up, respectively. diff --git a/platform/darwin/src/MGLSymbolStyleLayer.h b/platform/darwin/src/MGLSymbolStyleLayer.h index c8a7aff60..ef88ff996 100644 --- a/platform/darwin/src/MGLSymbolStyleLayer.h +++ b/platform/darwin/src/MGLSymbolStyleLayer.h @@ -400,23 +400,45 @@ typedef NS_ENUM(NSUInteger, MGLSymbolStyleLayerTextTranslateAnchor) { */ @property (nonatomic, null_resettable) id iconOpacity; +#if TARGET_OS_IPHONE /** The color of the icon. This can only be used with sdf icons. - The default value of this property is `NSColor.blackColor` or `UIColor.blackColor`. Set this property to `nil` to reset it to the default value. + The default value of this property is `UIColor.blackColor`. Set this property to `nil` to reset it to the default value. This property is only applied to the style if `iconImage` is non-`nil`. Otherwise, it is ignored. */ @property (nonatomic, null_resettable) id iconColor; +#else +/** + The color of the icon. This can only be used with sdf icons. + + The default value of this property is `NSColor.blackColor`. Set this property to `nil` to reset it to the default value. + This property is only applied to the style if `iconImage` is non-`nil`. Otherwise, it is ignored. + */ +@property (nonatomic, null_resettable) id iconColor; +#endif + +#if TARGET_OS_IPHONE /** The color of the icon's halo. Icon halos can only be used with sdf icons. - The default value of this property is `NSColor.clearColor` or `UIColor.clearColor`. Set this property to `nil` to reset it to the default value. + The default value of this property is `UIColor.clearColor`. Set this property to `nil` to reset it to the default value. This property is only applied to the style if `iconImage` is non-`nil`. Otherwise, it is ignored. */ @property (nonatomic, null_resettable) id iconHaloColor; +#else +/** + The color of the icon's halo. Icon halos can only be used with sdf icons. + + The default value of this property is `NSColor.clearColor`. Set this property to `nil` to reset it to the default value. + + This property is only applied to the style if `iconImage` is non-`nil`. Otherwise, it is ignored. + */ +@property (nonatomic, null_resettable) id iconHaloColor; +#endif /** Distance of halo to the icon outline. @@ -469,23 +491,45 @@ typedef NS_ENUM(NSUInteger, MGLSymbolStyleLayerTextTranslateAnchor) { */ @property (nonatomic, null_resettable) id textOpacity; +#if TARGET_OS_IPHONE /** The color with which the text will be drawn. - The default value of this property is `NSColor.blackColor` or `UIColor.blackColor`. Set this property to `nil` to reset it to the default value. + The default value of this property is `UIColor.blackColor`. Set this property to `nil` to reset it to the default value. This property is only applied to the style if `textField` is non-`nil`. Otherwise, it is ignored. */ @property (nonatomic, null_resettable) id textColor; +#else +/** + The color with which the text will be drawn. + + The default value of this property is `NSColor.blackColor`. Set this property to `nil` to reset it to the default value. + This property is only applied to the style if `textField` is non-`nil`. Otherwise, it is ignored. + */ +@property (nonatomic, null_resettable) id textColor; +#endif + +#if TARGET_OS_IPHONE /** The color of the text's halo, which helps it stand out from backgrounds. - The default value of this property is `NSColor.clearColor` or `UIColor.clearColor`. Set this property to `nil` to reset it to the default value. + The default value of this property is `UIColor.clearColor`. Set this property to `nil` to reset it to the default value. This property is only applied to the style if `textField` is non-`nil`. Otherwise, it is ignored. */ @property (nonatomic, null_resettable) id textHaloColor; +#else +/** + The color of the text's halo, which helps it stand out from backgrounds. + + The default value of this property is `NSColor.clearColor`. Set this property to `nil` to reset it to the default value. + + This property is only applied to the style if `textField` is non-`nil`. Otherwise, it is ignored. + */ +@property (nonatomic, null_resettable) id textHaloColor; +#endif /** Distance of halo to the font outline. Max text halo width is 1/4 of the font-size.